sysctl.c revision 1.64 1 /* $NetBSD: sysctl.c,v 1.64 2003/03/07 00:42:04 fvdl 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.64 2003/03/07 00:42:04 fvdl 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, (long long)bi->bi_lbasecs,
660 bi->bi_cyl, bi->bi_head, bi->bi_sec,
661 bi->bi_flags);
662 lim = dl->dl_nnativedisks;
663 ni = dl->dl_nativedisks;
664 bi = dl->dl_biosdisks;
665 if ((char *)&ni[lim] != (char *)buf + size) {
666 printf("size mismatch\n");
667 return;
668 }
669 for (i = 0; i < lim; ni++, i++) {
670 char sep = ':';
671 printf(" %.*s", (int)sizeof ni->ni_devname,
672 ni->ni_devname);
673 for (b = 0; b < ni->ni_nmatches; sep = ',', b++)
674 printf("%c%x", sep,
675 bi[ni->ni_biosmatches[b]].bi_dev);
676 }
677 printf("\n");
678 #endif
679 return;
680 }
681 if (special & CPTIME) {
682 u_int64_t *cp_time = (u_int64_t *)buf;
683
684 if (!nflag)
685 printf("%s: ", string);
686 printf("user = %llu, nice = %llu, sys = %llu, intr = %llu, "
687 "idle = %llu\n", (unsigned long long) cp_time[0],
688 (unsigned long long) cp_time[1],
689 (unsigned long long) cp_time[2],
690 (unsigned long long) cp_time[3],
691 (unsigned long long) cp_time[4]);
692 return;
693 }
694
695 switch (type) {
696 case CTLTYPE_INT:
697 if (newsize == 0) {
698 if (!nflag)
699 printf("%s = ", string);
700 printf("%d\n", *(int *)buf);
701 } else {
702 if (!nflag)
703 printf("%s: %d -> ", string, *(int *)buf);
704 printf("%d\n", *(int *)newval);
705 }
706 return;
707
708 case CTLTYPE_STRING:
709 /* If sysctl() didn't return any data, don't print a string. */
710 if (size == 0)
711 buf[0] = '\0';
712 if (newsize == 0) {
713 if (!nflag)
714 printf("%s = ", string);
715 printf("%s\n", buf);
716 } else {
717 if (!nflag)
718 printf("%s: %s -> ", string, buf);
719 printf("%s\n", (char *) newval);
720 }
721 return;
722
723 case CTLTYPE_LIMIT:
724 #define PRINTF_LIMIT(lim) { \
725 if ((lim) == RLIM_INFINITY) \
726 printf("unlimited");\
727 else \
728 printf("%lld", (long long)(lim)); \
729 }
730
731 if (newsize == 0) {
732 if (!nflag)
733 printf("%s = ", string);
734 PRINTF_LIMIT((long long)(*(quad_t *)buf));
735 } else {
736 if (!nflag) {
737 printf("%s: ", string);
738 PRINTF_LIMIT((long long)(*(quad_t *)buf));
739 printf(" -> ");
740 }
741 PRINTF_LIMIT((long long)(*(quad_t *)newval));
742 }
743 printf("\n");
744 return;
745 #undef PRINTF_LIMIT
746
747 case CTLTYPE_QUAD:
748 if (newsize == 0) {
749 if (!nflag)
750 printf("%s = ", string);
751 printf("%lld\n", (long long)(*(quad_t *)buf));
752 } else {
753 if (!nflag)
754 printf("%s: %lld -> ", string,
755 (long long)(*(quad_t *)buf));
756 printf("%lld\n", (long long)(*(quad_t *)newval));
757 }
758 return;
759
760 case CTLTYPE_STRUCT:
761 warnx("%s: unknown structure returned", string);
762 return;
763
764 default:
765 case CTLTYPE_NODE:
766 warnx("%s: unknown type returned", string);
767 return;
768 }
769 }
770
771 /*
772 * Initialize the set of debugging names
773 */
774 static void
775 debuginit(void)
776 {
777 int mib[3], loc, i;
778 size_t size;
779
780 if (secondlevel[CTL_DEBUG].list != 0)
781 return;
782 secondlevel[CTL_DEBUG].list = debugname;
783 mib[0] = CTL_DEBUG;
784 mib[2] = CTL_DEBUG_NAME;
785 for (loc = 0, i = 0; i < CTL_DEBUG_MAXID; i++) {
786 mib[1] = i;
787 size = BUFSIZ - loc;
788 if (sysctl(mib, 3, &names[loc], &size, NULL, 0) == -1)
789 continue;
790 debugname[i].ctl_name = &names[loc];
791 debugname[i].ctl_type = CTLTYPE_INT;
792 loc += size;
793 }
794 }
795
796 struct ctlname inetname[] = CTL_IPPROTO_NAMES;
797 struct ctlname ipname[] = IPCTL_NAMES;
798 struct ctlname icmpname[] = ICMPCTL_NAMES;
799 struct ctlname tcpname[] = TCPCTL_NAMES;
800 struct ctlname udpname[] = UDPCTL_NAMES;
801 #ifdef IPSEC
802 struct ctlname ipsecname[] = IPSECCTL_NAMES;
803 #endif
804 struct list inetlist = { inetname, IPPROTO_MAXID };
805 struct list inetvars[] = {
806 /*0*/ { ipname, IPCTL_MAXID }, /* ip */
807 { icmpname, ICMPCTL_MAXID }, /* icmp */
808 { 0, 0 }, /* igmp */
809 { 0, 0 }, /* ggmp */
810 { 0, 0 },
811 { 0, 0 },
812 { tcpname, TCPCTL_MAXID }, /* tcp */
813 { 0, 0 },
814 { 0, 0 }, /* egp */
815 { 0, 0 },
816 /*10*/ { 0, 0 },
817 { 0, 0 },
818 { 0, 0 }, /* pup */
819 { 0, 0 },
820 { 0, 0 },
821 { 0, 0 },
822 { 0, 0 },
823 { udpname, UDPCTL_MAXID }, /* udp */
824 { 0, 0 },
825 { 0, 0 },
826 /*20*/ { 0, 0 },
827 { 0, 0 },
828 { 0, 0 }, /* idp */
829 { 0, 0 },
830 { 0, 0 },
831 { 0, 0 },
832 { 0, 0 },
833 { 0, 0 },
834 { 0, 0 },
835 { 0, 0 },
836 /*30*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
837 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
838 /*40*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
839 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
840 #ifdef IPSEC
841 { ipsecname, IPSECCTL_MAXID }, /* esp - for backward compatibility */
842 { ipsecname, IPSECCTL_MAXID }, /* ah */
843 #else
844 { 0, 0 },
845 { 0, 0 },
846 #endif
847 };
848
849 /*
850 * handle internet requests
851 */
852 static int
853 sysctl_inet(char *string, char **bufpp, int mib[], int flags, int *typep)
854 {
855 struct list *lp;
856 int indx;
857
858 if (*bufpp == NULL) {
859 listall(string, &inetlist);
860 return (-1);
861 }
862 if ((indx = findname(string, "third", bufpp, &inetlist)) == -1)
863 return (-1);
864 mib[2] = indx;
865 if (indx <= IPPROTO_MAXID && inetvars[indx].list != NULL)
866 lp = &inetvars[indx];
867 else if (!flags)
868 return (-1);
869 else {
870 printf("%s: no variables defined for protocol\n", string);
871 return (-1);
872 }
873 if (*bufpp == NULL) {
874 listall(string, lp);
875 return (-1);
876 }
877 if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
878 return (-1);
879 mib[3] = indx;
880 *typep = lp->list[indx].ctl_type;
881 return (4);
882 }
883
884 #ifdef INET6
885 struct ctlname inet6name[] = CTL_IPV6PROTO_NAMES;
886 struct ctlname ip6name[] = IPV6CTL_NAMES;
887 struct ctlname icmp6name[] = ICMPV6CTL_NAMES;
888 struct ctlname udp6name[] = UDP6CTL_NAMES;
889 struct ctlname pim6name[] = PIM6CTL_NAMES;
890 struct ctlname ipsec6name[] = IPSEC6CTL_NAMES;
891 struct list inet6list = { inet6name, IPV6PROTO_MAXID };
892 struct list inet6vars[] = {
893 /*0*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
894 { 0, 0 },
895 { tcpname, TCPCTL_MAXID }, /* tcp6 */
896 { 0, 0 },
897 { 0, 0 },
898 { 0, 0 },
899 /*10*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
900 { 0, 0 },
901 { 0, 0 },
902 { udp6name, UDP6CTL_MAXID }, /* udp6 */
903 { 0, 0 },
904 { 0, 0 },
905 /*20*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
906 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
907 /*30*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
908 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
909 /*40*/ { 0, 0 },
910 { ip6name, IPV6CTL_MAXID }, /* ipv6 */
911 { 0, 0 },
912 { 0, 0 },
913 { 0, 0 },
914 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
915 #ifdef IPSEC
916 /*50*/ { ipsec6name, IPSECCTL_MAXID }, /* esp6 - for backward compatibility */
917 { ipsec6name, IPSECCTL_MAXID }, /* ah6 */
918 #else
919 { 0, 0 },
920 { 0, 0 },
921 #endif
922 { 0, 0 },
923 { 0, 0 },
924 { 0, 0 },
925 { 0, 0 },
926 { 0, 0 },
927 { 0, 0 },
928 { icmp6name, ICMPV6CTL_MAXID }, /* icmp6 */
929 { 0, 0 },
930 /*60*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
931 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
932 /*70*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
933 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
934 /*80*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
935 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
936 /*90*/ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
937 { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
938 /*100*/ { 0, 0 },
939 { 0, 0 },
940 { 0, 0 },
941 { pim6name, PIM6CTL_MAXID }, /* pim6 */
942 };
943
944 /*
945 * handle internet6 requests
946 */
947 static int
948 sysctl_inet6(char *string, char **bufpp, int mib[], int flags, int *typep)
949 {
950 struct list *lp;
951 int indx;
952
953 if (*bufpp == NULL) {
954 listall(string, &inet6list);
955 return (-1);
956 }
957 if ((indx = findname(string, "third", bufpp, &inet6list)) == -1)
958 return (-1);
959 mib[2] = indx;
960 if (indx <= sizeof(inet6vars)/sizeof(inet6vars[0])
961 && inet6vars[indx].list != NULL) {
962 lp = &inet6vars[indx];
963 } else if (!flags) {
964 return (-1);
965 } else {
966 fprintf(stderr, "%s: no variables defined for this protocol\n",
967 string);
968 return (-1);
969 }
970 if (*bufpp == NULL) {
971 listall(string, lp);
972 return (-1);
973 }
974 if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
975 return (-1);
976 mib[3] = indx;
977 *typep = lp->list[indx].ctl_type;
978 return (4);
979 }
980 #endif /* INET6 */
981
982 struct ctlname ffsname[] = FFS_NAMES;
983 struct ctlname nfsname[] = NFS_NAMES;
984 struct list vfsvars[] = {
985 { 0, 0 }, /* generic */
986 { ffsname, FFS_MAXID }, /* FFS */
987 { nfsname, NFS_MAXID }, /* NFS */
988 { 0, 0 }, /* MFS */
989 { 0, 0 }, /* MSDOS */
990 { 0, 0 }, /* LFS */
991 { 0, 0 }, /* old LOFS */
992 { 0, 0 }, /* FDESC */
993 { 0, 0 }, /* PORTAL */
994 { 0, 0 }, /* NULL */
995 { 0, 0 }, /* UMAP */
996 { 0, 0 }, /* KERNFS */
997 { 0, 0 }, /* PROCFS */
998 { 0, 0 }, /* AFS */
999 { 0, 0 }, /* CD9660 */
1000 { 0, 0 }, /* UNION */
1001 { 0, 0 }, /* ADOSFS */
1002 { 0, 0 }, /* EXT2FS */
1003 { 0, 0 }, /* CODA */
1004 { 0, 0 }, /* FILECORE */
1005 };
1006
1007 /*
1008 * handle vfs requests
1009 */
1010 static int
1011 sysctl_vfs(char *string, char **bufpp, int mib[], int flags, int *typep)
1012 {
1013 struct list *lp = &vfsvars[mib[1]];
1014 int indx;
1015
1016 if (lp->list == NULL) {
1017 if (flags)
1018 printf("%s: no variables defined for file system\n",
1019 string);
1020 return (-1);
1021 }
1022 if (*bufpp == NULL) {
1023 listall(string, lp);
1024 return (-1);
1025 }
1026 if ((indx = findname(string, "third", bufpp, lp)) == -1)
1027 return (-1);
1028 mib[2] = indx;
1029 *typep = lp->list[indx].ctl_type;
1030 return (3);
1031 }
1032
1033 /*
1034 * handle 3rd level requests.
1035 */
1036 static int
1037 sysctl_3rd(struct list *lp, char *string, char **bufpp, int mib[], int flags,
1038 int *typep)
1039 {
1040 int indx;
1041
1042 if (*bufpp == NULL) {
1043 listall(string, lp);
1044 return (-1);
1045 }
1046 if ((indx = findname(string, "third", bufpp, lp)) == -1)
1047 return (-1);
1048 mib[2] = indx;
1049 *typep = lp->list[indx].ctl_type;
1050 return (3);
1051 }
1052
1053 struct ctlname procnames[] = PROC_PID_NAMES;
1054 struct list procvars = {procnames, PROC_PID_MAXID};
1055 struct ctlname proclimitnames[] = PROC_PID_LIMIT_NAMES;
1056 struct list proclimitvars = {proclimitnames, PROC_PID_LIMIT_MAXID};
1057 struct ctlname proclimittypenames[] = PROC_PID_LIMIT_TYPE_NAMES;
1058 struct list proclimittypevars = {proclimittypenames,
1059 PROC_PID_LIMIT_TYPE_MAXID};
1060 /*
1061 * handle kern.proc requests
1062 */
1063 static int
1064 sysctl_proc(char *string, char **bufpp, int mib[], int flags, int *typep)
1065 {
1066 char *cp, name[BUFSIZ];
1067 struct list *lp;
1068 int indx;
1069
1070 if (*bufpp == NULL) {
1071 strcpy(name, string);
1072 cp = &name[strlen(name)];
1073 *cp++ = '.';
1074 strcpy(cp, "curproc");
1075 parse(name, Aflag);
1076 return (-1);
1077 }
1078 cp = strsep(bufpp, ".");
1079 if (cp == NULL) {
1080 warnx("%s: incomplete specification", string);
1081 return (-1);
1082 }
1083 if (strcmp(cp, "curproc") == 0) {
1084 mib[1] = PROC_CURPROC;
1085 } else {
1086 mib[1] = atoi(cp);
1087 if (mib[1] == 0) {
1088 warnx("second level name %s in %s is invalid", cp,
1089 string);
1090 return (-1);
1091 }
1092 }
1093 *typep = CTLTYPE_NODE;
1094 lp = &procvars;
1095 if (*bufpp == NULL) {
1096 listall(string, lp);
1097 return (-1);
1098 }
1099 if ((indx = findname(string, "third", bufpp, lp)) == -1)
1100 return (-1);
1101 mib[2] = indx;
1102 *typep = lp->list[indx].ctl_type;
1103 if (*typep != CTLTYPE_NODE)
1104 return(3);
1105 lp = &proclimitvars;
1106 if (*bufpp == NULL) {
1107 listall(string, lp);
1108 return (-1);
1109 }
1110 if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
1111 return (-1);
1112 mib[3] = indx;
1113 lp = &proclimittypevars;
1114 if (*bufpp == NULL) {
1115 listall(string, lp);
1116 return (-1);
1117 }
1118 if ((indx = findname(string, "fifth", bufpp, lp)) == -1)
1119 return (-1);
1120 mib[4] = indx;
1121 *typep = CTLTYPE_LIMIT;
1122 return(5);
1123 }
1124
1125 struct ctlname linuxnames[] = EMUL_LINUX_NAMES;
1126 struct list linuxvars = { linuxnames, EMUL_LINUX_MAXID };
1127 struct ctlname linuxkernnames[] = EMUL_LINUX_KERN_NAMES;
1128 struct list linuxkernvars = { linuxkernnames, EMUL_LINUX_KERN_MAXID };
1129
1130 static int
1131 sysctl_linux(char *string, char **bufpp, int mib[], int flags, int *typep)
1132 {
1133 struct list *lp = &linuxvars;
1134 int indx;
1135 char name[BUFSIZ], *cp;
1136
1137 if (*bufpp == NULL) {
1138 (void)strcpy(name, string);
1139 cp = &name[strlen(name)];
1140 *cp++ = '.';
1141 (void)strcpy(cp, "kern");
1142 listall(name, &linuxkernvars);
1143 return (-1);
1144 }
1145 if ((indx = findname(string, "third", bufpp, lp)) == -1)
1146 return (-1);
1147 mib[2] = indx;
1148 lp = &linuxkernvars;
1149 *typep = lp->list[indx].ctl_type;
1150 if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
1151 return (-1);
1152 mib[3] = indx;
1153 *typep = lp->list[indx].ctl_type;
1154 return (4);
1155 }
1156
1157 struct ctlname irixnames[] = EMUL_IRIX_NAMES;
1158 struct list irixvars = { irixnames, EMUL_IRIX_MAXID };
1159 struct ctlname irixkernnames[] = EMUL_IRIX_KERN_NAMES;
1160 struct list irixkernvars = { irixkernnames, EMUL_IRIX_KERN_MAXID };
1161
1162 static int
1163 sysctl_irix(char *string, char **bufpp, int mib[], int flags, int *typep)
1164 {
1165 struct list *lp = &irixvars;
1166 int indx;
1167 char name[BUFSIZ], *cp;
1168
1169 if (*bufpp == NULL) {
1170 (void)strcpy(name, string);
1171 cp = &name[strlen(name)];
1172 *cp++ = '.';
1173 (void)strcpy(cp, "kern");
1174 listall(name, &irixkernvars);
1175 return (-1);
1176 }
1177 if ((indx = findname(string, "third", bufpp, lp)) == -1)
1178 return (-1);
1179 mib[2] = indx;
1180 lp = &irixkernvars;
1181 *typep = lp->list[indx].ctl_type;
1182 if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
1183 return (-1);
1184 mib[3] = indx;
1185 *typep = lp->list[indx].ctl_type;
1186 return (4);
1187 }
1188
1189 struct ctlname darwinnames[] = EMUL_DARWIN_NAMES;
1190 struct list darwinvars = { darwinnames, EMUL_DARWIN_MAXID };
1191
1192 static int
1193 sysctl_darwin(char *string, char **bufpp, int mib[], int flags, int *typep)
1194 {
1195 struct list *lp = &darwinvars;
1196 int indx;
1197
1198 if (*bufpp == NULL) {
1199 listall(string, &darwinvars);
1200 return (-1);
1201 }
1202 if ((indx = findname(string, "third", bufpp, lp)) == -1)
1203 return (-1);
1204 mib[2] = indx;
1205 lp = &darwinvars;
1206 *typep = lp->list[indx].ctl_type;
1207 return (3);
1208 }
1209
1210 /*
1211 * Scan a list of names searching for a particular name.
1212 */
1213 static int
1214 findname(char *string, char *level, char **bufp, struct list *namelist)
1215 {
1216 char *name;
1217 int i;
1218
1219 if (namelist->list == 0 || (name = strsep(bufp, ".")) == NULL) {
1220 warnx("%s: incomplete specification", string);
1221 return (-1);
1222 }
1223 for (i = 0; i < namelist->size; i++)
1224 if (namelist->list[i].ctl_name != NULL &&
1225 strcmp(name, namelist->list[i].ctl_name) == 0)
1226 break;
1227 if (i == namelist->size) {
1228 warnx("%s level name %s in %s is invalid",
1229 level, name, string);
1230 return (-1);
1231 }
1232 return (i);
1233 }
1234
1235 static void
1236 usage(void)
1237 {
1238 const char *progname = getprogname();
1239
1240 (void)fprintf(stderr,
1241 "Usage:\t%s %s\n\t%s %s\n\t%s %s\n\t%s %s\n\t%s %s\n",
1242 progname, "[-n] variable ...",
1243 progname, "[-n] [-q] -w variable=value ...",
1244 progname, "[-n] -a",
1245 progname, "[-n] -A",
1246 progname, "[-n] [-q] -f file");
1247 exit(1);
1248 }
1249