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