xref: /openbsd-src/distrib/special/sysctl/sysctl.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: sysctl.c,v 1.4 2009/04/20 16:10:39 jasper Exp $	*/
2 
3 /*
4  * Copyright (c) 2009 Theo de Raadt <deraadt@openbsd.org>
5  * Copyright (c) 2007 Kenneth R. Westerback <krw@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 #include <sys/param.h>
20 #include <sys/sysctl.h>
21 #include <sys/uio.h>
22 
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 struct var {
29 	char *name;
30 	int (*print)(struct var *);
31 	int nmib;
32 	int mib[3];
33 };
34 
35 int	pstring(struct var *);
36 int	pint(struct var *);
37 
38 struct var vars[] = {
39 	{ "hw.model", pstring, 2,
40 	    { CTL_HW, HW_MODEL }},
41 	{ "hw.disknames", pstring, 2,
42 	    { CTL_HW, HW_DISKNAMES }},
43 	{ "hw.ncpufound", pint, 2,
44 	    { CTL_HW, HW_NCPUFOUND }},
45 };
46 
47 int	nflag;
48 char	*name;
49 
50 int
51 pint(struct var *v)
52 {
53 	int n;
54 	size_t len = sizeof(n);
55 
56 	if (sysctl(v->mib, v->nmib, &n, &len, NULL, 0) != -1) {
57 		if (nflag == 0)
58 			printf("%s=", v->name);
59 		printf("%d\n", n);
60 		return (0);
61 	}
62 	return (1);
63 }
64 
65 int
66 pstring(struct var *v)
67 {
68 	struct iovec iov[2];
69 	char *p;
70 	size_t len;
71 
72 	if (sysctl(v->mib, v->nmib, NULL, &len, NULL, 0) != -1)
73 		if ((p = malloc(len)) != NULL)
74 			if (sysctl(v->mib, v->nmib, p, &len, NULL, 0) != -1) {
75 				if (nflag == 0)
76 					printf("%s=", v->name);
77 				fwrite(p, len, 1, stdout);
78 				printf("\n");
79 				return (0);
80 			}
81 	return (1);
82 }
83 
84 int
85 main(int argc, char *argv[])
86 {
87 	int ch, i;
88 
89 	while ((ch = getopt(argc, argv, "n")) != -1) {
90 		switch (ch) {
91 		case 'n':
92 			nflag = 1;
93 			break;
94 		default:
95 			fprintf(stderr, "usage: sysctl [-n] name\n");
96 			exit(1);
97 		}
98 	}
99 
100 	argc -= optind;
101 	argv += optind;
102 
103 	if (argc == 0) {
104 		for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++)
105 			(vars[i].print)(&vars[i]);
106 		exit(0);
107 	}
108 
109 	while (argc--) {
110 		name = *argv++;
111 
112 		for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++) {
113 			if (strcmp(name, vars[i].name) == 0) {
114 				(vars[i].print)(&vars[i]);
115 				break;
116 			}
117 		}
118 	}
119 
120 	exit(0);
121 }
122