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