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