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