kern_sysctl.c revision 127050
190792Sgshapiro/*-
2261363Sgshapiro * Copyright (c) 1982, 1986, 1989, 1993
390792Sgshapiro *	The Regents of the University of California.  All rights reserved.
490792Sgshapiro *
590792Sgshapiro * This code is derived from software contributed to Berkeley by
690792Sgshapiro * Mike Karels at Berkeley Software Design, Inc.
790792Sgshapiro *
890792Sgshapiro * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
990792Sgshapiro * project, to make these variables more userfriendly.
1090792Sgshapiro *
1190792Sgshapiro * Redistribution and use in source and binary forms, with or without
1290792Sgshapiro * modification, are permitted provided that the following conditions
1390792Sgshapiro * are met:
1490792Sgshapiro * 1. Redistributions of source code must retain the above copyright
1590792Sgshapiro *    notice, this list of conditions and the following disclaimer.
16266692Sgshapiro * 2. Redistributions in binary form must reproduce the above copyright
1790792Sgshapiro *    notice, this list of conditions and the following disclaimer in the
1890792Sgshapiro *    documentation and/or other materials provided with the distribution.
1990792Sgshapiro * 3. All advertising materials mentioning features or use of this software
2090792Sgshapiro *    must display the following acknowledgement:
2190792Sgshapiro *	This product includes software developed by the University of
2290792Sgshapiro *	California, Berkeley and its contributors.
2390792Sgshapiro * 4. Neither the name of the University nor the names of its contributors
2490792Sgshapiro *    may be used to endorse or promote products derived from this software
2590792Sgshapiro *    without specific prior written permission.
2690792Sgshapiro *
2790792Sgshapiro * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2890792Sgshapiro * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2990792Sgshapiro * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3090792Sgshapiro * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3190792Sgshapiro * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3290792Sgshapiro * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3390792Sgshapiro * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3490792Sgshapiro * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3590792Sgshapiro * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3690792Sgshapiro * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3790792Sgshapiro * SUCH DAMAGE.
3890792Sgshapiro *
3990792Sgshapiro *	@(#)kern_sysctl.c	8.4 (Berkeley) 4/14/94
4090792Sgshapiro */
4190792Sgshapiro
4290792Sgshapiro#include <sys/cdefs.h>
4390792Sgshapiro__FBSDID("$FreeBSD: head/sys/kern/kern_sysctl.c 127050 2004-03-16 01:28:45Z truckman $");
4490792Sgshapiro
4590792Sgshapiro#include "opt_compat.h"
4690792Sgshapiro#include "opt_mac.h"
4790792Sgshapiro
4890792Sgshapiro#include <sys/param.h>
4990792Sgshapiro#include <sys/systm.h>
5094334Sgshapiro#include <sys/kernel.h>
5190792Sgshapiro#include <sys/sysctl.h>
5290792Sgshapiro#include <sys/mac.h>
5390792Sgshapiro#include <sys/malloc.h>
5490792Sgshapiro#include <sys/proc.h>
5590792Sgshapiro#include <sys/lock.h>
5690792Sgshapiro#include <sys/mutex.h>
5790792Sgshapiro#include <sys/sx.h>
5890792Sgshapiro#include <sys/sysproto.h>
5990792Sgshapiro#include <vm/vm.h>
6090792Sgshapiro#include <vm/vm_extern.h>
6190792Sgshapiro
6290792Sgshapirostatic MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
6390792Sgshapirostatic MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
6490792Sgshapirostatic MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
6590792Sgshapiro
6690792Sgshapiro/*
6790792Sgshapiro * Locking - this locks the sysctl tree in memory.
6890792Sgshapiro */
6990792Sgshapirostatic struct sx sysctllock;
7090792Sgshapiro
7190792Sgshapiro#define	SYSCTL_LOCK()		sx_xlock(&sysctllock)
7290792Sgshapiro#define	SYSCTL_UNLOCK()		sx_xunlock(&sysctllock)
7390792Sgshapiro#define	SYSCTL_INIT()		sx_init(&sysctllock, "sysctl lock")
7490792Sgshapiro
7590792Sgshapirostatic int sysctl_root(SYSCTL_HANDLER_ARGS);
7690792Sgshapiro
7790792Sgshapirostruct sysctl_oid_list sysctl__children; /* root list */
7890792Sgshapiro
7990792Sgshapirostatic struct sysctl_oid *
8090792Sgshapirosysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
8190792Sgshapiro{
8290792Sgshapiro	struct sysctl_oid *oidp;
8390792Sgshapiro
8490792Sgshapiro	SLIST_FOREACH(oidp, list, oid_link) {
8590792Sgshapiro		if (strcmp(oidp->oid_name, name) == 0) {
8690792Sgshapiro			return (oidp);
8790792Sgshapiro		}
8890792Sgshapiro	}
8990792Sgshapiro	return (NULL);
9090792Sgshapiro}
9190792Sgshapiro
9290792Sgshapiro/*
9390792Sgshapiro * Initialization of the MIB tree.
9490792Sgshapiro *
9590792Sgshapiro * Order by number in each list.
9690792Sgshapiro */
9790792Sgshapiro
9890792Sgshapirovoid
9990792Sgshapirosysctl_register_oid(struct sysctl_oid *oidp)
10090792Sgshapiro{
10190792Sgshapiro	struct sysctl_oid_list *parent = oidp->oid_parent;
10290792Sgshapiro	struct sysctl_oid *p;
10390792Sgshapiro	struct sysctl_oid *q;
10490792Sgshapiro
10590792Sgshapiro	/*
10690792Sgshapiro	 * First check if another oid with the same name already
10790792Sgshapiro	 * exists in the parent's list.
10890792Sgshapiro	 */
10990792Sgshapiro	p = sysctl_find_oidname(oidp->oid_name, parent);
11090792Sgshapiro	if (p != NULL) {
11190792Sgshapiro		if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
11290792Sgshapiro			p->oid_refcnt++;
11390792Sgshapiro			return;
11490792Sgshapiro		} else {
11590792Sgshapiro			printf("can't re-use a leaf (%s)!\n", p->oid_name);
11690792Sgshapiro			return;
11790792Sgshapiro		}
11890792Sgshapiro	}
11990792Sgshapiro	/*
12090792Sgshapiro	 * If this oid has a number OID_AUTO, give it a number which
12190792Sgshapiro	 * is greater than any current oid.
12294334Sgshapiro	 * NOTE: DO NOT change the starting value here, change it in
12390792Sgshapiro	 * <sys/sysctl.h>, and make sure it is at least 256 to
12494334Sgshapiro	 * accomodate e.g. net.inet.raw as a static sysctl node.
12590792Sgshapiro	 */
12690792Sgshapiro	if (oidp->oid_number == OID_AUTO) {
12790792Sgshapiro		static int newoid = CTL_AUTO_START;
12890792Sgshapiro
12990792Sgshapiro		oidp->oid_number = newoid++;
13090792Sgshapiro		if (newoid == 0x7fffffff)
13190792Sgshapiro			panic("out of oids");
13290792Sgshapiro	}
13390792Sgshapiro#if 0
13490792Sgshapiro	else if (oidp->oid_number >= CTL_AUTO_START) {
13590792Sgshapiro		/* do not panic; this happens when unregistering sysctl sets */
13690792Sgshapiro		printf("static sysctl oid too high: %d", oidp->oid_number);
13790792Sgshapiro	}
13890792Sgshapiro#endif
13990792Sgshapiro
14090792Sgshapiro	/*
14190792Sgshapiro	 * Insert the oid into the parent's list in order.
14290792Sgshapiro	 */
14390792Sgshapiro	q = NULL;
14490792Sgshapiro	SLIST_FOREACH(p, parent, oid_link) {
14590792Sgshapiro		if (oidp->oid_number < p->oid_number)
14690792Sgshapiro			break;
14790792Sgshapiro		q = p;
14890792Sgshapiro	}
14990792Sgshapiro	if (q)
15090792Sgshapiro		SLIST_INSERT_AFTER(q, oidp, oid_link);
15190792Sgshapiro	else
15290792Sgshapiro		SLIST_INSERT_HEAD(parent, oidp, oid_link);
15390792Sgshapiro}
15490792Sgshapiro
15590792Sgshapirovoid
15690792Sgshapirosysctl_unregister_oid(struct sysctl_oid *oidp)
15790792Sgshapiro{
15890792Sgshapiro	struct sysctl_oid *p;
15990792Sgshapiro	int error;
16090792Sgshapiro
16190792Sgshapiro	error = ENOENT;
16290792Sgshapiro	if (oidp->oid_number == OID_AUTO) {
16390792Sgshapiro		error = EINVAL;
16490792Sgshapiro	} else {
16590792Sgshapiro		SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
16690792Sgshapiro			if (p == oidp) {
16790792Sgshapiro				SLIST_REMOVE(oidp->oid_parent, oidp,
16890792Sgshapiro				    sysctl_oid, oid_link);
16990792Sgshapiro				error = 0;
17090792Sgshapiro				break;
17190792Sgshapiro			}
17290792Sgshapiro		}
17390792Sgshapiro	}
17490792Sgshapiro
17590792Sgshapiro	/*
17690792Sgshapiro	 * This can happen when a module fails to register and is
17790792Sgshapiro	 * being unloaded afterwards.  It should not be a panic()
17890792Sgshapiro	 * for normal use.
17990792Sgshapiro	 */
18090792Sgshapiro	if (error)
18190792Sgshapiro		printf("%s: failed to unregister sysctl\n", __func__);
18290792Sgshapiro}
18390792Sgshapiro
18490792Sgshapiro/* Initialize a new context to keep track of dynamically added sysctls. */
18590792Sgshapiroint
18690792Sgshapirosysctl_ctx_init(struct sysctl_ctx_list *c)
18790792Sgshapiro{
18890792Sgshapiro
18990792Sgshapiro	if (c == NULL) {
19090792Sgshapiro		return (EINVAL);
19190792Sgshapiro	}
19290792Sgshapiro	TAILQ_INIT(c);
19390792Sgshapiro	return (0);
19490792Sgshapiro}
19590792Sgshapiro
19690792Sgshapiro/* Free the context, and destroy all dynamic oids registered in this context */
19790792Sgshapiroint
19890792Sgshapirosysctl_ctx_free(struct sysctl_ctx_list *clist)
19990792Sgshapiro{
20090792Sgshapiro	struct sysctl_ctx_entry *e, *e1;
20190792Sgshapiro	int error;
20290792Sgshapiro
20390792Sgshapiro	error = 0;
20490792Sgshapiro	/*
20590792Sgshapiro	 * First perform a "dry run" to check if it's ok to remove oids.
20690792Sgshapiro	 * XXX FIXME
20790792Sgshapiro	 * XXX This algorithm is a hack. But I don't know any
20890792Sgshapiro	 * XXX better solution for now...
20990792Sgshapiro	 */
21090792Sgshapiro	TAILQ_FOREACH(e, clist, link) {
21190792Sgshapiro		error = sysctl_remove_oid(e->entry, 0, 0);
21290792Sgshapiro		if (error)
21390792Sgshapiro			break;
21490792Sgshapiro	}
21590792Sgshapiro	/*
21690792Sgshapiro	 * Restore deregistered entries, either from the end,
21790792Sgshapiro	 * or from the place where error occured.
21890792Sgshapiro	 * e contains the entry that was not unregistered
21990792Sgshapiro	 */
22090792Sgshapiro	if (error)
22190792Sgshapiro		e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
22290792Sgshapiro	else
22390792Sgshapiro		e1 = TAILQ_LAST(clist, sysctl_ctx_list);
22490792Sgshapiro	while (e1 != NULL) {
22590792Sgshapiro		sysctl_register_oid(e1->entry);
22690792Sgshapiro		e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
22790792Sgshapiro	}
22890792Sgshapiro	if (error)
22990792Sgshapiro		return(EBUSY);
23090792Sgshapiro	/* Now really delete the entries */
23190792Sgshapiro	e = TAILQ_FIRST(clist);
23290792Sgshapiro	while (e != NULL) {
23390792Sgshapiro		e1 = TAILQ_NEXT(e, link);
23490792Sgshapiro		error = sysctl_remove_oid(e->entry, 1, 0);
23590792Sgshapiro		if (error)
23690792Sgshapiro			panic("sysctl_remove_oid: corrupt tree, entry: %s",
23790792Sgshapiro			    e->entry->oid_name);
23890792Sgshapiro		free(e, M_SYSCTLOID);
23990792Sgshapiro		e = e1;
24090792Sgshapiro	}
24190792Sgshapiro	return (error);
24290792Sgshapiro}
24390792Sgshapiro
24490792Sgshapiro/* Add an entry to the context */
24590792Sgshapirostruct sysctl_ctx_entry *
24690792Sgshapirosysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
24790792Sgshapiro{
24890792Sgshapiro	struct sysctl_ctx_entry *e;
24990792Sgshapiro
25090792Sgshapiro	if (clist == NULL || oidp == NULL)
25190792Sgshapiro		return(NULL);
25290792Sgshapiro	e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
25390792Sgshapiro	e->entry = oidp;
25490792Sgshapiro	TAILQ_INSERT_HEAD(clist, e, link);
25590792Sgshapiro	return (e);
25690792Sgshapiro}
25790792Sgshapiro
25890792Sgshapiro/* Find an entry in the context */
25990792Sgshapirostruct sysctl_ctx_entry *
26090792Sgshapirosysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
26190792Sgshapiro{
26290792Sgshapiro	struct sysctl_ctx_entry *e;
26390792Sgshapiro
26490792Sgshapiro	if (clist == NULL || oidp == NULL)
26590792Sgshapiro		return(NULL);
26690792Sgshapiro	TAILQ_FOREACH(e, clist, link) {
26790792Sgshapiro		if(e->entry == oidp)
26890792Sgshapiro			return(e);
26990792Sgshapiro	}
27090792Sgshapiro	return (e);
27190792Sgshapiro}
27290792Sgshapiro
27390792Sgshapiro/*
27490792Sgshapiro * Delete an entry from the context.
27590792Sgshapiro * NOTE: this function doesn't free oidp! You have to remove it
27690792Sgshapiro * with sysctl_remove_oid().
27790792Sgshapiro */
27890792Sgshapiroint
27990792Sgshapirosysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
28090792Sgshapiro{
28190792Sgshapiro	struct sysctl_ctx_entry *e;
28290792Sgshapiro
28390792Sgshapiro	if (clist == NULL || oidp == NULL)
28490792Sgshapiro		return (EINVAL);
28590792Sgshapiro	e = sysctl_ctx_entry_find(clist, oidp);
28690792Sgshapiro	if (e != NULL) {
28790792Sgshapiro		TAILQ_REMOVE(clist, e, link);
28890792Sgshapiro		free(e, M_SYSCTLOID);
28990792Sgshapiro		return (0);
29090792Sgshapiro	} else
29190792Sgshapiro		return (ENOENT);
29290792Sgshapiro}
29390792Sgshapiro
29490792Sgshapiro/*
29590792Sgshapiro * Remove dynamically created sysctl trees.
29690792Sgshapiro * oidp - top of the tree to be removed
29790792Sgshapiro * del - if 0 - just deregister, otherwise free up entries as well
29890792Sgshapiro * recurse - if != 0 traverse the subtree to be deleted
29990792Sgshapiro */
30090792Sgshapiroint
30190792Sgshapirosysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
30290792Sgshapiro{
30390792Sgshapiro	struct sysctl_oid *p;
30490792Sgshapiro	int error;
30590792Sgshapiro
30690792Sgshapiro	if (oidp == NULL)
30790792Sgshapiro		return(EINVAL);
30890792Sgshapiro	if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
30990792Sgshapiro		printf("can't remove non-dynamic nodes!\n");
31090792Sgshapiro		return (EINVAL);
31190792Sgshapiro	}
31290792Sgshapiro	/*
31390792Sgshapiro	 * WARNING: normal method to do this should be through
31490792Sgshapiro	 * sysctl_ctx_free(). Use recursing as the last resort
31590792Sgshapiro	 * method to purge your sysctl tree of leftovers...
31690792Sgshapiro	 * However, if some other code still references these nodes,
31790792Sgshapiro	 * it will panic.
31890792Sgshapiro	 */
31990792Sgshapiro	if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
32090792Sgshapiro		if (oidp->oid_refcnt == 1) {
32190792Sgshapiro			SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
32290792Sgshapiro				if (!recurse)
32390792Sgshapiro					return (ENOTEMPTY);
32490792Sgshapiro				error = sysctl_remove_oid(p, del, recurse);
32590792Sgshapiro				if (error)
32690792Sgshapiro					return (error);
32790792Sgshapiro			}
32890792Sgshapiro			if (del)
32990792Sgshapiro				free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
33090792Sgshapiro		}
33190792Sgshapiro	}
33290792Sgshapiro	if (oidp->oid_refcnt > 1 ) {
33390792Sgshapiro		oidp->oid_refcnt--;
33490792Sgshapiro	} else {
33590792Sgshapiro		if (oidp->oid_refcnt == 0) {
33690792Sgshapiro			printf("Warning: bad oid_refcnt=%u (%s)!\n",
33790792Sgshapiro				oidp->oid_refcnt, oidp->oid_name);
33890792Sgshapiro			return (EINVAL);
33990792Sgshapiro		}
34090792Sgshapiro		sysctl_unregister_oid(oidp);
34190792Sgshapiro		if (del) {
34290792Sgshapiro			if (oidp->descr)
34390792Sgshapiro				free((void *)(uintptr_t)(const void *)oidp->descr, M_SYSCTLOID);
34490792Sgshapiro			free((void *)(uintptr_t)(const void *)oidp->oid_name,
34590792Sgshapiro			     M_SYSCTLOID);
34690792Sgshapiro			free(oidp, M_SYSCTLOID);
34790792Sgshapiro		}
34890792Sgshapiro	}
34990792Sgshapiro	return (0);
35090792Sgshapiro}
35190792Sgshapiro
35290792Sgshapiro/*
35390792Sgshapiro * Create new sysctls at run time.
35490792Sgshapiro * clist may point to a valid context initialized with sysctl_ctx_init().
35590792Sgshapiro */
35690792Sgshapirostruct sysctl_oid *
35790792Sgshapirosysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
35890792Sgshapiro	int number, const char *name, int kind, void *arg1, int arg2,
35990792Sgshapiro	int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
36090792Sgshapiro{
36190792Sgshapiro	struct sysctl_oid *oidp;
36290792Sgshapiro	ssize_t len;
36390792Sgshapiro	char *newname;
36490792Sgshapiro
36590792Sgshapiro	/* You have to hook up somewhere.. */
36690792Sgshapiro	if (parent == NULL)
36790792Sgshapiro		return(NULL);
368168515Sgshapiro	/* Check if the node already exists, otherwise create it */
369168515Sgshapiro	oidp = sysctl_find_oidname(name, parent);
370168515Sgshapiro	if (oidp != NULL) {
371168515Sgshapiro		if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
372168515Sgshapiro			oidp->oid_refcnt++;
37390792Sgshapiro			/* Update the context */
37490792Sgshapiro			if (clist != NULL)
37590792Sgshapiro				sysctl_ctx_entry_add(clist, oidp);
37690792Sgshapiro			return (oidp);
37790792Sgshapiro		} else {
37890792Sgshapiro			printf("can't re-use a leaf (%s)!\n", name);
37990792Sgshapiro			return (NULL);
38090792Sgshapiro		}
38190792Sgshapiro	}
38290792Sgshapiro	oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
38390792Sgshapiro	oidp->oid_parent = parent;
38490792Sgshapiro	SLIST_NEXT(oidp, oid_link) = NULL;
38590792Sgshapiro	oidp->oid_number = number;
38690792Sgshapiro	oidp->oid_refcnt = 1;
38790792Sgshapiro	len = strlen(name);
38890792Sgshapiro	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
38990792Sgshapiro	bcopy(name, newname, len + 1);
39090792Sgshapiro	newname[len] = '\0';
39190792Sgshapiro	oidp->oid_name = newname;
39290792Sgshapiro	oidp->oid_handler = handler;
39390792Sgshapiro	oidp->oid_kind = CTLFLAG_DYN | kind;
39490792Sgshapiro	if ((kind & CTLTYPE) == CTLTYPE_NODE) {
39590792Sgshapiro		/* Allocate space for children */
39690792Sgshapiro		SYSCTL_CHILDREN(oidp) = malloc(sizeof(struct sysctl_oid_list),
39790792Sgshapiro		    M_SYSCTLOID, M_WAITOK);
39890792Sgshapiro		SLIST_INIT(SYSCTL_CHILDREN(oidp));
39990792Sgshapiro	} else {
40090792Sgshapiro		oidp->oid_arg1 = arg1;
40190792Sgshapiro		oidp->oid_arg2 = arg2;
40290792Sgshapiro	}
40390792Sgshapiro	oidp->oid_fmt = fmt;
40490792Sgshapiro	if (descr) {
40590792Sgshapiro		int len = strlen(descr) + 1;
40690792Sgshapiro		oidp->descr = malloc(len, M_SYSCTLOID, M_WAITOK);
40790792Sgshapiro		if (oidp->descr)
40890792Sgshapiro			strcpy((char *)(uintptr_t)(const void *)oidp->descr, descr);
40990792Sgshapiro	}
41090792Sgshapiro	/* Update the context, if used */
41190792Sgshapiro	if (clist != NULL)
41290792Sgshapiro		sysctl_ctx_entry_add(clist, oidp);
41390792Sgshapiro	/* Register this oid */
41490792Sgshapiro	sysctl_register_oid(oidp);
41590792Sgshapiro	return (oidp);
41690792Sgshapiro}
41790792Sgshapiro
41890792Sgshapiro/*
41990792Sgshapiro * Reparent an existing oid.
42090792Sgshapiro */
42190792Sgshapiroint
42290792Sgshapirosysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
42390792Sgshapiro{
42490792Sgshapiro	struct sysctl_oid *oidp;
42590792Sgshapiro
42690792Sgshapiro	if (oid->oid_parent == parent)
42790792Sgshapiro		return (0);
42890792Sgshapiro	oidp = sysctl_find_oidname(oid->oid_name, parent);
42990792Sgshapiro	if (oidp != NULL)
43090792Sgshapiro		return (EEXIST);
431	sysctl_unregister_oid(oid);
432	oid->oid_parent = parent;
433	oid->oid_number = OID_AUTO;
434	sysctl_register_oid(oid);
435	return (0);
436}
437
438/*
439 * Register the kernel's oids on startup.
440 */
441SET_DECLARE(sysctl_set, struct sysctl_oid);
442
443static void
444sysctl_register_all(void *arg)
445{
446	struct sysctl_oid **oidp;
447
448	SYSCTL_INIT();
449	SET_FOREACH(oidp, sysctl_set)
450		sysctl_register_oid(*oidp);
451}
452SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
453
454/*
455 * "Staff-functions"
456 *
457 * These functions implement a presently undocumented interface
458 * used by the sysctl program to walk the tree, and get the type
459 * so it can print the value.
460 * This interface is under work and consideration, and should probably
461 * be killed with a big axe by the first person who can find the time.
462 * (be aware though, that the proper interface isn't as obvious as it
463 * may seem, there are various conflicting requirements.
464 *
465 * {0,0}	printf the entire MIB-tree.
466 * {0,1,...}	return the name of the "..." OID.
467 * {0,2,...}	return the next OID.
468 * {0,3}	return the OID of the name in "new"
469 * {0,4,...}	return the kind & format info for the "..." OID.
470 * {0,5,...}	return the description the "..." OID.
471 */
472
473static void
474sysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
475{
476	int k;
477	struct sysctl_oid *oidp;
478
479	SLIST_FOREACH(oidp, l, oid_link) {
480
481		for (k=0; k<i; k++)
482			printf(" ");
483
484		printf("%d %s ", oidp->oid_number, oidp->oid_name);
485
486		printf("%c%c",
487			oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
488			oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
489
490		if (oidp->oid_handler)
491			printf(" *Handler");
492
493		switch (oidp->oid_kind & CTLTYPE) {
494			case CTLTYPE_NODE:
495				printf(" Node\n");
496				if (!oidp->oid_handler) {
497					sysctl_sysctl_debug_dump_node(
498						oidp->oid_arg1, i+2);
499				}
500				break;
501			case CTLTYPE_INT:    printf(" Int\n"); break;
502			case CTLTYPE_STRING: printf(" String\n"); break;
503			case CTLTYPE_QUAD:   printf(" Quad\n"); break;
504			case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
505			default:	     printf("\n");
506		}
507
508	}
509}
510
511static int
512sysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
513{
514	int error;
515
516	error = suser(req->td);
517	if (error)
518		return error;
519	sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
520	return ENOENT;
521}
522
523SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
524	0, 0, sysctl_sysctl_debug, "-", "");
525
526static int
527sysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
528{
529	int *name = (int *) arg1;
530	u_int namelen = arg2;
531	int error = 0;
532	struct sysctl_oid *oid;
533	struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
534	char buf[10];
535
536	while (namelen) {
537		if (!lsp) {
538			snprintf(buf,sizeof(buf),"%d",*name);
539			if (req->oldidx)
540				error = SYSCTL_OUT(req, ".", 1);
541			if (!error)
542				error = SYSCTL_OUT(req, buf, strlen(buf));
543			if (error)
544				return (error);
545			namelen--;
546			name++;
547			continue;
548		}
549		lsp2 = 0;
550		SLIST_FOREACH(oid, lsp, oid_link) {
551			if (oid->oid_number != *name)
552				continue;
553
554			if (req->oldidx)
555				error = SYSCTL_OUT(req, ".", 1);
556			if (!error)
557				error = SYSCTL_OUT(req, oid->oid_name,
558					strlen(oid->oid_name));
559			if (error)
560				return (error);
561
562			namelen--;
563			name++;
564
565			if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
566				break;
567
568			if (oid->oid_handler)
569				break;
570
571			lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
572			break;
573		}
574		lsp = lsp2;
575	}
576	return (SYSCTL_OUT(req, "", 1));
577}
578
579SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
580
581static int
582sysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
583	int *next, int *len, int level, struct sysctl_oid **oidpp)
584{
585	struct sysctl_oid *oidp;
586
587	*len = level;
588	SLIST_FOREACH(oidp, lsp, oid_link) {
589		*next = oidp->oid_number;
590		*oidpp = oidp;
591
592		if (oidp->oid_kind & CTLFLAG_SKIP)
593			continue;
594
595		if (!namelen) {
596			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
597				return 0;
598			if (oidp->oid_handler)
599				/* We really should call the handler here...*/
600				return 0;
601			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
602			if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
603				len, level+1, oidpp))
604				return 0;
605			goto emptynode;
606		}
607
608		if (oidp->oid_number < *name)
609			continue;
610
611		if (oidp->oid_number > *name) {
612			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
613				return 0;
614			if (oidp->oid_handler)
615				return 0;
616			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
617			if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
618				next+1, len, level+1, oidpp))
619				return (0);
620			goto next;
621		}
622		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
623			continue;
624
625		if (oidp->oid_handler)
626			continue;
627
628		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
629		if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
630			len, level+1, oidpp))
631			return (0);
632	next:
633		namelen = 1;
634	emptynode:
635		*len = level;
636	}
637	return 1;
638}
639
640static int
641sysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
642{
643	int *name = (int *) arg1;
644	u_int namelen = arg2;
645	int i, j, error;
646	struct sysctl_oid *oid;
647	struct sysctl_oid_list *lsp = &sysctl__children;
648	int newoid[CTL_MAXNAME];
649
650	i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
651	if (i)
652		return ENOENT;
653	error = SYSCTL_OUT(req, newoid, j * sizeof (int));
654	return (error);
655}
656
657SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
658
659static int
660name2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
661{
662	int i;
663	struct sysctl_oid *oidp;
664	struct sysctl_oid_list *lsp = &sysctl__children;
665	char *p;
666
667	if (!*name)
668		return ENOENT;
669
670	p = name + strlen(name) - 1 ;
671	if (*p == '.')
672		*p = '\0';
673
674	*len = 0;
675
676	for (p = name; *p && *p != '.'; p++)
677		;
678	i = *p;
679	if (i == '.')
680		*p = '\0';
681
682	oidp = SLIST_FIRST(lsp);
683
684	while (oidp && *len < CTL_MAXNAME) {
685		if (strcmp(name, oidp->oid_name)) {
686			oidp = SLIST_NEXT(oidp, oid_link);
687			continue;
688		}
689		*oid++ = oidp->oid_number;
690		(*len)++;
691
692		if (!i) {
693			if (oidpp)
694				*oidpp = oidp;
695			return (0);
696		}
697
698		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
699			break;
700
701		if (oidp->oid_handler)
702			break;
703
704		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
705		oidp = SLIST_FIRST(lsp);
706		name = p+1;
707		for (p = name; *p && *p != '.'; p++)
708				;
709		i = *p;
710		if (i == '.')
711			*p = '\0';
712	}
713	return ENOENT;
714}
715
716static int
717sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
718{
719	char *p;
720	int error, oid[CTL_MAXNAME], len;
721	struct sysctl_oid *op = 0;
722
723	if (!req->newlen)
724		return ENOENT;
725	if (req->newlen >= MAXPATHLEN)	/* XXX arbitrary, undocumented */
726		return (ENAMETOOLONG);
727
728	p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
729
730	error = SYSCTL_IN(req, p, req->newlen);
731	if (error) {
732		free(p, M_SYSCTL);
733		return (error);
734	}
735
736	p [req->newlen] = '\0';
737
738	error = name2oid(p, oid, &len, &op);
739
740	free(p, M_SYSCTL);
741
742	if (error)
743		return (error);
744
745	error = SYSCTL_OUT(req, oid, len * sizeof *oid);
746	return (error);
747}
748
749SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
750	sysctl_sysctl_name2oid, "I", "");
751
752static int
753sysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
754{
755	struct sysctl_oid *oid;
756	int error;
757
758	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
759	if (error)
760		return (error);
761
762	if (!oid->oid_fmt)
763		return (ENOENT);
764	error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
765	if (error)
766		return (error);
767	error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
768	return (error);
769}
770
771
772SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
773
774static int
775sysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
776{
777	struct sysctl_oid *oid;
778	int error;
779
780	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
781	if (error)
782		return (error);
783
784	if (!oid->descr)
785		return (ENOENT);
786	error = SYSCTL_OUT(req, oid->descr, strlen(oid->descr) + 1);
787	return (error);
788}
789
790SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
791
792/*
793 * Default "handler" functions.
794 */
795
796/*
797 * Handle an int, signed or unsigned.
798 * Two cases:
799 *     a variable:  point arg1 at it.
800 *     a constant:  pass it in arg2.
801 */
802
803int
804sysctl_handle_int(SYSCTL_HANDLER_ARGS)
805{
806	int tmpout, error = 0;
807
808	/*
809	 * Attempt to get a coherent snapshot by making a copy of the data.
810	 */
811	if (arg1)
812		tmpout = *(int *)arg1;
813	else
814		tmpout = arg2;
815	error = SYSCTL_OUT(req, &tmpout, sizeof(int));
816
817	if (error || !req->newptr)
818		return (error);
819
820	if (!arg1)
821		error = EPERM;
822	else
823		error = SYSCTL_IN(req, arg1, sizeof(int));
824	return (error);
825}
826
827/*
828 * Handle a long, signed or unsigned.  arg1 points to it.
829 */
830
831int
832sysctl_handle_long(SYSCTL_HANDLER_ARGS)
833{
834	int error = 0;
835	long tmpout;
836
837	/*
838	 * Attempt to get a coherent snapshot by making a copy of the data.
839	 */
840	if (!arg1)
841		return (EINVAL);
842	tmpout = *(long *)arg1;
843	error = SYSCTL_OUT(req, &tmpout, sizeof(long));
844
845	if (error || !req->newptr)
846		return (error);
847
848	error = SYSCTL_IN(req, arg1, sizeof(long));
849	return (error);
850}
851
852/*
853 * Handle our generic '\0' terminated 'C' string.
854 * Two cases:
855 * 	a variable string:  point arg1 at it, arg2 is max length.
856 * 	a constant string:  point arg1 at it, arg2 is zero.
857 */
858
859int
860sysctl_handle_string(SYSCTL_HANDLER_ARGS)
861{
862	int error=0;
863	char *tmparg;
864	size_t outlen;
865
866	/*
867	 * Attempt to get a coherent snapshot by copying to a
868	 * temporary kernel buffer.
869	 */
870retry:
871	outlen = strlen((char *)arg1)+1;
872	tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
873
874	if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
875		free(tmparg, M_SYSCTLTMP);
876		goto retry;
877	}
878
879	error = SYSCTL_OUT(req, tmparg, outlen);
880	free(tmparg, M_SYSCTLTMP);
881
882	if (error || !req->newptr)
883		return (error);
884
885	if ((req->newlen - req->newidx) >= arg2) {
886		error = EINVAL;
887	} else {
888		arg2 = (req->newlen - req->newidx);
889		error = SYSCTL_IN(req, arg1, arg2);
890		((char *)arg1)[arg2] = '\0';
891	}
892
893	return (error);
894}
895
896/*
897 * Handle any kind of opaque data.
898 * arg1 points to it, arg2 is the size.
899 */
900
901int
902sysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
903{
904	int error, tries;
905	u_int generation;
906	struct sysctl_req req2;
907
908	/*
909	 * Attempt to get a coherent snapshot, by using the thread
910	 * pre-emption counter updated from within mi_switch() to
911	 * determine if we were pre-empted during a bcopy() or
912	 * copyout(). Make 3 attempts at doing this before giving up.
913	 * If we encounter an error, stop immediately.
914	 */
915	tries = 0;
916	req2 = *req;
917retry:
918	generation = curthread->td_generation;
919	error = SYSCTL_OUT(req, arg1, arg2);
920	if (error)
921		return (error);
922	tries++;
923	if (generation != curthread->td_generation && tries < 3) {
924		*req = req2;
925		goto retry;
926	}
927
928	error = SYSCTL_IN(req, arg1, arg2);
929
930	return (error);
931}
932
933/*
934 * Transfer functions to/from kernel space.
935 * XXX: rather untested at this point
936 */
937static int
938sysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
939{
940	size_t i = 0;
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			bcopy(p, (char *)req->oldptr + req->oldidx, i);
951	}
952	req->oldidx += l;
953	if (req->oldptr && i != l)
954		return (ENOMEM);
955	return (0);
956}
957
958static int
959sysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
960{
961	if (!req->newptr)
962		return 0;
963	if (req->newlen - req->newidx < l)
964		return (EINVAL);
965	bcopy((char *)req->newptr + req->newidx, p, l);
966	req->newidx += l;
967	return (0);
968}
969
970int
971kernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
972    size_t *oldlenp, void *new, size_t newlen, size_t *retval)
973{
974	int error = 0;
975	struct sysctl_req req;
976
977	bzero(&req, sizeof req);
978
979	req.td = td;
980
981	if (oldlenp) {
982		req.oldlen = *oldlenp;
983	}
984
985	if (old) {
986		req.oldptr= old;
987	}
988
989	if (new != NULL) {
990		req.newlen = newlen;
991		req.newptr = new;
992	}
993
994	req.oldfunc = sysctl_old_kernel;
995	req.newfunc = sysctl_new_kernel;
996	req.lock = REQ_LOCKED;
997
998	SYSCTL_LOCK();
999
1000	error = sysctl_root(0, name, namelen, &req);
1001
1002	if (req.lock == REQ_WIRED && req.wiredlen > 0)
1003		vsunlock(req.oldptr, req.wiredlen);
1004
1005	SYSCTL_UNLOCK();
1006
1007	if (error && error != ENOMEM)
1008		return (error);
1009
1010	if (retval) {
1011		if (req.oldptr && req.oldidx > req.oldlen)
1012			*retval = req.oldlen;
1013		else
1014			*retval = req.oldidx;
1015	}
1016	return (error);
1017}
1018
1019int
1020kernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1021    void *new, size_t newlen, size_t *retval)
1022{
1023        int oid[CTL_MAXNAME];
1024        size_t oidlen, plen;
1025	int error;
1026
1027	oid[0] = 0;		/* sysctl internal magic */
1028	oid[1] = 3;		/* name2oid */
1029	oidlen = sizeof(oid);
1030
1031	error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1032	    (void *)name, strlen(name), &plen);
1033	if (error)
1034		return (error);
1035
1036	error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1037	    new, newlen, retval);
1038	return (error);
1039}
1040
1041/*
1042 * Transfer function to/from user space.
1043 */
1044static int
1045sysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
1046{
1047	int error = 0;
1048	size_t i, len, origidx;
1049
1050	origidx = req->oldidx;
1051	req->oldidx += l;
1052	if (req->oldptr == NULL)
1053		return (0);
1054	if (req->lock == REQ_LOCKED)
1055		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1056		    "sysctl_old_user()");
1057	i = l;
1058	len = (req->lock == REQ_WIRED) ? req->wiredlen : req->oldlen;
1059	if (len <= origidx)
1060		i = 0;
1061	else {
1062		if (i > len - origidx)
1063			i = len - origidx;
1064		error = copyout(p, (char *)req->oldptr + origidx, i);
1065	}
1066	if (error)
1067		return (error);
1068	if (i < l)
1069		return (ENOMEM);
1070	return (0);
1071}
1072
1073static int
1074sysctl_new_user(struct sysctl_req *req, void *p, size_t l)
1075{
1076	int error;
1077
1078	if (!req->newptr)
1079		return 0;
1080	if (req->newlen - req->newidx < l)
1081		return (EINVAL);
1082	error = copyin((char *)req->newptr + req->newidx, p, l);
1083	req->newidx += l;
1084	return (error);
1085}
1086
1087/*
1088 * Wire the user space destination buffer.  If set to a value greater than
1089 * zero, the len parameter limits the maximum amount of wired memory.
1090 *
1091 * XXX - The len parameter is currently ignored due to the lack of
1092 * a place to save it in the sysctl_req structure so that the matching
1093 * amount of memory can be unwired in the sysctl exit code.
1094 */
1095int
1096sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1097{
1098	int ret;
1099	size_t wiredlen;
1100
1101	wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1102	ret = 0;
1103	if (req->lock == REQ_LOCKED && req->oldptr &&
1104	    req->oldfunc == sysctl_old_user) {
1105		if (wiredlen != 0) {
1106			ret = vslock(req->oldptr, wiredlen);
1107			if (ret != 0 && ret != ENOMEM)
1108				return (ret);
1109		}
1110		req->lock = REQ_WIRED;
1111		req->wiredlen = wiredlen;
1112	}
1113	return (0);
1114}
1115
1116int
1117sysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
1118    int *nindx, struct sysctl_req *req)
1119{
1120	struct sysctl_oid *oid;
1121	int indx;
1122
1123	oid = SLIST_FIRST(&sysctl__children);
1124	indx = 0;
1125	while (oid && indx < CTL_MAXNAME) {
1126		if (oid->oid_number == name[indx]) {
1127			indx++;
1128			if (oid->oid_kind & CTLFLAG_NOLOCK)
1129				req->lock = REQ_UNLOCKED;
1130			if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1131				if (oid->oid_handler != NULL ||
1132				    indx == namelen) {
1133					*noid = oid;
1134					if (nindx != NULL)
1135						*nindx = indx;
1136					return (0);
1137				}
1138				oid = SLIST_FIRST(
1139				    (struct sysctl_oid_list *)oid->oid_arg1);
1140			} else if (indx == namelen) {
1141				*noid = oid;
1142				if (nindx != NULL)
1143					*nindx = indx;
1144				return (0);
1145			} else {
1146				return (ENOTDIR);
1147			}
1148		} else {
1149			oid = SLIST_NEXT(oid, oid_link);
1150		}
1151	}
1152	return (ENOENT);
1153}
1154
1155/*
1156 * Traverse our tree, and find the right node, execute whatever it points
1157 * to, and return the resulting error code.
1158 */
1159
1160static int
1161sysctl_root(SYSCTL_HANDLER_ARGS)
1162{
1163	struct sysctl_oid *oid;
1164	int error, indx, lvl;
1165
1166	error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
1167	if (error)
1168		return (error);
1169
1170	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1171		/*
1172		 * You can't call a sysctl when it's a node, but has
1173		 * no handler.  Inform the user that it's a node.
1174		 * The indx may or may not be the same as namelen.
1175		 */
1176		if (oid->oid_handler == NULL)
1177			return (EISDIR);
1178	}
1179
1180	/* Is this sysctl writable? */
1181	if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
1182		return (EPERM);
1183
1184	KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
1185
1186	/* Is this sysctl sensitive to securelevels? */
1187	if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1188		lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1189		error = securelevel_gt(req->td->td_ucred, lvl);
1190		if (error)
1191			return (error);
1192	}
1193
1194	/* Is this sysctl writable by only privileged users? */
1195	if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
1196		int flags;
1197
1198		if (oid->oid_kind & CTLFLAG_PRISON)
1199			flags = PRISON_ROOT;
1200		else
1201			flags = 0;
1202		error = suser_cred(req->td->td_ucred, flags);
1203		if (error)
1204			return (error);
1205	}
1206
1207	if (!oid->oid_handler)
1208		return EINVAL;
1209
1210	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1211		(int *)arg1 += indx;
1212		arg2 -= indx;
1213	} else {
1214		arg1 = oid->oid_arg1;
1215		arg2 = oid->oid_arg2;
1216	}
1217#ifdef MAC
1218	error = mac_check_system_sysctl(req->td->td_ucred, oid, arg1, arg2,
1219	    req);
1220	if (error != 0)
1221		return (error);
1222#endif
1223	error = oid->oid_handler(oid, arg1, arg2, req);
1224
1225	return (error);
1226}
1227
1228#ifndef _SYS_SYSPROTO_H_
1229struct sysctl_args {
1230	int	*name;
1231	u_int	namelen;
1232	void	*old;
1233	size_t	*oldlenp;
1234	void	*new;
1235	size_t	newlen;
1236};
1237#endif
1238
1239/*
1240 * MPSAFE
1241 */
1242int
1243__sysctl(struct thread *td, struct sysctl_args *uap)
1244{
1245	int error, name[CTL_MAXNAME];
1246	size_t j;
1247
1248	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
1249		return (EINVAL);
1250
1251 	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
1252 	if (error)
1253		return (error);
1254
1255	mtx_lock(&Giant);
1256
1257	error = userland_sysctl(td, name, uap->namelen,
1258		uap->old, uap->oldlenp, 0,
1259		uap->new, uap->newlen, &j);
1260	if (error && error != ENOMEM)
1261		goto done2;
1262	if (uap->oldlenp) {
1263		int i = copyout(&j, uap->oldlenp, sizeof(j));
1264		if (i)
1265			error = i;
1266	}
1267done2:
1268	mtx_unlock(&Giant);
1269	return (error);
1270}
1271
1272/*
1273 * This is used from various compatibility syscalls too.  That's why name
1274 * must be in kernel space.
1275 */
1276int
1277userland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1278    size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval)
1279{
1280	int error = 0;
1281	struct sysctl_req req, req2;
1282
1283	bzero(&req, sizeof req);
1284
1285	req.td = td;
1286
1287	if (oldlenp) {
1288		if (inkernel) {
1289			req.oldlen = *oldlenp;
1290		} else {
1291			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
1292			if (error)
1293				return (error);
1294		}
1295	}
1296
1297	if (old) {
1298		if (!useracc(old, req.oldlen, VM_PROT_WRITE))
1299			return (EFAULT);
1300		req.oldptr= old;
1301	}
1302
1303	if (new != NULL) {
1304		if (!useracc(new, req.newlen, VM_PROT_READ))
1305			return (EFAULT);
1306		req.newlen = newlen;
1307		req.newptr = new;
1308	}
1309
1310	req.oldfunc = sysctl_old_user;
1311	req.newfunc = sysctl_new_user;
1312	req.lock = REQ_LOCKED;
1313
1314	SYSCTL_LOCK();
1315
1316	do {
1317	    req2 = req;
1318	    error = sysctl_root(0, name, namelen, &req2);
1319	} while (error == EAGAIN);
1320
1321	req = req2;
1322	if (req.lock == REQ_WIRED && req.wiredlen > 0)
1323		vsunlock(req.oldptr, req.wiredlen);
1324
1325	SYSCTL_UNLOCK();
1326
1327	if (error && error != ENOMEM)
1328		return (error);
1329
1330	if (retval) {
1331		if (req.oldptr && req.oldidx > req.oldlen)
1332			*retval = req.oldlen;
1333		else
1334			*retval = req.oldidx;
1335	}
1336	return (error);
1337}
1338
1339#ifdef COMPAT_43
1340#include <sys/socket.h>
1341#include <vm/vm_param.h>
1342
1343#define	KINFO_PROC		(0<<8)
1344#define	KINFO_RT		(1<<8)
1345#define	KINFO_VNODE		(2<<8)
1346#define	KINFO_FILE		(3<<8)
1347#define	KINFO_METER		(4<<8)
1348#define	KINFO_LOADAVG		(5<<8)
1349#define	KINFO_CLOCKRATE		(6<<8)
1350
1351/* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
1352#define	KINFO_BSDI_SYSINFO	(101<<8)
1353
1354/*
1355 * XXX this is bloat, but I hope it's better here than on the potentially
1356 * limited kernel stack...  -Peter
1357 */
1358
1359static struct {
1360	int	bsdi_machine;		/* "i386" on BSD/386 */
1361/*      ^^^ this is an offset to the string, relative to the struct start */
1362	char	*pad0;
1363	long	pad1;
1364	long	pad2;
1365	long	pad3;
1366	u_long	pad4;
1367	u_long	pad5;
1368	u_long	pad6;
1369
1370	int	bsdi_ostype;		/* "BSD/386" on BSD/386 */
1371	int	bsdi_osrelease;		/* "1.1" on BSD/386 */
1372	long	pad7;
1373	long	pad8;
1374	char	*pad9;
1375
1376	long	pad10;
1377	long	pad11;
1378	int	pad12;
1379	long	pad13;
1380	quad_t	pad14;
1381	long	pad15;
1382
1383	struct	timeval pad16;
1384	/* we dont set this, because BSDI's uname used gethostname() instead */
1385	int	bsdi_hostname;		/* hostname on BSD/386 */
1386
1387	/* the actual string data is appended here */
1388
1389} bsdi_si;
1390/*
1391 * this data is appended to the end of the bsdi_si structure during copyout.
1392 * The "char *" offsets are relative to the base of the bsdi_si struct.
1393 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
1394 * should not exceed the length of the buffer here... (or else!! :-)
1395 */
1396static char bsdi_strings[80];	/* It had better be less than this! */
1397
1398#ifndef _SYS_SYSPROTO_H_
1399struct getkerninfo_args {
1400	int	op;
1401	char	*where;
1402	size_t	*size;
1403	int	arg;
1404};
1405#endif
1406
1407/*
1408 * MPSAFE
1409 */
1410int
1411ogetkerninfo(struct thread *td, struct getkerninfo_args *uap)
1412{
1413	int error, name[6];
1414	size_t size;
1415	u_int needed = 0;
1416
1417	mtx_lock(&Giant);
1418
1419	switch (uap->op & 0xff00) {
1420
1421	case KINFO_RT:
1422		name[0] = CTL_NET;
1423		name[1] = PF_ROUTE;
1424		name[2] = 0;
1425		name[3] = (uap->op & 0xff0000) >> 16;
1426		name[4] = uap->op & 0xff;
1427		name[5] = uap->arg;
1428		error = userland_sysctl(td, name, 6, uap->where, uap->size,
1429			0, 0, 0, &size);
1430		break;
1431
1432	case KINFO_VNODE:
1433		name[0] = CTL_KERN;
1434		name[1] = KERN_VNODE;
1435		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1436			0, 0, 0, &size);
1437		break;
1438
1439	case KINFO_PROC:
1440		name[0] = CTL_KERN;
1441		name[1] = KERN_PROC;
1442		name[2] = uap->op & 0xff;
1443		name[3] = uap->arg;
1444		error = userland_sysctl(td, name, 4, uap->where, uap->size,
1445			0, 0, 0, &size);
1446		break;
1447
1448	case KINFO_FILE:
1449		name[0] = CTL_KERN;
1450		name[1] = KERN_FILE;
1451		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1452			0, 0, 0, &size);
1453		break;
1454
1455	case KINFO_METER:
1456		name[0] = CTL_VM;
1457		name[1] = VM_TOTAL;
1458		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1459			0, 0, 0, &size);
1460		break;
1461
1462	case KINFO_LOADAVG:
1463		name[0] = CTL_VM;
1464		name[1] = VM_LOADAVG;
1465		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1466			0, 0, 0, &size);
1467		break;
1468
1469	case KINFO_CLOCKRATE:
1470		name[0] = CTL_KERN;
1471		name[1] = KERN_CLOCKRATE;
1472		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1473			0, 0, 0, &size);
1474		break;
1475
1476	case KINFO_BSDI_SYSINFO: {
1477		/*
1478		 * this is pretty crude, but it's just enough for uname()
1479		 * from BSDI's 1.x libc to work.
1480		 *
1481		 * *size gives the size of the buffer before the call, and
1482		 * the amount of data copied after a successful call.
1483		 * If successful, the return value is the amount of data
1484		 * available, which can be larger than *size.
1485		 *
1486		 * BSDI's 2.x product apparently fails with ENOMEM if *size
1487		 * is too small.
1488		 */
1489
1490		u_int left;
1491		char *s;
1492
1493		bzero((char *)&bsdi_si, sizeof(bsdi_si));
1494		bzero(bsdi_strings, sizeof(bsdi_strings));
1495
1496		s = bsdi_strings;
1497
1498		bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
1499		strcpy(s, ostype);
1500		s += strlen(s) + 1;
1501
1502		bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
1503		strcpy(s, osrelease);
1504		s += strlen(s) + 1;
1505
1506		bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
1507		strcpy(s, machine);
1508		s += strlen(s) + 1;
1509
1510		needed = sizeof(bsdi_si) + (s - bsdi_strings);
1511
1512		if ((uap->where == NULL) || (uap->size == NULL)) {
1513			/* process is asking how much buffer to supply.. */
1514			size = needed;
1515			error = 0;
1516			break;
1517		}
1518
1519		if ((error = copyin(uap->size, &size, sizeof(size))) != 0)
1520			break;
1521
1522		/* if too much buffer supplied, trim it down */
1523		if (size > needed)
1524			size = needed;
1525
1526		/* how much of the buffer is remaining */
1527		left = size;
1528
1529		if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
1530			break;
1531
1532		/* is there any point in continuing? */
1533		if (left > sizeof(bsdi_si)) {
1534			left -= sizeof(bsdi_si);
1535			error = copyout(&bsdi_strings,
1536					uap->where + sizeof(bsdi_si), left);
1537		}
1538		break;
1539	}
1540
1541	default:
1542		error = EOPNOTSUPP;
1543		break;
1544	}
1545	if (error == 0) {
1546		td->td_retval[0] = needed ? needed : size;
1547		if (uap->size) {
1548			error = copyout(&size, uap->size, sizeof(size));
1549		}
1550	}
1551	mtx_unlock(&Giant);
1552	return (error);
1553}
1554#endif /* COMPAT_43 */
1555