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