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