kern_sysctl.c revision 174113
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 174113 2007-11-30 21:29:08Z peter $");
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/*
418174113Speter * Rename an existing oid.
419174113Speter */
420174113Spetervoid
421174113Spetersysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
422174113Speter{
423174113Speter	ssize_t len;
424174113Speter	char *newname;
425174113Speter	void *oldname;
426174113Speter
427174113Speter	oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
428174113Speter	len = strlen(name);
429174113Speter	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
430174113Speter	bcopy(name, newname, len + 1);
431174113Speter	newname[len] = '\0';
432174113Speter	oidp->oid_name = newname;
433174113Speter	free(oldname, M_SYSCTLOID);
434174113Speter}
435174113Speter
436174113Speter/*
437126319Sdes * Reparent an existing oid.
438126319Sdes */
439126319Sdesint
440126319Sdessysctl_move_oid(struct sysctl_oid *oid, struct sysctl_oid_list *parent)
441126319Sdes{
442126319Sdes	struct sysctl_oid *oidp;
443126319Sdes
444126319Sdes	if (oid->oid_parent == parent)
445126319Sdes		return (0);
446126319Sdes	oidp = sysctl_find_oidname(oid->oid_name, parent);
447126319Sdes	if (oidp != NULL)
448126319Sdes		return (EEXIST);
449126319Sdes	sysctl_unregister_oid(oid);
450126319Sdes	oid->oid_parent = parent;
451126319Sdes	oid->oid_number = OID_AUTO;
452126319Sdes	sysctl_register_oid(oid);
453126319Sdes	return (0);
454126319Sdes}
455126319Sdes
456126319Sdes/*
45744078Sdfr * Register the kernel's oids on startup.
45844078Sdfr */
45978161SpeterSET_DECLARE(sysctl_set, struct sysctl_oid);
46012152Sphk
46180338Sroamstatic void
46280338Sroamsysctl_register_all(void *arg)
46338869Sbde{
46478161Speter	struct sysctl_oid **oidp;
46578161Speter
46693625Srwatson	SYSCTL_INIT();
46778161Speter	SET_FOREACH(oidp, sysctl_set)
46878161Speter		sysctl_register_oid(*oidp);
46938869Sbde}
47044078SdfrSYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_register_all, 0);
47144078Sdfr
47212623Sphk/*
47312623Sphk * "Staff-functions"
47412623Sphk *
47512650Sphk * These functions implement a presently undocumented interface
47612650Sphk * used by the sysctl program to walk the tree, and get the type
47712650Sphk * so it can print the value.
47812650Sphk * This interface is under work and consideration, and should probably
47912650Sphk * be killed with a big axe by the first person who can find the time.
48012650Sphk * (be aware though, that the proper interface isn't as obvious as it
48112650Sphk * may seem, there are various conflicting requirements.
48212650Sphk *
48312623Sphk * {0,0}	printf the entire MIB-tree.
48412623Sphk * {0,1,...}	return the name of the "..." OID.
48542467Sphk * {0,2,...}	return the next OID.
48612623Sphk * {0,3}	return the OID of the name in "new"
48712650Sphk * {0,4,...}	return the kind & format info for the "..." OID.
48888006Sluigi * {0,5,...}	return the description the "..." OID.
48912623Sphk */
49012623Sphk
491136999Srwatson#ifdef SYSCTL_DEBUG
49212152Sphkstatic void
49344078Sdfrsysctl_sysctl_debug_dump_node(struct sysctl_oid_list *l, int i)
49412152Sphk{
49544078Sdfr	int k;
49644078Sdfr	struct sysctl_oid *oidp;
49712152Sphk
49844078Sdfr	SLIST_FOREACH(oidp, l, oid_link) {
49912152Sphk
50012152Sphk		for (k=0; k<i; k++)
50112152Sphk			printf(" ");
50212152Sphk
50344078Sdfr		printf("%d %s ", oidp->oid_number, oidp->oid_name);
50412152Sphk
50512152Sphk		printf("%c%c",
50644078Sdfr			oidp->oid_kind & CTLFLAG_RD ? 'R':' ',
50744078Sdfr			oidp->oid_kind & CTLFLAG_WR ? 'W':' ');
50812152Sphk
50944078Sdfr		if (oidp->oid_handler)
51015241Sphk			printf(" *Handler");
51115241Sphk
51244078Sdfr		switch (oidp->oid_kind & CTLTYPE) {
51312243Sphk			case CTLTYPE_NODE:
51415241Sphk				printf(" Node\n");
51544078Sdfr				if (!oidp->oid_handler) {
51612152Sphk					sysctl_sysctl_debug_dump_node(
51744078Sdfr						oidp->oid_arg1, i+2);
51812152Sphk				}
51912152Sphk				break;
52012152Sphk			case CTLTYPE_INT:    printf(" Int\n"); break;
52112152Sphk			case CTLTYPE_STRING: printf(" String\n"); break;
52212152Sphk			case CTLTYPE_QUAD:   printf(" Quad\n"); break;
52312152Sphk			case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
52412152Sphk			default:	     printf("\n");
52512152Sphk		}
52612152Sphk
52712152Sphk	}
52812152Sphk}
52912152Sphk
53012152Sphkstatic int
53162573Sphksysctl_sysctl_debug(SYSCTL_HANDLER_ARGS)
53212152Sphk{
53387024Speter	int error;
53487024Speter
535164033Srwatson	error = priv_check(req->td, PRIV_SYSCTL_DEBUG);
53687024Speter	if (error)
537139483Spjd		return (error);
53844078Sdfr	sysctl_sysctl_debug_dump_node(&sysctl__children, 0);
539139483Spjd	return (ENOENT);
54012152Sphk}
54112152Sphk
54212152SphkSYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
54312623Sphk	0, 0, sysctl_sysctl_debug, "-", "");
544136999Srwatson#endif
54512152Sphk
54612623Sphkstatic int
54762573Sphksysctl_sysctl_name(SYSCTL_HANDLER_ARGS)
54812623Sphk{
54912623Sphk	int *name = (int *) arg1;
55012623Sphk	u_int namelen = arg2;
55144078Sdfr	int error = 0;
55244078Sdfr	struct sysctl_oid *oid;
55344972Sphk	struct sysctl_oid_list *lsp = &sysctl__children, *lsp2;
55412623Sphk	char buf[10];
55512131Sphk
55612623Sphk	while (namelen) {
55712623Sphk		if (!lsp) {
55841514Sarchie			snprintf(buf,sizeof(buf),"%d",*name);
55912623Sphk			if (req->oldidx)
56012623Sphk				error = SYSCTL_OUT(req, ".", 1);
56112623Sphk			if (!error)
56212623Sphk				error = SYSCTL_OUT(req, buf, strlen(buf));
56312623Sphk			if (error)
56412623Sphk				return (error);
56512623Sphk			namelen--;
56612623Sphk			name++;
56712623Sphk			continue;
56812623Sphk		}
56944972Sphk		lsp2 = 0;
57044078Sdfr		SLIST_FOREACH(oid, lsp, oid_link) {
57144078Sdfr			if (oid->oid_number != *name)
57212623Sphk				continue;
57312131Sphk
57412623Sphk			if (req->oldidx)
57512623Sphk				error = SYSCTL_OUT(req, ".", 1);
57612623Sphk			if (!error)
57744078Sdfr				error = SYSCTL_OUT(req, oid->oid_name,
57844078Sdfr					strlen(oid->oid_name));
57912623Sphk			if (error)
58012623Sphk				return (error);
58112623Sphk
58212623Sphk			namelen--;
58312623Sphk			name++;
58412623Sphk
58544972Sphk			if ((oid->oid_kind & CTLTYPE) != CTLTYPE_NODE)
58612623Sphk				break;
58712623Sphk
58844078Sdfr			if (oid->oid_handler)
58912623Sphk				break;
59012623Sphk
59144972Sphk			lsp2 = (struct sysctl_oid_list *)oid->oid_arg1;
59212623Sphk			break;
59312623Sphk		}
59444972Sphk		lsp = lsp2;
59512623Sphk	}
59612623Sphk	return (SYSCTL_OUT(req, "", 1));
59712623Sphk}
59812623Sphk
599141626Sphkstatic SYSCTL_NODE(_sysctl, 1, name, CTLFLAG_RD, sysctl_sysctl_name, "");
60012623Sphk
60112623Sphkstatic int
60263978Spetersysctl_sysctl_next_ls(struct sysctl_oid_list *lsp, int *name, u_int namelen,
60344078Sdfr	int *next, int *len, int level, struct sysctl_oid **oidpp)
60412623Sphk{
60544078Sdfr	struct sysctl_oid *oidp;
60612623Sphk
60712623Sphk	*len = level;
60844078Sdfr	SLIST_FOREACH(oidp, lsp, oid_link) {
60944078Sdfr		*next = oidp->oid_number;
61044078Sdfr		*oidpp = oidp;
61112623Sphk
612101650Smux		if (oidp->oid_kind & CTLFLAG_SKIP)
613101650Smux			continue;
614101650Smux
61512623Sphk		if (!namelen) {
61644078Sdfr			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
617139483Spjd				return (0);
61844078Sdfr			if (oidp->oid_handler)
61912623Sphk				/* We really should call the handler here...*/
620139483Spjd				return (0);
62144078Sdfr			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
62263978Speter			if (!sysctl_sysctl_next_ls(lsp, 0, 0, next+1,
62344078Sdfr				len, level+1, oidpp))
624139483Spjd				return (0);
625111260Srwatson			goto emptynode;
62612623Sphk		}
62712623Sphk
62844078Sdfr		if (oidp->oid_number < *name)
62912623Sphk			continue;
63012623Sphk
63144078Sdfr		if (oidp->oid_number > *name) {
63244078Sdfr			if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
633139483Spjd				return (0);
63444078Sdfr			if (oidp->oid_handler)
635139483Spjd				return (0);
63644078Sdfr			lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
63763978Speter			if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1,
63844078Sdfr				next+1, len, level+1, oidpp))
63912623Sphk				return (0);
64015241Sphk			goto next;
64112623Sphk		}
64244078Sdfr		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
64312623Sphk			continue;
64412623Sphk
64544078Sdfr		if (oidp->oid_handler)
64612623Sphk			continue;
64712623Sphk
64844078Sdfr		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
64963978Speter		if (!sysctl_sysctl_next_ls(lsp, name+1, namelen-1, next+1,
65044078Sdfr			len, level+1, oidpp))
65112623Sphk			return (0);
65215241Sphk	next:
65312623Sphk		namelen = 1;
654111260Srwatson	emptynode:
65512623Sphk		*len = level;
65612623Sphk	}
657139483Spjd	return (1);
65812623Sphk}
65912623Sphk
66012623Sphkstatic int
66162573Sphksysctl_sysctl_next(SYSCTL_HANDLER_ARGS)
66212623Sphk{
66312623Sphk	int *name = (int *) arg1;
66412623Sphk	u_int namelen = arg2;
66512623Sphk	int i, j, error;
66612623Sphk	struct sysctl_oid *oid;
66744078Sdfr	struct sysctl_oid_list *lsp = &sysctl__children;
66812623Sphk	int newoid[CTL_MAXNAME];
66912623Sphk
67063978Speter	i = sysctl_sysctl_next_ls(lsp, name, namelen, newoid, &j, 1, &oid);
67112623Sphk	if (i)
672139483Spjd		return (ENOENT);
67312650Sphk	error = SYSCTL_OUT(req, newoid, j * sizeof (int));
67412623Sphk	return (error);
67512623Sphk}
67612623Sphk
677141626Sphkstatic SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD, sysctl_sysctl_next, "");
67812623Sphk
67912623Sphkstatic int
68044078Sdfrname2oid (char *name, int *oid, int *len, struct sysctl_oid **oidpp)
68112623Sphk{
68244078Sdfr	int i;
68344078Sdfr	struct sysctl_oid *oidp;
68444078Sdfr	struct sysctl_oid_list *lsp = &sysctl__children;
68512623Sphk	char *p;
68612623Sphk
68712623Sphk	if (!*name)
688139483Spjd		return (ENOENT);
68912623Sphk
69012623Sphk	p = name + strlen(name) - 1 ;
69112623Sphk	if (*p == '.')
69212623Sphk		*p = '\0';
69312623Sphk
69412623Sphk	*len = 0;
69512623Sphk
69612623Sphk	for (p = name; *p && *p != '.'; p++)
69712623Sphk		;
69812623Sphk	i = *p;
69912623Sphk	if (i == '.')
70012623Sphk		*p = '\0';
70112623Sphk
70244078Sdfr	oidp = SLIST_FIRST(lsp);
70312623Sphk
70444078Sdfr	while (oidp && *len < CTL_MAXNAME) {
70544078Sdfr		if (strcmp(name, oidp->oid_name)) {
70644078Sdfr			oidp = SLIST_NEXT(oidp, oid_link);
70712623Sphk			continue;
70812623Sphk		}
70944078Sdfr		*oid++ = oidp->oid_number;
71012623Sphk		(*len)++;
71112623Sphk
71212623Sphk		if (!i) {
71344078Sdfr			if (oidpp)
71444078Sdfr				*oidpp = oidp;
71512623Sphk			return (0);
71612623Sphk		}
71712623Sphk
71844078Sdfr		if ((oidp->oid_kind & CTLTYPE) != CTLTYPE_NODE)
71912623Sphk			break;
72012623Sphk
72144078Sdfr		if (oidp->oid_handler)
72212623Sphk			break;
72312623Sphk
72444078Sdfr		lsp = (struct sysctl_oid_list *)oidp->oid_arg1;
72544078Sdfr		oidp = SLIST_FIRST(lsp);
72612623Sphk		name = p+1;
72712623Sphk		for (p = name; *p && *p != '.'; p++)
72812623Sphk				;
72912623Sphk		i = *p;
73012623Sphk		if (i == '.')
73112623Sphk			*p = '\0';
73212623Sphk	}
733139483Spjd	return (ENOENT);
73412623Sphk}
73512623Sphk
73612623Sphkstatic int
73762573Sphksysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
73812623Sphk{
73912623Sphk	char *p;
74012623Sphk	int error, oid[CTL_MAXNAME], len;
74112623Sphk	struct sysctl_oid *op = 0;
74212623Sphk
74312623Sphk	if (!req->newlen)
744139483Spjd		return (ENOENT);
74545140Sphk	if (req->newlen >= MAXPATHLEN)	/* XXX arbitrary, undocumented */
74645140Sphk		return (ENAMETOOLONG);
74712623Sphk
748111119Simp	p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
74912623Sphk
75012623Sphk	error = SYSCTL_IN(req, p, req->newlen);
75112623Sphk	if (error) {
75212623Sphk		free(p, M_SYSCTL);
75312623Sphk		return (error);
75412623Sphk	}
75512623Sphk
75612623Sphk	p [req->newlen] = '\0';
75712623Sphk
75812623Sphk	error = name2oid(p, oid, &len, &op);
75912623Sphk
76012623Sphk	free(p, M_SYSCTL);
76112623Sphk
76212623Sphk	if (error)
76312623Sphk		return (error);
76412623Sphk
76512650Sphk	error = SYSCTL_OUT(req, oid, len * sizeof *oid);
76612623Sphk	return (error);
76712623Sphk}
76812623Sphk
76912910SphkSYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
77012623Sphk	sysctl_sysctl_name2oid, "I", "");
77112623Sphk
77212623Sphkstatic int
77362573Sphksysctl_sysctl_oidfmt(SYSCTL_HANDLER_ARGS)
77412623Sphk{
77544078Sdfr	struct sysctl_oid *oid;
77653977Sgreen	int error;
77712623Sphk
77853977Sgreen	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
77953977Sgreen	if (error)
78053977Sgreen		return (error);
78112623Sphk
78244078Sdfr	if (!oid->oid_fmt)
78353977Sgreen		return (ENOENT);
78453977Sgreen	error = SYSCTL_OUT(req, &oid->oid_kind, sizeof(oid->oid_kind));
78553977Sgreen	if (error)
78653977Sgreen		return (error);
78753977Sgreen	error = SYSCTL_OUT(req, oid->oid_fmt, strlen(oid->oid_fmt) + 1);
78812650Sphk	return (error);
78912623Sphk}
79012623Sphk
79142467Sphk
792141626Sphkstatic SYSCTL_NODE(_sysctl, 4, oidfmt, CTLFLAG_RD, sysctl_sysctl_oidfmt, "");
79312623Sphk
79488006Sluigistatic int
79588006Sluigisysctl_sysctl_oiddescr(SYSCTL_HANDLER_ARGS)
79688006Sluigi{
79788006Sluigi	struct sysctl_oid *oid;
79888006Sluigi	int error;
79988006Sluigi
80088006Sluigi	error = sysctl_find_oid(arg1, arg2, &oid, NULL, req);
80188006Sluigi	if (error)
80288006Sluigi		return (error);
80388006Sluigi
804141433Sphk	if (!oid->oid_descr)
80588006Sluigi		return (ENOENT);
806141433Sphk	error = SYSCTL_OUT(req, oid->oid_descr, strlen(oid->oid_descr) + 1);
80788006Sluigi	return (error);
80888006Sluigi}
80988006Sluigi
810141626Sphkstatic SYSCTL_NODE(_sysctl, 5, oiddescr, CTLFLAG_RD, sysctl_sysctl_oiddescr, "");
81188006Sluigi
81212243Sphk/*
81312623Sphk * Default "handler" functions.
81412623Sphk */
81512623Sphk
81612623Sphk/*
81742095Sdfr * Handle an int, signed or unsigned.
81812243Sphk * Two cases:
81912243Sphk *     a variable:  point arg1 at it.
82012243Sphk *     a constant:  pass it in arg2.
82112243Sphk */
82212243Sphk
82311865Sphkint
82462573Sphksysctl_handle_int(SYSCTL_HANDLER_ARGS)
82511863Sphk{
826100833Struckman	int tmpout, error = 0;
82711863Sphk
828100833Struckman	/*
829100833Struckman	 * Attempt to get a coherent snapshot by making a copy of the data.
830100833Struckman	 */
83112243Sphk	if (arg1)
832100833Struckman		tmpout = *(int *)arg1;
83320506Sbde	else
834100833Struckman		tmpout = arg2;
835100833Struckman	error = SYSCTL_OUT(req, &tmpout, sizeof(int));
83611863Sphk
83712243Sphk	if (error || !req->newptr)
83812243Sphk		return (error);
83911863Sphk
84012243Sphk	if (!arg1)
84112243Sphk		error = EPERM;
84212243Sphk	else
84312243Sphk		error = SYSCTL_IN(req, arg1, sizeof(int));
84412243Sphk	return (error);
84511863Sphk}
84611863Sphk
847155758Sandre
84812243Sphk/*
849155758Sandre * Based on on sysctl_handle_int() convert milliseconds into ticks.
850155758Sandre */
851155758Sandre
852155758Sandreint
853155758Sandresysctl_msec_to_ticks(SYSCTL_HANDLER_ARGS)
854155758Sandre{
855155758Sandre	int error, s, tt;
856155758Sandre
857155758Sandre	tt = *(int *)oidp->oid_arg1;
858155758Sandre	s = (int)((int64_t)tt * 1000 / hz);
859155758Sandre
860155758Sandre	error = sysctl_handle_int(oidp, &s, 0, req);
861155758Sandre	if (error || !req->newptr)
862155758Sandre		return (error);
863155758Sandre
864155758Sandre	tt = (int)((int64_t)s * hz / 1000);
865155758Sandre	if (tt < 1)
866155758Sandre		return (EINVAL);
867155758Sandre
868155758Sandre	*(int *)oidp->oid_arg1 = tt;
869155758Sandre	return (0);
870155758Sandre}
871155758Sandre
872155758Sandre
873155758Sandre/*
87445140Sphk * Handle a long, signed or unsigned.  arg1 points to it.
87538517Sdfr */
87638517Sdfr
87738517Sdfrint
87862573Sphksysctl_handle_long(SYSCTL_HANDLER_ARGS)
87938517Sdfr{
88038517Sdfr	int error = 0;
881136404Speter	long tmplong;
882136404Speter#ifdef SCTL_MASK32
883136404Speter	int tmpint;
884136404Speter#endif
88538517Sdfr
886100833Struckman	/*
887100833Struckman	 * Attempt to get a coherent snapshot by making a copy of the data.
888100833Struckman	 */
88945140Sphk	if (!arg1)
89045140Sphk		return (EINVAL);
891136404Speter	tmplong = *(long *)arg1;
892136404Speter#ifdef SCTL_MASK32
893136404Speter	if (req->flags & SCTL_MASK32) {
894136404Speter		tmpint = tmplong;
895136404Speter		error = SYSCTL_OUT(req, &tmpint, sizeof(int));
896136404Speter	} else
897136404Speter#endif
898136404Speter		error = SYSCTL_OUT(req, &tmplong, sizeof(long));
89938517Sdfr
90038517Sdfr	if (error || !req->newptr)
90138517Sdfr		return (error);
90238517Sdfr
903136404Speter#ifdef SCTL_MASK32
904136404Speter	if (req->flags & SCTL_MASK32) {
905136404Speter		error = SYSCTL_IN(req, &tmpint, sizeof(int));
906136404Speter		*(long *)arg1 = (long)tmpint;
907136404Speter	} else
908136404Speter#endif
909136404Speter		error = SYSCTL_IN(req, arg1, sizeof(long));
91038517Sdfr	return (error);
91138517Sdfr}
91238517Sdfr
91338517Sdfr/*
914170288Sdwmalone * Handle a 64 bit int, signed or unsigned.  arg1 points to it.
915170288Sdwmalone */
916170288Sdwmalone
917170288Sdwmaloneint
918170288Sdwmalonesysctl_handle_quad(SYSCTL_HANDLER_ARGS)
919170288Sdwmalone{
920170288Sdwmalone	int error = 0;
921170288Sdwmalone	uint64_t tmpout;
922170288Sdwmalone
923170288Sdwmalone	/*
924170288Sdwmalone	 * Attempt to get a coherent snapshot by making a copy of the data.
925170288Sdwmalone	 */
926170288Sdwmalone	if (!arg1)
927170288Sdwmalone		return (EINVAL);
928170288Sdwmalone	tmpout = *(uint64_t *)arg1;
929170288Sdwmalone	error = SYSCTL_OUT(req, &tmpout, sizeof(uint64_t));
930170288Sdwmalone
931170288Sdwmalone	if (error || !req->newptr)
932170288Sdwmalone		return (error);
933170288Sdwmalone
934170288Sdwmalone	error = SYSCTL_IN(req, arg1, sizeof(uint64_t));
935170288Sdwmalone	return (error);
936170288Sdwmalone}
937170288Sdwmalone
938170288Sdwmalone/*
93912243Sphk * Handle our generic '\0' terminated 'C' string.
94012243Sphk * Two cases:
94112243Sphk * 	a variable string:  point arg1 at it, arg2 is max length.
94212243Sphk * 	a constant string:  point arg1 at it, arg2 is zero.
94312243Sphk */
94412243Sphk
94511865Sphkint
94662573Sphksysctl_handle_string(SYSCTL_HANDLER_ARGS)
94711863Sphk{
94812243Sphk	int error=0;
949100833Struckman	char *tmparg;
950100833Struckman	size_t outlen;
95111863Sphk
952100833Struckman	/*
953100833Struckman	 * Attempt to get a coherent snapshot by copying to a
954100833Struckman	 * temporary kernel buffer.
955100833Struckman	 */
956100833Struckmanretry:
957100833Struckman	outlen = strlen((char *)arg1)+1;
958111119Simp	tmparg = malloc(outlen, M_SYSCTLTMP, M_WAITOK);
959105354Srobert
960105354Srobert	if (strlcpy(tmparg, (char *)arg1, outlen) >= outlen) {
961100833Struckman		free(tmparg, M_SYSCTLTMP);
962100833Struckman		goto retry;
963100833Struckman	}
964105354Srobert
965100833Struckman	error = SYSCTL_OUT(req, tmparg, outlen);
966100833Struckman	free(tmparg, M_SYSCTLTMP);
96711863Sphk
96845140Sphk	if (error || !req->newptr)
96912243Sphk		return (error);
97011863Sphk
97145140Sphk	if ((req->newlen - req->newidx) >= arg2) {
97245140Sphk		error = EINVAL;
97312243Sphk	} else {
97412243Sphk		arg2 = (req->newlen - req->newidx);
97512243Sphk		error = SYSCTL_IN(req, arg1, arg2);
97612243Sphk		((char *)arg1)[arg2] = '\0';
97711863Sphk	}
97812131Sphk
97912131Sphk	return (error);
98011863Sphk}
98111863Sphk
98212243Sphk/*
98312243Sphk * Handle any kind of opaque data.
98412243Sphk * arg1 points to it, arg2 is the size.
98512243Sphk */
98612243Sphk
98711865Sphkint
98862573Sphksysctl_handle_opaque(SYSCTL_HANDLER_ARGS)
98911863Sphk{
990120803Sbms	int error, tries;
991120803Sbms	u_int generation;
992120813Sbms	struct sysctl_req req2;
99312243Sphk
994100833Struckman	/*
995120803Sbms	 * Attempt to get a coherent snapshot, by using the thread
996120803Sbms	 * pre-emption counter updated from within mi_switch() to
997120803Sbms	 * determine if we were pre-empted during a bcopy() or
998120803Sbms	 * copyout(). Make 3 attempts at doing this before giving up.
999120803Sbms	 * If we encounter an error, stop immediately.
1000100833Struckman	 */
1001120803Sbms	tries = 0;
1002120813Sbms	req2 = *req;
1003120813Sbmsretry:
1004120813Sbms	generation = curthread->td_generation;
1005120813Sbms	error = SYSCTL_OUT(req, arg1, arg2);
1006120813Sbms	if (error)
1007120813Sbms		return (error);
1008120813Sbms	tries++;
1009120813Sbms	if (generation != curthread->td_generation && tries < 3) {
1010120813Sbms		*req = req2;
1011120813Sbms		goto retry;
1012120813Sbms	}
101312243Sphk
101412243Sphk	error = SYSCTL_IN(req, arg1, arg2);
101512243Sphk
101612243Sphk	return (error);
101712243Sphk}
101812243Sphk
101912260Sphk/*
102012260Sphk * Transfer functions to/from kernel space.
102112260Sphk * XXX: rather untested at this point
102212260Sphk */
102312260Sphkstatic int
102438517Sdfrsysctl_old_kernel(struct sysctl_req *req, const void *p, size_t l)
102512243Sphk{
102638517Sdfr	size_t i = 0;
102712260Sphk
102812260Sphk	if (req->oldptr) {
102938517Sdfr		i = l;
103073971Stmm		if (req->oldlen <= req->oldidx)
103173971Stmm			i = 0;
103273971Stmm		else
103373971Stmm			if (i > req->oldlen - req->oldidx)
103473971Stmm				i = req->oldlen - req->oldidx;
103512260Sphk		if (i > 0)
103617971Sbde			bcopy(p, (char *)req->oldptr + req->oldidx, i);
103712243Sphk	}
103812260Sphk	req->oldidx += l;
103916282Snate	if (req->oldptr && i != l)
104011863Sphk		return (ENOMEM);
104112260Sphk	return (0);
104212243Sphk}
104312243Sphk
104412260Sphkstatic int
104538517Sdfrsysctl_new_kernel(struct sysctl_req *req, void *p, size_t l)
104612243Sphk{
104712260Sphk	if (!req->newptr)
1048139483Spjd		return (0);
104912260Sphk	if (req->newlen - req->newidx < l)
105011863Sphk		return (EINVAL);
105117971Sbde	bcopy((char *)req->newptr + req->newidx, p, l);
105212243Sphk	req->newidx += l;
105312131Sphk	return (0);
105411863Sphk}
105511863Sphk
105616282Snateint
105783366Sjuliankernel_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1058136404Speter    size_t *oldlenp, void *new, size_t newlen, size_t *retval, int flags)
105916282Snate{
106016282Snate	int error = 0;
106116282Snate	struct sysctl_req req;
106216282Snate
106316282Snate	bzero(&req, sizeof req);
106416282Snate
106586183Srwatson	req.td = td;
1066136404Speter	req.flags = flags;
106716282Snate
106816282Snate	if (oldlenp) {
106916282Snate		req.oldlen = *oldlenp;
107016282Snate	}
1071127052Struckman	req.validlen = req.oldlen;
107216282Snate
107316282Snate	if (old) {
107416282Snate		req.oldptr= old;
107516282Snate	}
107616282Snate
107777646Sdd	if (new != NULL) {
107816282Snate		req.newlen = newlen;
107916282Snate		req.newptr = new;
108016282Snate	}
108116282Snate
108216282Snate	req.oldfunc = sysctl_old_kernel;
108316282Snate	req.newfunc = sysctl_new_kernel;
1084120781Sbms	req.lock = REQ_LOCKED;
108516282Snate
108693625Srwatson	SYSCTL_LOCK();
108716282Snate
108816282Snate	error = sysctl_root(0, name, namelen, &req);
1089120813Sbms
1090127052Struckman	if (req.lock == REQ_WIRED && req.validlen > 0)
1091127052Struckman		vsunlock(req.oldptr, req.validlen);
109216282Snate
109393625Srwatson	SYSCTL_UNLOCK();
109416282Snate
109516282Snate	if (error && error != ENOMEM)
109616282Snate		return (error);
109716282Snate
109816282Snate	if (retval) {
1099127052Struckman		if (req.oldptr && req.oldidx > req.validlen)
1100127052Struckman			*retval = req.validlen;
110116282Snate		else
110216282Snate			*retval = req.oldidx;
110316282Snate	}
110416282Snate	return (error);
110516282Snate}
110616282Snate
110776834Sjlemonint
110883366Sjuliankernel_sysctlbyname(struct thread *td, char *name, void *old, size_t *oldlenp,
1109136404Speter    void *new, size_t newlen, size_t *retval, int flags)
111076834Sjlemon{
111176834Sjlemon        int oid[CTL_MAXNAME];
111278620Smjacob        size_t oidlen, plen;
111378620Smjacob	int error;
111476834Sjlemon
111576834Sjlemon	oid[0] = 0;		/* sysctl internal magic */
111676834Sjlemon	oid[1] = 3;		/* name2oid */
111776834Sjlemon	oidlen = sizeof(oid);
111876834Sjlemon
111983366Sjulian	error = kernel_sysctl(td, oid, 2, oid, &oidlen,
1120136404Speter	    (void *)name, strlen(name), &plen, flags);
112176834Sjlemon	if (error)
112276834Sjlemon		return (error);
112376834Sjlemon
112483366Sjulian	error = kernel_sysctl(td, oid, plen / sizeof(int), old, oldlenp,
1125136404Speter	    new, newlen, retval, flags);
112676834Sjlemon	return (error);
112776834Sjlemon}
112876834Sjlemon
112912260Sphk/*
113012260Sphk * Transfer function to/from user space.
113112260Sphk */
113212260Sphkstatic int
113338517Sdfrsysctl_old_user(struct sysctl_req *req, const void *p, size_t l)
113412243Sphk{
113538517Sdfr	int error = 0;
1136126253Struckman	size_t i, len, origidx;
113712243Sphk
1138126253Struckman	origidx = req->oldidx;
1139126253Struckman	req->oldidx += l;
1140126253Struckman	if (req->oldptr == NULL)
1141126253Struckman		return (0);
1142148864Scsjp	/*
1143148864Scsjp	 * If we have not wired the user supplied buffer and we are currently
1144148864Scsjp	 * holding locks, drop a witness warning, as it's possible that
1145148864Scsjp	 * write operations to the user page can sleep.
1146148864Scsjp	 */
1147148864Scsjp	if (req->lock != REQ_WIRED)
1148111883Sjhb		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1149111883Sjhb		    "sysctl_old_user()");
1150126253Struckman	i = l;
1151127052Struckman	len = req->validlen;
1152126253Struckman	if (len <= origidx)
1153126253Struckman		i = 0;
1154126253Struckman	else {
1155126253Struckman		if (i > len - origidx)
1156126253Struckman			i = len - origidx;
1157126253Struckman		error = copyout(p, (char *)req->oldptr + origidx, i);
115812260Sphk	}
115912243Sphk	if (error)
116012243Sphk		return (error);
1161126253Struckman	if (i < l)
116212243Sphk		return (ENOMEM);
116312260Sphk	return (0);
116412243Sphk}
116512243Sphk
116612260Sphkstatic int
116738517Sdfrsysctl_new_user(struct sysctl_req *req, void *p, size_t l)
116812243Sphk{
116912285Sphk	int error;
117012260Sphk
117112260Sphk	if (!req->newptr)
1172139483Spjd		return (0);
117312260Sphk	if (req->newlen - req->newidx < l)
117412243Sphk		return (EINVAL);
1175148873Scsjp	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1176148873Scsjp	    "sysctl_new_user()");
117717971Sbde	error = copyin((char *)req->newptr + req->newidx, p, l);
117812243Sphk	req->newidx += l;
117912243Sphk	return (error);
118012243Sphk}
118112243Sphk
1182100487Struckman/*
1183100487Struckman * Wire the user space destination buffer.  If set to a value greater than
1184100487Struckman * zero, the len parameter limits the maximum amount of wired memory.
1185100487Struckman */
1186126253Struckmanint
1187100487Struckmansysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
1188100487Struckman{
1189126253Struckman	int ret;
1190154792Struckman	size_t i, wiredlen;
1191154792Struckman	char *cp, dummy;
1192126253Struckman
1193126253Struckman	wiredlen = (len > 0 && len < req->oldlen) ? len : req->oldlen;
1194126253Struckman	ret = 0;
1195120781Sbms	if (req->lock == REQ_LOCKED && req->oldptr &&
1196120781Sbms	    req->oldfunc == sysctl_old_user) {
1197127050Struckman		if (wiredlen != 0) {
1198127050Struckman			ret = vslock(req->oldptr, wiredlen);
1199130327Sgreen			if (ret != 0) {
1200130327Sgreen				if (ret != ENOMEM)
1201130327Sgreen					return (ret);
1202130327Sgreen				wiredlen = 0;
1203130327Sgreen			}
1204154792Struckman			/*
1205154792Struckman			 * Touch all the wired pages to avoid PTE modified
1206154792Struckman			 * bit emulation traps on Alpha while holding locks
1207154792Struckman			 * in the sysctl handler.
1208154792Struckman			 */
1209154792Struckman			for (i = (wiredlen + PAGE_SIZE - 1) / PAGE_SIZE,
1210154792Struckman			    cp = req->oldptr; i > 0; i--, cp += PAGE_SIZE) {
1211154792Struckman				copyin(cp, &dummy, 1);
1212154792Struckman				copyout(&dummy, cp, 1);
1213154792Struckman			}
1214126253Struckman		}
1215127050Struckman		req->lock = REQ_WIRED;
1216127052Struckman		req->validlen = wiredlen;
1217100487Struckman	}
1218127050Struckman	return (0);
1219100487Struckman}
1220100487Struckman
12211541Srgrimesint
122253977Sgreensysctl_find_oid(int *name, u_int namelen, struct sysctl_oid **noid,
122353977Sgreen    int *nindx, struct sysctl_req *req)
122412131Sphk{
122544078Sdfr	struct sysctl_oid *oid;
122653977Sgreen	int indx;
122712131Sphk
122853977Sgreen	oid = SLIST_FIRST(&sysctl__children);
122912131Sphk	indx = 0;
123044078Sdfr	while (oid && indx < CTL_MAXNAME) {
123144078Sdfr		if (oid->oid_number == name[indx]) {
123212131Sphk			indx++;
123344078Sdfr			if (oid->oid_kind & CTLFLAG_NOLOCK)
1234120781Sbms				req->lock = REQ_UNLOCKED;
123544078Sdfr			if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
123653977Sgreen				if (oid->oid_handler != NULL ||
123753977Sgreen				    indx == namelen) {
123853977Sgreen					*noid = oid;
123953977Sgreen					if (nindx != NULL)
124053977Sgreen						*nindx = indx;
124153977Sgreen					return (0);
124253977Sgreen				}
124353977Sgreen				oid = SLIST_FIRST(
124453977Sgreen				    (struct sysctl_oid_list *)oid->oid_arg1);
124553977Sgreen			} else if (indx == namelen) {
124653977Sgreen				*noid = oid;
124753977Sgreen				if (nindx != NULL)
124853977Sgreen					*nindx = indx;
124953977Sgreen				return (0);
125012131Sphk			} else {
125153977Sgreen				return (ENOTDIR);
125212131Sphk			}
125312131Sphk		} else {
125444078Sdfr			oid = SLIST_NEXT(oid, oid_link);
125512131Sphk		}
125612131Sphk	}
125753977Sgreen	return (ENOENT);
125853977Sgreen}
125953977Sgreen
126053977Sgreen/*
126153977Sgreen * Traverse our tree, and find the right node, execute whatever it points
126253977Sgreen * to, and return the resulting error code.
126353977Sgreen */
126453977Sgreen
1265104094Sphkstatic int
126662573Sphksysctl_root(SYSCTL_HANDLER_ARGS)
126753977Sgreen{
126853977Sgreen	struct sysctl_oid *oid;
1269109246Sdillon	int error, indx, lvl;
127053977Sgreen
127153977Sgreen	error = sysctl_find_oid(arg1, arg2, &oid, &indx, req);
127253977Sgreen	if (error)
127353977Sgreen		return (error);
127453977Sgreen
127553977Sgreen	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
127653977Sgreen		/*
127753977Sgreen		 * You can't call a sysctl when it's a node, but has
127853977Sgreen		 * no handler.  Inform the user that it's a node.
127953977Sgreen		 * The indx may or may not be the same as namelen.
128053977Sgreen		 */
128153977Sgreen		if (oid->oid_handler == NULL)
128253977Sgreen			return (EISDIR);
128353977Sgreen	}
128453977Sgreen
128583968Srwatson	/* Is this sysctl writable? */
128683968Srwatson	if (req->newptr && !(oid->oid_kind & CTLFLAG_WR))
128712131Sphk		return (EPERM);
128812131Sphk
128992953Srwatson	KASSERT(req->td != NULL, ("sysctl_root(): req->td == NULL"));
129092953Srwatson
129183968Srwatson	/* Is this sysctl sensitive to securelevels? */
129283968Srwatson	if (req->newptr && (oid->oid_kind & CTLFLAG_SECURE)) {
1293109246Sdillon		lvl = (oid->oid_kind & CTLMASK_SECURE) >> CTLSHIFT_SECURE;
1294109246Sdillon		error = securelevel_gt(req->td->td_ucred, lvl);
129592953Srwatson		if (error)
129692953Srwatson			return (error);
129783968Srwatson	}
129812910Sphk
129983968Srwatson	/* Is this sysctl writable by only privileged users? */
130083968Srwatson	if (req->newptr && !(oid->oid_kind & CTLFLAG_ANYBODY)) {
130192953Srwatson		if (oid->oid_kind & CTLFLAG_PRISON)
1302170587Srwatson			error = priv_check(req->td, PRIV_SYSCTL_WRITEJAIL);
130392953Srwatson		else
1304164033Srwatson			error = priv_check(req->td, PRIV_SYSCTL_WRITE);
130592953Srwatson		if (error)
130692953Srwatson			return (error);
130783968Srwatson	}
130883968Srwatson
130944078Sdfr	if (!oid->oid_handler)
1310139483Spjd		return (EINVAL);
131112131Sphk
1312126121Spjd	if ((oid->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
1313132776Skan		arg1 = (int *)arg1 + indx;
1314126121Spjd		arg2 -= indx;
1315126121Spjd	} else {
1316126121Spjd		arg1 = oid->oid_arg1;
1317126121Spjd		arg2 = oid->oid_arg2;
1318126121Spjd	}
1319126121Spjd#ifdef MAC
1320172930Srwatson	error = mac_system_check_sysctl(req->td->td_ucred, oid, arg1, arg2,
1321126121Spjd	    req);
1322126121Spjd	if (error != 0)
1323126121Spjd		return (error);
1324126121Spjd#endif
1325126121Spjd	error = oid->oid_handler(oid, arg1, arg2, req);
1326126121Spjd
132753977Sgreen	return (error);
132812131Sphk}
132912131Sphk
133012221Sbde#ifndef _SYS_SYSPROTO_H_
133112171Sphkstruct sysctl_args {
133212171Sphk	int	*name;
133312171Sphk	u_int	namelen;
133412171Sphk	void	*old;
133512171Sphk	size_t	*oldlenp;
133612171Sphk	void	*new;
133712171Sphk	size_t	newlen;
133812171Sphk};
133912221Sbde#endif
134012131Sphkint
134183366Sjulian__sysctl(struct thread *td, struct sysctl_args *uap)
13421541Srgrimes{
134382746Sdillon	int error, name[CTL_MAXNAME];
134438517Sdfr	size_t j;
13451541Srgrimes
13461541Srgrimes	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
13471541Srgrimes		return (EINVAL);
134811863Sphk
13493308Sphk 	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
13503308Sphk 	if (error)
13511541Srgrimes		return (error);
13521541Srgrimes
135382746Sdillon	mtx_lock(&Giant);
135482746Sdillon
135583366Sjulian	error = userland_sysctl(td, name, uap->namelen,
135612171Sphk		uap->old, uap->oldlenp, 0,
1357136404Speter		uap->new, uap->newlen, &j, 0);
135812260Sphk	if (error && error != ENOMEM)
135982746Sdillon		goto done2;
136012260Sphk	if (uap->oldlenp) {
136182746Sdillon		int i = copyout(&j, uap->oldlenp, sizeof(j));
136212260Sphk		if (i)
136382746Sdillon			error = i;
136412260Sphk	}
136582746Sdillondone2:
136682746Sdillon	mtx_unlock(&Giant);
136712260Sphk	return (error);
136812171Sphk}
136912171Sphk
137012171Sphk/*
137112171Sphk * This is used from various compatibility syscalls too.  That's why name
137212171Sphk * must be in kernel space.
137312171Sphk */
137412171Sphkint
137583366Sjulianuserland_sysctl(struct thread *td, int *name, u_int namelen, void *old,
1376136404Speter    size_t *oldlenp, int inkernel, void *new, size_t newlen, size_t *retval,
1377136404Speter    int flags)
137812171Sphk{
137912429Sphk	int error = 0;
1380127052Struckman	struct sysctl_req req;
138112171Sphk
138212243Sphk	bzero(&req, sizeof req);
138312243Sphk
138486183Srwatson	req.td = td;
1385136404Speter	req.flags = flags;
138612285Sphk
138712171Sphk	if (oldlenp) {
138812171Sphk		if (inkernel) {
138912243Sphk			req.oldlen = *oldlenp;
139012171Sphk		} else {
139112260Sphk			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
139212171Sphk			if (error)
139312171Sphk				return (error);
139412171Sphk		}
139512171Sphk	}
1396127052Struckman	req.validlen = req.oldlen;
139712171Sphk
139812243Sphk	if (old) {
139952644Sphk		if (!useracc(old, req.oldlen, VM_PROT_WRITE))
140012243Sphk			return (EFAULT);
140112243Sphk		req.oldptr= old;
140212243Sphk	}
140312131Sphk
140477646Sdd	if (new != NULL) {
1405172038Srwatson		if (!useracc(new, newlen, VM_PROT_READ))
140612243Sphk			return (EFAULT);
140712243Sphk		req.newlen = newlen;
140812243Sphk		req.newptr = new;
140911863Sphk	}
141012131Sphk
141112243Sphk	req.oldfunc = sysctl_old_user;
141212243Sphk	req.newfunc = sysctl_new_user;
1413120781Sbms	req.lock = REQ_LOCKED;
141411863Sphk
141593625Srwatson	SYSCTL_LOCK();
141612429Sphk
141716159Sphk	do {
1418127052Struckman		req.oldidx = 0;
1419127052Struckman		req.newidx = 0;
1420127052Struckman		error = sysctl_root(0, name, namelen, &req);
142116159Sphk	} while (error == EAGAIN);
142212243Sphk
1423127052Struckman	if (req.lock == REQ_WIRED && req.validlen > 0)
1424127052Struckman		vsunlock(req.oldptr, req.validlen);
142512429Sphk
142693625Srwatson	SYSCTL_UNLOCK();
142712429Sphk
142812260Sphk	if (error && error != ENOMEM)
142912260Sphk		return (error);
143012260Sphk
143112260Sphk	if (retval) {
1432127052Struckman		if (req.oldptr && req.oldidx > req.validlen)
1433127052Struckman			*retval = req.validlen;
143412260Sphk		else
143512260Sphk			*retval = req.oldidx;
143611863Sphk	}
143712260Sphk	return (error);
14381541Srgrimes}
14391541Srgrimes
14401541Srgrimes#ifdef COMPAT_43
14411541Srgrimes#include <sys/socket.h>
144215103Sphk#include <vm/vm_param.h>
144315103Sphk
14441541Srgrimes#define	KINFO_PROC		(0<<8)
14451541Srgrimes#define	KINFO_RT		(1<<8)
14461541Srgrimes#define	KINFO_VNODE		(2<<8)
14471541Srgrimes#define	KINFO_FILE		(3<<8)
14481541Srgrimes#define	KINFO_METER		(4<<8)
14491541Srgrimes#define	KINFO_LOADAVG		(5<<8)
14501541Srgrimes#define	KINFO_CLOCKRATE		(6<<8)
14511541Srgrimes
14529455Speter/* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
14539455Speter#define	KINFO_BSDI_SYSINFO	(101<<8)
14549455Speter
14559455Speter/*
14569455Speter * XXX this is bloat, but I hope it's better here than on the potentially
14579455Speter * limited kernel stack...  -Peter
14589455Speter */
14599455Speter
146012819Sphkstatic struct {
14619455Speter	int	bsdi_machine;		/* "i386" on BSD/386 */
14629455Speter/*      ^^^ this is an offset to the string, relative to the struct start */
14639455Speter	char	*pad0;
14649455Speter	long	pad1;
14659455Speter	long	pad2;
14669455Speter	long	pad3;
14679455Speter	u_long	pad4;
14689455Speter	u_long	pad5;
14699455Speter	u_long	pad6;
14709455Speter
14719455Speter	int	bsdi_ostype;		/* "BSD/386" on BSD/386 */
14729455Speter	int	bsdi_osrelease;		/* "1.1" on BSD/386 */
14739455Speter	long	pad7;
14749455Speter	long	pad8;
14759455Speter	char	*pad9;
14769455Speter
14779455Speter	long	pad10;
14789455Speter	long	pad11;
14799455Speter	int	pad12;
14809455Speter	long	pad13;
14819455Speter	quad_t	pad14;
14829455Speter	long	pad15;
14839455Speter
14849455Speter	struct	timeval pad16;
14859455Speter	/* we dont set this, because BSDI's uname used gethostname() instead */
14869455Speter	int	bsdi_hostname;		/* hostname on BSD/386 */
14879455Speter
14889455Speter	/* the actual string data is appended here */
14899455Speter
14909455Speter} bsdi_si;
1491167232Srwatson
14929455Speter/*
14939455Speter * this data is appended to the end of the bsdi_si structure during copyout.
14949455Speter * The "char *" offsets are relative to the base of the bsdi_si struct.
14959455Speter * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
14969455Speter * should not exceed the length of the buffer here... (or else!! :-)
14979455Speter */
149812819Sphkstatic char bsdi_strings[80];	/* It had better be less than this! */
14999455Speter
150012221Sbde#ifndef _SYS_SYSPROTO_H_
15011541Srgrimesstruct getkerninfo_args {
15021541Srgrimes	int	op;
15031541Srgrimes	char	*where;
150438864Sbde	size_t	*size;
15051541Srgrimes	int	arg;
15061541Srgrimes};
150712221Sbde#endif
15081549Srgrimesint
150983366Sjulianogetkerninfo(struct thread *td, struct getkerninfo_args *uap)
15101541Srgrimes{
151112171Sphk	int error, name[6];
151238517Sdfr	size_t size;
151382494Speter	u_int needed = 0;
15141541Srgrimes
151582746Sdillon	mtx_lock(&Giant);
151682746Sdillon
15171541Srgrimes	switch (uap->op & 0xff00) {
15181541Srgrimes
15191541Srgrimes	case KINFO_RT:
152012171Sphk		name[0] = CTL_NET;
152112171Sphk		name[1] = PF_ROUTE;
152212171Sphk		name[2] = 0;
152312171Sphk		name[3] = (uap->op & 0xff0000) >> 16;
152412171Sphk		name[4] = uap->op & 0xff;
152512171Sphk		name[5] = uap->arg;
152683366Sjulian		error = userland_sysctl(td, name, 6, uap->where, uap->size,
1527136417Sphk			0, 0, 0, &size, 0);
15281541Srgrimes		break;
15291541Srgrimes
15301541Srgrimes	case KINFO_VNODE:
153112171Sphk		name[0] = CTL_KERN;
153212171Sphk		name[1] = KERN_VNODE;
153383366Sjulian		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1534136417Sphk			0, 0, 0, &size, 0);
15351541Srgrimes		break;
15361541Srgrimes
15371541Srgrimes	case KINFO_PROC:
153812171Sphk		name[0] = CTL_KERN;
153912171Sphk		name[1] = KERN_PROC;
154012171Sphk		name[2] = uap->op & 0xff;
154112171Sphk		name[3] = uap->arg;
154283366Sjulian		error = userland_sysctl(td, name, 4, uap->where, uap->size,
1543136417Sphk			0, 0, 0, &size, 0);
15441541Srgrimes		break;
15451541Srgrimes
15461541Srgrimes	case KINFO_FILE:
154712171Sphk		name[0] = CTL_KERN;
154812171Sphk		name[1] = KERN_FILE;
154983366Sjulian		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1550136417Sphk			0, 0, 0, &size, 0);
15511541Srgrimes		break;
15521541Srgrimes
15531541Srgrimes	case KINFO_METER:
155412171Sphk		name[0] = CTL_VM;
1555109102Smux		name[1] = VM_TOTAL;
155683366Sjulian		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1557136417Sphk			0, 0, 0, &size, 0);
15581541Srgrimes		break;
15591541Srgrimes
15601541Srgrimes	case KINFO_LOADAVG:
156112171Sphk		name[0] = CTL_VM;
156212171Sphk		name[1] = VM_LOADAVG;
156383366Sjulian		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1564136417Sphk			0, 0, 0, &size, 0);
15651541Srgrimes		break;
15661541Srgrimes
15671541Srgrimes	case KINFO_CLOCKRATE:
156812171Sphk		name[0] = CTL_KERN;
156912171Sphk		name[1] = KERN_CLOCKRATE;
157083366Sjulian		error = userland_sysctl(td, name, 2, uap->where, uap->size,
1571136417Sphk			0, 0, 0, &size, 0);
15721541Srgrimes		break;
15731541Srgrimes
15749455Speter	case KINFO_BSDI_SYSINFO: {
15759455Speter		/*
15769455Speter		 * this is pretty crude, but it's just enough for uname()
15779455Speter		 * from BSDI's 1.x libc to work.
15789455Speter		 *
157982494Speter		 * *size gives the size of the buffer before the call, and
158082494Speter		 * the amount of data copied after a successful call.
158182494Speter		 * If successful, the return value is the amount of data
158282494Speter		 * available, which can be larger than *size.
158382494Speter		 *
158482494Speter		 * BSDI's 2.x product apparently fails with ENOMEM if *size
158582494Speter		 * is too small.
15869455Speter		 */
15879455Speter
15889455Speter		u_int left;
15899455Speter		char *s;
15909455Speter
15919455Speter		bzero((char *)&bsdi_si, sizeof(bsdi_si));
15929455Speter		bzero(bsdi_strings, sizeof(bsdi_strings));
15939455Speter
15949455Speter		s = bsdi_strings;
15959455Speter
15969455Speter		bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
15979455Speter		strcpy(s, ostype);
15989455Speter		s += strlen(s) + 1;
15999455Speter
16009455Speter		bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
16019455Speter		strcpy(s, osrelease);
16029455Speter		s += strlen(s) + 1;
16039455Speter
16049455Speter		bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
16059455Speter		strcpy(s, machine);
16069455Speter		s += strlen(s) + 1;
16079455Speter
16089455Speter		needed = sizeof(bsdi_si) + (s - bsdi_strings);
16099455Speter
161082494Speter		if ((uap->where == NULL) || (uap->size == NULL)) {
16119455Speter			/* process is asking how much buffer to supply.. */
16129455Speter			size = needed;
16139455Speter			error = 0;
16149455Speter			break;
16159455Speter		}
16169455Speter
161782494Speter		if ((error = copyin(uap->size, &size, sizeof(size))) != 0)
161882494Speter			break;
16199455Speter
16209455Speter		/* if too much buffer supplied, trim it down */
16219455Speter		if (size > needed)
16229455Speter			size = needed;
16239455Speter
16249455Speter		/* how much of the buffer is remaining */
16259455Speter		left = size;
16269455Speter
16279455Speter		if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
16289455Speter			break;
16299455Speter
16309455Speter		/* is there any point in continuing? */
16319455Speter		if (left > sizeof(bsdi_si)) {
16329455Speter			left -= sizeof(bsdi_si);
16339455Speter			error = copyout(&bsdi_strings,
16349455Speter					uap->where + sizeof(bsdi_si), left);
16359455Speter		}
16369455Speter		break;
16379455Speter	}
16389455Speter
16391541Srgrimes	default:
164082746Sdillon		error = EOPNOTSUPP;
164182746Sdillon		break;
16421541Srgrimes	}
164382746Sdillon	if (error == 0) {
164483366Sjulian		td->td_retval[0] = needed ? needed : size;
164582746Sdillon		if (uap->size) {
164699012Salfred			error = copyout(&size, uap->size, sizeof(size));
164782746Sdillon		}
164882746Sdillon	}
164982746Sdillon	mtx_unlock(&Giant);
16501541Srgrimes	return (error);
16511541Srgrimes}
16521541Srgrimes#endif /* COMPAT_43 */
1653