kern_sysctl.c revision 12340
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 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	@(#)kern_sysctl.c	8.4 (Berkeley) 4/14/94
37 * $Id: kern_sysctl.c,v 1.50 1995/11/14 20:43:29 phk Exp $
38 */
39
40/*
41 * sysctl system call.
42 */
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/sysproto.h>
47#include <sys/kernel.h>
48#include <sys/vnode.h>
49#include <sys/unistd.h>
50#include <sys/conf.h>
51#include <sys/sysctl.h>
52
53extern struct linker_set sysctl_;
54
55/* BEGIN_MIB */
56SYSCTL_NODE(, 0,	  sysctl, CTLFLAG_RW, 0,
57	"Sysctl internal magic");
58SYSCTL_NODE(, CTL_KERN,	  kern,   CTLFLAG_RW, 0,
59	"High kernel, proc, limits &c");
60SYSCTL_NODE(, CTL_VM,	  vm,     CTLFLAG_RW, 0,
61	"Virtual memory");
62SYSCTL_NODE(, CTL_FS,	  fs,     CTLFLAG_RW, 0,
63	"File system");
64SYSCTL_NODE(, CTL_NET,	  net,    CTLFLAG_RW, 0,
65	"Network, (see socket.h)");
66SYSCTL_NODE(, CTL_DEBUG,  debug,  CTLFLAG_RW, 0,
67	"Debugging");
68SYSCTL_NODE(, CTL_HW,	  hw,     CTLFLAG_RW, 0,
69	"hardware");
70SYSCTL_NODE(, CTL_MACHDEP, machdep, CTLFLAG_RW, 0,
71	"machine dependent");
72SYSCTL_NODE(, CTL_USER,	  user,   CTLFLAG_RW, 0,
73	"user-level");
74
75SYSCTL_STRING(_kern, KERN_OSRELEASE, osrelease, CTLFLAG_RD, osrelease, 0, "");
76
77SYSCTL_INT(_kern, KERN_OSREV, osrevision, CTLFLAG_RD, 0, BSD, "");
78
79SYSCTL_STRING(_kern, KERN_VERSION, version, CTLFLAG_RD, version, 0, "");
80
81SYSCTL_STRING(_kern, KERN_OSTYPE, ostype, CTLFLAG_RD, ostype, 0, "");
82
83extern int osreldate;
84SYSCTL_INT(_kern, KERN_OSRELDATE, osreldate, CTLFLAG_RD, &osreldate, 0, "");
85
86SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RD, &desiredvnodes, 0, "");
87
88SYSCTL_INT(_kern, KERN_MAXPROC, maxproc, CTLFLAG_RD, &maxproc, 0, "");
89
90SYSCTL_INT(_kern, KERN_MAXPROCPERUID, maxprocperuid,
91	CTLFLAG_RD, &maxprocperuid, 0, "");
92
93SYSCTL_INT(_kern, KERN_ARGMAX, argmax, CTLFLAG_RD, 0, ARG_MAX, "");
94
95SYSCTL_INT(_kern, KERN_POSIX1, posix1version, CTLFLAG_RD, 0, _POSIX_VERSION, "");
96
97SYSCTL_INT(_kern, KERN_NGROUPS, ngroups, CTLFLAG_RD, 0, NGROUPS_MAX, "");
98
99SYSCTL_INT(_kern, KERN_JOB_CONTROL, job_control, CTLFLAG_RD, 0, 1, "");
100
101#ifdef _POSIX_SAVED_IDS
102SYSCTL_INT(_kern, KERN_SAVED_IDS, saved_ids, CTLFLAG_RD, 0, 1, "");
103#else
104SYSCTL_INT(_kern, KERN_SAVED_IDS, saved_ids, CTLFLAG_RD, 0, 0, "");
105#endif
106
107char kernelname[MAXPATHLEN] = "/kernel";	/* XXX bloat */
108
109SYSCTL_STRING(_kern, KERN_BOOTFILE, bootfile,
110	CTLFLAG_RW, kernelname, sizeof kernelname, "");
111
112SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime,
113	CTLFLAG_RW, &boottime, timeval, "");
114
115SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, "");
116
117SYSCTL_STRING(_hw, HW_MODEL, model, CTLFLAG_RD, cpu_model, 0, "");
118
119SYSCTL_INT(_hw, HW_NCPU, ncpu, CTLFLAG_RD, 0, 1, "");
120
121SYSCTL_INT(_hw, HW_BYTEORDER, byteorder, CTLFLAG_RD, 0, BYTE_ORDER, "");
122
123SYSCTL_INT(_hw, HW_PAGESIZE, pagesize, CTLFLAG_RD, 0, PAGE_SIZE, "");
124
125/* END_MIB */
126
127extern int vfs_update_wakeup;
128extern int vfs_update_interval;
129static int
130sysctl_kern_updateinterval SYSCTL_HANDLER_ARGS
131{
132	int error = sysctl_handle_int(oidp,
133		oidp->oid_arg1, oidp->oid_arg2, req);
134	if (!error)
135		wakeup(&vfs_update_wakeup);
136	return error;
137}
138
139SYSCTL_PROC(_kern, KERN_UPDATEINTERVAL, update, CTLTYPE_INT|CTLFLAG_RW,
140	&vfs_update_interval, 0, sysctl_kern_updateinterval, "");
141
142
143char hostname[MAXHOSTNAMELEN];
144
145SYSCTL_STRING(_kern, KERN_HOSTNAME, hostname, CTLFLAG_RW,
146	hostname, sizeof(hostname), "");
147
148static int
149sysctl_order_cmp(const void *a, const void *b)
150{
151	const struct sysctl_oid **pa, **pb;
152
153	pa = (const struct sysctl_oid **)a;
154	pb = (const struct sysctl_oid **)b;
155	if (*pa == NULL)
156		return (1);
157	if (*pb == NULL)
158		return (-1);
159	return ((*pa)->oid_number - (*pb)->oid_number);
160}
161
162static void
163sysctl_order(void *arg)
164{
165	int j;
166	struct linker_set *l = (struct linker_set *) arg;
167	struct sysctl_oid **oidpp;
168
169	j = l->ls_length;
170	oidpp = (struct sysctl_oid **) l->ls_items;
171	for (; j--; oidpp++) {
172		if (!*oidpp)
173			continue;
174		if ((*oidpp)->oid_arg1 == arg) {
175			*oidpp = 0;
176			continue;
177		}
178		if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE)
179			if (!(*oidpp)->oid_handler)
180				sysctl_order((*oidpp)->oid_arg1);
181	}
182	qsort(l->ls_items, l->ls_length, sizeof l->ls_items[0],
183		sysctl_order_cmp);
184}
185
186SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_order, &sysctl_);
187
188static void
189sysctl_sysctl_debug_dump_node(struct linker_set *l, int i)
190{
191	int j, k;
192	struct sysctl_oid **oidpp;
193
194	j = l->ls_length;
195	oidpp = (struct sysctl_oid **) l->ls_items;
196	for (; j--; oidpp++) {
197
198		if (!*oidpp)
199			continue;
200
201		for (k=0; k<i; k++)
202			printf(" ");
203
204		if ((*oidpp)->oid_number > 100) {
205			printf("Junk! %p  # %d  %s  k %x  a1 %p  a2 %x  h %p\n",
206				*oidpp,
207		 		(*oidpp)->oid_number, (*oidpp)->oid_name,
208		 		(*oidpp)->oid_kind, (*oidpp)->oid_arg1,
209		 		(*oidpp)->oid_arg2, (*oidpp)->oid_handler);
210			continue;
211		}
212		printf("%d %s ", (*oidpp)->oid_number, (*oidpp)->oid_name);
213
214		printf("%c%c",
215			(*oidpp)->oid_kind & CTLFLAG_RD ? 'R':' ',
216			(*oidpp)->oid_kind & CTLFLAG_WR ? 'W':' ');
217
218		switch ((*oidpp)->oid_kind & CTLTYPE) {
219			case CTLTYPE_NODE:
220				if ((*oidpp)->oid_handler) {
221					printf(" Node(proc)\n");
222				} else {
223					printf(" Node\n");
224					sysctl_sysctl_debug_dump_node(
225						(*oidpp)->oid_arg1, i+2);
226				}
227				break;
228			case CTLTYPE_INT:    printf(" Int\n"); break;
229			case CTLTYPE_STRING: printf(" String\n"); break;
230			case CTLTYPE_QUAD:   printf(" Quad\n"); break;
231			case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
232			default:	     printf("\n");
233		}
234
235	}
236}
237
238
239static int
240sysctl_sysctl_debug SYSCTL_HANDLER_ARGS
241{
242	sysctl_sysctl_debug_dump_node(&sysctl_, 0);
243	return ENOENT;
244}
245
246SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
247	0, 0, sysctl_sysctl_debug, "");
248
249char domainname[MAXHOSTNAMELEN];
250SYSCTL_STRING(_kern, KERN_DOMAINNAME, domainname, CTLFLAG_RW,
251	&domainname, sizeof(domainname), "");
252
253long hostid;
254/* Some trouble here, if sizeof (int) != sizeof (long) */
255SYSCTL_INT(_kern, KERN_HOSTID, hostid, CTLFLAG_RW, &hostid, 0, "");
256
257/*
258 * Handle an integer, signed or unsigned.
259 * Two cases:
260 *     a variable:  point arg1 at it.
261 *     a constant:  pass it in arg2.
262 */
263
264int
265sysctl_handle_int SYSCTL_HANDLER_ARGS
266{
267	int error = 0;
268
269	if (arg1)
270		error = SYSCTL_OUT(req, arg1, sizeof(int));
271	else if (arg2)
272		error = SYSCTL_OUT(req, &arg2, sizeof(int));
273
274	if (error || !req->newptr)
275		return (error);
276
277	if (!arg1)
278		error = EPERM;
279	else
280		error = SYSCTL_IN(req, arg1, sizeof(int));
281	return (error);
282}
283
284/*
285 * Handle our generic '\0' terminated 'C' string.
286 * Two cases:
287 * 	a variable string:  point arg1 at it, arg2 is max length.
288 * 	a constant string:  point arg1 at it, arg2 is zero.
289 */
290
291int
292sysctl_handle_string SYSCTL_HANDLER_ARGS
293{
294	int error=0;
295
296	error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
297
298	if (error || !req->newptr || !arg2)
299		return (error);
300
301	if ((req->newlen - req->newidx) > arg2) {
302		error = E2BIG;
303	} else {
304		arg2 = (req->newlen - req->newidx);
305		error = SYSCTL_IN(req, arg1, arg2);
306		((char *)arg1)[arg2] = '\0';
307	}
308
309	return (error);
310}
311
312/*
313 * Handle any kind of opaque data.
314 * arg1 points to it, arg2 is the size.
315 */
316
317int
318sysctl_handle_opaque SYSCTL_HANDLER_ARGS
319{
320	int error;
321
322	error = SYSCTL_OUT(req, arg1, arg2);
323
324	if (error || !req->newptr)
325		return (error);
326
327	error = SYSCTL_IN(req, arg1, arg2);
328
329	return (error);
330}
331
332/*
333 * Transfer functions to/from kernel space.
334 * XXX: rather untested at this point
335 */
336static int
337sysctl_old_kernel(struct sysctl_req *req, void *p, int l)
338{
339	int i = 0;
340
341	if (req->oldptr) {
342		i = min(req->oldlen - req->oldidx, l);
343		if (i > 0)
344			bcopy(p, req->oldptr + req->oldidx, i);
345	}
346	req->oldidx += l;
347	if (i != l)
348		return (ENOMEM);
349	return (0);
350
351}
352
353static int
354sysctl_new_kernel(struct sysctl_req *req, void *p, int l)
355{
356	if (!req->newptr)
357		return 0;
358	if (req->newlen - req->newidx < l)
359		return (EINVAL);
360	bcopy(req->newptr + req->newidx, p, l);
361	req->newidx += l;
362	return (0);
363}
364
365/*
366 * Transfer function to/from user space.
367 */
368static int
369sysctl_old_user(struct sysctl_req *req, void *p, int l)
370{
371	int error = 0, i = 0;
372
373	if (req->oldptr) {
374		i = min(req->oldlen - req->oldidx, l);
375		if (i > 0)
376			error  = copyout(p, req->oldptr + req->oldidx, i);
377	}
378	req->oldidx += l;
379	if (error)
380		return (error);
381	if (req->oldptr && i < l)
382		return (ENOMEM);
383	return (0);
384}
385
386static int
387sysctl_new_user(struct sysctl_req *req, void *p, int l)
388{
389	int error;
390
391	if (!req->newptr)
392		return 0;
393	if (req->newlen - req->newidx < l)
394		return (EINVAL);
395	error = copyin(req->newptr + req->newidx, p, l);
396	req->newidx += l;
397	return (error);
398}
399
400/*
401 * Locking and stats
402 */
403static struct sysctl_lock {
404	int	sl_lock;
405	int	sl_want;
406	int	sl_locked;
407} memlock;
408
409
410
411/*
412 * Traverse our tree, and find the right node, execute whatever it points
413 * at, and return the resulting error code.
414 * We work entirely in kernel-space at this time.
415 */
416
417
418int
419sysctl_root SYSCTL_HANDLER_ARGS
420{
421	int *name = (int *) arg1;
422	int namelen = arg2;
423	int indx, i, j;
424	struct sysctl_oid **oidpp;
425	struct linker_set *lsp = &sysctl_;
426
427	j = lsp->ls_length;
428	oidpp = (struct sysctl_oid **) lsp->ls_items;
429
430	indx = 0;
431	while (j-- && indx < CTL_MAXNAME) {
432		if (*oidpp && ((*oidpp)->oid_number == name[indx])) {
433			indx++;
434			if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
435				if ((*oidpp)->oid_handler)
436					goto found;
437				if (indx == namelen)
438					return ENOENT;
439				lsp = (struct linker_set*)(*oidpp)->oid_arg1;
440				j = lsp->ls_length;
441				oidpp = (struct sysctl_oid **)lsp->ls_items;
442			} else {
443				if (indx != namelen)
444					return EISDIR;
445				goto found;
446			}
447		} else {
448			oidpp++;
449		}
450	}
451	return ENOENT;
452found:
453
454	/* If writing isn't allowed */
455	if (req->newptr && !((*oidpp)->oid_kind & CTLFLAG_WR))
456		return (EPERM);
457
458	if (!(*oidpp)->oid_handler)
459		return EINVAL;
460
461	if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
462		i = ((*oidpp)->oid_handler) (*oidpp,
463					name + indx, namelen - indx,
464					req);
465	} else {
466		i = ((*oidpp)->oid_handler) (*oidpp,
467					(*oidpp)->oid_arg1, (*oidpp)->oid_arg2,
468					req);
469	}
470	return (i);
471}
472
473#ifndef _SYS_SYSPROTO_H_
474struct sysctl_args {
475	int	*name;
476	u_int	namelen;
477	void	*old;
478	size_t	*oldlenp;
479	void	*new;
480	size_t	newlen;
481};
482#endif
483
484int
485__sysctl(p, uap, retval)
486	struct proc *p;
487	register struct sysctl_args *uap;
488	int *retval;
489{
490	int error, i, j, name[CTL_MAXNAME];
491
492	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
493		return (EINVAL);
494
495 	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
496 	if (error)
497		return (error);
498
499	error = userland_sysctl(p, name, uap->namelen,
500		uap->old, uap->oldlenp, 0,
501		uap->new, uap->newlen, &j);
502	if (error && error != ENOMEM)
503		return (error);
504	if (uap->oldlenp) {
505		i = copyout(&j, uap->oldlenp, sizeof(j));
506		if (i)
507			return (i);
508	}
509	return (error);
510}
511
512static sysctlfn kern_sysctl;
513
514/*
515 * This is used from various compatibility syscalls too.  That's why name
516 * must be in kernel space.
517 */
518int
519userland_sysctl(struct proc *p, int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, int *retval)
520{
521	int error = 0, dolock = 1, oldlen = 0;
522	u_int savelen = 0;
523	sysctlfn *fn;
524	struct sysctl_req req;
525
526	bzero(&req, sizeof req);
527
528	req.p = p;
529
530	if (new != NULL && (error = suser(p->p_ucred, &p->p_acflag)))
531		return (error);
532
533	if (oldlenp) {
534		if (inkernel) {
535			req.oldlen = *oldlenp;
536		} else {
537			error = copyin(oldlenp, &req.oldlen, sizeof(*oldlenp));
538			if (error)
539				return (error);
540		}
541	}
542
543	if (old) {
544		if (!useracc(old, req.oldlen, B_WRITE))
545			return (EFAULT);
546		req.oldptr= old;
547	}
548
549	if (newlen) {
550		if (!useracc(new, req.newlen, B_READ))
551			return (EFAULT);
552		req.newlen = newlen;
553		req.newptr = new;
554	}
555
556	req.oldfunc = sysctl_old_user;
557	req.newfunc = sysctl_new_user;
558
559	error = sysctl_root(0, name, namelen, &req);
560
561/*
562	if (error && error != ENOMEM)
563		return (error);
564*/
565	if (error == ENOENT)
566		goto oldstuff;
567
568	if (retval) {
569		if (req.oldptr && req.oldidx > req.oldlen)
570			*retval = req.oldlen;
571		else
572			*retval = req.oldidx;
573	}
574	return (error);
575
576oldstuff:
577	oldlen = req.oldlen;
578
579	switch (name[0]) {
580	case CTL_KERN:
581		fn = kern_sysctl;
582		if (name[1] != KERN_VNODE)      /* XXX */
583			dolock = 0;
584		break;
585	case CTL_HW:
586		fn = hw_sysctl;
587		break;
588	case CTL_FS:
589		fn = fs_sysctl;
590		break;
591	default:
592		return (EOPNOTSUPP);
593	}
594	if (old != NULL) {
595		if (!useracc(old, oldlen, B_WRITE))
596			return (EFAULT);
597		while (memlock.sl_lock) {
598			memlock.sl_want = 1;
599			(void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0);
600			memlock.sl_locked++;
601		}
602		memlock.sl_lock = 1;
603		if (dolock)
604			vslock(old, oldlen);
605		savelen = oldlen;
606	}
607
608
609	error = (*fn)(name + 1, namelen - 1, old, &oldlen,
610	    new, newlen, p);
611
612
613	if (old != NULL) {
614		if (dolock)
615			vsunlock(old, savelen, B_WRITE);
616		memlock.sl_lock = 0;
617		if (memlock.sl_want) {
618			memlock.sl_want = 0;
619			wakeup((caddr_t)&memlock);
620		}
621	}
622#if 0
623	if (error) {
624		printf("SYSCTL_ERROR: ");
625		for(i=0;i<namelen;i++)
626			printf("%d ", name[i]);
627		printf("= %d\n", error);
628	}
629#endif
630	if (error)
631		return (error);
632	if (retval)
633		*retval = oldlen;
634	return (error);
635}
636
637/*
638 * Attributes stored in the kernel.
639 */
640int securelevel = -1;
641
642/*
643 * kernel related system variables.
644 */
645static int
646kern_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
647	int *name;
648	u_int namelen;
649	void *oldp;
650	size_t *oldlenp;
651	void *newp;
652	size_t newlen;
653	struct proc *p;
654{
655
656	/* all sysctl names at this level are terminal */
657	if (namelen != 1 && !(name[0] == KERN_PROC || name[0] == KERN_PROF
658			      || name[0] == KERN_NTP_PLL))
659		return (ENOTDIR);		/* overloaded */
660
661	switch (name[0]) {
662
663	case KERN_VNODE:
664		return (sysctl_vnode(oldp, oldlenp));
665#ifdef GPROF
666	case KERN_PROF:
667		return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp,
668		    newp, newlen));
669#endif
670	default:
671		return (EOPNOTSUPP);
672	}
673	/* NOTREACHED */
674}
675
676static int
677sysctl_kern_securelvl SYSCTL_HANDLER_ARGS
678{
679		int error, level;
680
681		level = securelevel;
682		error = sysctl_handle_int(oidp, &level, 0, req);
683		if (error || !req->newptr)
684			return (error);
685		if (level < securelevel && req->p->p_pid != 1)
686			return (EPERM);
687		securelevel = level;
688		return (error);
689}
690
691SYSCTL_PROC(_kern, KERN_SECURELVL, securelevel, CTLTYPE_INT|CTLFLAG_RW,
692	0, 0, sysctl_kern_securelvl, "");
693
694static int
695sysctl_kern_dumpdev SYSCTL_HANDLER_ARGS
696{
697	int error;
698	dev_t ndumpdev;
699
700	ndumpdev = dumpdev;
701	error = sysctl_handle_opaque(oidp, &ndumpdev, sizeof ndumpdev, req);
702	if (!error && ndumpdev != dumpdev) {
703		error = setdumpdev(ndumpdev);
704	}
705	return (error);
706}
707
708SYSCTL_PROC(_kern, KERN_DUMPDEV, dumpdev, CTLTYPE_OPAQUE|CTLFLAG_RW,
709	0, sizeof dumpdev, sysctl_kern_dumpdev, "");
710/*
711 * hardware related system variables.
712 */
713int
714hw_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
715	int *name;
716	u_int namelen;
717	void *oldp;
718	size_t *oldlenp;
719	void *newp;
720	size_t newlen;
721	struct proc *p;
722{
723	/* almost all sysctl names at this level are terminal */
724	if (namelen != 1 && name[0] != HW_DEVCONF)
725		return (ENOTDIR);		/* overloaded */
726
727	switch (name[0]) {
728	case HW_PHYSMEM:
729		return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem)));
730	case HW_USERMEM:
731		return (sysctl_rdint(oldp, oldlenp, newp,
732		    ctob(physmem - cnt.v_wire_count)));
733	case HW_DEVCONF:
734		return (dev_sysctl(name + 1, namelen - 1, oldp, oldlenp,
735				   newp, newlen, p));
736	default:
737		return (EOPNOTSUPP);
738	}
739	/* NOTREACHED */
740}
741
742/*
743 * Validate parameters and get old / set new parameters
744 * for an integer-valued sysctl function.
745 */
746int
747sysctl_int(oldp, oldlenp, newp, newlen, valp)
748	void *oldp;
749	size_t *oldlenp;
750	void *newp;
751	size_t newlen;
752	int *valp;
753{
754	int error = 0;
755
756	if (oldp && *oldlenp < sizeof(int))
757		return (ENOMEM);
758	if (newp && newlen != sizeof(int))
759		return (EINVAL);
760	*oldlenp = sizeof(int);
761	if (oldp)
762		error = copyout(valp, oldp, sizeof(int));
763	if (error == 0 && newp)
764		error = copyin(newp, valp, sizeof(int));
765	return (error);
766}
767
768/*
769 * As above, but read-only.
770 */
771int
772sysctl_rdint(oldp, oldlenp, newp, val)
773	void *oldp;
774	size_t *oldlenp;
775	void *newp;
776	int val;
777{
778	int error = 0;
779
780	if (oldp && *oldlenp < sizeof(int))
781		return (ENOMEM);
782	if (newp)
783		return (EPERM);
784	*oldlenp = sizeof(int);
785	if (oldp)
786		error = copyout((caddr_t)&val, oldp, sizeof(int));
787	return (error);
788}
789
790/*
791 * Validate parameters and get old / set new parameters
792 * for a string-valued sysctl function.
793 */
794int
795sysctl_string(oldp, oldlenp, newp, newlen, str, maxlen)
796	void *oldp;
797	size_t *oldlenp;
798	void *newp;
799	size_t newlen;
800	char *str;
801	int maxlen;
802{
803	int len, error = 0, rval = 0;
804
805	len = strlen(str) + 1;
806	if (oldp && *oldlenp < len) {
807		len = *oldlenp;
808		rval = ENOMEM;
809	}
810	if (newp && newlen >= maxlen)
811		return (EINVAL);
812	if (oldp) {
813		*oldlenp = len;
814		error = copyout(str, oldp, len);
815		if (error)
816			rval = error;
817	}
818	if ((error == 0 || error == ENOMEM) && newp) {
819		error = copyin(newp, str, newlen);
820		if (error)
821			rval = error;
822		str[newlen] = 0;
823	}
824	return (rval);
825}
826
827/*
828 * As above, but read-only.
829 */
830int
831sysctl_rdstring(oldp, oldlenp, newp, str)
832	void *oldp;
833	size_t *oldlenp;
834	void *newp;
835	char *str;
836{
837	int len, error = 0, rval = 0;
838
839	len = strlen(str) + 1;
840	if (oldp && *oldlenp < len) {
841		len = *oldlenp;
842		rval = ENOMEM;
843	}
844	if (newp)
845		return (EPERM);
846	*oldlenp = len;
847	if (oldp)
848		error = copyout(str, oldp, len);
849		if (error)
850			rval = error;
851	return (rval);
852}
853
854/*
855 * Validate parameters and get old / set new parameters
856 * for a structure oriented sysctl function.
857 */
858int
859sysctl_struct(oldp, oldlenp, newp, newlen, sp, len)
860	void *oldp;
861	size_t *oldlenp;
862	void *newp;
863	size_t newlen;
864	void *sp;
865	int len;
866{
867	int error = 0;
868
869	if (oldp && *oldlenp < len)
870		return (ENOMEM);
871	if (newp && newlen > len)
872		return (EINVAL);
873	if (oldp) {
874		*oldlenp = len;
875		error = copyout(sp, oldp, len);
876	}
877	if (error == 0 && newp)
878		error = copyin(newp, sp, len);
879	return (error);
880}
881
882/*
883 * Validate parameters and get old parameters
884 * for a structure oriented sysctl function.
885 */
886int
887sysctl_rdstruct(oldp, oldlenp, newp, sp, len)
888	void *oldp;
889	size_t *oldlenp;
890	void *newp, *sp;
891	int len;
892{
893	int error = 0;
894
895	if (oldp && *oldlenp < len)
896		return (ENOMEM);
897	if (newp)
898		return (EPERM);
899	*oldlenp = len;
900	if (oldp)
901		error = copyout(sp, oldp, len);
902	return (error);
903}
904
905#ifdef COMPAT_43
906#include <sys/socket.h>
907#define	KINFO_PROC		(0<<8)
908#define	KINFO_RT		(1<<8)
909#define	KINFO_VNODE		(2<<8)
910#define	KINFO_FILE		(3<<8)
911#define	KINFO_METER		(4<<8)
912#define	KINFO_LOADAVG		(5<<8)
913#define	KINFO_CLOCKRATE		(6<<8)
914
915/* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
916#define	KINFO_BSDI_SYSINFO	(101<<8)
917
918/*
919 * XXX this is bloat, but I hope it's better here than on the potentially
920 * limited kernel stack...  -Peter
921 */
922
923struct {
924	int	bsdi_machine;		/* "i386" on BSD/386 */
925/*      ^^^ this is an offset to the string, relative to the struct start */
926	char	*pad0;
927	long	pad1;
928	long	pad2;
929	long	pad3;
930	u_long	pad4;
931	u_long	pad5;
932	u_long	pad6;
933
934	int	bsdi_ostype;		/* "BSD/386" on BSD/386 */
935	int	bsdi_osrelease;		/* "1.1" on BSD/386 */
936	long	pad7;
937	long	pad8;
938	char	*pad9;
939
940	long	pad10;
941	long	pad11;
942	int	pad12;
943	long	pad13;
944	quad_t	pad14;
945	long	pad15;
946
947	struct	timeval pad16;
948	/* we dont set this, because BSDI's uname used gethostname() instead */
949	int	bsdi_hostname;		/* hostname on BSD/386 */
950
951	/* the actual string data is appended here */
952
953} bsdi_si;
954/*
955 * this data is appended to the end of the bsdi_si structure during copyout.
956 * The "char *" offsets are relative to the base of the bsdi_si struct.
957 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
958 * should not exceed the length of the buffer here... (or else!! :-)
959 */
960char bsdi_strings[80];	/* It had better be less than this! */
961
962#ifndef _SYS_SYSPROTO_H_
963struct getkerninfo_args {
964	int	op;
965	char	*where;
966	int	*size;
967	int	arg;
968};
969#endif
970
971int
972ogetkerninfo(p, uap, retval)
973	struct proc *p;
974	register struct getkerninfo_args *uap;
975	int *retval;
976{
977	int error, name[6];
978	u_int size;
979
980	switch (uap->op & 0xff00) {
981
982	case KINFO_RT:
983		name[0] = CTL_NET;
984		name[1] = PF_ROUTE;
985		name[2] = 0;
986		name[3] = (uap->op & 0xff0000) >> 16;
987		name[4] = uap->op & 0xff;
988		name[5] = uap->arg;
989		error = userland_sysctl(p, name, 6, uap->where, uap->size,
990			0, 0, 0, 0);
991		break;
992
993	case KINFO_VNODE:
994		name[0] = CTL_KERN;
995		name[1] = KERN_VNODE;
996		error = userland_sysctl(p, name, 2, uap->where, uap->size,
997			0, 0, 0, 0);
998		break;
999
1000	case KINFO_PROC:
1001		name[0] = CTL_KERN;
1002		name[1] = KERN_PROC;
1003		name[2] = uap->op & 0xff;
1004		name[3] = uap->arg;
1005		error = userland_sysctl(p, name, 4, uap->where, uap->size,
1006			0, 0, 0, 0);
1007		break;
1008
1009	case KINFO_FILE:
1010		name[0] = CTL_KERN;
1011		name[1] = KERN_FILE;
1012		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1013			0, 0, 0, 0);
1014		break;
1015
1016	case KINFO_METER:
1017		name[0] = CTL_VM;
1018		name[1] = VM_METER;
1019		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1020			0, 0, 0, 0);
1021		break;
1022
1023	case KINFO_LOADAVG:
1024		name[0] = CTL_VM;
1025		name[1] = VM_LOADAVG;
1026		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1027			0, 0, 0, 0);
1028		break;
1029
1030	case KINFO_CLOCKRATE:
1031		name[0] = CTL_KERN;
1032		name[1] = KERN_CLOCKRATE;
1033		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1034			0, 0, 0, 0);
1035		break;
1036
1037	case KINFO_BSDI_SYSINFO: {
1038		/*
1039		 * this is pretty crude, but it's just enough for uname()
1040		 * from BSDI's 1.x libc to work.
1041		 *
1042		 * In particular, it doesn't return the same results when
1043		 * the supplied buffer is too small.  BSDI's version apparently
1044		 * will return the amount copied, and set the *size to how
1045		 * much was needed.  The emulation framework here isn't capable
1046		 * of that, so we just set both to the amount copied.
1047		 * BSDI's 2.x product apparently fails with ENOMEM in this
1048		 * scenario.
1049		 */
1050
1051		u_int needed;
1052		u_int left;
1053		char *s;
1054
1055		bzero((char *)&bsdi_si, sizeof(bsdi_si));
1056		bzero(bsdi_strings, sizeof(bsdi_strings));
1057
1058		s = bsdi_strings;
1059
1060		bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
1061		strcpy(s, ostype);
1062		s += strlen(s) + 1;
1063
1064		bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
1065		strcpy(s, osrelease);
1066		s += strlen(s) + 1;
1067
1068		bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
1069		strcpy(s, machine);
1070		s += strlen(s) + 1;
1071
1072		needed = sizeof(bsdi_si) + (s - bsdi_strings);
1073
1074		if (uap->where == NULL) {
1075			/* process is asking how much buffer to supply.. */
1076			size = needed;
1077			error = 0;
1078			break;
1079		}
1080
1081
1082		/* if too much buffer supplied, trim it down */
1083		if (size > needed)
1084			size = needed;
1085
1086		/* how much of the buffer is remaining */
1087		left = size;
1088
1089		if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
1090			break;
1091
1092		/* is there any point in continuing? */
1093		if (left > sizeof(bsdi_si)) {
1094			left -= sizeof(bsdi_si);
1095			error = copyout(&bsdi_strings,
1096					uap->where + sizeof(bsdi_si), left);
1097		}
1098		break;
1099	}
1100
1101	default:
1102		return (EOPNOTSUPP);
1103	}
1104	if (error)
1105		return (error);
1106	*retval = size;
1107	if (uap->size)
1108		error = copyout((caddr_t)&size, (caddr_t)uap->size,
1109		    sizeof(size));
1110	return (error);
1111}
1112#endif /* COMPAT_43 */
1113