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