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