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