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