xref: /openbsd-src/distrib/special/sysctl/sysctl.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: sysctl.c,v 1.9 2015/01/16 06:39:34 deraadt 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 
20 #include <sys/types.h>
21 #include <sys/sysctl.h>
22 #include <sys/uio.h>
23 
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 struct var {
30 	char *name;
31 	int (*print)(struct var *);
32 	int nmib;
33 	int mib[3];
34 };
35 
36 int	pstring(struct var *);
37 int	pint(struct var *);
38 
39 struct var vars[] = {
40 	{ "kern.osrelease", pstring, 2,
41 	    { CTL_KERN, KERN_OSRELEASE }},
42 	{ "hw.machine", pstring, 2,
43 	    { CTL_HW, HW_MACHINE }},
44 	{ "hw.model", pstring, 2,
45 	    { CTL_HW, HW_MODEL }},
46 	{ "hw.product", pstring, 2,
47 	    { CTL_HW, HW_PRODUCT }},
48 	{ "hw.disknames", pstring, 2,
49 	    { CTL_HW, HW_DISKNAMES }},
50 	{ "hw.ncpufound", pint, 2,
51 	    { CTL_HW, HW_NCPUFOUND }},
52 };
53 
54 int	nflag;
55 char	*name;
56 
57 int
58 pint(struct var *v)
59 {
60 	int n;
61 	size_t len = sizeof(n);
62 
63 	if (sysctl(v->mib, v->nmib, &n, &len, NULL, 0) != -1) {
64 		if (nflag == 0)
65 			printf("%s=", v->name);
66 		printf("%d\n", n);
67 		return (0);
68 	}
69 	return (1);
70 }
71 
72 int
73 pstring(struct var *v)
74 {
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