summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>2009-04-19 00:57:45 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>2009-04-19 00:57:45 +0000
commit2e748f2a763c37516476f31741fc6badd09c05d7 (patch)
tree57d7b8838c30b4dcbce922e23a4edb71d33a295f
parent234054217345300ca7229d3406512a711d2b8628 (diff)
A mini sysctl(8) that only knows a few variables we need to know in
the install script. For now, just hw.disknames
-rw-r--r--distrib/special/sysctl/Makefile10
-rw-r--r--distrib/special/sysctl/sysctl.c115
2 files changed, 125 insertions, 0 deletions
diff --git a/distrib/special/sysctl/Makefile b/distrib/special/sysctl/Makefile
new file mode 100644
index 00000000000..7ae2abed16b
--- /dev/null
+++ b/distrib/special/sysctl/Makefile
@@ -0,0 +1,10 @@
+# $OpenBSD: Makefile,v 1.1 2009/04/19 00:57:44 deraadt Exp $
+
+PROG= sysctl
+SRCS= sysctl.c
+CFLAGS+= -Os
+MAN=
+
+.PATH: ${.CURDIR}/../../../distrib/special/sysctl
+
+.include <bsd.prog.mk>
diff --git a/distrib/special/sysctl/sysctl.c b/distrib/special/sysctl/sysctl.c
new file mode 100644
index 00000000000..42c72e44203
--- /dev/null
+++ b/distrib/special/sysctl/sysctl.c
@@ -0,0 +1,115 @@
+/* $OpenBSD: sysctl.c,v 1.1 2009/04/19 00:57:44 deraadt Exp $ */
+
+/*
+ * Copyright (c) 2007 Kenneth R. Westerback <krw@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+#include <sys/param.h>
+#include <sys/sysctl.h>
+#include <sys/uio.h>
+
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+struct var {
+ char *name;
+ int (*print)(struct var *);
+ int nmib;
+ int mib[3];
+};
+
+int pstring(struct var *);
+int pint(struct var *);
+
+struct var vars[] = {
+ { "hw.disknames", pstring, 2,
+ { CTL_HW, HW_DISKNAMES }},
+};
+
+int nflag;
+char *name;
+
+int
+pint(struct var *v)
+{
+ int n;
+ size_t len = sizeof(n);
+
+ if (sysctl(v->mib, v->nmib, &n, &len, NULL, 0) != -1) {
+ if (nflag == 0)
+ printf("%s=", v->name);
+ printf("%d\n", n);
+ return (0);
+ }
+ return (1);
+}
+
+int
+pstring(struct var *v)
+{
+ struct iovec iov[2];
+ char *p;
+ size_t len;
+
+ if (sysctl(v->mib, v->nmib, NULL, &len, NULL, 0) != -1)
+ if ((p = malloc(len)) != NULL)
+ if (sysctl(v->mib, v->nmib, p, &len, NULL, 0) != -1) {
+ if (nflag == 0)
+ printf("%s=", v->name);
+ fwrite(p, len, 1, stdout);
+ printf("\n");
+ return (0);
+ }
+ return (1);
+}
+
+int
+main(int argc, char *argv[])
+{
+ int ch, i;
+
+ while ((ch = getopt(argc, argv, "n")) != -1) {
+ switch (ch) {
+ case 'n':
+ nflag = 1;
+ break;
+ default:
+ fprintf(stderr, "usage: sysctl [-n] name\n");
+ exit(1);
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if (argc == 0) {
+ for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++)
+ (vars[i].print)(&vars[i]);
+ exit(0);
+ }
+
+ while (argc--) {
+ name = *argv++;
+
+ for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++) {
+ if (strcmp(name, vars[i].name) == 0) {
+ (vars[i].print)(&vars[i]);
+ break;
+ }
+ }
+ }
+
+ exit(0);
+}