kern_sysctl.c revision 12297
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.49 1995/11/14 09:42:22 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_NET:
589		fn = net_sysctl;
590		break;
591	case CTL_FS:
592		fn = fs_sysctl;
593		break;
594	default:
595		return (EOPNOTSUPP);
596	}
597	if (old != NULL) {
598		if (!useracc(old, oldlen, B_WRITE))
599			return (EFAULT);
600		while (memlock.sl_lock) {
601			memlock.sl_want = 1;
602			(void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0);
603			memlock.sl_locked++;
604		}
605		memlock.sl_lock = 1;
606		if (dolock)
607			vslock(old, oldlen);
608		savelen = oldlen;
609	}
610
611
612	error = (*fn)(name + 1, namelen - 1, old, &oldlen,
613	    new, newlen, p);
614
615
616	if (old != NULL) {
617		if (dolock)
618			vsunlock(old, savelen, B_WRITE);
619		memlock.sl_lock = 0;
620		if (memlock.sl_want) {
621			memlock.sl_want = 0;
622			wakeup((caddr_t)&memlock);
623		}
624	}
625#if 0
626	if (error) {
627		printf("SYSCTL_ERROR: ");
628		for(i=0;i<namelen;i++)
629			printf("%d ", name[i]);
630		printf("= %d\n", error);
631	}
632#endif
633	if (error)
634		return (error);
635	if (retval)
636		*retval = oldlen;
637	return (error);
638}
639
640/*
641 * Attributes stored in the kernel.
642 */
643int securelevel = -1;
644
645/*
646 * kernel related system variables.
647 */
648static int
649kern_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
650	int *name;
651	u_int namelen;
652	void *oldp;
653	size_t *oldlenp;
654	void *newp;
655	size_t newlen;
656	struct proc *p;
657{
658
659	/* all sysctl names at this level are terminal */
660	if (namelen != 1 && !(name[0] == KERN_PROC || name[0] == KERN_PROF
661			      || name[0] == KERN_NTP_PLL))
662		return (ENOTDIR);		/* overloaded */
663
664	switch (name[0]) {
665
666	case KERN_VNODE:
667		return (sysctl_vnode(oldp, oldlenp));
668#ifdef GPROF
669	case KERN_PROF:
670		return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp,
671		    newp, newlen));
672#endif
673	default:
674		return (EOPNOTSUPP);
675	}
676	/* NOTREACHED */
677}
678
679static int
680sysctl_kern_securelvl SYSCTL_HANDLER_ARGS
681{
682		int error, level;
683
684		level = securelevel;
685		error = sysctl_handle_int(oidp, &level, 0, req);
686		if (error || !req->newptr)
687			return (error);
688		if (level < securelevel && req->p->p_pid != 1)
689			return (EPERM);
690		securelevel = level;
691		return (error);
692}
693
694SYSCTL_PROC(_kern, KERN_SECURELVL, securelevel, CTLTYPE_INT|CTLFLAG_RW,
695	0, 0, sysctl_kern_securelvl, "");
696
697static int
698sysctl_kern_dumpdev SYSCTL_HANDLER_ARGS
699{
700	int error;
701	dev_t ndumpdev;
702
703	ndumpdev = dumpdev;
704	error = sysctl_handle_opaque(oidp, &ndumpdev, sizeof ndumpdev, req);
705	if (!error && ndumpdev != dumpdev) {
706		error = setdumpdev(ndumpdev);
707	}
708	return (error);
709}
710
711SYSCTL_PROC(_kern, KERN_DUMPDEV, dumpdev, CTLTYPE_OPAQUE|CTLFLAG_RW,
712	0, sizeof dumpdev, sysctl_kern_dumpdev, "");
713/*
714 * hardware related system variables.
715 */
716int
717hw_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
718	int *name;
719	u_int namelen;
720	void *oldp;
721	size_t *oldlenp;
722	void *newp;
723	size_t newlen;
724	struct proc *p;
725{
726	/* almost all sysctl names at this level are terminal */
727	if (namelen != 1 && name[0] != HW_DEVCONF)
728		return (ENOTDIR);		/* overloaded */
729
730	switch (name[0]) {
731	case HW_PHYSMEM:
732		return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem)));
733	case HW_USERMEM:
734		return (sysctl_rdint(oldp, oldlenp, newp,
735		    ctob(physmem - cnt.v_wire_count)));
736	case HW_DEVCONF:
737		return (dev_sysctl(name + 1, namelen - 1, oldp, oldlenp,
738				   newp, newlen, p));
739	default:
740		return (EOPNOTSUPP);
741	}
742	/* NOTREACHED */
743}
744
745/*
746 * Validate parameters and get old / set new parameters
747 * for an integer-valued sysctl function.
748 */
749int
750sysctl_int(oldp, oldlenp, newp, newlen, valp)
751	void *oldp;
752	size_t *oldlenp;
753	void *newp;
754	size_t newlen;
755	int *valp;
756{
757	int error = 0;
758
759	if (oldp && *oldlenp < sizeof(int))
760		return (ENOMEM);
761	if (newp && newlen != sizeof(int))
762		return (EINVAL);
763	*oldlenp = sizeof(int);
764	if (oldp)
765		error = copyout(valp, oldp, sizeof(int));
766	if (error == 0 && newp)
767		error = copyin(newp, valp, sizeof(int));
768	return (error);
769}
770
771/*
772 * As above, but read-only.
773 */
774int
775sysctl_rdint(oldp, oldlenp, newp, val)
776	void *oldp;
777	size_t *oldlenp;
778	void *newp;
779	int val;
780{
781	int error = 0;
782
783	if (oldp && *oldlenp < sizeof(int))
784		return (ENOMEM);
785	if (newp)
786		return (EPERM);
787	*oldlenp = sizeof(int);
788	if (oldp)
789		error = copyout((caddr_t)&val, oldp, sizeof(int));
790	return (error);
791}
792
793/*
794 * Validate parameters and get old / set new parameters
795 * for a string-valued sysctl function.
796 */
797int
798sysctl_string(oldp, oldlenp, newp, newlen, str, maxlen)
799	void *oldp;
800	size_t *oldlenp;
801	void *newp;
802	size_t newlen;
803	char *str;
804	int maxlen;
805{
806	int len, error = 0, rval = 0;
807
808	len = strlen(str) + 1;
809	if (oldp && *oldlenp < len) {
810		len = *oldlenp;
811		rval = ENOMEM;
812	}
813	if (newp && newlen >= maxlen)
814		return (EINVAL);
815	if (oldp) {
816		*oldlenp = len;
817		error = copyout(str, oldp, len);
818		if (error)
819			rval = error;
820	}
821	if ((error == 0 || error == ENOMEM) && newp) {
822		error = copyin(newp, str, newlen);
823		if (error)
824			rval = error;
825		str[newlen] = 0;
826	}
827	return (rval);
828}
829
830/*
831 * As above, but read-only.
832 */
833int
834sysctl_rdstring(oldp, oldlenp, newp, str)
835	void *oldp;
836	size_t *oldlenp;
837	void *newp;
838	char *str;
839{
840	int len, error = 0, rval = 0;
841
842	len = strlen(str) + 1;
843	if (oldp && *oldlenp < len) {
844		len = *oldlenp;
845		rval = ENOMEM;
846	}
847	if (newp)
848		return (EPERM);
849	*oldlenp = len;
850	if (oldp)
851		error = copyout(str, oldp, len);
852		if (error)
853			rval = error;
854	return (rval);
855}
856
857/*
858 * Validate parameters and get old / set new parameters
859 * for a structure oriented sysctl function.
860 */
861int
862sysctl_struct(oldp, oldlenp, newp, newlen, sp, len)
863	void *oldp;
864	size_t *oldlenp;
865	void *newp;
866	size_t newlen;
867	void *sp;
868	int len;
869{
870	int error = 0;
871
872	if (oldp && *oldlenp < len)
873		return (ENOMEM);
874	if (newp && newlen > len)
875		return (EINVAL);
876	if (oldp) {
877		*oldlenp = len;
878		error = copyout(sp, oldp, len);
879	}
880	if (error == 0 && newp)
881		error = copyin(newp, sp, len);
882	return (error);
883}
884
885/*
886 * Validate parameters and get old parameters
887 * for a structure oriented sysctl function.
888 */
889int
890sysctl_rdstruct(oldp, oldlenp, newp, sp, len)
891	void *oldp;
892	size_t *oldlenp;
893	void *newp, *sp;
894	int len;
895{
896	int error = 0;
897
898	if (oldp && *oldlenp < len)
899		return (ENOMEM);
900	if (newp)
901		return (EPERM);
902	*oldlenp = len;
903	if (oldp)
904		error = copyout(sp, oldp, len);
905	return (error);
906}
907
908#ifdef COMPAT_43
909#include <sys/socket.h>
910#define	KINFO_PROC		(0<<8)
911#define	KINFO_RT		(1<<8)
912#define	KINFO_VNODE		(2<<8)
913#define	KINFO_FILE		(3<<8)
914#define	KINFO_METER		(4<<8)
915#define	KINFO_LOADAVG		(5<<8)
916#define	KINFO_CLOCKRATE		(6<<8)
917
918/* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
919#define	KINFO_BSDI_SYSINFO	(101<<8)
920
921/*
922 * XXX this is bloat, but I hope it's better here than on the potentially
923 * limited kernel stack...  -Peter
924 */
925
926struct {
927	int	bsdi_machine;		/* "i386" on BSD/386 */
928/*      ^^^ this is an offset to the string, relative to the struct start */
929	char	*pad0;
930	long	pad1;
931	long	pad2;
932	long	pad3;
933	u_long	pad4;
934	u_long	pad5;
935	u_long	pad6;
936
937	int	bsdi_ostype;		/* "BSD/386" on BSD/386 */
938	int	bsdi_osrelease;		/* "1.1" on BSD/386 */
939	long	pad7;
940	long	pad8;
941	char	*pad9;
942
943	long	pad10;
944	long	pad11;
945	int	pad12;
946	long	pad13;
947	quad_t	pad14;
948	long	pad15;
949
950	struct	timeval pad16;
951	/* we dont set this, because BSDI's uname used gethostname() instead */
952	int	bsdi_hostname;		/* hostname on BSD/386 */
953
954	/* the actual string data is appended here */
955
956} bsdi_si;
957/*
958 * this data is appended to the end of the bsdi_si structure during copyout.
959 * The "char *" offsets are relative to the base of the bsdi_si struct.
960 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
961 * should not exceed the length of the buffer here... (or else!! :-)
962 */
963char bsdi_strings[80];	/* It had better be less than this! */
964
965#ifndef _SYS_SYSPROTO_H_
966struct getkerninfo_args {
967	int	op;
968	char	*where;
969	int	*size;
970	int	arg;
971};
972#endif
973
974int
975ogetkerninfo(p, uap, retval)
976	struct proc *p;
977	register struct getkerninfo_args *uap;
978	int *retval;
979{
980	int error, name[6];
981	u_int size;
982
983	switch (uap->op & 0xff00) {
984
985	case KINFO_RT:
986		name[0] = CTL_NET;
987		name[1] = PF_ROUTE;
988		name[2] = 0;
989		name[3] = (uap->op & 0xff0000) >> 16;
990		name[4] = uap->op & 0xff;
991		name[5] = uap->arg;
992		error = userland_sysctl(p, name, 6, uap->where, uap->size,
993			0, 0, 0, 0);
994		break;
995
996	case KINFO_VNODE:
997		name[0] = CTL_KERN;
998		name[1] = KERN_VNODE;
999		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1000			0, 0, 0, 0);
1001		break;
1002
1003	case KINFO_PROC:
1004		name[0] = CTL_KERN;
1005		name[1] = KERN_PROC;
1006		name[2] = uap->op & 0xff;
1007		name[3] = uap->arg;
1008		error = userland_sysctl(p, name, 4, uap->where, uap->size,
1009			0, 0, 0, 0);
1010		break;
1011
1012	case KINFO_FILE:
1013		name[0] = CTL_KERN;
1014		name[1] = KERN_FILE;
1015		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1016			0, 0, 0, 0);
1017		break;
1018
1019	case KINFO_METER:
1020		name[0] = CTL_VM;
1021		name[1] = VM_METER;
1022		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1023			0, 0, 0, 0);
1024		break;
1025
1026	case KINFO_LOADAVG:
1027		name[0] = CTL_VM;
1028		name[1] = VM_LOADAVG;
1029		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1030			0, 0, 0, 0);
1031		break;
1032
1033	case KINFO_CLOCKRATE:
1034		name[0] = CTL_KERN;
1035		name[1] = KERN_CLOCKRATE;
1036		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1037			0, 0, 0, 0);
1038		break;
1039
1040	case KINFO_BSDI_SYSINFO: {
1041		/*
1042		 * this is pretty crude, but it's just enough for uname()
1043		 * from BSDI's 1.x libc to work.
1044		 *
1045		 * In particular, it doesn't return the same results when
1046		 * the supplied buffer is too small.  BSDI's version apparently
1047		 * will return the amount copied, and set the *size to how
1048		 * much was needed.  The emulation framework here isn't capable
1049		 * of that, so we just set both to the amount copied.
1050		 * BSDI's 2.x product apparently fails with ENOMEM in this
1051		 * scenario.
1052		 */
1053
1054		u_int needed;
1055		u_int left;
1056		char *s;
1057
1058		bzero((char *)&bsdi_si, sizeof(bsdi_si));
1059		bzero(bsdi_strings, sizeof(bsdi_strings));
1060
1061		s = bsdi_strings;
1062
1063		bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
1064		strcpy(s, ostype);
1065		s += strlen(s) + 1;
1066
1067		bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
1068		strcpy(s, osrelease);
1069		s += strlen(s) + 1;
1070
1071		bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
1072		strcpy(s, machine);
1073		s += strlen(s) + 1;
1074
1075		needed = sizeof(bsdi_si) + (s - bsdi_strings);
1076
1077		if (uap->where == NULL) {
1078			/* process is asking how much buffer to supply.. */
1079			size = needed;
1080			error = 0;
1081			break;
1082		}
1083
1084
1085		/* if too much buffer supplied, trim it down */
1086		if (size > needed)
1087			size = needed;
1088
1089		/* how much of the buffer is remaining */
1090		left = size;
1091
1092		if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
1093			break;
1094
1095		/* is there any point in continuing? */
1096		if (left > sizeof(bsdi_si)) {
1097			left -= sizeof(bsdi_si);
1098			error = copyout(&bsdi_strings,
1099					uap->where + sizeof(bsdi_si), left);
1100		}
1101		break;
1102	}
1103
1104	default:
1105		return (EOPNOTSUPP);
1106	}
1107	if (error)
1108		return (error);
1109	*retval = size;
1110	if (uap->size)
1111		error = copyout((caddr_t)&size, (caddr_t)uap->size,
1112		    sizeof(size));
1113	return (error);
1114}
1115#endif /* COMPAT_43 */
1116