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