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