xref: /netbsd-src/sys/kern/init_sysctl.c (revision e6d6e05cb173f30287ab619b21120b27baa66ad6)
1 /*	$NetBSD: init_sysctl.c,v 1.123 2008/02/27 19:57:18 matt 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.123 2008/02/27 19:57:18 matt 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 struct file *, struct proc *,
196 		      int);
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(&proclist_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(&proclist_lock);
1691 
1692 			mutex_enter(&p->p_smutex);
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_smutex);
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_smutex);
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_smutex);
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_smutex);
1730 
1731 			/* Drop reference to process. */
1732 			mutex_enter(&proclist_lock);
1733 			rw_exit(&p->p_reflock);
1734 		}
1735 		mutex_exit(&proclist_lock);
1736 	} else {
1737 		mutex_enter(&proclist_lock);
1738 		p = p_find(pid, PFIND_LOCKED);
1739 		if (p == NULL) {
1740 			error = ESRCH;
1741 			mutex_exit(&proclist_lock);
1742 			goto cleanup;
1743 		}
1744 		/* Grab a hold on the process. */
1745 		gotit = rw_tryenter(&p->p_reflock, RW_READER);
1746 		mutex_exit(&proclist_lock);
1747 		if (!gotit) {
1748 			error = ESRCH;
1749 			goto cleanup;
1750 		}
1751 
1752 		mutex_enter(&p->p_smutex);
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_smutex);
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_smutex);
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_smutex);
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_smutex);
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 
1929 	if (namelen == 1 && name[0] == CTL_QUERY)
1930 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
1931 
1932 	if (namelen != 4)
1933 		return (EINVAL);
1934 
1935 	error = 0;
1936 	dp = oldp;
1937 	len = (oldp != NULL) ? *oldlenp : 0;
1938 	op = name[0];
1939 	arg = name[1];
1940 	elem_size = name[2];
1941 	elem_count = name[3];
1942 	out_size = MIN(sizeof(kf), elem_size);
1943 	needed = 0;
1944 
1945 	if (elem_size < 1 || elem_count < 0)
1946 		return (EINVAL);
1947 
1948 	switch (op) {
1949 	case KERN_FILE_BYFILE:
1950 		/*
1951 		 * doesn't use arg so it must be zero
1952 		 */
1953 		if (arg != 0)
1954 			return (EINVAL);
1955 		sysctl_unlock();
1956 		/*
1957 		 * allocate dummy file descriptor to make position in list
1958 		 */
1959 		if ((tp = fgetdummy()) == NULL) {
1960 		 	sysctl_relock();
1961 			return ENOMEM;
1962 		}
1963 		mutex_enter(&filelist_lock);
1964 		for (fp = LIST_FIRST(&filehead); fp != NULL; fp = np) {
1965 			np = LIST_NEXT(fp, f_list);
1966 			mutex_enter(&fp->f_lock);
1967 			if (fp->f_count == 0) {
1968 				mutex_exit(&fp->f_lock);
1969 				continue;
1970 			}
1971 			/*
1972 			 * XXX Need to prevent that from being an alternative
1973 			 * XXX way for getting process information.
1974 			 */
1975 			if (kauth_authorize_generic(l->l_cred,
1976 			    KAUTH_GENERIC_CANSEE, fp->f_cred) != 0) {
1977 				mutex_exit(&fp->f_lock);
1978 				continue;
1979 			}
1980 			if (len >= elem_size && elem_count > 0) {
1981 				fill_file(&kf, fp, NULL, 0);
1982 				LIST_INSERT_AFTER(fp, tp, f_list);
1983 				mutex_exit(&fp->f_lock);
1984 				mutex_exit(&filelist_lock);
1985 				error = dcopyout(l, &kf, dp, out_size);
1986 				mutex_enter(&filelist_lock);
1987 				np = LIST_NEXT(tp, f_list);
1988 				LIST_REMOVE(tp, f_list);
1989 				if (error) {
1990 					break;
1991 				}
1992 				dp += elem_size;
1993 				len -= elem_size;
1994 			} else {
1995 				mutex_exit(&fp->f_lock);
1996 			}
1997 			if (elem_count > 0) {
1998 				needed += elem_size;
1999 				if (elem_count != INT_MAX)
2000 					elem_count--;
2001 			}
2002 		}
2003 		mutex_exit(&filelist_lock);
2004 		fputdummy(tp);
2005 		sysctl_relock();
2006 		break;
2007 	case KERN_FILE_BYPID:
2008 		if (arg < -1)
2009 			/* -1 means all processes */
2010 			return (EINVAL);
2011 		sysctl_unlock();
2012 		mutex_enter(&proclist_lock);
2013 		LIST_FOREACH(p, &allproc, p_list) {
2014 			if (p->p_stat == SIDL) {
2015 				/* skip embryonic processes */
2016 				continue;
2017 			}
2018 			if (arg > 0 && p->p_pid != arg) {
2019 				/* pick only the one we want */
2020 				/* XXX want 0 to mean "kernel files" */
2021 				continue;
2022 			}
2023 			mutex_enter(&p->p_mutex);
2024 			error = kauth_authorize_process(l->l_cred,
2025 			    KAUTH_PROCESS_CANSEE, p,
2026 			    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_OPENFILES),
2027 			    NULL, NULL);
2028 			mutex_exit(&p->p_mutex);
2029 			if (error != 0) {
2030 				continue;
2031 			}
2032 
2033 			/*
2034 			 * Grab a hold on the process.
2035 			 */
2036 			if (!rw_tryenter(&p->p_reflock, RW_READER)) {
2037 				continue;
2038 			}
2039 			mutex_exit(&proclist_lock);
2040 
2041 			/* XXX Do we need to check permission per file? */
2042 			fd = p->p_fd;
2043 			rw_enter(&fd->fd_lock, RW_READER);
2044 			for (i = 0; i < fd->fd_nfiles; i++) {
2045 				fp = fd->fd_ofiles[i];
2046 				if (fp == NULL) {
2047 					continue;
2048 				}
2049 				mutex_enter(&fp->f_lock);
2050 				if (!FILE_IS_USABLE(fp)) {
2051 					mutex_exit(&fp->f_lock);
2052 					continue;
2053 				}
2054 				if (len >= elem_size && elem_count > 0) {
2055 					fill_file(&kf, fd->fd_ofiles[i], p, i);
2056 					mutex_exit(&fp->f_lock);
2057 					rw_exit(&fd->fd_lock);
2058 					error = dcopyout(l, &kf, dp, out_size);
2059 					rw_enter(&fd->fd_lock, RW_READER);
2060 					if (error)
2061 						break;
2062 					dp += elem_size;
2063 					len -= elem_size;
2064 				} else {
2065 					mutex_exit(&fp->f_lock);
2066 				}
2067 				if (elem_count > 0) {
2068 					needed += elem_size;
2069 					if (elem_count != INT_MAX)
2070 						elem_count--;
2071 				}
2072 			}
2073 			rw_exit(&fd->fd_lock);
2074 
2075 			/*
2076 			 * Release reference to process.
2077 			 */
2078 			mutex_enter(&proclist_lock);
2079 			rw_exit(&p->p_reflock);
2080 		}
2081 		mutex_exit(&proclist_lock);
2082 		sysctl_relock();
2083 		break;
2084 	default:
2085 		return (EINVAL);
2086 	}
2087 
2088 	if (oldp == NULL)
2089 		needed += KERN_FILESLOP * elem_size;
2090 	*oldlenp = needed;
2091 
2092 	return (error);
2093 }
2094 
2095 static void
2096 fill_file(struct kinfo_file *kp, const struct file *fp, struct proc *p, int i)
2097 {
2098 
2099 	memset(kp, 0, sizeof(*kp));
2100 
2101 	kp->ki_fileaddr =	PTRTOUINT64(fp);
2102 	kp->ki_flag =		fp->f_flag;
2103 	kp->ki_iflags =		fp->f_iflags;
2104 	kp->ki_ftype =		fp->f_type;
2105 	kp->ki_count =		fp->f_count;
2106 	kp->ki_msgcount =	fp->f_msgcount;
2107 	kp->ki_usecount =	fp->f_usecount;
2108 	kp->ki_fucred =		PTRTOUINT64(fp->f_cred);
2109 	kp->ki_fuid =		kauth_cred_geteuid(fp->f_cred);
2110 	kp->ki_fgid =		kauth_cred_getegid(fp->f_cred);
2111 	kp->ki_fops =		PTRTOUINT64(fp->f_ops);
2112 	kp->ki_foffset =	fp->f_offset;
2113 	kp->ki_fdata =		PTRTOUINT64(fp->f_data);
2114 
2115 	/* vnode information to glue this file to something */
2116 	if (fp->f_type == DTYPE_VNODE) {
2117 		struct vnode *vp = (struct vnode *)fp->f_data;
2118 
2119 		kp->ki_vun =	PTRTOUINT64(vp->v_un.vu_socket);
2120 		kp->ki_vsize =	vp->v_size;
2121 		kp->ki_vtype =	vp->v_type;
2122 		kp->ki_vtag =	vp->v_tag;
2123 		kp->ki_vdata =	PTRTOUINT64(vp->v_data);
2124 	}
2125 
2126 	/* process information when retrieved via KERN_FILE_BYPID */
2127 	if (p) {
2128 		KASSERT(rw_lock_held(&p->p_fd->fd_lock));
2129 
2130 		kp->ki_pid =		p->p_pid;
2131 		kp->ki_fd =		i;
2132 		kp->ki_ofileflags =	p->p_fd->fd_ofileflags[i];
2133 	}
2134 }
2135 
2136 static int
2137 sysctl_doeproc(SYSCTLFN_ARGS)
2138 {
2139 	struct eproc *eproc;
2140 	struct kinfo_proc2 *kproc2;
2141 	struct kinfo_proc *dp;
2142 	struct proc *p;
2143 	char *where, *dp2;
2144 	int type, op, arg;
2145 	u_int elem_size, elem_count;
2146 	size_t buflen, needed;
2147 	bool match;
2148 	int error;
2149 
2150 	if (namelen == 1 && name[0] == CTL_QUERY)
2151 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
2152 
2153 	dp = oldp;
2154 	dp2 = where = oldp;
2155 	buflen = where != NULL ? *oldlenp : 0;
2156 	error = 0;
2157 	needed = 0;
2158 	type = rnode->sysctl_num;
2159 
2160 	if (type == KERN_PROC) {
2161 		if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
2162 			return (EINVAL);
2163 		op = name[0];
2164 		if (op != KERN_PROC_ALL)
2165 			arg = name[1];
2166 		else
2167 			arg = 0;		/* Quell compiler warning */
2168 		elem_size = elem_count = 0;	/* Ditto */
2169 	} else {
2170 		if (namelen != 4)
2171 			return (EINVAL);
2172 		op = name[0];
2173 		arg = name[1];
2174 		elem_size = name[2];
2175 		elem_count = name[3];
2176 	}
2177 
2178 	sysctl_unlock();
2179 
2180 	if (type == KERN_PROC) {
2181 		eproc = kmem_alloc(sizeof(*eproc), KM_SLEEP);
2182 		kproc2 = NULL;
2183 	} else {
2184 		eproc = NULL;
2185 		kproc2 = kmem_alloc(sizeof(*kproc2), KM_SLEEP);
2186 	}
2187 
2188 	mutex_enter(&proclist_lock);
2189 	LIST_FOREACH(p, &allproc, p_list) {
2190 		/*
2191 		 * Skip embryonic processes.
2192 		 */
2193 		if (p->p_stat == SIDL)
2194 			continue;
2195 
2196 		mutex_enter(&p->p_mutex);
2197 		error = kauth_authorize_process(l->l_cred,
2198 		    KAUTH_PROCESS_CANSEE, p,
2199 		    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
2200 		if (error != 0) {
2201 			mutex_exit(&p->p_mutex);
2202 			continue;
2203 		}
2204 
2205 		/*
2206 		 * TODO - make more efficient (see notes below).
2207 		 * do by session.
2208 		 */
2209 		switch (op) {
2210 		case KERN_PROC_PID:
2211 			/* could do this with just a lookup */
2212 			match = (p->p_pid == (pid_t)arg);
2213 			break;
2214 
2215 		case KERN_PROC_PGRP:
2216 			/* could do this by traversing pgrp */
2217 			match = (p->p_pgrp->pg_id == (pid_t)arg);
2218 			break;
2219 
2220 		case KERN_PROC_SESSION:
2221 			match = (p->p_session->s_sid == (pid_t)arg);
2222 			break;
2223 
2224 		case KERN_PROC_TTY:
2225 			match = true;
2226 			if (arg == (int) KERN_PROC_TTY_REVOKE) {
2227 				if ((p->p_lflag & PL_CONTROLT) == 0 ||
2228 				    p->p_session->s_ttyp == NULL ||
2229 				    p->p_session->s_ttyvp != NULL) {
2230 				    	match = false;
2231 				}
2232 			} else if ((p->p_lflag & PL_CONTROLT) == 0 ||
2233 			    p->p_session->s_ttyp == NULL) {
2234 				if ((dev_t)arg != KERN_PROC_TTY_NODEV) {
2235 					match = false;
2236 				}
2237 			} else if (p->p_session->s_ttyp->t_dev != (dev_t)arg) {
2238 				match = false;
2239 			}
2240 			break;
2241 
2242 		case KERN_PROC_UID:
2243 			match = (kauth_cred_geteuid(p->p_cred) == (uid_t)arg);
2244 			break;
2245 
2246 		case KERN_PROC_RUID:
2247 			match = (kauth_cred_getuid(p->p_cred) == (uid_t)arg);
2248 			break;
2249 
2250 		case KERN_PROC_GID:
2251 			match = (kauth_cred_getegid(p->p_cred) == (uid_t)arg);
2252 			break;
2253 
2254 		case KERN_PROC_RGID:
2255 			match = (kauth_cred_getgid(p->p_cred) == (uid_t)arg);
2256 			break;
2257 
2258 		case KERN_PROC_ALL:
2259 			match = true;
2260 			/* allow everything */
2261 			break;
2262 
2263 		default:
2264 			error = EINVAL;
2265 			mutex_exit(&p->p_mutex);
2266 			goto cleanup;
2267 		}
2268 		if (!match) {
2269 			mutex_exit(&p->p_mutex);
2270 			continue;
2271 		}
2272 
2273 		/*
2274 		 * Grab a hold on the process.
2275 		 */
2276 		if (!rw_tryenter(&p->p_reflock, RW_READER)) {
2277 			mutex_exit(&p->p_mutex);
2278 			continue;
2279 		}
2280 
2281 		if (type == KERN_PROC) {
2282 			if (buflen >= sizeof(struct kinfo_proc)) {
2283 				fill_eproc(p, eproc);
2284 				mutex_exit(&p->p_mutex);
2285 				mutex_exit(&proclist_lock);
2286 				error = dcopyout(l, p, &dp->kp_proc,
2287 				    sizeof(struct proc));
2288 				mutex_enter(&proclist_lock);
2289 				if (error) {
2290 					rw_exit(&p->p_reflock);
2291 					goto cleanup;
2292 				}
2293 				error = dcopyout(l, eproc, &dp->kp_eproc,
2294 				    sizeof(*eproc));
2295 				if (error) {
2296 					rw_exit(&p->p_reflock);
2297 					goto cleanup;
2298 				}
2299 				dp++;
2300 				buflen -= sizeof(struct kinfo_proc);
2301 			} else {
2302 				mutex_exit(&p->p_mutex);
2303 			}
2304 			needed += sizeof(struct kinfo_proc);
2305 		} else { /* KERN_PROC2 */
2306 			if (buflen >= elem_size && elem_count > 0) {
2307 				fill_kproc2(p, kproc2);
2308 				mutex_exit(&p->p_mutex);
2309 				mutex_exit(&proclist_lock);
2310 				/*
2311 				 * Copy out elem_size, but not larger than
2312 				 * the size of a struct kinfo_proc2.
2313 				 */
2314 				error = dcopyout(l, kproc2, dp2,
2315 				    min(sizeof(*kproc2), elem_size));
2316 				mutex_enter(&proclist_lock);
2317 				if (error) {
2318 					rw_exit(&p->p_reflock);
2319 					goto cleanup;
2320 				}
2321 				dp2 += elem_size;
2322 				buflen -= elem_size;
2323 				elem_count--;
2324 			} else {
2325 				mutex_exit(&p->p_mutex);
2326 			}
2327 			needed += elem_size;
2328 		}
2329 
2330 		/*
2331 		 * Release reference to process.
2332 		 */
2333 		rw_exit(&p->p_reflock);
2334 	}
2335 	mutex_exit(&proclist_lock);
2336 
2337 	if (where != NULL) {
2338 		if (type == KERN_PROC)
2339 			*oldlenp = (char *)dp - where;
2340 		else
2341 			*oldlenp = dp2 - where;
2342 		if (needed > *oldlenp) {
2343 			error = ENOMEM;
2344 			goto out;
2345 		}
2346 	} else {
2347 		needed += KERN_PROCSLOP;
2348 		*oldlenp = needed;
2349 	}
2350 	if (kproc2)
2351 		kmem_free(kproc2, sizeof(*kproc2));
2352 	if (eproc)
2353 		kmem_free(eproc, sizeof(*eproc));
2354 	sysctl_relock();
2355 	return 0;
2356  cleanup:
2357 	mutex_exit(&proclist_lock);
2358  out:
2359 	if (kproc2)
2360 		kmem_free(kproc2, sizeof(*kproc2));
2361 	if (eproc)
2362 		kmem_free(eproc, sizeof(*eproc));
2363 	sysctl_relock();
2364 	return error;
2365 }
2366 
2367 /*
2368  * sysctl helper routine for kern.proc_args pseudo-subtree.
2369  */
2370 static int
2371 sysctl_kern_proc_args(SYSCTLFN_ARGS)
2372 {
2373 	struct ps_strings pss;
2374 	struct proc *p;
2375 	size_t len, i;
2376 	struct uio auio;
2377 	struct iovec aiov;
2378 	pid_t pid;
2379 	int nargv, type, error, argvlen;
2380 	char *arg;
2381 	char **argv = NULL;
2382 	char *tmp;
2383 	struct vmspace *vmspace;
2384 	vaddr_t psstr_addr;
2385 	vaddr_t offsetn;
2386 	vaddr_t offsetv;
2387 
2388 	if (namelen == 1 && name[0] == CTL_QUERY)
2389 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
2390 
2391 	if (newp != NULL || namelen != 2)
2392 		return (EINVAL);
2393 	pid = name[0];
2394 	type = name[1];
2395 	argv = NULL;
2396 	argvlen = 0;
2397 
2398 	switch (type) {
2399 	case KERN_PROC_ARGV:
2400 	case KERN_PROC_NARGV:
2401 	case KERN_PROC_ENV:
2402 	case KERN_PROC_NENV:
2403 		/* ok */
2404 		break;
2405 	default:
2406 		return (EINVAL);
2407 	}
2408 
2409 	sysctl_unlock();
2410 
2411 	/* check pid */
2412 	mutex_enter(&proclist_lock);
2413 	if ((p = p_find(pid, PFIND_LOCKED)) == NULL) {
2414 		error = EINVAL;
2415 		goto out_locked;
2416 	}
2417 	mutex_enter(&p->p_mutex);
2418 
2419 	/* Check permission. */
2420 	if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV)
2421 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
2422 		    p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ARGS), NULL, NULL);
2423 	else if (type == KERN_PROC_ENV || type == KERN_PROC_NENV)
2424 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
2425 		    p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENV), NULL, NULL);
2426 	else
2427 		error = EINVAL; /* XXXGCC */
2428 	if (error) {
2429 		mutex_exit(&p->p_mutex);
2430 		goto out_locked;
2431 	}
2432 
2433 	if (oldp == NULL) {
2434 		if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
2435 			*oldlenp = sizeof (int);
2436 		else
2437 			*oldlenp = ARG_MAX;	/* XXX XXX XXX */
2438 		error = 0;
2439 		mutex_exit(&p->p_mutex);
2440 		goto out_locked;
2441 	}
2442 
2443 	/*
2444 	 * Zombies don't have a stack, so we can't read their psstrings.
2445 	 * System processes also don't have a user stack.
2446 	 */
2447 	if (P_ZOMBIE(p) || (p->p_flag & PK_SYSTEM) != 0) {
2448 		error = EINVAL;
2449 		mutex_exit(&p->p_mutex);
2450 		goto out_locked;
2451 	}
2452 
2453 	/*
2454 	 * Lock the process down in memory.
2455 	 */
2456 	psstr_addr = (vaddr_t)p->p_psstr;
2457 	if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV) {
2458 		offsetn = p->p_psnargv;
2459 		offsetv = p->p_psargv;
2460 	} else {
2461 		offsetn = p->p_psnenv;
2462 		offsetv = p->p_psenv;
2463 	}
2464 	vmspace = p->p_vmspace;
2465 	uvmspace_addref(vmspace);
2466 	mutex_exit(&p->p_mutex);
2467 	mutex_exit(&proclist_lock);
2468 
2469 	/*
2470 	 * Allocate a temporary buffer to hold the arguments.
2471 	 */
2472 	arg = kmem_alloc(PAGE_SIZE, KM_SLEEP);
2473 
2474 	/*
2475 	 * Read in the ps_strings structure.
2476 	 */
2477 	aiov.iov_base = &pss;
2478 	aiov.iov_len = sizeof(pss);
2479 	auio.uio_iov = &aiov;
2480 	auio.uio_iovcnt = 1;
2481 	auio.uio_offset = psstr_addr;
2482 	auio.uio_resid = sizeof(pss);
2483 	auio.uio_rw = UIO_READ;
2484 	UIO_SETUP_SYSSPACE(&auio);
2485 	error = uvm_io(&vmspace->vm_map, &auio);
2486 	if (error)
2487 		goto done;
2488 
2489 	memcpy(&nargv, (char *)&pss + offsetn, sizeof(nargv));
2490 	if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
2491 		error = dcopyout(l, &nargv, oldp, sizeof(nargv));
2492 		*oldlenp = sizeof(nargv);
2493 		goto done;
2494 	}
2495 	/*
2496 	 * Now read the address of the argument vector.
2497 	 */
2498 	switch (type) {
2499 	case KERN_PROC_ARGV:
2500 		/* FALLTHROUGH */
2501 	case KERN_PROC_ENV:
2502 		memcpy(&tmp, (char *)&pss + offsetv, sizeof(tmp));
2503 		break;
2504 	default:
2505 		error = EINVAL;
2506 		goto done;
2507 	}
2508 
2509 #ifdef COMPAT_NETBSD32
2510 	if (p->p_flag & PK_32)
2511 		len = sizeof(netbsd32_charp) * nargv;
2512 	else
2513 #endif
2514 		len = sizeof(char *) * nargv;
2515 
2516 	if ((argvlen = len) != 0)
2517 		argv = kmem_alloc(len, KM_SLEEP);
2518 
2519 	aiov.iov_base = argv;
2520 	aiov.iov_len = len;
2521 	auio.uio_iov = &aiov;
2522 	auio.uio_iovcnt = 1;
2523 	auio.uio_offset = (off_t)(unsigned long)tmp;
2524 	auio.uio_resid = len;
2525 	auio.uio_rw = UIO_READ;
2526 	UIO_SETUP_SYSSPACE(&auio);
2527 	error = uvm_io(&vmspace->vm_map, &auio);
2528 	if (error)
2529 		goto done;
2530 
2531 	/*
2532 	 * Now copy each string.
2533 	 */
2534 	len = 0; /* bytes written to user buffer */
2535 	for (i = 0; i < nargv; i++) {
2536 		int finished = 0;
2537 		vaddr_t base;
2538 		size_t xlen;
2539 		int j;
2540 
2541 #ifdef COMPAT_NETBSD32
2542 		if (p->p_flag & PK_32) {
2543 			netbsd32_charp *argv32;
2544 
2545 			argv32 = (netbsd32_charp *)argv;
2546 			base = (vaddr_t)NETBSD32PTR64(argv32[i]);
2547 		} else
2548 #endif
2549 			base = (vaddr_t)argv[i];
2550 
2551 		/*
2552 		 * The program has messed around with its arguments,
2553 		 * possibly deleting some, and replacing them with
2554 		 * NULL's. Treat this as the last argument and not
2555 		 * a failure.
2556 		 */
2557 		if (base == 0)
2558 			break;
2559 
2560 		while (!finished) {
2561 			xlen = PAGE_SIZE - (base & PAGE_MASK);
2562 
2563 			aiov.iov_base = arg;
2564 			aiov.iov_len = PAGE_SIZE;
2565 			auio.uio_iov = &aiov;
2566 			auio.uio_iovcnt = 1;
2567 			auio.uio_offset = base;
2568 			auio.uio_resid = xlen;
2569 			auio.uio_rw = UIO_READ;
2570 			UIO_SETUP_SYSSPACE(&auio);
2571 			error = uvm_io(&vmspace->vm_map, &auio);
2572 			if (error)
2573 				goto done;
2574 
2575 			/* Look for the end of the string */
2576 			for (j = 0; j < xlen; j++) {
2577 				if (arg[j] == '\0') {
2578 					xlen = j + 1;
2579 					finished = 1;
2580 					break;
2581 				}
2582 			}
2583 
2584 			/* Check for user buffer overflow */
2585 			if (len + xlen > *oldlenp) {
2586 				finished = 1;
2587 				if (len > *oldlenp)
2588 					xlen = 0;
2589 				else
2590 					xlen = *oldlenp - len;
2591 			}
2592 
2593 			/* Copyout the page */
2594 			error = dcopyout(l, arg, (char *)oldp + len, xlen);
2595 			if (error)
2596 				goto done;
2597 
2598 			len += xlen;
2599 			base += xlen;
2600 		}
2601 	}
2602 	*oldlenp = len;
2603 
2604 done:
2605 	if (argvlen != 0)
2606 		kmem_free(argv, argvlen);
2607 	uvmspace_free(vmspace);
2608 	kmem_free(arg, PAGE_SIZE);
2609 	sysctl_relock();
2610 	return error;
2611 
2612 out_locked:
2613 	mutex_exit(&proclist_lock);
2614 	sysctl_relock();
2615 	return error;
2616 }
2617 
2618 static int
2619 sysctl_security_setidcore(SYSCTLFN_ARGS)
2620 {
2621 	int newsize, error;
2622 	struct sysctlnode node;
2623 
2624 	node = *rnode;
2625 	node.sysctl_data = &newsize;
2626 	newsize = *(int *)rnode->sysctl_data;
2627 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
2628 	if (error || newp == NULL)
2629 		return error;
2630 
2631 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
2632 	    0, NULL, NULL, NULL))
2633 		return (EPERM);
2634 
2635 	*(int *)rnode->sysctl_data = newsize;
2636 
2637 	return 0;
2638 }
2639 
2640 static int
2641 sysctl_security_setidcorename(SYSCTLFN_ARGS)
2642 {
2643 	int error;
2644 	char *newsetidcorename;
2645 	struct sysctlnode node;
2646 
2647 	newsetidcorename = PNBUF_GET();
2648 	node = *rnode;
2649 	node.sysctl_data = newsetidcorename;
2650 	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
2651 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
2652 	if (error || newp == NULL) {
2653 		goto out;
2654 	}
2655 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
2656 	    0, NULL, NULL, NULL)) {
2657 		error = EPERM;
2658 		goto out;
2659 	}
2660 	if (strlen(newsetidcorename) == 0) {
2661 		error = EINVAL;
2662 		goto out;
2663 	}
2664 	memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
2665 out:
2666 	PNBUF_PUT(newsetidcorename);
2667 	return error;
2668 }
2669 
2670 /*
2671  * sysctl helper routine for kern.cp_id node. Maps cpus to their
2672  * cpuids.
2673  */
2674 static int
2675 sysctl_kern_cpid(SYSCTLFN_ARGS)
2676 {
2677 	struct sysctlnode node = *rnode;
2678 	uint64_t *cp_id = NULL;
2679 	int error, n = ncpu;
2680 	struct cpu_info *ci;
2681 	CPU_INFO_ITERATOR cii;
2682 
2683 	/*
2684 	 * Here you may either retrieve a single cpu id or the whole
2685 	 * set. The size you get back when probing depends on what
2686 	 * you ask for.
2687 	 */
2688 	switch (namelen) {
2689 	case 0:
2690 		node.sysctl_size = n * sizeof(uint64_t);
2691 		n = -2; /* ALL */
2692 		break;
2693 	case 1:
2694 		if (name[0] < 0 || name[0] >= n)
2695 			return (ENOENT); /* ENOSUCHPROCESSOR */
2696 		node.sysctl_size = sizeof(uint64_t);
2697 		n = name[0];
2698 		/*
2699 		 * adjust these so that sysctl_lookup() will be happy
2700 		 */
2701 		name++;
2702 		namelen--;
2703 		break;
2704 	default:
2705 		return (EINVAL);
2706 	}
2707 
2708 	cp_id = kmem_alloc(node.sysctl_size, KM_SLEEP);
2709 	if (cp_id == NULL)
2710 		return (ENOMEM);
2711 	node.sysctl_data = cp_id;
2712 	memset(cp_id, 0, node.sysctl_size);
2713 
2714 	for (CPU_INFO_FOREACH(cii, ci)) {
2715 		if (n <= 0)
2716 			cp_id[0] = ci->ci_cpuid;
2717 		/*
2718 		 * if a specific processor was requested and we just
2719 		 * did it, we're done here
2720 		 */
2721 		if (n == 0)
2722 			break;
2723 		/*
2724 		 * if doing "all", skip to next cp_id slot for next processor
2725 		 */
2726 		if (n == -2)
2727 			cp_id++;
2728 		/*
2729 		 * if we're doing a specific processor, we're one
2730 		 * processor closer
2731 		 */
2732 		if (n > 0)
2733 			n--;
2734 	}
2735 
2736 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
2737 	kmem_free(node.sysctl_data, node.sysctl_size);
2738 	return (error);
2739 }
2740 
2741 /*
2742  * sysctl helper routine for hw.usermem and hw.usermem64. Values are
2743  * calculate on the fly taking into account integer overflow and the
2744  * current wired count.
2745  */
2746 static int
2747 sysctl_hw_usermem(SYSCTLFN_ARGS)
2748 {
2749 	u_int ui;
2750 	u_quad_t uq;
2751 	struct sysctlnode node;
2752 
2753 	node = *rnode;
2754 	switch (rnode->sysctl_num) {
2755 	    case HW_USERMEM:
2756 		if ((ui = physmem - uvmexp.wired) > (UINT_MAX / PAGE_SIZE))
2757 			ui = UINT_MAX;
2758 		else
2759 			ui *= PAGE_SIZE;
2760 		node.sysctl_data = &ui;
2761 		break;
2762 	case HW_USERMEM64:
2763 		uq = (u_quad_t)(physmem - uvmexp.wired) * PAGE_SIZE;
2764 		node.sysctl_data = &uq;
2765 		break;
2766 	default:
2767 		return (EINVAL);
2768 	}
2769 
2770 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
2771 }
2772 
2773 /*
2774  * sysctl helper routine for kern.cnmagic node. Pulls the old value
2775  * out, encoded, and stuffs the new value in for decoding.
2776  */
2777 static int
2778 sysctl_hw_cnmagic(SYSCTLFN_ARGS)
2779 {
2780 	char magic[CNS_LEN];
2781 	int error;
2782 	struct sysctlnode node;
2783 
2784 	if (oldp)
2785 		cn_get_magic(magic, CNS_LEN);
2786 	node = *rnode;
2787 	node.sysctl_data = &magic[0];
2788 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
2789 	if (error || newp == NULL)
2790 		return (error);
2791 
2792 	return (cn_set_magic(magic));
2793 }
2794 
2795 /*
2796  * ********************************************************************
2797  * section 3: public helper routines that are used for more than one
2798  * node
2799  * ********************************************************************
2800  */
2801 
2802 /*
2803  * sysctl helper routine for the kern.root_device node and some ports'
2804  * machdep.root_device nodes.
2805  */
2806 int
2807 sysctl_root_device(SYSCTLFN_ARGS)
2808 {
2809 	struct sysctlnode node;
2810 
2811 	node = *rnode;
2812 	node.sysctl_data = root_device->dv_xname;
2813 	node.sysctl_size = strlen(root_device->dv_xname) + 1;
2814 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
2815 }
2816 
2817 /*
2818  * sysctl helper routine for kern.consdev, dependent on the current
2819  * state of the console. Also used for machdep.console_device on some
2820  * ports.
2821  */
2822 int
2823 sysctl_consdev(SYSCTLFN_ARGS)
2824 {
2825 	dev_t consdev;
2826 	struct sysctlnode node;
2827 
2828 	if (cn_tab != NULL)
2829 		consdev = cn_tab->cn_dev;
2830 	else
2831 		consdev = NODEV;
2832 	node = *rnode;
2833 	node.sysctl_data = &consdev;
2834 	node.sysctl_size = sizeof(consdev);
2835 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
2836 }
2837 
2838 /*
2839  * ********************************************************************
2840  * section 4: support for some helpers
2841  * ********************************************************************
2842  */
2843 
2844 /*
2845  * Fill in a kinfo_proc2 structure for the specified process.
2846  */
2847 static void
2848 fill_kproc2(struct proc *p, struct kinfo_proc2 *ki)
2849 {
2850 	struct tty *tp;
2851 	struct lwp *l, *l2;
2852 	struct timeval ut, st, rt;
2853 	sigset_t ss1, ss2;
2854 
2855 	KASSERT(mutex_owned(&proclist_lock));
2856 	KASSERT(mutex_owned(&p->p_mutex));
2857 
2858 	memset(ki, 0, sizeof(*ki));
2859 
2860 	ki->p_paddr = PTRTOUINT64(p);
2861 	ki->p_fd = PTRTOUINT64(p->p_fd);
2862 	ki->p_cwdi = PTRTOUINT64(p->p_cwdi);
2863 	ki->p_stats = PTRTOUINT64(p->p_stats);
2864 	ki->p_limit = PTRTOUINT64(p->p_limit);
2865 	ki->p_vmspace = PTRTOUINT64(p->p_vmspace);
2866 	ki->p_sigacts = PTRTOUINT64(p->p_sigacts);
2867 	ki->p_sess = PTRTOUINT64(p->p_session);
2868 	ki->p_tsess = 0;	/* may be changed if controlling tty below */
2869 	ki->p_ru = PTRTOUINT64(&p->p_stats->p_ru);
2870 
2871 	ki->p_eflag = 0;
2872 	ki->p_exitsig = p->p_exitsig;
2873 
2874 	ki->p_flag = sysctl_map_flags(sysctl_flagmap, p->p_flag);
2875 	ki->p_flag |= sysctl_map_flags(sysctl_sflagmap, p->p_sflag);
2876 	ki->p_flag |= sysctl_map_flags(sysctl_slflagmap, p->p_slflag);
2877 	ki->p_flag |= sysctl_map_flags(sysctl_lflagmap, p->p_lflag);
2878 	ki->p_flag |= sysctl_map_flags(sysctl_stflagmap, p->p_stflag);
2879 
2880 	ki->p_pid = p->p_pid;
2881 	if (p->p_pptr)
2882 		ki->p_ppid = p->p_pptr->p_pid;
2883 	else
2884 		ki->p_ppid = 0;
2885 	ki->p_sid = p->p_session->s_sid;
2886 	ki->p__pgid = p->p_pgrp->pg_id;
2887 
2888 	ki->p_tpgid = NO_PGID;	/* may be changed if controlling tty below */
2889 
2890 	ki->p_uid = kauth_cred_geteuid(p->p_cred);
2891 	ki->p_ruid = kauth_cred_getuid(p->p_cred);
2892 	ki->p_gid = kauth_cred_getegid(p->p_cred);
2893 	ki->p_rgid = kauth_cred_getgid(p->p_cred);
2894 	ki->p_svuid = kauth_cred_getsvuid(p->p_cred);
2895 	ki->p_svgid = kauth_cred_getsvgid(p->p_cred);
2896 
2897 	ki->p_ngroups = kauth_cred_ngroups(p->p_cred);
2898 	kauth_cred_getgroups(p->p_cred, ki->p_groups,
2899 	    min(ki->p_ngroups, sizeof(ki->p_groups) / sizeof(ki->p_groups[0])),
2900 	    UIO_SYSSPACE);
2901 
2902 	ki->p_jobc = p->p_pgrp->pg_jobc;
2903 	if ((p->p_lflag & PL_CONTROLT) && (tp = p->p_session->s_ttyp)) {
2904 		ki->p_tdev = tp->t_dev;
2905 		ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
2906 		ki->p_tsess = PTRTOUINT64(tp->t_session);
2907 	} else {
2908 		ki->p_tdev = NODEV;
2909 	}
2910 
2911 	mutex_enter(&p->p_smutex);
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_wmesg)
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 		ki->p_uru_maxrss = p->p_stats->p_ru.ru_maxrss;
3018 		ki->p_uru_ixrss = p->p_stats->p_ru.ru_ixrss;
3019 		ki->p_uru_idrss = p->p_stats->p_ru.ru_idrss;
3020 		ki->p_uru_isrss = p->p_stats->p_ru.ru_isrss;
3021 		ki->p_uru_minflt = p->p_stats->p_ru.ru_minflt;
3022 		ki->p_uru_majflt = p->p_stats->p_ru.ru_majflt;
3023 		ki->p_uru_nswap = p->p_stats->p_ru.ru_nswap;
3024 		ki->p_uru_inblock = p->p_stats->p_ru.ru_inblock;
3025 		ki->p_uru_oublock = p->p_stats->p_ru.ru_oublock;
3026 		ki->p_uru_msgsnd = p->p_stats->p_ru.ru_msgsnd;
3027 		ki->p_uru_msgrcv = p->p_stats->p_ru.ru_msgrcv;
3028 		ki->p_uru_nsignals = p->p_stats->p_ru.ru_nsignals;
3029 
3030 		ki->p_uru_nvcsw = 0;
3031 		ki->p_uru_nivcsw = 0;
3032 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
3033 			ki->p_uru_nvcsw += (l->l_ncsw - l->l_nivcsw);
3034 			ki->p_uru_nivcsw += l->l_nivcsw;
3035 		}
3036 
3037 		timeradd(&p->p_stats->p_cru.ru_utime,
3038 			 &p->p_stats->p_cru.ru_stime, &ut);
3039 		ki->p_uctime_sec = ut.tv_sec;
3040 		ki->p_uctime_usec = ut.tv_usec;
3041 	}
3042 	if (l != NULL)
3043 		ki->p_cpuid = l->l_cpu->ci_cpuid;
3044 
3045 	mutex_exit(&p->p_smutex);
3046 }
3047 
3048 /*
3049  * Fill in a kinfo_lwp structure for the specified lwp.
3050  */
3051 static void
3052 fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
3053 {
3054 	struct proc *p = l->l_proc;
3055 	struct timeval tv;
3056 
3057 	kl->l_forw = 0;
3058 	kl->l_back = 0;
3059 	kl->l_laddr = PTRTOUINT64(l);
3060 	kl->l_addr = PTRTOUINT64(l->l_addr);
3061 	kl->l_stat = l->l_stat;
3062 	kl->l_lid = l->l_lid;
3063 	kl->l_flag = sysctl_map_flags(sysctl_lwpprflagmap, l->l_prflag);
3064 
3065 	kl->l_swtime = l->l_swtime;
3066 	kl->l_slptime = l->l_slptime;
3067 	if (l->l_stat == LSONPROC)
3068 		kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
3069 	else
3070 		kl->l_schedflags = 0;
3071 	kl->l_holdcnt = l->l_holdcnt;
3072 	kl->l_priority = lwp_eprio(l);
3073 	kl->l_usrpri = l->l_priority;
3074 	if (l->l_wmesg)
3075 		strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
3076 	kl->l_wchan = PTRTOUINT64(l->l_wchan);
3077 	kl->l_cpuid = l->l_cpu->ci_cpuid;
3078 	bintime2timeval(&l->l_rtime, &tv);
3079 	kl->l_rtime_sec = tv.tv_sec;
3080 	kl->l_rtime_usec = tv.tv_usec;
3081 	kl->l_cpticks = l->l_cpticks;
3082 	kl->l_pctcpu = l->l_pctcpu;
3083 	kl->l_pid = p->p_pid;
3084 	if (l->l_name == NULL)
3085 		kl->l_name[0] = '\0';
3086 	else
3087 		strlcpy(kl->l_name, l->l_name, sizeof(kl->l_name));
3088 }
3089 
3090 /*
3091  * Fill in an eproc structure for the specified process.
3092  */
3093 void
3094 fill_eproc(struct proc *p, struct eproc *ep)
3095 {
3096 	struct tty *tp;
3097 	struct lwp *l;
3098 
3099 	KASSERT(mutex_owned(&proclist_lock));
3100 	KASSERT(mutex_owned(&p->p_mutex));
3101 
3102 	ep->e_paddr = p;
3103 	ep->e_sess = p->p_session;
3104 	kauth_cred_topcred(p->p_cred, &ep->e_pcred);
3105 	kauth_cred_toucred(p->p_cred, &ep->e_ucred);
3106 	if (p->p_stat == SIDL || P_ZOMBIE(p)) {
3107 		ep->e_vm.vm_rssize = 0;
3108 		ep->e_vm.vm_tsize = 0;
3109 		ep->e_vm.vm_dsize = 0;
3110 		ep->e_vm.vm_ssize = 0;
3111 		/* ep->e_vm.vm_pmap = XXX; */
3112 	} else {
3113 		struct vmspace *vm = p->p_vmspace;
3114 
3115 		ep->e_vm.vm_rssize = vm_resident_count(vm);
3116 		ep->e_vm.vm_tsize = vm->vm_tsize;
3117 		ep->e_vm.vm_dsize = vm->vm_dsize;
3118 		ep->e_vm.vm_ssize = vm->vm_ssize;
3119 
3120 		/* Pick a "representative" LWP */
3121 		mutex_enter(&p->p_smutex);
3122 		l = proc_representative_lwp(p, NULL, 1);
3123 		lwp_lock(l);
3124 		if (l->l_wmesg)
3125 			strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN);
3126 		lwp_unlock(l);
3127 		mutex_exit(&p->p_smutex);
3128 	}
3129 	if (p->p_pptr)
3130 		ep->e_ppid = p->p_pptr->p_pid;
3131 	else
3132 		ep->e_ppid = 0;
3133 	ep->e_pgid = p->p_pgrp->pg_id;
3134 	ep->e_sid = ep->e_sess->s_sid;
3135 	ep->e_jobc = p->p_pgrp->pg_jobc;
3136 	if ((p->p_lflag & PL_CONTROLT) &&
3137 	    (tp = ep->e_sess->s_ttyp)) {
3138 		ep->e_tdev = tp->t_dev;
3139 		ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
3140 		ep->e_tsess = tp->t_session;
3141 	} else
3142 		ep->e_tdev = NODEV;
3143 
3144 	ep->e_xsize = ep->e_xrssize = 0;
3145 	ep->e_xccount = ep->e_xswrss = 0;
3146 	ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
3147 	if (SESS_LEADER(p))
3148 		ep->e_flag |= EPROC_SLEADER;
3149 	strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
3150 }
3151 
3152 u_int
3153 sysctl_map_flags(const u_int *map, u_int word)
3154 {
3155 	u_int rv;
3156 
3157 	for (rv = 0; *map != 0; map += 2)
3158 		if ((word & map[0]) != 0)
3159 			rv |= map[1];
3160 
3161 	return rv;
3162 }
3163