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