xref: /freebsd/sys/kern/kern_sysctl.c (revision ae0eb976c5013497b3f7fe256d8dc1ebbc7bbbf8)
1df8bae1dSRodney W. Grimes /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
6df8bae1dSRodney W. Grimes  * Mike Karels at Berkeley Software Design, Inc.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes  * are met:
11df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
17df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
18df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
19df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
20df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  *
36df8bae1dSRodney W. Grimes  *	@(#)kern_sysctl.c	8.4 (Berkeley) 4/14/94
37ae0eb976SPoul-Henning Kamp  * $Id: kern_sysctl.c,v 1.37 1995/11/12 06:43:01 bde Exp $
38df8bae1dSRodney W. Grimes  */
39df8bae1dSRodney W. Grimes 
40df8bae1dSRodney W. Grimes /*
41df8bae1dSRodney W. Grimes  * sysctl system call.
42df8bae1dSRodney W. Grimes  */
43df8bae1dSRodney W. Grimes 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45df8bae1dSRodney W. Grimes #include <sys/systm.h>
46d2d3e875SBruce Evans #include <sys/sysproto.h>
47df8bae1dSRodney W. Grimes #include <sys/kernel.h>
48df8bae1dSRodney W. Grimes #include <sys/malloc.h>
49df8bae1dSRodney W. Grimes #include <sys/proc.h>
50df8bae1dSRodney W. Grimes #include <sys/file.h>
51df8bae1dSRodney W. Grimes #include <sys/vnode.h>
52df8bae1dSRodney W. Grimes #include <sys/unistd.h>
53df8bae1dSRodney W. Grimes #include <sys/buf.h>
54df8bae1dSRodney W. Grimes #include <sys/ioctl.h>
55df8bae1dSRodney W. Grimes #include <sys/tty.h>
565bb4f738SGarrett Wollman #include <sys/conf.h>
57df8bae1dSRodney W. Grimes #include <vm/vm.h>
58df8bae1dSRodney W. Grimes #include <sys/sysctl.h>
593a34a5c3SPoul-Henning Kamp #include <sys/user.h>
60df8bae1dSRodney W. Grimes 
61787d58f2SPoul-Henning Kamp extern struct linker_set sysctl_;
62787d58f2SPoul-Henning Kamp 
63b396cd83SPoul-Henning Kamp /* BEGIN_MIB */
64787d58f2SPoul-Henning Kamp SYSCTL_NODE(, 0,	  sysctl, CTLFLAG_RW, 0,
65787d58f2SPoul-Henning Kamp 	"Sysctl internal magic");
662e210993SPoul-Henning Kamp SYSCTL_NODE(, CTL_KERN,	  kern,   CTLFLAG_RW, 0,
672e210993SPoul-Henning Kamp 	"High kernel, proc, limits &c");
682e210993SPoul-Henning Kamp SYSCTL_NODE(, CTL_VM,	  vm,     CTLFLAG_RW, 0,
692e210993SPoul-Henning Kamp 	"Virtual memory");
702e210993SPoul-Henning Kamp SYSCTL_NODE(, CTL_FS,	  fs,     CTLFLAG_RW, 0,
712e210993SPoul-Henning Kamp 	"File system");
722e210993SPoul-Henning Kamp SYSCTL_NODE(, CTL_NET,	  net,    CTLFLAG_RW, 0,
732e210993SPoul-Henning Kamp 	"Network, (see socket.h)");
742e210993SPoul-Henning Kamp SYSCTL_NODE(, CTL_DEBUG,  debug,  CTLFLAG_RW, 0,
752e210993SPoul-Henning Kamp 	"Debugging");
762e210993SPoul-Henning Kamp SYSCTL_NODE(, CTL_HW,	  hw,     CTLFLAG_RW, 0,
772e210993SPoul-Henning Kamp 	"hardware");
782e210993SPoul-Henning Kamp SYSCTL_NODE(, CTL_MACHDEP, machdep, CTLFLAG_RW, 0,
792e210993SPoul-Henning Kamp 	"machine dependent");
802e210993SPoul-Henning Kamp SYSCTL_NODE(, CTL_USER,	  user,   CTLFLAG_RW, 0,
812e210993SPoul-Henning Kamp 	"user-level");
82b396cd83SPoul-Henning Kamp 
832e210993SPoul-Henning Kamp SYSCTL_STRING(_kern, KERN_OSRELEASE, osrelease, CTLFLAG_RD, osrelease, 0, "");
84b396cd83SPoul-Henning Kamp 
852e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_OSREV, osrevision, CTLFLAG_RD, 0, BSD, "");
86b396cd83SPoul-Henning Kamp 
872e210993SPoul-Henning Kamp SYSCTL_STRING(_kern, KERN_VERSION, version, CTLFLAG_RD, version, 0, "");
88b396cd83SPoul-Henning Kamp 
892e210993SPoul-Henning Kamp SYSCTL_STRING(_kern, KERN_OSTYPE, ostype, CTLFLAG_RD, ostype, 0, "");
90b396cd83SPoul-Henning Kamp 
91b396cd83SPoul-Henning Kamp extern int osreldate;
922e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_OSRELDATE, osreldate, CTLFLAG_RD, &osreldate, 0, "");
93b396cd83SPoul-Henning Kamp 
942e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RD, &desiredvnodes, 0, "");
95b396cd83SPoul-Henning Kamp 
962e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_MAXPROC, maxproc, CTLFLAG_RD, &maxproc, 0, "");
97b396cd83SPoul-Henning Kamp 
98b396cd83SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_MAXPROCPERUID, maxprocperuid,
993a34a5c3SPoul-Henning Kamp 	CTLFLAG_RD, &maxprocperuid, 0, "");
100b396cd83SPoul-Henning Kamp 
101b396cd83SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc,
1023a34a5c3SPoul-Henning Kamp 	CTLFLAG_RD, &maxfilesperproc, 0, "");
103b396cd83SPoul-Henning Kamp 
1042e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_ARGMAX, argmax, CTLFLAG_RD, 0, ARG_MAX, "");
105b396cd83SPoul-Henning Kamp 
1062e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_POSIX1, posix1version, CTLFLAG_RD, 0, _POSIX_VERSION, "");
107b396cd83SPoul-Henning Kamp 
1082e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_NGROUPS, ngroups, CTLFLAG_RD, 0, NGROUPS_MAX, "");
109b396cd83SPoul-Henning Kamp 
1102e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_JOB_CONTROL, job_control, CTLFLAG_RD, 0, 1, "");
111b396cd83SPoul-Henning Kamp 
1122e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW, &maxfiles, 0, "");
113b396cd83SPoul-Henning Kamp 
114b396cd83SPoul-Henning Kamp #ifdef _POSIX_SAVED_IDS
1152e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_SAVED_IDS, saved_ids, CTLFLAG_RD, 0, 1, "");
116b396cd83SPoul-Henning Kamp #else
1172e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_SAVED_IDS, saved_ids, CTLFLAG_RD, 0, 0, "");
118b396cd83SPoul-Henning Kamp #endif
119b396cd83SPoul-Henning Kamp 
120b396cd83SPoul-Henning Kamp char kernelname[MAXPATHLEN] = "/kernel";	/* XXX bloat */
121b396cd83SPoul-Henning Kamp 
122b396cd83SPoul-Henning Kamp SYSCTL_STRING(_kern, KERN_BOOTFILE, bootfile,
1233a34a5c3SPoul-Henning Kamp 	CTLFLAG_RW, kernelname, sizeof kernelname, "");
124b396cd83SPoul-Henning Kamp 
125b396cd83SPoul-Henning Kamp SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime,
1263a34a5c3SPoul-Henning Kamp 	CTLFLAG_RW, &boottime, timeval, "");
127b396cd83SPoul-Henning Kamp 
1282e210993SPoul-Henning Kamp SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, "");
129b396cd83SPoul-Henning Kamp 
130787d58f2SPoul-Henning Kamp SYSCTL_STRING(_hw, HW_MODEL, model, CTLFLAG_RD, cpu_model, 0, "");
131b396cd83SPoul-Henning Kamp 
1322e210993SPoul-Henning Kamp SYSCTL_INT(_hw, HW_NCPU, ncpu, CTLFLAG_RD, 0, 1, "");
133b396cd83SPoul-Henning Kamp 
1342e210993SPoul-Henning Kamp SYSCTL_INT(_hw, HW_BYTEORDER, byteorder, CTLFLAG_RD, 0, BYTE_ORDER, "");
135b396cd83SPoul-Henning Kamp 
1362e210993SPoul-Henning Kamp SYSCTL_INT(_hw, HW_PAGESIZE, pagesize, CTLFLAG_RD, 0, PAGE_SIZE, "");
137b396cd83SPoul-Henning Kamp 
138b396cd83SPoul-Henning Kamp /* END_MIB */
139b396cd83SPoul-Henning Kamp 
140b396cd83SPoul-Henning Kamp extern int vfs_update_wakeup;
141b396cd83SPoul-Henning Kamp extern int vfs_update_interval;
142b396cd83SPoul-Henning Kamp static int
1433a34a5c3SPoul-Henning Kamp sysctl_kern_updateinterval SYSCTL_HANDLER_ARGS
144b396cd83SPoul-Henning Kamp {
1453a34a5c3SPoul-Henning Kamp 	int error = sysctl_handle_int(oidp,
146ae0eb976SPoul-Henning Kamp 		oidp->oid_arg1, oidp->oid_arg2, req);
1473a34a5c3SPoul-Henning Kamp 	if (!error)
148b396cd83SPoul-Henning Kamp 		wakeup(&vfs_update_wakeup);
1493a34a5c3SPoul-Henning Kamp 	return error;
150b396cd83SPoul-Henning Kamp }
151b396cd83SPoul-Henning Kamp 
1522e210993SPoul-Henning Kamp SYSCTL_PROC(_kern, KERN_UPDATEINTERVAL, update, CTLTYPE_INT|CTLFLAG_RW,
1533a34a5c3SPoul-Henning Kamp 	&vfs_update_interval, 0, sysctl_kern_updateinterval, "");
154b396cd83SPoul-Henning Kamp 
1552e210993SPoul-Henning Kamp 
1562e210993SPoul-Henning Kamp char hostname[MAXHOSTNAMELEN];
1572e210993SPoul-Henning Kamp int hostnamelen;
1582e210993SPoul-Henning Kamp static int
1592e210993SPoul-Henning Kamp sysctl_kern_hostname SYSCTL_HANDLER_ARGS
1602e210993SPoul-Henning Kamp {
1612e210993SPoul-Henning Kamp 	int error = sysctl_handle_string(oidp,
162ae0eb976SPoul-Henning Kamp 		oidp->oid_arg1, oidp->oid_arg2, req);
163ae0eb976SPoul-Henning Kamp 	if (req->newptr && (error == 0 || error == ENOMEM))
164ae0eb976SPoul-Henning Kamp 		hostnamelen = req->newlen;
1652e210993SPoul-Henning Kamp 	return error;
1662e210993SPoul-Henning Kamp }
1672e210993SPoul-Henning Kamp 
1682e210993SPoul-Henning Kamp SYSCTL_PROC(_kern, KERN_HOSTNAME, hostname, CTLTYPE_STRING|CTLFLAG_RW,
1692e210993SPoul-Henning Kamp 	&hostname, sizeof(hostname), sysctl_kern_hostname, "");
1702e210993SPoul-Henning Kamp 
171787d58f2SPoul-Henning Kamp static int
1723c8e79ddSBruce Evans sysctl_order_cmp(const void *a, const void *b)
173787d58f2SPoul-Henning Kamp {
1743c8e79ddSBruce Evans 	const struct sysctl_oid **pa, **pb;
1753c8e79ddSBruce Evans 
1763c8e79ddSBruce Evans 	pa = (const struct sysctl_oid **)a;
1773c8e79ddSBruce Evans 	pb = (const struct sysctl_oid **)b;
1783c8e79ddSBruce Evans 	if (*pa == NULL)
1793c8e79ddSBruce Evans 		return (1);
1803c8e79ddSBruce Evans 	if (*pb == NULL)
1813c8e79ddSBruce Evans 		return (-1);
182787d58f2SPoul-Henning Kamp 	return ((*pa)->oid_number - (*pb)->oid_number);
183787d58f2SPoul-Henning Kamp }
184787d58f2SPoul-Henning Kamp 
185787d58f2SPoul-Henning Kamp static void
186787d58f2SPoul-Henning Kamp sysctl_order(void *arg)
187787d58f2SPoul-Henning Kamp {
188b8da2396SPoul-Henning Kamp 	int j;
189787d58f2SPoul-Henning Kamp 	struct linker_set *l = (struct linker_set *) arg;
190787d58f2SPoul-Henning Kamp 	struct sysctl_oid **oidpp;
191787d58f2SPoul-Henning Kamp 
192787d58f2SPoul-Henning Kamp 	j = l->ls_length;
193787d58f2SPoul-Henning Kamp 	oidpp = (struct sysctl_oid **) l->ls_items;
194787d58f2SPoul-Henning Kamp 	for (; j--; oidpp++) {
195787d58f2SPoul-Henning Kamp 		if (!*oidpp)
196787d58f2SPoul-Henning Kamp 			continue;
197787d58f2SPoul-Henning Kamp 		if ((*oidpp)->oid_arg1 == arg) {
198787d58f2SPoul-Henning Kamp 			*oidpp = 0;
199787d58f2SPoul-Henning Kamp 			continue;
200787d58f2SPoul-Henning Kamp 		}
201787d58f2SPoul-Henning Kamp 		if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE)
202787d58f2SPoul-Henning Kamp 			if (!(*oidpp)->oid_handler)
203787d58f2SPoul-Henning Kamp 				sysctl_order((*oidpp)->oid_arg1);
204787d58f2SPoul-Henning Kamp 	}
205787d58f2SPoul-Henning Kamp 	qsort(l->ls_items, l->ls_length, sizeof l->ls_items[0],
206787d58f2SPoul-Henning Kamp 		sysctl_order_cmp);
207787d58f2SPoul-Henning Kamp }
208787d58f2SPoul-Henning Kamp 
209787d58f2SPoul-Henning Kamp SYSINIT(sysctl, SI_SUB_KMEM, SI_ORDER_ANY, sysctl_order, &sysctl_);
210787d58f2SPoul-Henning Kamp 
211787d58f2SPoul-Henning Kamp static void
212787d58f2SPoul-Henning Kamp sysctl_sysctl_debug_dump_node(struct linker_set *l, int i)
213787d58f2SPoul-Henning Kamp {
214787d58f2SPoul-Henning Kamp 	int j, k;
215787d58f2SPoul-Henning Kamp 	struct sysctl_oid **oidpp;
216787d58f2SPoul-Henning Kamp 
217787d58f2SPoul-Henning Kamp 	j = l->ls_length;
218787d58f2SPoul-Henning Kamp 	oidpp = (struct sysctl_oid **) l->ls_items;
219787d58f2SPoul-Henning Kamp 	for (; j--; oidpp++) {
220787d58f2SPoul-Henning Kamp 
221787d58f2SPoul-Henning Kamp 		if (!*oidpp)
222787d58f2SPoul-Henning Kamp 			continue;
223787d58f2SPoul-Henning Kamp 
224787d58f2SPoul-Henning Kamp 		for (k=0; k<i; k++)
225787d58f2SPoul-Henning Kamp 			printf(" ");
226787d58f2SPoul-Henning Kamp 
227787d58f2SPoul-Henning Kamp 		if ((*oidpp)->oid_number > 100) {
228b8da2396SPoul-Henning Kamp 			printf("Junk! %p  # %d  %s  k %x  a1 %p  a2 %x  h %p\n",
229787d58f2SPoul-Henning Kamp 				*oidpp,
230787d58f2SPoul-Henning Kamp 		 		(*oidpp)->oid_number, (*oidpp)->oid_name,
231787d58f2SPoul-Henning Kamp 		 		(*oidpp)->oid_kind, (*oidpp)->oid_arg1,
232787d58f2SPoul-Henning Kamp 		 		(*oidpp)->oid_arg2, (*oidpp)->oid_handler);
233787d58f2SPoul-Henning Kamp 			continue;
234787d58f2SPoul-Henning Kamp 		}
235787d58f2SPoul-Henning Kamp 		printf("%d %s ", (*oidpp)->oid_number, (*oidpp)->oid_name);
236787d58f2SPoul-Henning Kamp 
237787d58f2SPoul-Henning Kamp 		printf("%c%c",
238787d58f2SPoul-Henning Kamp 			(*oidpp)->oid_kind & CTLFLAG_RD ? 'R':' ',
239787d58f2SPoul-Henning Kamp 			(*oidpp)->oid_kind & CTLFLAG_WR ? 'W':' ');
240787d58f2SPoul-Henning Kamp 
241787d58f2SPoul-Henning Kamp 		switch ((*oidpp)->oid_kind & CTLTYPE) {
242787d58f2SPoul-Henning Kamp 			case CTLTYPE_NODE:
243787d58f2SPoul-Henning Kamp 				if ((*oidpp)->oid_handler) {
244787d58f2SPoul-Henning Kamp 					printf(" Node(proc)\n");
245787d58f2SPoul-Henning Kamp 				} else {
246787d58f2SPoul-Henning Kamp 					printf(" Node\n");
247787d58f2SPoul-Henning Kamp 					sysctl_sysctl_debug_dump_node(
248787d58f2SPoul-Henning Kamp 						(*oidpp)->oid_arg1, i+2);
249787d58f2SPoul-Henning Kamp 				}
250787d58f2SPoul-Henning Kamp 				break;
251787d58f2SPoul-Henning Kamp 			case CTLTYPE_INT:    printf(" Int\n"); break;
252787d58f2SPoul-Henning Kamp 			case CTLTYPE_STRING: printf(" String\n"); break;
253787d58f2SPoul-Henning Kamp 			case CTLTYPE_QUAD:   printf(" Quad\n"); break;
254787d58f2SPoul-Henning Kamp 			case CTLTYPE_OPAQUE: printf(" Opaque/struct\n"); break;
255787d58f2SPoul-Henning Kamp 			default:	     printf("\n");
256787d58f2SPoul-Henning Kamp 		}
257787d58f2SPoul-Henning Kamp 
258787d58f2SPoul-Henning Kamp 	}
259787d58f2SPoul-Henning Kamp }
260787d58f2SPoul-Henning Kamp 
261787d58f2SPoul-Henning Kamp 
262787d58f2SPoul-Henning Kamp static int
263787d58f2SPoul-Henning Kamp sysctl_sysctl_debug SYSCTL_HANDLER_ARGS
264787d58f2SPoul-Henning Kamp {
265787d58f2SPoul-Henning Kamp 	sysctl_sysctl_debug_dump_node(&sysctl_, 0);
266787d58f2SPoul-Henning Kamp 	return ENOENT;
267787d58f2SPoul-Henning Kamp }
268787d58f2SPoul-Henning Kamp 
269787d58f2SPoul-Henning Kamp SYSCTL_PROC(_sysctl, 0, debug, CTLTYPE_STRING|CTLFLAG_RD,
270787d58f2SPoul-Henning Kamp 	0, 0, sysctl_sysctl_debug, "");
2712e210993SPoul-Henning Kamp 
2722e210993SPoul-Henning Kamp char domainname[MAXHOSTNAMELEN];
2732e210993SPoul-Henning Kamp int domainnamelen;
2742e210993SPoul-Henning Kamp static int
2752e210993SPoul-Henning Kamp sysctl_kern_domainname SYSCTL_HANDLER_ARGS
2762e210993SPoul-Henning Kamp {
2772e210993SPoul-Henning Kamp 	int error = sysctl_handle_string(oidp,
278ae0eb976SPoul-Henning Kamp 		oidp->oid_arg1, oidp->oid_arg2, req);
279ae0eb976SPoul-Henning Kamp 	if (req->newptr && (error == 0 || error == ENOMEM))
280ae0eb976SPoul-Henning Kamp 		domainnamelen = req->newlen;
2812e210993SPoul-Henning Kamp 	return error;
2822e210993SPoul-Henning Kamp }
2832e210993SPoul-Henning Kamp 
2842e210993SPoul-Henning Kamp SYSCTL_PROC(_kern, KERN_DOMAINNAME, domainname, CTLTYPE_STRING|CTLFLAG_RW,
2852e210993SPoul-Henning Kamp 	&domainname, sizeof(domainname), sysctl_kern_domainname, "");
2862e210993SPoul-Henning Kamp 
2872e210993SPoul-Henning Kamp long hostid;
2882e210993SPoul-Henning Kamp /* Some trouble here, if sizeof (int) != sizeof (long) */
2892e210993SPoul-Henning Kamp SYSCTL_INT(_kern, KERN_HOSTID, hostid, CTLFLAG_RW, &hostid, 0, "");
2902e210993SPoul-Henning Kamp 
291ae0eb976SPoul-Henning Kamp /*
292ae0eb976SPoul-Henning Kamp  * Handle an integer, signed or unsigned.
293ae0eb976SPoul-Henning Kamp  * Two cases:
294ae0eb976SPoul-Henning Kamp  *     a variable:  point arg1 at it.
295ae0eb976SPoul-Henning Kamp  *     a constant:  pass it in arg2.
296ae0eb976SPoul-Henning Kamp  */
297ae0eb976SPoul-Henning Kamp 
2983a34a5c3SPoul-Henning Kamp int
2993a34a5c3SPoul-Henning Kamp sysctl_handle_int SYSCTL_HANDLER_ARGS
300b396cd83SPoul-Henning Kamp {
301ae0eb976SPoul-Henning Kamp 	int error = 0;
302b396cd83SPoul-Henning Kamp 
303ae0eb976SPoul-Henning Kamp 	if (arg1)
304ae0eb976SPoul-Henning Kamp 		error = SYSCTL_OUT(req, arg1, sizeof(int));
305ae0eb976SPoul-Henning Kamp 	else if (arg2)
306ae0eb976SPoul-Henning Kamp 		error = SYSCTL_OUT(req, &arg2, sizeof(int));
307b396cd83SPoul-Henning Kamp 
308ae0eb976SPoul-Henning Kamp 	if (error || !req->newptr)
309ae0eb976SPoul-Henning Kamp 		return (error);
310b396cd83SPoul-Henning Kamp 
311ae0eb976SPoul-Henning Kamp 	if (!arg1)
312ae0eb976SPoul-Henning Kamp 		error = EPERM;
313ae0eb976SPoul-Henning Kamp 	else
314ae0eb976SPoul-Henning Kamp 		error = SYSCTL_IN(req, arg1, sizeof(int));
315ae0eb976SPoul-Henning Kamp 	return (error);
316b396cd83SPoul-Henning Kamp }
317b396cd83SPoul-Henning Kamp 
318ae0eb976SPoul-Henning Kamp /*
319ae0eb976SPoul-Henning Kamp  * Handle our generic '\0' terminated 'C' string.
320ae0eb976SPoul-Henning Kamp  * Two cases:
321ae0eb976SPoul-Henning Kamp  * 	a variable string:  point arg1 at it, arg2 is max length.
322ae0eb976SPoul-Henning Kamp  * 	a constant string:  point arg1 at it, arg2 is zero.
323ae0eb976SPoul-Henning Kamp  */
324ae0eb976SPoul-Henning Kamp 
3253a34a5c3SPoul-Henning Kamp int
3263a34a5c3SPoul-Henning Kamp sysctl_handle_string SYSCTL_HANDLER_ARGS
327b396cd83SPoul-Henning Kamp {
328ae0eb976SPoul-Henning Kamp 	int error=0;
329b396cd83SPoul-Henning Kamp 
330ae0eb976SPoul-Henning Kamp 	if (arg2)
331ae0eb976SPoul-Henning Kamp 		error = SYSCTL_OUT(req, arg1, arg2);
332ae0eb976SPoul-Henning Kamp 	else
333ae0eb976SPoul-Henning Kamp 		error = SYSCTL_OUT(req, arg1, strlen((char *)arg1)+1);
334b396cd83SPoul-Henning Kamp 
335ae0eb976SPoul-Henning Kamp 	if (error || !req->newptr)
336ae0eb976SPoul-Henning Kamp 		return (error);
337ae0eb976SPoul-Henning Kamp 
338ae0eb976SPoul-Henning Kamp 	if ((req->newlen - req->newidx) > arg2) {
339ae0eb976SPoul-Henning Kamp 		error = E2BIG;
340ae0eb976SPoul-Henning Kamp 	} else {
341ae0eb976SPoul-Henning Kamp 		arg2 = (req->newlen - req->newidx);
342ae0eb976SPoul-Henning Kamp 		error = SYSCTL_IN(req, arg1, arg2);
343ae0eb976SPoul-Henning Kamp 		((char *)arg1)[arg2] = '\0';
344b396cd83SPoul-Henning Kamp 	}
345b396cd83SPoul-Henning Kamp 
3462e210993SPoul-Henning Kamp 	return (error);
347b396cd83SPoul-Henning Kamp }
348b396cd83SPoul-Henning Kamp 
349ae0eb976SPoul-Henning Kamp /*
350ae0eb976SPoul-Henning Kamp  * Handle any kind of opaque data.
351ae0eb976SPoul-Henning Kamp  * arg1 points to it, arg2 is the size.
352ae0eb976SPoul-Henning Kamp  */
353ae0eb976SPoul-Henning Kamp 
3543a34a5c3SPoul-Henning Kamp int
3553a34a5c3SPoul-Henning Kamp sysctl_handle_opaque SYSCTL_HANDLER_ARGS
356b396cd83SPoul-Henning Kamp {
357ae0eb976SPoul-Henning Kamp 	int error;
358b396cd83SPoul-Henning Kamp 
359ae0eb976SPoul-Henning Kamp 	error = SYSCTL_OUT(req, arg1, arg2);
360b396cd83SPoul-Henning Kamp 
361ae0eb976SPoul-Henning Kamp 	if (error || !req->newptr)
362ae0eb976SPoul-Henning Kamp 		return (error);
363ae0eb976SPoul-Henning Kamp 
364ae0eb976SPoul-Henning Kamp 	error = SYSCTL_IN(req, arg1, arg2);
365ae0eb976SPoul-Henning Kamp 
366ae0eb976SPoul-Henning Kamp 	return (error);
367b396cd83SPoul-Henning Kamp }
368ae0eb976SPoul-Henning Kamp 
369ae0eb976SPoul-Henning Kamp int
370ae0eb976SPoul-Henning Kamp sysctl_old_kernel(struct sysctl_req *req, void *p, int l)
371ae0eb976SPoul-Henning Kamp {
372ae0eb976SPoul-Henning Kamp 	int i = min(req->oldlen - req->oldidx, l);
373ae0eb976SPoul-Henning Kamp 	if (i) {
374ae0eb976SPoul-Henning Kamp 		bcopy(p, req->oldptr + req->oldidx, i);
375ae0eb976SPoul-Henning Kamp 		req->oldidx += i;
376ae0eb976SPoul-Henning Kamp 	}
377ae0eb976SPoul-Henning Kamp 	if (i != l)
378ae0eb976SPoul-Henning Kamp 		return (ENOMEM);
379ae0eb976SPoul-Henning Kamp 	else
3802e210993SPoul-Henning Kamp 		return (0);
381ae0eb976SPoul-Henning Kamp 
382ae0eb976SPoul-Henning Kamp }
383ae0eb976SPoul-Henning Kamp 
384ae0eb976SPoul-Henning Kamp int
385ae0eb976SPoul-Henning Kamp sysctl_new_kernel(struct sysctl_req *req, void *p, int l)
386ae0eb976SPoul-Henning Kamp {
387ae0eb976SPoul-Henning Kamp 	int i = req->newlen - req->newidx;
388ae0eb976SPoul-Henning Kamp 	if (i < l)
389ae0eb976SPoul-Henning Kamp 		return (EINVAL);
390ae0eb976SPoul-Henning Kamp 	bcopy(req->newptr + req->newidx, p, l);
391ae0eb976SPoul-Henning Kamp 	req->newidx += l;
392ae0eb976SPoul-Henning Kamp 	return (0);
393ae0eb976SPoul-Henning Kamp }
394ae0eb976SPoul-Henning Kamp 
395ae0eb976SPoul-Henning Kamp int
396ae0eb976SPoul-Henning Kamp sysctl_old_user(struct sysctl_req *req, void *p, int l)
397ae0eb976SPoul-Henning Kamp {
398ae0eb976SPoul-Henning Kamp 	int error , i = min(req->oldlen - req->oldidx, l);
399ae0eb976SPoul-Henning Kamp 
400ae0eb976SPoul-Henning Kamp 	error  = copyout(p, req->oldptr + req->oldidx, i);
401ae0eb976SPoul-Henning Kamp 	req->oldidx += i;
402ae0eb976SPoul-Henning Kamp 	if (error)
403ae0eb976SPoul-Henning Kamp 		return (error);
404ae0eb976SPoul-Henning Kamp 	if (i < l)
405ae0eb976SPoul-Henning Kamp 		return (ENOMEM);
406ae0eb976SPoul-Henning Kamp 	return (error);
407ae0eb976SPoul-Henning Kamp }
408ae0eb976SPoul-Henning Kamp 
409ae0eb976SPoul-Henning Kamp int
410ae0eb976SPoul-Henning Kamp sysctl_new_user(struct sysctl_req *req, void *p, int l)
411ae0eb976SPoul-Henning Kamp {
412ae0eb976SPoul-Henning Kamp 	int error, i = req->newlen - req->newidx;
413ae0eb976SPoul-Henning Kamp 	if (i < l)
414ae0eb976SPoul-Henning Kamp 		return (EINVAL);
415ae0eb976SPoul-Henning Kamp 	error = copyin(req->newptr + req->newidx, p, l);
416ae0eb976SPoul-Henning Kamp 	req->newidx += l;
417ae0eb976SPoul-Henning Kamp 	return (error);
418b396cd83SPoul-Henning Kamp }
419b396cd83SPoul-Henning Kamp 
4203a34a5c3SPoul-Henning Kamp #ifdef DEBUG
4213a34a5c3SPoul-Henning Kamp static sysctlfn debug_sysctl;
4223a34a5c3SPoul-Henning Kamp #endif
423b396cd83SPoul-Henning Kamp 
424df8bae1dSRodney W. Grimes /*
425df8bae1dSRodney W. Grimes  * Locking and stats
426df8bae1dSRodney W. Grimes  */
427df8bae1dSRodney W. Grimes static struct sysctl_lock {
428df8bae1dSRodney W. Grimes 	int	sl_lock;
429df8bae1dSRodney W. Grimes 	int	sl_want;
430df8bae1dSRodney W. Grimes 	int	sl_locked;
431df8bae1dSRodney W. Grimes } memlock;
432df8bae1dSRodney W. Grimes 
4332e210993SPoul-Henning Kamp 
4342e210993SPoul-Henning Kamp 
4352e210993SPoul-Henning Kamp /*
4362e210993SPoul-Henning Kamp  * Traverse our tree, and find the right node, execute whatever it points
4372e210993SPoul-Henning Kamp  * at, and return the resulting error code.
4382e210993SPoul-Henning Kamp  * We work entirely in kernel-space at this time.
4392e210993SPoul-Henning Kamp  */
4402e210993SPoul-Henning Kamp 
4412e210993SPoul-Henning Kamp 
4422e210993SPoul-Henning Kamp int
4432e210993SPoul-Henning Kamp sysctl_root SYSCTL_HANDLER_ARGS
4442e210993SPoul-Henning Kamp {
4452e210993SPoul-Henning Kamp 	int *name = (int *) arg1;
4462e210993SPoul-Henning Kamp 	int namelen = arg2;
4472e210993SPoul-Henning Kamp 	int indx, i, j;
4482e210993SPoul-Henning Kamp 	struct sysctl_oid **oidpp;
4492e210993SPoul-Henning Kamp 	struct linker_set *lsp = &sysctl_;
4502e210993SPoul-Henning Kamp 
4512e210993SPoul-Henning Kamp 	j = lsp->ls_length;
4522e210993SPoul-Henning Kamp 	oidpp = (struct sysctl_oid **) lsp->ls_items;
4532e210993SPoul-Henning Kamp 
4542e210993SPoul-Henning Kamp 	indx = 0;
4552e210993SPoul-Henning Kamp 	while (j-- && indx < CTL_MAXNAME) {
456787d58f2SPoul-Henning Kamp 		if (*oidpp && ((*oidpp)->oid_number == name[indx])) {
4572e210993SPoul-Henning Kamp 			indx++;
4582e210993SPoul-Henning Kamp 			if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
4592e210993SPoul-Henning Kamp 				if ((*oidpp)->oid_handler)
4602e210993SPoul-Henning Kamp 					goto found;
4612e210993SPoul-Henning Kamp 				if (indx == namelen)
4622e210993SPoul-Henning Kamp 					return ENOENT;
4632e210993SPoul-Henning Kamp 				lsp = (struct linker_set*)(*oidpp)->oid_arg1;
4642e210993SPoul-Henning Kamp 				j = lsp->ls_length;
4652e210993SPoul-Henning Kamp 				oidpp = (struct sysctl_oid **)lsp->ls_items;
4662e210993SPoul-Henning Kamp 			} else {
4672e210993SPoul-Henning Kamp 				if (indx != namelen)
4682e210993SPoul-Henning Kamp 					return EISDIR;
4692e210993SPoul-Henning Kamp 				goto found;
4702e210993SPoul-Henning Kamp 			}
4712e210993SPoul-Henning Kamp 		} else {
4722e210993SPoul-Henning Kamp 			oidpp++;
4732e210993SPoul-Henning Kamp 		}
4742e210993SPoul-Henning Kamp 	}
4752e210993SPoul-Henning Kamp 	return EJUSTRETURN;
4762e210993SPoul-Henning Kamp found:
4772e210993SPoul-Henning Kamp 
4782e210993SPoul-Henning Kamp 	/* If writing isn't allowed */
479ae0eb976SPoul-Henning Kamp 	if (req->newptr && !((*oidpp)->oid_kind & CTLFLAG_WR))
4802e210993SPoul-Henning Kamp 		return (EPERM);
4812e210993SPoul-Henning Kamp 
4822e210993SPoul-Henning Kamp 	if (!(*oidpp)->oid_handler)
4832e210993SPoul-Henning Kamp 		return EINVAL;
4842e210993SPoul-Henning Kamp 
4852e210993SPoul-Henning Kamp 	if (((*oidpp)->oid_kind & CTLTYPE) == CTLTYPE_NODE) {
4862e210993SPoul-Henning Kamp 		i = ((*oidpp)->oid_handler) (*oidpp,
4872e210993SPoul-Henning Kamp 					name + indx, namelen - indx,
488ae0eb976SPoul-Henning Kamp 					req);
4892e210993SPoul-Henning Kamp 	} else {
4902e210993SPoul-Henning Kamp 		i = ((*oidpp)->oid_handler) (*oidpp,
4912e210993SPoul-Henning Kamp 					(*oidpp)->oid_arg1, (*oidpp)->oid_arg2,
492ae0eb976SPoul-Henning Kamp 					req);
4932e210993SPoul-Henning Kamp 	}
4942e210993SPoul-Henning Kamp 	return (i);
4952e210993SPoul-Henning Kamp }
4962e210993SPoul-Henning Kamp 
497d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
498b8da2396SPoul-Henning Kamp struct sysctl_args {
499b8da2396SPoul-Henning Kamp 	int	*name;
500b8da2396SPoul-Henning Kamp 	u_int	namelen;
501b8da2396SPoul-Henning Kamp 	void	*old;
502b8da2396SPoul-Henning Kamp 	size_t	*oldlenp;
503b8da2396SPoul-Henning Kamp 	void	*new;
504b8da2396SPoul-Henning Kamp 	size_t	newlen;
505b8da2396SPoul-Henning Kamp };
506d2d3e875SBruce Evans #endif
507b8da2396SPoul-Henning Kamp 
508df8bae1dSRodney W. Grimes int
509df8bae1dSRodney W. Grimes __sysctl(p, uap, retval)
510df8bae1dSRodney W. Grimes 	struct proc *p;
511df8bae1dSRodney W. Grimes 	register struct sysctl_args *uap;
512df8bae1dSRodney W. Grimes 	int *retval;
513df8bae1dSRodney W. Grimes {
514b8da2396SPoul-Henning Kamp 	int error, name[CTL_MAXNAME];
515b396cd83SPoul-Henning Kamp 
516df8bae1dSRodney W. Grimes 	if (uap->namelen > CTL_MAXNAME || uap->namelen < 2)
517df8bae1dSRodney W. Grimes 		return (EINVAL);
518b396cd83SPoul-Henning Kamp 
519797f2d22SPoul-Henning Kamp  	error = copyin(uap->name, &name, uap->namelen * sizeof(int));
520797f2d22SPoul-Henning Kamp  	if (error)
521df8bae1dSRodney W. Grimes 		return (error);
522df8bae1dSRodney W. Grimes 
523b8da2396SPoul-Henning Kamp 	return (userland_sysctl(p, name, uap->namelen,
524b8da2396SPoul-Henning Kamp 		uap->old, uap->oldlenp, 0,
525b8da2396SPoul-Henning Kamp 		uap->new, uap->newlen, retval));
526b8da2396SPoul-Henning Kamp }
527b8da2396SPoul-Henning Kamp 
5286ff1bbebSPoul-Henning Kamp static sysctlfn kern_sysctl;
5296ff1bbebSPoul-Henning Kamp 
530b8da2396SPoul-Henning Kamp /*
531b8da2396SPoul-Henning Kamp  * This is used from various compatibility syscalls too.  That's why name
532b8da2396SPoul-Henning Kamp  * must be in kernel space.
533b8da2396SPoul-Henning Kamp  */
534b8da2396SPoul-Henning Kamp int
535b8da2396SPoul-Henning Kamp userland_sysctl(struct proc *p, int *name, u_int namelen, void *old, size_t *oldlenp, int inkernel, void *new, size_t newlen, int *retval)
536b8da2396SPoul-Henning Kamp {
537ae0eb976SPoul-Henning Kamp 	int error = 0, dolock = 1, i, oldlen = 0;
538ae0eb976SPoul-Henning Kamp 	u_int savelen = 0;
539b8da2396SPoul-Henning Kamp 	sysctlfn *fn;
540ae0eb976SPoul-Henning Kamp 	struct sysctl_req req;
541ae0eb976SPoul-Henning Kamp 
542ae0eb976SPoul-Henning Kamp 	bzero(&req, sizeof req);
543b8da2396SPoul-Henning Kamp 
544b8da2396SPoul-Henning Kamp 	if (new != NULL && (error = suser(p->p_ucred, &p->p_acflag)))
545b396cd83SPoul-Henning Kamp 		return (error);
546b396cd83SPoul-Henning Kamp 
547b8da2396SPoul-Henning Kamp 	if (oldlenp) {
548b8da2396SPoul-Henning Kamp 		if (inkernel) {
549ae0eb976SPoul-Henning Kamp 			req.oldlen = *oldlenp;
550b8da2396SPoul-Henning Kamp 		} else {
551ae0eb976SPoul-Henning Kamp 			error = copyin(oldlenp, &req.oldlen,
552ae0eb976SPoul-Henning Kamp 				sizeof(req.oldlen));
553b8da2396SPoul-Henning Kamp 			if (error)
554b8da2396SPoul-Henning Kamp 				return (error);
555b8da2396SPoul-Henning Kamp 		}
556ae0eb976SPoul-Henning Kamp 		oldlen = req.oldlen;
557b8da2396SPoul-Henning Kamp 	}
558b8da2396SPoul-Henning Kamp 
559ae0eb976SPoul-Henning Kamp 	if (old) {
560ae0eb976SPoul-Henning Kamp 		if (!useracc(old, req.oldlen, B_WRITE))
561ae0eb976SPoul-Henning Kamp 			return (EFAULT);
562ae0eb976SPoul-Henning Kamp 		req.oldptr= old;
563ae0eb976SPoul-Henning Kamp 	}
5642e210993SPoul-Henning Kamp 
565b8da2396SPoul-Henning Kamp 	if (newlen) {
566ae0eb976SPoul-Henning Kamp 		if (!useracc(new, req.newlen, B_READ))
567ae0eb976SPoul-Henning Kamp 			return (EFAULT);
568ae0eb976SPoul-Henning Kamp 		req.newlen = newlen;
569ae0eb976SPoul-Henning Kamp 		req.newptr = new;
570b396cd83SPoul-Henning Kamp 	}
571b396cd83SPoul-Henning Kamp 
572ae0eb976SPoul-Henning Kamp 	req.oldfunc = sysctl_old_user;
573ae0eb976SPoul-Henning Kamp 	req.newfunc = sysctl_new_user;
574ae0eb976SPoul-Henning Kamp 
575ae0eb976SPoul-Henning Kamp 	error = sysctl_root(0, name, namelen, &req);
576b396cd83SPoul-Henning Kamp 
5772e210993SPoul-Henning Kamp         if (!error || error == ENOMEM) {
57869feb388SPoul-Henning Kamp 		if (retval)
579ae0eb976SPoul-Henning Kamp 			*retval = req.oldlen;
580b8da2396SPoul-Henning Kamp 		if (oldlenp) {
581b8da2396SPoul-Henning Kamp 			if (inkernel) {
582ae0eb976SPoul-Henning Kamp 				*oldlenp = req.oldlen;
583b8da2396SPoul-Henning Kamp 			} else {
584ae0eb976SPoul-Henning Kamp 				i = copyout(&req.oldlen, oldlenp,
585ae0eb976SPoul-Henning Kamp 					sizeof(req.oldlen));
586b396cd83SPoul-Henning Kamp 				if (i)
587b396cd83SPoul-Henning Kamp 					error = i;
5882e210993SPoul-Henning Kamp 			}
589b8da2396SPoul-Henning Kamp 		}
5902e210993SPoul-Henning Kamp 		return (error);
5912e210993SPoul-Henning Kamp 	}
592b396cd83SPoul-Henning Kamp 
593df8bae1dSRodney W. Grimes 	switch (name[0]) {
594df8bae1dSRodney W. Grimes 	case CTL_KERN:
595df8bae1dSRodney W. Grimes 		fn = kern_sysctl;
596e9e2a852SAndrey A. Chernov 		if (name[1] != KERN_VNODE)      /* XXX */
597df8bae1dSRodney W. Grimes 			dolock = 0;
598df8bae1dSRodney W. Grimes 		break;
599df8bae1dSRodney W. Grimes 	case CTL_HW:
600df8bae1dSRodney W. Grimes 		fn = hw_sysctl;
601df8bae1dSRodney W. Grimes 		break;
602df8bae1dSRodney W. Grimes 	case CTL_VM:
603df8bae1dSRodney W. Grimes 		fn = vm_sysctl;
604df8bae1dSRodney W. Grimes 		break;
605df8bae1dSRodney W. Grimes 	case CTL_NET:
606df8bae1dSRodney W. Grimes 		fn = net_sysctl;
607df8bae1dSRodney W. Grimes 		break;
608df8bae1dSRodney W. Grimes 	case CTL_FS:
609df8bae1dSRodney W. Grimes 		fn = fs_sysctl;
610df8bae1dSRodney W. Grimes 		break;
611df8bae1dSRodney W. Grimes #ifdef DEBUG
612df8bae1dSRodney W. Grimes 	case CTL_DEBUG:
613df8bae1dSRodney W. Grimes 		fn = debug_sysctl;
614df8bae1dSRodney W. Grimes 		break;
615df8bae1dSRodney W. Grimes #endif
616df8bae1dSRodney W. Grimes 	default:
617df8bae1dSRodney W. Grimes 		return (EOPNOTSUPP);
618df8bae1dSRodney W. Grimes 	}
619b8da2396SPoul-Henning Kamp 	if (old != NULL) {
620b8da2396SPoul-Henning Kamp 		if (!useracc(old, oldlen, B_WRITE))
621df8bae1dSRodney W. Grimes 			return (EFAULT);
622df8bae1dSRodney W. Grimes 		while (memlock.sl_lock) {
623df8bae1dSRodney W. Grimes 			memlock.sl_want = 1;
62482478919SDavid Greenman 			(void) tsleep((caddr_t)&memlock, PRIBIO+1, "sysctl", 0);
625df8bae1dSRodney W. Grimes 			memlock.sl_locked++;
626df8bae1dSRodney W. Grimes 		}
627df8bae1dSRodney W. Grimes 		memlock.sl_lock = 1;
628df8bae1dSRodney W. Grimes 		if (dolock)
629b8da2396SPoul-Henning Kamp 			vslock(old, oldlen);
630df8bae1dSRodney W. Grimes 		savelen = oldlen;
631df8bae1dSRodney W. Grimes 	}
632b396cd83SPoul-Henning Kamp 
633b396cd83SPoul-Henning Kamp 
634b8da2396SPoul-Henning Kamp 	error = (*fn)(name + 1, namelen - 1, old, &oldlen,
635b8da2396SPoul-Henning Kamp 	    new, newlen, p);
6362e210993SPoul-Henning Kamp 
6372e210993SPoul-Henning Kamp 
638b8da2396SPoul-Henning Kamp 	if (old != NULL) {
639df8bae1dSRodney W. Grimes 		if (dolock)
640b8da2396SPoul-Henning Kamp 			vsunlock(old, savelen, B_WRITE);
641df8bae1dSRodney W. Grimes 		memlock.sl_lock = 0;
642df8bae1dSRodney W. Grimes 		if (memlock.sl_want) {
643df8bae1dSRodney W. Grimes 			memlock.sl_want = 0;
644df8bae1dSRodney W. Grimes 			wakeup((caddr_t)&memlock);
645df8bae1dSRodney W. Grimes 		}
646df8bae1dSRodney W. Grimes 	}
647787d58f2SPoul-Henning Kamp #if 0
648787d58f2SPoul-Henning Kamp 	if (error) {
649787d58f2SPoul-Henning Kamp 		printf("SYSCTL_ERROR: ");
650b8da2396SPoul-Henning Kamp 		for(i=0;i<namelen;i++)
651787d58f2SPoul-Henning Kamp 			printf("%d ", name[i]);
652787d58f2SPoul-Henning Kamp 		printf("= %d\n", error);
653787d58f2SPoul-Henning Kamp 	}
654787d58f2SPoul-Henning Kamp #endif
655df8bae1dSRodney W. Grimes 	if (error)
656df8bae1dSRodney W. Grimes 		return (error);
657b8da2396SPoul-Henning Kamp 	if (retval)
658df8bae1dSRodney W. Grimes 		*retval = oldlen;
659b8da2396SPoul-Henning Kamp 	if (oldlenp) {
660b8da2396SPoul-Henning Kamp 		if (inkernel) {
661b8da2396SPoul-Henning Kamp 			*oldlenp = oldlen;
662b8da2396SPoul-Henning Kamp 		} else {
663b8da2396SPoul-Henning Kamp 			error = copyout(&oldlen, oldlenp, sizeof(oldlen));
664b8da2396SPoul-Henning Kamp 		}
665b8da2396SPoul-Henning Kamp 	}
666b8da2396SPoul-Henning Kamp 	return (error);
667df8bae1dSRodney W. Grimes }
668df8bae1dSRodney W. Grimes 
669df8bae1dSRodney W. Grimes /*
670df8bae1dSRodney W. Grimes  * Attributes stored in the kernel.
671df8bae1dSRodney W. Grimes  */
6726ae6a09bSGarrett Wollman int securelevel = -1;
673df8bae1dSRodney W. Grimes 
674df8bae1dSRodney W. Grimes /*
675df8bae1dSRodney W. Grimes  * kernel related system variables.
676df8bae1dSRodney W. Grimes  */
6773c8e79ddSBruce Evans static int
678df8bae1dSRodney W. Grimes kern_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
679df8bae1dSRodney W. Grimes 	int *name;
680df8bae1dSRodney W. Grimes 	u_int namelen;
681df8bae1dSRodney W. Grimes 	void *oldp;
682df8bae1dSRodney W. Grimes 	size_t *oldlenp;
683df8bae1dSRodney W. Grimes 	void *newp;
684df8bae1dSRodney W. Grimes 	size_t newlen;
685df8bae1dSRodney W. Grimes 	struct proc *p;
686df8bae1dSRodney W. Grimes {
6872e210993SPoul-Henning Kamp 	int error, level;
6885bb4f738SGarrett Wollman 	dev_t ndumpdev;
689df8bae1dSRodney W. Grimes 
690df8bae1dSRodney W. Grimes 	/* all sysctl names at this level are terminal */
6913f31c649SGarrett Wollman 	if (namelen != 1 && !(name[0] == KERN_PROC || name[0] == KERN_PROF
6923f31c649SGarrett Wollman 			      || name[0] == KERN_NTP_PLL))
693df8bae1dSRodney W. Grimes 		return (ENOTDIR);		/* overloaded */
694df8bae1dSRodney W. Grimes 
695df8bae1dSRodney W. Grimes 	switch (name[0]) {
696b396cd83SPoul-Henning Kamp 
697df8bae1dSRodney W. Grimes 	case KERN_SECURELVL:
698df8bae1dSRodney W. Grimes 		level = securelevel;
699df8bae1dSRodney W. Grimes 		if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &level)) ||
700df8bae1dSRodney W. Grimes 		    newp == NULL)
701df8bae1dSRodney W. Grimes 			return (error);
702df8bae1dSRodney W. Grimes 		if (level < securelevel && p->p_pid != 1)
703df8bae1dSRodney W. Grimes 			return (EPERM);
704df8bae1dSRodney W. Grimes 		securelevel = level;
705df8bae1dSRodney W. Grimes 		return (0);
706df8bae1dSRodney W. Grimes 	case KERN_VNODE:
707df8bae1dSRodney W. Grimes 		return (sysctl_vnode(oldp, oldlenp));
708df8bae1dSRodney W. Grimes 	case KERN_PROC:
709df8bae1dSRodney W. Grimes 		return (sysctl_doproc(name + 1, namelen - 1, oldp, oldlenp));
710df8bae1dSRodney W. Grimes 	case KERN_FILE:
711df8bae1dSRodney W. Grimes 		return (sysctl_file(oldp, oldlenp));
712df8bae1dSRodney W. Grimes #ifdef GPROF
713df8bae1dSRodney W. Grimes 	case KERN_PROF:
714df8bae1dSRodney W. Grimes 		return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp,
715df8bae1dSRodney W. Grimes 		    newp, newlen));
716df8bae1dSRodney W. Grimes #endif
7173f31c649SGarrett Wollman 	case KERN_NTP_PLL:
7183f31c649SGarrett Wollman 		return (ntp_sysctl(name + 1, namelen - 1, oldp, oldlenp,
7193f31c649SGarrett Wollman 				   newp, newlen, p));
7205bb4f738SGarrett Wollman 	case KERN_DUMPDEV:
7215bb4f738SGarrett Wollman 		ndumpdev = dumpdev;
7225bb4f738SGarrett Wollman 		error = sysctl_struct(oldp, oldlenp, newp, newlen, &ndumpdev,
7235bb4f738SGarrett Wollman 				      sizeof ndumpdev);
7245bb4f738SGarrett Wollman 		if (!error && ndumpdev != dumpdev) {
7255bb4f738SGarrett Wollman 			error = setdumpdev(ndumpdev);
7265bb4f738SGarrett Wollman 		}
7275bb4f738SGarrett Wollman 		return error;
728df8bae1dSRodney W. Grimes 	default:
729df8bae1dSRodney W. Grimes 		return (EOPNOTSUPP);
730df8bae1dSRodney W. Grimes 	}
731df8bae1dSRodney W. Grimes 	/* NOTREACHED */
732df8bae1dSRodney W. Grimes }
733df8bae1dSRodney W. Grimes 
734df8bae1dSRodney W. Grimes /*
735df8bae1dSRodney W. Grimes  * hardware related system variables.
736df8bae1dSRodney W. Grimes  */
73726f9a767SRodney W. Grimes int
738df8bae1dSRodney W. Grimes hw_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
739df8bae1dSRodney W. Grimes 	int *name;
740df8bae1dSRodney W. Grimes 	u_int namelen;
741df8bae1dSRodney W. Grimes 	void *oldp;
742df8bae1dSRodney W. Grimes 	size_t *oldlenp;
743df8bae1dSRodney W. Grimes 	void *newp;
744df8bae1dSRodney W. Grimes 	size_t newlen;
745df8bae1dSRodney W. Grimes 	struct proc *p;
746df8bae1dSRodney W. Grimes {
7478478cabaSGarrett Wollman 	/* almost all sysctl names at this level are terminal */
7488478cabaSGarrett Wollman 	if (namelen != 1 && name[0] != HW_DEVCONF)
749df8bae1dSRodney W. Grimes 		return (ENOTDIR);		/* overloaded */
750df8bae1dSRodney W. Grimes 
751df8bae1dSRodney W. Grimes 	switch (name[0]) {
752df8bae1dSRodney W. Grimes 	case HW_PHYSMEM:
753df8bae1dSRodney W. Grimes 		return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem)));
754df8bae1dSRodney W. Grimes 	case HW_USERMEM:
755df8bae1dSRodney W. Grimes 		return (sysctl_rdint(oldp, oldlenp, newp,
756df8bae1dSRodney W. Grimes 		    ctob(physmem - cnt.v_wire_count)));
7578478cabaSGarrett Wollman 	case HW_DEVCONF:
7588478cabaSGarrett Wollman 		return (dev_sysctl(name + 1, namelen - 1, oldp, oldlenp,
7598478cabaSGarrett Wollman 				   newp, newlen, p));
760df8bae1dSRodney W. Grimes 	default:
761df8bae1dSRodney W. Grimes 		return (EOPNOTSUPP);
762df8bae1dSRodney W. Grimes 	}
763df8bae1dSRodney W. Grimes 	/* NOTREACHED */
764df8bae1dSRodney W. Grimes }
765df8bae1dSRodney W. Grimes 
766df8bae1dSRodney W. Grimes #ifdef DEBUG
767df8bae1dSRodney W. Grimes /*
768df8bae1dSRodney W. Grimes  * Debugging related system variables.
769df8bae1dSRodney W. Grimes  */
770df8bae1dSRodney W. Grimes struct ctldebug debug0, debug1, debug2, debug3, debug4;
771df8bae1dSRodney W. Grimes struct ctldebug debug5, debug6, debug7, debug8, debug9;
772df8bae1dSRodney W. Grimes struct ctldebug debug10, debug11, debug12, debug13, debug14;
773df8bae1dSRodney W. Grimes struct ctldebug debug15, debug16, debug17, debug18, debug19;
774df8bae1dSRodney W. Grimes static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
775df8bae1dSRodney W. Grimes 	&debug0, &debug1, &debug2, &debug3, &debug4,
776df8bae1dSRodney W. Grimes 	&debug5, &debug6, &debug7, &debug8, &debug9,
777df8bae1dSRodney W. Grimes 	&debug10, &debug11, &debug12, &debug13, &debug14,
778df8bae1dSRodney W. Grimes 	&debug15, &debug16, &debug17, &debug18, &debug19,
779df8bae1dSRodney W. Grimes };
7806124ac44SBruce Evans static int
781df8bae1dSRodney W. Grimes debug_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
782df8bae1dSRodney W. Grimes 	int *name;
783df8bae1dSRodney W. Grimes 	u_int namelen;
784df8bae1dSRodney W. Grimes 	void *oldp;
785df8bae1dSRodney W. Grimes 	size_t *oldlenp;
786df8bae1dSRodney W. Grimes 	void *newp;
787df8bae1dSRodney W. Grimes 	size_t newlen;
788df8bae1dSRodney W. Grimes 	struct proc *p;
789df8bae1dSRodney W. Grimes {
790df8bae1dSRodney W. Grimes 	struct ctldebug *cdp;
791df8bae1dSRodney W. Grimes 
792df8bae1dSRodney W. Grimes 	/* all sysctl names at this level are name and field */
793df8bae1dSRodney W. Grimes 	if (namelen != 2)
794df8bae1dSRodney W. Grimes 		return (ENOTDIR);		/* overloaded */
795df8bae1dSRodney W. Grimes 	cdp = debugvars[name[0]];
796df8bae1dSRodney W. Grimes 	if (cdp->debugname == 0)
797df8bae1dSRodney W. Grimes 		return (EOPNOTSUPP);
798df8bae1dSRodney W. Grimes 	switch (name[1]) {
799df8bae1dSRodney W. Grimes 	case CTL_DEBUG_NAME:
800df8bae1dSRodney W. Grimes 		return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname));
801df8bae1dSRodney W. Grimes 	case CTL_DEBUG_VALUE:
802df8bae1dSRodney W. Grimes 		return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar));
803df8bae1dSRodney W. Grimes 	default:
804df8bae1dSRodney W. Grimes 		return (EOPNOTSUPP);
805df8bae1dSRodney W. Grimes 	}
806df8bae1dSRodney W. Grimes 	/* NOTREACHED */
807df8bae1dSRodney W. Grimes }
808df8bae1dSRodney W. Grimes #endif /* DEBUG */
809df8bae1dSRodney W. Grimes 
810df8bae1dSRodney W. Grimes /*
811df8bae1dSRodney W. Grimes  * Validate parameters and get old / set new parameters
812df8bae1dSRodney W. Grimes  * for an integer-valued sysctl function.
813df8bae1dSRodney W. Grimes  */
81426f9a767SRodney W. Grimes int
815df8bae1dSRodney W. Grimes sysctl_int(oldp, oldlenp, newp, newlen, valp)
816df8bae1dSRodney W. Grimes 	void *oldp;
817df8bae1dSRodney W. Grimes 	size_t *oldlenp;
818df8bae1dSRodney W. Grimes 	void *newp;
819df8bae1dSRodney W. Grimes 	size_t newlen;
820df8bae1dSRodney W. Grimes 	int *valp;
821df8bae1dSRodney W. Grimes {
822df8bae1dSRodney W. Grimes 	int error = 0;
823df8bae1dSRodney W. Grimes 
824df8bae1dSRodney W. Grimes 	if (oldp && *oldlenp < sizeof(int))
825df8bae1dSRodney W. Grimes 		return (ENOMEM);
826df8bae1dSRodney W. Grimes 	if (newp && newlen != sizeof(int))
827df8bae1dSRodney W. Grimes 		return (EINVAL);
828df8bae1dSRodney W. Grimes 	*oldlenp = sizeof(int);
829df8bae1dSRodney W. Grimes 	if (oldp)
830df8bae1dSRodney W. Grimes 		error = copyout(valp, oldp, sizeof(int));
831df8bae1dSRodney W. Grimes 	if (error == 0 && newp)
832df8bae1dSRodney W. Grimes 		error = copyin(newp, valp, sizeof(int));
833df8bae1dSRodney W. Grimes 	return (error);
834df8bae1dSRodney W. Grimes }
835df8bae1dSRodney W. Grimes 
836df8bae1dSRodney W. Grimes /*
837df8bae1dSRodney W. Grimes  * As above, but read-only.
838df8bae1dSRodney W. Grimes  */
83926f9a767SRodney W. Grimes int
840df8bae1dSRodney W. Grimes sysctl_rdint(oldp, oldlenp, newp, val)
841df8bae1dSRodney W. Grimes 	void *oldp;
842df8bae1dSRodney W. Grimes 	size_t *oldlenp;
843df8bae1dSRodney W. Grimes 	void *newp;
844df8bae1dSRodney W. Grimes 	int val;
845df8bae1dSRodney W. Grimes {
846df8bae1dSRodney W. Grimes 	int error = 0;
847df8bae1dSRodney W. Grimes 
848df8bae1dSRodney W. Grimes 	if (oldp && *oldlenp < sizeof(int))
849df8bae1dSRodney W. Grimes 		return (ENOMEM);
850df8bae1dSRodney W. Grimes 	if (newp)
851df8bae1dSRodney W. Grimes 		return (EPERM);
852df8bae1dSRodney W. Grimes 	*oldlenp = sizeof(int);
853df8bae1dSRodney W. Grimes 	if (oldp)
854df8bae1dSRodney W. Grimes 		error = copyout((caddr_t)&val, oldp, sizeof(int));
855df8bae1dSRodney W. Grimes 	return (error);
856df8bae1dSRodney W. Grimes }
857df8bae1dSRodney W. Grimes 
858df8bae1dSRodney W. Grimes /*
859df8bae1dSRodney W. Grimes  * Validate parameters and get old / set new parameters
860df8bae1dSRodney W. Grimes  * for a string-valued sysctl function.
861df8bae1dSRodney W. Grimes  */
86226f9a767SRodney W. Grimes int
863df8bae1dSRodney W. Grimes sysctl_string(oldp, oldlenp, newp, newlen, str, maxlen)
864df8bae1dSRodney W. Grimes 	void *oldp;
865df8bae1dSRodney W. Grimes 	size_t *oldlenp;
866df8bae1dSRodney W. Grimes 	void *newp;
867df8bae1dSRodney W. Grimes 	size_t newlen;
868df8bae1dSRodney W. Grimes 	char *str;
869df8bae1dSRodney W. Grimes 	int maxlen;
870df8bae1dSRodney W. Grimes {
871e1453736SMike Pritchard 	int len, error = 0, rval = 0;
872df8bae1dSRodney W. Grimes 
873df8bae1dSRodney W. Grimes 	len = strlen(str) + 1;
874e1453736SMike Pritchard 	if (oldp && *oldlenp < len) {
875f81fcd41SGuido van Rooij 		len = *oldlenp;
876e1453736SMike Pritchard 		rval = ENOMEM;
877e1453736SMike Pritchard 	}
878df8bae1dSRodney W. Grimes 	if (newp && newlen >= maxlen)
879df8bae1dSRodney W. Grimes 		return (EINVAL);
880df8bae1dSRodney W. Grimes 	if (oldp) {
881df8bae1dSRodney W. Grimes 		*oldlenp = len;
882df8bae1dSRodney W. Grimes 		error = copyout(str, oldp, len);
883e1453736SMike Pritchard 		if (error)
884e1453736SMike Pritchard 			rval = error;
885df8bae1dSRodney W. Grimes 	}
886e1453736SMike Pritchard 	if ((error == 0 || error == ENOMEM) && newp) {
887df8bae1dSRodney W. Grimes 		error = copyin(newp, str, newlen);
888e1453736SMike Pritchard 		if (error)
889e1453736SMike Pritchard 			rval = error;
890df8bae1dSRodney W. Grimes 		str[newlen] = 0;
891df8bae1dSRodney W. Grimes 	}
892e1453736SMike Pritchard 	return (rval);
893df8bae1dSRodney W. Grimes }
894df8bae1dSRodney W. Grimes 
895df8bae1dSRodney W. Grimes /*
896df8bae1dSRodney W. Grimes  * As above, but read-only.
897df8bae1dSRodney W. Grimes  */
89826f9a767SRodney W. Grimes int
899df8bae1dSRodney W. Grimes sysctl_rdstring(oldp, oldlenp, newp, str)
900df8bae1dSRodney W. Grimes 	void *oldp;
901df8bae1dSRodney W. Grimes 	size_t *oldlenp;
902df8bae1dSRodney W. Grimes 	void *newp;
903df8bae1dSRodney W. Grimes 	char *str;
904df8bae1dSRodney W. Grimes {
905e1453736SMike Pritchard 	int len, error = 0, rval = 0;
906df8bae1dSRodney W. Grimes 
907df8bae1dSRodney W. Grimes 	len = strlen(str) + 1;
908e1453736SMike Pritchard 	if (oldp && *oldlenp < len) {
909e1453736SMike Pritchard 		len = *oldlenp;
910e1453736SMike Pritchard 		rval = ENOMEM;
911e1453736SMike Pritchard 	}
912df8bae1dSRodney W. Grimes 	if (newp)
913df8bae1dSRodney W. Grimes 		return (EPERM);
914df8bae1dSRodney W. Grimes 	*oldlenp = len;
915df8bae1dSRodney W. Grimes 	if (oldp)
916df8bae1dSRodney W. Grimes 		error = copyout(str, oldp, len);
917e1453736SMike Pritchard 		if (error)
918e1453736SMike Pritchard 			rval = error;
919e1453736SMike Pritchard 	return (rval);
920df8bae1dSRodney W. Grimes }
921df8bae1dSRodney W. Grimes 
922df8bae1dSRodney W. Grimes /*
923df8bae1dSRodney W. Grimes  * Validate parameters and get old / set new parameters
924df8bae1dSRodney W. Grimes  * for a structure oriented sysctl function.
925df8bae1dSRodney W. Grimes  */
92626f9a767SRodney W. Grimes int
927df8bae1dSRodney W. Grimes sysctl_struct(oldp, oldlenp, newp, newlen, sp, len)
928df8bae1dSRodney W. Grimes 	void *oldp;
929df8bae1dSRodney W. Grimes 	size_t *oldlenp;
930df8bae1dSRodney W. Grimes 	void *newp;
931df8bae1dSRodney W. Grimes 	size_t newlen;
932df8bae1dSRodney W. Grimes 	void *sp;
933df8bae1dSRodney W. Grimes 	int len;
934df8bae1dSRodney W. Grimes {
935df8bae1dSRodney W. Grimes 	int error = 0;
936df8bae1dSRodney W. Grimes 
937df8bae1dSRodney W. Grimes 	if (oldp && *oldlenp < len)
938df8bae1dSRodney W. Grimes 		return (ENOMEM);
939df8bae1dSRodney W. Grimes 	if (newp && newlen > len)
940df8bae1dSRodney W. Grimes 		return (EINVAL);
941df8bae1dSRodney W. Grimes 	if (oldp) {
942df8bae1dSRodney W. Grimes 		*oldlenp = len;
943df8bae1dSRodney W. Grimes 		error = copyout(sp, oldp, len);
944df8bae1dSRodney W. Grimes 	}
945df8bae1dSRodney W. Grimes 	if (error == 0 && newp)
946df8bae1dSRodney W. Grimes 		error = copyin(newp, sp, len);
947df8bae1dSRodney W. Grimes 	return (error);
948df8bae1dSRodney W. Grimes }
949df8bae1dSRodney W. Grimes 
950df8bae1dSRodney W. Grimes /*
951df8bae1dSRodney W. Grimes  * Validate parameters and get old parameters
952df8bae1dSRodney W. Grimes  * for a structure oriented sysctl function.
953df8bae1dSRodney W. Grimes  */
95426f9a767SRodney W. Grimes int
955df8bae1dSRodney W. Grimes sysctl_rdstruct(oldp, oldlenp, newp, sp, len)
956df8bae1dSRodney W. Grimes 	void *oldp;
957df8bae1dSRodney W. Grimes 	size_t *oldlenp;
958df8bae1dSRodney W. Grimes 	void *newp, *sp;
959df8bae1dSRodney W. Grimes 	int len;
960df8bae1dSRodney W. Grimes {
961df8bae1dSRodney W. Grimes 	int error = 0;
962df8bae1dSRodney W. Grimes 
963df8bae1dSRodney W. Grimes 	if (oldp && *oldlenp < len)
964df8bae1dSRodney W. Grimes 		return (ENOMEM);
965df8bae1dSRodney W. Grimes 	if (newp)
966df8bae1dSRodney W. Grimes 		return (EPERM);
967df8bae1dSRodney W. Grimes 	*oldlenp = len;
968df8bae1dSRodney W. Grimes 	if (oldp)
969df8bae1dSRodney W. Grimes 		error = copyout(sp, oldp, len);
970df8bae1dSRodney W. Grimes 	return (error);
971df8bae1dSRodney W. Grimes }
972df8bae1dSRodney W. Grimes 
973df8bae1dSRodney W. Grimes /*
974df8bae1dSRodney W. Grimes  * Get file structures.
975df8bae1dSRodney W. Grimes  */
97626f9a767SRodney W. Grimes int
977df8bae1dSRodney W. Grimes sysctl_file(where, sizep)
978df8bae1dSRodney W. Grimes 	char *where;
979df8bae1dSRodney W. Grimes 	size_t *sizep;
980df8bae1dSRodney W. Grimes {
981df8bae1dSRodney W. Grimes 	int buflen, error;
982df8bae1dSRodney W. Grimes 	struct file *fp;
983df8bae1dSRodney W. Grimes 	char *start = where;
984df8bae1dSRodney W. Grimes 
985df8bae1dSRodney W. Grimes 	buflen = *sizep;
986df8bae1dSRodney W. Grimes 	if (where == NULL) {
987df8bae1dSRodney W. Grimes 		/*
988df8bae1dSRodney W. Grimes 		 * overestimate by 10 files
989df8bae1dSRodney W. Grimes 		 */
990df8bae1dSRodney W. Grimes 		*sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
991df8bae1dSRodney W. Grimes 		return (0);
992df8bae1dSRodney W. Grimes 	}
993df8bae1dSRodney W. Grimes 
994df8bae1dSRodney W. Grimes 	/*
995df8bae1dSRodney W. Grimes 	 * first copyout filehead
996df8bae1dSRodney W. Grimes 	 */
997df8bae1dSRodney W. Grimes 	if (buflen < sizeof(filehead)) {
998df8bae1dSRodney W. Grimes 		*sizep = 0;
999df8bae1dSRodney W. Grimes 		return (0);
1000df8bae1dSRodney W. Grimes 	}
1001797f2d22SPoul-Henning Kamp 	error = copyout((caddr_t)&filehead, where, sizeof(filehead));
1002797f2d22SPoul-Henning Kamp 	if (error)
1003df8bae1dSRodney W. Grimes 		return (error);
1004df8bae1dSRodney W. Grimes 	buflen -= sizeof(filehead);
1005df8bae1dSRodney W. Grimes 	where += sizeof(filehead);
1006df8bae1dSRodney W. Grimes 
1007df8bae1dSRodney W. Grimes 	/*
1008df8bae1dSRodney W. Grimes 	 * followed by an array of file structures
1009df8bae1dSRodney W. Grimes 	 */
1010df8bae1dSRodney W. Grimes 	for (fp = filehead; fp != NULL; fp = fp->f_filef) {
1011df8bae1dSRodney W. Grimes 		if (buflen < sizeof(struct file)) {
1012df8bae1dSRodney W. Grimes 			*sizep = where - start;
1013df8bae1dSRodney W. Grimes 			return (ENOMEM);
1014df8bae1dSRodney W. Grimes 		}
1015797f2d22SPoul-Henning Kamp 		error = copyout((caddr_t)fp, where, sizeof (struct file));
1016797f2d22SPoul-Henning Kamp 		if (error)
1017df8bae1dSRodney W. Grimes 			return (error);
1018df8bae1dSRodney W. Grimes 		buflen -= sizeof(struct file);
1019df8bae1dSRodney W. Grimes 		where += sizeof(struct file);
1020df8bae1dSRodney W. Grimes 	}
1021df8bae1dSRodney W. Grimes 	*sizep = where - start;
1022df8bae1dSRodney W. Grimes 	return (0);
1023df8bae1dSRodney W. Grimes }
1024df8bae1dSRodney W. Grimes 
1025df8bae1dSRodney W. Grimes /*
1026df8bae1dSRodney W. Grimes  * try over estimating by 5 procs
1027df8bae1dSRodney W. Grimes  */
1028df8bae1dSRodney W. Grimes #define KERN_PROCSLOP	(5 * sizeof (struct kinfo_proc))
1029df8bae1dSRodney W. Grimes 
103026f9a767SRodney W. Grimes int
1031df8bae1dSRodney W. Grimes sysctl_doproc(name, namelen, where, sizep)
1032df8bae1dSRodney W. Grimes 	int *name;
1033df8bae1dSRodney W. Grimes 	u_int namelen;
1034df8bae1dSRodney W. Grimes 	char *where;
1035df8bae1dSRodney W. Grimes 	size_t *sizep;
1036df8bae1dSRodney W. Grimes {
1037df8bae1dSRodney W. Grimes 	register struct proc *p;
1038df8bae1dSRodney W. Grimes 	register struct kinfo_proc *dp = (struct kinfo_proc *)where;
1039df8bae1dSRodney W. Grimes 	register int needed = 0;
1040df8bae1dSRodney W. Grimes 	int buflen = where != NULL ? *sizep : 0;
1041df8bae1dSRodney W. Grimes 	int doingzomb;
1042df8bae1dSRodney W. Grimes 	struct eproc eproc;
1043df8bae1dSRodney W. Grimes 	int error = 0;
1044df8bae1dSRodney W. Grimes 
1045df8bae1dSRodney W. Grimes 	if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
1046df8bae1dSRodney W. Grimes 		return (EINVAL);
1047df8bae1dSRodney W. Grimes 	p = (struct proc *)allproc;
1048df8bae1dSRodney W. Grimes 	doingzomb = 0;
1049df8bae1dSRodney W. Grimes again:
1050df8bae1dSRodney W. Grimes 	for (; p != NULL; p = p->p_next) {
1051df8bae1dSRodney W. Grimes 		/*
1052df8bae1dSRodney W. Grimes 		 * Skip embryonic processes.
1053df8bae1dSRodney W. Grimes 		 */
1054df8bae1dSRodney W. Grimes 		if (p->p_stat == SIDL)
1055df8bae1dSRodney W. Grimes 			continue;
1056df8bae1dSRodney W. Grimes 		/*
1057df8bae1dSRodney W. Grimes 		 * TODO - make more efficient (see notes below).
1058df8bae1dSRodney W. Grimes 		 * do by session.
1059df8bae1dSRodney W. Grimes 		 */
1060df8bae1dSRodney W. Grimes 		switch (name[0]) {
1061df8bae1dSRodney W. Grimes 
1062df8bae1dSRodney W. Grimes 		case KERN_PROC_PID:
1063df8bae1dSRodney W. Grimes 			/* could do this with just a lookup */
1064df8bae1dSRodney W. Grimes 			if (p->p_pid != (pid_t)name[1])
1065df8bae1dSRodney W. Grimes 				continue;
1066df8bae1dSRodney W. Grimes 			break;
1067df8bae1dSRodney W. Grimes 
1068df8bae1dSRodney W. Grimes 		case KERN_PROC_PGRP:
1069df8bae1dSRodney W. Grimes 			/* could do this by traversing pgrp */
1070be6a1d14SDavid Greenman 			if (p->p_pgrp == NULL || p->p_pgrp->pg_id != (pid_t)name[1])
1071df8bae1dSRodney W. Grimes 				continue;
1072df8bae1dSRodney W. Grimes 			break;
1073df8bae1dSRodney W. Grimes 
1074df8bae1dSRodney W. Grimes 		case KERN_PROC_TTY:
1075df8bae1dSRodney W. Grimes 			if ((p->p_flag & P_CONTROLT) == 0 ||
1076be6a1d14SDavid Greenman 			    p->p_session == NULL ||
1077df8bae1dSRodney W. Grimes 			    p->p_session->s_ttyp == NULL ||
1078df8bae1dSRodney W. Grimes 			    p->p_session->s_ttyp->t_dev != (dev_t)name[1])
1079df8bae1dSRodney W. Grimes 				continue;
1080df8bae1dSRodney W. Grimes 			break;
1081df8bae1dSRodney W. Grimes 
1082df8bae1dSRodney W. Grimes 		case KERN_PROC_UID:
1083be6a1d14SDavid Greenman 			if (p->p_ucred == NULL || p->p_ucred->cr_uid != (uid_t)name[1])
1084df8bae1dSRodney W. Grimes 				continue;
1085df8bae1dSRodney W. Grimes 			break;
1086df8bae1dSRodney W. Grimes 
1087df8bae1dSRodney W. Grimes 		case KERN_PROC_RUID:
1088be6a1d14SDavid Greenman 			if (p->p_ucred == NULL || p->p_cred->p_ruid != (uid_t)name[1])
1089df8bae1dSRodney W. Grimes 				continue;
1090df8bae1dSRodney W. Grimes 			break;
1091df8bae1dSRodney W. Grimes 		}
1092df8bae1dSRodney W. Grimes 		if (buflen >= sizeof(struct kinfo_proc)) {
1093df8bae1dSRodney W. Grimes 			fill_eproc(p, &eproc);
1094797f2d22SPoul-Henning Kamp 			error = copyout((caddr_t)p, &dp->kp_proc,
1095797f2d22SPoul-Henning Kamp 			    sizeof(struct proc));
1096797f2d22SPoul-Henning Kamp 			if (error)
1097df8bae1dSRodney W. Grimes 				return (error);
1098797f2d22SPoul-Henning Kamp 			error = copyout((caddr_t)&eproc, &dp->kp_eproc,
1099797f2d22SPoul-Henning Kamp 			    sizeof(eproc));
1100797f2d22SPoul-Henning Kamp 			if (error)
1101df8bae1dSRodney W. Grimes 				return (error);
1102df8bae1dSRodney W. Grimes 			dp++;
1103df8bae1dSRodney W. Grimes 			buflen -= sizeof(struct kinfo_proc);
1104df8bae1dSRodney W. Grimes 		}
1105df8bae1dSRodney W. Grimes 		needed += sizeof(struct kinfo_proc);
1106df8bae1dSRodney W. Grimes 	}
1107df8bae1dSRodney W. Grimes 	if (doingzomb == 0) {
1108df8bae1dSRodney W. Grimes 		p = zombproc;
1109df8bae1dSRodney W. Grimes 		doingzomb++;
1110df8bae1dSRodney W. Grimes 		goto again;
1111df8bae1dSRodney W. Grimes 	}
1112df8bae1dSRodney W. Grimes 	if (where != NULL) {
1113df8bae1dSRodney W. Grimes 		*sizep = (caddr_t)dp - where;
1114df8bae1dSRodney W. Grimes 		if (needed > *sizep)
1115df8bae1dSRodney W. Grimes 			return (ENOMEM);
1116df8bae1dSRodney W. Grimes 	} else {
1117df8bae1dSRodney W. Grimes 		needed += KERN_PROCSLOP;
1118df8bae1dSRodney W. Grimes 		*sizep = needed;
1119df8bae1dSRodney W. Grimes 	}
1120df8bae1dSRodney W. Grimes 	return (0);
1121df8bae1dSRodney W. Grimes }
1122df8bae1dSRodney W. Grimes 
1123df8bae1dSRodney W. Grimes /*
1124df8bae1dSRodney W. Grimes  * Fill in an eproc structure for the specified process.
1125df8bae1dSRodney W. Grimes  */
1126df8bae1dSRodney W. Grimes void
1127df8bae1dSRodney W. Grimes fill_eproc(p, ep)
1128df8bae1dSRodney W. Grimes 	register struct proc *p;
1129df8bae1dSRodney W. Grimes 	register struct eproc *ep;
1130df8bae1dSRodney W. Grimes {
1131df8bae1dSRodney W. Grimes 	register struct tty *tp;
1132df8bae1dSRodney W. Grimes 
1133be6a1d14SDavid Greenman 	bzero(ep, sizeof(*ep));
1134be6a1d14SDavid Greenman 
1135df8bae1dSRodney W. Grimes 	ep->e_paddr = p;
11361c8fc26cSDavid Greenman 	if (p->p_cred) {
1137df8bae1dSRodney W. Grimes 		ep->e_pcred = *p->p_cred;
1138be6a1d14SDavid Greenman 		if (p->p_ucred)
1139df8bae1dSRodney W. Grimes 			ep->e_ucred = *p->p_ucred;
11401c8fc26cSDavid Greenman 	}
11411c8fc26cSDavid Greenman 	if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) {
1142df8bae1dSRodney W. Grimes 		register struct vmspace *vm = p->p_vmspace;
1143df8bae1dSRodney W. Grimes 
1144df8bae1dSRodney W. Grimes #ifdef pmap_resident_count
1145df8bae1dSRodney W. Grimes 		ep->e_vm.vm_rssize = pmap_resident_count(&vm->vm_pmap); /*XXX*/
1146df8bae1dSRodney W. Grimes #else
1147df8bae1dSRodney W. Grimes 		ep->e_vm.vm_rssize = vm->vm_rssize;
1148df8bae1dSRodney W. Grimes #endif
1149df8bae1dSRodney W. Grimes 		ep->e_vm.vm_tsize = vm->vm_tsize;
1150df8bae1dSRodney W. Grimes 		ep->e_vm.vm_dsize = vm->vm_dsize;
1151df8bae1dSRodney W. Grimes 		ep->e_vm.vm_ssize = vm->vm_ssize;
1152df8bae1dSRodney W. Grimes #ifndef sparc
1153df8bae1dSRodney W. Grimes 		ep->e_vm.vm_pmap = vm->vm_pmap;
1154df8bae1dSRodney W. Grimes #endif
1155df8bae1dSRodney W. Grimes 	}
1156df8bae1dSRodney W. Grimes 	if (p->p_pptr)
1157df8bae1dSRodney W. Grimes 		ep->e_ppid = p->p_pptr->p_pid;
1158be6a1d14SDavid Greenman 	if (p->p_pgrp) {
1159be6a1d14SDavid Greenman 		ep->e_sess = p->p_pgrp->pg_session;
1160df8bae1dSRodney W. Grimes 		ep->e_pgid = p->p_pgrp->pg_id;
1161df8bae1dSRodney W. Grimes 		ep->e_jobc = p->p_pgrp->pg_jobc;
1162be6a1d14SDavid Greenman 	}
1163df8bae1dSRodney W. Grimes 	if ((p->p_flag & P_CONTROLT) &&
1164be6a1d14SDavid Greenman 	    (ep->e_sess != NULL) &&
1165be6a1d14SDavid Greenman 	    ((tp = ep->e_sess->s_ttyp) != NULL)) {
1166df8bae1dSRodney W. Grimes 		ep->e_tdev = tp->t_dev;
1167df8bae1dSRodney W. Grimes 		ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1168df8bae1dSRodney W. Grimes 		ep->e_tsess = tp->t_session;
1169df8bae1dSRodney W. Grimes 	} else
1170df8bae1dSRodney W. Grimes 		ep->e_tdev = NODEV;
1171be6a1d14SDavid Greenman 	if (ep->e_sess && ep->e_sess->s_ttyvp)
1172be6a1d14SDavid Greenman 		ep->e_flag = EPROC_CTTY;
1173df8bae1dSRodney W. Grimes 	if (SESS_LEADER(p))
1174df8bae1dSRodney W. Grimes 		ep->e_flag |= EPROC_SLEADER;
1175be6a1d14SDavid Greenman 	if (p->p_wmesg) {
1176df8bae1dSRodney W. Grimes 		strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN);
1177be6a1d14SDavid Greenman 		ep->e_wmesg[WMESGLEN] = 0;
1178be6a1d14SDavid Greenman 	}
1179df8bae1dSRodney W. Grimes }
1180df8bae1dSRodney W. Grimes 
1181df8bae1dSRodney W. Grimes #ifdef COMPAT_43
1182df8bae1dSRodney W. Grimes #include <sys/socket.h>
1183df8bae1dSRodney W. Grimes #define	KINFO_PROC		(0<<8)
1184df8bae1dSRodney W. Grimes #define	KINFO_RT		(1<<8)
1185df8bae1dSRodney W. Grimes #define	KINFO_VNODE		(2<<8)
1186df8bae1dSRodney W. Grimes #define	KINFO_FILE		(3<<8)
1187df8bae1dSRodney W. Grimes #define	KINFO_METER		(4<<8)
1188df8bae1dSRodney W. Grimes #define	KINFO_LOADAVG		(5<<8)
1189df8bae1dSRodney W. Grimes #define	KINFO_CLOCKRATE		(6<<8)
1190df8bae1dSRodney W. Grimes 
11916ece4a51SPeter Wemm /* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
11926ece4a51SPeter Wemm #define	KINFO_BSDI_SYSINFO	(101<<8)
11936ece4a51SPeter Wemm 
11946ece4a51SPeter Wemm /*
11956ece4a51SPeter Wemm  * XXX this is bloat, but I hope it's better here than on the potentially
11966ece4a51SPeter Wemm  * limited kernel stack...  -Peter
11976ece4a51SPeter Wemm  */
11986ece4a51SPeter Wemm 
11996ece4a51SPeter Wemm struct {
12006ece4a51SPeter Wemm 	int	bsdi_machine;		/* "i386" on BSD/386 */
12016ece4a51SPeter Wemm /*      ^^^ this is an offset to the string, relative to the struct start */
12026ece4a51SPeter Wemm 	char	*pad0;
12036ece4a51SPeter Wemm 	long	pad1;
12046ece4a51SPeter Wemm 	long	pad2;
12056ece4a51SPeter Wemm 	long	pad3;
12066ece4a51SPeter Wemm 	u_long	pad4;
12076ece4a51SPeter Wemm 	u_long	pad5;
12086ece4a51SPeter Wemm 	u_long	pad6;
12096ece4a51SPeter Wemm 
12106ece4a51SPeter Wemm 	int	bsdi_ostype;		/* "BSD/386" on BSD/386 */
12116ece4a51SPeter Wemm 	int	bsdi_osrelease;		/* "1.1" on BSD/386 */
12126ece4a51SPeter Wemm 	long	pad7;
12136ece4a51SPeter Wemm 	long	pad8;
12146ece4a51SPeter Wemm 	char	*pad9;
12156ece4a51SPeter Wemm 
12166ece4a51SPeter Wemm 	long	pad10;
12176ece4a51SPeter Wemm 	long	pad11;
12186ece4a51SPeter Wemm 	int	pad12;
12196ece4a51SPeter Wemm 	long	pad13;
12206ece4a51SPeter Wemm 	quad_t	pad14;
12216ece4a51SPeter Wemm 	long	pad15;
12226ece4a51SPeter Wemm 
12236ece4a51SPeter Wemm 	struct	timeval pad16;
12246ece4a51SPeter Wemm 	/* we dont set this, because BSDI's uname used gethostname() instead */
12256ece4a51SPeter Wemm 	int	bsdi_hostname;		/* hostname on BSD/386 */
12266ece4a51SPeter Wemm 
12276ece4a51SPeter Wemm 	/* the actual string data is appended here */
12286ece4a51SPeter Wemm 
12296ece4a51SPeter Wemm } bsdi_si;
12306ece4a51SPeter Wemm /*
12316ece4a51SPeter Wemm  * this data is appended to the end of the bsdi_si structure during copyout.
12326ece4a51SPeter Wemm  * The "char *" offsets are relative to the base of the bsdi_si struct.
12336ece4a51SPeter Wemm  * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
12346ece4a51SPeter Wemm  * should not exceed the length of the buffer here... (or else!! :-)
12356ece4a51SPeter Wemm  */
12366ece4a51SPeter Wemm char bsdi_strings[80];	/* It had better be less than this! */
12376ece4a51SPeter Wemm 
1238d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1239df8bae1dSRodney W. Grimes struct getkerninfo_args {
1240df8bae1dSRodney W. Grimes 	int	op;
1241df8bae1dSRodney W. Grimes 	char	*where;
1242df8bae1dSRodney W. Grimes 	int	*size;
1243df8bae1dSRodney W. Grimes 	int	arg;
1244df8bae1dSRodney W. Grimes };
1245d2d3e875SBruce Evans #endif
1246df8bae1dSRodney W. Grimes 
124726f9a767SRodney W. Grimes int
1248df8bae1dSRodney W. Grimes ogetkerninfo(p, uap, retval)
1249df8bae1dSRodney W. Grimes 	struct proc *p;
1250df8bae1dSRodney W. Grimes 	register struct getkerninfo_args *uap;
1251df8bae1dSRodney W. Grimes 	int *retval;
1252df8bae1dSRodney W. Grimes {
1253b8da2396SPoul-Henning Kamp 	int error, name[6];
1254df8bae1dSRodney W. Grimes 	u_int size;
1255df8bae1dSRodney W. Grimes 
1256df8bae1dSRodney W. Grimes 	switch (uap->op & 0xff00) {
1257df8bae1dSRodney W. Grimes 
1258df8bae1dSRodney W. Grimes 	case KINFO_RT:
1259b8da2396SPoul-Henning Kamp 		name[0] = CTL_NET;
1260b8da2396SPoul-Henning Kamp 		name[1] = PF_ROUTE;
1261b8da2396SPoul-Henning Kamp 		name[2] = 0;
1262b8da2396SPoul-Henning Kamp 		name[3] = (uap->op & 0xff0000) >> 16;
1263b8da2396SPoul-Henning Kamp 		name[4] = uap->op & 0xff;
1264b8da2396SPoul-Henning Kamp 		name[5] = uap->arg;
1265b8da2396SPoul-Henning Kamp 		error = userland_sysctl(p, name, 6, uap->where, uap->size,
1266b8da2396SPoul-Henning Kamp 			0, 0, 0, 0);
1267df8bae1dSRodney W. Grimes 		break;
1268df8bae1dSRodney W. Grimes 
1269df8bae1dSRodney W. Grimes 	case KINFO_VNODE:
1270b8da2396SPoul-Henning Kamp 		name[0] = CTL_KERN;
1271b8da2396SPoul-Henning Kamp 		name[1] = KERN_VNODE;
1272b8da2396SPoul-Henning Kamp 		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1273b8da2396SPoul-Henning Kamp 			0, 0, 0, 0);
1274df8bae1dSRodney W. Grimes 		break;
1275df8bae1dSRodney W. Grimes 
1276df8bae1dSRodney W. Grimes 	case KINFO_PROC:
1277b8da2396SPoul-Henning Kamp 		name[0] = CTL_KERN;
1278b8da2396SPoul-Henning Kamp 		name[1] = KERN_PROC;
1279b8da2396SPoul-Henning Kamp 		name[2] = uap->op & 0xff;
1280b8da2396SPoul-Henning Kamp 		name[3] = uap->arg;
1281b8da2396SPoul-Henning Kamp 		error = userland_sysctl(p, name, 4, uap->where, uap->size,
1282b8da2396SPoul-Henning Kamp 			0, 0, 0, 0);
1283df8bae1dSRodney W. Grimes 		break;
1284df8bae1dSRodney W. Grimes 
1285df8bae1dSRodney W. Grimes 	case KINFO_FILE:
1286b8da2396SPoul-Henning Kamp 		name[0] = CTL_KERN;
1287b8da2396SPoul-Henning Kamp 		name[1] = KERN_FILE;
1288b8da2396SPoul-Henning Kamp 		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1289b8da2396SPoul-Henning Kamp 			0, 0, 0, 0);
1290df8bae1dSRodney W. Grimes 		break;
1291df8bae1dSRodney W. Grimes 
1292df8bae1dSRodney W. Grimes 	case KINFO_METER:
1293b8da2396SPoul-Henning Kamp 		name[0] = CTL_VM;
1294b8da2396SPoul-Henning Kamp 		name[1] = VM_METER;
1295b8da2396SPoul-Henning Kamp 		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1296b8da2396SPoul-Henning Kamp 			0, 0, 0, 0);
1297df8bae1dSRodney W. Grimes 		break;
1298df8bae1dSRodney W. Grimes 
1299df8bae1dSRodney W. Grimes 	case KINFO_LOADAVG:
1300b8da2396SPoul-Henning Kamp 		name[0] = CTL_VM;
1301b8da2396SPoul-Henning Kamp 		name[1] = VM_LOADAVG;
1302b8da2396SPoul-Henning Kamp 		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1303b8da2396SPoul-Henning Kamp 			0, 0, 0, 0);
1304df8bae1dSRodney W. Grimes 		break;
1305df8bae1dSRodney W. Grimes 
1306df8bae1dSRodney W. Grimes 	case KINFO_CLOCKRATE:
1307b8da2396SPoul-Henning Kamp 		name[0] = CTL_KERN;
1308b8da2396SPoul-Henning Kamp 		name[1] = KERN_CLOCKRATE;
1309b8da2396SPoul-Henning Kamp 		error = userland_sysctl(p, name, 2, uap->where, uap->size,
1310b8da2396SPoul-Henning Kamp 			0, 0, 0, 0);
1311df8bae1dSRodney W. Grimes 		break;
1312df8bae1dSRodney W. Grimes 
13136ece4a51SPeter Wemm 	case KINFO_BSDI_SYSINFO: {
13146ece4a51SPeter Wemm 		/*
13156ece4a51SPeter Wemm 		 * this is pretty crude, but it's just enough for uname()
13166ece4a51SPeter Wemm 		 * from BSDI's 1.x libc to work.
13176ece4a51SPeter Wemm 		 *
13186ece4a51SPeter Wemm 		 * In particular, it doesn't return the same results when
13196ece4a51SPeter Wemm 		 * the supplied buffer is too small.  BSDI's version apparently
13206ece4a51SPeter Wemm 		 * will return the amount copied, and set the *size to how
13216ece4a51SPeter Wemm 		 * much was needed.  The emulation framework here isn't capable
13226ece4a51SPeter Wemm 		 * of that, so we just set both to the amount copied.
13236ece4a51SPeter Wemm 		 * BSDI's 2.x product apparently fails with ENOMEM in this
13246ece4a51SPeter Wemm 		 * scenario.
13256ece4a51SPeter Wemm 		 */
13266ece4a51SPeter Wemm 
13276ece4a51SPeter Wemm 		u_int needed;
13286ece4a51SPeter Wemm 		u_int left;
13296ece4a51SPeter Wemm 		char *s;
13306ece4a51SPeter Wemm 
13316ece4a51SPeter Wemm 		bzero((char *)&bsdi_si, sizeof(bsdi_si));
13326ece4a51SPeter Wemm 		bzero(bsdi_strings, sizeof(bsdi_strings));
13336ece4a51SPeter Wemm 
13346ece4a51SPeter Wemm 		s = bsdi_strings;
13356ece4a51SPeter Wemm 
13366ece4a51SPeter Wemm 		bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
13376ece4a51SPeter Wemm 		strcpy(s, ostype);
13386ece4a51SPeter Wemm 		s += strlen(s) + 1;
13396ece4a51SPeter Wemm 
13406ece4a51SPeter Wemm 		bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
13416ece4a51SPeter Wemm 		strcpy(s, osrelease);
13426ece4a51SPeter Wemm 		s += strlen(s) + 1;
13436ece4a51SPeter Wemm 
13446ece4a51SPeter Wemm 		bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
13456ece4a51SPeter Wemm 		strcpy(s, machine);
13466ece4a51SPeter Wemm 		s += strlen(s) + 1;
13476ece4a51SPeter Wemm 
13486ece4a51SPeter Wemm 		needed = sizeof(bsdi_si) + (s - bsdi_strings);
13496ece4a51SPeter Wemm 
13506ece4a51SPeter Wemm 		if (uap->where == NULL) {
13516ece4a51SPeter Wemm 			/* process is asking how much buffer to supply.. */
13526ece4a51SPeter Wemm 			size = needed;
13536ece4a51SPeter Wemm 			error = 0;
13546ece4a51SPeter Wemm 			break;
13556ece4a51SPeter Wemm 		}
13566ece4a51SPeter Wemm 
13576ece4a51SPeter Wemm 
13586ece4a51SPeter Wemm 		/* if too much buffer supplied, trim it down */
13596ece4a51SPeter Wemm 		if (size > needed)
13606ece4a51SPeter Wemm 			size = needed;
13616ece4a51SPeter Wemm 
13626ece4a51SPeter Wemm 		/* how much of the buffer is remaining */
13636ece4a51SPeter Wemm 		left = size;
13646ece4a51SPeter Wemm 
13656ece4a51SPeter Wemm 		if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
13666ece4a51SPeter Wemm 			break;
13676ece4a51SPeter Wemm 
13686ece4a51SPeter Wemm 		/* is there any point in continuing? */
13696ece4a51SPeter Wemm 		if (left > sizeof(bsdi_si)) {
13706ece4a51SPeter Wemm 			left -= sizeof(bsdi_si);
13716ece4a51SPeter Wemm 			error = copyout(&bsdi_strings,
13726ece4a51SPeter Wemm 					uap->where + sizeof(bsdi_si), left);
13736ece4a51SPeter Wemm 		}
13746ece4a51SPeter Wemm 		break;
13756ece4a51SPeter Wemm 	}
13766ece4a51SPeter Wemm 
1377df8bae1dSRodney W. Grimes 	default:
1378df8bae1dSRodney W. Grimes 		return (EOPNOTSUPP);
1379df8bae1dSRodney W. Grimes 	}
1380df8bae1dSRodney W. Grimes 	if (error)
1381df8bae1dSRodney W. Grimes 		return (error);
1382df8bae1dSRodney W. Grimes 	*retval = size;
1383df8bae1dSRodney W. Grimes 	if (uap->size)
1384df8bae1dSRodney W. Grimes 		error = copyout((caddr_t)&size, (caddr_t)uap->size,
1385df8bae1dSRodney W. Grimes 		    sizeof(size));
1386df8bae1dSRodney W. Grimes 	return (error);
1387df8bae1dSRodney W. Grimes }
1388df8bae1dSRodney W. Grimes #endif /* COMPAT_43 */
1389