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