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