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