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