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