sysctl.c revision 1.63 1 /* $NetBSD: sysctl.c,v 1.63 2003/01/22 17:12:41 dsl Exp $ */
2
3 /*
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT(
39 "@(#) Copyright (c) 1993\n\
40 The Regents of the University of California. All rights reserved.\n");
41 #endif /* not lint */
42
43 #ifndef lint
44 #if 0
45 static char sccsid[] = "@(#)sysctl.c 8.1 (Berkeley) 6/6/93";
46 #else
47 __RCSID("$NetBSD: sysctl.c,v 1.63 2003/01/22 17:12:41 dsl Exp $");
48 #endif
49 #endif /* not lint */
50
51 #include <sys/param.h>
52 #include <sys/gmon.h>
53 #include <sys/stat.h>
54 #include <sys/sysctl.h>
55 #include <sys/socket.h>
56 #include <sys/mount.h>
57 #include <sys/mbuf.h>
58 #include <sys/resource.h>
59 #include <uvm/uvm_param.h>
60 #include <machine/cpu.h>
61
62 #include <ufs/ufs/dinode.h>
63 #include <ufs/ufs/dir.h>
64 #include <ufs/ffs/fs.h>
65 #include <ufs/ffs/ffs_extern.h>
66
67 #include <nfs/rpcv2.h>
68 #include <nfs/nfsproto.h>
69 #include <nfs/nfs.h>
70
71 #include <netinet/in.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_icmp.h>
75 #include <netinet/icmp_var.h>
76 #include <netinet/ip_var.h>
77 #include <netinet/udp.h>
78 #include <netinet/udp_var.h>
79 #include <netinet/tcp.h>
80 #include <netinet/tcp_timer.h>
81 #include <netinet/tcp_var.h>
82
83 #ifdef INET6
84 #include <netinet/ip6.h>
85 #include <netinet/icmp6.h>
86 #include <netinet6/ip6_var.h>
87 #include <netinet6/udp6.h>
88 #include <netinet6/udp6_var.h>
89 #ifdef TCP6
90 #include <netinet6/tcp6.h>
91 #include <netinet6/tcp6_timer.h>
92 #include <netinet6/tcp6_var.h>
93 #endif
94 #include <netinet6/pim6_var.h>
95 #endif /* INET6 */
96
97 #include "../../sys/compat/linux/common/linux_exec.h"
98 #include "../../sys/compat/irix/irix_sysctl.h"
99 #include "../../sys/compat/darwin/darwin_sysctl.h"
100
101 #ifdef IPSEC
102 #include <net/route.h>
103 #include <netinet6/ipsec.h>
104 #include <netkey/key_var.h>
105 #endif /* IPSEC */
106
107 #include <sys/pipe.h>
108
109 #include <err.h>
110 #include <ctype.h>
111 #include <errno.h>
112 #include <stdio.h>
113 #include <stdlib.h>
114 #include <string.h>
115 #include <unistd.h>
116
117 struct ctlname topname[] = CTL_NAMES;
118 struct ctlname kernname[] = CTL_KERN_NAMES;
119 struct ctlname vmname[] = CTL_VM_NAMES;
120 struct ctlname vfsname[] = CTL_VFS_NAMES;
121 struct ctlname netname[] = CTL_NET_NAMES;
122 struct ctlname hwname[] = CTL_HW_NAMES;
123 struct ctlname username[] = CTL_USER_NAMES;
124 struct ctlname ddbname[] = CTL_DDB_NAMES;
125 struct ctlname debugname[CTL_DEBUG_MAXID];
126 #ifdef CTL_MACHDEP_NAMES
127 struct ctlname machdepname[] = CTL_MACHDEP_NAMES;
128 #endif
129 struct ctlname emulname[] = CTL_EMUL_NAMES;
130 struct ctlname vendorname[] = { { 0, 0 } };
131
132 /* this one is dummy, it's used only for '-a' or '-A' */
133 struct ctlname procname[] = { {0, 0}, {"curproc", CTLTYPE_NODE} };
134
135 char names[BUFSIZ];
136
137 struct list {
138 struct ctlname *list;
139 int size;
140 };
141 struct list toplist = { topname, CTL_MAXID };
142 struct list secondlevel[] = {
143 { 0, 0 }, /* CTL_UNSPEC */
144 { kernname, KERN_MAXID }, /* CTL_KERN */
145 { vmname, VM_MAXID }, /* CTL_VM */
146 { vfsname, VFS_MAXID }, /* CTL_VFS */
147 { netname, NET_MAXID }, /* CTL_NET */
148 { 0, CTL_DEBUG_MAXID }, /* CTL_DEBUG */
149 { hwname, HW_MAXID }, /* CTL_HW */
150 #ifdef CTL_MACHDEP_NAMES
151 { machdepname, CPU_MAXID }, /* CTL_MACHDEP */
152 #else
153 { 0, 0 }, /* CTL_MACHDEP */
154 #endif
155 { username, USER_MAXID }, /* CTL_USER_NAMES */
156 { ddbname, DDBCTL_MAXID }, /* CTL_DDB_NAMES */
157 { procname, 2 }, /* dummy name */
158 { vendorname, 0 }, /* CTL_VENDOR_NAMES */
159 { emulname, EMUL_MAXID }, /* CTL_EMUL_NAMES */
160
161 { 0, 0},
162 };
163
164 int Aflag, aflag, nflag, qflag, wflag;
165
166 /*
167 * Variables requiring special processing.
168 */
169 #define CLOCK 0x00000001
170 #define BOOTTIME 0x00000002
171 #define CONSDEV 0x00000004
172 #define DISKINFO 0x00000008
173 #define CPTIME 0x00000010
174
175 /*
176 * A dummy type for limits, which requires special parsing
177 */
178 #define CTLTYPE_LIMIT ((~0x1) << 31)
179
180 int main(int, char *[]);
181
182 static void listall(const char *, struct list *);
183 static void parse(char *, int);
184 static void debuginit(void);
185 static int sysctl_inet(char *, char **, int[], int, int *);
186 #ifdef INET6
187 static int sysctl_inet6(char *, char **, int[], int, int *);
188 #endif
189 static int sysctl_vfs(char *, char **, int[], int, int *);
190 static int sysctl_proc(char *, char **, int[], int, int *);
191 static int sysctl_3rd(struct list *, char *, char **, int[], int, int *);
192
193 #ifdef IPSEC
194 struct ctlname keynames[] = KEYCTL_NAMES;
195 struct list keyvars = { keynames, KEYCTL_MAXID };
196 #endif /*IPSEC*/
197 struct ctlname vfsgenname[] = CTL_VFSGENCTL_NAMES;
198 struct list vfsgenvars = { vfsgenname, VFSGEN_MAXID };
199 struct ctlname mbufnames[] = CTL_MBUF_NAMES;
200 struct list mbufvars = { mbufnames, MBUF_MAXID };
201 struct ctlname pipenames[] = CTL_PIPE_NAMES;
202 struct list pipevars = { pipenames, KERN_PIPE_MAXID };
203 struct ctlname tkstatnames[] = KERN_TKSTAT_NAMES;
204 struct list tkstatvars = { tkstatnames, KERN_TKSTAT_MAXID };
205
206 static int sysctl_linux(char *, char **, int[], int, int *);
207 static int sysctl_irix(char *, char **, int[], int, int *);
208 static int sysctl_darwin(char *, char **, int[], int, int *);
209 static int findname(char *, char *, char **, struct list *);
210 static void usage(void);
211
212 #define USEAPP(s, a) \
213 if (flags) printf("%s: use '%s' to view this information\n", s, a)
214
215
216 int
217 main(int argc, char *argv[])
218 {
219 char *fn = NULL;
220 int ch, lvl1;
221
222 while ((ch = getopt(argc, argv, "Aaf:nqw")) != -1) {
223 switch (ch) {
224
225 case 'A':
226 Aflag = 1;
227 break;
228
229 case 'a':
230 aflag = 1;
231 break;
232
233 case 'f':
234 fn = optarg;
235 wflag = 1;
236 break;
237
238 case 'n':
239 nflag = 1;
240 break;
241
242 case 'q':
243 qflag = 1;
244 break;
245
246 case 'w':
247 wflag = 1;
248 break;
249
250 default:
251 usage();
252 }
253 }
254
255 if (qflag && !wflag)
256 usage();
257
258 argc -= optind;
259 argv += optind;
260
261 if (Aflag || aflag) {
262 debuginit();
263 for (lvl1 = 1; lvl1 < CTL_MAXID; lvl1++)
264 listall(topname[lvl1].ctl_name, &secondlevel[lvl1]);
265 return 0;
266 }
267
268 if (fn) {
269 FILE *fp;
270 char *l;
271
272 fp = fopen(fn, "r");
273 if (fp == NULL) {
274 err(1, "%s", fn);
275 } else {
276 for (; (l = fparseln(fp, NULL, NULL, NULL, 0)) != NULL;
277 free(l)) {
278 if (*l)
279 parse(l, 1);
280 }
281 fclose(fp);
282 }
283 } else {
284 if (argc == 0)
285 usage();
286 while (argc-- > 0)
287 parse(*argv++, 1);
288 }
289 return 0;
290 }
291
292 /*
293 * List all variables known to the system.
294 */
295 static void
296 listall(const char *prefix, struct list *lp)
297 {
298 int lvl2;
299 char *cp, name[BUFSIZ];
300
301 if (lp->list == 0)
302 return;
303 strcpy(name, prefix);
304 cp = &name[strlen(name)];
305 *cp++ = '.';
306 for (lvl2 = 0; lvl2 < lp->size; lvl2++) {
307 if (lp->list[lvl2].ctl_name == 0)
308 continue;
309 strcpy(cp, lp->list[lvl2].ctl_name);
310 parse(name, Aflag);
311 }
312 }
313
314 /*
315 * Parse a name into a MIB entry.
316 * Lookup and print out the MIB entry if it exists.
317 * Set a new value if requested.
318 */
319 static void
320 parse(char *string, int flags)
321 {
322 int indx, type, state, len;
323 int special = 0;
324 void *newval = 0;
325 int intval, newsize = 0;
326 quad_t quadval;
327 size_t size;
328 struct list *lp;
329 int mib[CTL_MAXNAME];
330 char *cp, *bufp, buf[BUFSIZ];
331 double loads[3];
332
333 bufp = buf;
334 snprintf(buf, BUFSIZ, "%s", string);
335 if ((cp = strchr(string, '=')) != NULL) {
336 if (!wflag)
337 errx(2, "Must specify -w to set variables");
338 *strchr(buf, '=') = '\0';
339 *cp++ = '\0';
340 while (isspace((unsigned char) *cp))
341 cp++;
342 newval = cp;
343 newsize = strlen(cp);
344 }
345 if ((indx = findname(string, "top", &bufp, &toplist)) == -1)
346 return;
347 mib[0] = indx;
348 if (indx == CTL_DEBUG)
349 debuginit();
350 if (mib[0] == CTL_PROC) {
351 type = CTLTYPE_NODE;
352 len = 1;
353 } else {
354 lp = &secondlevel[indx];
355 if (lp->list == 0) {
356 warnx("Class `%s' is not implemented",
357 topname[indx].ctl_name);
358 return;
359 }
360 if (bufp == NULL) {
361 listall(topname[indx].ctl_name, lp);
362 return;
363 }
364 if ((indx = findname(string, "second", &bufp, lp)) == -1)
365 return;
366 mib[1] = indx;
367 type = lp->list[indx].ctl_type;
368 len = 2;
369 }
370 switch (mib[0]) {
371
372 case CTL_KERN:
373 switch (mib[1]) {
374 case KERN_PROF:
375 mib[2] = GPROF_STATE;
376 size = sizeof state;
377 if (sysctl(mib, 3, &state, &size, NULL, 0) < 0) {
378 if (flags == 0)
379 return;
380 if (!nflag)
381 printf("%s: ", string);
382 printf(
383 "kernel is not compiled for profiling\n");
384 return;
385 }
386 if (!nflag)
387 printf("%s: %s\n", string,
388 state == GMON_PROF_OFF ? "off" : "running");
389 return;
390 case KERN_VNODE:
391 case KERN_FILE:
392 USEAPP(string, "pstat");
393 return;
394 case KERN_PROC:
395 case KERN_PROC2:
396 case KERN_PROC_ARGS:
397 USEAPP(string, "ps");
398 return;
399 case KERN_CLOCKRATE:
400 special |= CLOCK;
401 break;
402 case KERN_BOOTTIME:
403 special |= BOOTTIME;
404 break;
405 case KERN_NTPTIME:
406 USEAPP(string, "ntpdc -c kerninfo");
407 return;
408 case KERN_MBUF:
409 len = sysctl_3rd(&mbufvars, string, &bufp, mib, flags,
410 &type);
411 if (len < 0)
412 return;
413 break;
414 case KERN_CP_TIME:
415 special |= CPTIME;
416 break;
417 case KERN_MSGBUF:
418 USEAPP(string, "dmesg");
419 return;
420 case KERN_CONSDEV:
421 special |= CONSDEV;
422 break;
423 case KERN_PIPE:
424 len = sysctl_3rd(&pipevars, string, &bufp, mib, flags,
425 &type);
426 if (len < 0)
427 return;
428 break;
429 case KERN_TKSTAT:
430 len = sysctl_3rd(&tkstatvars, string, &bufp, mib, flags,
431 &type);
432 if (len < 0)
433 return;
434 break;
435 }
436 break;
437
438 case CTL_HW:
439 switch (mib[1]) {
440 case HW_DISKSTATS:
441 USEAPP(string, "iostat");
442 return;
443 }
444 break;
445
446 case CTL_VM:
447 switch (mib[1]) {
448 case VM_LOADAVG:
449 getloadavg(loads, 3);
450 if (!nflag)
451 printf("%s: ", string);
452 printf("%.2f %.2f %.2f\n", loads[0], loads[1],
453 loads[2]);
454 return;
455
456 case VM_METER:
457 case VM_UVMEXP:
458 case VM_UVMEXP2:
459 USEAPP(string, "vmstat' or 'systat");
460 return;
461 }
462 break;
463
464 case CTL_NET:
465 if (mib[1] == PF_INET) {
466 len = sysctl_inet(string, &bufp, mib, flags, &type);
467 if (len >= 0)
468 break;
469 return;
470 }
471 #ifdef INET6
472 else if (mib[1] == PF_INET6) {
473 len = sysctl_inet6(string, &bufp, mib, flags, &type);
474 if (len >= 0)
475 break;
476 return;
477 }
478 #endif /* INET6 */
479 #ifdef IPSEC
480 else if (mib[1] == PF_KEY) {
481 len = sysctl_3rd(&keyvars, string, &bufp, mib, flags,
482 &type);
483 if (len >= 0)
484 break;
485 return;
486 }
487 #endif /* IPSEC */
488 if (flags == 0)
489 return;
490 USEAPP(string, "netstat");
491 return;
492
493 case CTL_DEBUG:
494 mib[2] = CTL_DEBUG_VALUE;
495 len = 3;
496 break;
497
498 case CTL_MACHDEP:
499 #ifdef CPU_CONSDEV
500 if (mib[1] == CPU_CONSDEV)
501 special |= CONSDEV;
502 #endif
503 #ifdef CPU_DISKINFO
504 if (mib[1] == CPU_DISKINFO)
505 special |= DISKINFO;
506 #endif
507 break;
508
509 case CTL_VFS:
510 if (mib[1] == VFS_GENERIC) {
511 len = sysctl_3rd(&vfsgenvars, string, &bufp, mib, flags,
512 &type);
513 /* Don't bother with VFS_CONF. */
514 if (mib[2] == VFS_CONF)
515 len = -1;
516 } else
517 len = sysctl_vfs(string, &bufp, mib, flags, &type);
518 if (len < 0)
519 return;
520
521 /* XXX Special-case for NFS stats. */
522 if (mib[1] == 2 && mib[2] == NFS_NFSSTATS) {
523 USEAPP(string, "nfsstat");
524 return;
525 }
526 break;
527
528 case CTL_VENDOR:
529 case CTL_USER:
530 case CTL_DDB:
531 break;
532 case CTL_PROC:
533 len = sysctl_proc(string, &bufp, mib, flags, &type);
534 if (len < 0)
535 return;
536 break;
537 case CTL_EMUL:
538 switch (mib[1]) {
539 case EMUL_IRIX:
540 len = sysctl_irix(string, &bufp, mib, flags, &type);
541 break;
542
543 case EMUL_LINUX:
544 len = sysctl_linux(string, &bufp, mib, flags, &type);
545 break;
546
547 case EMUL_DARWIN:
548 len = sysctl_darwin(string, &bufp, mib, flags, &type);
549 break;
550
551 default:
552 warnx("Illegal emul level value: %d", mib[0]);
553 break;
554 }
555 if (len < 0)
556 return;
557 break;
558 default:
559 warnx("Illegal top level value: %d", mib[0]);
560 return;
561
562 }
563 if (bufp) {
564 warnx("Name %s in %s is unknown", bufp, string);
565 return;
566 }
567 if (newsize > 0) {
568 switch (type) {
569 case CTLTYPE_INT:
570 intval = atoi(newval);
571 newval = &intval;
572 newsize = sizeof intval;
573 break;
574
575 case CTLTYPE_LIMIT:
576 if (strcmp(newval, "unlimited") == 0) {
577 quadval = RLIM_INFINITY;
578 newval = &quadval;
579 newsize = sizeof quadval;
580 break;
581 }
582 /* FALLTHROUGH */
583 case CTLTYPE_QUAD:
584 sscanf(newval, "%lld", (long long *)&quadval);
585 newval = &quadval;
586 newsize = sizeof quadval;
587 break;
588 }
589 }
590 size = BUFSIZ;
591 if (sysctl(mib, len, buf, &size, newsize ? newval : 0, newsize) == -1) {
592 if (flags == 0)
593 return;
594 switch (errno) {
595 case EOPNOTSUPP:
596 printf("%s: the value is not available\n", string);
597 return;
598 case ENOTDIR:
599 printf("%s: the specification is incomplete\n", string);
600 return;
601 case ENOMEM:
602 printf("%s: this type is unknown to this program\n",
603 string);
604 return;
605 default:
606 printf("%s: sysctl() failed with %s\n",
607 string, strerror(errno));
608 return;
609 }
610 }
611 if (qflag && (newsize > 0))
612 return;
613 if (special & CLOCK) {
614 struct clockinfo *clkp = (struct clockinfo *)buf;
615
616 if (!nflag)
617 printf("%s: ", string);
618 printf(
619 "tick = %d, tickadj = %d, hz = %d, profhz = %d, stathz = %d\n",
620 clkp->tick, clkp->tickadj, clkp->hz, clkp->profhz, clkp->stathz);
621 return;
622 }
623 if (special & BOOTTIME) {
624 struct timeval *btp = (struct timeval *)buf;
625 time_t boottime;
626
627 if (!nflag) {
628 boottime = btp->tv_sec;
629 /* ctime() provides the trailing newline */
630 printf("%s = %s", string, ctime(&boottime));
631 } else
632 printf("%ld\n", (long) btp->tv_sec);
633 return;
634 }
635 if (special & CONSDEV) {
636 dev_t dev = *(dev_t *)buf;
637
638 if (!nflag)
639 printf("%s = %s\n", string, devname(dev, S_IFCHR));
640 else
641 printf("0x%x\n", dev);
642 return;
643 }
644 if (special & DISKINFO) {
645 #ifdef CPU_DISKINFO
646 /* Don't know a good way to deal with this i386 specific one */
647 /* OTOH this does pass the info out... */
648 struct disklist *dl = (void *)buf;
649 struct biosdisk_info *bi;
650 struct nativedisk_info *ni;
651 uint i, b, lim;
652 if (!nflag)
653 printf("%s: ", string);
654 lim = dl->dl_nbiosdisks;
655 if (lim > MAX_BIOSDISKS)
656 lim = MAX_BIOSDISKS;
657 for (bi = dl->dl_biosdisks, i = 0; i < lim; bi++, i++)
658 printf("%x:%lld(%d/%d/%d),%x ",
659 bi->bi_dev, bi->bi_lbasecs, bi->bi_cyl,
660 bi->bi_head, bi->bi_sec, bi->bi_flags);
661 lim = dl->dl_nnativedisks;
662 ni = dl->dl_nativedisks;
663 bi = dl->dl_biosdisks;
664 if ((char *)&ni[lim] != (char *)buf + size) {
665 printf("size mismatch\n");
666 return;
667 }
668 for (i = 0; i < lim; ni++, i++) {
669 char sep = ':';
670 printf(" %.*s", (int)sizeof ni->ni_devname,
671 ni->ni_devname);
672 for (b = 0; b < ni->ni_nmatches; sep = ',', b++)
673 printf("%c%x", sep,
674 bi[ni->ni_biosmatches[b]].bi_dev);
675 }
676 printf("\n");
677 #endif
678 return;
679 }
680 if (special & CPTIME) {
681 u_int64_t *cp_time = (u_int64_t *)buf;
682
683 if (!nflag)
684 printf("%s: ", string);
685 printf("user = %llu, nice = %llu, sys = %llu, intr = %llu, "
686 "idle = %llu\n", (unsigned long long) cp_time[0],
687 (unsigned long long) cp_time[1],
688 (unsigned long long) cp_time[2],
689 (unsigned long long) cp_time[3],
690 (unsigned long long) cp_time[4]);
691 return;
692 }
693
694 switch (type) {
695 case CTLTYPE_INT:
696 if (newsize == 0) {
697 if (!nflag)
698 printf("%s = ", string);
699 printf("%d\n", *(int *)buf);
700 } else {
701 if (!nflag)
702 printf("%s: %d -> ", string, *(int *)buf);
703 printf("%d\n", *(int *)newval);
704 }
705 return;
706
707 case CTLTYPE_STRING:
708 /* If sysctl() didn't return any data, don't print a string. */
709 if (size == 0)
710 buf[0] = '\0';
711 if (newsize == 0) {
712 if (!nflag)
713 printf("%s = ", string);
714 printf("%s\n", buf);
715 } else {
716 if (!nflag)
717 printf("%s: %s -> ", string, buf);
718 printf("%s\n", (char *) newval);
719 }
720 return;
721
722 case CTLTYPE_LIMIT:
723 #define PRINTF_LIMIT(lim) { \
724 if ((lim) == RLIM_INFINITY) \
725 printf("unlimited");\
726 else \
727 printf("%lld", (long long)(lim)); \
728 }
729
730 if (newsize == 0) {
731 if (!nflag)
732 printf("%s = ", string);
733 PRINTF_LIMIT((long long)(*(quad_t *)buf));
734 } else {
735 if (!nflag) {
736 printf("%s: ", string);
737 PRINTF_LIMIT((long long)(*(quad_t *)buf));
738 printf(" -> ");
739 }
740 PRINTF_LIMIT((long long)(*(quad_t *)newval));
741 }
742 printf("\n");
743 return;
744 #undef PRINTF_LIMIT
745
746 case CTLTYPE_QUAD:
747 if (newsize == 0) {
748 if (!nflag)
749 printf("%s = ", string);
750 printf("%lld\n", (long long)(*(quad_t *)buf));
751 } else {
752 if (!nflag)
753 printf("%s: %lld -> ", string,
754 (long long)(*(quad_t *)buf));
755 printf("%lld\n", (long long)(*(quad_t *)newval));
756 }
757 return;
758
759 case CTLTYPE_STRUCT:
760 warnx("%s: unknown structure returned", string);
761 return;
762
763 default:
764 case CTLTYPE_NODE:
765 warnx("%s: unknown type returned", string);
766 return;
767 }
768 }
769
770 /*
771 * Initialize the set of debugging names
772 */
773 static void
774 debuginit(void)
775 {
776 int mib[3], loc, i;
777 size_t size;
778
779 if (secondlevel[CTL_DEBUG].list != 0)
780 return;
781 secondlevel[CTL_DEBUG].list = debugname;
782 mib[0] = CTL_DEBUG;
783 mib[2] = CTL_DEBUG_NAME;
784 for (loc = 0, i = 0; i < CTL_DEBUG_MAXID; i++) {
785 mib[1] = i;
786 size = BUFSIZ - loc;
787 if (sysctl(mib, 3, &names[loc], &size, NULL, 0) == -1)
788 continue;
789 debugname[i].ctl_name = &names[loc];
790 debugname[i].ctl_type = CTLTYPE_INT;
791 loc += size;
792 }
793 }
794
795 struct ctlname inetname[] = CTL_IPPROTO_NAMES;
796 struct ctlname ipname[] = IPCTL_NAMES;
797 struct ctlname icmpname[] = ICMPCTL_NAMES;
798 struct ctlname tcpname[] = TCPCTL_NAMES;
799 struct ctlname udpname[] = UDPCTL_NAMES;
800 #ifdef IPSEC
801 struct ctlname ipsecname[] = IPSECCTL_NAMES;
802 #endif
803 struct list inetlist = { inetname, IPPROTO_MAXID };
804 struct list inetvars[] = {
805 /*0*/ { ipname, IPCTL_MAXID }, /* ip */
806 { icmpname, ICMPCTL_MAXID }, /* icmp */
807 { 0, 0 }, /* igmp */
808 { 0, 0 }, /* ggmp */
809 { 0, 0 },
810 { 0, 0 },
811 { tcpname, TCPCTL_MAXID }, /* tcp */
812 { 0, 0 },
813 { 0, 0 }, /* egp */
814 { 0, 0 },
815 /*10*/ { 0, 0 },
816 { 0, 0 },
817 { 0, 0 }, /* pup */
818 { 0, 0 },
819 { 0, 0 },
820 { 0, 0 },
821 { 0, 0 },
822 { udpname, UDPCTL_MAXID }, /* udp */
823 { 0, 0 },
824 { 0, 0 },
825 /*20*/ { 0, 0 },
826 { 0, 0 },
827 { 0, 0 }, /* idp */
828 { 0, 0 },
829 { 0, 0 },
830 { 0, 0 },
831 { 0, 0 },
832 { 0, 0 },
833 { 0, 0 },
834 { 0, 0 },
835 /*30*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
836 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
837 /*40*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
838 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
839 #ifdef IPSEC
840 { ipsecname, IPSECCTL_MAXID }, /* esp - for backward compatibility */
841 { ipsecname, IPSECCTL_MAXID }, /* ah */
842 #else
843 { 0, 0 },
844 { 0, 0 },
845 #endif
846 };
847
848 /*
849 * handle internet requests
850 */
851 static int
852 sysctl_inet(char *string, char **bufpp, int mib[], int flags, int *typep)
853 {
854 struct list *lp;
855 int indx;
856
857 if (*bufpp == NULL) {
858 listall(string, &inetlist);
859 return (-1);
860 }
861 if ((indx = findname(string, "third", bufpp, &inetlist)) == -1)
862 return (-1);
863 mib[2] = indx;
864 if (indx <= IPPROTO_MAXID && inetvars[indx].list != NULL)
865 lp = &inetvars[indx];
866 else if (!flags)
867 return (-1);
868 else {
869 printf("%s: no variables defined for protocol\n", string);
870 return (-1);
871 }
872 if (*bufpp == NULL) {
873 listall(string, lp);
874 return (-1);
875 }
876 if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
877 return (-1);
878 mib[3] = indx;
879 *typep = lp->list[indx].ctl_type;
880 return (4);
881 }
882
883 #ifdef INET6
884 struct ctlname inet6name[] = CTL_IPV6PROTO_NAMES;
885 struct ctlname ip6name[] = IPV6CTL_NAMES;
886 struct ctlname icmp6name[] = ICMPV6CTL_NAMES;
887 struct ctlname udp6name[] = UDP6CTL_NAMES;
888 struct ctlname pim6name[] = PIM6CTL_NAMES;
889 struct ctlname ipsec6name[] = IPSEC6CTL_NAMES;
890 struct list inet6list = { inet6name, IPV6PROTO_MAXID };
891 struct list inet6vars[] = {
892 /*0*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
893 { 0, 0 },
894 { tcpname, TCPCTL_MAXID }, /* tcp6 */
895 { 0, 0 },
896 { 0, 0 },
897 { 0, 0 },
898 /*10*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
899 { 0, 0 },
900 { 0, 0 },
901 { udp6name, UDP6CTL_MAXID }, /* udp6 */
902 { 0, 0 },
903 { 0, 0 },
904 /*20*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
905 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
906 /*30*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
907 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
908 /*40*/ { 0, 0 },
909 { ip6name, IPV6CTL_MAXID }, /* ipv6 */
910 { 0, 0 },
911 { 0, 0 },
912 { 0, 0 },
913 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
914 #ifdef IPSEC
915 /*50*/ { ipsec6name, IPSECCTL_MAXID }, /* esp6 - for backward compatibility */
916 { ipsec6name, IPSECCTL_MAXID }, /* ah6 */
917 #else
918 { 0, 0 },
919 { 0, 0 },
920 #endif
921 { 0, 0 },
922 { 0, 0 },
923 { 0, 0 },
924 { 0, 0 },
925 { 0, 0 },
926 { 0, 0 },
927 { icmp6name, ICMPV6CTL_MAXID }, /* icmp6 */
928 { 0, 0 },
929 /*60*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
930 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
931 /*70*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
932 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
933 /*80*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
934 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
935 /*90*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
936 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
937 /*100*/ { 0, 0 },
938 { 0, 0 },
939 { 0, 0 },
940 { pim6name, PIM6CTL_MAXID }, /* pim6 */
941 };
942
943 /*
944 * handle internet6 requests
945 */
946 static int
947 sysctl_inet6(char *string, char **bufpp, int mib[], int flags, int *typep)
948 {
949 struct list *lp;
950 int indx;
951
952 if (*bufpp == NULL) {
953 listall(string, &inet6list);
954 return (-1);
955 }
956 if ((indx = findname(string, "third", bufpp, &inet6list)) == -1)
957 return (-1);
958 mib[2] = indx;
959 if (indx <= sizeof(inet6vars)/sizeof(inet6vars[0])
960 && inet6vars[indx].list != NULL) {
961 lp = &inet6vars[indx];
962 } else if (!flags) {
963 return (-1);
964 } else {
965 fprintf(stderr, "%s: no variables defined for this protocol\n",
966 string);
967 return (-1);
968 }
969 if (*bufpp == NULL) {
970 listall(string, lp);
971 return (-1);
972 }
973 if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
974 return (-1);
975 mib[3] = indx;
976 *typep = lp->list[indx].ctl_type;
977 return (4);
978 }
979 #endif /* INET6 */
980
981 struct ctlname ffsname[] = FFS_NAMES;
982 struct ctlname nfsname[] = NFS_NAMES;
983 struct list vfsvars[] = {
984 { 0, 0 }, /* generic */
985 { ffsname, FFS_MAXID }, /* FFS */
986 { nfsname, NFS_MAXID }, /* NFS */
987 { 0, 0 }, /* MFS */
988 { 0, 0 }, /* MSDOS */
989 { 0, 0 }, /* LFS */
990 { 0, 0 }, /* old LOFS */
991 { 0, 0 }, /* FDESC */
992 { 0, 0 }, /* PORTAL */
993 { 0, 0 }, /* NULL */
994 { 0, 0 }, /* UMAP */
995 { 0, 0 }, /* KERNFS */
996 { 0, 0 }, /* PROCFS */
997 { 0, 0 }, /* AFS */
998 { 0, 0 }, /* CD9660 */
999 { 0, 0 }, /* UNION */
1000 { 0, 0 }, /* ADOSFS */
1001 { 0, 0 }, /* EXT2FS */
1002 { 0, 0 }, /* CODA */
1003 { 0, 0 }, /* FILECORE */
1004 };
1005
1006 /*
1007 * handle vfs requests
1008 */
1009 static int
1010 sysctl_vfs(char *string, char **bufpp, int mib[], int flags, int *typep)
1011 {
1012 struct list *lp = &vfsvars[mib[1]];
1013 int indx;
1014
1015 if (lp->list == NULL) {
1016 if (flags)
1017 printf("%s: no variables defined for file system\n",
1018 string);
1019 return (-1);
1020 }
1021 if (*bufpp == NULL) {
1022 listall(string, lp);
1023 return (-1);
1024 }
1025 if ((indx = findname(string, "third", bufpp, lp)) == -1)
1026 return (-1);
1027 mib[2] = indx;
1028 *typep = lp->list[indx].ctl_type;
1029 return (3);
1030 }
1031
1032 /*
1033 * handle 3rd level requests.
1034 */
1035 static int
1036 sysctl_3rd(struct list *lp, char *string, char **bufpp, int mib[], int flags,
1037 int *typep)
1038 {
1039 int indx;
1040
1041 if (*bufpp == NULL) {
1042 listall(string, lp);
1043 return (-1);
1044 }
1045 if ((indx = findname(string, "third", bufpp, lp)) == -1)
1046 return (-1);
1047 mib[2] = indx;
1048 *typep = lp->list[indx].ctl_type;
1049 return (3);
1050 }
1051
1052 struct ctlname procnames[] = PROC_PID_NAMES;
1053 struct list procvars = {procnames, PROC_PID_MAXID};
1054 struct ctlname proclimitnames[] = PROC_PID_LIMIT_NAMES;
1055 struct list proclimitvars = {proclimitnames, PROC_PID_LIMIT_MAXID};
1056 struct ctlname proclimittypenames[] = PROC_PID_LIMIT_TYPE_NAMES;
1057 struct list proclimittypevars = {proclimittypenames,
1058 PROC_PID_LIMIT_TYPE_MAXID};
1059 /*
1060 * handle kern.proc requests
1061 */
1062 static int
1063 sysctl_proc(char *string, char **bufpp, int mib[], int flags, int *typep)
1064 {
1065 char *cp, name[BUFSIZ];
1066 struct list *lp;
1067 int indx;
1068
1069 if (*bufpp == NULL) {
1070 strcpy(name, string);
1071 cp = &name[strlen(name)];
1072 *cp++ = '.';
1073 strcpy(cp, "curproc");
1074 parse(name, Aflag);
1075 return (-1);
1076 }
1077 cp = strsep(bufpp, ".");
1078 if (cp == NULL) {
1079 warnx("%s: incomplete specification", string);
1080 return (-1);
1081 }
1082 if (strcmp(cp, "curproc") == 0) {
1083 mib[1] = PROC_CURPROC;
1084 } else {
1085 mib[1] = atoi(cp);
1086 if (mib[1] == 0) {
1087 warnx("second level name %s in %s is invalid", cp,
1088 string);
1089 return (-1);
1090 }
1091 }
1092 *typep = CTLTYPE_NODE;
1093 lp = &procvars;
1094 if (*bufpp == NULL) {
1095 listall(string, lp);
1096 return (-1);
1097 }
1098 if ((indx = findname(string, "third", bufpp, lp)) == -1)
1099 return (-1);
1100 mib[2] = indx;
1101 *typep = lp->list[indx].ctl_type;
1102 if (*typep != CTLTYPE_NODE)
1103 return(3);
1104 lp = &proclimitvars;
1105 if (*bufpp == NULL) {
1106 listall(string, lp);
1107 return (-1);
1108 }
1109 if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
1110 return (-1);
1111 mib[3] = indx;
1112 lp = &proclimittypevars;
1113 if (*bufpp == NULL) {
1114 listall(string, lp);
1115 return (-1);
1116 }
1117 if ((indx = findname(string, "fifth", bufpp, lp)) == -1)
1118 return (-1);
1119 mib[4] = indx;
1120 *typep = CTLTYPE_LIMIT;
1121 return(5);
1122 }
1123
1124 struct ctlname linuxnames[] = EMUL_LINUX_NAMES;
1125 struct list linuxvars = { linuxnames, EMUL_LINUX_MAXID };
1126 struct ctlname linuxkernnames[] = EMUL_LINUX_KERN_NAMES;
1127 struct list linuxkernvars = { linuxkernnames, EMUL_LINUX_KERN_MAXID };
1128
1129 static int
1130 sysctl_linux(char *string, char **bufpp, int mib[], int flags, int *typep)
1131 {
1132 struct list *lp = &linuxvars;
1133 int indx;
1134 char name[BUFSIZ], *cp;
1135
1136 if (*bufpp == NULL) {
1137 (void)strcpy(name, string);
1138 cp = &name[strlen(name)];
1139 *cp++ = '.';
1140 (void)strcpy(cp, "kern");
1141 listall(name, &linuxkernvars);
1142 return (-1);
1143 }
1144 if ((indx = findname(string, "third", bufpp, lp)) == -1)
1145 return (-1);
1146 mib[2] = indx;
1147 lp = &linuxkernvars;
1148 *typep = lp->list[indx].ctl_type;
1149 if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
1150 return (-1);
1151 mib[3] = indx;
1152 *typep = lp->list[indx].ctl_type;
1153 return (4);
1154 }
1155
1156 struct ctlname irixnames[] = EMUL_IRIX_NAMES;
1157 struct list irixvars = { irixnames, EMUL_IRIX_MAXID };
1158 struct ctlname irixkernnames[] = EMUL_IRIX_KERN_NAMES;
1159 struct list irixkernvars = { irixkernnames, EMUL_IRIX_KERN_MAXID };
1160
1161 static int
1162 sysctl_irix(char *string, char **bufpp, int mib[], int flags, int *typep)
1163 {
1164 struct list *lp = &irixvars;
1165 int indx;
1166 char name[BUFSIZ], *cp;
1167
1168 if (*bufpp == NULL) {
1169 (void)strcpy(name, string);
1170 cp = &name[strlen(name)];
1171 *cp++ = '.';
1172 (void)strcpy(cp, "kern");
1173 listall(name, &irixkernvars);
1174 return (-1);
1175 }
1176 if ((indx = findname(string, "third", bufpp, lp)) == -1)
1177 return (-1);
1178 mib[2] = indx;
1179 lp = &irixkernvars;
1180 *typep = lp->list[indx].ctl_type;
1181 if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
1182 return (-1);
1183 mib[3] = indx;
1184 *typep = lp->list[indx].ctl_type;
1185 return (4);
1186 }
1187
1188 struct ctlname darwinnames[] = EMUL_DARWIN_NAMES;
1189 struct list darwinvars = { darwinnames, EMUL_DARWIN_MAXID };
1190
1191 static int
1192 sysctl_darwin(char *string, char **bufpp, int mib[], int flags, int *typep)
1193 {
1194 struct list *lp = &darwinvars;
1195 int indx;
1196
1197 if (*bufpp == NULL) {
1198 listall(string, &darwinvars);
1199 return (-1);
1200 }
1201 if ((indx = findname(string, "third", bufpp, lp)) == -1)
1202 return (-1);
1203 mib[2] = indx;
1204 lp = &darwinvars;
1205 *typep = lp->list[indx].ctl_type;
1206 return (3);
1207 }
1208
1209 /*
1210 * Scan a list of names searching for a particular name.
1211 */
1212 static int
1213 findname(char *string, char *level, char **bufp, struct list *namelist)
1214 {
1215 char *name;
1216 int i;
1217
1218 if (namelist->list == 0 || (name = strsep(bufp, ".")) == NULL) {
1219 warnx("%s: incomplete specification", string);
1220 return (-1);
1221 }
1222 for (i = 0; i < namelist->size; i++)
1223 if (namelist->list[i].ctl_name != NULL &&
1224 strcmp(name, namelist->list[i].ctl_name) == 0)
1225 break;
1226 if (i == namelist->size) {
1227 warnx("%s level name %s in %s is invalid",
1228 level, name, string);
1229 return (-1);
1230 }
1231 return (i);
1232 }
1233
1234 static void
1235 usage(void)
1236 {
1237 const char *progname = getprogname();
1238
1239 (void)fprintf(stderr,
1240 "Usage:\t%s %s\n\t%s %s\n\t%s %s\n\t%s %s\n\t%s %s\n",
1241 progname, "[-n] variable ...",
1242 progname, "[-n] [-q] -w variable=value ...",
1243 progname, "[-n] -a",
1244 progname, "[-n] -A",
1245 progname, "[-n] [-q] -f file");
1246 exit(1);
1247 }
1248