kern_sysctl.c revision 44972
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.84 1999/02/16 10:49:48 dfr 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
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
412	p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
413
414	error = SYSCTL_IN(req, p, req->newlen);
415	if (error) {
416		free(p, M_SYSCTL);
417		return (error);
418	}
419
420	p [req->newlen] = '\0';
421
422	error = name2oid(p, oid, &len, &op);
423
424	free(p, M_SYSCTL);
425
426	if (error)
427		return (error);
428
429	error = SYSCTL_OUT(req, oid, len * sizeof *oid);
430	return (error);
431}
432
433SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
434	sysctl_sysctl_name2oid, "I", "");
435
436static int
437sysctl_sysctl_oidfmt SYSCTL_HANDLER_ARGS
438{
439	int *name = (int *) arg1, error;
440	u_int namelen = arg2;
441	int indx;
442	struct sysctl_oid *oid;
443	struct sysctl_oid_list *lsp = &sysctl__children;
444
445	oid = SLIST_FIRST(lsp);
446
447	indx = 0;
448	while (oid && indx < CTL_MAXNAME) {
449		if (oid->oid_number == name[indx]) {
450			indx++;
451			if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
452				if (oid->oid_handler)
453					goto found;
454				if (indx == namelen)
455					goto found;
456				lsp = (struct sysctl_oid_list *)oid->oid_arg1;
457				oid = SLIST_FIRST(lsp);
458			} else {
459				if (indx != namelen)
460					return EISDIR;
461				goto found;
462			}
463		} else {
464			oid = SLIST_NEXT(oid, oid_link);
465		}
466	}
467	return ENOENT;
468found:
469	if (!oid->oid_fmt)
470		return ENOENT;
471	error = SYSCTL_OUT(req,
472		&oid->oid_kind, sizeof(oid->oid_kind));
473	if (!error)
474		error = SYSCTL_OUT(req, oid->oid_fmt,
475			strlen(oid->oid_fmt)+1);
476	return (error);
477}
478
479
480SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
481
482/*
483 * Default "handler" functions.
484 */
485
486/*
487 * Handle an int, signed or unsigned.
488 * Two cases:
489 *     a variable:  point arg1 at it.
490 *     a constant:  pass it in arg2.
491 */
492
493int
494sysctl_handle_int SYSCTL_HANDLER_ARGS
495{
496	int error = 0;
497
498	if (arg1)
499		error = SYSCTL_OUT(req, arg1, sizeof(int));
500	else
501		error = SYSCTL_OUT(req, &arg2, sizeof(int));
502
503	if (error || !req->newptr)
504		return (error);
505
506	if (!arg1)
507		error = EPERM;
508	else
509		error = SYSCTL_IN(req, arg1, sizeof(int));
510	return (error);
511}
512
513/*
514 * Handle a long, signed or unsigned.
515 * Two cases:
516 *     a variable:  point arg1 at it.
517 *     a constant:  pass it in arg2.
518 */
519
520int
521sysctl_handle_long SYSCTL_HANDLER_ARGS
522{
523	int error = 0;
524
525	error = SYSCTL_OUT(req, arg1, sizeof(long));
526
527	if (error || !req->newptr)
528		return (error);
529
530	if (!arg1)
531		error = EPERM;
532	else
533		error = SYSCTL_IN(req, arg1, sizeof(long));
534	return (error);
535}
536
537/*
538 * Handle our generic '\0' terminated 'C' string.
539 * Two cases:
540 * 	a variable string:  point arg1 at it, arg2 is max length.
541 * 	a constant string:  point arg1 at it, arg2 is zero.
542 */
543
544int
545sysctl_handle_string SYSCTL_HANDLER_ARGS
546{
547	int error=0;
548
549	error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
550
551	if (error || !req->newptr || !arg2)
552		return (error);
553
554	if ((req->newlen - req->newidx) > arg2) {
555		error = E2BIG;
556	} else {
557		arg2 = (req->newlen - req->newidx);
558		error = SYSCTL_IN(req, arg1, arg2);
559		((char *)arg1)[arg2] = '\0';
560	}
561
562	return (error);
563}
564
565/*
566 * Handle any kind of opaque data.
567 * arg1 points to it, arg2 is the size.
568 */
569
570int
571sysctl_handle_opaque SYSCTL_HANDLER_ARGS
572{
573	int error;
574
575	error = SYSCTL_OUT(req, arg1, arg2);
576
577	if (error || !req->newptr)
578		return (error);
579
580	error = SYSCTL_IN(req, arg1, arg2);
581
582	return (error);
583}
584
585/*
586 * Transfer functions to/from kernel space.
587 * XXX: rather untested at this point
588 */
589static int
590sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
591{
592	size_t i = 0;
593
594	if (req->oldptr) {
595		i = l;
596		if (i > req->oldlen - req->oldidx)
597			i = req->oldlen - req->oldidx;
598		if (i > 0)
599			bcopy(p, (char *)req->oldptr + req->oldidx, i);
600	}
601	req->oldidx += l;
602	if (req->oldptr && i != l)
603		return (ENOMEM);
604	return (0);
605}
606
607static int
608sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
609{
610	if (!req->newptr)
611		return 0;
612	if (req->newlen - req->newidx < l)
613		return (EINVAL);
614	bcopy((char *)req->newptr + req->newidx, p, l);
615	req->newidx += l;
616	return (0);
617}
618
619int
620kernel_sysctl(struct proc *p, int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval)
621{
622	int error = 0;
623	struct sysctl_req req;
624
625	bzero(&req, sizeof req);
626
627	req.p = p;
628
629	if (oldlenp) {
630		req.oldlen = *oldlenp;
631	}
632
633	if (old) {
634		req.oldptr= old;
635	}
636
637	if (newlen) {
638		req.newlen = newlen;
639		req.newptr = new;
640	}
641
642	req.oldfunc = sysctl_old_kernel;
643	req.newfunc = sysctl_new_kernel;
644	req.lock = 1;
645
646	/* XXX this should probably be done in a general way */
647	while (memlock.sl_lock) {
648		memlock.sl_want = 1;
649		(void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0);
650		memlock.sl_locked++;
651	}
652	memlock.sl_lock = 1;
653
654	error = sysctl_root(0, name, namelen, &req);
655
656	if (req.lock == 2)
657		vsunlock(req.oldptr, req.oldlen, B_WRITE);
658
659	memlock.sl_lock = 0;
660
661	if (memlock.sl_want) {
662		memlock.sl_want = 0;
663		wakeup((caddr_t)&memlock);
664	}
665
666	if (error && error != ENOMEM)
667		return (error);
668
669	if (retval) {
670		if (req.oldptr && req.oldidx > req.oldlen)
671			*retval = req.oldlen;
672		else
673			*retval = req.oldidx;
674	}
675	return (error);
676}
677
678/*
679 * Transfer function to/from user space.
680 */
681static int
682sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
683{
684	int error = 0;
685	size_t i = 0;
686
687	if (req->lock == 1 && req->oldptr) {
688		vslock(req->oldptr, req->oldlen);
689		req->lock = 2;
690	}
691	if (req->oldptr) {
692		i = l;
693		if (i > req->oldlen - req->oldidx)
694			i = req->oldlen - req->oldidx;
695		if (i > 0)
696			error = copyout(p, (char *)req->oldptr + req->oldidx,
697					i);
698	}
699	req->oldidx += l;
700	if (error)
701		return (error);
702	if (req->oldptr && i < l)
703		return (ENOMEM);
704	return (0);
705}
706
707static int
708sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
709{
710	int error;
711
712	if (!req->newptr)
713		return 0;
714	if (req->newlen - req->newidx < l)
715		return (EINVAL);
716	error = copyin((char *)req->newptr + req->newidx, p, l);
717	req->newidx += l;
718	return (error);
719}
720
721/*
722 * Traverse our tree, and find the right node, execute whatever it points
723 * at, and return the resulting error code.
724 */
725
726int
727sysctl_root SYSCTL_HANDLER_ARGS
728{
729	int *name = (int *) arg1;
730	u_int namelen = arg2;
731	int indx, i;
732	struct sysctl_oid *oid;
733	struct sysctl_oid_list *lsp = &sysctl__children;
734
735	oid = SLIST_FIRST(lsp);
736
737	indx = 0;
738	while (oid && indx < CTL_MAXNAME) {
739		if (oid->oid_number == name[indx]) {
740			indx++;
741			if (oid->oid_kind & CTLFLAG_NOLOCK)
742				req->lock = 0;
743			if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
744				if (oid->oid_handler)
745					goto found;
746				if (indx == namelen)
747					return ENOENT;
748				lsp = (struct sysctl_oid_list *)oid->oid_arg1;
749				oid = SLIST_FIRST(lsp);
750			} else {
751				if (indx != namelen)
752					return EISDIR;
753				goto found;
754			}
755		} else {
756			oid = SLIST_NEXT(oid, oid_link);
757		}
758	}
759	return ENOENT;
760found:
761	/* If writing isn't allowed */
762	if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
763	    ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0)))
764		return (EPERM);
765
766	/* Most likely only root can write */
767	if (!(oid->oid_kind & CTLFLAG_ANYBODY) &&
768	    req->newptr && req->p &&
769	    (i = suser(req->p->p_ucred, &req->p->p_acflag)))
770		return (i);
771
772	if (!oid->oid_handler)
773		return EINVAL;
774
775	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
776		i = (oid->oid_handler) (oid,
777					name + indx, namelen - indx,
778					req);
779	} else {
780		i = (oid->oid_handler) (oid,
781					oid->oid_arg1, oid->oid_arg2,
782					req);
783	}
784	return (i);
785}
786
787#ifndef _SYS_SYSPROTO_H_
788struct sysctl_args {
789	int	*name;
790	u_int	namelen;
791	void	*old;
792	size_t	*oldlenp;
793	void	*new;
794	size_t	newlen;
795};
796#endif
797
798int
799__sysctl(struct proc *p, struct sysctl_args *uap)
800{
801	int error, i, name[CTL_MAXNAME];
802	size_t j;
803
804	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
805		return (EINVAL);
806
807 	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
808 	if (error)
809		return (error);
810
811	error = userland_sysctl(p, name, uap->namelen,
812		uap->old, uap->oldlenp, 0,
813		uap->new, uap->newlen, &j);
814	if (error && error != ENOMEM)
815		return (error);
816	if (uap->oldlenp) {
817		i = copyout(&j, uap->oldlenp, sizeof(j));
818		if (i)
819			return (i);
820	}
821	return (error);
822}
823
824/*
825 * This is used from various compatibility syscalls too.  That's why name
826 * must be in kernel space.
827 */
828int
829userland_sysctl(struct proc *p, int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
830{
831	int error = 0;
832	struct sysctl_req req, req2;
833
834	bzero(&req, sizeof req);
835
836	req.p = p;
837
838	if (oldlenp) {
839		if (inkernel) {
840			req.oldlen = *oldlenp;
841		} else {
842			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
843			if (error)
844				return (error);
845		}
846	}
847
848	if (old) {
849		if (!useracc(old, req.oldlen, B_WRITE))
850			return (EFAULT);
851		req.oldptr= old;
852	}
853
854	if (newlen) {
855		if (!useracc(new, req.newlen, B_READ))
856			return (EFAULT);
857		req.newlen = newlen;
858		req.newptr = new;
859	}
860
861	req.oldfunc = sysctl_old_user;
862	req.newfunc = sysctl_new_user;
863	req.lock = 1;
864
865	/* XXX this should probably be done in a general way */
866	while (memlock.sl_lock) {
867		memlock.sl_want = 1;
868		(void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0);
869		memlock.sl_locked++;
870	}
871	memlock.sl_lock = 1;
872
873	do {
874	    req2 = req;
875	    error = sysctl_root(0, name, namelen, &req2);
876	} while (error == EAGAIN);
877
878	req = req2;
879	if (req.lock == 2)
880		vsunlock(req.oldptr, req.oldlen, B_WRITE);
881
882	memlock.sl_lock = 0;
883
884	if (memlock.sl_want) {
885		memlock.sl_want = 0;
886		wakeup((caddr_t)&memlock);
887	}
888
889	if (error && error != ENOMEM)
890		return (error);
891
892	if (retval) {
893		if (req.oldptr && req.oldidx > req.oldlen)
894			*retval = req.oldlen;
895		else
896			*retval = req.oldidx;
897	}
898	return (error);
899}
900
901#ifdef COMPAT_43
902#include <sys/socket.h>
903#include <vm/vm_param.h>
904
905#define	KINFO_PROC		(0<<8)
906#define	KINFO_RT		(1<<8)
907#define	KINFO_VNODE		(2<<8)
908#define	KINFO_FILE		(3<<8)
909#define	KINFO_METER		(4<<8)
910#define	KINFO_LOADAVG		(5<<8)
911#define	KINFO_CLOCKRATE		(6<<8)
912
913/* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
914#define	KINFO_BSDI_SYSINFO	(101<<8)
915
916/*
917 * XXX this is bloat, but I hope it's better here than on the potentially
918 * limited kernel stack...  -Peter
919 */
920
921static struct {
922	int	bsdi_machine;		/* "i386" on BSD/386 */
923/*      ^^^ this is an offset to the string, relative to the struct start */
924	char	*pad0;
925	long	pad1;
926	long	pad2;
927	long	pad3;
928	u_long	pad4;
929	u_long	pad5;
930	u_long	pad6;
931
932	int	bsdi_ostype;		/* "BSD/386" on BSD/386 */
933	int	bsdi_osrelease;		/* "1.1" on BSD/386 */
934	long	pad7;
935	long	pad8;
936	char	*pad9;
937
938	long	pad10;
939	long	pad11;
940	int	pad12;
941	long	pad13;
942	quad_t	pad14;
943	long	pad15;
944
945	struct	timeval pad16;
946	/* we dont set this, because BSDI's uname used gethostname() instead */
947	int	bsdi_hostname;		/* hostname on BSD/386 */
948
949	/* the actual string data is appended here */
950
951} bsdi_si;
952/*
953 * this data is appended to the end of the bsdi_si structure during copyout.
954 * The "char *" offsets are relative to the base of the bsdi_si struct.
955 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
956 * should not exceed the length of the buffer here... (or else!! :-)
957 */
958static char bsdi_strings[80];	/* It had better be less than this! */
959
960#ifndef _SYS_SYSPROTO_H_
961struct getkerninfo_args {
962	int	op;
963	char	*where;
964	size_t	*size;
965	int	arg;
966};
967#endif
968
969int
970ogetkerninfo(struct proc *p, struct getkerninfo_args *uap)
971{
972	int error, name[6];
973	size_t size;
974
975	switch (uap->op & 0xff00) {
976
977	case KINFO_RT:
978		name[0] = CTL_NET;
979		name[1] = PF_ROUTE;
980		name[2] = 0;
981		name[3] = (uap->op & 0xff0000) >> 16;
982		name[4] = uap->op & 0xff;
983		name[5] = uap->arg;
984		error = userland_sysctl(p, name, 6, uap->where, uap->size,
985			0, 0, 0, &size);
986		break;
987
988	case KINFO_VNODE:
989		name[0] = CTL_KERN;
990		name[1] = KERN_VNODE;
991		error = userland_sysctl(p, name, 2, uap->where, uap->size,
992			0, 0, 0, &size);
993		break;
994
995	case KINFO_PROC:
996		name[0] = CTL_KERN;
997		name[1] = KERN_PROC;
998		name[2] = uap->op & 0xff;
999		name[3] = uap->arg;
1000		error = userland_sysctl(p, name, 4, uap->where, uap->size,
1001			0, 0, 0, &size);
1002		break;
1003
1004	case KINFO_FILE:
1005		name[0] = CTL_KERN;
1006		name[1] = KERN_FILE;
1007		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1008			0, 0, 0, &size);
1009		break;
1010
1011	case KINFO_METER:
1012		name[0] = CTL_VM;
1013		name[1] = VM_METER;
1014		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1015			0, 0, 0, &size);
1016		break;
1017
1018	case KINFO_LOADAVG:
1019		name[0] = CTL_VM;
1020		name[1] = VM_LOADAVG;
1021		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1022			0, 0, 0, &size);
1023		break;
1024
1025	case KINFO_CLOCKRATE:
1026		name[0] = CTL_KERN;
1027		name[1] = KERN_CLOCKRATE;
1028		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1029			0, 0, 0, &size);
1030		break;
1031
1032	case KINFO_BSDI_SYSINFO: {
1033		/*
1034		 * this is pretty crude, but it's just enough for uname()
1035		 * from BSDI's 1.x libc to work.
1036		 *
1037		 * In particular, it doesn't return the same results when
1038		 * the supplied buffer is too small.  BSDI's version apparently
1039		 * will return the amount copied, and set the *size to how
1040		 * much was needed.  The emulation framework here isn't capable
1041		 * of that, so we just set both to the amount copied.
1042		 * BSDI's 2.x product apparently fails with ENOMEM in this
1043		 * scenario.
1044		 */
1045
1046		u_int needed;
1047		u_int left;
1048		char *s;
1049
1050		bzero((char *)&bsdi_si, sizeof(bsdi_si));
1051		bzero(bsdi_strings, sizeof(bsdi_strings));
1052
1053		s = bsdi_strings;
1054
1055		bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
1056		strcpy(s, ostype);
1057		s += strlen(s) + 1;
1058
1059		bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
1060		strcpy(s, osrelease);
1061		s += strlen(s) + 1;
1062
1063		bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
1064		strcpy(s, machine);
1065		s += strlen(s) + 1;
1066
1067		needed = sizeof(bsdi_si) + (s - bsdi_strings);
1068
1069		if (uap->where == NULL) {
1070			/* process is asking how much buffer to supply.. */
1071			size = needed;
1072			error = 0;
1073			break;
1074		}
1075
1076
1077		/* if too much buffer supplied, trim it down */
1078		if (size > needed)
1079			size = needed;
1080
1081		/* how much of the buffer is remaining */
1082		left = size;
1083
1084		if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
1085			break;
1086
1087		/* is there any point in continuing? */
1088		if (left > sizeof(bsdi_si)) {
1089			left -= sizeof(bsdi_si);
1090			error = copyout(&bsdi_strings,
1091					uap->where + sizeof(bsdi_si), left);
1092		}
1093		break;
1094	}
1095
1096	default:
1097		return (EOPNOTSUPP);
1098	}
1099	if (error)
1100		return (error);
1101	p->p_retval[0] = size;
1102	if (uap->size)
1103		error = copyout((caddr_t)&size, (caddr_t)uap->size,
1104		    sizeof(size));
1105	return (error);
1106}
1107#endif /* COMPAT_43 */
1108