xref: /freebsd/sys/kern/kern_sysctl.c (revision 41466b50c1d5bfd1cf6adaae547a579a75d7c04e)
1 /*-
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Mike Karels at Berkeley Software Design, Inc.
7  *
8  * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
9  * project, to make these variables more userfriendly.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *	@(#)kern_sysctl.c	8.4 (Berkeley) 4/14/94
40  * $FreeBSD$
41  */
42 
43 #include "opt_compat.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/sysctl.h>
49 #include <sys/malloc.h>
50 #include <sys/proc.h>
51 #include <sys/lock.h>
52 #include <sys/mutex.h>
53 #include <sys/sysproto.h>
54 #include <vm/vm.h>
55 #include <vm/vm_extern.h>
56 
57 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
58 static MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
59 
60 /*
61  * Locking and stats
62  */
63 static struct sysctl_lock {
64 	int	sl_lock;
65 	int	sl_want;
66 	int	sl_locked;
67 } memlock;
68 
69 static int sysctl_root(SYSCTL_HANDLER_ARGS);
70 
71 struct sysctl_oid_list sysctl__children; /* root list */
72 
73 static struct sysctl_oid *
74 sysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
75 {
76 	struct sysctl_oid *oidp;
77 
78 	SLIST_FOREACH(oidp, list, oid_link) {
79 		if (strcmp(oidp->oid_name, name) == 0) {
80 			return (oidp);
81 		}
82 	}
83 	return (NULL);
84 }
85 
86 /*
87  * Initialization of the MIB tree.
88  *
89  * Order by number in each list.
90  */
91 
92 void
93 sysctl_register_oid(struct sysctl_oid *oidp)
94 {
95 	struct sysctl_oid_list *parent = oidp->oid_parent;
96 	struct sysctl_oid *p;
97 	struct sysctl_oid *q;
98 
99 	/*
100 	 * First check if another oid with the same name already
101 	 * exists in the parent's list.
102 	 */
103 	p = sysctl_find_oidname(oidp->oid_name, parent);
104 	if (p != NULL) {
105 		if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
106 			p->oid_refcnt++;
107 			return;
108 		} else {
109 			printf("can't re-use a leaf (%s)!\n", p->oid_name);
110 			return;
111 		}
112 	}
113 	/*
114 	 * If this oid has a number OID_AUTO, give it a number which
115 	 * is greater than any current oid.
116 	 * NOTE: DO NOT change the starting value here, change it in
117 	 * <sys/sysctl.h>, and make sure it is at least 256 to
118 	 * accomodate e.g. net.inet.raw as a static sysctl node.
119 	 */
120 	if (oidp->oid_number == OID_AUTO) {
121 		static int newoid = CTL_AUTO_START;
122 
123 		oidp->oid_number = newoid++;
124 		if (newoid == 0x7fffffff)
125 			panic("out of oids");
126 	}
127 #if 0
128 	else if (oidp->oid_number >= CTL_AUTO_START) {
129 		/* do not panic; this happens when unregistering sysctl sets */
130 		printf("static sysctl oid too high: %d", oidp->oid_number);
131 	}
132 #endif
133 
134 	/*
135 	 * Insert the oid into the parent's list in order.
136 	 */
137 	q = NULL;
138 	SLIST_FOREACH(p, parent, oid_link) {
139 		if (oidp->oid_number < p->oid_number)
140 			break;
141 		q = p;
142 	}
143 	if (q)
144 		SLIST_INSERT_AFTER(q, oidp, oid_link);
145 	else
146 		SLIST_INSERT_HEAD(parent, oidp, oid_link);
147 }
148 
149 void
150 sysctl_unregister_oid(struct sysctl_oid *oidp)
151 {
152 	SLIST_REMOVE(oidp->oid_parent, oidp, sysctl_oid, oid_link);
153 }
154 
155 /* Initialize a new context to keep track of dynamically added sysctls. */
156 int
157 sysctl_ctx_init(struct sysctl_ctx_list *c)
158 {
159 
160 	if (c == NULL) {
161 		return (EINVAL);
162 	}
163 	TAILQ_INIT(c);
164 	return (0);
165 }
166 
167 /* Free the context, and destroy all dynamic oids registered in this context */
168 int
169 sysctl_ctx_free(struct sysctl_ctx_list *clist)
170 {
171 	struct sysctl_ctx_entry *e, *e1;
172 	int error;
173 
174 	error = 0;
175 	/*
176 	 * First perform a "dry run" to check if it's ok to remove oids.
177 	 * XXX FIXME
178 	 * XXX This algorithm is a hack. But I don't know any
179 	 * XXX better solution for now...
180 	 */
181 	TAILQ_FOREACH(e, clist, link) {
182 		error = sysctl_remove_oid(e->entry, 0, 0);
183 		if (error)
184 			break;
185 	}
186 	/*
187 	 * Restore deregistered entries, either from the end,
188 	 * or from the place where error occured.
189 	 * e contains the entry that was not unregistered
190 	 */
191 	if (error)
192 		e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
193 	else
194 		e1 = TAILQ_LAST(clist, sysctl_ctx_list);
195 	while (e1 != NULL) {
196 		sysctl_register_oid(e1->entry);
197 		e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
198 	}
199 	if (error)
200 		return(EBUSY);
201 	/* Now really delete the entries */
202 	e = TAILQ_FIRST(clist);
203 	while (e != NULL) {
204 		e1 = TAILQ_NEXT(e, link);
205 		error = sysctl_remove_oid(e->entry, 1, 0);
206 		if (error)
207 			panic("sysctl_remove_oid: corrupt tree, entry: %s",
208 			    e->entry->oid_name);
209 		free(e, M_SYSCTLOID);
210 		e = e1;
211 	}
212 	return (error);
213 }
214 
215 /* Add an entry to the context */
216 struct sysctl_ctx_entry *
217 sysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
218 {
219 	struct sysctl_ctx_entry *e;
220 
221 	if (clist == NULL || oidp == NULL)
222 		return(NULL);
223 	e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
224 	e->entry = oidp;
225 	TAILQ_INSERT_HEAD(clist, e, link);
226 	return (e);
227 }
228 
229 /* Find an entry in the context */
230 struct sysctl_ctx_entry *
231 sysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
232 {
233 	struct sysctl_ctx_entry *e;
234 
235 	if (clist == NULL || oidp == NULL)
236 		return(NULL);
237 	TAILQ_FOREACH(e, clist, link) {
238 		if(e->entry == oidp)
239 			return(e);
240 	}
241 	return (e);
242 }
243 
244 /*
245  * Delete an entry from the context.
246  * NOTE: this function doesn't free oidp! You have to remove it
247  * with sysctl_remove_oid().
248  */
249 int
250 sysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
251 {
252 	struct sysctl_ctx_entry *e;
253 
254 	if (clist == NULL || oidp == NULL)
255 		return (EINVAL);
256 	e = sysctl_ctx_entry_find(clist, oidp);
257 	if (e != NULL) {
258 		TAILQ_REMOVE(clist, e, link);
259 		free(e, M_SYSCTLOID);
260 		return (0);
261 	} else
262 		return (ENOENT);
263 }
264 
265 /*
266  * Remove dynamically created sysctl trees.
267  * oidp - top of the tree to be removed
268  * del - if 0 - just deregister, otherwise free up entries as well
269  * recurse - if != 0 traverse the subtree to be deleted
270  */
271 int
272 sysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
273 {
274 	struct sysctl_oid *p;
275 	int error;
276 
277 	if (oidp == NULL)
278 		return(EINVAL);
279 	if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
280 		printf("can't remove non-dynamic nodes!\n");
281 		return (EINVAL);
282 	}
283 	/*
284 	 * WARNING: normal method to do this should be through
285 	 * sysctl_ctx_free(). Use recursing as the last resort
286 	 * method to purge your sysctl tree of leftovers...
287 	 * However, if some other code still references these nodes,
288 	 * it will panic.
289 	 */
290 	if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
291 		if (oidp->oid_refcnt == 1) {
292 			SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
293 				if (!recurse)
294 					return (ENOTEMPTY);
295 				error = sysctl_remove_oid(p, del, recurse);
296 				if (error)
297 					return (error);
298 			}
299 			if (del)
300 				free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
301 		}
302 	}
303 	if (oidp->oid_refcnt > 1 ) {
304 		oidp->oid_refcnt--;
305 	} else {
306 		if (oidp->oid_refcnt == 0) {
307 			printf("Warning: bad oid_refcnt=%u (%s)!\n",
308 				oidp->oid_refcnt, oidp->oid_name);
309 			return (EINVAL);
310 		}
311 		sysctl_unregister_oid(oidp);
312 		if (del) {
313 			free((void *)(uintptr_t)(const void *)oidp->oid_name,
314 			     M_SYSCTLOID);
315 			free(oidp, M_SYSCTLOID);
316 		}
317 	}
318 	return (0);
319 }
320 
321 /*
322  * Create new sysctls at run time.
323  * clist may point to a valid context initialized with sysctl_ctx_init().
324  */
325 struct sysctl_oid *
326 sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
327 	int number, const char *name, int kind, void *arg1, int arg2,
328 	int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
329 {
330 	struct sysctl_oid *oidp;
331 	ssize_t len;
332 	char *newname;
333 
334 	/* You have to hook up somewhere.. */
335 	if (parent == NULL)
336 		return(NULL);
337 	/* Check if the node already exists, otherwise create it */
338 	oidp = sysctl_find_oidname(name, parent);
339 	if (oidp != NULL) {
340 		if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
341 			oidp->oid_refcnt++;
342 			/* Update the context */
343 			if (clist != NULL)
344 				sysctl_ctx_entry_add(clist, oidp);
345 			return (oidp);
346 		} else {
347 			printf("can't re-use a leaf (%s)!\n", name);
348 			return (NULL);
349 		}
350 	}
351 	oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
352 	oidp->oid_parent = parent;
353 	SLIST_NEXT(oidp, oid_link) = NULL;
354 	oidp->oid_number = number;
355 	oidp->oid_refcnt = 1;
356 	len = strlen(name);
357 	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
358 	bcopy(name, newname, len + 1);
359 	newname[len] = '\0';
360 	oidp->oid_name = newname;
361 	oidp->oid_handler = handler;
362 	oidp->oid_kind = CTLFLAG_DYN | kind;
363 	if ((kind & CTLTYPE) == CTLTYPE_NODE) {
364 		/* Allocate space for children */
365 		SYSCTL_CHILDREN(oidp) = malloc(sizeof(struct sysctl_oid_list),
366 		    M_SYSCTLOID, M_WAITOK);
367 		SLIST_INIT(SYSCTL_CHILDREN(oidp));
368 	} else {
369 		oidp->oid_arg1 = arg1;
370 		oidp->oid_arg2 = arg2;
371 	}
372 	oidp->oid_fmt = fmt;
373 	/* Update the context, if used */
374 	if (clist != NULL)
375 		sysctl_ctx_entry_add(clist, oidp);
376 	/* Register this oid */
377 	sysctl_register_oid(oidp);
378 	return (oidp);
379 }
380 
381 /*
382  * Register the kernel's oids on startup.
383  */
384 SET_DECLARE(sysctl_set, struct sysctl_oid);
385 
386 static void
387 sysctl_register_all(void *arg)
388 {
389 	struct sysctl_oid **oidp;
390 
391 	SET_FOREACH(oidp, sysctl_set)
392 		sysctl_register_oid(*oidp);
393 }
394 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
395 
396 /*
397  * "Staff-functions"
398  *
399  * These functions implement a presently undocumented interface
400  * used by the sysctl program to walk the tree, and get the type
401  * so it can print the value.
402  * This interface is under work and consideration, and should probably
403  * be killed with a big axe by the first person who can find the time.
404  * (be aware though, that the proper interface isn't as obvious as it
405  * may seem, there are various conflicting requirements.
406  *
407  * {0,0}	printf the entire MIB-tree.
408  * {0,1,...}	return the name of the "..." OID.
409  * {0,2,...}	return the next OID.
410  * {0,3}	return the OID of the name in "new"
411  * {0,4,...}	return the kind & format info for the "..." OID.
412  */
413 
414 static void
415 sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
416 {
417 	int k;
418 	struct sysctl_oid *oidp;
419 
420 	SLIST_FOREACH(oidp, l, oid_link) {
421 
422 		for (k=0; k<i; k++)
423 			printf(" ");
424 
425 		printf("%d %s ", oidp->oid_number, oidp->oid_name);
426 
427 		printf("%c%c",
428 			oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
429 			oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
430 
431 		if (oidp->oid_handler)
432 			printf(" *Handler");
433 
434 		switch (oidp->oid_kind & CTLTYPE) {
435 			case CTLTYPE_NODE:
436 				printf(" Node\n");
437 				if (!oidp->oid_handler) {
438 					sysctl_sysctl_debug_dump_node(
439 						oidp->oid_arg1, i+2);
440 				}
441 				break;
442 			case CTLTYPE_INT:    printf(" Int\n"); break;
443 			case CTLTYPE_STRING: printf(" String\n"); break;
444 			case CTLTYPE_QUAD:   printf(" Quad\n"); break;
445 			case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
446 			default:	     printf("\n");
447 		}
448 
449 	}
450 }
451 
452 static int
453 sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
454 {
455 	sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
456 	return ENOENT;
457 }
458 
459 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
460 	0, 0, sysctl_sysctl_debug, "-", "");
461 
462 static int
463 sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
464 {
465 	int *name = (int *) arg1;
466 	u_int namelen = arg2;
467 	int error = 0;
468 	struct sysctl_oid *oid;
469 	struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
470 	char buf[10];
471 
472 	while (namelen) {
473 		if (!lsp) {
474 			snprintf(buf,sizeof(buf),"%d",*name);
475 			if (req->oldidx)
476 				error = SYSCTL_OUT(req, ".", 1);
477 			if (!error)
478 				error = SYSCTL_OUT(req, buf, strlen(buf));
479 			if (error)
480 				return (error);
481 			namelen--;
482 			name++;
483 			continue;
484 		}
485 		lsp2 = 0;
486 		SLIST_FOREACH(oid, lsp, oid_link) {
487 			if (oid->oid_number != *name)
488 				continue;
489 
490 			if (req->oldidx)
491 				error = SYSCTL_OUT(req, ".", 1);
492 			if (!error)
493 				error = SYSCTL_OUT(req, oid->oid_name,
494 					strlen(oid->oid_name));
495 			if (error)
496 				return (error);
497 
498 			namelen--;
499 			name++;
500 
501 			if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
502 				break;
503 
504 			if (oid->oid_handler)
505 				break;
506 
507 			lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
508 			break;
509 		}
510 		lsp = lsp2;
511 	}
512 	return (SYSCTL_OUT(req, "", 1));
513 }
514 
515 SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
516 
517 static int
518 sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
519 	int *next, int *len, int level, struct sysctl_oid **oidpp)
520 {
521 	struct sysctl_oid *oidp;
522 
523 	*len = level;
524 	SLIST_FOREACH(oidp, lsp, oid_link) {
525 		*next = oidp->oid_number;
526 		*oidpp = oidp;
527 
528 		if (!namelen) {
529 			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
530 				return 0;
531 			if (oidp->oid_handler)
532 				/* We really should call the handler here...*/
533 				return 0;
534 			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
535 			if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
536 				len, level+1, oidpp))
537 				return 0;
538 			goto next;
539 		}
540 
541 		if (oidp->oid_number < *name)
542 			continue;
543 
544 		if (oidp->oid_number > *name) {
545 			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
546 				return 0;
547 			if (oidp->oid_handler)
548 				return 0;
549 			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
550 			if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
551 				next+1, len, level+1, oidpp))
552 				return (0);
553 			goto next;
554 		}
555 		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
556 			continue;
557 
558 		if (oidp->oid_handler)
559 			continue;
560 
561 		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
562 		if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
563 			len, level+1, oidpp))
564 			return (0);
565 	next:
566 		namelen = 1;
567 		*len = level;
568 	}
569 	return 1;
570 }
571 
572 static int
573 sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
574 {
575 	int *name = (int *) arg1;
576 	u_int namelen = arg2;
577 	int i, j, error;
578 	struct sysctl_oid *oid;
579 	struct sysctl_oid_list *lsp = &sysctl__children;
580 	int newoid[CTL_MAXNAME];
581 
582 	i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
583 	if (i)
584 		return ENOENT;
585 	error = SYSCTL_OUT(req, newoid, j * sizeof (int));
586 	return (error);
587 }
588 
589 SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
590 
591 static int
592 name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
593 {
594 	int i;
595 	struct sysctl_oid *oidp;
596 	struct sysctl_oid_list *lsp = &sysctl__children;
597 	char *p;
598 
599 	if (!*name)
600 		return ENOENT;
601 
602 	p = name + strlen(name) - 1 ;
603 	if (*p == '.')
604 		*p = '\0';
605 
606 	*len = 0;
607 
608 	for (p = name; *p && *p != '.'; p++)
609 		;
610 	i = *p;
611 	if (i == '.')
612 		*p = '\0';
613 
614 	oidp = SLIST_FIRST(lsp);
615 
616 	while (oidp && *len < CTL_MAXNAME) {
617 		if (strcmp(name, oidp->oid_name)) {
618 			oidp = SLIST_NEXT(oidp, oid_link);
619 			continue;
620 		}
621 		*oid++ = oidp->oid_number;
622 		(*len)++;
623 
624 		if (!i) {
625 			if (oidpp)
626 				*oidpp = oidp;
627 			return (0);
628 		}
629 
630 		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
631 			break;
632 
633 		if (oidp->oid_handler)
634 			break;
635 
636 		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
637 		oidp = SLIST_FIRST(lsp);
638 		name = p+1;
639 		for (p = name; *p && *p != '.'; p++)
640 				;
641 		i = *p;
642 		if (i == '.')
643 			*p = '\0';
644 	}
645 	return ENOENT;
646 }
647 
648 static int
649 sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
650 {
651 	char *p;
652 	int error, oid[CTL_MAXNAME], len;
653 	struct sysctl_oid *op = 0;
654 
655 	if (!req->newlen)
656 		return ENOENT;
657 	if (req->newlen >= MAXPATHLEN)	/* XXX arbitrary, undocumented */
658 		return (ENAMETOOLONG);
659 
660 	p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
661 
662 	error = SYSCTL_IN(req, p, req->newlen);
663 	if (error) {
664 		free(p, M_SYSCTL);
665 		return (error);
666 	}
667 
668 	p [req->newlen] = '\0';
669 
670 	error = name2oid(p, oid, &len, &op);
671 
672 	free(p, M_SYSCTL);
673 
674 	if (error)
675 		return (error);
676 
677 	error = SYSCTL_OUT(req, oid, len * sizeof *oid);
678 	return (error);
679 }
680 
681 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
682 	sysctl_sysctl_name2oid, "I", "");
683 
684 static int
685 sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
686 {
687 	struct sysctl_oid *oid;
688 	int error;
689 
690 	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
691 	if (error)
692 		return (error);
693 
694 	if (!oid->oid_fmt)
695 		return (ENOENT);
696 	error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
697 	if (error)
698 		return (error);
699 	error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
700 	return (error);
701 }
702 
703 
704 SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
705 
706 /*
707  * Default "handler" functions.
708  */
709 
710 /*
711  * Handle an int, signed or unsigned.
712  * Two cases:
713  *     a variable:  point arg1 at it.
714  *     a constant:  pass it in arg2.
715  */
716 
717 int
718 sysctl_handle_int(SYSCTL_HANDLER_ARGS)
719 {
720 	int error = 0;
721 
722 	if (arg1)
723 		error = SYSCTL_OUT(req, arg1, sizeof(int));
724 	else
725 		error = SYSCTL_OUT(req, &arg2, sizeof(int));
726 
727 	if (error || !req->newptr)
728 		return (error);
729 
730 	if (!arg1)
731 		error = EPERM;
732 	else
733 		error = SYSCTL_IN(req, arg1, sizeof(int));
734 	return (error);
735 }
736 
737 /*
738  * Handle a long, signed or unsigned.  arg1 points to it.
739  */
740 
741 int
742 sysctl_handle_long(SYSCTL_HANDLER_ARGS)
743 {
744 	int error = 0;
745 
746 	if (!arg1)
747 		return (EINVAL);
748 	error = SYSCTL_OUT(req, arg1, sizeof(long));
749 
750 	if (error || !req->newptr)
751 		return (error);
752 
753 	error = SYSCTL_IN(req, arg1, sizeof(long));
754 	return (error);
755 }
756 
757 /*
758  * Handle our generic '\0' terminated 'C' string.
759  * Two cases:
760  * 	a variable string:  point arg1 at it, arg2 is max length.
761  * 	a constant string:  point arg1 at it, arg2 is zero.
762  */
763 
764 int
765 sysctl_handle_string(SYSCTL_HANDLER_ARGS)
766 {
767 	int error=0;
768 
769 	error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
770 
771 	if (error || !req->newptr)
772 		return (error);
773 
774 	if ((req->newlen - req->newidx) >= arg2) {
775 		error = EINVAL;
776 	} else {
777 		arg2 = (req->newlen - req->newidx);
778 		error = SYSCTL_IN(req, arg1, arg2);
779 		((char *)arg1)[arg2] = '\0';
780 	}
781 
782 	return (error);
783 }
784 
785 /*
786  * Handle any kind of opaque data.
787  * arg1 points to it, arg2 is the size.
788  */
789 
790 int
791 sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
792 {
793 	int error;
794 
795 	error = SYSCTL_OUT(req, arg1, arg2);
796 
797 	if (error || !req->newptr)
798 		return (error);
799 
800 	error = SYSCTL_IN(req, arg1, arg2);
801 
802 	return (error);
803 }
804 
805 /*
806  * Transfer functions to/from kernel space.
807  * XXX: rather untested at this point
808  */
809 static int
810 sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
811 {
812 	size_t i = 0;
813 
814 	if (req->oldptr) {
815 		i = l;
816 		if (req->oldlen <= req->oldidx)
817 			i = 0;
818 		else
819 			if (i > req->oldlen - req->oldidx)
820 				i = req->oldlen - req->oldidx;
821 		if (i > 0)
822 			bcopy(p, (char *)req->oldptr + req->oldidx, i);
823 	}
824 	req->oldidx += l;
825 	if (req->oldptr && i != l)
826 		return (ENOMEM);
827 	return (0);
828 }
829 
830 static int
831 sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
832 {
833 	if (!req->newptr)
834 		return 0;
835 	if (req->newlen - req->newidx < l)
836 		return (EINVAL);
837 	bcopy((char *)req->newptr + req->newidx, p, l);
838 	req->newidx += l;
839 	return (0);
840 }
841 
842 int
843 kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
844     size_t *oldlenp, void *new, size_t newlen, size_t *retval)
845 {
846 	int error = 0;
847 	struct sysctl_req req;
848 
849 	bzero(&req, sizeof req);
850 
851 	req.p = td->td_proc;
852 
853 	if (oldlenp) {
854 		req.oldlen = *oldlenp;
855 	}
856 
857 	if (old) {
858 		req.oldptr= old;
859 	}
860 
861 	if (new != NULL) {
862 		req.newlen = newlen;
863 		req.newptr = new;
864 	}
865 
866 	req.oldfunc = sysctl_old_kernel;
867 	req.newfunc = sysctl_new_kernel;
868 	req.lock = 1;
869 
870 	/* XXX this should probably be done in a general way */
871 	while (memlock.sl_lock) {
872 		memlock.sl_want = 1;
873 		(void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0);
874 		memlock.sl_locked++;
875 	}
876 	memlock.sl_lock = 1;
877 
878 	error = sysctl_root(0, name, namelen, &req);
879 
880 	if (req.lock == 2)
881 		vsunlock(req.oldptr, req.oldlen);
882 
883 	memlock.sl_lock = 0;
884 
885 	if (memlock.sl_want) {
886 		memlock.sl_want = 0;
887 		wakeup((caddr_t)&memlock);
888 	}
889 
890 	if (error && error != ENOMEM)
891 		return (error);
892 
893 	if (retval) {
894 		if (req.oldptr && req.oldidx > req.oldlen)
895 			*retval = req.oldlen;
896 		else
897 			*retval = req.oldidx;
898 	}
899 	return (error);
900 }
901 
902 int
903 kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
904     void *new, size_t newlen, size_t *retval)
905 {
906         int oid[CTL_MAXNAME];
907         size_t oidlen, plen;
908 	int error;
909 
910 	oid[0] = 0;		/* sysctl internal magic */
911 	oid[1] = 3;		/* name2oid */
912 	oidlen = sizeof(oid);
913 
914 	error = kernel_sysctl(td, oid, 2, oid, &oidlen,
915 	    (void *)name, strlen(name), &plen);
916 	if (error)
917 		return (error);
918 
919 	error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
920 	    new, newlen, retval);
921 	return (error);
922 }
923 
924 /*
925  * Transfer function to/from user space.
926  */
927 static int
928 sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
929 {
930 	int error = 0;
931 	size_t i = 0;
932 
933 	if (req->lock == 1 && req->oldptr) {
934 		vslock(req->oldptr, req->oldlen);
935 		req->lock = 2;
936 	}
937 	if (req->oldptr) {
938 		i = l;
939 		if (req->oldlen <= req->oldidx)
940 			i = 0;
941 		else
942 			if (i > req->oldlen - req->oldidx)
943 				i = req->oldlen - req->oldidx;
944 		if (i > 0)
945 			error = copyout(p, (char *)req->oldptr + req->oldidx,
946 					i);
947 	}
948 	req->oldidx += l;
949 	if (error)
950 		return (error);
951 	if (req->oldptr && i < l)
952 		return (ENOMEM);
953 	return (0);
954 }
955 
956 static int
957 sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
958 {
959 	int error;
960 
961 	if (!req->newptr)
962 		return 0;
963 	if (req->newlen - req->newidx < l)
964 		return (EINVAL);
965 	error = copyin((char *)req->newptr + req->newidx, p, l);
966 	req->newidx += l;
967 	return (error);
968 }
969 
970 int
971 sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
972     int *nindx, struct sysctl_req *req)
973 {
974 	struct sysctl_oid *oid;
975 	int indx;
976 
977 	oid = SLIST_FIRST(&sysctl__children);
978 	indx = 0;
979 	while (oid && indx < CTL_MAXNAME) {
980 		if (oid->oid_number == name[indx]) {
981 			indx++;
982 			if (oid->oid_kind & CTLFLAG_NOLOCK)
983 				req->lock = 0;
984 			if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
985 				if (oid->oid_handler != NULL ||
986 				    indx == namelen) {
987 					*noid = oid;
988 					if (nindx != NULL)
989 						*nindx = indx;
990 					return (0);
991 				}
992 				oid = SLIST_FIRST(
993 				    (struct sysctl_oid_list *)oid->oid_arg1);
994 			} else if (indx == namelen) {
995 				*noid = oid;
996 				if (nindx != NULL)
997 					*nindx = indx;
998 				return (0);
999 			} else {
1000 				return (ENOTDIR);
1001 			}
1002 		} else {
1003 			oid = SLIST_NEXT(oid, oid_link);
1004 		}
1005 	}
1006 	return (ENOENT);
1007 }
1008 
1009 /*
1010  * Traverse our tree, and find the right node, execute whatever it points
1011  * to, and return the resulting error code.
1012  */
1013 
1014 int
1015 sysctl_root(SYSCTL_HANDLER_ARGS)
1016 {
1017 	struct sysctl_oid *oid;
1018 	int error, indx;
1019 
1020 	error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1021 	if (error)
1022 		return (error);
1023 
1024 	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1025 		/*
1026 		 * You can't call a sysctl when it's a node, but has
1027 		 * no handler.  Inform the user that it's a node.
1028 		 * The indx may or may not be the same as namelen.
1029 		 */
1030 		if (oid->oid_handler == NULL)
1031 			return (EISDIR);
1032 	}
1033 
1034 	/* Is this sysctl writable? */
1035 	if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1036 		return (EPERM);
1037 
1038 	/* Is this sysctl sensitive to securelevels? */
1039 	if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1040 		if (req->p == NULL) {
1041 			error = securelevel_gt(NULL, 0);	/* XXX */
1042 			if (error)
1043 				return (error);
1044 		} else {
1045 			error = securelevel_gt(req->p->p_ucred, 0);
1046 			if (error)
1047 				return (error);
1048 		}
1049 	}
1050 
1051 	/* Is this sysctl writable by only privileged users? */
1052 	if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1053 		if (req->p != NULL) {
1054 			int flags;
1055 
1056 			if (oid->oid_kind & CTLFLAG_PRISON)
1057 				flags = PRISON_ROOT;
1058 			else
1059 				flags = 0;
1060 			error = suser_xxx(NULL, req->p, flags);
1061 			if (error)
1062 				return (error);
1063 		}
1064 	}
1065 
1066 	if (!oid->oid_handler)
1067 		return EINVAL;
1068 
1069 	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1070 		error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1071 		    req);
1072 	else
1073 		error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1074 		    req);
1075 	return (error);
1076 }
1077 
1078 #ifndef _SYS_SYSPROTO_H_
1079 struct sysctl_args {
1080 	int	*name;
1081 	u_int	namelen;
1082 	void	*old;
1083 	size_t	*oldlenp;
1084 	void	*new;
1085 	size_t	newlen;
1086 };
1087 #endif
1088 
1089 /*
1090  * MPSAFE
1091  */
1092 int
1093 __sysctl(struct thread *td, struct sysctl_args *uap)
1094 {
1095 	int error, name[CTL_MAXNAME];
1096 	size_t j;
1097 
1098 	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1099 		return (EINVAL);
1100 
1101  	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1102  	if (error)
1103 		return (error);
1104 
1105 	mtx_lock(&Giant);
1106 
1107 	error = userland_sysctl(td, name, uap->namelen,
1108 		uap->old, uap->oldlenp, 0,
1109 		uap->new, uap->newlen, &j);
1110 	if (error && error != ENOMEM)
1111 		goto done2;
1112 	if (uap->oldlenp) {
1113 		int i = copyout(&j, uap->oldlenp, sizeof(j));
1114 		if (i)
1115 			error = i;
1116 	}
1117 done2:
1118 	mtx_unlock(&Giant);
1119 	return (error);
1120 }
1121 
1122 /*
1123  * This is used from various compatibility syscalls too.  That's why name
1124  * must be in kernel space.
1125  */
1126 int
1127 userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1128     size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
1129 {
1130 	int error = 0;
1131 	struct sysctl_req req, req2;
1132 
1133 	bzero(&req, sizeof req);
1134 
1135 	req.p = td->td_proc;
1136 
1137 	if (oldlenp) {
1138 		if (inkernel) {
1139 			req.oldlen = *oldlenp;
1140 		} else {
1141 			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1142 			if (error)
1143 				return (error);
1144 		}
1145 	}
1146 
1147 	if (old) {
1148 		if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1149 			return (EFAULT);
1150 		req.oldptr= old;
1151 	}
1152 
1153 	if (new != NULL) {
1154 		if (!useracc(new, req.newlen, VM_PROT_READ))
1155 			return (EFAULT);
1156 		req.newlen = newlen;
1157 		req.newptr = new;
1158 	}
1159 
1160 	req.oldfunc = sysctl_old_user;
1161 	req.newfunc = sysctl_new_user;
1162 	req.lock = 1;
1163 
1164 	/* XXX this should probably be done in a general way */
1165 	while (memlock.sl_lock) {
1166 		memlock.sl_want = 1;
1167 		(void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0);
1168 		memlock.sl_locked++;
1169 	}
1170 	memlock.sl_lock = 1;
1171 
1172 	do {
1173 	    req2 = req;
1174 	    error = sysctl_root(0, name, namelen, &req2);
1175 	} while (error == EAGAIN);
1176 
1177 	req = req2;
1178 	if (req.lock == 2)
1179 		vsunlock(req.oldptr, req.oldlen);
1180 
1181 	memlock.sl_lock = 0;
1182 
1183 	if (memlock.sl_want) {
1184 		memlock.sl_want = 0;
1185 		wakeup((caddr_t)&memlock);
1186 	}
1187 
1188 	if (error && error != ENOMEM)
1189 		return (error);
1190 
1191 	if (retval) {
1192 		if (req.oldptr && req.oldidx > req.oldlen)
1193 			*retval = req.oldlen;
1194 		else
1195 			*retval = req.oldidx;
1196 	}
1197 	return (error);
1198 }
1199 
1200 #ifdef COMPAT_43
1201 #include <sys/socket.h>
1202 #include <vm/vm_param.h>
1203 
1204 #define	KINFO_PROC		(0<<8)
1205 #define	KINFO_RT		(1<<8)
1206 #define	KINFO_VNODE		(2<<8)
1207 #define	KINFO_FILE		(3<<8)
1208 #define	KINFO_METER		(4<<8)
1209 #define	KINFO_LOADAVG		(5<<8)
1210 #define	KINFO_CLOCKRATE		(6<<8)
1211 
1212 /* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
1213 #define	KINFO_BSDI_SYSINFO	(101<<8)
1214 
1215 /*
1216  * XXX this is bloat, but I hope it's better here than on the potentially
1217  * limited kernel stack...  -Peter
1218  */
1219 
1220 static struct {
1221 	int	bsdi_machine;		/* "i386" on BSD/386 */
1222 /*      ^^^ this is an offset to the string, relative to the struct start */
1223 	char	*pad0;
1224 	long	pad1;
1225 	long	pad2;
1226 	long	pad3;
1227 	u_long	pad4;
1228 	u_long	pad5;
1229 	u_long	pad6;
1230 
1231 	int	bsdi_ostype;		/* "BSD/386" on BSD/386 */
1232 	int	bsdi_osrelease;		/* "1.1" on BSD/386 */
1233 	long	pad7;
1234 	long	pad8;
1235 	char	*pad9;
1236 
1237 	long	pad10;
1238 	long	pad11;
1239 	int	pad12;
1240 	long	pad13;
1241 	quad_t	pad14;
1242 	long	pad15;
1243 
1244 	struct	timeval pad16;
1245 	/* we dont set this, because BSDI's uname used gethostname() instead */
1246 	int	bsdi_hostname;		/* hostname on BSD/386 */
1247 
1248 	/* the actual string data is appended here */
1249 
1250 } bsdi_si;
1251 /*
1252  * this data is appended to the end of the bsdi_si structure during copyout.
1253  * The "char *" offsets are relative to the base of the bsdi_si struct.
1254  * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
1255  * should not exceed the length of the buffer here... (or else!! :-)
1256  */
1257 static char bsdi_strings[80];	/* It had better be less than this! */
1258 
1259 #ifndef _SYS_SYSPROTO_H_
1260 struct getkerninfo_args {
1261 	int	op;
1262 	char	*where;
1263 	size_t	*size;
1264 	int	arg;
1265 };
1266 #endif
1267 
1268 /*
1269  * MPSAFE
1270  */
1271 int
1272 ogetkerninfo(struct thread *td, struct getkerninfo_args *uap)
1273 {
1274 	int error, name[6];
1275 	size_t size;
1276 	u_int needed = 0;
1277 
1278 	mtx_lock(&Giant);
1279 
1280 	switch (uap->op & 0xff00) {
1281 
1282 	case KINFO_RT:
1283 		name[0] = CTL_NET;
1284 		name[1] = PF_ROUTE;
1285 		name[2] = 0;
1286 		name[3] = (uap->op & 0xff0000) >> 16;
1287 		name[4] = uap->op & 0xff;
1288 		name[5] = uap->arg;
1289 		error = userland_sysctl(td, name, 6, uap->where, uap->size,
1290 			0, 0, 0, &size);
1291 		break;
1292 
1293 	case KINFO_VNODE:
1294 		name[0] = CTL_KERN;
1295 		name[1] = KERN_VNODE;
1296 		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1297 			0, 0, 0, &size);
1298 		break;
1299 
1300 	case KINFO_PROC:
1301 		name[0] = CTL_KERN;
1302 		name[1] = KERN_PROC;
1303 		name[2] = uap->op & 0xff;
1304 		name[3] = uap->arg;
1305 		error = userland_sysctl(td, name, 4, uap->where, uap->size,
1306 			0, 0, 0, &size);
1307 		break;
1308 
1309 	case KINFO_FILE:
1310 		name[0] = CTL_KERN;
1311 		name[1] = KERN_FILE;
1312 		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1313 			0, 0, 0, &size);
1314 		break;
1315 
1316 	case KINFO_METER:
1317 		name[0] = CTL_VM;
1318 		name[1] = VM_METER;
1319 		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1320 			0, 0, 0, &size);
1321 		break;
1322 
1323 	case KINFO_LOADAVG:
1324 		name[0] = CTL_VM;
1325 		name[1] = VM_LOADAVG;
1326 		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1327 			0, 0, 0, &size);
1328 		break;
1329 
1330 	case KINFO_CLOCKRATE:
1331 		name[0] = CTL_KERN;
1332 		name[1] = KERN_CLOCKRATE;
1333 		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1334 			0, 0, 0, &size);
1335 		break;
1336 
1337 	case KINFO_BSDI_SYSINFO: {
1338 		/*
1339 		 * this is pretty crude, but it's just enough for uname()
1340 		 * from BSDI's 1.x libc to work.
1341 		 *
1342 		 * *size gives the size of the buffer before the call, and
1343 		 * the amount of data copied after a successful call.
1344 		 * If successful, the return value is the amount of data
1345 		 * available, which can be larger than *size.
1346 		 *
1347 		 * BSDI's 2.x product apparently fails with ENOMEM if *size
1348 		 * is too small.
1349 		 */
1350 
1351 		u_int left;
1352 		char *s;
1353 
1354 		bzero((char *)&bsdi_si, sizeof(bsdi_si));
1355 		bzero(bsdi_strings, sizeof(bsdi_strings));
1356 
1357 		s = bsdi_strings;
1358 
1359 		bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
1360 		strcpy(s, ostype);
1361 		s += strlen(s) + 1;
1362 
1363 		bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
1364 		strcpy(s, osrelease);
1365 		s += strlen(s) + 1;
1366 
1367 		bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
1368 		strcpy(s, machine);
1369 		s += strlen(s) + 1;
1370 
1371 		needed = sizeof(bsdi_si) + (s - bsdi_strings);
1372 
1373 		if ((uap->where == NULL) || (uap->size == NULL)) {
1374 			/* process is asking how much buffer to supply.. */
1375 			size = needed;
1376 			error = 0;
1377 			break;
1378 		}
1379 
1380 		if ((error = copyin(uap->size, &size, sizeof(size))) != 0)
1381 			break;
1382 
1383 		/* if too much buffer supplied, trim it down */
1384 		if (size > needed)
1385 			size = needed;
1386 
1387 		/* how much of the buffer is remaining */
1388 		left = size;
1389 
1390 		if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
1391 			break;
1392 
1393 		/* is there any point in continuing? */
1394 		if (left > sizeof(bsdi_si)) {
1395 			left -= sizeof(bsdi_si);
1396 			error = copyout(&bsdi_strings,
1397 					uap->where + sizeof(bsdi_si), left);
1398 		}
1399 		break;
1400 	}
1401 
1402 	default:
1403 		error = EOPNOTSUPP;
1404 		break;
1405 	}
1406 	if (error == 0) {
1407 		td->td_retval[0] = needed ? needed : size;
1408 		if (uap->size) {
1409 			error = copyout((caddr_t)&size, (caddr_t)uap->size,
1410 				    sizeof(size));
1411 		}
1412 	}
1413 	mtx_unlock(&Giant);
1414 	return (error);
1415 }
1416 #endif /* COMPAT_43 */
1417