kern_sysctl.c revision 73971
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 73971 2001-03-08 01:20:43Z tmm $
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 * Bulk-register all the oids in a linker_set.
371 */
372void sysctl_register_set(struct linker_set *lsp)
373{
374	int count = lsp->ls_length;
375	int i;
376	for (i = 0; i < count; i++)
377		sysctl_register_oid((struct sysctl_oid *) lsp->ls_items[i]);
378}
379
380void sysctl_unregister_set(struct linker_set *lsp)
381{
382	int count = lsp->ls_length;
383	int i;
384	for (i = 0; i < count; i++)
385		sysctl_unregister_oid((struct sysctl_oid *) lsp->ls_items[i]);
386}
387
388/*
389 * Register the kernel's oids on startup.
390 */
391extern struct linker_set sysctl_set;
392
393static void sysctl_register_all(void *arg)
394{
395	sysctl_register_set(&sysctl_set);
396}
397
398SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
399
400/*
401 * "Staff-functions"
402 *
403 * These functions implement a presently undocumented interface
404 * used by the sysctl program to walk the tree, and get the type
405 * so it can print the value.
406 * This interface is under work and consideration, and should probably
407 * be killed with a big axe by the first person who can find the time.
408 * (be aware though, that the proper interface isn't as obvious as it
409 * may seem, there are various conflicting requirements.
410 *
411 * {0,0}	printf the entire MIB-tree.
412 * {0,1,...}	return the name of the "..." OID.
413 * {0,2,...}	return the next OID.
414 * {0,3}	return the OID of the name in "new"
415 * {0,4,...}	return the kind & format info for the "..." OID.
416 */
417
418static void
419sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
420{
421	int k;
422	struct sysctl_oid *oidp;
423
424	SLIST_FOREACH(oidp, l, oid_link) {
425
426		for (k=0; k<i; k++)
427			printf(" ");
428
429		printf("%d %s ", oidp->oid_number, oidp->oid_name);
430
431		printf("%c%c",
432			oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
433			oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
434
435		if (oidp->oid_handler)
436			printf(" *Handler");
437
438		switch (oidp->oid_kind & CTLTYPE) {
439			case CTLTYPE_NODE:
440				printf(" Node\n");
441				if (!oidp->oid_handler) {
442					sysctl_sysctl_debug_dump_node(
443						oidp->oid_arg1, i+2);
444				}
445				break;
446			case CTLTYPE_INT:    printf(" Int\n"); break;
447			case CTLTYPE_STRING: printf(" String\n"); break;
448			case CTLTYPE_QUAD:   printf(" Quad\n"); break;
449			case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
450			default:	     printf("\n");
451		}
452
453	}
454}
455
456static int
457sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
458{
459	sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
460	return ENOENT;
461}
462
463SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
464	0, 0, sysctl_sysctl_debug, "-", "");
465
466static int
467sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
468{
469	int *name = (int *) arg1;
470	u_int namelen = arg2;
471	int error = 0;
472	struct sysctl_oid *oid;
473	struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
474	char buf[10];
475
476	while (namelen) {
477		if (!lsp) {
478			snprintf(buf,sizeof(buf),"%d",*name);
479			if (req->oldidx)
480				error = SYSCTL_OUT(req, ".", 1);
481			if (!error)
482				error = SYSCTL_OUT(req, buf, strlen(buf));
483			if (error)
484				return (error);
485			namelen--;
486			name++;
487			continue;
488		}
489		lsp2 = 0;
490		SLIST_FOREACH(oid, lsp, oid_link) {
491			if (oid->oid_number != *name)
492				continue;
493
494			if (req->oldidx)
495				error = SYSCTL_OUT(req, ".", 1);
496			if (!error)
497				error = SYSCTL_OUT(req, oid->oid_name,
498					strlen(oid->oid_name));
499			if (error)
500				return (error);
501
502			namelen--;
503			name++;
504
505			if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
506				break;
507
508			if (oid->oid_handler)
509				break;
510
511			lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
512			break;
513		}
514		lsp = lsp2;
515	}
516	return (SYSCTL_OUT(req, "", 1));
517}
518
519SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
520
521static int
522sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
523	int *next, int *len, int level, struct sysctl_oid **oidpp)
524{
525	struct sysctl_oid *oidp;
526
527	*len = level;
528	SLIST_FOREACH(oidp, lsp, oid_link) {
529		*next = oidp->oid_number;
530		*oidpp = oidp;
531
532		if (!namelen) {
533			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
534				return 0;
535			if (oidp->oid_handler)
536				/* We really should call the handler here...*/
537				return 0;
538			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
539			if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
540				len, level+1, oidpp))
541				return 0;
542			goto next;
543		}
544
545		if (oidp->oid_number < *name)
546			continue;
547
548		if (oidp->oid_number > *name) {
549			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
550				return 0;
551			if (oidp->oid_handler)
552				return 0;
553			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
554			if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
555				next+1, len, level+1, oidpp))
556				return (0);
557			goto next;
558		}
559		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
560			continue;
561
562		if (oidp->oid_handler)
563			continue;
564
565		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
566		if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
567			len, level+1, oidpp))
568			return (0);
569	next:
570		namelen = 1;
571		*len = level;
572	}
573	return 1;
574}
575
576static int
577sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
578{
579	int *name = (int *) arg1;
580	u_int namelen = arg2;
581	int i, j, error;
582	struct sysctl_oid *oid;
583	struct sysctl_oid_list *lsp = &sysctl__children;
584	int newoid[CTL_MAXNAME];
585
586	i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
587	if (i)
588		return ENOENT;
589	error = SYSCTL_OUT(req, newoid, j * sizeof (int));
590	return (error);
591}
592
593SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
594
595static int
596name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
597{
598	int i;
599	struct sysctl_oid *oidp;
600	struct sysctl_oid_list *lsp = &sysctl__children;
601	char *p;
602
603	if (!*name)
604		return ENOENT;
605
606	p = name + strlen(name) - 1 ;
607	if (*p == '.')
608		*p = '\0';
609
610	*len = 0;
611
612	for (p = name; *p && *p != '.'; p++)
613		;
614	i = *p;
615	if (i == '.')
616		*p = '\0';
617
618	oidp = SLIST_FIRST(lsp);
619
620	while (oidp && *len < CTL_MAXNAME) {
621		if (strcmp(name, oidp->oid_name)) {
622			oidp = SLIST_NEXT(oidp, oid_link);
623			continue;
624		}
625		*oid++ = oidp->oid_number;
626		(*len)++;
627
628		if (!i) {
629			if (oidpp)
630				*oidpp = oidp;
631			return (0);
632		}
633
634		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
635			break;
636
637		if (oidp->oid_handler)
638			break;
639
640		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
641		oidp = SLIST_FIRST(lsp);
642		name = p+1;
643		for (p = name; *p && *p != '.'; p++)
644				;
645		i = *p;
646		if (i == '.')
647			*p = '\0';
648	}
649	return ENOENT;
650}
651
652static int
653sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
654{
655	char *p;
656	int error, oid[CTL_MAXNAME], len;
657	struct sysctl_oid *op = 0;
658
659	if (!req->newlen)
660		return ENOENT;
661	if (req->newlen >= MAXPATHLEN)	/* XXX arbitrary, undocumented */
662		return (ENAMETOOLONG);
663
664	p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
665
666	error = SYSCTL_IN(req, p, req->newlen);
667	if (error) {
668		free(p, M_SYSCTL);
669		return (error);
670	}
671
672	p [req->newlen] = '\0';
673
674	error = name2oid(p, oid, &len, &op);
675
676	free(p, M_SYSCTL);
677
678	if (error)
679		return (error);
680
681	error = SYSCTL_OUT(req, oid, len * sizeof *oid);
682	return (error);
683}
684
685SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
686	sysctl_sysctl_name2oid, "I", "");
687
688static int
689sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
690{
691	struct sysctl_oid *oid;
692	int error;
693
694	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
695	if (error)
696		return (error);
697
698	if (!oid->oid_fmt)
699		return (ENOENT);
700	error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
701	if (error)
702		return (error);
703	error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
704	return (error);
705}
706
707
708SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
709
710/*
711 * Default "handler" functions.
712 */
713
714/*
715 * Handle an int, signed or unsigned.
716 * Two cases:
717 *     a variable:  point arg1 at it.
718 *     a constant:  pass it in arg2.
719 */
720
721int
722sysctl_handle_int(SYSCTL_HANDLER_ARGS)
723{
724	int error = 0;
725
726	if (arg1)
727		error = SYSCTL_OUT(req, arg1, sizeof(int));
728	else
729		error = SYSCTL_OUT(req, &arg2, sizeof(int));
730
731	if (error || !req->newptr)
732		return (error);
733
734	if (!arg1)
735		error = EPERM;
736	else
737		error = SYSCTL_IN(req, arg1, sizeof(int));
738	return (error);
739}
740
741/*
742 * Handle a long, signed or unsigned.  arg1 points to it.
743 */
744
745int
746sysctl_handle_long(SYSCTL_HANDLER_ARGS)
747{
748	int error = 0;
749
750	if (!arg1)
751		return (EINVAL);
752	error = SYSCTL_OUT(req, arg1, sizeof(long));
753
754	if (error || !req->newptr)
755		return (error);
756
757	error = SYSCTL_IN(req, arg1, sizeof(long));
758	return (error);
759}
760
761/*
762 * Handle our generic '\0' terminated 'C' string.
763 * Two cases:
764 * 	a variable string:  point arg1 at it, arg2 is max length.
765 * 	a constant string:  point arg1 at it, arg2 is zero.
766 */
767
768int
769sysctl_handle_string(SYSCTL_HANDLER_ARGS)
770{
771	int error=0;
772
773	error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
774
775	if (error || !req->newptr)
776		return (error);
777
778	if ((req->newlen - req->newidx) >= arg2) {
779		error = EINVAL;
780	} else {
781		arg2 = (req->newlen - req->newidx);
782		error = SYSCTL_IN(req, arg1, arg2);
783		((char *)arg1)[arg2] = '\0';
784	}
785
786	return (error);
787}
788
789/*
790 * Handle any kind of opaque data.
791 * arg1 points to it, arg2 is the size.
792 */
793
794int
795sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
796{
797	int error;
798
799	error = SYSCTL_OUT(req, arg1, arg2);
800
801	if (error || !req->newptr)
802		return (error);
803
804	error = SYSCTL_IN(req, arg1, arg2);
805
806	return (error);
807}
808
809/*
810 * Transfer functions to/from kernel space.
811 * XXX: rather untested at this point
812 */
813static int
814sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
815{
816	size_t i = 0;
817
818	if (req->oldptr) {
819		i = l;
820		if (req->oldlen <= req->oldidx)
821			i = 0;
822		else
823			if (i > req->oldlen - req->oldidx)
824				i = req->oldlen - req->oldidx;
825		if (i > 0)
826			bcopy(p, (char *)req->oldptr + req->oldidx, i);
827	}
828	req->oldidx += l;
829	if (req->oldptr && i != l)
830		return (ENOMEM);
831	return (0);
832}
833
834static int
835sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
836{
837	if (!req->newptr)
838		return 0;
839	if (req->newlen - req->newidx < l)
840		return (EINVAL);
841	bcopy((char *)req->newptr + req->newidx, p, l);
842	req->newidx += l;
843	return (0);
844}
845
846int
847kernel_sysctl(struct proc *p, int *name, u_int namelen, void *old, size_t *oldlenp, void *new, size_t newlen, size_t *retval)
848{
849	int error = 0;
850	struct sysctl_req req;
851
852	bzero(&req, sizeof req);
853
854	req.p = p;
855
856	if (oldlenp) {
857		req.oldlen = *oldlenp;
858	}
859
860	if (old) {
861		req.oldptr= old;
862	}
863
864	if (newlen) {
865		req.newlen = newlen;
866		req.newptr = new;
867	}
868
869	req.oldfunc = sysctl_old_kernel;
870	req.newfunc = sysctl_new_kernel;
871	req.lock = 1;
872
873	/* XXX this should probably be done in a general way */
874	while (memlock.sl_lock) {
875		memlock.sl_want = 1;
876		(void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0);
877		memlock.sl_locked++;
878	}
879	memlock.sl_lock = 1;
880
881	error = sysctl_root(0, name, namelen, &req);
882
883	if (req.lock == 2)
884		vsunlock(req.oldptr, req.oldlen);
885
886	memlock.sl_lock = 0;
887
888	if (memlock.sl_want) {
889		memlock.sl_want = 0;
890		wakeup((caddr_t)&memlock);
891	}
892
893	if (error && error != ENOMEM)
894		return (error);
895
896	if (retval) {
897		if (req.oldptr && req.oldidx > req.oldlen)
898			*retval = req.oldlen;
899		else
900			*retval = req.oldidx;
901	}
902	return (error);
903}
904
905/*
906 * Transfer function to/from user space.
907 */
908static int
909sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
910{
911	int error = 0;
912	size_t i = 0;
913
914	if (req->lock == 1 && req->oldptr) {
915		vslock(req->oldptr, req->oldlen);
916		req->lock = 2;
917	}
918	if (req->oldptr) {
919		i = l;
920		if (req->oldlen <= req->oldidx)
921			i = 0;
922		else
923			if (i > req->oldlen - req->oldidx)
924				i = req->oldlen - req->oldidx;
925		if (i > 0)
926			error = copyout(p, (char *)req->oldptr + req->oldidx,
927					i);
928	}
929	req->oldidx += l;
930	if (error)
931		return (error);
932	if (req->oldptr && i < l)
933		return (ENOMEM);
934	return (0);
935}
936
937static int
938sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
939{
940	int error;
941
942	if (!req->newptr)
943		return 0;
944	if (req->newlen - req->newidx < l)
945		return (EINVAL);
946	error = copyin((char *)req->newptr + req->newidx, p, l);
947	req->newidx += l;
948	return (error);
949}
950
951int
952sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
953    int *nindx, struct sysctl_req *req)
954{
955	struct sysctl_oid *oid;
956	int indx;
957
958	oid = SLIST_FIRST(&sysctl__children);
959	indx = 0;
960	while (oid && indx < CTL_MAXNAME) {
961		if (oid->oid_number == name[indx]) {
962			indx++;
963			if (oid->oid_kind & CTLFLAG_NOLOCK)
964				req->lock = 0;
965			if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
966				if (oid->oid_handler != NULL ||
967				    indx == namelen) {
968					*noid = oid;
969					if (nindx != NULL)
970						*nindx = indx;
971					return (0);
972				}
973				oid = SLIST_FIRST(
974				    (struct sysctl_oid_list *)oid->oid_arg1);
975			} else if (indx == namelen) {
976				*noid = oid;
977				if (nindx != NULL)
978					*nindx = indx;
979				return (0);
980			} else {
981				return (ENOTDIR);
982			}
983		} else {
984			oid = SLIST_NEXT(oid, oid_link);
985		}
986	}
987	return (ENOENT);
988}
989
990/*
991 * Traverse our tree, and find the right node, execute whatever it points
992 * to, and return the resulting error code.
993 */
994
995int
996sysctl_root(SYSCTL_HANDLER_ARGS)
997{
998	struct sysctl_oid *oid;
999	int error, indx;
1000
1001	error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1002	if (error)
1003		return (error);
1004
1005	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1006		/*
1007		 * You can't call a sysctl when it's a node, but has
1008		 * no handler.  Inform the user that it's a node.
1009		 * The indx may or may not be the same as namelen.
1010		 */
1011		if (oid->oid_handler == NULL)
1012			return (EISDIR);
1013	}
1014
1015	/* If writing isn't allowed */
1016	if (req->newptr && (!(oid->oid_kind & CTLFLAG_WR) ||
1017	    ((oid->oid_kind & CTLFLAG_SECURE) && securelevel > 0)))
1018		return (EPERM);
1019
1020	/* Most likely only root can write */
1021	if (!(oid->oid_kind & CTLFLAG_ANYBODY) &&
1022	    req->newptr && req->p &&
1023	    (error = suser_xxx(0, req->p,
1024	    (oid->oid_kind & CTLFLAG_PRISON) ? PRISON_ROOT : 0)))
1025		return (error);
1026
1027	if (!oid->oid_handler)
1028		return EINVAL;
1029
1030	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1031		error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1032		    req);
1033	else
1034		error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1035		    req);
1036	return (error);
1037}
1038
1039#ifndef _SYS_SYSPROTO_H_
1040struct sysctl_args {
1041	int	*name;
1042	u_int	namelen;
1043	void	*old;
1044	size_t	*oldlenp;
1045	void	*new;
1046	size_t	newlen;
1047};
1048#endif
1049
1050int
1051__sysctl(struct proc *p, struct sysctl_args *uap)
1052{
1053	int error, i, name[CTL_MAXNAME];
1054	size_t j;
1055
1056	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1057		return (EINVAL);
1058
1059 	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1060 	if (error)
1061		return (error);
1062
1063	error = userland_sysctl(p, name, uap->namelen,
1064		uap->old, uap->oldlenp, 0,
1065		uap->new, uap->newlen, &j);
1066	if (error && error != ENOMEM)
1067		return (error);
1068	if (uap->oldlenp) {
1069		i = copyout(&j, uap->oldlenp, sizeof(j));
1070		if (i)
1071			return (i);
1072	}
1073	return (error);
1074}
1075
1076/*
1077 * This is used from various compatibility syscalls too.  That's why name
1078 * must be in kernel space.
1079 */
1080int
1081userland_sysctl(struct proc *p, int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
1082{
1083	int error = 0;
1084	struct sysctl_req req, req2;
1085
1086	bzero(&req, sizeof req);
1087
1088	req.p = p;
1089
1090	if (oldlenp) {
1091		if (inkernel) {
1092			req.oldlen = *oldlenp;
1093		} else {
1094			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1095			if (error)
1096				return (error);
1097		}
1098	}
1099
1100	if (old) {
1101		if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1102			return (EFAULT);
1103		req.oldptr= old;
1104	}
1105
1106	if (newlen) {
1107		if (!useracc(new, req.newlen, VM_PROT_READ))
1108			return (EFAULT);
1109		req.newlen = newlen;
1110		req.newptr = new;
1111	}
1112
1113	req.oldfunc = sysctl_old_user;
1114	req.newfunc = sysctl_new_user;
1115	req.lock = 1;
1116
1117	/* XXX this should probably be done in a general way */
1118	while (memlock.sl_lock) {
1119		memlock.sl_want = 1;
1120		(void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0);
1121		memlock.sl_locked++;
1122	}
1123	memlock.sl_lock = 1;
1124
1125	do {
1126	    req2 = req;
1127	    error = sysctl_root(0, name, namelen, &req2);
1128	} while (error == EAGAIN);
1129
1130	req = req2;
1131	if (req.lock == 2)
1132		vsunlock(req.oldptr, req.oldlen);
1133
1134	memlock.sl_lock = 0;
1135
1136	if (memlock.sl_want) {
1137		memlock.sl_want = 0;
1138		wakeup((caddr_t)&memlock);
1139	}
1140
1141	if (error && error != ENOMEM)
1142		return (error);
1143
1144	if (retval) {
1145		if (req.oldptr && req.oldidx > req.oldlen)
1146			*retval = req.oldlen;
1147		else
1148			*retval = req.oldidx;
1149	}
1150	return (error);
1151}
1152
1153#ifdef COMPAT_43
1154#include <sys/socket.h>
1155#include <vm/vm_param.h>
1156
1157#define	KINFO_PROC		(0<<8)
1158#define	KINFO_RT		(1<<8)
1159#define	KINFO_VNODE		(2<<8)
1160#define	KINFO_FILE		(3<<8)
1161#define	KINFO_METER		(4<<8)
1162#define	KINFO_LOADAVG		(5<<8)
1163#define	KINFO_CLOCKRATE		(6<<8)
1164
1165/* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
1166#define	KINFO_BSDI_SYSINFO	(101<<8)
1167
1168/*
1169 * XXX this is bloat, but I hope it's better here than on the potentially
1170 * limited kernel stack...  -Peter
1171 */
1172
1173static struct {
1174	int	bsdi_machine;		/* "i386" on BSD/386 */
1175/*      ^^^ this is an offset to the string, relative to the struct start */
1176	char	*pad0;
1177	long	pad1;
1178	long	pad2;
1179	long	pad3;
1180	u_long	pad4;
1181	u_long	pad5;
1182	u_long	pad6;
1183
1184	int	bsdi_ostype;		/* "BSD/386" on BSD/386 */
1185	int	bsdi_osrelease;		/* "1.1" on BSD/386 */
1186	long	pad7;
1187	long	pad8;
1188	char	*pad9;
1189
1190	long	pad10;
1191	long	pad11;
1192	int	pad12;
1193	long	pad13;
1194	quad_t	pad14;
1195	long	pad15;
1196
1197	struct	timeval pad16;
1198	/* we dont set this, because BSDI's uname used gethostname() instead */
1199	int	bsdi_hostname;		/* hostname on BSD/386 */
1200
1201	/* the actual string data is appended here */
1202
1203} bsdi_si;
1204/*
1205 * this data is appended to the end of the bsdi_si structure during copyout.
1206 * The "char *" offsets are relative to the base of the bsdi_si struct.
1207 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
1208 * should not exceed the length of the buffer here... (or else!! :-)
1209 */
1210static char bsdi_strings[80];	/* It had better be less than this! */
1211
1212#ifndef _SYS_SYSPROTO_H_
1213struct getkerninfo_args {
1214	int	op;
1215	char	*where;
1216	size_t	*size;
1217	int	arg;
1218};
1219#endif
1220
1221int
1222ogetkerninfo(struct proc *p, struct getkerninfo_args *uap)
1223{
1224	int error, name[6];
1225	size_t size;
1226
1227	switch (uap->op & 0xff00) {
1228
1229	case KINFO_RT:
1230		name[0] = CTL_NET;
1231		name[1] = PF_ROUTE;
1232		name[2] = 0;
1233		name[3] = (uap->op & 0xff0000) >> 16;
1234		name[4] = uap->op & 0xff;
1235		name[5] = uap->arg;
1236		error = userland_sysctl(p, name, 6, uap->where, uap->size,
1237			0, 0, 0, &size);
1238		break;
1239
1240	case KINFO_VNODE:
1241		name[0] = CTL_KERN;
1242		name[1] = KERN_VNODE;
1243		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1244			0, 0, 0, &size);
1245		break;
1246
1247	case KINFO_PROC:
1248		name[0] = CTL_KERN;
1249		name[1] = KERN_PROC;
1250		name[2] = uap->op & 0xff;
1251		name[3] = uap->arg;
1252		error = userland_sysctl(p, name, 4, uap->where, uap->size,
1253			0, 0, 0, &size);
1254		break;
1255
1256	case KINFO_FILE:
1257		name[0] = CTL_KERN;
1258		name[1] = KERN_FILE;
1259		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1260			0, 0, 0, &size);
1261		break;
1262
1263	case KINFO_METER:
1264		name[0] = CTL_VM;
1265		name[1] = VM_METER;
1266		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1267			0, 0, 0, &size);
1268		break;
1269
1270	case KINFO_LOADAVG:
1271		name[0] = CTL_VM;
1272		name[1] = VM_LOADAVG;
1273		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1274			0, 0, 0, &size);
1275		break;
1276
1277	case KINFO_CLOCKRATE:
1278		name[0] = CTL_KERN;
1279		name[1] = KERN_CLOCKRATE;
1280		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1281			0, 0, 0, &size);
1282		break;
1283
1284	case KINFO_BSDI_SYSINFO: {
1285		/*
1286		 * this is pretty crude, but it's just enough for uname()
1287		 * from BSDI's 1.x libc to work.
1288		 *
1289		 * In particular, it doesn't return the same results when
1290		 * the supplied buffer is too small.  BSDI's version apparently
1291		 * will return the amount copied, and set the *size to how
1292		 * much was needed.  The emulation framework here isn't capable
1293		 * of that, so we just set both to the amount copied.
1294		 * BSDI's 2.x product apparently fails with ENOMEM in this
1295		 * scenario.
1296		 */
1297
1298		u_int needed;
1299		u_int left;
1300		char *s;
1301
1302		bzero((char *)&bsdi_si, sizeof(bsdi_si));
1303		bzero(bsdi_strings, sizeof(bsdi_strings));
1304
1305		s = bsdi_strings;
1306
1307		bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
1308		strcpy(s, ostype);
1309		s += strlen(s) + 1;
1310
1311		bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
1312		strcpy(s, osrelease);
1313		s += strlen(s) + 1;
1314
1315		bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
1316		strcpy(s, machine);
1317		s += strlen(s) + 1;
1318
1319		needed = sizeof(bsdi_si) + (s - bsdi_strings);
1320
1321		if (uap->where == NULL) {
1322			/* process is asking how much buffer to supply.. */
1323			size = needed;
1324			error = 0;
1325			break;
1326		}
1327
1328
1329		/* if too much buffer supplied, trim it down */
1330		if (size > needed)
1331			size = needed;
1332
1333		/* how much of the buffer is remaining */
1334		left = size;
1335
1336		if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
1337			break;
1338
1339		/* is there any point in continuing? */
1340		if (left > sizeof(bsdi_si)) {
1341			left -= sizeof(bsdi_si);
1342			error = copyout(&bsdi_strings,
1343					uap->where + sizeof(bsdi_si), left);
1344		}
1345		break;
1346	}
1347
1348	default:
1349		return (EOPNOTSUPP);
1350	}
1351	if (error)
1352		return (error);
1353	p->p_retval[0] = size;
1354	if (uap->size)
1355		error = copyout((caddr_t)&size, (caddr_t)uap->size,
1356		    sizeof(size));
1357	return (error);
1358}
1359#endif /* COMPAT_43 */
1360