sysctl.c revision 85747
1/*
2 * Copyright (c) 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)from: sysctl.c	8.1 (Berkeley) 6/6/93";
43#endif
44static const char rcsid[] =
45  "$FreeBSD: head/sbin/sysctl/sysctl.c 85747 2001-10-30 20:15:32Z tobez $";
46#endif /* not lint */
47
48#include <sys/types.h>
49#include <sys/stat.h>
50#include <sys/sysctl.h>
51#include <sys/resource.h>
52
53#include <ctype.h>
54#include <err.h>
55#include <errno.h>
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
59#include <unistd.h>
60
61static int	aflag, bflag, eflag, Nflag, nflag, oflag, xflag;
62
63static int	oidfmt(int *, int, char *, u_int *);
64static void	parse(char *);
65static int	show_var(int *, int);
66static int	sysctl_all (int *oid, int len);
67static int	name2oid(char *, int *);
68
69static void
70usage(void)
71{
72
73	(void)fprintf(stderr, "%s\n%s\n",
74	    "usage: sysctl [-beNnox] variable[=value] ...",
75	    "       sysctl [-beNnox] -a");
76	exit(1);
77}
78
79int
80main(int argc, char **argv)
81{
82	int ch;
83	setbuf(stdout,0);
84	setbuf(stderr,0);
85
86	while ((ch = getopt(argc, argv, "AabeNnowxX")) != -1) {
87		switch (ch) {
88		case 'A':
89			/* compatibility */
90			aflag = oflag = 1;
91			break;
92		case 'a':
93			aflag = 1;
94			break;
95		case 'b':
96			bflag = 1;
97			break;
98		case 'e':
99			eflag = 1;
100			break;
101		case 'N':
102			Nflag = 1;
103			break;
104		case 'n':
105			nflag = 1;
106			break;
107		case 'o':
108			oflag = 1;
109			break;
110		case 'w':
111			/* compatibility */
112			/* ignored */
113			break;
114		case 'X':
115			/* compatibility */
116			aflag = xflag = 1;
117			break;
118		case 'x':
119			xflag = 1;
120			break;
121		default:
122			usage();
123		}
124	}
125	argc -= optind;
126	argv += optind;
127
128	if (Nflag && nflag)
129		usage();
130	if (aflag && argc == 0)
131		exit(sysctl_all(0, 0));
132	if (argc == 0)
133		usage();
134	while (argc-- > 0)
135		parse(*argv++);
136	exit(0);
137}
138
139/*
140 * Parse a name into a MIB entry.
141 * Lookup and print out the MIB entry if it exists.
142 * Set a new value if requested.
143 */
144static void
145parse(char *string)
146{
147	int len, i, j;
148	void *newval = 0;
149	int intval;
150	unsigned int uintval;
151	long longval;
152	unsigned long ulongval;
153	size_t newsize = 0;
154	quad_t quadval;
155	int mib[CTL_MAXNAME];
156	char *cp, *bufp, buf[BUFSIZ];
157	u_int kind;
158
159	bufp = buf;
160	snprintf(buf, BUFSIZ, "%s", string);
161	if ((cp = strchr(string, '=')) != NULL) {
162		*strchr(buf, '=') = '\0';
163		*cp++ = '\0';
164		while (isspace(*cp))
165			cp++;
166		newval = cp;
167		newsize = strlen(cp);
168	}
169	len = name2oid(bufp, mib);
170
171	if (len < 0)
172		errx(1, "unknown oid '%s'", bufp);
173
174	if (oidfmt(mib, len, 0, &kind))
175		err(1, "couldn't find format of oid '%s'", bufp);
176
177	if (newval == NULL) {
178		if ((kind & CTLTYPE) == CTLTYPE_NODE) {
179			sysctl_all(mib, len);
180		} else {
181			i = show_var(mib, len);
182			if (!i && !bflag)
183				putchar('\n');
184		}
185	} else {
186		if ((kind & CTLTYPE) == CTLTYPE_NODE)
187			errx(1, "oid '%s' isn't a leaf node", bufp);
188
189		if (!(kind&CTLFLAG_WR))
190			errx(1, "oid '%s' is read only", bufp);
191
192		switch (kind & CTLTYPE) {
193			case CTLTYPE_INT:
194				intval = (int) strtol(newval, NULL, 0);
195				newval = &intval;
196				newsize = sizeof(intval);
197				break;
198			case CTLTYPE_UINT:
199				uintval = (int) strtoul(newval, NULL, 0);
200				newval = &uintval;
201				newsize = sizeof uintval;
202				break;
203			case CTLTYPE_LONG:
204				longval = strtol(newval, NULL, 0);
205				newval = &longval;
206				newsize = sizeof longval;
207				break;
208			case CTLTYPE_ULONG:
209				ulongval = strtoul(newval, NULL, 0);
210				newval = &ulongval;
211				newsize = sizeof ulongval;
212				break;
213			case CTLTYPE_STRING:
214				break;
215			case CTLTYPE_QUAD:
216				break;
217				sscanf(newval, "%qd", &quadval);
218				newval = &quadval;
219				newsize = sizeof(quadval);
220				break;
221			default:
222				errx(1, "oid '%s' is type %d,"
223					" cannot set that", bufp,
224					kind & CTLTYPE);
225		}
226
227		i = show_var(mib, len);
228		if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
229			if (!i && !bflag)
230				putchar('\n');
231			switch (errno) {
232			case EOPNOTSUPP:
233				errx(1, "%s: value is not available",
234					string);
235			case ENOTDIR:
236				errx(1, "%s: specification is incomplete",
237					string);
238			case ENOMEM:
239				errx(1, "%s: type is unknown to this program",
240					string);
241			default:
242				warn("%s", string);
243				return;
244			}
245		}
246		if (!bflag)
247			printf(" -> ");
248		i = nflag;
249		nflag = 1;
250		j = show_var(mib, len);
251		if (!j && !bflag)
252			putchar('\n');
253		nflag = i;
254	}
255}
256
257/* These functions will dump out various interesting structures. */
258
259static int
260S_clockinfo(int l2, void *p)
261{
262	struct clockinfo *ci = (struct clockinfo*)p;
263	if (l2 != sizeof(*ci))
264		err(1, "S_clockinfo %d != %d", l2, sizeof(*ci));
265	printf("{ hz = %d, tick = %d, tickadj = %d, profhz = %d, stathz = %d }",
266		ci->hz, ci->tick, ci->tickadj, ci->profhz, ci->stathz);
267	return (0);
268}
269
270static int
271S_loadavg(int l2, void *p)
272{
273	struct loadavg *tv = (struct loadavg*)p;
274
275	if (l2 != sizeof(*tv))
276		err(1, "S_loadavg %d != %d", l2, sizeof(*tv));
277
278	printf("{ %.2f %.2f %.2f }",
279		(double)tv->ldavg[0]/(double)tv->fscale,
280		(double)tv->ldavg[1]/(double)tv->fscale,
281		(double)tv->ldavg[2]/(double)tv->fscale);
282	return (0);
283}
284
285static int
286S_timeval(int l2, void *p)
287{
288	struct timeval *tv = (struct timeval*)p;
289	time_t tv_sec;
290	char *p1, *p2;
291
292	if (l2 != sizeof(*tv))
293		err(1, "S_timeval %d != %d", l2, sizeof(*tv));
294	printf("{ sec = %ld, usec = %ld } ",
295		tv->tv_sec, tv->tv_usec);
296	tv_sec = tv->tv_sec;
297	p1 = strdup(ctime(&tv_sec));
298	for (p2=p1; *p2 ; p2++)
299		if (*p2 == '\n')
300			*p2 = '\0';
301	fputs(p1, stdout);
302	return (0);
303}
304
305static int
306T_dev_t(int l2, void *p)
307{
308	dev_t *d = (dev_t *)p;
309	if (l2 != sizeof(*d))
310		err(1, "T_dev_T %d != %d", l2, sizeof(*d));
311	if ((int)(*d) != -1) {
312		if (minor(*d) > 255 || minor(*d) < 0)
313			printf("{ major = %d, minor = 0x%x }",
314				major(*d), minor(*d));
315		else
316			printf("{ major = %d, minor = %d }",
317				major(*d), minor(*d));
318	}
319	return (0);
320}
321
322/*
323 * These functions uses a presently undocumented interface to the kernel
324 * to walk the tree and get the type so it can print the value.
325 * This interface is under work and consideration, and should probably
326 * be killed with a big axe by the first person who can find the time.
327 * (be aware though, that the proper interface isn't as obvious as it
328 * may seem, there are various conflicting requirements.
329 */
330
331static int
332name2oid(char *name, int *oidp)
333{
334	int oid[2];
335	int i;
336	size_t j;
337
338	oid[0] = 0;
339	oid[1] = 3;
340
341	j = CTL_MAXNAME * sizeof(int);
342	i = sysctl(oid, 2, oidp, &j, name, strlen(name));
343	if (i < 0)
344		return i;
345	j /= sizeof(int);
346	return (j);
347}
348
349static int
350oidfmt(int *oid, int len, char *fmt, u_int *kind)
351{
352	int qoid[CTL_MAXNAME+2];
353	u_char buf[BUFSIZ];
354	int i;
355	size_t j;
356
357	qoid[0] = 0;
358	qoid[1] = 4;
359	memcpy(qoid + 2, oid, len * sizeof(int));
360
361	j = sizeof(buf);
362	i = sysctl(qoid, len + 2, buf, &j, 0, 0);
363	if (i)
364		err(1, "sysctl fmt %d %d %d", i, j, errno);
365
366	if (kind)
367		*kind = *(u_int *)buf;
368
369	if (fmt)
370		strcpy(fmt, (char *)(buf + sizeof(u_int)));
371	return 0;
372}
373
374/*
375 * This formats and outputs the value of one variable
376 *
377 * Returns zero if anything was actually output.
378 * Returns one if didn't know what to do with this.
379 * Return minus one if we had errors.
380 */
381
382static int
383show_var(int *oid, int nlen)
384{
385	u_char buf[BUFSIZ], *val, *p;
386	char name[BUFSIZ], *fmt, *sep;
387	int qoid[CTL_MAXNAME+2];
388	int i;
389	size_t j, len;
390	u_int kind;
391	int (*func)(int, void *);
392
393	qoid[0] = 0;
394	memcpy(qoid + 2, oid, nlen * sizeof(int));
395
396	qoid[1] = 1;
397	j = sizeof(name);
398	i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
399	if (i || !j)
400		err(1, "sysctl name %d %d %d", i, j, errno);
401
402	if (Nflag) {
403		printf("%s", name);
404		return (0);
405	}
406
407	if (eflag)
408		sep = "=";
409	else
410		sep = ": ";
411
412	/* find an estimate of how much we need for this var */
413	j = 0;
414	i = sysctl(oid, nlen, 0, &j, 0, 0);
415	j += j; /* we want to be sure :-) */
416
417	val = alloca(j);
418	len = j;
419	i = sysctl(oid, nlen, val, &len, 0, 0);
420	if (i || !len)
421		return (1);
422
423	if (bflag) {
424		fwrite(val, 1, len, stdout);
425		return (0);
426	}
427
428	qoid[1] = 4;
429	j = sizeof(buf);
430	i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
431	if (i || !j)
432		err(1, "sysctl fmt %d %d %d", i, j, errno);
433
434	kind = *(u_int *)buf;
435
436	fmt = (char *)(buf + sizeof(u_int));
437
438	p = val;
439	switch (*fmt) {
440	case 'A':
441		if (!nflag)
442			printf("%s%s", name, sep);
443		printf("%s", p);
444		return (0);
445
446	case 'I':
447		if (!nflag)
448			printf("%s%s", name, sep);
449		fmt++;
450		val = "";
451		while (len >= sizeof(int)) {
452			if(*fmt == 'U')
453				printf("%s%u", val, *(unsigned int *)p);
454			else
455				printf("%s%d", val, *(int *)p);
456			val = " ";
457			len -= sizeof(int);
458			p += sizeof(int);
459		}
460		return (0);
461
462	case 'L':
463		if (!nflag)
464			printf("%s%s", name, sep);
465		fmt++;
466		val = "";
467		while (len >= sizeof(long)) {
468			if(*fmt == 'U')
469				printf("%s%lu", val, *(unsigned long *)p);
470			else
471				printf("%s%ld", val, *(long *)p);
472			val = " ";
473			len -= sizeof(long);
474			p += sizeof(long);
475		}
476		return (0);
477
478	case 'P':
479		if (!nflag)
480			printf("%s%s", name, sep);
481		printf("%p", *(void **)p);
482		return (0);
483
484	case 'T':
485	case 'S':
486		i = 0;
487		if (strcmp(fmt, "S,clockinfo") == 0)
488			func = S_clockinfo;
489		else if (strcmp(fmt, "S,timeval") == 0)
490			func = S_timeval;
491		else if (strcmp(fmt, "S,loadavg") == 0)
492			func = S_loadavg;
493		else if (strcmp(fmt, "T,dev_t") == 0)
494			func = T_dev_t;
495		else
496			func = NULL;
497		if (func) {
498			if (!nflag)
499				printf("%s%s", name, sep);
500			return ((*func)(len, p));
501		}
502		/* FALL THROUGH */
503	default:
504		if (!oflag && !xflag)
505			return (1);
506		if (!nflag)
507			printf("%s%s", name, sep);
508		printf("Format:%s Length:%d Dump:0x", fmt, len);
509		while (len-- && (xflag || p < val + 16))
510			printf("%02x", *p++);
511		if (!xflag && len > 16)
512			printf("...");
513		return (0);
514	}
515	return (1);
516}
517
518static int
519sysctl_all (int *oid, int len)
520{
521	int name1[22], name2[22];
522	int i, j;
523	size_t l1, l2;
524
525	name1[0] = 0;
526	name1[1] = 2;
527	l1 = 2;
528	if (len) {
529		memcpy(name1+2, oid, len * sizeof(int));
530		l1 += len;
531	} else {
532		name1[2] = 1;
533		l1++;
534	}
535	for (;;) {
536		l2 = sizeof(name2);
537		j = sysctl(name1, l1, name2, &l2, 0, 0);
538		if (j < 0) {
539			if (errno == ENOENT)
540				return 0;
541			else
542				err(1, "sysctl(getnext) %d %d", j, l2);
543		}
544
545		l2 /= sizeof(int);
546
547		if (l2 < len)
548			return 0;
549
550		for (i = 0; i < len; i++)
551			if (name2[i] != oid[i])
552				return 0;
553
554		i = show_var(name2, l2);
555		if (!i && !bflag)
556			putchar('\n');
557
558		memcpy(name1+2, name2, l2 * sizeof(int));
559		l1 = 2 + l2;
560	}
561}
562