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