summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorjoerg <joerg@NetBSD.org>2014-06-13 15:45:05 +0000
committerjoerg <joerg@NetBSD.org>2014-06-13 15:45:05 +0000
commitaa674fe70430929668eeca2dfd3842d3d981150f (patch)
treede4ea9de01ef804d998a31eff57e07919b3257eb /lib
parent5b008e80e29994b4a8fecccf87a90338b3f8842b (diff)
Add asysctl(3) and asysctlbyname(3) wrappers for the common idiom of
fetching dynamically sized data via sysctl.
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/gen/Makefile.inc6
-rw-r--r--lib/libc/gen/asysctl.c92
-rw-r--r--lib/libc/gen/sysctl.324
-rw-r--r--lib/libc/include/namespace.h4
-rw-r--r--lib/libc/shlib_version4
5 files changed, 121 insertions, 9 deletions
diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc
index 79abbd4668d..c90fdf331e4 100644
--- a/lib/libc/gen/Makefile.inc
+++ b/lib/libc/gen/Makefile.inc
@@ -1,11 +1,11 @@
-# $NetBSD: Makefile.inc,v 1.187 2014/01/16 20:31:42 christos Exp $
+# $NetBSD: Makefile.inc,v 1.188 2014/06/13 15:45:05 joerg Exp $
# from: @(#)Makefile.inc 8.6 (Berkeley) 5/4/95
# gen sources
.PATH: ${ARCHDIR}/gen ${.CURDIR}/gen
-SRCS+= _errno.c alarm.c alphasort.c arc4random.c assert.c basename.c clock.c \
- closedir.c closefrom.c \
+SRCS+= _errno.c alarm.c alphasort.c arc4random.c assert.c asysctl.c \
+ basename.c clock.c closedir.c closefrom.c \
confstr.c ctermid.c ctype_.c daemon.c \
dehumanize_number.c devname.c dirname.c disklabel.c err.c errx.c \
errc.c errlist.c errno.c execl.c execle.c execlp.c execv.c execvp.c \
diff --git a/lib/libc/gen/asysctl.c b/lib/libc/gen/asysctl.c
new file mode 100644
index 00000000000..c5201c699f5
--- /dev/null
+++ b/lib/libc/gen/asysctl.c
@@ -0,0 +1,92 @@
+/* $NetBSD: asysctl.c,v 1.1 2014/06/13 15:45:05 joerg Exp $ */
+
+/*-
+ * Copyright (c) 2014 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Joerg Sonnenberger.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: asysctl.c,v 1.1 2014/06/13 15:45:05 joerg Exp $");
+
+#include "namespace.h"
+#include <sys/sysctl.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdlib.h>
+
+__weak_alias(asysctl,_asysctl)
+__weak_alias(asysctlbyname,_asysctlbyname)
+
+void *
+asysctl(const int *oids, size_t oidlen, size_t *len)
+{
+ void *data;
+
+ *len = 0;
+ data = NULL;
+
+ for (;;) {
+ if (sysctl(oids, oidlen, data, len, NULL, 0) == 0) {
+ if (*len == 0) {
+ free(data);
+ return NULL;
+ }
+ if (data != NULL)
+ return data;
+ errno = ENOMEM;
+ }
+ free(data);
+ if (errno == ENOMEM && *len != SIZE_MAX) {
+ if (*len > SIZE_MAX / 2)
+ *len = SIZE_MAX;
+ else
+ *len *= 2;
+ data = malloc(*len);
+ if (data == NULL) {
+ *len = SIZE_MAX;
+ return NULL;
+ }
+ continue;
+ }
+ *len = SIZE_MAX;
+ return NULL;
+ }
+}
+
+void *
+asysctlbyname(const char *gname, size_t *len)
+{
+ int name[CTL_MAXNAME];
+ u_int namelen;
+
+ if (sysctlgetmibinfo(gname, &name[0], &namelen, NULL, NULL, NULL,
+ SYSCTL_VERSION)) {
+ *len = SIZE_MAX;
+ return NULL;
+ }
+ return asysctl(&name[0], namelen, len);
+}
diff --git a/lib/libc/gen/sysctl.3 b/lib/libc/gen/sysctl.3
index 79da5557dd0..1f8a09dca23 100644
--- a/lib/libc/gen/sysctl.3
+++ b/lib/libc/gen/sysctl.3
@@ -1,4 +1,4 @@
-.\" $NetBSD: sysctl.3,v 1.200 2010/03/22 19:30:54 joerg Exp $
+.\" $NetBSD: sysctl.3,v 1.201 2014/06/13 15:45:05 joerg Exp $
.\"
.\" Copyright (c) 1993
.\" The Regents of the University of California. All rights reserved.
@@ -29,14 +29,16 @@
.\"
.\" @(#)sysctl.3 8.4 (Berkeley) 5/9/95
.\"
-.Dd September 26, 2009
+.Dd June 13, 2014
.Dt SYSCTL 3
.Os
.Sh NAME
.Nm sysctl ,
.Nm sysctlbyname ,
.Nm sysctlgetmibinfo ,
-.Nm sysctlnametomib
+.Nm sysctlnametomib ,
+.Nm asysctl ,
+.Nm asysctlbyname
.Nd get or set system information
.Sh LIBRARY
.Lb libc
@@ -54,6 +56,10 @@
"char *cname" "size_t *csz" "struct sysctlnode **rnode" "int v"
.Ft int
.Fn sysctlnametomib "const char *sname" "int *name" "size_t *namelenp"
+.Ft void *
+.Fn asysctl "const int *name" "size_t namelen" "size_t *len"
+.Ft void *
+.Fn asysctlbyname "const char *sname" "size_t *len"
.Sh DESCRIPTION
The
.Nm
@@ -250,6 +256,18 @@ sysctl(mib, 2, NULL, \*[Am]len, NULL, 0);
p = malloc(len);
sysctl(mib, 2, p, \*[Am]len, NULL, 0);
.Ed
+.Pp
+The
+.Fn asysctl
+and
+.Fn asysctlbyname
+functions are wrappers for
+.Fn sysctl
+and
+.Fn sysctlbyname .
+They return memory allocated with
+.Xr malloc 3
+and resize the buffer in a loop until all data fits.
.Sh DYNAMIC OPERATIONS
Several meta-identifiers are provided to perform operations on the
.Nm
diff --git a/lib/libc/include/namespace.h b/lib/libc/include/namespace.h
index 7eef24ba6e0..ffe73381cd7 100644
--- a/lib/libc/include/namespace.h
+++ b/lib/libc/include/namespace.h
@@ -1,4 +1,4 @@
-/* $NetBSD: namespace.h,v 1.173 2014/01/16 21:02:30 christos Exp $ */
+/* $NetBSD: namespace.h,v 1.174 2014/06/13 15:45:05 joerg Exp $ */
/*-
* Copyright (c) 1997-2004 The NetBSD Foundation, Inc.
@@ -175,6 +175,8 @@
#define asctime_r _asctime_r
#define asprintf _asprintf
#define asprintf_l _asprintf_l
+#define asysctl _asysctl
+#define asysctlbyname _asysctlbyname
#define atoll _atoll
#define authnone_create _authnone_create
#define authunix_create _authunix_create
diff --git a/lib/libc/shlib_version b/lib/libc/shlib_version
index a0f2a53f86e..ecabc4802df 100644
--- a/lib/libc/shlib_version
+++ b/lib/libc/shlib_version
@@ -1,4 +1,4 @@
-# $NetBSD: shlib_version,v 1.250 2014/03/29 19:30:12 dholland Exp $
+# $NetBSD: shlib_version,v 1.251 2014/06/13 15:45:05 joerg Exp $
# Remember to update distrib/sets/lists/base/shl.* when changing
#
# things we wish to do on next major version bump:
@@ -41,4 +41,4 @@
# - redo stdin/stdout/stderr to not require copy relocations
# - move gethostbyname to a compat library
major=12
-minor=190
+minor=191