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