xref: /openbsd-src/distrib/special/sysctl/sysctl.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: sysctl.c,v 1.8 2014/05/03 00:27:19 chl 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 	{ "kern.osrelease", pstring, 2,
40 	    { CTL_KERN, KERN_OSRELEASE }},
41 	{ "hw.machine", pstring, 2,
42 	    { CTL_HW, HW_MACHINE }},
43 	{ "hw.model", pstring, 2,
44 	    { CTL_HW, HW_MODEL }},
45 	{ "hw.product", pstring, 2,
46 	    { CTL_HW, HW_PRODUCT }},
47 	{ "hw.disknames", pstring, 2,
48 	    { CTL_HW, HW_DISKNAMES }},
49 	{ "hw.ncpufound", pint, 2,
50 	    { CTL_HW, HW_NCPUFOUND }},
51 };
52 
53 int	nflag;
54 char	*name;
55 
56 int
57 pint(struct var *v)
58 {
59 	int n;
60 	size_t len = sizeof(n);
61 
62 	if (sysctl(v->mib, v->nmib, &n, &len, NULL, 0) != -1) {
63 		if (nflag == 0)
64 			printf("%s=", v->name);
65 		printf("%d\n", n);
66 		return (0);
67 	}
68 	return (1);
69 }
70 
71 int
72 pstring(struct var *v)
73 {
74 	char *p;
75 	size_t len;
76 
77 	if (sysctl(v->mib, v->nmib, NULL, &len, NULL, 0) != -1)
78 		if ((p = malloc(len)) != NULL)
79 			if (sysctl(v->mib, v->nmib, p, &len, NULL, 0) != -1) {
80 				if (nflag == 0)
81 					printf("%s=", v->name);
82 				printf("%s\n", p);
83 				return (0);
84 			}
85 	return (1);
86 }
87 
88 int
89 main(int argc, char *argv[])
90 {
91 	int ch, i;
92 
93 	while ((ch = getopt(argc, argv, "n")) != -1) {
94 		switch (ch) {
95 		case 'n':
96 			nflag = 1;
97 			break;
98 		default:
99 			fprintf(stderr, "usage: sysctl [-n] name\n");
100 			exit(1);
101 		}
102 	}
103 
104 	argc -= optind;
105 	argv += optind;
106 
107 	if (argc == 0) {
108 		for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++)
109 			(vars[i].print)(&vars[i]);
110 		exit(0);
111 	}
112 
113 	while (argc--) {
114 		name = *argv++;
115 
116 		for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++) {
117 			if (strcmp(name, vars[i].name) == 0) {
118 				(vars[i].print)(&vars[i]);
119 				break;
120 			}
121 		}
122 	}
123 
124 	exit(0);
125 }
126