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