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