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