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