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