xref: /netbsd-src/sys/kern/init_sysctl.c (revision 5b84b3983f71fd20a534cfa5d1556623a8aaa717)
1 /*	$NetBSD: init_sysctl.c,v 1.55 2005/09/07 17:30:07 elad Exp $ */
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Brown.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the NetBSD
21  *      Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: init_sysctl.c,v 1.55 2005/09/07 17:30:07 elad Exp $");
41 
42 #include "opt_sysv.h"
43 #include "opt_multiprocessor.h"
44 #include "opt_posix.h"
45 #include "opt_verified_exec.h"
46 #include "pty.h"
47 #include "rnd.h"
48 
49 #include <sys/types.h>
50 #include <sys/param.h>
51 #include <sys/sysctl.h>
52 #include <sys/errno.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/unistd.h>
56 #include <sys/disklabel.h>
57 #include <sys/rnd.h>
58 #include <sys/vnode.h>
59 #include <sys/mount.h>
60 #include <sys/namei.h>
61 #include <sys/msgbuf.h>
62 #include <dev/cons.h>
63 #include <sys/socketvar.h>
64 #include <sys/file.h>
65 #include <sys/filedesc.h>
66 #include <sys/tty.h>
67 #include <sys/malloc.h>
68 #include <sys/resource.h>
69 #include <sys/resourcevar.h>
70 #include <sys/exec.h>
71 #include <sys/conf.h>
72 #include <sys/device.h>
73 #ifdef VERIFIED_EXEC
74 #define	VERIEXEC_NEED_NODE
75 #include <sys/verified_exec.h>
76 #endif /* VERIFIED_EXEC */
77 
78 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
79 #include <sys/ipc.h>
80 #endif
81 #ifdef SYSVMSG
82 #include <sys/msg.h>
83 #endif
84 #ifdef SYSVSEM
85 #include <sys/sem.h>
86 #endif
87 #ifdef SYSVSHM
88 #include <sys/shm.h>
89 #endif
90 
91 #include <machine/cpu.h>
92 
93 /* XXX this should not be here */
94 int security_curtain = 0;
95 
96 /*
97  * try over estimating by 5 procs/lwps
98  */
99 #define KERN_PROCSLOP	(5 * sizeof(struct kinfo_proc))
100 #define KERN_LWPSLOP	(5 * sizeof(struct kinfo_lwp))
101 
102 #ifndef MULTIPROCESSOR
103 #define	sysctl_ncpus()	(1)
104 #else /* MULTIPROCESSOR */
105 #ifndef CPU_INFO_FOREACH
106 #define CPU_INFO_ITERATOR int
107 #define CPU_INFO_FOREACH(cii, ci) cii = 0, ci = curcpu(); ci != NULL; ci = NULL
108 #endif
109 static int
110 sysctl_ncpus(void)
111 {
112 	struct cpu_info *ci;
113 	CPU_INFO_ITERATOR cii;
114 
115 	int ncpus = 0;
116 	for (CPU_INFO_FOREACH(cii, ci))
117 		ncpus++;
118 	return (ncpus);
119 }
120 #endif /* MULTIPROCESSOR */
121 
122 static int sysctl_kern_maxvnodes(SYSCTLFN_PROTO);
123 static int sysctl_kern_rtc_offset(SYSCTLFN_PROTO);
124 static int sysctl_kern_maxproc(SYSCTLFN_PROTO);
125 static int sysctl_kern_securelevel(SYSCTLFN_PROTO);
126 static int sysctl_kern_hostid(SYSCTLFN_PROTO);
127 static int sysctl_setlen(SYSCTLFN_PROTO);
128 static int sysctl_kern_clockrate(SYSCTLFN_PROTO);
129 static int sysctl_kern_file(SYSCTLFN_PROTO);
130 static int sysctl_kern_autonice(SYSCTLFN_PROTO);
131 static int sysctl_msgbuf(SYSCTLFN_PROTO);
132 static int sysctl_kern_defcorename(SYSCTLFN_PROTO);
133 static int sysctl_kern_cptime(SYSCTLFN_PROTO);
134 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
135 static int sysctl_kern_sysvipc(SYSCTLFN_PROTO);
136 #endif /* defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM) */
137 #if NPTY > 0
138 static int sysctl_kern_maxptys(SYSCTLFN_PROTO);
139 #endif /* NPTY > 0 */
140 static int sysctl_kern_sbmax(SYSCTLFN_PROTO);
141 static int sysctl_kern_urnd(SYSCTLFN_PROTO);
142 static int sysctl_kern_lwp(SYSCTLFN_PROTO);
143 static int sysctl_kern_forkfsleep(SYSCTLFN_PROTO);
144 static int sysctl_kern_root_partition(SYSCTLFN_PROTO);
145 static int sysctl_kern_drivers(SYSCTLFN_PROTO);
146 static int sysctl_kern_file2(SYSCTLFN_PROTO);
147 #ifdef VERIFIED_EXEC
148 static int sysctl_kern_veriexec(SYSCTLFN_PROTO);
149 #endif
150 static int sysctl_kern_cpid(SYSCTLFN_PROTO);
151 static int sysctl_doeproc(SYSCTLFN_PROTO);
152 static int sysctl_kern_proc_args(SYSCTLFN_PROTO);
153 static int sysctl_hw_usermem(SYSCTLFN_PROTO);
154 static int sysctl_hw_cnmagic(SYSCTLFN_PROTO);
155 static int sysctl_hw_ncpu(SYSCTLFN_PROTO);
156 
157 static void fill_kproc2(struct proc *, struct kinfo_proc2 *);
158 static void fill_lwp(struct lwp *l, struct kinfo_lwp *kl);
159 static void fill_file(struct kinfo_file *, const struct file *, struct proc *,
160 		      int);
161 
162 /*
163  * ********************************************************************
164  * section 1: setup routines
165  * ********************************************************************
166  * these functions are stuffed into a link set for sysctl setup
167  * functions.  they're never called or referenced from anywhere else.
168  * ********************************************************************
169  */
170 
171 /*
172  * sets up the base nodes...
173  */
174 SYSCTL_SETUP(sysctl_root_setup, "sysctl base setup")
175 {
176 
177 	sysctl_createv(clog, 0, NULL, NULL,
178 		       CTLFLAG_PERMANENT,
179 		       CTLTYPE_NODE, "kern",
180 		       SYSCTL_DESCR("High kernel"),
181 		       NULL, 0, NULL, 0,
182 		       CTL_KERN, CTL_EOL);
183 	sysctl_createv(clog, 0, NULL, NULL,
184 		       CTLFLAG_PERMANENT,
185 		       CTLTYPE_NODE, "vm",
186 		       SYSCTL_DESCR("Virtual memory"),
187 		       NULL, 0, NULL, 0,
188 		       CTL_VM, CTL_EOL);
189 	sysctl_createv(clog, 0, NULL, NULL,
190 		       CTLFLAG_PERMANENT,
191 		       CTLTYPE_NODE, "vfs",
192 		       SYSCTL_DESCR("Filesystem"),
193 		       NULL, 0, NULL, 0,
194 		       CTL_VFS, CTL_EOL);
195 	sysctl_createv(clog, 0, NULL, NULL,
196 		       CTLFLAG_PERMANENT,
197 		       CTLTYPE_NODE, "net",
198 		       SYSCTL_DESCR("Networking"),
199 		       NULL, 0, NULL, 0,
200 		       CTL_NET, CTL_EOL);
201 	sysctl_createv(clog, 0, NULL, NULL,
202 		       CTLFLAG_PERMANENT,
203 		       CTLTYPE_NODE, "debug",
204 		       SYSCTL_DESCR("Debugging"),
205 		       NULL, 0, NULL, 0,
206 		       CTL_DEBUG, CTL_EOL);
207 	sysctl_createv(clog, 0, NULL, NULL,
208 		       CTLFLAG_PERMANENT,
209 		       CTLTYPE_NODE, "hw",
210 		       SYSCTL_DESCR("Generic CPU, I/O"),
211 		       NULL, 0, NULL, 0,
212 		       CTL_HW, CTL_EOL);
213 	sysctl_createv(clog, 0, NULL, NULL,
214 		       CTLFLAG_PERMANENT,
215 		       CTLTYPE_NODE, "machdep",
216 		       SYSCTL_DESCR("Machine dependent"),
217 		       NULL, 0, NULL, 0,
218 		       CTL_MACHDEP, CTL_EOL);
219 	/*
220 	 * this node is inserted so that the sysctl nodes in libc can
221 	 * operate.
222 	 */
223 	sysctl_createv(clog, 0, NULL, NULL,
224 		       CTLFLAG_PERMANENT,
225 		       CTLTYPE_NODE, "user",
226 		       SYSCTL_DESCR("User-level"),
227 		       NULL, 0, NULL, 0,
228 		       CTL_USER, CTL_EOL);
229 	sysctl_createv(clog, 0, NULL, NULL,
230 		       CTLFLAG_PERMANENT,
231 		       CTLTYPE_NODE, "ddb",
232 		       SYSCTL_DESCR("In-kernel debugger"),
233 		       NULL, 0, NULL, 0,
234 		       CTL_DDB, CTL_EOL);
235 	sysctl_createv(clog, 0, NULL, NULL,
236 		       CTLFLAG_PERMANENT,
237 		       CTLTYPE_NODE, "proc",
238 		       SYSCTL_DESCR("Per-process"),
239 		       NULL, 0, NULL, 0,
240 		       CTL_PROC, CTL_EOL);
241 	sysctl_createv(clog, 0, NULL, NULL,
242 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
243 		       CTLTYPE_NODE, "vendor",
244 		       SYSCTL_DESCR("Vendor specific"),
245 		       NULL, 0, NULL, 0,
246 		       CTL_VENDOR, CTL_EOL);
247 	sysctl_createv(clog, 0, NULL, NULL,
248 		       CTLFLAG_PERMANENT,
249 		       CTLTYPE_NODE, "emul",
250 		       SYSCTL_DESCR("Emulation settings"),
251 		       NULL, 0, NULL, 0,
252 		       CTL_EMUL, CTL_EOL);
253 	sysctl_createv(clog, 0, NULL, NULL,
254 		       CTLFLAG_PERMANENT,
255 		       CTLTYPE_NODE, "security",
256 		       SYSCTL_DESCR("Security"),
257 		       NULL, 0, NULL, 0,
258 		       CTL_SECURITY, CTL_EOL);
259 }
260 
261 /*
262  * this setup routine is a replacement for kern_sysctl()
263  */
264 SYSCTL_SETUP(sysctl_kern_setup, "sysctl kern subtree setup")
265 {
266 	extern int kern_logsigexit;	/* defined in kern/kern_sig.c */
267 	extern fixpt_t ccpu;		/* defined in kern/kern_synch.c */
268 	extern int dumponpanic;		/* defined in kern/subr_prf.c */
269 
270 	sysctl_createv(clog, 0, NULL, NULL,
271 		       CTLFLAG_PERMANENT,
272 		       CTLTYPE_NODE, "kern", NULL,
273 		       NULL, 0, NULL, 0,
274 		       CTL_KERN, CTL_EOL);
275 
276 	sysctl_createv(clog, 0, NULL, NULL,
277 		       CTLFLAG_PERMANENT,
278 		       CTLTYPE_STRING, "ostype",
279 		       SYSCTL_DESCR("Operating system type"),
280 		       NULL, 0, &ostype, 0,
281 		       CTL_KERN, KERN_OSTYPE, CTL_EOL);
282 	sysctl_createv(clog, 0, NULL, NULL,
283 		       CTLFLAG_PERMANENT,
284 		       CTLTYPE_STRING, "osrelease",
285 		       SYSCTL_DESCR("Operating system release"),
286 		       NULL, 0, &osrelease, 0,
287 		       CTL_KERN, KERN_OSRELEASE, CTL_EOL);
288 	sysctl_createv(clog, 0, NULL, NULL,
289 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
290 		       CTLTYPE_INT, "osrevision",
291 		       SYSCTL_DESCR("Operating system revision"),
292 		       NULL, __NetBSD_Version__, NULL, 0,
293 		       CTL_KERN, KERN_OSREV, CTL_EOL);
294 	sysctl_createv(clog, 0, NULL, NULL,
295 		       CTLFLAG_PERMANENT,
296 		       CTLTYPE_STRING, "version",
297 		       SYSCTL_DESCR("Kernel version"),
298 		       NULL, 0, &version, 0,
299 		       CTL_KERN, KERN_VERSION, CTL_EOL);
300 	sysctl_createv(clog, 0, NULL, NULL,
301 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
302 		       CTLTYPE_INT, "maxvnodes",
303 		       SYSCTL_DESCR("Maximum number of vnodes"),
304 		       sysctl_kern_maxvnodes, 0, NULL, 0,
305 		       CTL_KERN, KERN_MAXVNODES, CTL_EOL);
306 	sysctl_createv(clog, 0, NULL, NULL,
307 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
308 		       CTLTYPE_INT, "maxproc",
309 		       SYSCTL_DESCR("Maximum number of simultaneous processes"),
310 		       sysctl_kern_maxproc, 0, NULL, 0,
311 		       CTL_KERN, KERN_MAXPROC, CTL_EOL);
312 	sysctl_createv(clog, 0, NULL, NULL,
313 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
314 		       CTLTYPE_INT, "maxfiles",
315 		       SYSCTL_DESCR("Maximum number of open files"),
316 		       NULL, 0, &maxfiles, 0,
317 		       CTL_KERN, KERN_MAXFILES, CTL_EOL);
318 	sysctl_createv(clog, 0, NULL, NULL,
319 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
320 		       CTLTYPE_INT, "argmax",
321 		       SYSCTL_DESCR("Maximum number of bytes of arguments to "
322 				    "execve(2)"),
323 		       NULL, ARG_MAX, NULL, 0,
324 		       CTL_KERN, KERN_ARGMAX, CTL_EOL);
325 	sysctl_createv(clog, 0, NULL, NULL,
326 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
327 		       CTLTYPE_INT, "securelevel",
328 		       SYSCTL_DESCR("System security level"),
329 		       sysctl_kern_securelevel, 0, &securelevel, 0,
330 		       CTL_KERN, KERN_SECURELVL, CTL_EOL);
331 	sysctl_createv(clog, 0, NULL, NULL,
332 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
333 		       CTLTYPE_STRING, "hostname",
334 		       SYSCTL_DESCR("System hostname"),
335 		       sysctl_setlen, 0, &hostname, MAXHOSTNAMELEN,
336 		       CTL_KERN, KERN_HOSTNAME, CTL_EOL);
337 	sysctl_createv(clog, 0, NULL, NULL,
338 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
339 		       CTLTYPE_INT, "hostid",
340 		       SYSCTL_DESCR("System host ID number"),
341 		       sysctl_kern_hostid, 0, NULL, 0,
342 		       CTL_KERN, KERN_HOSTID, CTL_EOL);
343 	sysctl_createv(clog, 0, NULL, NULL,
344 		       CTLFLAG_PERMANENT,
345 		       CTLTYPE_STRUCT, "clockrate",
346 		       SYSCTL_DESCR("Kernel clock rates"),
347 		       sysctl_kern_clockrate, 0, NULL,
348 		       sizeof(struct clockinfo),
349 		       CTL_KERN, KERN_CLOCKRATE, CTL_EOL);
350 	sysctl_createv(clog, 0, NULL, NULL,
351 		       CTLFLAG_PERMANENT,
352 		       CTLTYPE_INT, "hardclock_ticks",
353 		       SYSCTL_DESCR("Number of hardclock ticks"),
354 		       NULL, 0, &hardclock_ticks, sizeof(hardclock_ticks),
355 		       CTL_KERN, KERN_HARDCLOCK_TICKS, CTL_EOL);
356 	sysctl_createv(clog, 0, NULL, NULL,
357 		       CTLFLAG_PERMANENT,
358 		       CTLTYPE_STRUCT, "vnode",
359 		       SYSCTL_DESCR("System vnode table"),
360 		       sysctl_kern_vnode, 0, NULL, 0,
361 		       CTL_KERN, KERN_VNODE, CTL_EOL);
362 	sysctl_createv(clog, 0, NULL, NULL,
363 		       CTLFLAG_PERMANENT,
364 		       CTLTYPE_STRUCT, "file",
365 		       SYSCTL_DESCR("System open file table"),
366 		       sysctl_kern_file, 0, NULL, 0,
367 		       CTL_KERN, KERN_FILE, CTL_EOL);
368 #ifndef GPROF
369 	sysctl_createv(clog, 0, NULL, NULL,
370 		       CTLFLAG_PERMANENT,
371 		       CTLTYPE_NODE, "profiling",
372 		       SYSCTL_DESCR("Profiling information (not available)"),
373 		       sysctl_notavail, 0, NULL, 0,
374 		       CTL_KERN, KERN_PROF, CTL_EOL);
375 #endif
376 	sysctl_createv(clog, 0, NULL, NULL,
377 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
378 		       CTLTYPE_INT, "posix1version",
379 		       SYSCTL_DESCR("Version of ISO/IEC 9945 (POSIX 1003.1) "
380 				    "with which the operating system attempts "
381 				    "to comply"),
382 		       NULL, _POSIX_VERSION, NULL, 0,
383 		       CTL_KERN, KERN_POSIX1, CTL_EOL);
384 	sysctl_createv(clog, 0, NULL, NULL,
385 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
386 		       CTLTYPE_INT, "ngroups",
387 		       SYSCTL_DESCR("Maximum number of supplemental groups"),
388 		       NULL, NGROUPS_MAX, NULL, 0,
389 		       CTL_KERN, KERN_NGROUPS, CTL_EOL);
390 	sysctl_createv(clog, 0, NULL, NULL,
391 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
392 		       CTLTYPE_INT, "job_control",
393 		       SYSCTL_DESCR("Whether job control is available"),
394 		       NULL, 1, NULL, 0,
395 		       CTL_KERN, KERN_JOB_CONTROL, CTL_EOL);
396 	sysctl_createv(clog, 0, NULL, NULL,
397 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
398 		       CTLTYPE_INT, "saved_ids",
399 		       SYSCTL_DESCR("Whether POSIX saved set-group/user ID is "
400 				    "available"), NULL,
401 #ifdef _POSIX_SAVED_IDS
402 		       1,
403 #else /* _POSIX_SAVED_IDS */
404 		       0,
405 #endif /* _POSIX_SAVED_IDS */
406 		       NULL, 0, CTL_KERN, KERN_SAVED_IDS, CTL_EOL);
407 	sysctl_createv(clog, 0, NULL, NULL,
408 		       CTLFLAG_PERMANENT,
409 		       CTLTYPE_STRUCT, "boottime",
410 		       SYSCTL_DESCR("System boot time"),
411 		       NULL, 0, &boottime, sizeof(boottime),
412 		       CTL_KERN, KERN_BOOTTIME, CTL_EOL);
413 	sysctl_createv(clog, 0, NULL, NULL,
414 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
415 		       CTLTYPE_STRING, "domainname",
416 		       SYSCTL_DESCR("YP domain name"),
417 		       sysctl_setlen, 0, &domainname, MAXHOSTNAMELEN,
418 		       CTL_KERN, KERN_DOMAINNAME, CTL_EOL);
419 	sysctl_createv(clog, 0, NULL, NULL,
420 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
421 		       CTLTYPE_INT, "maxpartitions",
422 		       SYSCTL_DESCR("Maximum number of partitions allowed per "
423 				    "disk"),
424 		       NULL, MAXPARTITIONS, NULL, 0,
425 		       CTL_KERN, KERN_MAXPARTITIONS, CTL_EOL);
426 	sysctl_createv(clog, 0, NULL, NULL,
427 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
428 		       CTLTYPE_INT, "rawpartition",
429 		       SYSCTL_DESCR("Raw partition of a disk"),
430 		       NULL, RAW_PART, NULL, 0,
431 		       CTL_KERN, KERN_RAWPARTITION, CTL_EOL);
432 	sysctl_createv(clog, 0, NULL, NULL,
433 		       CTLFLAG_PERMANENT,
434 		       CTLTYPE_STRUCT, "timex", NULL,
435 		       sysctl_notavail, 0, NULL, 0,
436 		       CTL_KERN, KERN_TIMEX, CTL_EOL);
437 	sysctl_createv(clog, 0, NULL, NULL,
438 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
439 		       CTLTYPE_INT, "autonicetime",
440 		       SYSCTL_DESCR("CPU clock seconds before non-root "
441 				    "process priority is lowered"),
442 		       sysctl_kern_autonice, 0, &autonicetime, 0,
443 		       CTL_KERN, KERN_AUTONICETIME, CTL_EOL);
444 	sysctl_createv(clog, 0, NULL, NULL,
445 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
446 		       CTLTYPE_INT, "autoniceval",
447 		       SYSCTL_DESCR("Automatic reniced non-root process "
448 				    "priority"),
449 		       sysctl_kern_autonice, 0, &autoniceval, 0,
450 		       CTL_KERN, KERN_AUTONICEVAL, CTL_EOL);
451 	sysctl_createv(clog, 0, NULL, NULL,
452 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
453 		       CTLTYPE_INT, "rtc_offset",
454 		       SYSCTL_DESCR("Offset of real time clock from UTC in "
455 				    "minutes"),
456 		       sysctl_kern_rtc_offset, 0, &rtc_offset, 0,
457 		       CTL_KERN, KERN_RTC_OFFSET, CTL_EOL);
458 	sysctl_createv(clog, 0, NULL, NULL,
459 		       CTLFLAG_PERMANENT,
460 		       CTLTYPE_STRING, "root_device",
461 		       SYSCTL_DESCR("Name of the root device"),
462 		       sysctl_root_device, 0, NULL, 0,
463 		       CTL_KERN, KERN_ROOT_DEVICE, CTL_EOL);
464 	sysctl_createv(clog, 0, NULL, NULL,
465 		       CTLFLAG_PERMANENT,
466 		       CTLTYPE_INT, "msgbufsize",
467 		       SYSCTL_DESCR("Size of the kernel message buffer"),
468 		       sysctl_msgbuf, 0, NULL, 0,
469 		       CTL_KERN, KERN_MSGBUFSIZE, CTL_EOL);
470 	sysctl_createv(clog, 0, NULL, NULL,
471 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
472 		       CTLTYPE_INT, "fsync",
473 		       SYSCTL_DESCR("Whether the POSIX 1003.1b File "
474 				    "Synchronization Option is available on "
475 				    "this system"),
476 		       NULL, 1, NULL, 0,
477 		       CTL_KERN, KERN_FSYNC, CTL_EOL);
478 	sysctl_createv(clog, 0, NULL, NULL,
479 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
480 		       CTLTYPE_INT, "sysvmsg",
481 		       SYSCTL_DESCR("System V style message support available"),
482 		       NULL,
483 #ifdef SYSVMSG
484 		       1,
485 #else /* SYSVMSG */
486 		       0,
487 #endif /* SYSVMSG */
488 		       NULL, 0, CTL_KERN, KERN_SYSVMSG, CTL_EOL);
489 	sysctl_createv(clog, 0, NULL, NULL,
490 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
491 		       CTLTYPE_INT, "sysvsem",
492 		       SYSCTL_DESCR("System V style semaphore support "
493 				    "available"), NULL,
494 #ifdef SYSVSEM
495 		       1,
496 #else /* SYSVSEM */
497 		       0,
498 #endif /* SYSVSEM */
499 		       NULL, 0, CTL_KERN, KERN_SYSVSEM, CTL_EOL);
500 	sysctl_createv(clog, 0, NULL, NULL,
501 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
502 		       CTLTYPE_INT, "sysvshm",
503 		       SYSCTL_DESCR("System V style shared memory support "
504 				    "available"), NULL,
505 #ifdef SYSVSHM
506 		       1,
507 #else /* SYSVSHM */
508 		       0,
509 #endif /* SYSVSHM */
510 		       NULL, 0, CTL_KERN, KERN_SYSVSHM, CTL_EOL);
511 	sysctl_createv(clog, 0, NULL, NULL,
512 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
513 		       CTLTYPE_INT, "synchronized_io",
514 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Synchronized "
515 				    "I/O Option is available on this system"),
516 		       NULL, 1, NULL, 0,
517 		       CTL_KERN, KERN_SYNCHRONIZED_IO, CTL_EOL);
518 	sysctl_createv(clog, 0, NULL, NULL,
519 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
520 		       CTLTYPE_INT, "iov_max",
521 		       SYSCTL_DESCR("Maximum number of iovec structures per "
522 				    "process"),
523 		       NULL, IOV_MAX, NULL, 0,
524 		       CTL_KERN, KERN_IOV_MAX, CTL_EOL);
525 	sysctl_createv(clog, 0, NULL, NULL,
526 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
527 		       CTLTYPE_INT, "mapped_files",
528 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Memory Mapped "
529 				    "Files Option is available on this system"),
530 		       NULL, 1, NULL, 0,
531 		       CTL_KERN, KERN_MAPPED_FILES, CTL_EOL);
532 	sysctl_createv(clog, 0, NULL, NULL,
533 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
534 		       CTLTYPE_INT, "memlock",
535 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Process Memory "
536 				    "Locking Option is available on this "
537 				    "system"),
538 		       NULL, 1, NULL, 0,
539 		       CTL_KERN, KERN_MEMLOCK, CTL_EOL);
540 	sysctl_createv(clog, 0, NULL, NULL,
541 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
542 		       CTLTYPE_INT, "memlock_range",
543 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Range Memory "
544 				    "Locking Option is available on this "
545 				    "system"),
546 		       NULL, 1, NULL, 0,
547 		       CTL_KERN, KERN_MEMLOCK_RANGE, CTL_EOL);
548 	sysctl_createv(clog, 0, NULL, NULL,
549 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
550 		       CTLTYPE_INT, "memory_protection",
551 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Memory "
552 				    "Protection Option is available on this "
553 				    "system"),
554 		       NULL, 1, NULL, 0,
555 		       CTL_KERN, KERN_MEMORY_PROTECTION, CTL_EOL);
556 	sysctl_createv(clog, 0, NULL, NULL,
557 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
558 		       CTLTYPE_INT, "login_name_max",
559 		       SYSCTL_DESCR("Maximum login name length"),
560 		       NULL, LOGIN_NAME_MAX, NULL, 0,
561 		       CTL_KERN, KERN_LOGIN_NAME_MAX, CTL_EOL);
562 	sysctl_createv(clog, 0, NULL, NULL,
563 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
564 		       CTLTYPE_STRING, "defcorename",
565 		       SYSCTL_DESCR("Default core file name"),
566 		       sysctl_kern_defcorename, 0, defcorename, MAXPATHLEN,
567 		       CTL_KERN, KERN_DEFCORENAME, CTL_EOL);
568 	sysctl_createv(clog, 0, NULL, NULL,
569 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
570 		       CTLTYPE_INT, "logsigexit",
571 		       SYSCTL_DESCR("Log process exit when caused by signals"),
572 		       NULL, 0, &kern_logsigexit, 0,
573 		       CTL_KERN, KERN_LOGSIGEXIT, CTL_EOL);
574 	sysctl_createv(clog, 0, NULL, NULL,
575 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
576 		       CTLTYPE_INT, "fscale",
577 		       SYSCTL_DESCR("Kernel fixed-point scale factor"),
578 		       NULL, FSCALE, NULL, 0,
579 		       CTL_KERN, KERN_FSCALE, CTL_EOL);
580 	sysctl_createv(clog, 0, NULL, NULL,
581 		       CTLFLAG_PERMANENT,
582 		       CTLTYPE_INT, "ccpu",
583 		       SYSCTL_DESCR("Scheduler exponential decay value"),
584 		       NULL, 0, &ccpu, 0,
585 		       CTL_KERN, KERN_CCPU, CTL_EOL);
586 	sysctl_createv(clog, 0, NULL, NULL,
587 		       CTLFLAG_PERMANENT,
588 		       CTLTYPE_STRUCT, "cp_time",
589 		       SYSCTL_DESCR("Clock ticks spent in different CPU states"),
590 		       sysctl_kern_cptime, 0, NULL, 0,
591 		       CTL_KERN, KERN_CP_TIME, CTL_EOL);
592 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
593 	sysctl_createv(clog, 0, NULL, NULL,
594 		       CTLFLAG_PERMANENT,
595 		       CTLTYPE_STRUCT, "sysvipc_info",
596 		       SYSCTL_DESCR("System V style IPC information"),
597 		       sysctl_kern_sysvipc, 0, NULL, 0,
598 		       CTL_KERN, KERN_SYSVIPC_INFO, CTL_EOL);
599 #endif /* SYSVMSG || SYSVSEM || SYSVSHM */
600 	sysctl_createv(clog, 0, NULL, NULL,
601 		       CTLFLAG_PERMANENT,
602 		       CTLTYPE_INT, "msgbuf",
603 		       SYSCTL_DESCR("Kernel message buffer"),
604 		       sysctl_msgbuf, 0, NULL, 0,
605 		       CTL_KERN, KERN_MSGBUF, CTL_EOL);
606 	sysctl_createv(clog, 0, NULL, NULL,
607 		       CTLFLAG_PERMANENT,
608 		       CTLTYPE_STRUCT, "consdev",
609 		       SYSCTL_DESCR("Console device"),
610 		       sysctl_consdev, 0, NULL, sizeof(dev_t),
611 		       CTL_KERN, KERN_CONSDEV, CTL_EOL);
612 #if NPTY > 0
613 	sysctl_createv(clog, 0, NULL, NULL,
614 		       CTLFLAG_PERMANENT,
615 		       CTLTYPE_INT, "maxptys",
616 		       SYSCTL_DESCR("Maximum number of pseudo-ttys"),
617 		       sysctl_kern_maxptys, 0, NULL, 0,
618 		       CTL_KERN, KERN_MAXPTYS, CTL_EOL);
619 #endif /* NPTY > 0 */
620 	sysctl_createv(clog, 0, NULL, NULL,
621 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
622 		       CTLTYPE_INT, "maxphys",
623 		       SYSCTL_DESCR("Maximum raw I/O transfer size"),
624 		       NULL, MAXPHYS, NULL, 0,
625 		       CTL_KERN, KERN_MAXPHYS, CTL_EOL);
626 	sysctl_createv(clog, 0, NULL, NULL,
627 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
628 		       CTLTYPE_INT, "sbmax",
629 		       SYSCTL_DESCR("Maximum socket buffer size"),
630 		       sysctl_kern_sbmax, 0, NULL, 0,
631 		       CTL_KERN, KERN_SBMAX, CTL_EOL);
632 	sysctl_createv(clog, 0, NULL, NULL,
633 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
634 		       CTLTYPE_INT, "monotonic_clock",
635 		       SYSCTL_DESCR("Implementation version of the POSIX "
636 				    "1003.1b Monotonic Clock Option"),
637 		       /* XXX _POSIX_VERSION */
638 		       NULL, _POSIX_MONOTONIC_CLOCK, NULL, 0,
639 		       CTL_KERN, KERN_MONOTONIC_CLOCK, CTL_EOL);
640 	sysctl_createv(clog, 0, NULL, NULL,
641 		       CTLFLAG_PERMANENT,
642 		       CTLTYPE_INT, "urandom",
643 		       SYSCTL_DESCR("Random integer value"),
644 		       sysctl_kern_urnd, 0, NULL, 0,
645 		       CTL_KERN, KERN_URND, CTL_EOL);
646 	sysctl_createv(clog, 0, NULL, NULL,
647 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
648 		       CTLTYPE_INT, "labelsector",
649 		       SYSCTL_DESCR("Sector number containing the disklabel"),
650 		       NULL, LABELSECTOR, NULL, 0,
651 		       CTL_KERN, KERN_LABELSECTOR, CTL_EOL);
652 	sysctl_createv(clog, 0, NULL, NULL,
653 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
654 		       CTLTYPE_INT, "labeloffset",
655 		       SYSCTL_DESCR("Offset of the disklabel within the "
656 				    "sector"),
657 		       NULL, LABELOFFSET, NULL, 0,
658 		       CTL_KERN, KERN_LABELOFFSET, CTL_EOL);
659 	sysctl_createv(clog, 0, NULL, NULL,
660 		       CTLFLAG_PERMANENT,
661 		       CTLTYPE_NODE, "lwp",
662 		       SYSCTL_DESCR("System-wide LWP information"),
663 		       sysctl_kern_lwp, 0, NULL, 0,
664 		       CTL_KERN, KERN_LWP, CTL_EOL);
665 	sysctl_createv(clog, 0, NULL, NULL,
666 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
667 		       CTLTYPE_INT, "forkfsleep",
668 		       SYSCTL_DESCR("Milliseconds to sleep on fork failure due "
669 				    "to process limits"),
670 		       sysctl_kern_forkfsleep, 0, NULL, 0,
671 		       CTL_KERN, KERN_FORKFSLEEP, CTL_EOL);
672 	sysctl_createv(clog, 0, NULL, NULL,
673 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
674 		       CTLTYPE_INT, "posix_threads",
675 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
676 				    "Threads option to which the system "
677 				    "attempts to conform"),
678 		       /* XXX _POSIX_VERSION */
679 		       NULL, _POSIX_THREADS, NULL, 0,
680 		       CTL_KERN, KERN_POSIX_THREADS, CTL_EOL);
681 	sysctl_createv(clog, 0, NULL, NULL,
682 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
683 		       CTLTYPE_INT, "posix_semaphores",
684 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
685 				    "Semaphores option to which the system "
686 				    "attempts to conform"), NULL,
687 #ifdef P1003_1B_SEMAPHORE
688 		       200112,
689 #else /* P1003_1B_SEMAPHORE */
690 		       0,
691 #endif /* P1003_1B_SEMAPHORE */
692 		       NULL, 0, CTL_KERN, KERN_POSIX_SEMAPHORES, CTL_EOL);
693 	sysctl_createv(clog, 0, NULL, NULL,
694 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
695 		       CTLTYPE_INT, "posix_barriers",
696 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
697 				    "Barriers option to which the system "
698 				    "attempts to conform"),
699 		       /* XXX _POSIX_VERSION */
700 		       NULL, _POSIX_BARRIERS, NULL, 0,
701 		       CTL_KERN, KERN_POSIX_BARRIERS, CTL_EOL);
702 	sysctl_createv(clog, 0, NULL, NULL,
703 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
704 		       CTLTYPE_INT, "posix_timers",
705 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
706 				    "Timers option to which the system "
707 				    "attempts to conform"),
708 		       /* XXX _POSIX_VERSION */
709 		       NULL, _POSIX_TIMERS, NULL, 0,
710 		       CTL_KERN, KERN_POSIX_TIMERS, CTL_EOL);
711 	sysctl_createv(clog, 0, NULL, NULL,
712 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
713 		       CTLTYPE_INT, "posix_spin_locks",
714 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its Spin "
715 				    "Locks option to which the system attempts "
716 				    "to conform"),
717 		       /* XXX _POSIX_VERSION */
718 		       NULL, _POSIX_SPIN_LOCKS, NULL, 0,
719 		       CTL_KERN, KERN_POSIX_SPIN_LOCKS, CTL_EOL);
720 	sysctl_createv(clog, 0, NULL, NULL,
721 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
722 		       CTLTYPE_INT, "posix_reader_writer_locks",
723 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
724 				    "Read-Write Locks option to which the "
725 				    "system attempts to conform"),
726 		       /* XXX _POSIX_VERSION */
727 		       NULL, _POSIX_READER_WRITER_LOCKS, NULL, 0,
728 		       CTL_KERN, KERN_POSIX_READER_WRITER_LOCKS, CTL_EOL);
729 	sysctl_createv(clog, 0, NULL, NULL,
730 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
731 		       CTLTYPE_INT, "dump_on_panic",
732 		       SYSCTL_DESCR("Perform a crash dump on system panic"),
733 		       NULL, 0, &dumponpanic, 0,
734 		       CTL_KERN, KERN_DUMP_ON_PANIC, CTL_EOL);
735 	sysctl_createv(clog, 0, NULL, NULL,
736 		       CTLFLAG_PERMANENT,
737 		       CTLTYPE_INT, "root_partition",
738 		       SYSCTL_DESCR("Root partition on the root device"),
739 		       sysctl_kern_root_partition, 0, NULL, 0,
740 		       CTL_KERN, KERN_ROOT_PARTITION, CTL_EOL);
741 	sysctl_createv(clog, 0, NULL, NULL,
742 		       CTLFLAG_PERMANENT,
743 		       CTLTYPE_STRUCT, "drivers",
744 		       SYSCTL_DESCR("List of all drivers with block and "
745 				    "character device numbers"),
746 		       sysctl_kern_drivers, 0, NULL, 0,
747 		       CTL_KERN, KERN_DRIVERS, CTL_EOL);
748 	sysctl_createv(clog, 0, NULL, NULL,
749 		       CTLFLAG_PERMANENT,
750 		       CTLTYPE_STRUCT, "file2",
751 		       SYSCTL_DESCR("System open file table"),
752 		       sysctl_kern_file2, 0, NULL, 0,
753 		       CTL_KERN, KERN_FILE2, CTL_EOL);
754 #ifdef VERIFIED_EXEC
755 	sysctl_createv(clog, 0, NULL, NULL,
756 		       CTLFLAG_PERMANENT,
757 		       CTLTYPE_NODE, "veriexec",
758 		       SYSCTL_DESCR("Verified Exec"),
759 		       NULL, 0, NULL, 0,
760 		       CTL_KERN, KERN_VERIEXEC, CTL_EOL);
761 	sysctl_createv(clog, 0, NULL, NULL,
762 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
763 		       CTLTYPE_INT, "verbose",
764 		       SYSCTL_DESCR("Verified Exec verbose level"),
765 		       NULL, 0, &veriexec_verbose, 0,
766 		       CTL_KERN, KERN_VERIEXEC, VERIEXEC_VERBOSE,
767 		       CTL_EOL);
768 	sysctl_createv(clog, 0, NULL, NULL,
769 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
770 		       CTLTYPE_INT, "strict",
771 		       SYSCTL_DESCR("Verified Exec strict level"),
772 		       sysctl_kern_veriexec, 0, NULL, 0,
773 		       CTL_KERN, KERN_VERIEXEC, VERIEXEC_STRICT, CTL_EOL);
774 	sysctl_createv(clog, 0, NULL, NULL,
775 		       CTLFLAG_PERMANENT,
776 		       CTLTYPE_STRING, "algorithms",
777 		       SYSCTL_DESCR("Verified Exec supported hashing "
778 				    "algorithms"),
779 		       sysctl_kern_veriexec, 0, NULL, 0,
780 		       CTL_KERN, KERN_VERIEXEC, VERIEXEC_ALGORITHMS, CTL_EOL);
781 	sysctl_createv(clog, 0, NULL, &veriexec_count_node,
782 		       CTLFLAG_PERMANENT,
783 		       CTLTYPE_NODE, "count",
784 		       SYSCTL_DESCR("Number of fingerprints on device(s)"),
785 		       NULL, 0, NULL, 0,
786 		       CTL_KERN, KERN_VERIEXEC, VERIEXEC_COUNT, CTL_EOL);
787 #endif /* VERIFIED_EXEC */
788 	sysctl_createv(clog, 0, NULL, NULL,
789 		       CTLFLAG_PERMANENT,
790 		       CTLTYPE_STRUCT, "cp_id",
791 		       SYSCTL_DESCR("Mapping of CPU number to CPU id"),
792 		       sysctl_kern_cpid, 0, NULL, 0,
793 		       CTL_KERN, KERN_CP_ID, CTL_EOL);
794 }
795 
796 SYSCTL_SETUP(sysctl_kern_proc_setup,
797 	     "sysctl kern.proc/proc2/proc_args subtree setup")
798 {
799 
800 	sysctl_createv(clog, 0, NULL, NULL,
801 		       CTLFLAG_PERMANENT,
802 		       CTLTYPE_NODE, "kern", NULL,
803 		       NULL, 0, NULL, 0,
804 		       CTL_KERN, CTL_EOL);
805 
806 	sysctl_createv(clog, 0, NULL, NULL,
807 		       CTLFLAG_PERMANENT,
808 		       CTLTYPE_NODE, "proc",
809 		       SYSCTL_DESCR("System-wide process information"),
810 		       sysctl_doeproc, 0, NULL, 0,
811 		       CTL_KERN, KERN_PROC, CTL_EOL);
812 	sysctl_createv(clog, 0, NULL, NULL,
813 		       CTLFLAG_PERMANENT,
814 		       CTLTYPE_NODE, "proc2",
815 		       SYSCTL_DESCR("Machine-independent process information"),
816 		       sysctl_doeproc, 0, NULL, 0,
817 		       CTL_KERN, KERN_PROC2, CTL_EOL);
818 	sysctl_createv(clog, 0, NULL, NULL,
819 		       CTLFLAG_PERMANENT,
820 		       CTLTYPE_NODE, "proc_args",
821 		       SYSCTL_DESCR("Process argument information"),
822 		       sysctl_kern_proc_args, 0, NULL, 0,
823 		       CTL_KERN, KERN_PROC_ARGS, CTL_EOL);
824 
825 	/*
826 	  "nodes" under these:
827 
828 	  KERN_PROC_ALL
829 	  KERN_PROC_PID pid
830 	  KERN_PROC_PGRP pgrp
831 	  KERN_PROC_SESSION sess
832 	  KERN_PROC_TTY tty
833 	  KERN_PROC_UID uid
834 	  KERN_PROC_RUID uid
835 	  KERN_PROC_GID gid
836 	  KERN_PROC_RGID gid
837 
838 	  all in all, probably not worth the effort...
839 	*/
840 }
841 
842 SYSCTL_SETUP(sysctl_hw_setup, "sysctl hw subtree setup")
843 {
844 	u_int u;
845 	u_quad_t q;
846 
847 	sysctl_createv(clog, 0, NULL, NULL,
848 		       CTLFLAG_PERMANENT,
849 		       CTLTYPE_NODE, "hw", NULL,
850 		       NULL, 0, NULL, 0,
851 		       CTL_HW, CTL_EOL);
852 
853 	sysctl_createv(clog, 0, NULL, NULL,
854 		       CTLFLAG_PERMANENT,
855 		       CTLTYPE_STRING, "machine",
856 		       SYSCTL_DESCR("Machine class"),
857 		       NULL, 0, machine, 0,
858 		       CTL_HW, HW_MACHINE, CTL_EOL);
859 	sysctl_createv(clog, 0, NULL, NULL,
860 		       CTLFLAG_PERMANENT,
861 		       CTLTYPE_STRING, "model",
862 		       SYSCTL_DESCR("Machine model"),
863 		       NULL, 0, cpu_model, 0,
864 		       CTL_HW, HW_MODEL, CTL_EOL);
865 	sysctl_createv(clog, 0, NULL, NULL,
866 		       CTLFLAG_PERMANENT,
867 		       CTLTYPE_INT, "ncpu",
868 		       SYSCTL_DESCR("Number of active CPUs"),
869 		       sysctl_hw_ncpu, 0, NULL, 0,
870 		       CTL_HW, HW_NCPU, CTL_EOL);
871 	sysctl_createv(clog, 0, NULL, NULL,
872 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
873 		       CTLTYPE_INT, "byteorder",
874 		       SYSCTL_DESCR("System byte order"),
875 		       NULL, BYTE_ORDER, NULL, 0,
876 		       CTL_HW, HW_BYTEORDER, CTL_EOL);
877 	u = ((u_int)physmem > (UINT_MAX / PAGE_SIZE)) ?
878 		UINT_MAX : physmem * PAGE_SIZE;
879 	sysctl_createv(clog, 0, NULL, NULL,
880 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
881 		       CTLTYPE_INT, "physmem",
882 		       SYSCTL_DESCR("Bytes of physical memory"),
883 		       NULL, u, NULL, 0,
884 		       CTL_HW, HW_PHYSMEM, CTL_EOL);
885 	sysctl_createv(clog, 0, NULL, NULL,
886 		       CTLFLAG_PERMANENT,
887 		       CTLTYPE_INT, "usermem",
888 		       SYSCTL_DESCR("Bytes of non-kernel memory"),
889 		       sysctl_hw_usermem, 0, NULL, 0,
890 		       CTL_HW, HW_USERMEM, CTL_EOL);
891 	sysctl_createv(clog, 0, NULL, NULL,
892 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
893 		       CTLTYPE_INT, "pagesize",
894 		       SYSCTL_DESCR("Software page size"),
895 		       NULL, PAGE_SIZE, NULL, 0,
896 		       CTL_HW, HW_PAGESIZE, CTL_EOL);
897 	sysctl_createv(clog, 0, NULL, NULL,
898 		       CTLFLAG_PERMANENT,
899 		       CTLTYPE_STRING, "disknames",
900 		       SYSCTL_DESCR("List of disk devices present"),
901 		       sysctl_hw_disknames, 0, NULL, 0,
902 		       CTL_HW, HW_DISKNAMES, CTL_EOL);
903 	sysctl_createv(clog, 0, NULL, NULL,
904 		       CTLFLAG_PERMANENT,
905 		       CTLTYPE_STRUCT, "diskstats",
906 		       SYSCTL_DESCR("Statistics on disk operation"),
907 		       sysctl_hw_diskstats, 0, NULL, 0,
908 		       CTL_HW, HW_DISKSTATS, CTL_EOL);
909 	sysctl_createv(clog, 0, NULL, NULL,
910 		       CTLFLAG_PERMANENT,
911 		       CTLTYPE_STRING, "machine_arch",
912 		       SYSCTL_DESCR("Machine CPU class"),
913 		       NULL, 0, machine_arch, 0,
914 		       CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
915 	sysctl_createv(clog, 0, NULL, NULL,
916 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
917 		       CTLTYPE_INT, "alignbytes",
918 		       SYSCTL_DESCR("Alignment constraint for all possible "
919 				    "data types"),
920 		       NULL, ALIGNBYTES, NULL, 0,
921 		       CTL_HW, HW_ALIGNBYTES, CTL_EOL);
922 	sysctl_createv(clog, 0, NULL, NULL,
923 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
924 		       CTLTYPE_STRING, "cnmagic",
925 		       SYSCTL_DESCR("Console magic key sequence"),
926 		       sysctl_hw_cnmagic, 0, NULL, CNS_LEN,
927 		       CTL_HW, HW_CNMAGIC, CTL_EOL);
928 	q = (u_quad_t)physmem * PAGE_SIZE;
929 	sysctl_createv(clog, 0, NULL, NULL,
930 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
931 		       CTLTYPE_QUAD, "physmem64",
932 		       SYSCTL_DESCR("Bytes of physical memory"),
933 		       NULL, q, NULL, 0,
934 		       CTL_HW, HW_PHYSMEM64, CTL_EOL);
935 	sysctl_createv(clog, 0, NULL, NULL,
936 		       CTLFLAG_PERMANENT,
937 		       CTLTYPE_QUAD, "usermem64",
938 		       SYSCTL_DESCR("Bytes of non-kernel memory"),
939 		       sysctl_hw_usermem, 0, NULL, 0,
940 		       CTL_HW, HW_USERMEM64, CTL_EOL);
941 }
942 
943 #ifdef DEBUG
944 /*
945  * Debugging related system variables.
946  */
947 struct ctldebug /* debug0, */ /* debug1, */ debug2, debug3, debug4;
948 struct ctldebug debug5, debug6, debug7, debug8, debug9;
949 struct ctldebug debug10, debug11, debug12, debug13, debug14;
950 struct ctldebug debug15, debug16, debug17, debug18, debug19;
951 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
952 	&debug0, &debug1, &debug2, &debug3, &debug4,
953 	&debug5, &debug6, &debug7, &debug8, &debug9,
954 	&debug10, &debug11, &debug12, &debug13, &debug14,
955 	&debug15, &debug16, &debug17, &debug18, &debug19,
956 };
957 
958 /*
959  * this setup routine is a replacement for debug_sysctl()
960  *
961  * note that it creates several nodes per defined debug variable
962  */
963 SYSCTL_SETUP(sysctl_debug_setup, "sysctl debug subtree setup")
964 {
965 	struct ctldebug *cdp;
966 	char nodename[20];
967 	int i;
968 
969 	/*
970 	 * two ways here:
971 	 *
972 	 * the "old" way (debug.name -> value) which was emulated by
973 	 * the sysctl(8) binary
974 	 *
975 	 * the new way, which the sysctl(8) binary was actually using
976 
977 	 node	debug
978 	 node	debug.0
979 	 string	debug.0.name
980 	 int	debug.0.value
981 	 int	debug.name
982 
983 	 */
984 
985 	sysctl_createv(clog, 0, NULL, NULL,
986 		       CTLFLAG_PERMANENT,
987 		       CTLTYPE_NODE, "debug", NULL,
988 		       NULL, 0, NULL, 0,
989 		       CTL_DEBUG, CTL_EOL);
990 
991 	for (i = 0; i < CTL_DEBUG_MAXID; i++) {
992 		cdp = debugvars[i];
993 		if (cdp->debugname == NULL || cdp->debugvar == NULL)
994 			continue;
995 
996 		snprintf(nodename, sizeof(nodename), "debug%d", i);
997 		sysctl_createv(clog, 0, NULL, NULL,
998 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
999 			       CTLTYPE_NODE, nodename, NULL,
1000 			       NULL, 0, NULL, 0,
1001 			       CTL_DEBUG, i, CTL_EOL);
1002 		sysctl_createv(clog, 0, NULL, NULL,
1003 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
1004 			       CTLTYPE_STRING, "name", NULL,
1005 			       /*XXXUNCONST*/
1006 			       NULL, 0, __UNCONST(cdp->debugname), 0,
1007 			       CTL_DEBUG, i, CTL_DEBUG_NAME, CTL_EOL);
1008 		sysctl_createv(clog, 0, NULL, NULL,
1009 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
1010 			       CTLTYPE_INT, "value", NULL,
1011 			       NULL, 0, cdp->debugvar, 0,
1012 			       CTL_DEBUG, i, CTL_DEBUG_VALUE, CTL_EOL);
1013 		sysctl_createv(clog, 0, NULL, NULL,
1014 			       CTLFLAG_PERMANENT,
1015 			       CTLTYPE_INT, cdp->debugname, NULL,
1016 			       NULL, 0, cdp->debugvar, 0,
1017 			       CTL_DEBUG, CTL_CREATE, CTL_EOL);
1018 	}
1019 }
1020 #endif /* DEBUG */
1021 
1022 SYSCTL_SETUP(sysctl_security_setup, "sysctl security subtree setup")
1023 {
1024 	sysctl_createv(clog, 0, NULL, NULL,
1025 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1026 		       CTLTYPE_INT, "curtain",
1027 		       SYSCTL_DESCR("Curtain information about objects"
1028 				    " to users not owning them."),
1029 		       NULL, 0, &security_curtain, 0,
1030 		       CTL_SECURITY, SECURITY_CURTAIN, CTL_EOL);
1031 }
1032 
1033 /*
1034  * ********************************************************************
1035  * section 2: private node-specific helper routines.
1036  * ********************************************************************
1037  */
1038 
1039 /*
1040  * sysctl helper routine for kern.maxvnodes.  drain vnodes if
1041  * new value is lower than desiredvnodes and then calls reinit
1042  * routines that needs to adjust to the new value.
1043  */
1044 static int
1045 sysctl_kern_maxvnodes(SYSCTLFN_ARGS)
1046 {
1047 	int error, new_vnodes, old_vnodes;
1048 	struct sysctlnode node;
1049 
1050 	new_vnodes = desiredvnodes;
1051 	node = *rnode;
1052 	node.sysctl_data = &new_vnodes;
1053 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1054 	if (error || newp == NULL)
1055 		return (error);
1056 
1057 	old_vnodes = desiredvnodes;
1058 	desiredvnodes = new_vnodes;
1059 	if (new_vnodes < old_vnodes) {
1060 		error = vfs_drainvnodes(new_vnodes, l->l_proc);
1061 		if (error) {
1062 			desiredvnodes = old_vnodes;
1063 			return (error);
1064 		}
1065 	}
1066 	vfs_reinit();
1067 	nchreinit();
1068 
1069 	return (0);
1070 }
1071 
1072 /*
1073  * sysctl helper routine for rtc_offset - set time after changes
1074  */
1075 static int
1076 sysctl_kern_rtc_offset(SYSCTLFN_ARGS)
1077 {
1078 	struct timeval tv, delta;
1079 	int s, error, new_rtc_offset;
1080 	struct sysctlnode node;
1081 
1082 	new_rtc_offset = rtc_offset;
1083 	node = *rnode;
1084 	node.sysctl_data = &new_rtc_offset;
1085 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1086 	if (error || newp == NULL)
1087 		return (error);
1088 
1089 	if (securelevel > 0)
1090 		return (EPERM);
1091 	if (rtc_offset == new_rtc_offset)
1092 		return (0);
1093 
1094 	/* if we change the offset, adjust the time */
1095 	s = splclock();
1096 	tv = time;
1097 	splx(s);
1098 	delta.tv_sec = 60*(new_rtc_offset - rtc_offset);
1099 	delta.tv_usec = 0;
1100 	timeradd(&tv, &delta, &tv);
1101 	rtc_offset = new_rtc_offset;
1102 	settime(&tv);
1103 
1104 	return (0);
1105 }
1106 
1107 /*
1108  * sysctl helper routine for kern.maxproc.  ensures that the new
1109  * values are not too low or too high.
1110  */
1111 static int
1112 sysctl_kern_maxproc(SYSCTLFN_ARGS)
1113 {
1114 	int error, nmaxproc;
1115 	struct sysctlnode node;
1116 
1117 	nmaxproc = maxproc;
1118 	node = *rnode;
1119 	node.sysctl_data = &nmaxproc;
1120 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1121 	if (error || newp == NULL)
1122 		return (error);
1123 
1124 	if (nmaxproc < 0 || nmaxproc >= PID_MAX)
1125 		return (EINVAL);
1126 #ifdef __HAVE_CPU_MAXPROC
1127 	if (nmaxproc > cpu_maxproc())
1128 		return (EINVAL);
1129 #endif
1130 	maxproc = nmaxproc;
1131 
1132 	return (0);
1133 }
1134 
1135 /*
1136  * sysctl helper routine for kern.securelevel.  ensures that the value
1137  * only rises unless the caller has pid 1 (assumed to be init).
1138  */
1139 static int
1140 sysctl_kern_securelevel(SYSCTLFN_ARGS)
1141 {
1142 	int newsecurelevel, error;
1143 	struct sysctlnode node;
1144 
1145 	newsecurelevel = securelevel;
1146 	node = *rnode;
1147 	node.sysctl_data = &newsecurelevel;
1148 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1149 	if (error || newp == NULL)
1150 		return (error);
1151 
1152 	if (newsecurelevel < securelevel && l && l->l_proc->p_pid != 1)
1153 		return (EPERM);
1154 	securelevel = newsecurelevel;
1155 
1156 	return (error);
1157 }
1158 
1159 /*
1160  * sysctl helper function for kern.hostid.  the hostid is a long, but
1161  * we export it as an int, so we need to give it a little help.
1162  */
1163 static int
1164 sysctl_kern_hostid(SYSCTLFN_ARGS)
1165 {
1166 	int error, inthostid;
1167 	struct sysctlnode node;
1168 
1169 	inthostid = hostid;  /* XXX assumes sizeof int <= sizeof long */
1170 	node = *rnode;
1171 	node.sysctl_data = &inthostid;
1172 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1173 	if (error || newp == NULL)
1174 		return (error);
1175 
1176 	hostid = (unsigned)inthostid;
1177 
1178 	return (0);
1179 }
1180 
1181 /*
1182  * sysctl helper function for kern.hostname and kern.domainnname.
1183  * resets the relevant recorded length when the underlying name is
1184  * changed.
1185  */
1186 static int
1187 sysctl_setlen(SYSCTLFN_ARGS)
1188 {
1189 	int error;
1190 
1191 	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
1192 	if (error || newp == NULL)
1193 		return (error);
1194 
1195 	switch (rnode->sysctl_num) {
1196 	case KERN_HOSTNAME:
1197 		hostnamelen = strlen((const char*)rnode->sysctl_data);
1198 		break;
1199 	case KERN_DOMAINNAME:
1200 		domainnamelen = strlen((const char*)rnode->sysctl_data);
1201 		break;
1202 	}
1203 
1204 	return (0);
1205 }
1206 
1207 /*
1208  * sysctl helper routine for kern.clockrate.  assembles a struct on
1209  * the fly to be returned to the caller.
1210  */
1211 static int
1212 sysctl_kern_clockrate(SYSCTLFN_ARGS)
1213 {
1214 	struct clockinfo clkinfo;
1215 	struct sysctlnode node;
1216 
1217 	clkinfo.tick = tick;
1218 	clkinfo.tickadj = tickadj;
1219 	clkinfo.hz = hz;
1220 	clkinfo.profhz = profhz;
1221 	clkinfo.stathz = stathz ? stathz : hz;
1222 
1223 	node = *rnode;
1224 	node.sysctl_data = &clkinfo;
1225 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1226 }
1227 
1228 
1229 /*
1230  * sysctl helper routine for kern.file pseudo-subtree.
1231  */
1232 static int
1233 sysctl_kern_file(SYSCTLFN_ARGS)
1234 {
1235 	int error;
1236 	size_t buflen;
1237 	struct file *fp;
1238 	char *start, *where;
1239 
1240 	start = where = oldp;
1241 	buflen = *oldlenp;
1242 	if (where == NULL) {
1243 		/*
1244 		 * overestimate by 10 files
1245 		 */
1246 		*oldlenp = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
1247 		return (0);
1248 	}
1249 
1250 	/*
1251 	 * first copyout filehead
1252 	 */
1253 	if (buflen < sizeof(filehead)) {
1254 		*oldlenp = 0;
1255 		return (0);
1256 	}
1257 	error = copyout(&filehead, where, sizeof(filehead));
1258 	if (error)
1259 		return (error);
1260 	buflen -= sizeof(filehead);
1261 	where += sizeof(filehead);
1262 
1263 	/*
1264 	 * followed by an array of file structures
1265 	 */
1266 	LIST_FOREACH(fp, &filehead, f_list) {
1267 		if (CURTAIN(l->l_proc->p_ucred->cr_uid, fp->f_cred->cr_uid))
1268 			continue;
1269 		if (buflen < sizeof(struct file)) {
1270 			*oldlenp = where - start;
1271 			return (ENOMEM);
1272 		}
1273 		error = copyout(fp, where, sizeof(struct file));
1274 		if (error)
1275 			return (error);
1276 		buflen -= sizeof(struct file);
1277 		where += sizeof(struct file);
1278 	}
1279 	*oldlenp = where - start;
1280 	return (0);
1281 }
1282 
1283 /*
1284  * sysctl helper routine for kern.autonicetime and kern.autoniceval.
1285  * asserts that the assigned value is in the correct range.
1286  */
1287 static int
1288 sysctl_kern_autonice(SYSCTLFN_ARGS)
1289 {
1290 	int error, t = 0;
1291 	struct sysctlnode node;
1292 
1293 	node = *rnode;
1294 	t = *(int*)node.sysctl_data;
1295 	node.sysctl_data = &t;
1296 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1297 	if (error || newp == NULL)
1298 		return (error);
1299 
1300 	switch (node.sysctl_num) {
1301 	case KERN_AUTONICETIME:
1302 		if (t >= 0)
1303 			autonicetime = t;
1304 		break;
1305 	case KERN_AUTONICEVAL:
1306 		if (t < PRIO_MIN)
1307 			t = PRIO_MIN;
1308 		else if (t > PRIO_MAX)
1309 			t = PRIO_MAX;
1310 		autoniceval = t;
1311 		break;
1312 	}
1313 
1314 	return (0);
1315 }
1316 
1317 /*
1318  * sysctl helper routine for kern.msgbufsize and kern.msgbuf.  for the
1319  * former it merely checks the message buffer is set up.  for the latter,
1320  * it also copies out the data if necessary.
1321  */
1322 static int
1323 sysctl_msgbuf(SYSCTLFN_ARGS)
1324 {
1325 	char *where = oldp;
1326 	size_t len, maxlen;
1327 	long beg, end;
1328 	int error;
1329 
1330 	if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
1331 		msgbufenabled = 0;
1332 		return (ENXIO);
1333 	}
1334 
1335 	switch (rnode->sysctl_num) {
1336 	case KERN_MSGBUFSIZE: {
1337 		struct sysctlnode node = *rnode;
1338 		int msg_bufs = (int)msgbufp->msg_bufs;
1339 		node.sysctl_data = &msg_bufs;
1340 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1341 	}
1342 	case KERN_MSGBUF:
1343 		break;
1344 	default:
1345 		return (EOPNOTSUPP);
1346 	}
1347 
1348 	if (newp != NULL)
1349 		return (EPERM);
1350 
1351         if (oldp == NULL) {
1352 		/* always return full buffer size */
1353 		*oldlenp = msgbufp->msg_bufs;
1354 		return (0);
1355         }
1356 
1357 	error = 0;
1358 	maxlen = MIN(msgbufp->msg_bufs, *oldlenp);
1359 
1360 	/*
1361 	 * First, copy from the write pointer to the end of
1362 	 * message buffer.
1363 	 */
1364 	beg = msgbufp->msg_bufx;
1365 	end = msgbufp->msg_bufs;
1366 	while (maxlen > 0) {
1367 		len = MIN(end - beg, maxlen);
1368 		if (len == 0)
1369 			break;
1370 		error = copyout(&msgbufp->msg_bufc[beg], where, len);
1371 		if (error)
1372 			break;
1373 		where += len;
1374 		maxlen -= len;
1375 
1376 		/*
1377 		 * ... then, copy from the beginning of message buffer to
1378 		 * the write pointer.
1379 		 */
1380 		beg = 0;
1381 		end = msgbufp->msg_bufx;
1382 	}
1383 
1384 	return (error);
1385 }
1386 
1387 /*
1388  * sysctl helper routine for kern.defcorename.  in the case of a new
1389  * string being assigned, check that it's not a zero-length string.
1390  * (XXX the check in -current doesn't work, but do we really care?)
1391  */
1392 static int
1393 sysctl_kern_defcorename(SYSCTLFN_ARGS)
1394 {
1395 	int error;
1396 	char newcorename[MAXPATHLEN];
1397 	struct sysctlnode node;
1398 
1399 	node = *rnode;
1400 	node.sysctl_data = &newcorename[0];
1401 	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
1402 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1403 	if (error || newp == NULL)
1404 		return (error);
1405 
1406 	/*
1407 	 * when sysctl_lookup() deals with a string, it's guaranteed
1408 	 * to come back nul terminated.  so there.  :)
1409 	 */
1410 	if (strlen(newcorename) == 0)
1411 		return (EINVAL);
1412 
1413 	memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
1414 
1415 	return (0);
1416 }
1417 
1418 /*
1419  * sysctl helper routine for kern.cp_time node.  adds up cpu time
1420  * across all cpus.
1421  */
1422 static int
1423 sysctl_kern_cptime(SYSCTLFN_ARGS)
1424 {
1425 	struct sysctlnode node = *rnode;
1426 
1427 #ifndef MULTIPROCESSOR
1428 
1429 	if (namelen == 1) {
1430 		if (name[0] != 0)
1431 			return (ENOENT);
1432 		/*
1433 		 * you're allowed to ask for the zero'th processor
1434 		 */
1435 		name++;
1436 		namelen--;
1437 	}
1438 	node.sysctl_data = curcpu()->ci_schedstate.spc_cp_time;
1439 	node.sysctl_size = sizeof(curcpu()->ci_schedstate.spc_cp_time);
1440 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1441 
1442 #else /* MULTIPROCESSOR */
1443 
1444 	u_int64_t *cp_time = NULL;
1445 	int error, n = sysctl_ncpus(), i;
1446 	struct cpu_info *ci;
1447 	CPU_INFO_ITERATOR cii;
1448 
1449 	/*
1450 	 * if you specifically pass a buffer that is the size of the
1451 	 * sum, or if you are probing for the size, you get the "sum"
1452 	 * of cp_time (and the size thereof) across all processors.
1453 	 *
1454 	 * alternately, you can pass an additional mib number and get
1455 	 * cp_time for that particular processor.
1456 	 */
1457 	switch (namelen) {
1458 	case 0:
1459 	    	if (*oldlenp == sizeof(u_int64_t) * CPUSTATES || oldp == NULL) {
1460 			node.sysctl_size = sizeof(u_int64_t) * CPUSTATES;
1461 			n = -1; /* SUM */
1462 		}
1463 		else {
1464 			node.sysctl_size = n * sizeof(u_int64_t) * CPUSTATES;
1465 			n = -2; /* ALL */
1466 		}
1467 		break;
1468 	case 1:
1469 		if (name[0] < 0 || name[0] >= n)
1470 			return (ENOENT); /* ENOSUCHPROCESSOR */
1471 		node.sysctl_size = sizeof(u_int64_t) * CPUSTATES;
1472 		n = name[0];
1473 		/*
1474 		 * adjust these so that sysctl_lookup() will be happy
1475 		 */
1476 		name++;
1477 		namelen--;
1478 		break;
1479 	default:
1480 		return (EINVAL);
1481 	}
1482 
1483 	cp_time = malloc(node.sysctl_size, M_TEMP, M_WAITOK|M_CANFAIL);
1484 	if (cp_time == NULL)
1485 		return (ENOMEM);
1486 	node.sysctl_data = cp_time;
1487 	memset(cp_time, 0, node.sysctl_size);
1488 
1489 	for (CPU_INFO_FOREACH(cii, ci)) {
1490 		if (n <= 0)
1491 			for (i = 0; i < CPUSTATES; i++)
1492 				cp_time[i] += ci->ci_schedstate.spc_cp_time[i];
1493 		/*
1494 		 * if a specific processor was requested and we just
1495 		 * did it, we're done here
1496 		 */
1497 		if (n == 0)
1498 			break;
1499 		/*
1500 		 * if doing "all", skip to next cp_time set for next processor
1501 		 */
1502 		if (n == -2)
1503 			cp_time += CPUSTATES;
1504 		/*
1505 		 * if we're doing a specific processor, we're one
1506 		 * processor closer
1507 		 */
1508 		if (n > 0)
1509 			n--;
1510 	}
1511 
1512 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1513 	free(node.sysctl_data, M_TEMP);
1514 	return (error);
1515 
1516 #endif /* MULTIPROCESSOR */
1517 }
1518 
1519 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
1520 /*
1521  * sysctl helper routine for kern.sysvipc_info subtree.
1522  */
1523 
1524 #define	FILL_PERM(src, dst) do { \
1525 	(dst)._key = (src)._key; \
1526 	(dst).uid = (src).uid; \
1527 	(dst).gid = (src).gid; \
1528 	(dst).cuid = (src).cuid; \
1529 	(dst).cgid = (src).cgid; \
1530 	(dst).mode = (src).mode; \
1531 	(dst)._seq = (src)._seq; \
1532 } while (/*CONSTCOND*/ 0);
1533 #define	FILL_MSG(src, dst) do { \
1534 	FILL_PERM((src).msg_perm, (dst).msg_perm); \
1535 	(dst).msg_qnum = (src).msg_qnum; \
1536 	(dst).msg_qbytes = (src).msg_qbytes; \
1537 	(dst)._msg_cbytes = (src)._msg_cbytes; \
1538 	(dst).msg_lspid = (src).msg_lspid; \
1539 	(dst).msg_lrpid = (src).msg_lrpid; \
1540 	(dst).msg_stime = (src).msg_stime; \
1541 	(dst).msg_rtime = (src).msg_rtime; \
1542 	(dst).msg_ctime = (src).msg_ctime; \
1543 } while (/*CONSTCOND*/ 0)
1544 #define	FILL_SEM(src, dst) do { \
1545 	FILL_PERM((src).sem_perm, (dst).sem_perm); \
1546 	(dst).sem_nsems = (src).sem_nsems; \
1547 	(dst).sem_otime = (src).sem_otime; \
1548 	(dst).sem_ctime = (src).sem_ctime; \
1549 } while (/*CONSTCOND*/ 0)
1550 #define	FILL_SHM(src, dst) do { \
1551 	FILL_PERM((src).shm_perm, (dst).shm_perm); \
1552 	(dst).shm_segsz = (src).shm_segsz; \
1553 	(dst).shm_lpid = (src).shm_lpid; \
1554 	(dst).shm_cpid = (src).shm_cpid; \
1555 	(dst).shm_atime = (src).shm_atime; \
1556 	(dst).shm_dtime = (src).shm_dtime; \
1557 	(dst).shm_ctime = (src).shm_ctime; \
1558 	(dst).shm_nattch = (src).shm_nattch; \
1559 } while (/*CONSTCOND*/ 0)
1560 
1561 static int
1562 sysctl_kern_sysvipc(SYSCTLFN_ARGS)
1563 {
1564 	void *where = oldp;
1565 	size_t *sizep = oldlenp;
1566 #ifdef SYSVMSG
1567 	struct msg_sysctl_info *msgsi = NULL;
1568 #endif
1569 #ifdef SYSVSEM
1570 	struct sem_sysctl_info *semsi = NULL;
1571 #endif
1572 #ifdef SYSVSHM
1573 	struct shm_sysctl_info *shmsi = NULL;
1574 #endif
1575 	size_t infosize, dssize, tsize, buflen;
1576 	void *bf = NULL;
1577 	char *start;
1578 	int32_t nds;
1579 	int i, error, ret;
1580 
1581 	if (namelen != 1)
1582 		return (EINVAL);
1583 
1584 	start = where;
1585 	buflen = *sizep;
1586 
1587 	switch (*name) {
1588 	case KERN_SYSVIPC_MSG_INFO:
1589 #ifdef SYSVMSG
1590 		infosize = sizeof(msgsi->msginfo);
1591 		nds = msginfo.msgmni;
1592 		dssize = sizeof(msgsi->msgids[0]);
1593 		break;
1594 #else
1595 		return (EINVAL);
1596 #endif
1597 	case KERN_SYSVIPC_SEM_INFO:
1598 #ifdef SYSVSEM
1599 		infosize = sizeof(semsi->seminfo);
1600 		nds = seminfo.semmni;
1601 		dssize = sizeof(semsi->semids[0]);
1602 		break;
1603 #else
1604 		return (EINVAL);
1605 #endif
1606 	case KERN_SYSVIPC_SHM_INFO:
1607 #ifdef SYSVSHM
1608 		infosize = sizeof(shmsi->shminfo);
1609 		nds = shminfo.shmmni;
1610 		dssize = sizeof(shmsi->shmids[0]);
1611 		break;
1612 #else
1613 		return (EINVAL);
1614 #endif
1615 	default:
1616 		return (EINVAL);
1617 	}
1618 	/*
1619 	 * Round infosize to 64 bit boundary if requesting more than just
1620 	 * the info structure or getting the total data size.
1621 	 */
1622 	if (where == NULL || *sizep > infosize)
1623 		infosize = ((infosize + 7) / 8) * 8;
1624 	tsize = infosize + nds * dssize;
1625 
1626 	/* Return just the total size required. */
1627 	if (where == NULL) {
1628 		*sizep = tsize;
1629 		return (0);
1630 	}
1631 
1632 	/* Not enough room for even the info struct. */
1633 	if (buflen < infosize) {
1634 		*sizep = 0;
1635 		return (ENOMEM);
1636 	}
1637 	bf = malloc(min(tsize, buflen), M_TEMP, M_WAITOK);
1638 	memset(bf, 0, min(tsize, buflen));
1639 
1640 	switch (*name) {
1641 #ifdef SYSVMSG
1642 	case KERN_SYSVIPC_MSG_INFO:
1643 		msgsi = (struct msg_sysctl_info *)bf;
1644 		msgsi->msginfo = msginfo;
1645 		break;
1646 #endif
1647 #ifdef SYSVSEM
1648 	case KERN_SYSVIPC_SEM_INFO:
1649 		semsi = (struct sem_sysctl_info *)bf;
1650 		semsi->seminfo = seminfo;
1651 		break;
1652 #endif
1653 #ifdef SYSVSHM
1654 	case KERN_SYSVIPC_SHM_INFO:
1655 		shmsi = (struct shm_sysctl_info *)bf;
1656 		shmsi->shminfo = shminfo;
1657 		break;
1658 #endif
1659 	}
1660 	buflen -= infosize;
1661 
1662 	ret = 0;
1663 	if (buflen > 0) {
1664 		/* Fill in the IPC data structures.  */
1665 		for (i = 0; i < nds; i++) {
1666 			if (buflen < dssize) {
1667 				ret = ENOMEM;
1668 				break;
1669 			}
1670 			switch (*name) {
1671 #ifdef SYSVMSG
1672 			case KERN_SYSVIPC_MSG_INFO:
1673 				FILL_MSG(msqids[i], msgsi->msgids[i]);
1674 				break;
1675 #endif
1676 #ifdef SYSVSEM
1677 			case KERN_SYSVIPC_SEM_INFO:
1678 				FILL_SEM(sema[i], semsi->semids[i]);
1679 				break;
1680 #endif
1681 #ifdef SYSVSHM
1682 			case KERN_SYSVIPC_SHM_INFO:
1683 				FILL_SHM(shmsegs[i], shmsi->shmids[i]);
1684 				break;
1685 #endif
1686 			}
1687 			buflen -= dssize;
1688 		}
1689 	}
1690 	*sizep -= buflen;
1691 	error = copyout(bf, start, *sizep);
1692 	/* If copyout succeeded, use return code set earlier. */
1693 	if (error == 0)
1694 		error = ret;
1695 	if (bf)
1696 		free(bf, M_TEMP);
1697 	return (error);
1698 }
1699 
1700 #undef FILL_PERM
1701 #undef FILL_MSG
1702 #undef FILL_SEM
1703 #undef FILL_SHM
1704 
1705 #endif /* defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM) */
1706 
1707 #if NPTY > 0
1708 /*
1709  * sysctl helper routine for kern.maxptys.  ensures that any new value
1710  * is acceptable to the pty subsystem.
1711  */
1712 static int
1713 sysctl_kern_maxptys(SYSCTLFN_ARGS)
1714 {
1715 	int pty_maxptys(int, int);		/* defined in kern/tty_pty.c */
1716 	int error, xmax;
1717 	struct sysctlnode node;
1718 
1719 	/* get current value of maxptys */
1720 	xmax = pty_maxptys(0, 0);
1721 
1722 	node = *rnode;
1723 	node.sysctl_data = &xmax;
1724 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1725 	if (error || newp == NULL)
1726 		return (error);
1727 
1728 	if (xmax != pty_maxptys(xmax, 1))
1729 		return (EINVAL);
1730 
1731 	return (0);
1732 }
1733 #endif /* NPTY > 0 */
1734 
1735 /*
1736  * sysctl helper routine for kern.sbmax.  basically just ensures that
1737  * any new value is not too small.
1738  */
1739 static int
1740 sysctl_kern_sbmax(SYSCTLFN_ARGS)
1741 {
1742 	int error, new_sbmax;
1743 	struct sysctlnode node;
1744 
1745 	new_sbmax = sb_max;
1746 	node = *rnode;
1747 	node.sysctl_data = &new_sbmax;
1748 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1749 	if (error || newp == NULL)
1750 		return (error);
1751 
1752 	error = sb_max_set(new_sbmax);
1753 
1754 	return (error);
1755 }
1756 
1757 /*
1758  * sysctl helper routine for kern.urandom node.  picks a random number
1759  * for you.
1760  */
1761 static int
1762 sysctl_kern_urnd(SYSCTLFN_ARGS)
1763 {
1764 #if NRND > 0
1765 	int v;
1766 
1767 	if (rnd_extract_data(&v, sizeof(v), RND_EXTRACT_ANY) == sizeof(v)) {
1768 		struct sysctlnode node = *rnode;
1769 		node.sysctl_data = &v;
1770 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1771 	}
1772 	else
1773 		return (EIO);	/*XXX*/
1774 #else
1775 	return (EOPNOTSUPP);
1776 #endif
1777 }
1778 
1779 /*
1780  * sysctl helper routine to do kern.lwp.* work.
1781  */
1782 static int
1783 sysctl_kern_lwp(SYSCTLFN_ARGS)
1784 {
1785 	struct kinfo_lwp klwp;
1786 	struct proc *p;
1787 	struct lwp *l2;
1788 	char *where, *dp;
1789 	int pid, elem_size, elem_count;
1790 	int buflen, needed, error;
1791 
1792 	if (namelen == 1 && name[0] == CTL_QUERY)
1793 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
1794 
1795 	dp = where = oldp;
1796 	buflen = where != NULL ? *oldlenp : 0;
1797 	error = needed = 0;
1798 
1799 	if (newp != NULL || namelen != 3)
1800 		return (EINVAL);
1801 	pid = name[0];
1802 	elem_size = name[1];
1803 	elem_count = name[2];
1804 
1805 	p = pfind(pid);
1806 	if (p == NULL)
1807 		return (ESRCH);
1808 	LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
1809 		if (buflen >= elem_size && elem_count > 0) {
1810 			fill_lwp(l2, &klwp);
1811 			/*
1812 			 * Copy out elem_size, but not larger than
1813 			 * the size of a struct kinfo_proc2.
1814 			 */
1815 			error = copyout(&klwp, dp,
1816 			    min(sizeof(klwp), elem_size));
1817 			if (error)
1818 				goto cleanup;
1819 			dp += elem_size;
1820 			buflen -= elem_size;
1821 			elem_count--;
1822 		}
1823 		needed += elem_size;
1824 	}
1825 
1826 	if (where != NULL) {
1827 		*oldlenp = dp - where;
1828 		if (needed > *oldlenp)
1829 			return (ENOMEM);
1830 	} else {
1831 		needed += KERN_LWPSLOP;
1832 		*oldlenp = needed;
1833 	}
1834 	return (0);
1835  cleanup:
1836 	return (error);
1837 }
1838 
1839 /*
1840  * sysctl helper routine for kern.forkfsleep node.  ensures that the
1841  * given value is not too large or two small, and is at least one
1842  * timer tick if not zero.
1843  */
1844 static int
1845 sysctl_kern_forkfsleep(SYSCTLFN_ARGS)
1846 {
1847 	/* userland sees value in ms, internally is in ticks */
1848 	extern int forkfsleep;		/* defined in kern/kern_fork.c */
1849 	int error, timo, lsleep;
1850 	struct sysctlnode node;
1851 
1852 	lsleep = forkfsleep * 1000 / hz;
1853 	node = *rnode;
1854 	node.sysctl_data = &lsleep;
1855 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1856 	if (error || newp == NULL)
1857 		return (error);
1858 
1859 	/* refuse negative values, and overly 'long time' */
1860 	if (lsleep < 0 || lsleep > MAXSLP * 1000)
1861 		return (EINVAL);
1862 
1863 	timo = mstohz(lsleep);
1864 
1865 	/* if the interval is >0 ms && <1 tick, use 1 tick */
1866 	if (lsleep != 0 && timo == 0)
1867 		forkfsleep = 1;
1868 	else
1869 		forkfsleep = timo;
1870 
1871 	return (0);
1872 }
1873 
1874 /*
1875  * sysctl helper routine for kern.root_partition
1876  */
1877 static int
1878 sysctl_kern_root_partition(SYSCTLFN_ARGS)
1879 {
1880 	int rootpart = DISKPART(rootdev);
1881 	struct sysctlnode node = *rnode;
1882 
1883 	node.sysctl_data = &rootpart;
1884 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1885 }
1886 
1887 /*
1888  * sysctl helper function for kern.drivers
1889  */
1890 static int
1891 sysctl_kern_drivers(SYSCTLFN_ARGS)
1892 {
1893 	int error;
1894 	size_t buflen;
1895 	struct kinfo_drivers kd;
1896 	char *start, *where;
1897 	const char *dname;
1898 	int i;
1899 	extern struct devsw_conv *devsw_conv;
1900 	extern int max_devsw_convs;
1901 
1902 	if (newp != NULL || namelen != 0)
1903 		return (EINVAL);
1904 
1905 	start = where = oldp;
1906 	buflen = *oldlenp;
1907 	if (where == NULL) {
1908 		*oldlenp = max_devsw_convs * sizeof kd;
1909 		return 0;
1910 	}
1911 
1912 	/*
1913 	 * An array of kinfo_drivers structures
1914 	 */
1915 	error = 0;
1916 	for (i = 0; i < max_devsw_convs; i++) {
1917 		dname = devsw_conv[i].d_name;
1918 		if (dname == NULL)
1919 			continue;
1920 		if (buflen < sizeof kd) {
1921 			error = ENOMEM;
1922 			break;
1923 		}
1924 		memset(&kd, 0, sizeof(kd));
1925 		kd.d_bmajor = devsw_conv[i].d_bmajor;
1926 		kd.d_cmajor = devsw_conv[i].d_cmajor;
1927 		strlcpy(kd.d_name, dname, sizeof kd.d_name);
1928 		error = copyout(&kd, where, sizeof kd);
1929 		if (error != 0)
1930 			break;
1931 		buflen -= sizeof kd;
1932 		where += sizeof kd;
1933 	}
1934 	*oldlenp = where - start;
1935 	return error;
1936 }
1937 
1938 /*
1939  * sysctl helper function for kern.file2
1940  */
1941 static int
1942 sysctl_kern_file2(SYSCTLFN_ARGS)
1943 {
1944 	struct proc *p;
1945 	struct file *fp;
1946 	struct filedesc *fd;
1947 	struct kinfo_file kf;
1948 	char *dp;
1949 	u_int i, op;
1950 	size_t len, needed, elem_size, out_size;
1951 	int error, arg, elem_count;
1952 
1953 	if (namelen == 1 && name[0] == CTL_QUERY)
1954 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
1955 
1956 	if (namelen != 4)
1957 		return (EINVAL);
1958 
1959 	error = 0;
1960 	dp = oldp;
1961 	len = (oldp != NULL) ? *oldlenp : 0;
1962 	op = name[0];
1963 	arg = name[1];
1964 	elem_size = name[2];
1965 	elem_count = name[3];
1966 	out_size = MIN(sizeof(kf), elem_size);
1967 	needed = 0;
1968 
1969 	if (elem_size < 1 || elem_count < 0)
1970 		return (EINVAL);
1971 
1972 	switch (op) {
1973 	case KERN_FILE_BYFILE:
1974 		/*
1975 		 * doesn't use arg so it must be zero
1976 		 */
1977 		if (arg != 0)
1978 			return (EINVAL);
1979 		LIST_FOREACH(fp, &filehead, f_list) {
1980 			if (CURTAIN(l->l_proc->p_ucred->cr_uid,
1981 				    fp->f_cred->cr_uid))
1982 				continue;
1983 			if (len >= elem_size && elem_count > 0) {
1984 				fill_file(&kf, fp, NULL, 0);
1985 				error = copyout(&kf, dp, out_size);
1986 				if (error)
1987 					break;
1988 				dp += elem_size;
1989 				len -= elem_size;
1990 			}
1991 			if (elem_count > 0) {
1992 				needed += elem_size;
1993 				if (elem_count != INT_MAX)
1994 					elem_count--;
1995 			}
1996 		}
1997 		break;
1998 	    case KERN_FILE_BYPID:
1999 		if (arg < -1)
2000 			/* -1 means all processes */
2001 			return (EINVAL);
2002 		proclist_lock_read();
2003 		PROCLIST_FOREACH(p, &allproc) {
2004 			if (p->p_stat == SIDL)
2005 				/* skip embryonic processes */
2006 				continue;
2007 			if (CURTAIN(l->l_proc->p_ucred->cr_uid,
2008 				    p->p_ucred->cr_uid))
2009 				continue;
2010 			if (arg > 0 && p->p_pid != arg)
2011 				/* pick only the one we want */
2012 				/* XXX want 0 to mean "kernel files" */
2013 				continue;
2014 			fd = p->p_fd;
2015 			for (i = 0; i < fd->fd_nfiles; i++) {
2016 				fp = fd->fd_ofiles[i];
2017 				if (fp == NULL || !FILE_IS_USABLE(fp))
2018 					continue;
2019 				if (len >= elem_size && elem_count > 0) {
2020 					fill_file(&kf, fd->fd_ofiles[i],
2021 						  p, i);
2022 					error = copyout(&kf, dp, out_size);
2023 					if (error)
2024 						break;
2025 					dp += elem_size;
2026 					len -= elem_size;
2027 				}
2028 				if (elem_count > 0) {
2029 					needed += elem_size;
2030 					if (elem_count != INT_MAX)
2031 						elem_count--;
2032 				}
2033 			}
2034 		}
2035 		proclist_unlock_read();
2036 		break;
2037 	default:
2038 		return (EINVAL);
2039 	}
2040 
2041 	if (oldp == NULL)
2042 		needed += KERN_FILESLOP * elem_size;
2043 	*oldlenp = needed;
2044 
2045 	return (error);
2046 }
2047 
2048 static void
2049 fill_file(struct kinfo_file *kp, const struct file *fp, struct proc *p, int i)
2050 {
2051 
2052 	memset(kp, 0, sizeof(*kp));
2053 
2054 	kp->ki_fileaddr =	PTRTOUINT64(fp);
2055 	kp->ki_flag =		fp->f_flag;
2056 	kp->ki_iflags =		fp->f_iflags;
2057 	kp->ki_ftype =		fp->f_type;
2058 	kp->ki_count =		fp->f_count;
2059 	kp->ki_msgcount =	fp->f_msgcount;
2060 	kp->ki_usecount =	fp->f_usecount;
2061 	kp->ki_fucred =		PTRTOUINT64(fp->f_cred);
2062 	kp->ki_fuid =		fp->f_cred->cr_uid;
2063 	kp->ki_fgid =		fp->f_cred->cr_gid;
2064 	kp->ki_fops =		PTRTOUINT64(fp->f_ops);
2065 	kp->ki_foffset =	fp->f_offset;
2066 	kp->ki_fdata =		PTRTOUINT64(fp->f_data);
2067 
2068 	/* vnode information to glue this file to something */
2069 	if (fp->f_type == DTYPE_VNODE) {
2070 		struct vnode *vp = (struct vnode *)fp->f_data;
2071 
2072 		kp->ki_vun =	PTRTOUINT64(vp->v_un.vu_socket);
2073 		kp->ki_vsize =	vp->v_size;
2074 		kp->ki_vtype =	vp->v_type;
2075 		kp->ki_vtag =	vp->v_tag;
2076 		kp->ki_vdata =	PTRTOUINT64(vp->v_data);
2077 	}
2078 
2079         /* process information when retrieved via KERN_FILE_BYPID */
2080 	if (p) {
2081 		kp->ki_pid =		p->p_pid;
2082 		kp->ki_fd =		i;
2083 		kp->ki_ofileflags =	p->p_fd->fd_ofileflags[i];
2084 	}
2085 }
2086 
2087 static int
2088 sysctl_doeproc(SYSCTLFN_ARGS)
2089 {
2090 	struct eproc eproc;
2091 	struct kinfo_proc2 kproc2;
2092 	struct kinfo_proc *dp;
2093 	struct proc *p;
2094 	const struct proclist_desc *pd;
2095 	char *where, *dp2;
2096 	int type, op, arg;
2097 	u_int elem_size, elem_count;
2098 	size_t buflen, needed;
2099 	int error;
2100 
2101 	if (namelen == 1 && name[0] == CTL_QUERY)
2102 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
2103 
2104 	dp = oldp;
2105 	dp2 = where = oldp;
2106 	buflen = where != NULL ? *oldlenp : 0;
2107 	error = 0;
2108 	needed = 0;
2109 	type = rnode->sysctl_num;
2110 
2111 	if (type == KERN_PROC) {
2112 		if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
2113 			return (EINVAL);
2114 		op = name[0];
2115 		if (op != KERN_PROC_ALL)
2116 			arg = name[1];
2117 		else
2118 			arg = 0;		/* Quell compiler warning */
2119 		elem_size = elem_count = 0;	/* Ditto */
2120 	} else {
2121 		if (namelen != 4)
2122 			return (EINVAL);
2123 		op = name[0];
2124 		arg = name[1];
2125 		elem_size = name[2];
2126 		elem_count = name[3];
2127 	}
2128 
2129 	proclist_lock_read();
2130 
2131 	pd = proclists;
2132 again:
2133 	PROCLIST_FOREACH(p, pd->pd_list) {
2134 		/*
2135 		 * Skip embryonic processes.
2136 		 */
2137 		if (p->p_stat == SIDL)
2138 			continue;
2139 
2140 		if (CURTAIN(l->l_proc->p_ucred->cr_uid, p->p_ucred->cr_uid))
2141 			continue;
2142 
2143 		/*
2144 		 * TODO - make more efficient (see notes below).
2145 		 * do by session.
2146 		 */
2147 		switch (op) {
2148 
2149 		case KERN_PROC_PID:
2150 			/* could do this with just a lookup */
2151 			if (p->p_pid != (pid_t)arg)
2152 				continue;
2153 			break;
2154 
2155 		case KERN_PROC_PGRP:
2156 			/* could do this by traversing pgrp */
2157 			if (p->p_pgrp->pg_id != (pid_t)arg)
2158 				continue;
2159 			break;
2160 
2161 		case KERN_PROC_SESSION:
2162 			if (p->p_session->s_sid != (pid_t)arg)
2163 				continue;
2164 			break;
2165 
2166 		case KERN_PROC_TTY:
2167 			if (arg == (int) KERN_PROC_TTY_REVOKE) {
2168 				if ((p->p_flag & P_CONTROLT) == 0 ||
2169 				    p->p_session->s_ttyp == NULL ||
2170 				    p->p_session->s_ttyvp != NULL)
2171 					continue;
2172 			} else if ((p->p_flag & P_CONTROLT) == 0 ||
2173 			    p->p_session->s_ttyp == NULL) {
2174 				if ((dev_t)arg != KERN_PROC_TTY_NODEV)
2175 					continue;
2176 			} else if (p->p_session->s_ttyp->t_dev != (dev_t)arg)
2177 				continue;
2178 			break;
2179 
2180 		case KERN_PROC_UID:
2181 			if (p->p_ucred->cr_uid != (uid_t)arg)
2182 				continue;
2183 			break;
2184 
2185 		case KERN_PROC_RUID:
2186 			if (p->p_cred->p_ruid != (uid_t)arg)
2187 				continue;
2188 			break;
2189 
2190 		case KERN_PROC_GID:
2191 			if (p->p_ucred->cr_gid != (uid_t)arg)
2192 				continue;
2193 			break;
2194 
2195 		case KERN_PROC_RGID:
2196 			if (p->p_cred->p_rgid != (uid_t)arg)
2197 				continue;
2198 			break;
2199 
2200 		case KERN_PROC_ALL:
2201 			/* allow everything */
2202 			break;
2203 
2204 		default:
2205 			error = EINVAL;
2206 			goto cleanup;
2207 		}
2208 		if (type == KERN_PROC) {
2209 			if (buflen >= sizeof(struct kinfo_proc)) {
2210 				fill_eproc(p, &eproc);
2211 				error = copyout(p, &dp->kp_proc,
2212 				    sizeof(struct proc));
2213 				if (error)
2214 					goto cleanup;
2215 				error = copyout(&eproc, &dp->kp_eproc,
2216 				    sizeof(eproc));
2217 				if (error)
2218 					goto cleanup;
2219 				dp++;
2220 				buflen -= sizeof(struct kinfo_proc);
2221 			}
2222 			needed += sizeof(struct kinfo_proc);
2223 		} else { /* KERN_PROC2 */
2224 			if (buflen >= elem_size && elem_count > 0) {
2225 				fill_kproc2(p, &kproc2);
2226 				/*
2227 				 * Copy out elem_size, but not larger than
2228 				 * the size of a struct kinfo_proc2.
2229 				 */
2230 				error = copyout(&kproc2, dp2,
2231 				    min(sizeof(kproc2), elem_size));
2232 				if (error)
2233 					goto cleanup;
2234 				dp2 += elem_size;
2235 				buflen -= elem_size;
2236 				elem_count--;
2237 			}
2238 			needed += elem_size;
2239 		}
2240 	}
2241 	pd++;
2242 	if (pd->pd_list != NULL)
2243 		goto again;
2244 	proclist_unlock_read();
2245 
2246 	if (where != NULL) {
2247 		if (type == KERN_PROC)
2248 			*oldlenp = (char *)dp - where;
2249 		else
2250 			*oldlenp = dp2 - where;
2251 		if (needed > *oldlenp)
2252 			return (ENOMEM);
2253 	} else {
2254 		needed += KERN_PROCSLOP;
2255 		*oldlenp = needed;
2256 	}
2257 	return (0);
2258  cleanup:
2259 	proclist_unlock_read();
2260 	return (error);
2261 }
2262 
2263 /*
2264  * sysctl helper routine for kern.proc_args pseudo-subtree.
2265  */
2266 static int
2267 sysctl_kern_proc_args(SYSCTLFN_ARGS)
2268 {
2269 	struct ps_strings pss;
2270 	struct proc *p, *up = l->l_proc;
2271 	size_t len, upper_bound, xlen, i;
2272 	struct uio auio;
2273 	struct iovec aiov;
2274 	vaddr_t argv;
2275 	pid_t pid;
2276 	int nargv, type, error;
2277 	char *arg;
2278 	char *tmp;
2279 
2280 	if (namelen == 1 && name[0] == CTL_QUERY)
2281 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
2282 
2283 	if (newp != NULL || namelen != 2)
2284 		return (EINVAL);
2285 	pid = name[0];
2286 	type = name[1];
2287 
2288 	switch (type) {
2289 	case KERN_PROC_ARGV:
2290 	case KERN_PROC_NARGV:
2291 	case KERN_PROC_ENV:
2292 	case KERN_PROC_NENV:
2293 		/* ok */
2294 		break;
2295 	default:
2296 		return (EINVAL);
2297 	}
2298 
2299 	/* check pid */
2300 	if ((p = pfind(pid)) == NULL)
2301 		return (EINVAL);
2302 
2303 	if (CURTAIN(l->l_proc->p_ucred->cr_uid, p->p_ucred->cr_uid))
2304 		return (EPERM);
2305 
2306 	/* only root or same user change look at the environment */
2307 	if (type == KERN_PROC_ENV || type == KERN_PROC_NENV) {
2308 		if (up->p_ucred->cr_uid != 0) {
2309 			if (up->p_cred->p_ruid != p->p_cred->p_ruid ||
2310 			    up->p_cred->p_ruid != p->p_cred->p_svuid)
2311 				return (EPERM);
2312 		}
2313 	}
2314 
2315 	if (oldp == NULL) {
2316 		if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
2317 			*oldlenp = sizeof (int);
2318 		else
2319 			*oldlenp = ARG_MAX;	/* XXX XXX XXX */
2320 		return (0);
2321 	}
2322 
2323 	/*
2324 	 * Zombies don't have a stack, so we can't read their psstrings.
2325 	 * System processes also don't have a user stack.
2326 	 */
2327 	if (P_ZOMBIE(p) || (p->p_flag & P_SYSTEM) != 0)
2328 		return (EINVAL);
2329 
2330 	/*
2331 	 * Lock the process down in memory.
2332 	 */
2333 	/* XXXCDC: how should locking work here? */
2334 	if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
2335 		return (EFAULT);
2336 
2337 	p->p_vmspace->vm_refcnt++;	/* XXX */
2338 
2339 	/*
2340 	 * Allocate a temporary buffer to hold the arguments.
2341 	 */
2342 	arg = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
2343 
2344 	/*
2345 	 * Read in the ps_strings structure.
2346 	 */
2347 	aiov.iov_base = &pss;
2348 	aiov.iov_len = sizeof(pss);
2349 	auio.uio_iov = &aiov;
2350 	auio.uio_iovcnt = 1;
2351 	auio.uio_offset = (vaddr_t)p->p_psstr;
2352 	auio.uio_resid = sizeof(pss);
2353 	auio.uio_segflg = UIO_SYSSPACE;
2354 	auio.uio_rw = UIO_READ;
2355 	auio.uio_procp = NULL;
2356 	error = uvm_io(&p->p_vmspace->vm_map, &auio);
2357 	if (error)
2358 		goto done;
2359 
2360 	if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV)
2361 		memcpy(&nargv, (char *)&pss + p->p_psnargv, sizeof(nargv));
2362 	else
2363 		memcpy(&nargv, (char *)&pss + p->p_psnenv, sizeof(nargv));
2364 	if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
2365 		error = copyout(&nargv, oldp, sizeof(nargv));
2366 		*oldlenp = sizeof(nargv);
2367 		goto done;
2368 	}
2369 	/*
2370 	 * Now read the address of the argument vector.
2371 	 */
2372 	switch (type) {
2373 	case KERN_PROC_ARGV:
2374 		/* XXX compat32 stuff here */
2375 		memcpy(&tmp, (char *)&pss + p->p_psargv, sizeof(tmp));
2376 		break;
2377 	case KERN_PROC_ENV:
2378 		memcpy(&tmp, (char *)&pss + p->p_psenv, sizeof(tmp));
2379 		break;
2380 	default:
2381 		return (EINVAL);
2382 	}
2383 	auio.uio_offset = (off_t)(unsigned long)tmp;
2384 	aiov.iov_base = &argv;
2385 	aiov.iov_len = sizeof(argv);
2386 	auio.uio_iov = &aiov;
2387 	auio.uio_iovcnt = 1;
2388 	auio.uio_resid = sizeof(argv);
2389 	auio.uio_segflg = UIO_SYSSPACE;
2390 	auio.uio_rw = UIO_READ;
2391 	auio.uio_procp = NULL;
2392 	error = uvm_io(&p->p_vmspace->vm_map, &auio);
2393 	if (error)
2394 		goto done;
2395 
2396 	/*
2397 	 * Now copy in the actual argument vector, one page at a time,
2398 	 * since we don't know how long the vector is (though, we do
2399 	 * know how many NUL-terminated strings are in the vector).
2400 	 */
2401 	len = 0;
2402 	upper_bound = *oldlenp;
2403 	for (; nargv != 0 && len < upper_bound; len += xlen) {
2404 		aiov.iov_base = arg;
2405 		aiov.iov_len = PAGE_SIZE;
2406 		auio.uio_iov = &aiov;
2407 		auio.uio_iovcnt = 1;
2408 		auio.uio_offset = argv + len;
2409 		xlen = PAGE_SIZE - ((argv + len) & PAGE_MASK);
2410 		auio.uio_resid = xlen;
2411 		auio.uio_segflg = UIO_SYSSPACE;
2412 		auio.uio_rw = UIO_READ;
2413 		auio.uio_procp = NULL;
2414 		error = uvm_io(&p->p_vmspace->vm_map, &auio);
2415 		if (error)
2416 			goto done;
2417 
2418 		for (i = 0; i < xlen && nargv != 0; i++) {
2419 			if (arg[i] == '\0')
2420 				nargv--;	/* one full string */
2421 		}
2422 
2423 		/*
2424 		 * Make sure we don't copyout past the end of the user's
2425 		 * buffer.
2426 		 */
2427 		if (len + i > upper_bound)
2428 			i = upper_bound - len;
2429 
2430 		error = copyout(arg, (char *)oldp + len, i);
2431 		if (error)
2432 			break;
2433 
2434 		if (nargv == 0) {
2435 			len += i;
2436 			break;
2437 		}
2438 	}
2439 	*oldlenp = len;
2440 
2441 done:
2442 	uvmspace_free(p->p_vmspace);
2443 
2444 	free(arg, M_TEMP);
2445 	return (error);
2446 }
2447 
2448 /*
2449  * Sysctl helper routine for Verified Exec.
2450  */
2451 #ifdef VERIFIED_EXEC
2452 static int
2453 sysctl_kern_veriexec(SYSCTLFN_ARGS)
2454 {
2455 	int newval, error;
2456 	int *var = NULL, raise_only = 0;
2457 	struct sysctlnode node;
2458 
2459 	node = *rnode;
2460 
2461 	switch (rnode->sysctl_num) {
2462 	case VERIEXEC_STRICT:
2463 		raise_only = 1;
2464 		var = &veriexec_strict;
2465 		break;
2466 	case VERIEXEC_ALGORITHMS:
2467 		node.sysctl_data = veriexec_fp_names;
2468 		node.sysctl_size = strlen(veriexec_fp_names) + 1;
2469 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
2470 	default:
2471 		return (EINVAL);
2472 	}
2473 
2474 	newval = *var;
2475 
2476 	node.sysctl_data = &newval;
2477 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
2478 	if (error || newp == NULL) {
2479 		return (error);
2480 	}
2481 
2482 	if (raise_only && (newval < *var))
2483 		return (EPERM);
2484 
2485 	*var = newval;
2486 
2487 	return (error);
2488 }
2489 #endif /* VERIFIED_EXEC */
2490 
2491 /*
2492  * sysctl helper routine for kern.cp_id node.  maps cpus to their
2493  * cpuids.
2494  */
2495 static int
2496 sysctl_kern_cpid(SYSCTLFN_ARGS)
2497 {
2498 	struct sysctlnode node = *rnode;
2499 
2500 #ifndef MULTIPROCESSOR
2501 	u_int64_t id;
2502 
2503 	if (namelen == 1) {
2504 		if (name[0] != 0)
2505 			return (ENOENT);
2506 		/*
2507 		 * you're allowed to ask for the zero'th processor
2508 		 */
2509 		name++;
2510 		namelen--;
2511 	}
2512 	node.sysctl_data = &id;
2513 	node.sysctl_size = sizeof(id);
2514 	id = cpu_number();
2515 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
2516 
2517 #else /* MULTIPROCESSOR */
2518 	u_int64_t *cp_id = NULL;
2519 	int error, n = sysctl_ncpus();
2520 	struct cpu_info *ci;
2521 	CPU_INFO_ITERATOR cii;
2522 
2523 	/*
2524 	 * here you may either retrieve a single cpu id or the whole
2525 	 * set.  the size you get back when probing depends on what
2526 	 * you ask for.
2527 	 */
2528 	switch (namelen) {
2529 	case 0:
2530 		node.sysctl_size = n * sizeof(u_int64_t);
2531 		n = -2; /* ALL */
2532 		break;
2533 	case 1:
2534 		if (name[0] < 0 || name[0] >= n)
2535 			return (ENOENT); /* ENOSUCHPROCESSOR */
2536 		node.sysctl_size = sizeof(u_int64_t);
2537 		n = name[0];
2538 		/*
2539 		 * adjust these so that sysctl_lookup() will be happy
2540 		 */
2541 		name++;
2542 		namelen--;
2543 		break;
2544 	default:
2545 		return (EINVAL);
2546 	}
2547 
2548 	cp_id = malloc(node.sysctl_size, M_TEMP, M_WAITOK|M_CANFAIL);
2549 	if (cp_id == NULL)
2550 		return (ENOMEM);
2551 	node.sysctl_data = cp_id;
2552 	memset(cp_id, 0, node.sysctl_size);
2553 
2554 	for (CPU_INFO_FOREACH(cii, ci)) {
2555 		if (n <= 0)
2556 			cp_id[0] = ci->ci_cpuid;
2557 		/*
2558 		 * if a specific processor was requested and we just
2559 		 * did it, we're done here
2560 		 */
2561 		if (n == 0)
2562 			break;
2563 		/*
2564 		 * if doing "all", skip to next cp_id slot for next processor
2565 		 */
2566 		if (n == -2)
2567 			cp_id++;
2568 		/*
2569 		 * if we're doing a specific processor, we're one
2570 		 * processor closer
2571 		 */
2572 		if (n > 0)
2573 			n--;
2574 	}
2575 
2576 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
2577 	free(node.sysctl_data, M_TEMP);
2578 	return (error);
2579 
2580 #endif /* MULTIPROCESSOR */
2581 }
2582 
2583 /*
2584  * sysctl helper routine for hw.usermem and hw.usermem64.  values are
2585  * calculate on the fly taking into account integer overflow and the
2586  * current wired count.
2587  */
2588 static int
2589 sysctl_hw_usermem(SYSCTLFN_ARGS)
2590 {
2591 	u_int ui;
2592 	u_quad_t uq;
2593 	struct sysctlnode node;
2594 
2595 	node = *rnode;
2596 	switch (rnode->sysctl_num) {
2597 	    case HW_USERMEM:
2598 		if ((ui = physmem - uvmexp.wired) > (UINT_MAX / PAGE_SIZE))
2599 			ui = UINT_MAX;
2600 		else
2601 			ui *= PAGE_SIZE;
2602 		node.sysctl_data = &ui;
2603 		break;
2604 	case HW_USERMEM64:
2605 		uq = (u_quad_t)(physmem - uvmexp.wired) * PAGE_SIZE;
2606 		node.sysctl_data = &uq;
2607 		break;
2608 	default:
2609 		return (EINVAL);
2610 	}
2611 
2612 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
2613 }
2614 
2615 /*
2616  * sysctl helper routine for kern.cnmagic node.  pulls the old value
2617  * out, encoded, and stuffs the new value in for decoding.
2618  */
2619 static int
2620 sysctl_hw_cnmagic(SYSCTLFN_ARGS)
2621 {
2622 	char magic[CNS_LEN];
2623 	int error;
2624 	struct sysctlnode node;
2625 
2626 	if (oldp)
2627 		cn_get_magic(magic, CNS_LEN);
2628 	node = *rnode;
2629 	node.sysctl_data = &magic[0];
2630 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
2631 	if (error || newp == NULL)
2632 		return (error);
2633 
2634 	return (cn_set_magic(magic));
2635 }
2636 
2637 static int
2638 sysctl_hw_ncpu(SYSCTLFN_ARGS)
2639 {
2640 	int ncpu;
2641 	struct sysctlnode node;
2642 
2643 	ncpu = sysctl_ncpus();
2644 	node = *rnode;
2645 	node.sysctl_data = &ncpu;
2646 
2647 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
2648 }
2649 
2650 
2651 /*
2652  * ********************************************************************
2653  * section 3: public helper routines that are used for more than one
2654  * node
2655  * ********************************************************************
2656  */
2657 
2658 /*
2659  * sysctl helper routine for the kern.root_device node and some ports'
2660  * machdep.root_device nodes.
2661  */
2662 int
2663 sysctl_root_device(SYSCTLFN_ARGS)
2664 {
2665 	struct sysctlnode node;
2666 
2667 	node = *rnode;
2668 	node.sysctl_data = root_device->dv_xname;
2669 	node.sysctl_size = strlen(root_device->dv_xname) + 1;
2670 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
2671 }
2672 
2673 /*
2674  * sysctl helper routine for kern.consdev, dependent on the current
2675  * state of the console.  also used for machdep.console_device on some
2676  * ports.
2677  */
2678 int
2679 sysctl_consdev(SYSCTLFN_ARGS)
2680 {
2681 	dev_t consdev;
2682 	struct sysctlnode node;
2683 
2684 	if (cn_tab != NULL)
2685 		consdev = cn_tab->cn_dev;
2686 	else
2687 		consdev = NODEV;
2688 	node = *rnode;
2689 	node.sysctl_data = &consdev;
2690 	node.sysctl_size = sizeof(consdev);
2691 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
2692 }
2693 
2694 /*
2695  * ********************************************************************
2696  * section 4: support for some helpers
2697  * ********************************************************************
2698  */
2699 
2700 /*
2701  * Fill in a kinfo_proc2 structure for the specified process.
2702  */
2703 static void
2704 fill_kproc2(struct proc *p, struct kinfo_proc2 *ki)
2705 {
2706 	struct tty *tp;
2707 	struct lwp *l;
2708 	struct timeval ut, st;
2709 
2710 	memset(ki, 0, sizeof(*ki));
2711 
2712 	ki->p_paddr = PTRTOUINT64(p);
2713 	ki->p_fd = PTRTOUINT64(p->p_fd);
2714 	ki->p_cwdi = PTRTOUINT64(p->p_cwdi);
2715 	ki->p_stats = PTRTOUINT64(p->p_stats);
2716 	ki->p_limit = PTRTOUINT64(p->p_limit);
2717 	ki->p_vmspace = PTRTOUINT64(p->p_vmspace);
2718 	ki->p_sigacts = PTRTOUINT64(p->p_sigacts);
2719 	ki->p_sess = PTRTOUINT64(p->p_session);
2720 	ki->p_tsess = 0;	/* may be changed if controlling tty below */
2721 	ki->p_ru = PTRTOUINT64(p->p_ru);
2722 
2723 	ki->p_eflag = 0;
2724 	ki->p_exitsig = p->p_exitsig;
2725 	ki->p_flag = p->p_flag;
2726 
2727 	ki->p_pid = p->p_pid;
2728 	if (p->p_pptr)
2729 		ki->p_ppid = p->p_pptr->p_pid;
2730 	else
2731 		ki->p_ppid = 0;
2732 	ki->p_sid = p->p_session->s_sid;
2733 	ki->p__pgid = p->p_pgrp->pg_id;
2734 
2735 	ki->p_tpgid = NO_PGID;	/* may be changed if controlling tty below */
2736 
2737 	ki->p_uid = p->p_ucred->cr_uid;
2738 	ki->p_ruid = p->p_cred->p_ruid;
2739 	ki->p_gid = p->p_ucred->cr_gid;
2740 	ki->p_rgid = p->p_cred->p_rgid;
2741 	ki->p_svuid = p->p_cred->p_svuid;
2742 	ki->p_svgid = p->p_cred->p_svgid;
2743 
2744 	memcpy(ki->p_groups, p->p_cred->pc_ucred->cr_groups,
2745 	    min(sizeof(ki->p_groups), sizeof(p->p_cred->pc_ucred->cr_groups)));
2746 	ki->p_ngroups = p->p_cred->pc_ucred->cr_ngroups;
2747 
2748 	ki->p_jobc = p->p_pgrp->pg_jobc;
2749 	if ((p->p_flag & P_CONTROLT) && (tp = p->p_session->s_ttyp)) {
2750 		ki->p_tdev = tp->t_dev;
2751 		ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
2752 		ki->p_tsess = PTRTOUINT64(tp->t_session);
2753 	} else {
2754 		ki->p_tdev = NODEV;
2755 	}
2756 
2757 	ki->p_estcpu = p->p_estcpu;
2758 	ki->p_rtime_sec = p->p_rtime.tv_sec;
2759 	ki->p_rtime_usec = p->p_rtime.tv_usec;
2760 	ki->p_cpticks = p->p_cpticks;
2761 	ki->p_pctcpu = p->p_pctcpu;
2762 
2763 	ki->p_uticks = p->p_uticks;
2764 	ki->p_sticks = p->p_sticks;
2765 	ki->p_iticks = p->p_iticks;
2766 
2767 	ki->p_tracep = PTRTOUINT64(p->p_tracep);
2768 	ki->p_traceflag = p->p_traceflag;
2769 
2770 
2771 	memcpy(&ki->p_siglist, &p->p_sigctx.ps_siglist, sizeof(ki_sigset_t));
2772 	memcpy(&ki->p_sigmask, &p->p_sigctx.ps_sigmask, sizeof(ki_sigset_t));
2773 	memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
2774 	memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
2775 
2776 	ki->p_stat = p->p_stat; /* Will likely be overridden by LWP status */
2777 	ki->p_realstat = p->p_stat;
2778 	ki->p_nice = p->p_nice;
2779 
2780 	ki->p_xstat = p->p_xstat;
2781 	ki->p_acflag = p->p_acflag;
2782 
2783 	strncpy(ki->p_comm, p->p_comm,
2784 	    min(sizeof(ki->p_comm), sizeof(p->p_comm)));
2785 
2786 	strncpy(ki->p_login, p->p_session->s_login,
2787 	    min(sizeof ki->p_login - 1, sizeof p->p_session->s_login));
2788 
2789 	ki->p_nlwps = p->p_nlwps;
2790 	ki->p_nrlwps = p->p_nrlwps;
2791 	ki->p_realflag = p->p_flag;
2792 
2793 	if (p->p_stat == SIDL || P_ZOMBIE(p)) {
2794 		ki->p_vm_rssize = 0;
2795 		ki->p_vm_tsize = 0;
2796 		ki->p_vm_dsize = 0;
2797 		ki->p_vm_ssize = 0;
2798 		l = NULL;
2799 	} else {
2800 		struct vmspace *vm = p->p_vmspace;
2801 
2802 		ki->p_vm_rssize = vm_resident_count(vm);
2803 		ki->p_vm_tsize = vm->vm_tsize;
2804 		ki->p_vm_dsize = vm->vm_dsize;
2805 		ki->p_vm_ssize = vm->vm_ssize;
2806 
2807 		/* Pick a "representative" LWP */
2808 		l = proc_representative_lwp(p);
2809 		ki->p_forw = PTRTOUINT64(l->l_forw);
2810 		ki->p_back = PTRTOUINT64(l->l_back);
2811 		ki->p_addr = PTRTOUINT64(l->l_addr);
2812 		ki->p_stat = l->l_stat;
2813 		ki->p_flag |= l->l_flag;
2814 		ki->p_swtime = l->l_swtime;
2815 		ki->p_slptime = l->l_slptime;
2816 		if (l->l_stat == LSONPROC) {
2817 			KDASSERT(l->l_cpu != NULL);
2818 			ki->p_schedflags = l->l_cpu->ci_schedstate.spc_flags;
2819 		} else
2820 			ki->p_schedflags = 0;
2821 		ki->p_holdcnt = l->l_holdcnt;
2822 		ki->p_priority = l->l_priority;
2823 		ki->p_usrpri = l->l_usrpri;
2824 		if (l->l_wmesg)
2825 			strncpy(ki->p_wmesg, l->l_wmesg, sizeof(ki->p_wmesg));
2826 		ki->p_wchan = PTRTOUINT64(l->l_wchan);
2827 
2828 	}
2829 
2830 	if (p->p_session->s_ttyvp)
2831 		ki->p_eflag |= EPROC_CTTY;
2832 	if (SESS_LEADER(p))
2833 		ki->p_eflag |= EPROC_SLEADER;
2834 
2835 	/* XXX Is this double check necessary? */
2836 	if (P_ZOMBIE(p)) {
2837 		ki->p_uvalid = 0;
2838 	} else {
2839 		ki->p_uvalid = 1;
2840 
2841 		ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
2842 		ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
2843 
2844 		calcru(p, &ut, &st, 0);
2845 		ki->p_uutime_sec = ut.tv_sec;
2846 		ki->p_uutime_usec = ut.tv_usec;
2847 		ki->p_ustime_sec = st.tv_sec;
2848 		ki->p_ustime_usec = st.tv_usec;
2849 
2850 		ki->p_uru_maxrss = p->p_stats->p_ru.ru_maxrss;
2851 		ki->p_uru_ixrss = p->p_stats->p_ru.ru_ixrss;
2852 		ki->p_uru_idrss = p->p_stats->p_ru.ru_idrss;
2853 		ki->p_uru_isrss = p->p_stats->p_ru.ru_isrss;
2854 		ki->p_uru_minflt = p->p_stats->p_ru.ru_minflt;
2855 		ki->p_uru_majflt = p->p_stats->p_ru.ru_majflt;
2856 		ki->p_uru_nswap = p->p_stats->p_ru.ru_nswap;
2857 		ki->p_uru_inblock = p->p_stats->p_ru.ru_inblock;
2858 		ki->p_uru_oublock = p->p_stats->p_ru.ru_oublock;
2859 		ki->p_uru_msgsnd = p->p_stats->p_ru.ru_msgsnd;
2860 		ki->p_uru_msgrcv = p->p_stats->p_ru.ru_msgrcv;
2861 		ki->p_uru_nsignals = p->p_stats->p_ru.ru_nsignals;
2862 		ki->p_uru_nvcsw = p->p_stats->p_ru.ru_nvcsw;
2863 		ki->p_uru_nivcsw = p->p_stats->p_ru.ru_nivcsw;
2864 
2865 		timeradd(&p->p_stats->p_cru.ru_utime,
2866 			 &p->p_stats->p_cru.ru_stime, &ut);
2867 		ki->p_uctime_sec = ut.tv_sec;
2868 		ki->p_uctime_usec = ut.tv_usec;
2869 	}
2870 #ifdef MULTIPROCESSOR
2871 	if (l && l->l_cpu != NULL)
2872 		ki->p_cpuid = l->l_cpu->ci_cpuid;
2873 	else
2874 #endif
2875 		ki->p_cpuid = KI_NOCPU;
2876 }
2877 
2878 /*
2879  * Fill in a kinfo_lwp structure for the specified lwp.
2880  */
2881 static void
2882 fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
2883 {
2884 
2885 	kl->l_forw = PTRTOUINT64(l->l_forw);
2886 	kl->l_back = PTRTOUINT64(l->l_back);
2887 	kl->l_laddr = PTRTOUINT64(l);
2888 	kl->l_addr = PTRTOUINT64(l->l_addr);
2889 	kl->l_stat = l->l_stat;
2890 	kl->l_lid = l->l_lid;
2891 	kl->l_flag = l->l_flag;
2892 
2893 	kl->l_swtime = l->l_swtime;
2894 	kl->l_slptime = l->l_slptime;
2895 	if (l->l_stat == LSONPROC) {
2896 		KDASSERT(l->l_cpu != NULL);
2897 		kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
2898 	} else
2899 		kl->l_schedflags = 0;
2900 	kl->l_holdcnt = l->l_holdcnt;
2901 	kl->l_priority = l->l_priority;
2902 	kl->l_usrpri = l->l_usrpri;
2903 	if (l->l_wmesg)
2904 		strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
2905 	kl->l_wchan = PTRTOUINT64(l->l_wchan);
2906 #ifdef MULTIPROCESSOR
2907 	if (l->l_cpu != NULL)
2908 		kl->l_cpuid = l->l_cpu->ci_cpuid;
2909 	else
2910 #endif
2911 		kl->l_cpuid = KI_NOCPU;
2912 }
2913 
2914 /*
2915  * Fill in an eproc structure for the specified process.
2916  */
2917 void
2918 fill_eproc(struct proc *p, struct eproc *ep)
2919 {
2920 	struct tty *tp;
2921 	struct lwp *l;
2922 
2923 	ep->e_paddr = p;
2924 	ep->e_sess = p->p_session;
2925 	ep->e_pcred = *p->p_cred;
2926 	ep->e_ucred = *p->p_ucred;
2927 	if (p->p_stat == SIDL || P_ZOMBIE(p)) {
2928 		ep->e_vm.vm_rssize = 0;
2929 		ep->e_vm.vm_tsize = 0;
2930 		ep->e_vm.vm_dsize = 0;
2931 		ep->e_vm.vm_ssize = 0;
2932 		/* ep->e_vm.vm_pmap = XXX; */
2933 	} else {
2934 		struct vmspace *vm = p->p_vmspace;
2935 
2936 		ep->e_vm.vm_rssize = vm_resident_count(vm);
2937 		ep->e_vm.vm_tsize = vm->vm_tsize;
2938 		ep->e_vm.vm_dsize = vm->vm_dsize;
2939 		ep->e_vm.vm_ssize = vm->vm_ssize;
2940 
2941 		/* Pick a "representative" LWP */
2942 		l = proc_representative_lwp(p);
2943 
2944 		if (l->l_wmesg)
2945 			strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN);
2946 	}
2947 	if (p->p_pptr)
2948 		ep->e_ppid = p->p_pptr->p_pid;
2949 	else
2950 		ep->e_ppid = 0;
2951 	ep->e_pgid = p->p_pgrp->pg_id;
2952 	ep->e_sid = ep->e_sess->s_sid;
2953 	ep->e_jobc = p->p_pgrp->pg_jobc;
2954 	if ((p->p_flag & P_CONTROLT) &&
2955 	    (tp = ep->e_sess->s_ttyp)) {
2956 		ep->e_tdev = tp->t_dev;
2957 		ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
2958 		ep->e_tsess = tp->t_session;
2959 	} else
2960 		ep->e_tdev = NODEV;
2961 
2962 	ep->e_xsize = ep->e_xrssize = 0;
2963 	ep->e_xccount = ep->e_xswrss = 0;
2964 	ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
2965 	if (SESS_LEADER(p))
2966 		ep->e_flag |= EPROC_SLEADER;
2967 	strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
2968 }
2969