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