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