kern_sysctl.c revision 172930
11541Srgrimes/*-
21541Srgrimes * Copyright (c) 1982, 1986, 1989, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * This code is derived from software contributed to Berkeley by
61541Srgrimes * Mike Karels at Berkeley Software Design, Inc.
71541Srgrimes *
812623Sphk * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
912623Sphk * project, to make these variables more userfriendly.
1012623Sphk *
111541Srgrimes * Redistribution and use in source and binary forms, with or without
121541Srgrimes * modification, are permitted provided that the following conditions
131541Srgrimes * are met:
141541Srgrimes * 1. Redistributions of source code must retain the above copyright
151541Srgrimes *    notice, this list of conditions and the following disclaimer.
161541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
171541Srgrimes *    notice, this list of conditions and the following disclaimer in the
181541Srgrimes *    documentation and/or other materials provided with the distribution.
191541Srgrimes * 4. Neither the name of the University nor the names of its contributors
201541Srgrimes *    may be used to endorse or promote products derived from this software
211541Srgrimes *    without specific prior written permission.
221541Srgrimes *
231541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
241541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
251541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
261541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
271541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
281541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
291541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
301541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
311541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
321541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
331541Srgrimes * SUCH DAMAGE.
341541Srgrimes *
351541Srgrimes *	@(#)kern_sysctl.c	8.4 (Berkeley) 4/14/94
361541Srgrimes */
371541Srgrimes
38116182Sobrien#include <sys/cdefs.h>
39116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/kern_sysctl.c 172930 2007-10-24 19:04:04Z rwatson $");
40116182Sobrien
4131778Seivind#include "opt_compat.h"
42106025Srwatson#include "opt_mac.h"
4331778Seivind
441541Srgrimes#include <sys/param.h>
4548274Speter#include <sys/systm.h>
4648274Speter#include <sys/kernel.h>
471541Srgrimes#include <sys/sysctl.h>
4812623Sphk#include <sys/malloc.h>
49164033Srwatson#include <sys/priv.h>
5012662Sdg#include <sys/proc.h>
5182746Sdillon#include <sys/lock.h>
5282746Sdillon#include <sys/mutex.h>
5393616Salfred#include <sys/sx.h>
5415103Sphk#include <sys/sysproto.h>
55163606Srwatson
56163606Srwatson#include <security/mac/mac_framework.h>
57163606Srwatson
5812645Sbde#include <vm/vm.h>
5912662Sdg#include <vm/vm_extern.h>
6012645Sbde
6130354Sphkstatic MALLOC_DEFINE(M_SYSCTL, "sysctl", "sysctl internal magic");
6263212Sabialstatic MALLOC_DEFINE(M_SYSCTLOID, "sysctloid", "sysctl dynamic oids");
63100833Struckmanstatic MALLOC_DEFINE(M_SYSCTLTMP, "sysctltmp", "sysctl temp output buffer");
6430309Sphk
6512429Sphk/*
6693625Srwatson * Locking - this locks the sysctl tree in memory.
6712429Sphk */
6893625Srwatsonstatic struct sx sysctllock;
6912429Sphk
7093625Srwatson#define	SYSCTL_LOCK()		sx_xlock(&sysctllock)
71105999Smux#define	SYSCTL_UNLOCK()		sx_xunlock(&sysctllock)
72112107Sjhb#define	SYSCTL_INIT()		sx_init(&sysctllock, "sysctl lock")
7393616Salfred
7462573Sphkstatic int sysctl_root(SYSCTL_HANDLER_ARGS);
7512429Sphk
7644078Sdfrstruct sysctl_oid_list sysctl__children; /* root list */
7712152Sphk
7863212Sabialstatic struct sysctl_oid *
7963212Sabialsysctl_find_oidname(const char *name, struct sysctl_oid_list *list)
8063212Sabial{
8163212Sabial	struct sysctl_oid *oidp;
8263212Sabial
8363212Sabial	SLIST_FOREACH(oidp, list, oid_link) {
8463212Sabial		if (strcmp(oidp->oid_name, name) == 0) {
8563212Sabial			return (oidp);
8663212Sabial		}
8763212Sabial	}
8863212Sabial	return (NULL);
8963212Sabial}
9063212Sabial
9112623Sphk/*
9212623Sphk * Initialization of the MIB tree.
9312623Sphk *
9444078Sdfr * Order by number in each list.
9512623Sphk */
9612429Sphk
9780338Sroamvoid
9880338Sroamsysctl_register_oid(struct sysctl_oid *oidp)
9912152Sphk{
10044078Sdfr	struct sysctl_oid_list *parent = oidp->oid_parent;
10144078Sdfr	struct sysctl_oid *p;
10244078Sdfr	struct sysctl_oid *q;
10312197Sbde
10444078Sdfr	/*
10563212Sabial	 * First check if another oid with the same name already
10663212Sabial	 * exists in the parent's list.
10763212Sabial	 */
10863212Sabial	p = sysctl_find_oidname(oidp->oid_name, parent);
10963212Sabial	if (p != NULL) {
11063212Sabial		if ((p->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
11163212Sabial			p->oid_refcnt++;
11263212Sabial			return;
11363212Sabial		} else {
11463212Sabial			printf("can't re-use a leaf (%s)!\n", p->oid_name);
11563212Sabial			return;
11663212Sabial		}
11763212Sabial	}
11863212Sabial	/*
11944078Sdfr	 * If this oid has a number OID_AUTO, give it a number which
12080339Sroam	 * is greater than any current oid.
12180339Sroam	 * NOTE: DO NOT change the starting value here, change it in
12280339Sroam	 * <sys/sysctl.h>, and make sure it is at least 256 to
12380339Sroam	 * accomodate e.g. net.inet.raw as a static sysctl node.
12444078Sdfr	 */
12544078Sdfr	if (oidp->oid_number == OID_AUTO) {
12680339Sroam		static int newoid = CTL_AUTO_START;
12771510Smckusick
12871510Smckusick		oidp->oid_number = newoid++;
12971510Smckusick		if (newoid == 0x7fffffff)
13071510Smckusick			panic("out of oids");
13144078Sdfr	}
13284832Sroam#if 0
13384832Sroam	else if (oidp->oid_number >= CTL_AUTO_START) {
13484832Sroam		/* do not panic; this happens when unregistering sysctl sets */
13584832Sroam		printf("static sysctl oid too high: %d", oidp->oid_number);
13684832Sroam	}
13784832Sroam#endif
13844078Sdfr
13944078Sdfr	/*
14044078Sdfr	 * Insert the oid into the parent's list in order.
14144078Sdfr	 */
14244078Sdfr	q = NULL;
14344078Sdfr	SLIST_FOREACH(p, parent, oid_link) {
14444078Sdfr		if (oidp->oid_number < p->oid_number)
14544078Sdfr			break;
14644078Sdfr		q = p;
14744078Sdfr	}
14844078Sdfr	if (q)
14944078Sdfr		SLIST_INSERT_AFTER(q, oidp, oid_link);
15044078Sdfr	else
15144078Sdfr		SLIST_INSERT_HEAD(parent, oidp, oid_link);
15212152Sphk}
15312131Sphk
15480338Sroamvoid
15580338Sroamsysctl_unregister_oid(struct sysctl_oid *oidp)
15612152Sphk{
157115391Smux	struct sysctl_oid *p;
158115391Smux	int error;
159115391Smux
160115391Smux	error = ENOENT;
161115391Smux	if (oidp->oid_number == OID_AUTO) {
162115391Smux		error = EINVAL;
163115391Smux	} else {
164115391Smux		SLIST_FOREACH(p, oidp->oid_parent, oid_link) {
165115391Smux			if (p == oidp) {
166115391Smux				SLIST_REMOVE(oidp->oid_parent, oidp,
167115391Smux				    sysctl_oid, oid_link);
168115391Smux				error = 0;
169115391Smux				break;
170115391Smux			}
171115391Smux		}
172115391Smux	}
173115391Smux
174115391Smux	/*
175115391Smux	 * This can happen when a module fails to register and is
176115391Smux	 * being unloaded afterwards.  It should not be a panic()
177115391Smux	 * for normal use.
178115391Smux	 */
179115391Smux	if (error)
180115391Smux		printf("%s: failed to unregister sysctl\n", __func__);
18144078Sdfr}
18212152Sphk
18363212Sabial/* Initialize a new context to keep track of dynamically added sysctls. */
18463212Sabialint
18563212Sabialsysctl_ctx_init(struct sysctl_ctx_list *c)
18663212Sabial{
18763212Sabial
18863212Sabial	if (c == NULL) {
18963212Sabial		return (EINVAL);
19063212Sabial	}
19163212Sabial	TAILQ_INIT(c);
19263212Sabial	return (0);
19363212Sabial}
19463212Sabial
19563212Sabial/* Free the context, and destroy all dynamic oids registered in this context */
19663212Sabialint
19763212Sabialsysctl_ctx_free(struct sysctl_ctx_list *clist)
19863212Sabial{
19963212Sabial	struct sysctl_ctx_entry *e, *e1;
20063212Sabial	int error;
20163212Sabial
20263212Sabial	error = 0;
20363212Sabial	/*
20463212Sabial	 * First perform a "dry run" to check if it's ok to remove oids.
20563212Sabial	 * XXX FIXME
20663212Sabial	 * XXX This algorithm is a hack. But I don't know any
20763212Sabial	 * XXX better solution for now...
20863212Sabial	 */
20963212Sabial	TAILQ_FOREACH(e, clist, link) {
21063212Sabial		error = sysctl_remove_oid(e->entry, 0, 0);
21163212Sabial		if (error)
21263212Sabial			break;
21363212Sabial	}
21463212Sabial	/*
21563212Sabial	 * Restore deregistered entries, either from the end,
21663212Sabial	 * or from the place where error occured.
21763212Sabial	 * e contains the entry that was not unregistered
21863212Sabial	 */
21963212Sabial	if (error)
22063212Sabial		e1 = TAILQ_PREV(e, sysctl_ctx_list, link);
22163212Sabial	else
22263212Sabial		e1 = TAILQ_LAST(clist, sysctl_ctx_list);
22363212Sabial	while (e1 != NULL) {
22463212Sabial		sysctl_register_oid(e1->entry);
22563212Sabial		e1 = TAILQ_PREV(e1, sysctl_ctx_list, link);
22663212Sabial	}
22763212Sabial	if (error)
22863212Sabial		return(EBUSY);
22963212Sabial	/* Now really delete the entries */
23063212Sabial	e = TAILQ_FIRST(clist);
23163212Sabial	while (e != NULL) {
23263212Sabial		e1 = TAILQ_NEXT(e, link);
23363212Sabial		error = sysctl_remove_oid(e->entry, 1, 0);
23463212Sabial		if (error)
23563212Sabial			panic("sysctl_remove_oid: corrupt tree, entry: %s",
23663212Sabial			    e->entry->oid_name);
23763212Sabial		free(e, M_SYSCTLOID);
23863212Sabial		e = e1;
23963212Sabial	}
24063212Sabial	return (error);
24163212Sabial}
24263212Sabial
24363212Sabial/* Add an entry to the context */
24463212Sabialstruct sysctl_ctx_entry *
24563212Sabialsysctl_ctx_entry_add(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
24663212Sabial{
24763212Sabial	struct sysctl_ctx_entry *e;
24863212Sabial
24963212Sabial	if (clist == NULL || oidp == NULL)
25063212Sabial		return(NULL);
251111119Simp	e = malloc(sizeof(struct sysctl_ctx_entry), M_SYSCTLOID, M_WAITOK);
25263212Sabial	e->entry = oidp;
25363212Sabial	TAILQ_INSERT_HEAD(clist, e, link);
25463212Sabial	return (e);
25563212Sabial}
25663212Sabial
25763212Sabial/* Find an entry in the context */
25863212Sabialstruct sysctl_ctx_entry *
25963212Sabialsysctl_ctx_entry_find(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
26063212Sabial{
26163212Sabial	struct sysctl_ctx_entry *e;
26263212Sabial
26363212Sabial	if (clist == NULL || oidp == NULL)
26463212Sabial		return(NULL);
26571999Sphk	TAILQ_FOREACH(e, clist, link) {
26663212Sabial		if(e->entry == oidp)
26763212Sabial			return(e);
26863212Sabial	}
26963212Sabial	return (e);
27063212Sabial}
27163212Sabial
27244078Sdfr/*
27363212Sabial * Delete an entry from the context.
27463212Sabial * NOTE: this function doesn't free oidp! You have to remove it
27563212Sabial * with sysctl_remove_oid().
27663212Sabial */
27763212Sabialint
27863212Sabialsysctl_ctx_entry_del(struct sysctl_ctx_list *clist, struct sysctl_oid *oidp)
27963212Sabial{
28063212Sabial	struct sysctl_ctx_entry *e;
28163212Sabial
28263212Sabial	if (clist == NULL || oidp == NULL)
28363212Sabial		return (EINVAL);
28463212Sabial	e = sysctl_ctx_entry_find(clist, oidp);
28563212Sabial	if (e != NULL) {
28663212Sabial		TAILQ_REMOVE(clist, e, link);
28763212Sabial		free(e, M_SYSCTLOID);
28863212Sabial		return (0);
28963212Sabial	} else
29063212Sabial		return (ENOENT);
29163212Sabial}
29263212Sabial
29363212Sabial/*
29463212Sabial * Remove dynamically created sysctl trees.
29563212Sabial * oidp - top of the tree to be removed
29663212Sabial * del - if 0 - just deregister, otherwise free up entries as well
29763212Sabial * recurse - if != 0 traverse the subtree to be deleted
29863212Sabial */
29963212Sabialint
30063212Sabialsysctl_remove_oid(struct sysctl_oid *oidp, int del, int recurse)
30163212Sabial{
30263212Sabial	struct sysctl_oid *p;
30363212Sabial	int error;
30463212Sabial
30563212Sabial	if (oidp == NULL)
30663212Sabial		return(EINVAL);
30763212Sabial	if ((oidp->oid_kind & CTLFLAG_DYN) == 0) {
30863212Sabial		printf("can't remove non-dynamic nodes!\n");
30963212Sabial		return (EINVAL);
31063212Sabial	}
31163212Sabial	/*
31263212Sabial	 * WARNING: normal method to do this should be through
31363212Sabial	 * sysctl_ctx_free(). Use recursing as the last resort
31463212Sabial	 * method to purge your sysctl tree of leftovers...
31563212Sabial	 * However, if some other code still references these nodes,
31663212Sabial	 * it will panic.
31763212Sabial	 */
31863212Sabial	if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
31963212Sabial		if (oidp->oid_refcnt == 1) {
32063212Sabial			SLIST_FOREACH(p, SYSCTL_CHILDREN(oidp), oid_link) {
32163212Sabial				if (!recurse)
32263212Sabial					return (ENOTEMPTY);
32363212Sabial				error = sysctl_remove_oid(p, del, recurse);
32463212Sabial				if (error)
32563212Sabial					return (error);
32663212Sabial			}
32763212Sabial			if (del)
32863212Sabial				free(SYSCTL_CHILDREN(oidp), M_SYSCTLOID);
32963212Sabial		}
33063212Sabial	}
33163212Sabial	if (oidp->oid_refcnt > 1 ) {
33263212Sabial		oidp->oid_refcnt--;
33363212Sabial	} else {
33463212Sabial		if (oidp->oid_refcnt == 0) {
33563212Sabial			printf("Warning: bad oid_refcnt=%u (%s)!\n",
33663212Sabial				oidp->oid_refcnt, oidp->oid_name);
33763212Sabial			return (EINVAL);
33863212Sabial		}
33963212Sabial		sysctl_unregister_oid(oidp);
34063212Sabial		if (del) {
341141433Sphk			if (oidp->oid_descr)
342141433Sphk				free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
34363978Speter			free((void *)(uintptr_t)(const void *)oidp->oid_name,
34463978Speter			     M_SYSCTLOID);
34563212Sabial			free(oidp, M_SYSCTLOID);
34663212Sabial		}
34763212Sabial	}
34863212Sabial	return (0);
34963212Sabial}
35063212Sabial
35163212Sabial/*
35263212Sabial * Create new sysctls at run time.
35363212Sabial * clist may point to a valid context initialized with sysctl_ctx_init().
35463212Sabial */
35563212Sabialstruct sysctl_oid *
35663212Sabialsysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
35770679Sjhb	int number, const char *name, int kind, void *arg1, int arg2,
35870679Sjhb	int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
35963212Sabial{
36063212Sabial	struct sysctl_oid *oidp;
36163212Sabial	ssize_t len;
36263978Speter	char *newname;
36363212Sabial
36463212Sabial	/* You have to hook up somewhere.. */
36563212Sabial	if (parent == NULL)
36663212Sabial		return(NULL);
36763212Sabial	/* Check if the node already exists, otherwise create it */
36863212Sabial	oidp = sysctl_find_oidname(name, parent);
36963212Sabial	if (oidp != NULL) {
37063212Sabial		if ((oidp->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
37163212Sabial			oidp->oid_refcnt++;
37263212Sabial			/* Update the context */
37363212Sabial			if (clist != NULL)
37463212Sabial				sysctl_ctx_entry_add(clist, oidp);
37563212Sabial			return (oidp);
37663212Sabial		} else {
37763212Sabial			printf("can't re-use a leaf (%s)!\n", name);
37863212Sabial			return (NULL);
37963212Sabial		}
38063212Sabial	}
381111119Simp	oidp = malloc(sizeof(struct sysctl_oid), M_SYSCTLOID, M_WAITOK|M_ZERO);
38263212Sabial	oidp->oid_parent = parent;
38363212Sabial	SLIST_NEXT(oidp, oid_link) = NULL;
38463212Sabial	oidp->oid_number = number;
38563212Sabial	oidp->oid_refcnt = 1;
38663212Sabial	len = strlen(name);
387111119Simp	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
38863978Speter	bcopy(name, newname, len + 1);
38963978Speter	newname[len] = '\0';
39063978Speter	oidp->oid_name = newname;
39163212Sabial	oidp->oid_handler = handler;
39263212Sabial	oidp->oid_kind = CTLFLAG_DYN | kind;
39363212Sabial	if ((kind & CTLTYPE) == CTLTYPE_NODE) {
39463212Sabial		/* Allocate space for children */
395132776Skan		SYSCTL_CHILDREN_SET(oidp, malloc(sizeof(struct sysctl_oid_list),
396132776Skan		    M_SYSCTLOID, M_WAITOK));
39763212Sabial		SLIST_INIT(SYSCTL_CHILDREN(oidp));
39863212Sabial	} else {
39963212Sabial		oidp->oid_arg1 = arg1;
40063212Sabial		oidp->oid_arg2 = arg2;
40163212Sabial	}
40263212Sabial	oidp->oid_fmt = fmt;
40388006Sluigi	if (descr) {
40488006Sluigi		int len = strlen(descr) + 1;
405141433Sphk		oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
406141433Sphk		if (oidp->oid_descr)
407141433Sphk			strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
40888006Sluigi	}
40963212Sabial	/* Update the context, if used */
41063212Sabial	if (clist != NULL)
41163212Sabial		sysctl_ctx_entry_add(clist, oidp);
41263212Sabial	/* Register this oid */
41363212Sabial	sysctl_register_oid(oidp);
41463212Sabial	return (oidp);
41563212Sabial}
41663212Sabial
41763212Sabial/*
418126319Sdes * Reparent an existing oid.
419126319Sdes */
420126319Sdesint
421126319Sdessysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
422126319Sdes{
423126319Sdes	struct sysctl_oid *oidp;
424126319Sdes
425126319Sdes	if (oid->oid_parent == parent)
426126319Sdes		return (0);
427126319Sdes	oidp = sysctl_find_oidname(oid->oid_name, parent);
428126319Sdes	if (oidp != NULL)
429126319Sdes		return (EEXIST);
430126319Sdes	sysctl_unregister_oid(oid);
431126319Sdes	oid->oid_parent = parent;
432126319Sdes	oid->oid_number = OID_AUTO;
433126319Sdes	sysctl_register_oid(oid);
434126319Sdes	return (0);
435126319Sdes}
436126319Sdes
437126319Sdes/*
43844078Sdfr * Register the kernel's oids on startup.
43944078Sdfr */
44078161SpeterSET_DECLARE(sysctl_set, struct sysctl_oid);
44112152Sphk
44280338Sroamstatic void
44380338Sroamsysctl_register_all(void *arg)
44438869Sbde{
44578161Speter	struct sysctl_oid **oidp;
44678161Speter
44793625Srwatson	SYSCTL_INIT();
44878161Speter	SET_FOREACH(oidp, sysctl_set)
44978161Speter		sysctl_register_oid(*oidp);
45038869Sbde}
45144078SdfrSYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
45244078Sdfr
45312623Sphk/*
45412623Sphk * "Staff-functions"
45512623Sphk *
45612650Sphk * These functions implement a presently undocumented interface
45712650Sphk * used by the sysctl program to walk the tree, and get the type
45812650Sphk * so it can print the value.
45912650Sphk * This interface is under work and consideration, and should probably
46012650Sphk * be killed with a big axe by the first person who can find the time.
46112650Sphk * (be aware though, that the proper interface isn't as obvious as it
46212650Sphk * may seem, there are various conflicting requirements.
46312650Sphk *
46412623Sphk * {0,0}	printf the entire MIB-tree.
46512623Sphk * {0,1,...}	return the name of the "..." OID.
46642467Sphk * {0,2,...}	return the next OID.
46712623Sphk * {0,3}	return the OID of the name in "new"
46812650Sphk * {0,4,...}	return the kind & format info for the "..." OID.
46988006Sluigi * {0,5,...}	return the description the "..." OID.
47012623Sphk */
47112623Sphk
472136999Srwatson#ifdef SYSCTL_DEBUG
47312152Sphkstatic void
47444078Sdfrsysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
47512152Sphk{
47644078Sdfr	int k;
47744078Sdfr	struct sysctl_oid *oidp;
47812152Sphk
47944078Sdfr	SLIST_FOREACH(oidp, l, oid_link) {
48012152Sphk
48112152Sphk		for (k=0; k<i; k++)
48212152Sphk			printf(" ");
48312152Sphk
48444078Sdfr		printf("%d %s ", oidp->oid_number, oidp->oid_name);
48512152Sphk
48612152Sphk		printf("%c%c",
48744078Sdfr			oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
48844078Sdfr			oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
48912152Sphk
49044078Sdfr		if (oidp->oid_handler)
49115241Sphk			printf(" *Handler");
49215241Sphk
49344078Sdfr		switch (oidp->oid_kind & CTLTYPE) {
49412243Sphk			case CTLTYPE_NODE:
49515241Sphk				printf(" Node\n");
49644078Sdfr				if (!oidp->oid_handler) {
49712152Sphk					sysctl_sysctl_debug_dump_node(
49844078Sdfr						oidp->oid_arg1, i+2);
49912152Sphk				}
50012152Sphk				break;
50112152Sphk			case CTLTYPE_INT:    printf(" Int\n"); break;
50212152Sphk			case CTLTYPE_STRING: printf(" String\n"); break;
50312152Sphk			case CTLTYPE_QUAD:   printf(" Quad\n"); break;
50412152Sphk			case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
50512152Sphk			default:	     printf("\n");
50612152Sphk		}
50712152Sphk
50812152Sphk	}
50912152Sphk}
51012152Sphk
51112152Sphkstatic int
51262573Sphksysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
51312152Sphk{
51487024Speter	int error;
51587024Speter
516164033Srwatson	error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
51787024Speter	if (error)
518139483Spjd		return (error);
51944078Sdfr	sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
520139483Spjd	return (ENOENT);
52112152Sphk}
52212152Sphk
52312152SphkSYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
52412623Sphk	0, 0, sysctl_sysctl_debug, "-", "");
525136999Srwatson#endif
52612152Sphk
52712623Sphkstatic int
52862573Sphksysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
52912623Sphk{
53012623Sphk	int *name = (int *) arg1;
53112623Sphk	u_int namelen = arg2;
53244078Sdfr	int error = 0;
53344078Sdfr	struct sysctl_oid *oid;
53444972Sphk	struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
53512623Sphk	char buf[10];
53612131Sphk
53712623Sphk	while (namelen) {
53812623Sphk		if (!lsp) {
53941514Sarchie			snprintf(buf,sizeof(buf),"%d",*name);
54012623Sphk			if (req->oldidx)
54112623Sphk				error = SYSCTL_OUT(req, ".", 1);
54212623Sphk			if (!error)
54312623Sphk				error = SYSCTL_OUT(req, buf, strlen(buf));
54412623Sphk			if (error)
54512623Sphk				return (error);
54612623Sphk			namelen--;
54712623Sphk			name++;
54812623Sphk			continue;
54912623Sphk		}
55044972Sphk		lsp2 = 0;
55144078Sdfr		SLIST_FOREACH(oid, lsp, oid_link) {
55244078Sdfr			if (oid->oid_number != *name)
55312623Sphk				continue;
55412131Sphk
55512623Sphk			if (req->oldidx)
55612623Sphk				error = SYSCTL_OUT(req, ".", 1);
55712623Sphk			if (!error)
55844078Sdfr				error = SYSCTL_OUT(req, oid->oid_name,
55944078Sdfr					strlen(oid->oid_name));
56012623Sphk			if (error)
56112623Sphk				return (error);
56212623Sphk
56312623Sphk			namelen--;
56412623Sphk			name++;
56512623Sphk
56644972Sphk			if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
56712623Sphk				break;
56812623Sphk
56944078Sdfr			if (oid->oid_handler)
57012623Sphk				break;
57112623Sphk
57244972Sphk			lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
57312623Sphk			break;
57412623Sphk		}
57544972Sphk		lsp = lsp2;
57612623Sphk	}
57712623Sphk	return (SYSCTL_OUT(req, "", 1));
57812623Sphk}
57912623Sphk
580141626Sphkstatic SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
58112623Sphk
58212623Sphkstatic int
58363978Spetersysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
58444078Sdfr	int *next, int *len, int level, struct sysctl_oid **oidpp)
58512623Sphk{
58644078Sdfr	struct sysctl_oid *oidp;
58712623Sphk
58812623Sphk	*len = level;
58944078Sdfr	SLIST_FOREACH(oidp, lsp, oid_link) {
59044078Sdfr		*next = oidp->oid_number;
59144078Sdfr		*oidpp = oidp;
59212623Sphk
593101650Smux		if (oidp->oid_kind & CTLFLAG_SKIP)
594101650Smux			continue;
595101650Smux
59612623Sphk		if (!namelen) {
59744078Sdfr			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
598139483Spjd				return (0);
59944078Sdfr			if (oidp->oid_handler)
60012623Sphk				/* We really should call the handler here...*/
601139483Spjd				return (0);
60244078Sdfr			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
60363978Speter			if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
60444078Sdfr				len, level+1, oidpp))
605139483Spjd				return (0);
606111260Srwatson			goto emptynode;
60712623Sphk		}
60812623Sphk
60944078Sdfr		if (oidp->oid_number < *name)
61012623Sphk			continue;
61112623Sphk
61244078Sdfr		if (oidp->oid_number > *name) {
61344078Sdfr			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
614139483Spjd				return (0);
61544078Sdfr			if (oidp->oid_handler)
616139483Spjd				return (0);
61744078Sdfr			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
61863978Speter			if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
61944078Sdfr				next+1, len, level+1, oidpp))
62012623Sphk				return (0);
62115241Sphk			goto next;
62212623Sphk		}
62344078Sdfr		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
62412623Sphk			continue;
62512623Sphk
62644078Sdfr		if (oidp->oid_handler)
62712623Sphk			continue;
62812623Sphk
62944078Sdfr		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
63063978Speter		if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
63144078Sdfr			len, level+1, oidpp))
63212623Sphk			return (0);
63315241Sphk	next:
63412623Sphk		namelen = 1;
635111260Srwatson	emptynode:
63612623Sphk		*len = level;
63712623Sphk	}
638139483Spjd	return (1);
63912623Sphk}
64012623Sphk
64112623Sphkstatic int
64262573Sphksysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
64312623Sphk{
64412623Sphk	int *name = (int *) arg1;
64512623Sphk	u_int namelen = arg2;
64612623Sphk	int i, j, error;
64712623Sphk	struct sysctl_oid *oid;
64844078Sdfr	struct sysctl_oid_list *lsp = &sysctl__children;
64912623Sphk	int newoid[CTL_MAXNAME];
65012623Sphk
65163978Speter	i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
65212623Sphk	if (i)
653139483Spjd		return (ENOENT);
65412650Sphk	error = SYSCTL_OUT(req, newoid, j * sizeof (int));
65512623Sphk	return (error);
65612623Sphk}
65712623Sphk
658141626Sphkstatic SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
65912623Sphk
66012623Sphkstatic int
66144078Sdfrname2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
66212623Sphk{
66344078Sdfr	int i;
66444078Sdfr	struct sysctl_oid *oidp;
66544078Sdfr	struct sysctl_oid_list *lsp = &sysctl__children;
66612623Sphk	char *p;
66712623Sphk
66812623Sphk	if (!*name)
669139483Spjd		return (ENOENT);
67012623Sphk
67112623Sphk	p = name + strlen(name) - 1 ;
67212623Sphk	if (*p == '.')
67312623Sphk		*p = '\0';
67412623Sphk
67512623Sphk	*len = 0;
67612623Sphk
67712623Sphk	for (p = name; *p && *p != '.'; p++)
67812623Sphk		;
67912623Sphk	i = *p;
68012623Sphk	if (i == '.')
68112623Sphk		*p = '\0';
68212623Sphk
68344078Sdfr	oidp = SLIST_FIRST(lsp);
68412623Sphk
68544078Sdfr	while (oidp && *len < CTL_MAXNAME) {
68644078Sdfr		if (strcmp(name, oidp->oid_name)) {
68744078Sdfr			oidp = SLIST_NEXT(oidp, oid_link);
68812623Sphk			continue;
68912623Sphk		}
69044078Sdfr		*oid++ = oidp->oid_number;
69112623Sphk		(*len)++;
69212623Sphk
69312623Sphk		if (!i) {
69444078Sdfr			if (oidpp)
69544078Sdfr				*oidpp = oidp;
69612623Sphk			return (0);
69712623Sphk		}
69812623Sphk
69944078Sdfr		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
70012623Sphk			break;
70112623Sphk
70244078Sdfr		if (oidp->oid_handler)
70312623Sphk			break;
70412623Sphk
70544078Sdfr		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
70644078Sdfr		oidp = SLIST_FIRST(lsp);
70712623Sphk		name = p+1;
70812623Sphk		for (p = name; *p && *p != '.'; p++)
70912623Sphk				;
71012623Sphk		i = *p;
71112623Sphk		if (i == '.')
71212623Sphk			*p = '\0';
71312623Sphk	}
714139483Spjd	return (ENOENT);
71512623Sphk}
71612623Sphk
71712623Sphkstatic int
71862573Sphksysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
71912623Sphk{
72012623Sphk	char *p;
72112623Sphk	int error, oid[CTL_MAXNAME], len;
72212623Sphk	struct sysctl_oid *op = 0;
72312623Sphk
72412623Sphk	if (!req->newlen)
725139483Spjd		return (ENOENT);
72645140Sphk	if (req->newlen >= MAXPATHLEN)	/* XXX arbitrary, undocumented */
72745140Sphk		return (ENAMETOOLONG);
72812623Sphk
729111119Simp	p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
73012623Sphk
73112623Sphk	error = SYSCTL_IN(req, p, req->newlen);
73212623Sphk	if (error) {
73312623Sphk		free(p, M_SYSCTL);
73412623Sphk		return (error);
73512623Sphk	}
73612623Sphk
73712623Sphk	p [req->newlen] = '\0';
73812623Sphk
73912623Sphk	error = name2oid(p, oid, &len, &op);
74012623Sphk
74112623Sphk	free(p, M_SYSCTL);
74212623Sphk
74312623Sphk	if (error)
74412623Sphk		return (error);
74512623Sphk
74612650Sphk	error = SYSCTL_OUT(req, oid, len * sizeof *oid);
74712623Sphk	return (error);
74812623Sphk}
74912623Sphk
75012910SphkSYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
75112623Sphk	sysctl_sysctl_name2oid, "I", "");
75212623Sphk
75312623Sphkstatic int
75462573Sphksysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
75512623Sphk{
75644078Sdfr	struct sysctl_oid *oid;
75753977Sgreen	int error;
75812623Sphk
75953977Sgreen	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
76053977Sgreen	if (error)
76153977Sgreen		return (error);
76212623Sphk
76344078Sdfr	if (!oid->oid_fmt)
76453977Sgreen		return (ENOENT);
76553977Sgreen	error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
76653977Sgreen	if (error)
76753977Sgreen		return (error);
76853977Sgreen	error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
76912650Sphk	return (error);
77012623Sphk}
77112623Sphk
77242467Sphk
773141626Sphkstatic SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
77412623Sphk
77588006Sluigistatic int
77688006Sluigisysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
77788006Sluigi{
77888006Sluigi	struct sysctl_oid *oid;
77988006Sluigi	int error;
78088006Sluigi
78188006Sluigi	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
78288006Sluigi	if (error)
78388006Sluigi		return (error);
78488006Sluigi
785141433Sphk	if (!oid->oid_descr)
78688006Sluigi		return (ENOENT);
787141433Sphk	error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
78888006Sluigi	return (error);
78988006Sluigi}
79088006Sluigi
791141626Sphkstatic SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
79288006Sluigi
79312243Sphk/*
79412623Sphk * Default "handler" functions.
79512623Sphk */
79612623Sphk
79712623Sphk/*
79842095Sdfr * Handle an int, signed or unsigned.
79912243Sphk * Two cases:
80012243Sphk *     a variable:  point arg1 at it.
80112243Sphk *     a constant:  pass it in arg2.
80212243Sphk */
80312243Sphk
80411865Sphkint
80562573Sphksysctl_handle_int(SYSCTL_HANDLER_ARGS)
80611863Sphk{
807100833Struckman	int tmpout, error = 0;
80811863Sphk
809100833Struckman	/*
810100833Struckman	 * Attempt to get a coherent snapshot by making a copy of the data.
811100833Struckman	 */
81212243Sphk	if (arg1)
813100833Struckman		tmpout = *(int *)arg1;
81420506Sbde	else
815100833Struckman		tmpout = arg2;
816100833Struckman	error = SYSCTL_OUT(req, &tmpout, sizeof(int));
81711863Sphk
81812243Sphk	if (error || !req->newptr)
81912243Sphk		return (error);
82011863Sphk
82112243Sphk	if (!arg1)
82212243Sphk		error = EPERM;
82312243Sphk	else
82412243Sphk		error = SYSCTL_IN(req, arg1, sizeof(int));
82512243Sphk	return (error);
82611863Sphk}
82711863Sphk
828155758Sandre
82912243Sphk/*
830155758Sandre * Based on on sysctl_handle_int() convert milliseconds into ticks.
831155758Sandre */
832155758Sandre
833155758Sandreint
834155758Sandresysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
835155758Sandre{
836155758Sandre	int error, s, tt;
837155758Sandre
838155758Sandre	tt = *(int *)oidp->oid_arg1;
839155758Sandre	s = (int)((int64_t)tt * 1000 / hz);
840155758Sandre
841155758Sandre	error = sysctl_handle_int(oidp, &s, 0, req);
842155758Sandre	if (error || !req->newptr)
843155758Sandre		return (error);
844155758Sandre
845155758Sandre	tt = (int)((int64_t)s * hz / 1000);
846155758Sandre	if (tt < 1)
847155758Sandre		return (EINVAL);
848155758Sandre
849155758Sandre	*(int *)oidp->oid_arg1 = tt;
850155758Sandre	return (0);
851155758Sandre}
852155758Sandre
853155758Sandre
854155758Sandre/*
85545140Sphk * Handle a long, signed or unsigned.  arg1 points to it.
85638517Sdfr */
85738517Sdfr
85838517Sdfrint
85962573Sphksysctl_handle_long(SYSCTL_HANDLER_ARGS)
86038517Sdfr{
86138517Sdfr	int error = 0;
862136404Speter	long tmplong;
863136404Speter#ifdef SCTL_MASK32
864136404Speter	int tmpint;
865136404Speter#endif
86638517Sdfr
867100833Struckman	/*
868100833Struckman	 * Attempt to get a coherent snapshot by making a copy of the data.
869100833Struckman	 */
87045140Sphk	if (!arg1)
87145140Sphk		return (EINVAL);
872136404Speter	tmplong = *(long *)arg1;
873136404Speter#ifdef SCTL_MASK32
874136404Speter	if (req->flags & SCTL_MASK32) {
875136404Speter		tmpint = tmplong;
876136404Speter		error = SYSCTL_OUT(req, &tmpint, sizeof(int));
877136404Speter	} else
878136404Speter#endif
879136404Speter		error = SYSCTL_OUT(req, &tmplong, sizeof(long));
88038517Sdfr
88138517Sdfr	if (error || !req->newptr)
88238517Sdfr		return (error);
88338517Sdfr
884136404Speter#ifdef SCTL_MASK32
885136404Speter	if (req->flags & SCTL_MASK32) {
886136404Speter		error = SYSCTL_IN(req, &tmpint, sizeof(int));
887136404Speter		*(long *)arg1 = (long)tmpint;
888136404Speter	} else
889136404Speter#endif
890136404Speter		error = SYSCTL_IN(req, arg1, sizeof(long));
89138517Sdfr	return (error);
89238517Sdfr}
89338517Sdfr
89438517Sdfr/*
895170288Sdwmalone * Handle a 64 bit int, signed or unsigned.  arg1 points to it.
896170288Sdwmalone */
897170288Sdwmalone
898170288Sdwmaloneint
899170288Sdwmalonesysctl_handle_quad(SYSCTL_HANDLER_ARGS)
900170288Sdwmalone{
901170288Sdwmalone	int error = 0;
902170288Sdwmalone	uint64_t tmpout;
903170288Sdwmalone
904170288Sdwmalone	/*
905170288Sdwmalone	 * Attempt to get a coherent snapshot by making a copy of the data.
906170288Sdwmalone	 */
907170288Sdwmalone	if (!arg1)
908170288Sdwmalone		return (EINVAL);
909170288Sdwmalone	tmpout = *(uint64_t *)arg1;
910170288Sdwmalone	error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
911170288Sdwmalone
912170288Sdwmalone	if (error || !req->newptr)
913170288Sdwmalone		return (error);
914170288Sdwmalone
915170288Sdwmalone	error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
916170288Sdwmalone	return (error);
917170288Sdwmalone}
918170288Sdwmalone
919170288Sdwmalone/*
92012243Sphk * Handle our generic '\0' terminated 'C' string.
92112243Sphk * Two cases:
92212243Sphk * 	a variable string:  point arg1 at it, arg2 is max length.
92312243Sphk * 	a constant string:  point arg1 at it, arg2 is zero.
92412243Sphk */
92512243Sphk
92611865Sphkint
92762573Sphksysctl_handle_string(SYSCTL_HANDLER_ARGS)
92811863Sphk{
92912243Sphk	int error=0;
930100833Struckman	char *tmparg;
931100833Struckman	size_t outlen;
93211863Sphk
933100833Struckman	/*
934100833Struckman	 * Attempt to get a coherent snapshot by copying to a
935100833Struckman	 * temporary kernel buffer.
936100833Struckman	 */
937100833Struckmanretry:
938100833Struckman	outlen = strlen((char *)arg1)+1;
939111119Simp	tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
940105354Srobert
941105354Srobert	if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
942100833Struckman		free(tmparg, M_SYSCTLTMP);
943100833Struckman		goto retry;
944100833Struckman	}
945105354Srobert
946100833Struckman	error = SYSCTL_OUT(req, tmparg, outlen);
947100833Struckman	free(tmparg, M_SYSCTLTMP);
94811863Sphk
94945140Sphk	if (error || !req->newptr)
95012243Sphk		return (error);
95111863Sphk
95245140Sphk	if ((req->newlen - req->newidx) >= arg2) {
95345140Sphk		error = EINVAL;
95412243Sphk	} else {
95512243Sphk		arg2 = (req->newlen - req->newidx);
95612243Sphk		error = SYSCTL_IN(req, arg1, arg2);
95712243Sphk		((char *)arg1)[arg2] = '\0';
95811863Sphk	}
95912131Sphk
96012131Sphk	return (error);
96111863Sphk}
96211863Sphk
96312243Sphk/*
96412243Sphk * Handle any kind of opaque data.
96512243Sphk * arg1 points to it, arg2 is the size.
96612243Sphk */
96712243Sphk
96811865Sphkint
96962573Sphksysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
97011863Sphk{
971120803Sbms	int error, tries;
972120803Sbms	u_int generation;
973120813Sbms	struct sysctl_req req2;
97412243Sphk
975100833Struckman	/*
976120803Sbms	 * Attempt to get a coherent snapshot, by using the thread
977120803Sbms	 * pre-emption counter updated from within mi_switch() to
978120803Sbms	 * determine if we were pre-empted during a bcopy() or
979120803Sbms	 * copyout(). Make 3 attempts at doing this before giving up.
980120803Sbms	 * If we encounter an error, stop immediately.
981100833Struckman	 */
982120803Sbms	tries = 0;
983120813Sbms	req2 = *req;
984120813Sbmsretry:
985120813Sbms	generation = curthread->td_generation;
986120813Sbms	error = SYSCTL_OUT(req, arg1, arg2);
987120813Sbms	if (error)
988120813Sbms		return (error);
989120813Sbms	tries++;
990120813Sbms	if (generation != curthread->td_generation && tries < 3) {
991120813Sbms		*req = req2;
992120813Sbms		goto retry;
993120813Sbms	}
99412243Sphk
99512243Sphk	error = SYSCTL_IN(req, arg1, arg2);
99612243Sphk
99712243Sphk	return (error);
99812243Sphk}
99912243Sphk
100012260Sphk/*
100112260Sphk * Transfer functions to/from kernel space.
100212260Sphk * XXX: rather untested at this point
100312260Sphk */
100412260Sphkstatic int
100538517Sdfrsysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
100612243Sphk{
100738517Sdfr	size_t i = 0;
100812260Sphk
100912260Sphk	if (req->oldptr) {
101038517Sdfr		i = l;
101173971Stmm		if (req->oldlen <= req->oldidx)
101273971Stmm			i = 0;
101373971Stmm		else
101473971Stmm			if (i > req->oldlen - req->oldidx)
101573971Stmm				i = req->oldlen - req->oldidx;
101612260Sphk		if (i > 0)
101717971Sbde			bcopy(p, (char *)req->oldptr + req->oldidx, i);
101812243Sphk	}
101912260Sphk	req->oldidx += l;
102016282Snate	if (req->oldptr && i != l)
102111863Sphk		return (ENOMEM);
102212260Sphk	return (0);
102312243Sphk}
102412243Sphk
102512260Sphkstatic int
102638517Sdfrsysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
102712243Sphk{
102812260Sphk	if (!req->newptr)
1029139483Spjd		return (0);
103012260Sphk	if (req->newlen - req->newidx < l)
103111863Sphk		return (EINVAL);
103217971Sbde	bcopy((char *)req->newptr + req->newidx, p, l);
103312243Sphk	req->newidx += l;
103412131Sphk	return (0);
103511863Sphk}
103611863Sphk
103716282Snateint
103883366Sjuliankernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1039136404Speter    size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
104016282Snate{
104116282Snate	int error = 0;
104216282Snate	struct sysctl_req req;
104316282Snate
104416282Snate	bzero(&req, sizeof req);
104516282Snate
104686183Srwatson	req.td = td;
1047136404Speter	req.flags = flags;
104816282Snate
104916282Snate	if (oldlenp) {
105016282Snate		req.oldlen = *oldlenp;
105116282Snate	}
1052127052Struckman	req.validlen = req.oldlen;
105316282Snate
105416282Snate	if (old) {
105516282Snate		req.oldptr= old;
105616282Snate	}
105716282Snate
105877646Sdd	if (new != NULL) {
105916282Snate		req.newlen = newlen;
106016282Snate		req.newptr = new;
106116282Snate	}
106216282Snate
106316282Snate	req.oldfunc = sysctl_old_kernel;
106416282Snate	req.newfunc = sysctl_new_kernel;
1065120781Sbms	req.lock = REQ_LOCKED;
106616282Snate
106793625Srwatson	SYSCTL_LOCK();
106816282Snate
106916282Snate	error = sysctl_root(0, name, namelen, &req);
1070120813Sbms
1071127052Struckman	if (req.lock == REQ_WIRED && req.validlen > 0)
1072127052Struckman		vsunlock(req.oldptr, req.validlen);
107316282Snate
107493625Srwatson	SYSCTL_UNLOCK();
107516282Snate
107616282Snate	if (error && error != ENOMEM)
107716282Snate		return (error);
107816282Snate
107916282Snate	if (retval) {
1080127052Struckman		if (req.oldptr && req.oldidx > req.validlen)
1081127052Struckman			*retval = req.validlen;
108216282Snate		else
108316282Snate			*retval = req.oldidx;
108416282Snate	}
108516282Snate	return (error);
108616282Snate}
108716282Snate
108876834Sjlemonint
108983366Sjuliankernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1090136404Speter    void *new, size_t newlen, size_t *retval, int flags)
109176834Sjlemon{
109276834Sjlemon        int oid[CTL_MAXNAME];
109378620Smjacob        size_t oidlen, plen;
109478620Smjacob	int error;
109576834Sjlemon
109676834Sjlemon	oid[0] = 0;		/* sysctl internal magic */
109776834Sjlemon	oid[1] = 3;		/* name2oid */
109876834Sjlemon	oidlen = sizeof(oid);
109976834Sjlemon
110083366Sjulian	error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1101136404Speter	    (void *)name, strlen(name), &plen, flags);
110276834Sjlemon	if (error)
110376834Sjlemon		return (error);
110476834Sjlemon
110583366Sjulian	error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1106136404Speter	    new, newlen, retval, flags);
110776834Sjlemon	return (error);
110876834Sjlemon}
110976834Sjlemon
111012260Sphk/*
111112260Sphk * Transfer function to/from user space.
111212260Sphk */
111312260Sphkstatic int
111438517Sdfrsysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
111512243Sphk{
111638517Sdfr	int error = 0;
1117126253Struckman	size_t i, len, origidx;
111812243Sphk
1119126253Struckman	origidx = req->oldidx;
1120126253Struckman	req->oldidx += l;
1121126253Struckman	if (req->oldptr == NULL)
1122126253Struckman		return (0);
1123148864Scsjp	/*
1124148864Scsjp	 * If we have not wired the user supplied buffer and we are currently
1125148864Scsjp	 * holding locks, drop a witness warning, as it's possible that
1126148864Scsjp	 * write operations to the user page can sleep.
1127148864Scsjp	 */
1128148864Scsjp	if (req->lock != REQ_WIRED)
1129111883Sjhb		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1130111883Sjhb		    "sysctl_old_user()");
1131126253Struckman	i = l;
1132127052Struckman	len = req->validlen;
1133126253Struckman	if (len <= origidx)
1134126253Struckman		i = 0;
1135126253Struckman	else {
1136126253Struckman		if (i > len - origidx)
1137126253Struckman			i = len - origidx;
1138126253Struckman		error = copyout(p, (char *)req->oldptr + origidx, i);
113912260Sphk	}
114012243Sphk	if (error)
114112243Sphk		return (error);
1142126253Struckman	if (i < l)
114312243Sphk		return (ENOMEM);
114412260Sphk	return (0);
114512243Sphk}
114612243Sphk
114712260Sphkstatic int
114838517Sdfrsysctl_new_user(struct sysctl_req *req, void *p, size_t l)
114912243Sphk{
115012285Sphk	int error;
115112260Sphk
115212260Sphk	if (!req->newptr)
1153139483Spjd		return (0);
115412260Sphk	if (req->newlen - req->newidx < l)
115512243Sphk		return (EINVAL);
1156148873Scsjp	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1157148873Scsjp	    "sysctl_new_user()");
115817971Sbde	error = copyin((char *)req->newptr + req->newidx, p, l);
115912243Sphk	req->newidx += l;
116012243Sphk	return (error);
116112243Sphk}
116212243Sphk
1163100487Struckman/*
1164100487Struckman * Wire the user space destination buffer.  If set to a value greater than
1165100487Struckman * zero, the len parameter limits the maximum amount of wired memory.
1166100487Struckman */
1167126253Struckmanint
1168100487Struckmansysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1169100487Struckman{
1170126253Struckman	int ret;
1171154792Struckman	size_t i, wiredlen;
1172154792Struckman	char *cp, dummy;
1173126253Struckman
1174126253Struckman	wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1175126253Struckman	ret = 0;
1176120781Sbms	if (req->lock == REQ_LOCKED && req->oldptr &&
1177120781Sbms	    req->oldfunc == sysctl_old_user) {
1178127050Struckman		if (wiredlen != 0) {
1179127050Struckman			ret = vslock(req->oldptr, wiredlen);
1180130327Sgreen			if (ret != 0) {
1181130327Sgreen				if (ret != ENOMEM)
1182130327Sgreen					return (ret);
1183130327Sgreen				wiredlen = 0;
1184130327Sgreen			}
1185154792Struckman			/*
1186154792Struckman			 * Touch all the wired pages to avoid PTE modified
1187154792Struckman			 * bit emulation traps on Alpha while holding locks
1188154792Struckman			 * in the sysctl handler.
1189154792Struckman			 */
1190154792Struckman			for (i = (wiredlen + PAGE_SIZE - 1) / PAGE_SIZE,
1191154792Struckman			    cp = req->oldptr; i > 0; i--, cp += PAGE_SIZE) {
1192154792Struckman				copyin(cp, &dummy, 1);
1193154792Struckman				copyout(&dummy, cp, 1);
1194154792Struckman			}
1195126253Struckman		}
1196127050Struckman		req->lock = REQ_WIRED;
1197127052Struckman		req->validlen = wiredlen;
1198100487Struckman	}
1199127050Struckman	return (0);
1200100487Struckman}
1201100487Struckman
12021541Srgrimesint
120353977Sgreensysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
120453977Sgreen    int *nindx, struct sysctl_req *req)
120512131Sphk{
120644078Sdfr	struct sysctl_oid *oid;
120753977Sgreen	int indx;
120812131Sphk
120953977Sgreen	oid = SLIST_FIRST(&sysctl__children);
121012131Sphk	indx = 0;
121144078Sdfr	while (oid && indx < CTL_MAXNAME) {
121244078Sdfr		if (oid->oid_number == name[indx]) {
121312131Sphk			indx++;
121444078Sdfr			if (oid->oid_kind & CTLFLAG_NOLOCK)
1215120781Sbms				req->lock = REQ_UNLOCKED;
121644078Sdfr			if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
121753977Sgreen				if (oid->oid_handler != NULL ||
121853977Sgreen				    indx == namelen) {
121953977Sgreen					*noid = oid;
122053977Sgreen					if (nindx != NULL)
122153977Sgreen						*nindx = indx;
122253977Sgreen					return (0);
122353977Sgreen				}
122453977Sgreen				oid = SLIST_FIRST(
122553977Sgreen				    (struct sysctl_oid_list *)oid->oid_arg1);
122653977Sgreen			} else if (indx == namelen) {
122753977Sgreen				*noid = oid;
122853977Sgreen				if (nindx != NULL)
122953977Sgreen					*nindx = indx;
123053977Sgreen				return (0);
123112131Sphk			} else {
123253977Sgreen				return (ENOTDIR);
123312131Sphk			}
123412131Sphk		} else {
123544078Sdfr			oid = SLIST_NEXT(oid, oid_link);
123612131Sphk		}
123712131Sphk	}
123853977Sgreen	return (ENOENT);
123953977Sgreen}
124053977Sgreen
124153977Sgreen/*
124253977Sgreen * Traverse our tree, and find the right node, execute whatever it points
124353977Sgreen * to, and return the resulting error code.
124453977Sgreen */
124553977Sgreen
1246104094Sphkstatic int
124762573Sphksysctl_root(SYSCTL_HANDLER_ARGS)
124853977Sgreen{
124953977Sgreen	struct sysctl_oid *oid;
1250109246Sdillon	int error, indx, lvl;
125153977Sgreen
125253977Sgreen	error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
125353977Sgreen	if (error)
125453977Sgreen		return (error);
125553977Sgreen
125653977Sgreen	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
125753977Sgreen		/*
125853977Sgreen		 * You can't call a sysctl when it's a node, but has
125953977Sgreen		 * no handler.  Inform the user that it's a node.
126053977Sgreen		 * The indx may or may not be the same as namelen.
126153977Sgreen		 */
126253977Sgreen		if (oid->oid_handler == NULL)
126353977Sgreen			return (EISDIR);
126453977Sgreen	}
126553977Sgreen
126683968Srwatson	/* Is this sysctl writable? */
126783968Srwatson	if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
126812131Sphk		return (EPERM);
126912131Sphk
127092953Srwatson	KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
127192953Srwatson
127283968Srwatson	/* Is this sysctl sensitive to securelevels? */
127383968Srwatson	if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1274109246Sdillon		lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1275109246Sdillon		error = securelevel_gt(req->td->td_ucred, lvl);
127692953Srwatson		if (error)
127792953Srwatson			return (error);
127883968Srwatson	}
127912910Sphk
128083968Srwatson	/* Is this sysctl writable by only privileged users? */
128183968Srwatson	if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
128292953Srwatson		if (oid->oid_kind & CTLFLAG_PRISON)
1283170587Srwatson			error = priv_check(req->td, PRIV_SYSCTL_WRITEJAIL);
128492953Srwatson		else
1285164033Srwatson			error = priv_check(req->td, PRIV_SYSCTL_WRITE);
128692953Srwatson		if (error)
128792953Srwatson			return (error);
128883968Srwatson	}
128983968Srwatson
129044078Sdfr	if (!oid->oid_handler)
1291139483Spjd		return (EINVAL);
129212131Sphk
1293126121Spjd	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1294132776Skan		arg1 = (int *)arg1 + indx;
1295126121Spjd		arg2 -= indx;
1296126121Spjd	} else {
1297126121Spjd		arg1 = oid->oid_arg1;
1298126121Spjd		arg2 = oid->oid_arg2;
1299126121Spjd	}
1300126121Spjd#ifdef MAC
1301172930Srwatson	error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1302126121Spjd	    req);
1303126121Spjd	if (error != 0)
1304126121Spjd		return (error);
1305126121Spjd#endif
1306126121Spjd	error = oid->oid_handler(oid, arg1, arg2, req);
1307126121Spjd
130853977Sgreen	return (error);
130912131Sphk}
131012131Sphk
131112221Sbde#ifndef _SYS_SYSPROTO_H_
131212171Sphkstruct sysctl_args {
131312171Sphk	int	*name;
131412171Sphk	u_int	namelen;
131512171Sphk	void	*old;
131612171Sphk	size_t	*oldlenp;
131712171Sphk	void	*new;
131812171Sphk	size_t	newlen;
131912171Sphk};
132012221Sbde#endif
132112131Sphkint
132283366Sjulian__sysctl(struct thread *td, struct sysctl_args *uap)
13231541Srgrimes{
132482746Sdillon	int error, name[CTL_MAXNAME];
132538517Sdfr	size_t j;
13261541Srgrimes
13271541Srgrimes	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
13281541Srgrimes		return (EINVAL);
132911863Sphk
13303308Sphk 	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
13313308Sphk 	if (error)
13321541Srgrimes		return (error);
13331541Srgrimes
133482746Sdillon	mtx_lock(&Giant);
133582746Sdillon
133683366Sjulian	error = userland_sysctl(td, name, uap->namelen,
133712171Sphk		uap->old, uap->oldlenp, 0,
1338136404Speter		uap->new, uap->newlen, &j, 0);
133912260Sphk	if (error && error != ENOMEM)
134082746Sdillon		goto done2;
134112260Sphk	if (uap->oldlenp) {
134282746Sdillon		int i = copyout(&j, uap->oldlenp, sizeof(j));
134312260Sphk		if (i)
134482746Sdillon			error = i;
134512260Sphk	}
134682746Sdillondone2:
134782746Sdillon	mtx_unlock(&Giant);
134812260Sphk	return (error);
134912171Sphk}
135012171Sphk
135112171Sphk/*
135212171Sphk * This is used from various compatibility syscalls too.  That's why name
135312171Sphk * must be in kernel space.
135412171Sphk */
135512171Sphkint
135683366Sjulianuserland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1357136404Speter    size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1358136404Speter    int flags)
135912171Sphk{
136012429Sphk	int error = 0;
1361127052Struckman	struct sysctl_req req;
136212171Sphk
136312243Sphk	bzero(&req, sizeof req);
136412243Sphk
136586183Srwatson	req.td = td;
1366136404Speter	req.flags = flags;
136712285Sphk
136812171Sphk	if (oldlenp) {
136912171Sphk		if (inkernel) {
137012243Sphk			req.oldlen = *oldlenp;
137112171Sphk		} else {
137212260Sphk			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
137312171Sphk			if (error)
137412171Sphk				return (error);
137512171Sphk		}
137612171Sphk	}
1377127052Struckman	req.validlen = req.oldlen;
137812171Sphk
137912243Sphk	if (old) {
138052644Sphk		if (!useracc(old, req.oldlen, VM_PROT_WRITE))
138112243Sphk			return (EFAULT);
138212243Sphk		req.oldptr= old;
138312243Sphk	}
138412131Sphk
138577646Sdd	if (new != NULL) {
1386172038Srwatson		if (!useracc(new, newlen, VM_PROT_READ))
138712243Sphk			return (EFAULT);
138812243Sphk		req.newlen = newlen;
138912243Sphk		req.newptr = new;
139011863Sphk	}
139112131Sphk
139212243Sphk	req.oldfunc = sysctl_old_user;
139312243Sphk	req.newfunc = sysctl_new_user;
1394120781Sbms	req.lock = REQ_LOCKED;
139511863Sphk
139693625Srwatson	SYSCTL_LOCK();
139712429Sphk
139816159Sphk	do {
1399127052Struckman		req.oldidx = 0;
1400127052Struckman		req.newidx = 0;
1401127052Struckman		error = sysctl_root(0, name, namelen, &req);
140216159Sphk	} while (error == EAGAIN);
140312243Sphk
1404127052Struckman	if (req.lock == REQ_WIRED && req.validlen > 0)
1405127052Struckman		vsunlock(req.oldptr, req.validlen);
140612429Sphk
140793625Srwatson	SYSCTL_UNLOCK();
140812429Sphk
140912260Sphk	if (error && error != ENOMEM)
141012260Sphk		return (error);
141112260Sphk
141212260Sphk	if (retval) {
1413127052Struckman		if (req.oldptr && req.oldidx > req.validlen)
1414127052Struckman			*retval = req.validlen;
141512260Sphk		else
141612260Sphk			*retval = req.oldidx;
141711863Sphk	}
141812260Sphk	return (error);
14191541Srgrimes}
14201541Srgrimes
14211541Srgrimes#ifdef COMPAT_43
14221541Srgrimes#include <sys/socket.h>
142315103Sphk#include <vm/vm_param.h>
142415103Sphk
14251541Srgrimes#define	KINFO_PROC		(0<<8)
14261541Srgrimes#define	KINFO_RT		(1<<8)
14271541Srgrimes#define	KINFO_VNODE		(2<<8)
14281541Srgrimes#define	KINFO_FILE		(3<<8)
14291541Srgrimes#define	KINFO_METER		(4<<8)
14301541Srgrimes#define	KINFO_LOADAVG		(5<<8)
14311541Srgrimes#define	KINFO_CLOCKRATE		(6<<8)
14321541Srgrimes
14339455Speter/* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
14349455Speter#define	KINFO_BSDI_SYSINFO	(101<<8)
14359455Speter
14369455Speter/*
14379455Speter * XXX this is bloat, but I hope it's better here than on the potentially
14389455Speter * limited kernel stack...  -Peter
14399455Speter */
14409455Speter
144112819Sphkstatic struct {
14429455Speter	int	bsdi_machine;		/* "i386" on BSD/386 */
14439455Speter/*      ^^^ this is an offset to the string, relative to the struct start */
14449455Speter	char	*pad0;
14459455Speter	long	pad1;
14469455Speter	long	pad2;
14479455Speter	long	pad3;
14489455Speter	u_long	pad4;
14499455Speter	u_long	pad5;
14509455Speter	u_long	pad6;
14519455Speter
14529455Speter	int	bsdi_ostype;		/* "BSD/386" on BSD/386 */
14539455Speter	int	bsdi_osrelease;		/* "1.1" on BSD/386 */
14549455Speter	long	pad7;
14559455Speter	long	pad8;
14569455Speter	char	*pad9;
14579455Speter
14589455Speter	long	pad10;
14599455Speter	long	pad11;
14609455Speter	int	pad12;
14619455Speter	long	pad13;
14629455Speter	quad_t	pad14;
14639455Speter	long	pad15;
14649455Speter
14659455Speter	struct	timeval pad16;
14669455Speter	/* we dont set this, because BSDI's uname used gethostname() instead */
14679455Speter	int	bsdi_hostname;		/* hostname on BSD/386 */
14689455Speter
14699455Speter	/* the actual string data is appended here */
14709455Speter
14719455Speter} bsdi_si;
1472167232Srwatson
14739455Speter/*
14749455Speter * this data is appended to the end of the bsdi_si structure during copyout.
14759455Speter * The "char *" offsets are relative to the base of the bsdi_si struct.
14769455Speter * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
14779455Speter * should not exceed the length of the buffer here... (or else!! :-)
14789455Speter */
147912819Sphkstatic char bsdi_strings[80];	/* It had better be less than this! */
14809455Speter
148112221Sbde#ifndef _SYS_SYSPROTO_H_
14821541Srgrimesstruct getkerninfo_args {
14831541Srgrimes	int	op;
14841541Srgrimes	char	*where;
148538864Sbde	size_t	*size;
14861541Srgrimes	int	arg;
14871541Srgrimes};
148812221Sbde#endif
14891549Srgrimesint
149083366Sjulianogetkerninfo(struct thread *td, struct getkerninfo_args *uap)
14911541Srgrimes{
149212171Sphk	int error, name[6];
149338517Sdfr	size_t size;
149482494Speter	u_int needed = 0;
14951541Srgrimes
149682746Sdillon	mtx_lock(&Giant);
149782746Sdillon
14981541Srgrimes	switch (uap->op & 0xff00) {
14991541Srgrimes
15001541Srgrimes	case KINFO_RT:
150112171Sphk		name[0] = CTL_NET;
150212171Sphk		name[1] = PF_ROUTE;
150312171Sphk		name[2] = 0;
150412171Sphk		name[3] = (uap->op & 0xff0000) >> 16;
150512171Sphk		name[4] = uap->op & 0xff;
150612171Sphk		name[5] = uap->arg;
150783366Sjulian		error = userland_sysctl(td, name, 6, uap->where, uap->size,
1508136417Sphk			0, 0, 0, &size, 0);
15091541Srgrimes		break;
15101541Srgrimes
15111541Srgrimes	case KINFO_VNODE:
151212171Sphk		name[0] = CTL_KERN;
151312171Sphk		name[1] = KERN_VNODE;
151483366Sjulian		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1515136417Sphk			0, 0, 0, &size, 0);
15161541Srgrimes		break;
15171541Srgrimes
15181541Srgrimes	case KINFO_PROC:
151912171Sphk		name[0] = CTL_KERN;
152012171Sphk		name[1] = KERN_PROC;
152112171Sphk		name[2] = uap->op & 0xff;
152212171Sphk		name[3] = uap->arg;
152383366Sjulian		error = userland_sysctl(td, name, 4, uap->where, uap->size,
1524136417Sphk			0, 0, 0, &size, 0);
15251541Srgrimes		break;
15261541Srgrimes
15271541Srgrimes	case KINFO_FILE:
152812171Sphk		name[0] = CTL_KERN;
152912171Sphk		name[1] = KERN_FILE;
153083366Sjulian		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1531136417Sphk			0, 0, 0, &size, 0);
15321541Srgrimes		break;
15331541Srgrimes
15341541Srgrimes	case KINFO_METER:
153512171Sphk		name[0] = CTL_VM;
1536109102Smux		name[1] = VM_TOTAL;
153783366Sjulian		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1538136417Sphk			0, 0, 0, &size, 0);
15391541Srgrimes		break;
15401541Srgrimes
15411541Srgrimes	case KINFO_LOADAVG:
154212171Sphk		name[0] = CTL_VM;
154312171Sphk		name[1] = VM_LOADAVG;
154483366Sjulian		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1545136417Sphk			0, 0, 0, &size, 0);
15461541Srgrimes		break;
15471541Srgrimes
15481541Srgrimes	case KINFO_CLOCKRATE:
154912171Sphk		name[0] = CTL_KERN;
155012171Sphk		name[1] = KERN_CLOCKRATE;
155183366Sjulian		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1552136417Sphk			0, 0, 0, &size, 0);
15531541Srgrimes		break;
15541541Srgrimes
15559455Speter	case KINFO_BSDI_SYSINFO: {
15569455Speter		/*
15579455Speter		 * this is pretty crude, but it's just enough for uname()
15589455Speter		 * from BSDI's 1.x libc to work.
15599455Speter		 *
156082494Speter		 * *size gives the size of the buffer before the call, and
156182494Speter		 * the amount of data copied after a successful call.
156282494Speter		 * If successful, the return value is the amount of data
156382494Speter		 * available, which can be larger than *size.
156482494Speter		 *
156582494Speter		 * BSDI's 2.x product apparently fails with ENOMEM if *size
156682494Speter		 * is too small.
15679455Speter		 */
15689455Speter
15699455Speter		u_int left;
15709455Speter		char *s;
15719455Speter
15729455Speter		bzero((char *)&bsdi_si, sizeof(bsdi_si));
15739455Speter		bzero(bsdi_strings, sizeof(bsdi_strings));
15749455Speter
15759455Speter		s = bsdi_strings;
15769455Speter
15779455Speter		bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
15789455Speter		strcpy(s, ostype);
15799455Speter		s += strlen(s) + 1;
15809455Speter
15819455Speter		bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
15829455Speter		strcpy(s, osrelease);
15839455Speter		s += strlen(s) + 1;
15849455Speter
15859455Speter		bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
15869455Speter		strcpy(s, machine);
15879455Speter		s += strlen(s) + 1;
15889455Speter
15899455Speter		needed = sizeof(bsdi_si) + (s - bsdi_strings);
15909455Speter
159182494Speter		if ((uap->where == NULL) || (uap->size == NULL)) {
15929455Speter			/* process is asking how much buffer to supply.. */
15939455Speter			size = needed;
15949455Speter			error = 0;
15959455Speter			break;
15969455Speter		}
15979455Speter
159882494Speter		if ((error = copyin(uap->size, &size, sizeof(size))) != 0)
159982494Speter			break;
16009455Speter
16019455Speter		/* if too much buffer supplied, trim it down */
16029455Speter		if (size > needed)
16039455Speter			size = needed;
16049455Speter
16059455Speter		/* how much of the buffer is remaining */
16069455Speter		left = size;
16079455Speter
16089455Speter		if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
16099455Speter			break;
16109455Speter
16119455Speter		/* is there any point in continuing? */
16129455Speter		if (left > sizeof(bsdi_si)) {
16139455Speter			left -= sizeof(bsdi_si);
16149455Speter			error = copyout(&bsdi_strings,
16159455Speter					uap->where + sizeof(bsdi_si), left);
16169455Speter		}
16179455Speter		break;
16189455Speter	}
16199455Speter
16201541Srgrimes	default:
162182746Sdillon		error = EOPNOTSUPP;
162282746Sdillon		break;
16231541Srgrimes	}
162482746Sdillon	if (error == 0) {
162583366Sjulian		td->td_retval[0] = needed ? needed : size;
162682746Sdillon		if (uap->size) {
162799012Salfred			error = copyout(&size, uap->size, sizeof(size));
162882746Sdillon		}
162982746Sdillon	}
163082746Sdillon	mtx_unlock(&Giant);
16311541Srgrimes	return (error);
16321541Srgrimes}
16331541Srgrimes#endif /* COMPAT_43 */
1634