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