xref: /openbsd-src/distrib/special/sysctl/sysctl.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: sysctl.c,v 1.7 2010/11/23 12:37:25 halex 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 	struct iovec iov[2];
75 	char *p;
76 	size_t len;
77 
78 	if (sysctl(v->mib, v->nmib, NULL, &len, NULL, 0) != -1)
79 		if ((p = malloc(len)) != NULL)
80 			if (sysctl(v->mib, v->nmib, p, &len, NULL, 0) != -1) {
81 				if (nflag == 0)
82 					printf("%s=", v->name);
83 				printf("%s\n", p);
84 				return (0);
85 			}
86 	return (1);
87 }
88 
89 int
90 main(int argc, char *argv[])
91 {
92 	int ch, i;
93 
94 	while ((ch = getopt(argc, argv, "n")) != -1) {
95 		switch (ch) {
96 		case 'n':
97 			nflag = 1;
98 			break;
99 		default:
100 			fprintf(stderr, "usage: sysctl [-n] name\n");
101 			exit(1);
102 		}
103 	}
104 
105 	argc -= optind;
106 	argv += optind;
107 
108 	if (argc == 0) {
109 		for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++)
110 			(vars[i].print)(&vars[i]);
111 		exit(0);
112 	}
113 
114 	while (argc--) {
115 		name = *argv++;
116 
117 		for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++) {
118 			if (strcmp(name, vars[i].name) == 0) {
119 				(vars[i].print)(&vars[i]);
120 				break;
121 			}
122 		}
123 	}
124 
125 	exit(0);
126 }
127