kern_sysctl.c revision 101422
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 101422 2002-08-06 11:28:09Z truckman $
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 (!namelen) {
546			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
547				return 0;
548			if (oidp->oid_handler)
549				/* We really should call the handler here...*/
550				return 0;
551			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
552			if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
553				len, level+1, oidpp))
554				return 0;
555			goto next;
556		}
557
558		if (oidp->oid_number < *name)
559			continue;
560
561		if (oidp->oid_number > *name) {
562			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
563				return 0;
564			if (oidp->oid_handler)
565				return 0;
566			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
567			if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
568				next+1, len, level+1, oidpp))
569				return (0);
570			goto next;
571		}
572		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
573			continue;
574
575		if (oidp->oid_handler)
576			continue;
577
578		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
579		if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
580			len, level+1, oidpp))
581			return (0);
582	next:
583		namelen = 1;
584		*len = level;
585	}
586	return 1;
587}
588
589static int
590sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
591{
592	int *name = (int *) arg1;
593	u_int namelen = arg2;
594	int i, j, error;
595	struct sysctl_oid *oid;
596	struct sysctl_oid_list *lsp = &sysctl__children;
597	int newoid[CTL_MAXNAME];
598
599	i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
600	if (i)
601		return ENOENT;
602	error = SYSCTL_OUT(req, newoid, j * sizeof (int));
603	return (error);
604}
605
606SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
607
608static int
609name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
610{
611	int i;
612	struct sysctl_oid *oidp;
613	struct sysctl_oid_list *lsp = &sysctl__children;
614	char *p;
615
616	if (!*name)
617		return ENOENT;
618
619	p = name + strlen(name) - 1 ;
620	if (*p == '.')
621		*p = '\0';
622
623	*len = 0;
624
625	for (p = name; *p && *p != '.'; p++)
626		;
627	i = *p;
628	if (i == '.')
629		*p = '\0';
630
631	oidp = SLIST_FIRST(lsp);
632
633	while (oidp && *len < CTL_MAXNAME) {
634		if (strcmp(name, oidp->oid_name)) {
635			oidp = SLIST_NEXT(oidp, oid_link);
636			continue;
637		}
638		*oid++ = oidp->oid_number;
639		(*len)++;
640
641		if (!i) {
642			if (oidpp)
643				*oidpp = oidp;
644			return (0);
645		}
646
647		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
648			break;
649
650		if (oidp->oid_handler)
651			break;
652
653		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
654		oidp = SLIST_FIRST(lsp);
655		name = p+1;
656		for (p = name; *p && *p != '.'; p++)
657				;
658		i = *p;
659		if (i == '.')
660			*p = '\0';
661	}
662	return ENOENT;
663}
664
665static int
666sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
667{
668	char *p;
669	int error, oid[CTL_MAXNAME], len;
670	struct sysctl_oid *op = 0;
671
672	if (!req->newlen)
673		return ENOENT;
674	if (req->newlen >= MAXPATHLEN)	/* XXX arbitrary, undocumented */
675		return (ENAMETOOLONG);
676
677	p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
678
679	error = SYSCTL_IN(req, p, req->newlen);
680	if (error) {
681		free(p, M_SYSCTL);
682		return (error);
683	}
684
685	p [req->newlen] = '\0';
686
687	error = name2oid(p, oid, &len, &op);
688
689	free(p, M_SYSCTL);
690
691	if (error)
692		return (error);
693
694	error = SYSCTL_OUT(req, oid, len * sizeof *oid);
695	return (error);
696}
697
698SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
699	sysctl_sysctl_name2oid, "I", "");
700
701static int
702sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
703{
704	struct sysctl_oid *oid;
705	int error;
706
707	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
708	if (error)
709		return (error);
710
711	if (!oid->oid_fmt)
712		return (ENOENT);
713	error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
714	if (error)
715		return (error);
716	error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
717	return (error);
718}
719
720
721SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
722
723static int
724sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
725{
726	struct sysctl_oid *oid;
727	int error;
728
729	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
730	if (error)
731		return (error);
732
733	if (!oid->descr)
734		return (ENOENT);
735	error = SYSCTL_OUT(req, oid->descr, strlen(oid->descr) + 1);
736	return (error);
737}
738
739SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
740
741/*
742 * Default "handler" functions.
743 */
744
745/*
746 * Handle an int, signed or unsigned.
747 * Two cases:
748 *     a variable:  point arg1 at it.
749 *     a constant:  pass it in arg2.
750 */
751
752int
753sysctl_handle_int(SYSCTL_HANDLER_ARGS)
754{
755	int tmpout, error = 0;
756
757	/*
758	 * Attempt to get a coherent snapshot by making a copy of the data.
759	 */
760	if (arg1)
761		tmpout = *(int *)arg1;
762	else
763		tmpout = arg2;
764	error = SYSCTL_OUT(req, &tmpout, sizeof(int));
765
766	if (error || !req->newptr)
767		return (error);
768
769	if (!arg1)
770		error = EPERM;
771	else
772		error = SYSCTL_IN(req, arg1, sizeof(int));
773	return (error);
774}
775
776/*
777 * Handle a long, signed or unsigned.  arg1 points to it.
778 */
779
780int
781sysctl_handle_long(SYSCTL_HANDLER_ARGS)
782{
783	int error = 0;
784	long tmpout;
785
786	/*
787	 * Attempt to get a coherent snapshot by making a copy of the data.
788	 */
789	if (!arg1)
790		return (EINVAL);
791	tmpout = *(long *)arg1;
792	error = SYSCTL_OUT(req, &tmpout, sizeof(long));
793
794	if (error || !req->newptr)
795		return (error);
796
797	error = SYSCTL_IN(req, arg1, sizeof(long));
798	return (error);
799}
800
801/*
802 * Handle our generic '\0' terminated 'C' string.
803 * Two cases:
804 * 	a variable string:  point arg1 at it, arg2 is max length.
805 * 	a constant string:  point arg1 at it, arg2 is zero.
806 */
807
808int
809sysctl_handle_string(SYSCTL_HANDLER_ARGS)
810{
811	int error=0;
812	char *tmparg;
813	size_t outlen;
814
815	/*
816	 * Attempt to get a coherent snapshot by copying to a
817	 * temporary kernel buffer.
818	 */
819retry:
820	outlen = strlen((char *)arg1)+1;
821	tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
822	strncpy(tmparg, (char *)arg1, outlen);
823	if (tmparg[outlen-1] != '\0') {
824		free(tmparg, M_SYSCTLTMP);
825		goto retry;
826	}
827	error = SYSCTL_OUT(req, tmparg, outlen);
828	free(tmparg, M_SYSCTLTMP);
829
830	if (error || !req->newptr)
831		return (error);
832
833	if ((req->newlen - req->newidx) >= arg2) {
834		error = EINVAL;
835	} else {
836		arg2 = (req->newlen - req->newidx);
837		error = SYSCTL_IN(req, arg1, arg2);
838		((char *)arg1)[arg2] = '\0';
839	}
840
841	return (error);
842}
843
844/*
845 * Handle any kind of opaque data.
846 * arg1 points to it, arg2 is the size.
847 */
848
849int
850sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
851{
852	int error;
853	void *tmparg;
854
855	/*
856	 * Attempt to get a coherent snapshot, either by wiring the
857	 * user space buffer or copying to a temporary kernel buffer
858	 * depending on the size of the data.
859	 */
860	if (arg2 > PAGE_SIZE) {
861		sysctl_wire_old_buffer(req, arg2);
862		error = SYSCTL_OUT(req, arg1, arg2);
863	} else {
864		tmparg = malloc(arg2, M_SYSCTLTMP, M_WAITOK);
865		bcopy(arg1, tmparg, arg2);
866		error = SYSCTL_OUT(req, tmparg, arg2);
867		free(tmparg, M_SYSCTLTMP);
868	}
869
870	if (error || !req->newptr)
871		return (error);
872
873	error = SYSCTL_IN(req, arg1, arg2);
874
875	return (error);
876}
877
878/*
879 * Transfer functions to/from kernel space.
880 * XXX: rather untested at this point
881 */
882static int
883sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
884{
885	size_t i = 0;
886
887	if (req->oldptr) {
888		i = l;
889		if (req->oldlen <= req->oldidx)
890			i = 0;
891		else
892			if (i > req->oldlen - req->oldidx)
893				i = req->oldlen - req->oldidx;
894		if (i > 0)
895			bcopy(p, (char *)req->oldptr + req->oldidx, i);
896	}
897	req->oldidx += l;
898	if (req->oldptr && i != l)
899		return (ENOMEM);
900	return (0);
901}
902
903static int
904sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
905{
906	if (!req->newptr)
907		return 0;
908	if (req->newlen - req->newidx < l)
909		return (EINVAL);
910	bcopy((char *)req->newptr + req->newidx, p, l);
911	req->newidx += l;
912	return (0);
913}
914
915int
916kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
917    size_t *oldlenp, void *new, size_t newlen, size_t *retval)
918{
919	int error = 0;
920	struct sysctl_req req;
921
922	bzero(&req, sizeof req);
923
924	req.td = td;
925
926	if (oldlenp) {
927		req.oldlen = *oldlenp;
928	}
929
930	if (old) {
931		req.oldptr= old;
932	}
933
934	if (new != NULL) {
935		req.newlen = newlen;
936		req.newptr = new;
937	}
938
939	req.oldfunc = sysctl_old_kernel;
940	req.newfunc = sysctl_new_kernel;
941	req.lock = 1;
942
943	SYSCTL_LOCK();
944
945	error = sysctl_root(0, name, namelen, &req);
946
947	if (req.lock == 2)
948		vsunlock(req.oldptr, req.oldlen);
949
950	SYSCTL_UNLOCK();
951
952	if (error && error != ENOMEM)
953		return (error);
954
955	if (retval) {
956		if (req.oldptr && req.oldidx > req.oldlen)
957			*retval = req.oldlen;
958		else
959			*retval = req.oldidx;
960	}
961	return (error);
962}
963
964int
965kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
966    void *new, size_t newlen, size_t *retval)
967{
968        int oid[CTL_MAXNAME];
969        size_t oidlen, plen;
970	int error;
971
972	oid[0] = 0;		/* sysctl internal magic */
973	oid[1] = 3;		/* name2oid */
974	oidlen = sizeof(oid);
975
976	error = kernel_sysctl(td, oid, 2, oid, &oidlen,
977	    (void *)name, strlen(name), &plen);
978	if (error)
979		return (error);
980
981	error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
982	    new, newlen, retval);
983	return (error);
984}
985
986/*
987 * Transfer function to/from user space.
988 */
989static int
990sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
991{
992	int error = 0;
993	size_t i = 0;
994
995	if (req->lock == 1 && req->oldptr)
996		WITNESS_SLEEP(1, NULL);
997	if (req->oldptr) {
998		i = l;
999		if (req->oldlen <= req->oldidx)
1000			i = 0;
1001		else
1002			if (i > req->oldlen - req->oldidx)
1003				i = req->oldlen - req->oldidx;
1004		if (i > 0)
1005			error = copyout(p, (char *)req->oldptr + req->oldidx,
1006					i);
1007	}
1008	req->oldidx += l;
1009	if (error)
1010		return (error);
1011	if (req->oldptr && i < l)
1012		return (ENOMEM);
1013	return (0);
1014}
1015
1016static int
1017sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1018{
1019	int error;
1020
1021	if (!req->newptr)
1022		return 0;
1023	if (req->newlen - req->newidx < l)
1024		return (EINVAL);
1025	error = copyin((char *)req->newptr + req->newidx, p, l);
1026	req->newidx += l;
1027	return (error);
1028}
1029
1030/*
1031 * Wire the user space destination buffer.  If set to a value greater than
1032 * zero, the len parameter limits the maximum amount of wired memory.
1033 *
1034 * XXX - The len parameter is currently ignored due to the lack of
1035 * a place to save it in the sysctl_req structure so that the matching
1036 * amount of memory can be unwired in the sysctl exit code.
1037 */
1038void
1039sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1040{
1041	if (req->lock == 1 && req->oldptr && req->oldfunc == sysctl_old_user) {
1042		vslock(req->oldptr, req->oldlen);
1043		req->lock = 2;
1044	}
1045}
1046
1047int
1048sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1049    int *nindx, struct sysctl_req *req)
1050{
1051	struct sysctl_oid *oid;
1052	int indx;
1053
1054	oid = SLIST_FIRST(&sysctl__children);
1055	indx = 0;
1056	while (oid && indx < CTL_MAXNAME) {
1057		if (oid->oid_number == name[indx]) {
1058			indx++;
1059			if (oid->oid_kind & CTLFLAG_NOLOCK)
1060				req->lock = 0;
1061			if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1062				if (oid->oid_handler != NULL ||
1063				    indx == namelen) {
1064					*noid = oid;
1065					if (nindx != NULL)
1066						*nindx = indx;
1067					return (0);
1068				}
1069				oid = SLIST_FIRST(
1070				    (struct sysctl_oid_list *)oid->oid_arg1);
1071			} else if (indx == namelen) {
1072				*noid = oid;
1073				if (nindx != NULL)
1074					*nindx = indx;
1075				return (0);
1076			} else {
1077				return (ENOTDIR);
1078			}
1079		} else {
1080			oid = SLIST_NEXT(oid, oid_link);
1081		}
1082	}
1083	return (ENOENT);
1084}
1085
1086/*
1087 * Traverse our tree, and find the right node, execute whatever it points
1088 * to, and return the resulting error code.
1089 */
1090
1091int
1092sysctl_root(SYSCTL_HANDLER_ARGS)
1093{
1094	struct sysctl_oid *oid;
1095	int error, indx;
1096
1097	error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1098	if (error)
1099		return (error);
1100
1101	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1102		/*
1103		 * You can't call a sysctl when it's a node, but has
1104		 * no handler.  Inform the user that it's a node.
1105		 * The indx may or may not be the same as namelen.
1106		 */
1107		if (oid->oid_handler == NULL)
1108			return (EISDIR);
1109	}
1110
1111	/* Is this sysctl writable? */
1112	if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1113		return (EPERM);
1114
1115	KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1116
1117	/* Is this sysctl sensitive to securelevels? */
1118	if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1119		error = securelevel_gt(req->td->td_ucred, 0);
1120		if (error)
1121			return (error);
1122	}
1123
1124	/* Is this sysctl writable by only privileged users? */
1125	if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1126		int flags;
1127
1128		if (oid->oid_kind & CTLFLAG_PRISON)
1129			flags = PRISON_ROOT;
1130		else
1131			flags = 0;
1132		error = suser_cred(req->td->td_ucred, flags);
1133		if (error)
1134			return (error);
1135	}
1136
1137	if (!oid->oid_handler)
1138		return EINVAL;
1139
1140	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE)
1141		error = oid->oid_handler(oid, (int *)arg1 + indx, arg2 - indx,
1142		    req);
1143	else
1144		error = oid->oid_handler(oid, oid->oid_arg1, oid->oid_arg2,
1145		    req);
1146	return (error);
1147}
1148
1149#ifndef _SYS_SYSPROTO_H_
1150struct sysctl_args {
1151	int	*name;
1152	u_int	namelen;
1153	void	*old;
1154	size_t	*oldlenp;
1155	void	*new;
1156	size_t	newlen;
1157};
1158#endif
1159
1160/*
1161 * MPSAFE
1162 */
1163int
1164__sysctl(struct thread *td, struct sysctl_args *uap)
1165{
1166	int error, name[CTL_MAXNAME];
1167	size_t j;
1168
1169	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1170		return (EINVAL);
1171
1172 	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1173 	if (error)
1174		return (error);
1175
1176	mtx_lock(&Giant);
1177
1178	error = userland_sysctl(td, name, uap->namelen,
1179		uap->old, uap->oldlenp, 0,
1180		uap->new, uap->newlen, &j);
1181	if (error && error != ENOMEM)
1182		goto done2;
1183	if (uap->oldlenp) {
1184		int i = copyout(&j, uap->oldlenp, sizeof(j));
1185		if (i)
1186			error = i;
1187	}
1188done2:
1189	mtx_unlock(&Giant);
1190	return (error);
1191}
1192
1193/*
1194 * This is used from various compatibility syscalls too.  That's why name
1195 * must be in kernel space.
1196 */
1197int
1198userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1199    size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
1200{
1201	int error = 0;
1202	struct sysctl_req req, req2;
1203
1204	bzero(&req, sizeof req);
1205
1206	req.td = td;
1207
1208	if (oldlenp) {
1209		if (inkernel) {
1210			req.oldlen = *oldlenp;
1211		} else {
1212			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1213			if (error)
1214				return (error);
1215		}
1216	}
1217
1218	if (old) {
1219		if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1220			return (EFAULT);
1221		req.oldptr= old;
1222	}
1223
1224	if (new != NULL) {
1225		if (!useracc(new, req.newlen, VM_PROT_READ))
1226			return (EFAULT);
1227		req.newlen = newlen;
1228		req.newptr = new;
1229	}
1230
1231	req.oldfunc = sysctl_old_user;
1232	req.newfunc = sysctl_new_user;
1233	req.lock = 1;
1234
1235	SYSCTL_LOCK();
1236
1237	do {
1238	    req2 = req;
1239	    error = sysctl_root(0, name, namelen, &req2);
1240	} while (error == EAGAIN);
1241
1242	req = req2;
1243	if (req.lock == 2)
1244		vsunlock(req.oldptr, req.oldlen);
1245
1246	SYSCTL_UNLOCK();
1247
1248	if (error && error != ENOMEM)
1249		return (error);
1250
1251	if (retval) {
1252		if (req.oldptr && req.oldidx > req.oldlen)
1253			*retval = req.oldlen;
1254		else
1255			*retval = req.oldidx;
1256	}
1257	return (error);
1258}
1259
1260#ifdef COMPAT_43
1261#include <sys/socket.h>
1262#include <vm/vm_param.h>
1263
1264#define	KINFO_PROC		(0<<8)
1265#define	KINFO_RT		(1<<8)
1266#define	KINFO_VNODE		(2<<8)
1267#define	KINFO_FILE		(3<<8)
1268#define	KINFO_METER		(4<<8)
1269#define	KINFO_LOADAVG		(5<<8)
1270#define	KINFO_CLOCKRATE		(6<<8)
1271
1272/* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
1273#define	KINFO_BSDI_SYSINFO	(101<<8)
1274
1275/*
1276 * XXX this is bloat, but I hope it's better here than on the potentially
1277 * limited kernel stack...  -Peter
1278 */
1279
1280static struct {
1281	int	bsdi_machine;		/* "i386" on BSD/386 */
1282/*      ^^^ this is an offset to the string, relative to the struct start */
1283	char	*pad0;
1284	long	pad1;
1285	long	pad2;
1286	long	pad3;
1287	u_long	pad4;
1288	u_long	pad5;
1289	u_long	pad6;
1290
1291	int	bsdi_ostype;		/* "BSD/386" on BSD/386 */
1292	int	bsdi_osrelease;		/* "1.1" on BSD/386 */
1293	long	pad7;
1294	long	pad8;
1295	char	*pad9;
1296
1297	long	pad10;
1298	long	pad11;
1299	int	pad12;
1300	long	pad13;
1301	quad_t	pad14;
1302	long	pad15;
1303
1304	struct	timeval pad16;
1305	/* we dont set this, because BSDI's uname used gethostname() instead */
1306	int	bsdi_hostname;		/* hostname on BSD/386 */
1307
1308	/* the actual string data is appended here */
1309
1310} bsdi_si;
1311/*
1312 * this data is appended to the end of the bsdi_si structure during copyout.
1313 * The "char *" offsets are relative to the base of the bsdi_si struct.
1314 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
1315 * should not exceed the length of the buffer here... (or else!! :-)
1316 */
1317static char bsdi_strings[80];	/* It had better be less than this! */
1318
1319#ifndef _SYS_SYSPROTO_H_
1320struct getkerninfo_args {
1321	int	op;
1322	char	*where;
1323	size_t	*size;
1324	int	arg;
1325};
1326#endif
1327
1328/*
1329 * MPSAFE
1330 */
1331int
1332ogetkerninfo(struct thread *td, struct getkerninfo_args *uap)
1333{
1334	int error, name[6];
1335	size_t size;
1336	u_int needed = 0;
1337
1338	mtx_lock(&Giant);
1339
1340	switch (uap->op & 0xff00) {
1341
1342	case KINFO_RT:
1343		name[0] = CTL_NET;
1344		name[1] = PF_ROUTE;
1345		name[2] = 0;
1346		name[3] = (uap->op & 0xff0000) >> 16;
1347		name[4] = uap->op & 0xff;
1348		name[5] = uap->arg;
1349		error = userland_sysctl(td, name, 6, uap->where, uap->size,
1350			0, 0, 0, &size);
1351		break;
1352
1353	case KINFO_VNODE:
1354		name[0] = CTL_KERN;
1355		name[1] = KERN_VNODE;
1356		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1357			0, 0, 0, &size);
1358		break;
1359
1360	case KINFO_PROC:
1361		name[0] = CTL_KERN;
1362		name[1] = KERN_PROC;
1363		name[2] = uap->op & 0xff;
1364		name[3] = uap->arg;
1365		error = userland_sysctl(td, name, 4, uap->where, uap->size,
1366			0, 0, 0, &size);
1367		break;
1368
1369	case KINFO_FILE:
1370		name[0] = CTL_KERN;
1371		name[1] = KERN_FILE;
1372		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1373			0, 0, 0, &size);
1374		break;
1375
1376	case KINFO_METER:
1377		name[0] = CTL_VM;
1378		name[1] = VM_METER;
1379		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1380			0, 0, 0, &size);
1381		break;
1382
1383	case KINFO_LOADAVG:
1384		name[0] = CTL_VM;
1385		name[1] = VM_LOADAVG;
1386		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1387			0, 0, 0, &size);
1388		break;
1389
1390	case KINFO_CLOCKRATE:
1391		name[0] = CTL_KERN;
1392		name[1] = KERN_CLOCKRATE;
1393		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1394			0, 0, 0, &size);
1395		break;
1396
1397	case KINFO_BSDI_SYSINFO: {
1398		/*
1399		 * this is pretty crude, but it's just enough for uname()
1400		 * from BSDI's 1.x libc to work.
1401		 *
1402		 * *size gives the size of the buffer before the call, and
1403		 * the amount of data copied after a successful call.
1404		 * If successful, the return value is the amount of data
1405		 * available, which can be larger than *size.
1406		 *
1407		 * BSDI's 2.x product apparently fails with ENOMEM if *size
1408		 * is too small.
1409		 */
1410
1411		u_int left;
1412		char *s;
1413
1414		bzero((char *)&bsdi_si, sizeof(bsdi_si));
1415		bzero(bsdi_strings, sizeof(bsdi_strings));
1416
1417		s = bsdi_strings;
1418
1419		bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
1420		strcpy(s, ostype);
1421		s += strlen(s) + 1;
1422
1423		bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
1424		strcpy(s, osrelease);
1425		s += strlen(s) + 1;
1426
1427		bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
1428		strcpy(s, machine);
1429		s += strlen(s) + 1;
1430
1431		needed = sizeof(bsdi_si) + (s - bsdi_strings);
1432
1433		if ((uap->where == NULL) || (uap->size == NULL)) {
1434			/* process is asking how much buffer to supply.. */
1435			size = needed;
1436			error = 0;
1437			break;
1438		}
1439
1440		if ((error = copyin(uap->size, &size, sizeof(size))) != 0)
1441			break;
1442
1443		/* if too much buffer supplied, trim it down */
1444		if (size > needed)
1445			size = needed;
1446
1447		/* how much of the buffer is remaining */
1448		left = size;
1449
1450		if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
1451			break;
1452
1453		/* is there any point in continuing? */
1454		if (left > sizeof(bsdi_si)) {
1455			left -= sizeof(bsdi_si);
1456			error = copyout(&bsdi_strings,
1457					uap->where + sizeof(bsdi_si), left);
1458		}
1459		break;
1460	}
1461
1462	default:
1463		error = EOPNOTSUPP;
1464		break;
1465	}
1466	if (error == 0) {
1467		td->td_retval[0] = needed ? needed : size;
1468		if (uap->size) {
1469			error = copyout(&size, uap->size, sizeof(size));
1470		}
1471	}
1472	mtx_unlock(&Giant);
1473	return (error);
1474}
1475#endif /* COMPAT_43 */
1476