xref: /freebsd/sys/kern/kern_sysctl.c (revision 33b77e2decd50e53798014b70bf7ca3bdc4c0c7e)
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  * $Id: kern_sysctl.c,v 1.73 1997/11/06 19:29:15 phk Exp $
41  */
42 
43 #include "opt_compat.h"
44 
45 #include <sys/param.h>
46 #include <sys/buf.h>
47 #include <sys/kernel.h>
48 #include <sys/sysctl.h>
49 #include <sys/malloc.h>
50 #include <sys/proc.h>
51 #include <sys/systm.h>
52 #include <sys/sysproto.h>
53 #include <vm/vm.h>
54 #include <vm/vm_extern.h>
55 
56 static MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
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 extern struct linker_set sysctl_;
70 
71 /*
72  * Initialization of the MIB tree.
73  *
74  * Order by number in each linker_set.
75  */
76 
77 static int
78 sysctl_order_cmp(const void *a, const void *b)
79 {
80 	struct sysctl_oid const * const *pa;
81 	struct sysctl_oid const * const *pb;
82 
83 	pa = (struct sysctl_oid const * const *)a;
84 	pb = (struct sysctl_oid const * const *)b;
85 	if (*pa == NULL)
86 		return (1);
87 	if (*pb == NULL)
88 		return (-1);
89 	return ((*pa)->oid_number - (*pb)->oid_number);
90 }
91 
92 static void
93 sysctl_order(void *arg)
94 {
95 	int j, k;
96 	struct linker_set *l = (struct linker_set *) arg;
97 	struct sysctl_oid **oidpp;
98 
99 	/* First, find the highest oid we have */
100 	j = l->ls_length;
101 	oidpp = (struct sysctl_oid **) l->ls_items;
102 	for (k = 0; j--; oidpp++) {
103 		if ((*oidpp)->oid_arg1 == arg) {
104 			*oidpp = 0;
105 			continue;
106 		}
107 		if (*oidpp && (*oidpp)->oid_number > k)
108 			k = (*oidpp)->oid_number;
109 	}
110 
111 	/* Next, replace all OID_AUTO oids with new numbers */
112 	j = l->ls_length;
113 	oidpp = (struct sysctl_oid **) l->ls_items;
114 	k += 100;
115 	for (; j--; oidpp++)
116 		if (*oidpp && (*oidpp)->oid_number == OID_AUTO)
117 			(*oidpp)->oid_number = k++;
118 
119 	/* Finally: sort by oid */
120 	j = l->ls_length;
121 	oidpp = (struct sysctl_oid **) l->ls_items;
122 	for (; j--; oidpp++) {
123 		if (!*oidpp)
124 			continue;
125 		if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE)
126 			if (!(*oidpp)->oid_handler)
127 				sysctl_order((*oidpp)->oid_arg1);
128 	}
129 	qsort(l->ls_items, l->ls_length, sizeof l->ls_items[0],
130 		sysctl_order_cmp);
131 }
132 
133 SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_order, &sysctl_);
134 
135 /*
136  * "Staff-functions"
137  *
138  * These functions implement a presently undocumented interface
139  * used by the sysctl program to walk the tree, and get the type
140  * so it can print the value.
141  * This interface is under work and consideration, and should probably
142  * be killed with a big axe by the first person who can find the time.
143  * (be aware though, that the proper interface isn't as obvious as it
144  * may seem, there are various conflicting requirements.
145  *
146  * {0,0}	printf the entire MIB-tree.
147  * {0,1,...}	return the name of the "..." OID.
148  * {0,2,...}	return the next OID.
149  * {0,3}	return the OID of the name in "new"
150  * {0,4,...}	return the kind & format info for the "..." OID.
151  */
152 
153 static void
154 sysctl_sysctl_debug_dump_node(struct linker_set *l, int i)
155 {
156 	int j, k;
157 	struct sysctl_oid **oidpp;
158 
159 	j = l->ls_length;
160 	oidpp = (struct sysctl_oid **) l->ls_items;
161 	for (; j--; oidpp++) {
162 
163 		if (!*oidpp)
164 			continue;
165 
166 		for (k=0; k<i; k++)
167 			printf(" ");
168 
169 		printf("%d %s ", (*oidpp)->oid_number, (*oidpp)->oid_name);
170 
171 		printf("%c%c",
172 			(*oidpp)->oid_kind & CTLFLAG_RD ? 'R':' ',
173 			(*oidpp)->oid_kind & CTLFLAG_WR ? 'W':' ');
174 
175 		if ((*oidpp)->oid_handler)
176 			printf(" *Handler");
177 
178 		switch ((*oidpp)->oid_kind & CTLTYPE) {
179 			case CTLTYPE_NODE:
180 				printf(" Node\n");
181 				if (!(*oidpp)->oid_handler) {
182 					sysctl_sysctl_debug_dump_node(
183 						(*oidpp)->oid_arg1, i+2);
184 				}
185 				break;
186 			case CTLTYPE_INT:    printf(" Int\n"); break;
187 			case CTLTYPE_STRING: printf(" String\n"); break;
188 			case CTLTYPE_QUAD:   printf(" Quad\n"); break;
189 			case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
190 			default:	     printf("\n");
191 		}
192 
193 	}
194 }
195 
196 static int
197 sysctl_sysctl_debug SYSCTL_HANDLER_ARGS
198 {
199 	sysctl_sysctl_debug_dump_node(&sysctl_, 0);
200 	return ENOENT;
201 }
202 
203 SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
204 	0, 0, sysctl_sysctl_debug, "-", "");
205 
206 static int
207 sysctl_sysctl_name SYSCTL_HANDLER_ARGS
208 {
209 	int *name = (int *) arg1;
210 	u_int namelen = arg2;
211 	int i, j, error = 0;
212 	struct sysctl_oid **oidpp;
213 	struct linker_set *lsp = &sysctl_;
214 	char buf[10];
215 
216 	while (namelen) {
217 		if (!lsp) {
218 			sprintf(buf,"%d",*name);
219 			if (req->oldidx)
220 				error = SYSCTL_OUT(req, ".", 1);
221 			if (!error)
222 				error = SYSCTL_OUT(req, buf, strlen(buf));
223 			if (error)
224 				return (error);
225 			namelen--;
226 			name++;
227 			continue;
228 		}
229 		oidpp = (struct sysctl_oid **) lsp->ls_items;
230 		j = lsp->ls_length;
231 		lsp = 0;
232 		for (i = 0; i < j; i++, oidpp++) {
233 			if (*oidpp && ((*oidpp)->oid_number != *name))
234 				continue;
235 
236 			if (req->oldidx)
237 				error = SYSCTL_OUT(req, ".", 1);
238 			if (!error)
239 				error = SYSCTL_OUT(req, (*oidpp)->oid_name,
240 					strlen((*oidpp)->oid_name));
241 			if (error)
242 				return (error);
243 
244 			namelen--;
245 			name++;
246 
247 			if (((*oidpp)->oid_kind & CTLTYPE) != CTLTYPE_NODE)
248 				break;
249 
250 			if ((*oidpp)->oid_handler)
251 				break;
252 
253 			lsp = (struct linker_set*)(*oidpp)->oid_arg1;
254 			break;
255 		}
256 	}
257 	return (SYSCTL_OUT(req, "", 1));
258 }
259 
260 SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
261 
262 static int
263 sysctl_sysctl_next_ls (struct linker_set *lsp, int *name, u_int namelen,
264 	int *next, int *len, int level, struct sysctl_oid **oidp)
265 {
266 	int i, j;
267 	struct sysctl_oid **oidpp;
268 
269 	oidpp = (struct sysctl_oid **) lsp->ls_items;
270 	j = lsp->ls_length;
271 	*len = level;
272 	for (i = 0; i < j; i++, oidpp++) {
273 		if (!*oidpp)
274 			continue;
275 
276 		*next = (*oidpp)->oid_number;
277 		*oidp = *oidpp;
278 
279 		if (!namelen) {
280 			if (((*oidpp)->oid_kind & CTLTYPE) != CTLTYPE_NODE)
281 				return 0;
282 			if ((*oidpp)->oid_handler)
283 				/* We really should call the handler here...*/
284 				return 0;
285 			lsp = (struct linker_set*)(*oidpp)->oid_arg1;
286 			if (!sysctl_sysctl_next_ls (lsp, 0, 0, next+1,
287 				len, level+1, oidp))
288 				return 0;
289 			goto next;
290 		}
291 
292 		if ((*oidpp)->oid_number < *name)
293 			continue;
294 
295 		if ((*oidpp)->oid_number > *name) {
296 			if (((*oidpp)->oid_kind & CTLTYPE) != CTLTYPE_NODE)
297 				return 0;
298 			if ((*oidpp)->oid_handler)
299 				return 0;
300 			lsp = (struct linker_set*)(*oidpp)->oid_arg1;
301 			if (!sysctl_sysctl_next_ls (lsp, name+1, namelen-1,
302 				next+1, len, level+1, oidp))
303 				return (0);
304 			goto next;
305 		}
306 		if (((*oidpp)->oid_kind & CTLTYPE) != CTLTYPE_NODE)
307 			continue;
308 
309 		if ((*oidpp)->oid_handler)
310 			continue;
311 
312 		lsp = (struct linker_set*)(*oidpp)->oid_arg1;
313 		if (!sysctl_sysctl_next_ls (lsp, name+1, namelen-1, next+1,
314 			len, level+1, oidp))
315 			return (0);
316 	next:
317 		namelen = 1;
318 		*len = level;
319 	}
320 	return 1;
321 }
322 
323 static int
324 sysctl_sysctl_next SYSCTL_HANDLER_ARGS
325 {
326 	int *name = (int *) arg1;
327 	u_int namelen = arg2;
328 	int i, j, error;
329 	struct sysctl_oid *oid;
330 	struct linker_set *lsp = &sysctl_;
331 	int newoid[CTL_MAXNAME];
332 
333 	i = sysctl_sysctl_next_ls (lsp, name, namelen, newoid, &j, 1, &oid);
334 	if (i)
335 		return ENOENT;
336 	error = SYSCTL_OUT(req, newoid, j * sizeof (int));
337 	return (error);
338 }
339 
340 SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
341 
342 static int
343 name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidp)
344 {
345 	int i, j;
346 	struct sysctl_oid **oidpp;
347 	struct linker_set *lsp = &sysctl_;
348 	char *p;
349 
350 	if (!*name)
351 		return ENOENT;
352 
353 	p = name + strlen(name) - 1 ;
354 	if (*p == '.')
355 		*p = '\0';
356 
357 	*len = 0;
358 
359 	for (p = name; *p && *p != '.'; p++)
360 		;
361 	i = *p;
362 	if (i == '.')
363 		*p = '\0';
364 
365 	j = lsp->ls_length;
366 	oidpp = (struct sysctl_oid **) lsp->ls_items;
367 
368 	while (j-- && *len < CTL_MAXNAME) {
369 		if (!*oidpp)
370 			continue;
371 		if (strcmp(name, (*oidpp)->oid_name)) {
372 			oidpp++;
373 			continue;
374 		}
375 		*oid++ = (*oidpp)->oid_number;
376 		(*len)++;
377 
378 		if (!i) {
379 			if (oidp)
380 				*oidp = *oidpp;
381 			return (0);
382 		}
383 
384 		if (((*oidpp)->oid_kind & CTLTYPE) != CTLTYPE_NODE)
385 			break;
386 
387 		if ((*oidpp)->oid_handler)
388 			break;
389 
390 		lsp = (struct linker_set*)(*oidpp)->oid_arg1;
391 		j = lsp->ls_length;
392 		oidpp = (struct sysctl_oid **)lsp->ls_items;
393 		name = p+1;
394 		for (p = name; *p && *p != '.'; p++)
395 				;
396 		i = *p;
397 		if (i == '.')
398 			*p = '\0';
399 	}
400 	return ENOENT;
401 }
402 
403 static int
404 sysctl_sysctl_name2oid SYSCTL_HANDLER_ARGS
405 {
406 	char *p;
407 	int error, oid[CTL_MAXNAME], len;
408 	struct sysctl_oid *op = 0;
409 
410 	if (!req->newlen)
411 		return ENOENT;
412 
413 	p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
414 
415 	error = SYSCTL_IN(req, p, req->newlen);
416 	if (error) {
417 		free(p, M_SYSCTL);
418 		return (error);
419 	}
420 
421 	p [req->newlen] = '\0';
422 
423 	error = name2oid(p, oid, &len, &op);
424 
425 	free(p, M_SYSCTL);
426 
427 	if (error)
428 		return (error);
429 
430 	error = SYSCTL_OUT(req, oid, len * sizeof *oid);
431 	return (error);
432 }
433 
434 SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
435 	sysctl_sysctl_name2oid, "I", "");
436 
437 static int
438 sysctl_sysctl_oidfmt SYSCTL_HANDLER_ARGS
439 {
440 	int *name = (int *) arg1, error;
441 	u_int namelen = arg2;
442 	int indx, j;
443 	struct sysctl_oid **oidpp;
444 	struct linker_set *lsp = &sysctl_;
445 
446 	j = lsp->ls_length;
447 	oidpp = (struct sysctl_oid **) lsp->ls_items;
448 
449 	indx = 0;
450 	while (j-- && indx < CTL_MAXNAME) {
451 		if (*oidpp && ((*oidpp)->oid_number == name[indx])) {
452 			indx++;
453 			if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
454 				if ((*oidpp)->oid_handler)
455 					goto found;
456 				if (indx == namelen)
457 					goto found;
458 				lsp = (struct linker_set*)(*oidpp)->oid_arg1;
459 				j = lsp->ls_length;
460 				oidpp = (struct sysctl_oid **)lsp->ls_items;
461 			} else {
462 				if (indx != namelen)
463 					return EISDIR;
464 				goto found;
465 			}
466 		} else {
467 			oidpp++;
468 		}
469 	}
470 	return ENOENT;
471 found:
472 	if (!(*oidpp)->oid_fmt)
473 		return ENOENT;
474 	error = SYSCTL_OUT(req,
475 		&(*oidpp)->oid_kind, sizeof((*oidpp)->oid_kind));
476 	if (!error)
477 		error = SYSCTL_OUT(req, (*oidpp)->oid_fmt,
478 			strlen((*oidpp)->oid_fmt)+1);
479 	return (error);
480 }
481 
482 
483 SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
484 
485 /*
486  * Default "handler" functions.
487  */
488 
489 /*
490  * Handle an integer, signed or unsigned.
491  * Two cases:
492  *     a variable:  point arg1 at it.
493  *     a constant:  pass it in arg2.
494  */
495 
496 int
497 sysctl_handle_int SYSCTL_HANDLER_ARGS
498 {
499 	int error = 0;
500 
501 	if (arg1)
502 		error = SYSCTL_OUT(req, arg1, sizeof(int));
503 	else
504 		error = SYSCTL_OUT(req, &arg2, sizeof(int));
505 
506 	if (error || !req->newptr)
507 		return (error);
508 
509 	if (!arg1)
510 		error = EPERM;
511 	else
512 		error = SYSCTL_IN(req, arg1, sizeof(int));
513 	return (error);
514 }
515 
516 /*
517  * Handle our generic '\0' terminated 'C' string.
518  * Two cases:
519  * 	a variable string:  point arg1 at it, arg2 is max length.
520  * 	a constant string:  point arg1 at it, arg2 is zero.
521  */
522 
523 int
524 sysctl_handle_string SYSCTL_HANDLER_ARGS
525 {
526 	int error=0;
527 
528 	error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
529 
530 	if (error || !req->newptr || !arg2)
531 		return (error);
532 
533 	if ((req->newlen - req->newidx) > arg2) {
534 		error = E2BIG;
535 	} else {
536 		arg2 = (req->newlen - req->newidx);
537 		error = SYSCTL_IN(req, arg1, arg2);
538 		((char *)arg1)[arg2] = '\0';
539 	}
540 
541 	return (error);
542 }
543 
544 /*
545  * Handle any kind of opaque data.
546  * arg1 points to it, arg2 is the size.
547  */
548 
549 int
550 sysctl_handle_opaque SYSCTL_HANDLER_ARGS
551 {
552 	int error;
553 
554 	error = SYSCTL_OUT(req, arg1, arg2);
555 
556 	if (error || !req->newptr)
557 		return (error);
558 
559 	error = SYSCTL_IN(req, arg1, arg2);
560 
561 	return (error);
562 }
563 
564 /*
565  * Transfer functions to/from kernel space.
566  * XXX: rather untested at this point
567  */
568 static int
569 sysctl_old_kernel(struct sysctl_req *req, const void *p, int l)
570 {
571 	int i = 0;
572 
573 	if (req->oldptr) {
574 		i = min(req->oldlen - req->oldidx, l);
575 		if (i > 0)
576 			bcopy(p, (char *)req->oldptr + req->oldidx, i);
577 	}
578 	req->oldidx += l;
579 	if (req->oldptr && i != l)
580 		return (ENOMEM);
581 	return (0);
582 }
583 
584 static int
585 sysctl_new_kernel(struct sysctl_req *req, void *p, int l)
586 {
587 	if (!req->newptr)
588 		return 0;
589 	if (req->newlen - req->newidx < l)
590 		return (EINVAL);
591 	bcopy((char *)req->newptr + req->newidx, p, l);
592 	req->newidx += l;
593 	return (0);
594 }
595 
596 int
597 kernel_sysctl(struct proc *p, int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, int *retval)
598 {
599 	int error = 0;
600 	struct sysctl_req req;
601 
602 	bzero(&req, sizeof req);
603 
604 	req.p = p;
605 
606 	if (oldlenp) {
607 		req.oldlen = *oldlenp;
608 	}
609 
610 	if (old) {
611 		req.oldptr= old;
612 	}
613 
614 	if (newlen) {
615 		req.newlen = newlen;
616 		req.newptr = new;
617 	}
618 
619 	req.oldfunc = sysctl_old_kernel;
620 	req.newfunc = sysctl_new_kernel;
621 	req.lock = 1;
622 
623 	/* XXX this should probably be done in a general way */
624 	while (memlock.sl_lock) {
625 		memlock.sl_want = 1;
626 		(void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0);
627 		memlock.sl_locked++;
628 	}
629 	memlock.sl_lock = 1;
630 
631 	error = sysctl_root(0, name, namelen, &req);
632 
633 	if (req.lock == 2)
634 		vsunlock(req.oldptr, req.oldlen, B_WRITE);
635 
636 	memlock.sl_lock = 0;
637 
638 	if (memlock.sl_want) {
639 		memlock.sl_want = 0;
640 		wakeup((caddr_t)&memlock);
641 	}
642 
643 	if (error && error != ENOMEM)
644 		return (error);
645 
646 	if (retval) {
647 		if (req.oldptr && req.oldidx > req.oldlen)
648 			*retval = req.oldlen;
649 		else
650 			*retval = req.oldidx;
651 	}
652 	return (error);
653 }
654 
655 /*
656  * Transfer function to/from user space.
657  */
658 static int
659 sysctl_old_user(struct sysctl_req *req, const void *p, int l)
660 {
661 	int error = 0, i = 0;
662 
663 	if (req->lock == 1 && req->oldptr) {
664 		vslock(req->oldptr, req->oldlen);
665 		req->lock = 2;
666 	}
667 	if (req->oldptr) {
668 		i = min(req->oldlen - req->oldidx, l);
669 		if (i > 0)
670 			error = copyout(p, (char *)req->oldptr + req->oldidx,
671 					i);
672 	}
673 	req->oldidx += l;
674 	if (error)
675 		return (error);
676 	if (req->oldptr && i < l)
677 		return (ENOMEM);
678 	return (0);
679 }
680 
681 static int
682 sysctl_new_user(struct sysctl_req *req, void *p, int l)
683 {
684 	int error;
685 
686 	if (!req->newptr)
687 		return 0;
688 	if (req->newlen - req->newidx < l)
689 		return (EINVAL);
690 	error = copyin((char *)req->newptr + req->newidx, p, l);
691 	req->newidx += l;
692 	return (error);
693 }
694 
695 /*
696  * Traverse our tree, and find the right node, execute whatever it points
697  * at, and return the resulting error code.
698  */
699 
700 int
701 sysctl_root SYSCTL_HANDLER_ARGS
702 {
703 	int *name = (int *) arg1;
704 	u_int namelen = arg2;
705 	int indx, i, j;
706 	struct sysctl_oid **oidpp;
707 	struct linker_set *lsp = &sysctl_;
708 
709 	j = lsp->ls_length;
710 	oidpp = (struct sysctl_oid **) lsp->ls_items;
711 
712 	indx = 0;
713 	while (j-- && indx < CTL_MAXNAME) {
714 		if (*oidpp && ((*oidpp)->oid_number == name[indx])) {
715 			indx++;
716 			if ((*oidpp)->oid_kind & CTLFLAG_NOLOCK)
717 				req->lock = 0;
718 			if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
719 				if ((*oidpp)->oid_handler)
720 					goto found;
721 				if (indx == namelen)
722 					return ENOENT;
723 				lsp = (struct linker_set*)(*oidpp)->oid_arg1;
724 				j = lsp->ls_length;
725 				oidpp = (struct sysctl_oid **)lsp->ls_items;
726 			} else {
727 				if (indx != namelen)
728 					return EISDIR;
729 				goto found;
730 			}
731 		} else {
732 			oidpp++;
733 		}
734 	}
735 	return ENOENT;
736 found:
737 	/* If writing isn't allowed */
738 	if (req->newptr && !((*oidpp)->oid_kind & CTLFLAG_WR))
739 		return (EPERM);
740 
741 	/* Most likely only root can write */
742 	if (!((*oidpp)->oid_kind & CTLFLAG_ANYBODY) &&
743 	    req->newptr && req->p &&
744 	    (i = suser(req->p->p_ucred, &req->p->p_acflag)))
745 		return (i);
746 
747 	if (!(*oidpp)->oid_handler)
748 		return EINVAL;
749 
750 	if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
751 		i = ((*oidpp)->oid_handler) (*oidpp,
752 					name + indx, namelen - indx,
753 					req);
754 	} else {
755 		i = ((*oidpp)->oid_handler) (*oidpp,
756 					(*oidpp)->oid_arg1, (*oidpp)->oid_arg2,
757 					req);
758 	}
759 	return (i);
760 }
761 
762 #ifndef _SYS_SYSPROTO_H_
763 struct sysctl_args {
764 	int	*name;
765 	u_int	namelen;
766 	void	*old;
767 	size_t	*oldlenp;
768 	void	*new;
769 	size_t	newlen;
770 };
771 #endif
772 
773 int
774 __sysctl(struct proc *p, struct sysctl_args *uap)
775 {
776 	int error, i, j, name[CTL_MAXNAME];
777 
778 	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
779 		return (EINVAL);
780 
781  	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
782  	if (error)
783 		return (error);
784 
785 	error = userland_sysctl(p, name, uap->namelen,
786 		uap->old, uap->oldlenp, 0,
787 		uap->new, uap->newlen, &j);
788 	if (error && error != ENOMEM)
789 		return (error);
790 	if (uap->oldlenp) {
791 		i = copyout(&j, uap->oldlenp, sizeof(j));
792 		if (i)
793 			return (i);
794 	}
795 	return (error);
796 }
797 
798 /*
799  * This is used from various compatibility syscalls too.  That's why name
800  * must be in kernel space.
801  */
802 int
803 userland_sysctl(struct proc *p, int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, int *retval)
804 {
805 	int error = 0;
806 	struct sysctl_req req, req2;
807 
808 	bzero(&req, sizeof req);
809 
810 	req.p = p;
811 
812 	if (oldlenp) {
813 		if (inkernel) {
814 			req.oldlen = *oldlenp;
815 		} else {
816 			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
817 			if (error)
818 				return (error);
819 		}
820 	}
821 
822 	if (old) {
823 		if (!useracc(old, req.oldlen, B_WRITE))
824 			return (EFAULT);
825 		req.oldptr= old;
826 	}
827 
828 	if (newlen) {
829 		if (!useracc(new, req.newlen, B_READ))
830 			return (EFAULT);
831 		req.newlen = newlen;
832 		req.newptr = new;
833 	}
834 
835 	req.oldfunc = sysctl_old_user;
836 	req.newfunc = sysctl_new_user;
837 	req.lock = 1;
838 
839 	/* XXX this should probably be done in a general way */
840 	while (memlock.sl_lock) {
841 		memlock.sl_want = 1;
842 		(void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0);
843 		memlock.sl_locked++;
844 	}
845 	memlock.sl_lock = 1;
846 
847 	do {
848 	    req2 = req;
849 	    error = sysctl_root(0, name, namelen, &req2);
850 	} while (error == EAGAIN);
851 
852 	req = req2;
853 	if (req.lock == 2)
854 		vsunlock(req.oldptr, req.oldlen, B_WRITE);
855 
856 	memlock.sl_lock = 0;
857 
858 	if (memlock.sl_want) {
859 		memlock.sl_want = 0;
860 		wakeup((caddr_t)&memlock);
861 	}
862 
863 	if (error && error != ENOMEM)
864 		return (error);
865 
866 	if (retval) {
867 		if (req.oldptr && req.oldidx > req.oldlen)
868 			*retval = req.oldlen;
869 		else
870 			*retval = req.oldidx;
871 	}
872 	return (error);
873 }
874 
875 #ifdef COMPAT_43
876 #include <sys/socket.h>
877 #include <vm/vm_param.h>
878 
879 #define	KINFO_PROC		(0<<8)
880 #define	KINFO_RT		(1<<8)
881 #define	KINFO_VNODE		(2<<8)
882 #define	KINFO_FILE		(3<<8)
883 #define	KINFO_METER		(4<<8)
884 #define	KINFO_LOADAVG		(5<<8)
885 #define	KINFO_CLOCKRATE		(6<<8)
886 
887 /* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
888 #define	KINFO_BSDI_SYSINFO	(101<<8)
889 
890 /*
891  * XXX this is bloat, but I hope it's better here than on the potentially
892  * limited kernel stack...  -Peter
893  */
894 
895 static struct {
896 	int	bsdi_machine;		/* "i386" on BSD/386 */
897 /*      ^^^ this is an offset to the string, relative to the struct start */
898 	char	*pad0;
899 	long	pad1;
900 	long	pad2;
901 	long	pad3;
902 	u_long	pad4;
903 	u_long	pad5;
904 	u_long	pad6;
905 
906 	int	bsdi_ostype;		/* "BSD/386" on BSD/386 */
907 	int	bsdi_osrelease;		/* "1.1" on BSD/386 */
908 	long	pad7;
909 	long	pad8;
910 	char	*pad9;
911 
912 	long	pad10;
913 	long	pad11;
914 	int	pad12;
915 	long	pad13;
916 	quad_t	pad14;
917 	long	pad15;
918 
919 	struct	timeval pad16;
920 	/* we dont set this, because BSDI's uname used gethostname() instead */
921 	int	bsdi_hostname;		/* hostname on BSD/386 */
922 
923 	/* the actual string data is appended here */
924 
925 } bsdi_si;
926 /*
927  * this data is appended to the end of the bsdi_si structure during copyout.
928  * The "char *" offsets are relative to the base of the bsdi_si struct.
929  * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
930  * should not exceed the length of the buffer here... (or else!! :-)
931  */
932 static char bsdi_strings[80];	/* It had better be less than this! */
933 
934 #ifndef _SYS_SYSPROTO_H_
935 struct getkerninfo_args {
936 	int	op;
937 	char	*where;
938 	int	*size;
939 	int	arg;
940 };
941 #endif
942 
943 int
944 ogetkerninfo(struct proc *p, struct getkerninfo_args *uap)
945 {
946 	int error, name[6];
947 	u_int size;
948 
949 	switch (uap->op & 0xff00) {
950 
951 	case KINFO_RT:
952 		name[0] = CTL_NET;
953 		name[1] = PF_ROUTE;
954 		name[2] = 0;
955 		name[3] = (uap->op & 0xff0000) >> 16;
956 		name[4] = uap->op & 0xff;
957 		name[5] = uap->arg;
958 		error = userland_sysctl(p, name, 6, uap->where, uap->size,
959 			0, 0, 0, &size);
960 		break;
961 
962 	case KINFO_VNODE:
963 		name[0] = CTL_KERN;
964 		name[1] = KERN_VNODE;
965 		error = userland_sysctl(p, name, 2, uap->where, uap->size,
966 			0, 0, 0, &size);
967 		break;
968 
969 	case KINFO_PROC:
970 		name[0] = CTL_KERN;
971 		name[1] = KERN_PROC;
972 		name[2] = uap->op & 0xff;
973 		name[3] = uap->arg;
974 		error = userland_sysctl(p, name, 4, uap->where, uap->size,
975 			0, 0, 0, &size);
976 		break;
977 
978 	case KINFO_FILE:
979 		name[0] = CTL_KERN;
980 		name[1] = KERN_FILE;
981 		error = userland_sysctl(p, name, 2, uap->where, uap->size,
982 			0, 0, 0, &size);
983 		break;
984 
985 	case KINFO_METER:
986 		name[0] = CTL_VM;
987 		name[1] = VM_METER;
988 		error = userland_sysctl(p, name, 2, uap->where, uap->size,
989 			0, 0, 0, &size);
990 		break;
991 
992 	case KINFO_LOADAVG:
993 		name[0] = CTL_VM;
994 		name[1] = VM_LOADAVG;
995 		error = userland_sysctl(p, name, 2, uap->where, uap->size,
996 			0, 0, 0, &size);
997 		break;
998 
999 	case KINFO_CLOCKRATE:
1000 		name[0] = CTL_KERN;
1001 		name[1] = KERN_CLOCKRATE;
1002 		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1003 			0, 0, 0, &size);
1004 		break;
1005 
1006 	case KINFO_BSDI_SYSINFO: {
1007 		/*
1008 		 * this is pretty crude, but it's just enough for uname()
1009 		 * from BSDI's 1.x libc to work.
1010 		 *
1011 		 * In particular, it doesn't return the same results when
1012 		 * the supplied buffer is too small.  BSDI's version apparently
1013 		 * will return the amount copied, and set the *size to how
1014 		 * much was needed.  The emulation framework here isn't capable
1015 		 * of that, so we just set both to the amount copied.
1016 		 * BSDI's 2.x product apparently fails with ENOMEM in this
1017 		 * scenario.
1018 		 */
1019 
1020 		u_int needed;
1021 		u_int left;
1022 		char *s;
1023 
1024 		bzero((char *)&bsdi_si, sizeof(bsdi_si));
1025 		bzero(bsdi_strings, sizeof(bsdi_strings));
1026 
1027 		s = bsdi_strings;
1028 
1029 		bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
1030 		strcpy(s, ostype);
1031 		s += strlen(s) + 1;
1032 
1033 		bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
1034 		strcpy(s, osrelease);
1035 		s += strlen(s) + 1;
1036 
1037 		bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
1038 		strcpy(s, machine);
1039 		s += strlen(s) + 1;
1040 
1041 		needed = sizeof(bsdi_si) + (s - bsdi_strings);
1042 
1043 		if (uap->where == NULL) {
1044 			/* process is asking how much buffer to supply.. */
1045 			size = needed;
1046 			error = 0;
1047 			break;
1048 		}
1049 
1050 
1051 		/* if too much buffer supplied, trim it down */
1052 		if (size > needed)
1053 			size = needed;
1054 
1055 		/* how much of the buffer is remaining */
1056 		left = size;
1057 
1058 		if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
1059 			break;
1060 
1061 		/* is there any point in continuing? */
1062 		if (left > sizeof(bsdi_si)) {
1063 			left -= sizeof(bsdi_si);
1064 			error = copyout(&bsdi_strings,
1065 					uap->where + sizeof(bsdi_si), left);
1066 		}
1067 		break;
1068 	}
1069 
1070 	default:
1071 		return (EOPNOTSUPP);
1072 	}
1073 	if (error)
1074 		return (error);
1075 	p->p_retval[0] = size;
1076 	if (uap->size)
1077 		error = copyout((caddr_t)&size, (caddr_t)uap->size,
1078 		    sizeof(size));
1079 	return (error);
1080 }
1081 #endif /* COMPAT_43 */
1082