init_sysctl.c revision 1.186
1/*	$NetBSD: init_sysctl.c,v 1.186 2011/12/17 20:05:39 tls Exp $ */
2
3/*-
4 * Copyright (c) 2003, 2007, 2008, 2009 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, and by Andrew Doran.
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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: init_sysctl.c,v 1.186 2011/12/17 20:05:39 tls Exp $");
34
35#include "opt_sysv.h"
36#include "opt_compat_netbsd.h"
37#include "opt_modular.h"
38#include "opt_sa.h"
39#include "opt_posix.h"
40#include "pty.h"
41
42#include <sys/types.h>
43#include <sys/param.h>
44#include <sys/sysctl.h>
45#include <sys/cpu.h>
46#include <sys/errno.h>
47#include <sys/systm.h>
48#include <sys/kernel.h>
49#include <sys/unistd.h>
50#include <sys/disklabel.h>
51#include <sys/cprng.h>
52#include <sys/vnode.h>
53#include <sys/mount.h>
54#include <sys/namei.h>
55#include <sys/msgbuf.h>
56#include <dev/cons.h>
57#include <sys/socketvar.h>
58#include <sys/file.h>
59#include <sys/filedesc.h>
60#include <sys/tty.h>
61#include <sys/kmem.h>
62#include <sys/resource.h>
63#include <sys/resourcevar.h>
64#include <sys/exec.h>
65#include <sys/conf.h>
66#include <sys/device.h>
67#include <sys/stat.h>
68#include <sys/kauth.h>
69#include <sys/ktrace.h>
70#include <sys/ksem.h>
71
72#ifdef COMPAT_50
73#include <compat/sys/time.h>
74#endif
75
76#ifdef KERN_SA
77#include <sys/sa.h>
78#endif
79
80#include <sys/cpu.h>
81
82#if defined(MODULAR) || defined(P1003_1B_SEMAPHORE)
83int posix_semaphores = 200112;
84#else
85int posix_semaphores;
86#endif
87
88int security_setidcore_dump;
89char security_setidcore_path[MAXPATHLEN] = "/var/crash/%n.core";
90uid_t security_setidcore_owner = 0;
91gid_t security_setidcore_group = 0;
92mode_t security_setidcore_mode = (S_IRUSR|S_IWUSR);
93
94static const u_int sysctl_flagmap[] = {
95	PK_ADVLOCK, P_ADVLOCK,
96	PK_EXEC, P_EXEC,
97	PK_NOCLDWAIT, P_NOCLDWAIT,
98	PK_32, P_32,
99	PK_CLDSIGIGN, P_CLDSIGIGN,
100	PK_SUGID, P_SUGID,
101	0
102};
103
104static const u_int sysctl_sflagmap[] = {
105	PS_NOCLDSTOP, P_NOCLDSTOP,
106	PS_WEXIT, P_WEXIT,
107	PS_STOPFORK, P_STOPFORK,
108	PS_STOPEXEC, P_STOPEXEC,
109	PS_STOPEXIT, P_STOPEXIT,
110	0
111};
112
113static const u_int sysctl_slflagmap[] = {
114	PSL_TRACED, P_TRACED,
115	PSL_FSTRACE, P_FSTRACE,
116	PSL_CHTRACED, P_CHTRACED,
117	PSL_SYSCALL, P_SYSCALL,
118	0
119};
120
121static const u_int sysctl_lflagmap[] = {
122	PL_CONTROLT, P_CONTROLT,
123	PL_PPWAIT, P_PPWAIT,
124	0
125};
126
127static const u_int sysctl_stflagmap[] = {
128	PST_PROFIL, P_PROFIL,
129	0
130
131};
132
133static const u_int sysctl_lwpprflagmap[] = {
134	LPR_DETACHED, L_DETACHED,
135	0
136};
137
138/*
139 * try over estimating by 5 procs/lwps
140 */
141#define KERN_LWPSLOP	(5 * sizeof(struct kinfo_lwp))
142
143static int dcopyout(struct lwp *, const void *, void *, size_t);
144
145static int
146dcopyout(struct lwp *l, const void *kaddr, void *uaddr, size_t len)
147{
148	int error;
149
150	error = copyout(kaddr, uaddr, len);
151	ktrmibio(-1, UIO_READ, uaddr, len, error);
152
153	return error;
154}
155
156#ifdef DIAGNOSTIC
157static int sysctl_kern_trigger_panic(SYSCTLFN_PROTO);
158#endif
159static int sysctl_kern_maxvnodes(SYSCTLFN_PROTO);
160static int sysctl_kern_rtc_offset(SYSCTLFN_PROTO);
161static int sysctl_kern_maxproc(SYSCTLFN_PROTO);
162static int sysctl_kern_hostid(SYSCTLFN_PROTO);
163static int sysctl_setlen(SYSCTLFN_PROTO);
164static int sysctl_kern_clockrate(SYSCTLFN_PROTO);
165static int sysctl_msgbuf(SYSCTLFN_PROTO);
166static int sysctl_kern_defcorename(SYSCTLFN_PROTO);
167static int sysctl_kern_cptime(SYSCTLFN_PROTO);
168#if NPTY > 0
169static int sysctl_kern_maxptys(SYSCTLFN_PROTO);
170#endif /* NPTY > 0 */
171static int sysctl_kern_sbmax(SYSCTLFN_PROTO);
172static int sysctl_kern_urnd(SYSCTLFN_PROTO);
173static int sysctl_kern_arnd(SYSCTLFN_PROTO);
174static int sysctl_kern_lwp(SYSCTLFN_PROTO);
175static int sysctl_kern_forkfsleep(SYSCTLFN_PROTO);
176static int sysctl_kern_root_partition(SYSCTLFN_PROTO);
177static int sysctl_kern_drivers(SYSCTLFN_PROTO);
178static int sysctl_security_setidcore(SYSCTLFN_PROTO);
179static int sysctl_security_setidcorename(SYSCTLFN_PROTO);
180static int sysctl_kern_cpid(SYSCTLFN_PROTO);
181static int sysctl_hw_usermem(SYSCTLFN_PROTO);
182static int sysctl_hw_cnmagic(SYSCTLFN_PROTO);
183
184static void fill_lwp(struct lwp *l, struct kinfo_lwp *kl);
185
186/*
187 * ********************************************************************
188 * section 1: setup routines
189 * ********************************************************************
190 * These functions are stuffed into a link set for sysctl setup
191 * functions. They're never called or referenced from anywhere else.
192 * ********************************************************************
193 */
194
195/*
196 * this setup routine is a replacement for kern_sysctl()
197 */
198SYSCTL_SETUP(sysctl_kern_setup, "sysctl kern subtree setup")
199{
200	extern int kern_logsigexit;	/* defined in kern/kern_sig.c */
201	extern fixpt_t ccpu;		/* defined in kern/kern_synch.c */
202	extern int dumponpanic;		/* defined in kern/subr_prf.c */
203	const struct sysctlnode *rnode;
204
205	sysctl_createv(clog, 0, NULL, NULL,
206		       CTLFLAG_PERMANENT,
207		       CTLTYPE_NODE, "kern", NULL,
208		       NULL, 0, NULL, 0,
209		       CTL_KERN, CTL_EOL);
210
211	sysctl_createv(clog, 0, NULL, NULL,
212		       CTLFLAG_PERMANENT,
213		       CTLTYPE_STRING, "ostype",
214		       SYSCTL_DESCR("Operating system type"),
215		       NULL, 0, __UNCONST(&ostype), 0,
216		       CTL_KERN, KERN_OSTYPE, CTL_EOL);
217	sysctl_createv(clog, 0, NULL, NULL,
218		       CTLFLAG_PERMANENT,
219		       CTLTYPE_STRING, "osrelease",
220		       SYSCTL_DESCR("Operating system release"),
221		       NULL, 0, __UNCONST(&osrelease), 0,
222		       CTL_KERN, KERN_OSRELEASE, CTL_EOL);
223	sysctl_createv(clog, 0, NULL, NULL,
224		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
225		       CTLTYPE_INT, "osrevision",
226		       SYSCTL_DESCR("Operating system revision"),
227		       NULL, __NetBSD_Version__, NULL, 0,
228		       CTL_KERN, KERN_OSREV, CTL_EOL);
229	sysctl_createv(clog, 0, NULL, NULL,
230		       CTLFLAG_PERMANENT,
231		       CTLTYPE_STRING, "version",
232		       SYSCTL_DESCR("Kernel version"),
233		       NULL, 0, __UNCONST(&version), 0,
234		       CTL_KERN, KERN_VERSION, CTL_EOL);
235	sysctl_createv(clog, 0, NULL, NULL,
236		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
237		       CTLTYPE_INT, "maxvnodes",
238		       SYSCTL_DESCR("Maximum number of vnodes"),
239		       sysctl_kern_maxvnodes, 0, NULL, 0,
240		       CTL_KERN, KERN_MAXVNODES, CTL_EOL);
241	sysctl_createv(clog, 0, NULL, NULL,
242		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
243		       CTLTYPE_INT, "maxproc",
244		       SYSCTL_DESCR("Maximum number of simultaneous processes"),
245		       sysctl_kern_maxproc, 0, NULL, 0,
246		       CTL_KERN, KERN_MAXPROC, CTL_EOL);
247	sysctl_createv(clog, 0, NULL, NULL,
248		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
249		       CTLTYPE_INT, "maxfiles",
250		       SYSCTL_DESCR("Maximum number of open files"),
251		       NULL, 0, &maxfiles, 0,
252		       CTL_KERN, KERN_MAXFILES, CTL_EOL);
253	sysctl_createv(clog, 0, NULL, NULL,
254		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
255		       CTLTYPE_INT, "argmax",
256		       SYSCTL_DESCR("Maximum number of bytes of arguments to "
257				    "execve(2)"),
258		       NULL, ARG_MAX, NULL, 0,
259		       CTL_KERN, KERN_ARGMAX, CTL_EOL);
260	sysctl_createv(clog, 0, NULL, NULL,
261		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
262		       CTLTYPE_STRING, "hostname",
263		       SYSCTL_DESCR("System hostname"),
264		       sysctl_setlen, 0, &hostname, MAXHOSTNAMELEN,
265		       CTL_KERN, KERN_HOSTNAME, CTL_EOL);
266	sysctl_createv(clog, 0, NULL, NULL,
267		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
268		       CTLTYPE_INT, "hostid",
269		       SYSCTL_DESCR("System host ID number"),
270		       sysctl_kern_hostid, 0, NULL, 0,
271		       CTL_KERN, KERN_HOSTID, CTL_EOL);
272	sysctl_createv(clog, 0, NULL, NULL,
273		       CTLFLAG_PERMANENT,
274		       CTLTYPE_STRUCT, "clockrate",
275		       SYSCTL_DESCR("Kernel clock rates"),
276		       sysctl_kern_clockrate, 0, NULL,
277		       sizeof(struct clockinfo),
278		       CTL_KERN, KERN_CLOCKRATE, CTL_EOL);
279	sysctl_createv(clog, 0, NULL, NULL,
280		       CTLFLAG_PERMANENT,
281		       CTLTYPE_INT, "hardclock_ticks",
282		       SYSCTL_DESCR("Number of hardclock ticks"),
283		       NULL, 0, &hardclock_ticks, sizeof(hardclock_ticks),
284		       CTL_KERN, KERN_HARDCLOCK_TICKS, CTL_EOL);
285	sysctl_createv(clog, 0, NULL, NULL,
286		       CTLFLAG_PERMANENT,
287		       CTLTYPE_STRUCT, "vnode",
288		       SYSCTL_DESCR("System vnode table"),
289		       sysctl_kern_vnode, 0, NULL, 0,
290		       CTL_KERN, KERN_VNODE, CTL_EOL);
291#ifndef GPROF
292	sysctl_createv(clog, 0, NULL, NULL,
293		       CTLFLAG_PERMANENT,
294		       CTLTYPE_NODE, "profiling",
295		       SYSCTL_DESCR("Profiling information (not available)"),
296		       sysctl_notavail, 0, NULL, 0,
297		       CTL_KERN, KERN_PROF, CTL_EOL);
298#endif
299	sysctl_createv(clog, 0, NULL, NULL,
300		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
301		       CTLTYPE_INT, "posix1version",
302		       SYSCTL_DESCR("Version of ISO/IEC 9945 (POSIX 1003.1) "
303				    "with which the operating system attempts "
304				    "to comply"),
305		       NULL, _POSIX_VERSION, NULL, 0,
306		       CTL_KERN, KERN_POSIX1, CTL_EOL);
307	sysctl_createv(clog, 0, NULL, NULL,
308		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
309		       CTLTYPE_INT, "ngroups",
310		       SYSCTL_DESCR("Maximum number of supplemental groups"),
311		       NULL, NGROUPS_MAX, NULL, 0,
312		       CTL_KERN, KERN_NGROUPS, CTL_EOL);
313	sysctl_createv(clog, 0, NULL, NULL,
314		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
315		       CTLTYPE_INT, "job_control",
316		       SYSCTL_DESCR("Whether job control is available"),
317		       NULL, 1, NULL, 0,
318		       CTL_KERN, KERN_JOB_CONTROL, CTL_EOL);
319	sysctl_createv(clog, 0, NULL, NULL,
320		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
321		       CTLTYPE_INT, "saved_ids",
322		       SYSCTL_DESCR("Whether POSIX saved set-group/user ID is "
323				    "available"), NULL,
324#ifdef _POSIX_SAVED_IDS
325		       1,
326#else /* _POSIX_SAVED_IDS */
327		       0,
328#endif /* _POSIX_SAVED_IDS */
329		       NULL, 0, CTL_KERN, KERN_SAVED_IDS, CTL_EOL);
330	sysctl_createv(clog, 0, NULL, NULL,
331		       CTLFLAG_PERMANENT|CTLFLAG_HEX,
332		       CTLTYPE_INT, "boothowto",
333		       SYSCTL_DESCR("Flags from boot loader"),
334		       NULL, 0, &boothowto, sizeof(boothowto),
335		       CTL_KERN, CTL_CREATE, CTL_EOL);
336	sysctl_createv(clog, 0, NULL, NULL,
337		       CTLFLAG_PERMANENT,
338		       CTLTYPE_STRUCT, "boottime",
339		       SYSCTL_DESCR("System boot time"),
340		       NULL, 0, &boottime, sizeof(boottime),
341		       CTL_KERN, KERN_BOOTTIME, CTL_EOL);
342#ifdef COMPAT_50
343	{
344		extern struct timeval50 boottime50;
345		sysctl_createv(clog, 0, NULL, NULL,
346			       CTLFLAG_PERMANENT,
347			       CTLTYPE_STRUCT, "oboottime",
348			       SYSCTL_DESCR("System boot time"),
349			       NULL, 0, &boottime50, sizeof(boottime50),
350			       CTL_KERN, KERN_OBOOTTIME, CTL_EOL);
351	}
352#endif
353	sysctl_createv(clog, 0, NULL, NULL,
354		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
355		       CTLTYPE_STRING, "domainname",
356		       SYSCTL_DESCR("YP domain name"),
357		       sysctl_setlen, 0, &domainname, MAXHOSTNAMELEN,
358		       CTL_KERN, KERN_DOMAINNAME, CTL_EOL);
359	sysctl_createv(clog, 0, NULL, NULL,
360		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
361		       CTLTYPE_INT, "maxpartitions",
362		       SYSCTL_DESCR("Maximum number of partitions allowed per "
363				    "disk"),
364		       NULL, MAXPARTITIONS, NULL, 0,
365		       CTL_KERN, KERN_MAXPARTITIONS, CTL_EOL);
366	sysctl_createv(clog, 0, NULL, NULL,
367		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
368		       CTLTYPE_INT, "rawpartition",
369		       SYSCTL_DESCR("Raw partition of a disk"),
370		       NULL, RAW_PART, NULL, 0,
371		       CTL_KERN, KERN_RAWPARTITION, CTL_EOL);
372	sysctl_createv(clog, 0, NULL, NULL,
373		       CTLFLAG_PERMANENT,
374		       CTLTYPE_STRUCT, "timex", NULL,
375		       sysctl_notavail, 0, NULL, 0,
376		       CTL_KERN, KERN_TIMEX, CTL_EOL);
377	sysctl_createv(clog, 0, NULL, NULL,
378		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
379		       CTLTYPE_INT, "rtc_offset",
380		       SYSCTL_DESCR("Offset of real time clock from UTC in "
381				    "minutes"),
382		       sysctl_kern_rtc_offset, 0, &rtc_offset, 0,
383		       CTL_KERN, KERN_RTC_OFFSET, CTL_EOL);
384	sysctl_createv(clog, 0, NULL, NULL,
385		       CTLFLAG_PERMANENT,
386		       CTLTYPE_STRING, "root_device",
387		       SYSCTL_DESCR("Name of the root device"),
388		       sysctl_root_device, 0, NULL, 0,
389		       CTL_KERN, KERN_ROOT_DEVICE, CTL_EOL);
390	sysctl_createv(clog, 0, NULL, NULL,
391		       CTLFLAG_PERMANENT,
392		       CTLTYPE_INT, "msgbufsize",
393		       SYSCTL_DESCR("Size of the kernel message buffer"),
394		       sysctl_msgbuf, 0, NULL, 0,
395		       CTL_KERN, KERN_MSGBUFSIZE, CTL_EOL);
396	sysctl_createv(clog, 0, NULL, NULL,
397		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
398		       CTLTYPE_INT, "fsync",
399		       SYSCTL_DESCR("Whether the POSIX 1003.1b File "
400				    "Synchronization Option is available on "
401				    "this system"),
402		       NULL, 1, NULL, 0,
403		       CTL_KERN, KERN_FSYNC, CTL_EOL);
404	sysctl_createv(clog, 0, NULL, NULL,
405		       CTLFLAG_PERMANENT,
406		       CTLTYPE_NODE, "ipc",
407		       SYSCTL_DESCR("SysV IPC options"),
408		       NULL, 0, NULL, 0,
409		       CTL_KERN, KERN_SYSVIPC, CTL_EOL);
410	sysctl_createv(clog, 0, NULL, NULL,
411		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
412		       CTLTYPE_INT, "sysvmsg",
413		       SYSCTL_DESCR("System V style message support available"),
414		       NULL,
415#ifdef SYSVMSG
416		       1,
417#else /* SYSVMSG */
418		       0,
419#endif /* SYSVMSG */
420		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_MSG, CTL_EOL);
421	sysctl_createv(clog, 0, NULL, NULL,
422		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
423		       CTLTYPE_INT, "sysvsem",
424		       SYSCTL_DESCR("System V style semaphore support "
425				    "available"), NULL,
426#ifdef SYSVSEM
427		       1,
428#else /* SYSVSEM */
429		       0,
430#endif /* SYSVSEM */
431		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SEM, CTL_EOL);
432	sysctl_createv(clog, 0, NULL, NULL,
433		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
434		       CTLTYPE_INT, "sysvshm",
435		       SYSCTL_DESCR("System V style shared memory support "
436				    "available"), NULL,
437#ifdef SYSVSHM
438		       1,
439#else /* SYSVSHM */
440		       0,
441#endif /* SYSVSHM */
442		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHM, CTL_EOL);
443	sysctl_createv(clog, 0, NULL, NULL,
444		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
445		       CTLTYPE_INT, "synchronized_io",
446		       SYSCTL_DESCR("Whether the POSIX 1003.1b Synchronized "
447				    "I/O Option is available on this system"),
448		       NULL, 1, NULL, 0,
449		       CTL_KERN, KERN_SYNCHRONIZED_IO, CTL_EOL);
450	sysctl_createv(clog, 0, NULL, NULL,
451		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
452		       CTLTYPE_INT, "iov_max",
453		       SYSCTL_DESCR("Maximum number of iovec structures per "
454				    "process"),
455		       NULL, IOV_MAX, NULL, 0,
456		       CTL_KERN, KERN_IOV_MAX, CTL_EOL);
457	sysctl_createv(clog, 0, NULL, NULL,
458		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
459		       CTLTYPE_INT, "mapped_files",
460		       SYSCTL_DESCR("Whether the POSIX 1003.1b Memory Mapped "
461				    "Files Option is available on this system"),
462		       NULL, 1, NULL, 0,
463		       CTL_KERN, KERN_MAPPED_FILES, CTL_EOL);
464	sysctl_createv(clog, 0, NULL, NULL,
465		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
466		       CTLTYPE_INT, "memlock",
467		       SYSCTL_DESCR("Whether the POSIX 1003.1b Process Memory "
468				    "Locking Option is available on this "
469				    "system"),
470		       NULL, 1, NULL, 0,
471		       CTL_KERN, KERN_MEMLOCK, CTL_EOL);
472	sysctl_createv(clog, 0, NULL, NULL,
473		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
474		       CTLTYPE_INT, "memlock_range",
475		       SYSCTL_DESCR("Whether the POSIX 1003.1b Range Memory "
476				    "Locking Option is available on this "
477				    "system"),
478		       NULL, 1, NULL, 0,
479		       CTL_KERN, KERN_MEMLOCK_RANGE, CTL_EOL);
480	sysctl_createv(clog, 0, NULL, NULL,
481		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
482		       CTLTYPE_INT, "memory_protection",
483		       SYSCTL_DESCR("Whether the POSIX 1003.1b Memory "
484				    "Protection Option is available on this "
485				    "system"),
486		       NULL, 1, NULL, 0,
487		       CTL_KERN, KERN_MEMORY_PROTECTION, CTL_EOL);
488	sysctl_createv(clog, 0, NULL, NULL,
489		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
490		       CTLTYPE_INT, "login_name_max",
491		       SYSCTL_DESCR("Maximum login name length"),
492		       NULL, LOGIN_NAME_MAX, NULL, 0,
493		       CTL_KERN, KERN_LOGIN_NAME_MAX, CTL_EOL);
494	sysctl_createv(clog, 0, NULL, NULL,
495		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
496		       CTLTYPE_STRING, "defcorename",
497		       SYSCTL_DESCR("Default core file name"),
498		       sysctl_kern_defcorename, 0, defcorename, MAXPATHLEN,
499		       CTL_KERN, KERN_DEFCORENAME, CTL_EOL);
500	sysctl_createv(clog, 0, NULL, NULL,
501		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
502		       CTLTYPE_INT, "logsigexit",
503		       SYSCTL_DESCR("Log process exit when caused by signals"),
504		       NULL, 0, &kern_logsigexit, 0,
505		       CTL_KERN, KERN_LOGSIGEXIT, CTL_EOL);
506	sysctl_createv(clog, 0, NULL, NULL,
507		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
508		       CTLTYPE_INT, "fscale",
509		       SYSCTL_DESCR("Kernel fixed-point scale factor"),
510		       NULL, FSCALE, NULL, 0,
511		       CTL_KERN, KERN_FSCALE, CTL_EOL);
512	sysctl_createv(clog, 0, NULL, NULL,
513		       CTLFLAG_PERMANENT,
514		       CTLTYPE_INT, "ccpu",
515		       SYSCTL_DESCR("Scheduler exponential decay value"),
516		       NULL, 0, &ccpu, 0,
517		       CTL_KERN, KERN_CCPU, CTL_EOL);
518	sysctl_createv(clog, 0, NULL, NULL,
519		       CTLFLAG_PERMANENT,
520		       CTLTYPE_STRUCT, "cp_time",
521		       SYSCTL_DESCR("Clock ticks spent in different CPU states"),
522		       sysctl_kern_cptime, 0, NULL, 0,
523		       CTL_KERN, KERN_CP_TIME, CTL_EOL);
524	sysctl_createv(clog, 0, NULL, NULL,
525		       CTLFLAG_PERMANENT,
526		       CTLTYPE_INT, "msgbuf",
527		       SYSCTL_DESCR("Kernel message buffer"),
528		       sysctl_msgbuf, 0, NULL, 0,
529		       CTL_KERN, KERN_MSGBUF, CTL_EOL);
530	sysctl_createv(clog, 0, NULL, NULL,
531		       CTLFLAG_PERMANENT,
532		       CTLTYPE_STRUCT, "consdev",
533		       SYSCTL_DESCR("Console device"),
534		       sysctl_consdev, 0, NULL, sizeof(dev_t),
535		       CTL_KERN, KERN_CONSDEV, CTL_EOL);
536#if NPTY > 0
537	sysctl_createv(clog, 0, NULL, NULL,
538		       CTLFLAG_PERMANENT,
539		       CTLTYPE_INT, "maxptys",
540		       SYSCTL_DESCR("Maximum number of pseudo-ttys"),
541		       sysctl_kern_maxptys, 0, NULL, 0,
542		       CTL_KERN, KERN_MAXPTYS, CTL_EOL);
543#endif /* NPTY > 0 */
544	sysctl_createv(clog, 0, NULL, NULL,
545		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
546		       CTLTYPE_INT, "maxphys",
547		       SYSCTL_DESCR("Maximum raw I/O transfer size"),
548		       NULL, MAXPHYS, NULL, 0,
549		       CTL_KERN, KERN_MAXPHYS, CTL_EOL);
550	sysctl_createv(clog, 0, NULL, NULL,
551		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
552		       CTLTYPE_INT, "sbmax",
553		       SYSCTL_DESCR("Maximum socket buffer size"),
554		       sysctl_kern_sbmax, 0, NULL, 0,
555		       CTL_KERN, KERN_SBMAX, CTL_EOL);
556	sysctl_createv(clog, 0, NULL, NULL,
557		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
558		       CTLTYPE_INT, "monotonic_clock",
559		       SYSCTL_DESCR("Implementation version of the POSIX "
560				    "1003.1b Monotonic Clock Option"),
561		       /* XXX _POSIX_VERSION */
562		       NULL, _POSIX_MONOTONIC_CLOCK, NULL, 0,
563		       CTL_KERN, KERN_MONOTONIC_CLOCK, CTL_EOL);
564	sysctl_createv(clog, 0, NULL, NULL,
565		       CTLFLAG_PERMANENT,
566		       CTLTYPE_INT, "urandom",
567		       SYSCTL_DESCR("Random integer value"),
568		       sysctl_kern_urnd, 0, NULL, 0,
569		       CTL_KERN, KERN_URND, CTL_EOL);
570	sysctl_createv(clog, 0, NULL, NULL,
571		       CTLFLAG_PERMANENT,
572		       CTLTYPE_INT, "arandom",
573		       SYSCTL_DESCR("n bytes of random data"),
574		       sysctl_kern_arnd, 0, NULL, 0,
575		       CTL_KERN, KERN_ARND, CTL_EOL);
576	sysctl_createv(clog, 0, NULL, NULL,
577		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
578		       CTLTYPE_INT, "labelsector",
579		       SYSCTL_DESCR("Sector number containing the disklabel"),
580		       NULL, LABELSECTOR, NULL, 0,
581		       CTL_KERN, KERN_LABELSECTOR, CTL_EOL);
582	sysctl_createv(clog, 0, NULL, NULL,
583		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
584		       CTLTYPE_INT, "labeloffset",
585		       SYSCTL_DESCR("Offset of the disklabel within the "
586				    "sector"),
587		       NULL, LABELOFFSET, NULL, 0,
588		       CTL_KERN, KERN_LABELOFFSET, CTL_EOL);
589	sysctl_createv(clog, 0, NULL, NULL,
590		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
591		       CTLTYPE_INT, "labelusesmbr",
592		       SYSCTL_DESCR("disklabel is inside MBR partition"),
593		       NULL, LABELUSESMBR, NULL, 0,
594		       CTL_KERN, CTL_CREATE, CTL_EOL);
595	sysctl_createv(clog, 0, NULL, NULL,
596		       CTLFLAG_PERMANENT,
597		       CTLTYPE_NODE, "lwp",
598		       SYSCTL_DESCR("System-wide LWP information"),
599		       sysctl_kern_lwp, 0, NULL, 0,
600		       CTL_KERN, KERN_LWP, CTL_EOL);
601	sysctl_createv(clog, 0, NULL, NULL,
602		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
603		       CTLTYPE_INT, "forkfsleep",
604		       SYSCTL_DESCR("Milliseconds to sleep on fork failure due "
605				    "to process limits"),
606		       sysctl_kern_forkfsleep, 0, NULL, 0,
607		       CTL_KERN, KERN_FORKFSLEEP, CTL_EOL);
608	sysctl_createv(clog, 0, NULL, NULL,
609		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
610		       CTLTYPE_INT, "posix_threads",
611		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
612				    "Threads option to which the system "
613				    "attempts to conform"),
614		       /* XXX _POSIX_VERSION */
615		       NULL, _POSIX_THREADS, NULL, 0,
616		       CTL_KERN, KERN_POSIX_THREADS, CTL_EOL);
617	sysctl_createv(clog, 0, NULL, NULL,
618		       CTLFLAG_PERMANENT,
619		       CTLTYPE_INT, "posix_semaphores",
620		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
621				    "Semaphores option to which the system "
622				    "attempts to conform"), NULL,
623		       0, &posix_semaphores,
624		       0, CTL_KERN, KERN_POSIX_SEMAPHORES, CTL_EOL);
625	sysctl_createv(clog, 0, NULL, NULL,
626		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
627		       CTLTYPE_INT, "posix_barriers",
628		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
629				    "Barriers option to which the system "
630				    "attempts to conform"),
631		       /* XXX _POSIX_VERSION */
632		       NULL, _POSIX_BARRIERS, NULL, 0,
633		       CTL_KERN, KERN_POSIX_BARRIERS, CTL_EOL);
634	sysctl_createv(clog, 0, NULL, NULL,
635		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
636		       CTLTYPE_INT, "posix_timers",
637		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
638				    "Timers option to which the system "
639				    "attempts to conform"),
640		       /* XXX _POSIX_VERSION */
641		       NULL, _POSIX_TIMERS, NULL, 0,
642		       CTL_KERN, KERN_POSIX_TIMERS, CTL_EOL);
643	sysctl_createv(clog, 0, NULL, NULL,
644		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
645		       CTLTYPE_INT, "posix_spin_locks",
646		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its Spin "
647				    "Locks option to which the system attempts "
648				    "to conform"),
649		       /* XXX _POSIX_VERSION */
650		       NULL, _POSIX_SPIN_LOCKS, NULL, 0,
651		       CTL_KERN, KERN_POSIX_SPIN_LOCKS, CTL_EOL);
652	sysctl_createv(clog, 0, NULL, NULL,
653		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
654		       CTLTYPE_INT, "posix_reader_writer_locks",
655		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
656				    "Read-Write Locks option to which the "
657				    "system attempts to conform"),
658		       /* XXX _POSIX_VERSION */
659		       NULL, _POSIX_READER_WRITER_LOCKS, NULL, 0,
660		       CTL_KERN, KERN_POSIX_READER_WRITER_LOCKS, CTL_EOL);
661	sysctl_createv(clog, 0, NULL, NULL,
662		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
663		       CTLTYPE_INT, "dump_on_panic",
664		       SYSCTL_DESCR("Perform a crash dump on system panic"),
665		       NULL, 0, &dumponpanic, 0,
666		       CTL_KERN, KERN_DUMP_ON_PANIC, CTL_EOL);
667#ifdef DIAGNOSTIC
668	sysctl_createv(clog, 0, NULL, NULL,
669		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
670		       CTLTYPE_INT, "panic_now",
671		       SYSCTL_DESCR("Trigger a panic"),
672		       sysctl_kern_trigger_panic, 0, NULL, 0,
673		       CTL_KERN, CTL_CREATE, CTL_EOL);
674#endif
675	sysctl_createv(clog, 0, NULL, NULL,
676		       CTLFLAG_PERMANENT,
677		       CTLTYPE_INT, "root_partition",
678		       SYSCTL_DESCR("Root partition on the root device"),
679		       sysctl_kern_root_partition, 0, NULL, 0,
680		       CTL_KERN, KERN_ROOT_PARTITION, CTL_EOL);
681	sysctl_createv(clog, 0, NULL, NULL,
682		       CTLFLAG_PERMANENT,
683		       CTLTYPE_STRUCT, "drivers",
684		       SYSCTL_DESCR("List of all drivers with block and "
685				    "character device numbers"),
686		       sysctl_kern_drivers, 0, NULL, 0,
687		       CTL_KERN, KERN_DRIVERS, CTL_EOL);
688	sysctl_createv(clog, 0, NULL, NULL,
689		       CTLFLAG_PERMANENT,
690		       CTLTYPE_STRUCT, "cp_id",
691		       SYSCTL_DESCR("Mapping of CPU number to CPU id"),
692		       sysctl_kern_cpid, 0, NULL, 0,
693		       CTL_KERN, KERN_CP_ID, CTL_EOL);
694	sysctl_createv(clog, 0, NULL, &rnode,
695		       CTLFLAG_PERMANENT,
696		       CTLTYPE_NODE, "coredump",
697		       SYSCTL_DESCR("Coredump settings."),
698		       NULL, 0, NULL, 0,
699		       CTL_KERN, CTL_CREATE, CTL_EOL);
700	sysctl_createv(clog, 0, &rnode, &rnode,
701		       CTLFLAG_PERMANENT,
702		       CTLTYPE_NODE, "setid",
703		       SYSCTL_DESCR("Set-id processes' coredump settings."),
704		       NULL, 0, NULL, 0,
705		       CTL_CREATE, CTL_EOL);
706	sysctl_createv(clog, 0, &rnode, NULL,
707		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
708		       CTLTYPE_INT, "dump",
709		       SYSCTL_DESCR("Allow set-id processes to dump core."),
710		       sysctl_security_setidcore, 0, &security_setidcore_dump,
711		       sizeof(security_setidcore_dump),
712		       CTL_CREATE, CTL_EOL);
713	sysctl_createv(clog, 0, &rnode, NULL,
714		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
715		       CTLTYPE_STRING, "path",
716		       SYSCTL_DESCR("Path pattern for set-id coredumps."),
717		       sysctl_security_setidcorename, 0,
718		       &security_setidcore_path,
719		       sizeof(security_setidcore_path),
720		       CTL_CREATE, CTL_EOL);
721	sysctl_createv(clog, 0, &rnode, NULL,
722		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
723		       CTLTYPE_INT, "owner",
724		       SYSCTL_DESCR("Owner id for set-id processes' cores."),
725		       sysctl_security_setidcore, 0, &security_setidcore_owner,
726		       0,
727		       CTL_CREATE, CTL_EOL);
728	sysctl_createv(clog, 0, &rnode, NULL,
729		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
730		       CTLTYPE_INT, "group",
731		       SYSCTL_DESCR("Group id for set-id processes' cores."),
732		       sysctl_security_setidcore, 0, &security_setidcore_group,
733		       0,
734		       CTL_CREATE, CTL_EOL);
735	sysctl_createv(clog, 0, &rnode, NULL,
736		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
737		       CTLTYPE_INT, "mode",
738		       SYSCTL_DESCR("Mode for set-id processes' cores."),
739		       sysctl_security_setidcore, 0, &security_setidcore_mode,
740		       0,
741		       CTL_CREATE, CTL_EOL);
742	sysctl_createv(clog, 0, NULL, NULL,
743#ifndef KERN_SA
744		       CTLFLAG_IMMEDIATE|
745#endif
746		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
747		       CTLTYPE_INT, "no_sa_support",
748		       SYSCTL_DESCR("0 if the kernel supports SA, otherwise "
749		       "it doesn't"),
750		       NULL,
751#ifndef KERN_SA
752		       1, NULL,
753#else
754		       0, &sa_system_disabled,
755#endif
756		       0,
757		       CTL_KERN, CTL_CREATE, CTL_EOL);
758	/* kern.posix. */
759	sysctl_createv(clog, 0, NULL, &rnode,
760			CTLFLAG_PERMANENT,
761			CTLTYPE_NODE, "posix",
762			SYSCTL_DESCR("POSIX options"),
763			NULL, 0, NULL, 0,
764			CTL_KERN, CTL_CREATE, CTL_EOL);
765	sysctl_createv(clog, 0, &rnode, NULL,
766			CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
767			CTLTYPE_INT, "semmax",
768			SYSCTL_DESCR("Maximal number of semaphores"),
769			NULL, 0, &ksem_max, 0,
770			CTL_CREATE, CTL_EOL);
771}
772
773SYSCTL_SETUP(sysctl_hw_setup, "sysctl hw subtree setup")
774{
775	u_int u;
776	u_quad_t q;
777
778	sysctl_createv(clog, 0, NULL, NULL,
779		       CTLFLAG_PERMANENT,
780		       CTLTYPE_NODE, "hw", NULL,
781		       NULL, 0, NULL, 0,
782		       CTL_HW, CTL_EOL);
783
784	sysctl_createv(clog, 0, NULL, NULL,
785		       CTLFLAG_PERMANENT,
786		       CTLTYPE_STRING, "machine",
787		       SYSCTL_DESCR("Machine class"),
788		       NULL, 0, machine, 0,
789		       CTL_HW, HW_MACHINE, CTL_EOL);
790	sysctl_createv(clog, 0, NULL, NULL,
791		       CTLFLAG_PERMANENT,
792		       CTLTYPE_STRING, "model",
793		       SYSCTL_DESCR("Machine model"),
794		       NULL, 0, cpu_model, 0,
795		       CTL_HW, HW_MODEL, CTL_EOL);
796	sysctl_createv(clog, 0, NULL, NULL,
797		       CTLFLAG_PERMANENT,
798		       CTLTYPE_INT, "ncpu",
799		       SYSCTL_DESCR("Number of CPUs configured"),
800		       NULL, 0, &ncpu, 0,
801		       CTL_HW, HW_NCPU, CTL_EOL);
802	sysctl_createv(clog, 0, NULL, NULL,
803		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
804		       CTLTYPE_INT, "byteorder",
805		       SYSCTL_DESCR("System byte order"),
806		       NULL, BYTE_ORDER, NULL, 0,
807		       CTL_HW, HW_BYTEORDER, CTL_EOL);
808	u = ((u_int)physmem > (UINT_MAX / PAGE_SIZE)) ?
809		UINT_MAX : physmem * PAGE_SIZE;
810	sysctl_createv(clog, 0, NULL, NULL,
811		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
812		       CTLTYPE_INT, "physmem",
813		       SYSCTL_DESCR("Bytes of physical memory"),
814		       NULL, u, NULL, 0,
815		       CTL_HW, HW_PHYSMEM, CTL_EOL);
816	sysctl_createv(clog, 0, NULL, NULL,
817		       CTLFLAG_PERMANENT,
818		       CTLTYPE_INT, "usermem",
819		       SYSCTL_DESCR("Bytes of non-kernel memory"),
820		       sysctl_hw_usermem, 0, NULL, 0,
821		       CTL_HW, HW_USERMEM, CTL_EOL);
822	sysctl_createv(clog, 0, NULL, NULL,
823		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
824		       CTLTYPE_INT, "pagesize",
825		       SYSCTL_DESCR("Software page size"),
826		       NULL, PAGE_SIZE, NULL, 0,
827		       CTL_HW, HW_PAGESIZE, CTL_EOL);
828	sysctl_createv(clog, 0, NULL, NULL,
829		       CTLFLAG_PERMANENT,
830		       CTLTYPE_STRING, "machine_arch",
831		       SYSCTL_DESCR("Machine CPU class"),
832		       NULL, 0, machine_arch, 0,
833		       CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
834	sysctl_createv(clog, 0, NULL, NULL,
835		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
836		       CTLTYPE_INT, "alignbytes",
837		       SYSCTL_DESCR("Alignment constraint for all possible "
838				    "data types"),
839		       NULL, ALIGNBYTES, NULL, 0,
840		       CTL_HW, HW_ALIGNBYTES, CTL_EOL);
841	sysctl_createv(clog, 0, NULL, NULL,
842		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
843		       CTLTYPE_STRING, "cnmagic",
844		       SYSCTL_DESCR("Console magic key sequence"),
845		       sysctl_hw_cnmagic, 0, NULL, CNS_LEN,
846		       CTL_HW, HW_CNMAGIC, CTL_EOL);
847	q = (u_quad_t)physmem * PAGE_SIZE;
848	sysctl_createv(clog, 0, NULL, NULL,
849		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
850		       CTLTYPE_QUAD, "physmem64",
851		       SYSCTL_DESCR("Bytes of physical memory"),
852		       NULL, q, NULL, 0,
853		       CTL_HW, HW_PHYSMEM64, CTL_EOL);
854	sysctl_createv(clog, 0, NULL, NULL,
855		       CTLFLAG_PERMANENT,
856		       CTLTYPE_QUAD, "usermem64",
857		       SYSCTL_DESCR("Bytes of non-kernel memory"),
858		       sysctl_hw_usermem, 0, NULL, 0,
859		       CTL_HW, HW_USERMEM64, CTL_EOL);
860	sysctl_createv(clog, 0, NULL, NULL,
861		       CTLFLAG_PERMANENT,
862		       CTLTYPE_INT, "ncpuonline",
863		       SYSCTL_DESCR("Number of CPUs online"),
864		       NULL, 0, &ncpuonline, 0,
865		       CTL_HW, HW_NCPUONLINE, CTL_EOL);
866}
867
868#ifdef DEBUG
869/*
870 * Debugging related system variables.
871 */
872struct ctldebug /* debug0, */ /* debug1, */ debug2, debug3, debug4;
873struct ctldebug debug5, debug6, debug7, debug8, debug9;
874struct ctldebug debug10, debug11, debug12, debug13, debug14;
875struct ctldebug debug15, debug16, debug17, debug18, debug19;
876static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
877	&debug0, &debug1, &debug2, &debug3, &debug4,
878	&debug5, &debug6, &debug7, &debug8, &debug9,
879	&debug10, &debug11, &debug12, &debug13, &debug14,
880	&debug15, &debug16, &debug17, &debug18, &debug19,
881};
882
883/*
884 * this setup routine is a replacement for debug_sysctl()
885 *
886 * note that it creates several nodes per defined debug variable
887 */
888SYSCTL_SETUP(sysctl_debug_setup, "sysctl debug subtree setup")
889{
890	struct ctldebug *cdp;
891	char nodename[20];
892	int i;
893
894	/*
895	 * two ways here:
896	 *
897	 * the "old" way (debug.name -> value) which was emulated by
898	 * the sysctl(8) binary
899	 *
900	 * the new way, which the sysctl(8) binary was actually using
901
902	 node	debug
903	 node	debug.0
904	 string debug.0.name
905	 int	debug.0.value
906	 int	debug.name
907
908	 */
909
910	sysctl_createv(clog, 0, NULL, NULL,
911		       CTLFLAG_PERMANENT,
912		       CTLTYPE_NODE, "debug", NULL,
913		       NULL, 0, NULL, 0,
914		       CTL_DEBUG, CTL_EOL);
915
916	for (i = 0; i < CTL_DEBUG_MAXID; i++) {
917		cdp = debugvars[i];
918		if (cdp->debugname == NULL || cdp->debugvar == NULL)
919			continue;
920
921		snprintf(nodename, sizeof(nodename), "debug%d", i);
922		sysctl_createv(clog, 0, NULL, NULL,
923			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
924			       CTLTYPE_NODE, nodename, NULL,
925			       NULL, 0, NULL, 0,
926			       CTL_DEBUG, i, CTL_EOL);
927		sysctl_createv(clog, 0, NULL, NULL,
928			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
929			       CTLTYPE_STRING, "name", NULL,
930			       /*XXXUNCONST*/
931			       NULL, 0, __UNCONST(cdp->debugname), 0,
932			       CTL_DEBUG, i, CTL_DEBUG_NAME, CTL_EOL);
933		sysctl_createv(clog, 0, NULL, NULL,
934			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
935			       CTLTYPE_INT, "value", NULL,
936			       NULL, 0, cdp->debugvar, 0,
937			       CTL_DEBUG, i, CTL_DEBUG_VALUE, CTL_EOL);
938		sysctl_createv(clog, 0, NULL, NULL,
939			       CTLFLAG_PERMANENT,
940			       CTLTYPE_INT, cdp->debugname, NULL,
941			       NULL, 0, cdp->debugvar, 0,
942			       CTL_DEBUG, CTL_CREATE, CTL_EOL);
943	}
944}
945#endif /* DEBUG */
946
947/*
948 * ********************************************************************
949 * section 2: private node-specific helper routines.
950 * ********************************************************************
951 */
952
953#ifdef DIAGNOSTIC
954static int
955sysctl_kern_trigger_panic(SYSCTLFN_ARGS)
956{
957	int newtrig, error;
958	struct sysctlnode node;
959
960	newtrig = 0;
961	node = *rnode;
962	node.sysctl_data = &newtrig;
963	error = sysctl_lookup(SYSCTLFN_CALL(&node));
964	if (error || newp == NULL)
965		return (error);
966
967	if (newtrig != 0)
968		panic("Panic triggered");
969
970	return (error);
971}
972#endif
973
974/*
975 * sysctl helper routine for kern.maxvnodes.  Drain vnodes if
976 * new value is lower than desiredvnodes and then calls reinit
977 * routines that needs to adjust to the new value.
978 */
979static int
980sysctl_kern_maxvnodes(SYSCTLFN_ARGS)
981{
982	int error, new_vnodes, old_vnodes, new_max;
983	struct sysctlnode node;
984
985	new_vnodes = desiredvnodes;
986	node = *rnode;
987	node.sysctl_data = &new_vnodes;
988	error = sysctl_lookup(SYSCTLFN_CALL(&node));
989	if (error || newp == NULL)
990		return (error);
991
992	/* Limits: 75% of KVA and physical memory. */
993	new_max = calc_cache_size(kernel_map, 75, 75) / VNODE_COST;
994	if (new_vnodes > new_max)
995		new_vnodes = new_max;
996
997	old_vnodes = desiredvnodes;
998	desiredvnodes = new_vnodes;
999	if (new_vnodes < old_vnodes) {
1000		error = vfs_drainvnodes(new_vnodes);
1001		if (error) {
1002			desiredvnodes = old_vnodes;
1003			return (error);
1004		}
1005	}
1006	vfs_reinit();
1007	nchreinit();
1008
1009	return (0);
1010}
1011
1012/*
1013 * sysctl helper routine for rtc_offset - set time after changes
1014 */
1015static int
1016sysctl_kern_rtc_offset(SYSCTLFN_ARGS)
1017{
1018	struct timespec ts, delta;
1019	int error, new_rtc_offset;
1020	struct sysctlnode node;
1021
1022	new_rtc_offset = rtc_offset;
1023	node = *rnode;
1024	node.sysctl_data = &new_rtc_offset;
1025	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1026	if (error || newp == NULL)
1027		return (error);
1028
1029	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
1030	    KAUTH_REQ_SYSTEM_TIME_RTCOFFSET,
1031	    KAUTH_ARG(new_rtc_offset), NULL, NULL))
1032		return (EPERM);
1033	if (rtc_offset == new_rtc_offset)
1034		return (0);
1035
1036	/* if we change the offset, adjust the time */
1037	nanotime(&ts);
1038	delta.tv_sec = 60 * (new_rtc_offset - rtc_offset);
1039	delta.tv_nsec = 0;
1040	timespecadd(&ts, &delta, &ts);
1041	rtc_offset = new_rtc_offset;
1042	return (settime(l->l_proc, &ts));
1043}
1044
1045/*
1046 * sysctl helper routine for kern.maxproc. Ensures that the new
1047 * values are not too low or too high.
1048 */
1049static int
1050sysctl_kern_maxproc(SYSCTLFN_ARGS)
1051{
1052	int error, nmaxproc;
1053	struct sysctlnode node;
1054
1055	nmaxproc = maxproc;
1056	node = *rnode;
1057	node.sysctl_data = &nmaxproc;
1058	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1059	if (error || newp == NULL)
1060		return (error);
1061
1062	if (nmaxproc < 0 || nmaxproc >= PID_MAX)
1063		return (EINVAL);
1064#ifdef __HAVE_CPU_MAXPROC
1065	if (nmaxproc > cpu_maxproc())
1066		return (EINVAL);
1067#endif
1068	maxproc = nmaxproc;
1069
1070	return (0);
1071}
1072
1073/*
1074 * sysctl helper function for kern.hostid. The hostid is a long, but
1075 * we export it as an int, so we need to give it a little help.
1076 */
1077static int
1078sysctl_kern_hostid(SYSCTLFN_ARGS)
1079{
1080	int error, inthostid;
1081	struct sysctlnode node;
1082
1083	inthostid = hostid;  /* XXX assumes sizeof int <= sizeof long */
1084	node = *rnode;
1085	node.sysctl_data = &inthostid;
1086	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1087	if (error || newp == NULL)
1088		return (error);
1089
1090	hostid = (unsigned)inthostid;
1091
1092	return (0);
1093}
1094
1095/*
1096 * sysctl helper function for kern.hostname and kern.domainnname.
1097 * resets the relevant recorded length when the underlying name is
1098 * changed.
1099 */
1100static int
1101sysctl_setlen(SYSCTLFN_ARGS)
1102{
1103	int error;
1104
1105	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
1106	if (error || newp == NULL)
1107		return (error);
1108
1109	switch (rnode->sysctl_num) {
1110	case KERN_HOSTNAME:
1111		hostnamelen = strlen((const char*)rnode->sysctl_data);
1112		break;
1113	case KERN_DOMAINNAME:
1114		domainnamelen = strlen((const char*)rnode->sysctl_data);
1115		break;
1116	}
1117
1118	return (0);
1119}
1120
1121/*
1122 * sysctl helper routine for kern.clockrate. Assembles a struct on
1123 * the fly to be returned to the caller.
1124 */
1125static int
1126sysctl_kern_clockrate(SYSCTLFN_ARGS)
1127{
1128	struct clockinfo clkinfo;
1129	struct sysctlnode node;
1130
1131	clkinfo.tick = tick;
1132	clkinfo.tickadj = tickadj;
1133	clkinfo.hz = hz;
1134	clkinfo.profhz = profhz;
1135	clkinfo.stathz = stathz ? stathz : hz;
1136
1137	node = *rnode;
1138	node.sysctl_data = &clkinfo;
1139	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1140}
1141
1142/*
1143 * sysctl helper routine for kern.msgbufsize and kern.msgbuf. For the
1144 * former it merely checks the message buffer is set up. For the latter,
1145 * it also copies out the data if necessary.
1146 */
1147static int
1148sysctl_msgbuf(SYSCTLFN_ARGS)
1149{
1150	char *where = oldp;
1151	size_t len, maxlen;
1152	long beg, end;
1153	extern kmutex_t log_lock;
1154	int error;
1155
1156	if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
1157		msgbufenabled = 0;
1158		return (ENXIO);
1159	}
1160
1161	switch (rnode->sysctl_num) {
1162	case KERN_MSGBUFSIZE: {
1163		struct sysctlnode node = *rnode;
1164		int msg_bufs = (int)msgbufp->msg_bufs;
1165		node.sysctl_data = &msg_bufs;
1166		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1167	}
1168	case KERN_MSGBUF:
1169		break;
1170	default:
1171		return (EOPNOTSUPP);
1172	}
1173
1174	if (newp != NULL)
1175		return (EPERM);
1176
1177	if (oldp == NULL) {
1178		/* always return full buffer size */
1179		*oldlenp = msgbufp->msg_bufs;
1180		return (0);
1181	}
1182
1183	sysctl_unlock();
1184
1185	/*
1186	 * First, copy from the write pointer to the end of
1187	 * message buffer.
1188	 */
1189	error = 0;
1190	mutex_spin_enter(&log_lock);
1191	maxlen = MIN(msgbufp->msg_bufs, *oldlenp);
1192	beg = msgbufp->msg_bufx;
1193	end = msgbufp->msg_bufs;
1194	mutex_spin_exit(&log_lock);
1195
1196	while (maxlen > 0) {
1197		len = MIN(end - beg, maxlen);
1198		if (len == 0)
1199			break;
1200		/* XXX unlocked, but hardly matters. */
1201		error = dcopyout(l, &msgbufp->msg_bufc[beg], where, len);
1202		if (error)
1203			break;
1204		where += len;
1205		maxlen -= len;
1206
1207		/*
1208		 * ... then, copy from the beginning of message buffer to
1209		 * the write pointer.
1210		 */
1211		beg = 0;
1212		end = msgbufp->msg_bufx;
1213	}
1214
1215	sysctl_relock();
1216	return (error);
1217}
1218
1219/*
1220 * sysctl helper routine for kern.defcorename. In the case of a new
1221 * string being assigned, check that it's not a zero-length string.
1222 * (XXX the check in -current doesn't work, but do we really care?)
1223 */
1224static int
1225sysctl_kern_defcorename(SYSCTLFN_ARGS)
1226{
1227	int error;
1228	char *newcorename;
1229	struct sysctlnode node;
1230
1231	newcorename = PNBUF_GET();
1232	node = *rnode;
1233	node.sysctl_data = &newcorename[0];
1234	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
1235	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1236	if (error || newp == NULL) {
1237		goto done;
1238	}
1239
1240	/*
1241	 * when sysctl_lookup() deals with a string, it's guaranteed
1242	 * to come back nul terminated. So there.  :)
1243	 */
1244	if (strlen(newcorename) == 0) {
1245		error = EINVAL;
1246	} else {
1247		memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
1248		error = 0;
1249	}
1250done:
1251	PNBUF_PUT(newcorename);
1252	return error;
1253}
1254
1255/*
1256 * sysctl helper routine for kern.cp_time node. Adds up cpu time
1257 * across all cpus.
1258 */
1259static int
1260sysctl_kern_cptime(SYSCTLFN_ARGS)
1261{
1262	struct sysctlnode node = *rnode;
1263	uint64_t *cp_time = NULL;
1264	int error, n = ncpu, i;
1265	struct cpu_info *ci;
1266	CPU_INFO_ITERATOR cii;
1267
1268	/*
1269	 * if you specifically pass a buffer that is the size of the
1270	 * sum, or if you are probing for the size, you get the "sum"
1271	 * of cp_time (and the size thereof) across all processors.
1272	 *
1273	 * alternately, you can pass an additional mib number and get
1274	 * cp_time for that particular processor.
1275	 */
1276	switch (namelen) {
1277	case 0:
1278		if (*oldlenp == sizeof(uint64_t) * CPUSTATES || oldp == NULL) {
1279			node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
1280			n = -1; /* SUM */
1281		}
1282		else {
1283			node.sysctl_size = n * sizeof(uint64_t) * CPUSTATES;
1284			n = -2; /* ALL */
1285		}
1286		break;
1287	case 1:
1288		if (name[0] < 0 || name[0] >= n)
1289			return (ENOENT); /* ENOSUCHPROCESSOR */
1290		node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
1291		n = name[0];
1292		/*
1293		 * adjust these so that sysctl_lookup() will be happy
1294		 */
1295		name++;
1296		namelen--;
1297		break;
1298	default:
1299		return (EINVAL);
1300	}
1301
1302	cp_time = kmem_alloc(node.sysctl_size, KM_SLEEP);
1303	if (cp_time == NULL)
1304		return (ENOMEM);
1305	node.sysctl_data = cp_time;
1306	memset(cp_time, 0, node.sysctl_size);
1307
1308	for (CPU_INFO_FOREACH(cii, ci)) {
1309		if (n <= 0) {
1310			for (i = 0; i < CPUSTATES; i++) {
1311				cp_time[i] += ci->ci_schedstate.spc_cp_time[i];
1312			}
1313		}
1314		/*
1315		 * if a specific processor was requested and we just
1316		 * did it, we're done here
1317		 */
1318		if (n == 0)
1319			break;
1320		/*
1321		 * if doing "all", skip to next cp_time set for next processor
1322		 */
1323		if (n == -2)
1324			cp_time += CPUSTATES;
1325		/*
1326		 * if we're doing a specific processor, we're one
1327		 * processor closer
1328		 */
1329		if (n > 0)
1330			n--;
1331	}
1332
1333	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1334	kmem_free(node.sysctl_data, node.sysctl_size);
1335	return (error);
1336}
1337
1338#if NPTY > 0
1339/*
1340 * sysctl helper routine for kern.maxptys. Ensures that any new value
1341 * is acceptable to the pty subsystem.
1342 */
1343static int
1344sysctl_kern_maxptys(SYSCTLFN_ARGS)
1345{
1346	int pty_maxptys(int, int);		/* defined in kern/tty_pty.c */
1347	int error, xmax;
1348	struct sysctlnode node;
1349
1350	/* get current value of maxptys */
1351	xmax = pty_maxptys(0, 0);
1352
1353	node = *rnode;
1354	node.sysctl_data = &xmax;
1355	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1356	if (error || newp == NULL)
1357		return (error);
1358
1359	if (xmax != pty_maxptys(xmax, 1))
1360		return (EINVAL);
1361
1362	return (0);
1363}
1364#endif /* NPTY > 0 */
1365
1366/*
1367 * sysctl helper routine for kern.sbmax. Basically just ensures that
1368 * any new value is not too small.
1369 */
1370static int
1371sysctl_kern_sbmax(SYSCTLFN_ARGS)
1372{
1373	int error, new_sbmax;
1374	struct sysctlnode node;
1375
1376	new_sbmax = sb_max;
1377	node = *rnode;
1378	node.sysctl_data = &new_sbmax;
1379	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1380	if (error || newp == NULL)
1381		return (error);
1382
1383	KERNEL_LOCK(1, NULL);
1384	error = sb_max_set(new_sbmax);
1385	KERNEL_UNLOCK_ONE(NULL);
1386
1387	return (error);
1388}
1389
1390/*
1391 * sysctl helper routine for kern.urandom node. Picks a random number
1392 * for you.
1393 */
1394static int
1395sysctl_kern_urnd(SYSCTLFN_ARGS)
1396{
1397	int v, rv;
1398
1399	rv = cprng_strong(sysctl_prng, &v, sizeof(v), 0);
1400	if (rv == sizeof(v)) {
1401		struct sysctlnode node = *rnode;
1402		node.sysctl_data = &v;
1403		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1404	}
1405	else
1406		return (EIO);	/*XXX*/
1407}
1408
1409/*
1410 * sysctl helper routine for kern.arandom node. Picks a random number
1411 * for you.
1412 */
1413static int
1414sysctl_kern_arnd(SYSCTLFN_ARGS)
1415{
1416	int error;
1417	void *v;
1418	struct sysctlnode node = *rnode;
1419
1420	if (*oldlenp == 0)
1421		return 0;
1422	/*
1423	 * This code used to allow sucking 8192 bytes at a time out
1424	 * of the kernel arc4random generator.  Evidently there is some
1425	 * very old OpenBSD application code that may try to do this.
1426	 *
1427	 * Note that this node is documented as type "INT" -- 4 or 8
1428	 * bytes, not 8192.
1429	 *
1430	 * We continue to support this abuse of the "len" pointer here
1431	 * but only 256 bytes at a time, as, anecdotally, the actual
1432	 * application use here was to generate RC4 keys in userspace.
1433	 *
1434	 * Support for such large requests will probably be removed
1435	 * entirely in the future.
1436	 */
1437	if (*oldlenp > 256)
1438		return E2BIG;
1439
1440	v = kmem_alloc(*oldlenp, KM_SLEEP);
1441	cprng_fast(v, *oldlenp);
1442	node.sysctl_data = v;
1443	node.sysctl_size = *oldlenp;
1444	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1445	kmem_free(v, *oldlenp);
1446	return error;
1447}
1448/*
1449 * sysctl helper routine to do kern.lwp.* work.
1450 */
1451static int
1452sysctl_kern_lwp(SYSCTLFN_ARGS)
1453{
1454	struct kinfo_lwp klwp;
1455	struct proc *p;
1456	struct lwp *l2, *l3;
1457	char *where, *dp;
1458	int pid, elem_size, elem_count;
1459	int buflen, needed, error;
1460	bool gotit;
1461
1462	if (namelen == 1 && name[0] == CTL_QUERY)
1463		return (sysctl_query(SYSCTLFN_CALL(rnode)));
1464
1465	dp = where = oldp;
1466	buflen = where != NULL ? *oldlenp : 0;
1467	error = needed = 0;
1468
1469	if (newp != NULL || namelen != 3)
1470		return (EINVAL);
1471	pid = name[0];
1472	elem_size = name[1];
1473	elem_count = name[2];
1474
1475	sysctl_unlock();
1476	if (pid == -1) {
1477		mutex_enter(proc_lock);
1478		PROCLIST_FOREACH(p, &allproc) {
1479			/* Grab a hold on the process. */
1480			if (!rw_tryenter(&p->p_reflock, RW_READER)) {
1481				continue;
1482			}
1483			mutex_exit(proc_lock);
1484
1485			mutex_enter(p->p_lock);
1486			LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
1487				if (buflen >= elem_size && elem_count > 0) {
1488					lwp_lock(l2);
1489					fill_lwp(l2, &klwp);
1490					lwp_unlock(l2);
1491					mutex_exit(p->p_lock);
1492
1493					/*
1494					 * Copy out elem_size, but not
1495					 * larger than the size of a
1496					 * struct kinfo_proc2.
1497					 */
1498					error = dcopyout(l, &klwp, dp,
1499					    min(sizeof(klwp), elem_size));
1500					if (error) {
1501						rw_exit(&p->p_reflock);
1502						goto cleanup;
1503					}
1504					mutex_enter(p->p_lock);
1505					LIST_FOREACH(l3, &p->p_lwps,
1506					    l_sibling) {
1507						if (l2 == l3)
1508							break;
1509					}
1510					if (l3 == NULL) {
1511						mutex_exit(p->p_lock);
1512						rw_exit(&p->p_reflock);
1513						error = EAGAIN;
1514						goto cleanup;
1515					}
1516					dp += elem_size;
1517					buflen -= elem_size;
1518					elem_count--;
1519				}
1520				needed += elem_size;
1521			}
1522			mutex_exit(p->p_lock);
1523
1524			/* Drop reference to process. */
1525			mutex_enter(proc_lock);
1526			rw_exit(&p->p_reflock);
1527		}
1528		mutex_exit(proc_lock);
1529	} else {
1530		mutex_enter(proc_lock);
1531		p = proc_find(pid);
1532		if (p == NULL) {
1533			error = ESRCH;
1534			mutex_exit(proc_lock);
1535			goto cleanup;
1536		}
1537		/* Grab a hold on the process. */
1538		gotit = rw_tryenter(&p->p_reflock, RW_READER);
1539		mutex_exit(proc_lock);
1540		if (!gotit) {
1541			error = ESRCH;
1542			goto cleanup;
1543		}
1544
1545		mutex_enter(p->p_lock);
1546		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
1547			if (buflen >= elem_size && elem_count > 0) {
1548				lwp_lock(l2);
1549				fill_lwp(l2, &klwp);
1550				lwp_unlock(l2);
1551				mutex_exit(p->p_lock);
1552				/*
1553				 * Copy out elem_size, but not larger than
1554				 * the size of a struct kinfo_proc2.
1555				 */
1556				error = dcopyout(l, &klwp, dp,
1557				    min(sizeof(klwp), elem_size));
1558				if (error) {
1559					rw_exit(&p->p_reflock);
1560					goto cleanup;
1561				}
1562				mutex_enter(p->p_lock);
1563				LIST_FOREACH(l3, &p->p_lwps, l_sibling) {
1564					if (l2 == l3)
1565						break;
1566				}
1567				if (l3 == NULL) {
1568					mutex_exit(p->p_lock);
1569					rw_exit(&p->p_reflock);
1570					error = EAGAIN;
1571					goto cleanup;
1572				}
1573				dp += elem_size;
1574				buflen -= elem_size;
1575				elem_count--;
1576			}
1577			needed += elem_size;
1578		}
1579		mutex_exit(p->p_lock);
1580
1581		/* Drop reference to process. */
1582		rw_exit(&p->p_reflock);
1583	}
1584
1585	if (where != NULL) {
1586		*oldlenp = dp - where;
1587		if (needed > *oldlenp) {
1588			sysctl_relock();
1589			return (ENOMEM);
1590		}
1591	} else {
1592		needed += KERN_LWPSLOP;
1593		*oldlenp = needed;
1594	}
1595	error = 0;
1596 cleanup:
1597	sysctl_relock();
1598	return (error);
1599}
1600
1601/*
1602 * sysctl helper routine for kern.forkfsleep node. Ensures that the
1603 * given value is not too large or two small, and is at least one
1604 * timer tick if not zero.
1605 */
1606static int
1607sysctl_kern_forkfsleep(SYSCTLFN_ARGS)
1608{
1609	/* userland sees value in ms, internally is in ticks */
1610	extern int forkfsleep;		/* defined in kern/kern_fork.c */
1611	int error, timo, lsleep;
1612	struct sysctlnode node;
1613
1614	lsleep = forkfsleep * 1000 / hz;
1615	node = *rnode;
1616	node.sysctl_data = &lsleep;
1617	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1618	if (error || newp == NULL)
1619		return (error);
1620
1621	/* refuse negative values, and overly 'long time' */
1622	if (lsleep < 0 || lsleep > MAXSLP * 1000)
1623		return (EINVAL);
1624
1625	timo = mstohz(lsleep);
1626
1627	/* if the interval is >0 ms && <1 tick, use 1 tick */
1628	if (lsleep != 0 && timo == 0)
1629		forkfsleep = 1;
1630	else
1631		forkfsleep = timo;
1632
1633	return (0);
1634}
1635
1636/*
1637 * sysctl helper routine for kern.root_partition
1638 */
1639static int
1640sysctl_kern_root_partition(SYSCTLFN_ARGS)
1641{
1642	int rootpart = DISKPART(rootdev);
1643	struct sysctlnode node = *rnode;
1644
1645	node.sysctl_data = &rootpart;
1646	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1647}
1648
1649/*
1650 * sysctl helper function for kern.drivers
1651 */
1652static int
1653sysctl_kern_drivers(SYSCTLFN_ARGS)
1654{
1655	int error;
1656	size_t buflen;
1657	struct kinfo_drivers kd;
1658	char *start, *where;
1659	const char *dname;
1660	int i;
1661	extern struct devsw_conv *devsw_conv;
1662	extern int max_devsw_convs;
1663
1664	if (newp != NULL || namelen != 0)
1665		return (EINVAL);
1666
1667	start = where = oldp;
1668	buflen = *oldlenp;
1669	if (where == NULL) {
1670		*oldlenp = max_devsw_convs * sizeof kd;
1671		return 0;
1672	}
1673
1674	/*
1675	 * An array of kinfo_drivers structures
1676	 */
1677	error = 0;
1678	sysctl_unlock();
1679	mutex_enter(&device_lock);
1680	for (i = 0; i < max_devsw_convs; i++) {
1681		dname = devsw_conv[i].d_name;
1682		if (dname == NULL)
1683			continue;
1684		if (buflen < sizeof kd) {
1685			error = ENOMEM;
1686			break;
1687		}
1688		memset(&kd, 0, sizeof(kd));
1689		kd.d_bmajor = devsw_conv[i].d_bmajor;
1690		kd.d_cmajor = devsw_conv[i].d_cmajor;
1691		strlcpy(kd.d_name, dname, sizeof kd.d_name);
1692		mutex_exit(&device_lock);
1693		error = dcopyout(l, &kd, where, sizeof kd);
1694		mutex_enter(&device_lock);
1695		if (error != 0)
1696			break;
1697		buflen -= sizeof kd;
1698		where += sizeof kd;
1699	}
1700	mutex_exit(&device_lock);
1701	sysctl_relock();
1702	*oldlenp = where - start;
1703	return error;
1704}
1705
1706static int
1707sysctl_security_setidcore(SYSCTLFN_ARGS)
1708{
1709	int newsize, error;
1710	struct sysctlnode node;
1711
1712	node = *rnode;
1713	node.sysctl_data = &newsize;
1714	newsize = *(int *)rnode->sysctl_data;
1715	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1716	if (error || newp == NULL)
1717		return error;
1718
1719	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
1720	    0, NULL, NULL, NULL))
1721		return (EPERM);
1722
1723	*(int *)rnode->sysctl_data = newsize;
1724
1725	return 0;
1726}
1727
1728static int
1729sysctl_security_setidcorename(SYSCTLFN_ARGS)
1730{
1731	int error;
1732	char *newsetidcorename;
1733	struct sysctlnode node;
1734
1735	newsetidcorename = PNBUF_GET();
1736	node = *rnode;
1737	node.sysctl_data = newsetidcorename;
1738	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
1739	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1740	if (error || newp == NULL) {
1741		goto out;
1742	}
1743	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
1744	    0, NULL, NULL, NULL)) {
1745		error = EPERM;
1746		goto out;
1747	}
1748	if (strlen(newsetidcorename) == 0) {
1749		error = EINVAL;
1750		goto out;
1751	}
1752	memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
1753out:
1754	PNBUF_PUT(newsetidcorename);
1755	return error;
1756}
1757
1758/*
1759 * sysctl helper routine for kern.cp_id node. Maps cpus to their
1760 * cpuids.
1761 */
1762static int
1763sysctl_kern_cpid(SYSCTLFN_ARGS)
1764{
1765	struct sysctlnode node = *rnode;
1766	uint64_t *cp_id = NULL;
1767	int error, n = ncpu;
1768	struct cpu_info *ci;
1769	CPU_INFO_ITERATOR cii;
1770
1771	/*
1772	 * Here you may either retrieve a single cpu id or the whole
1773	 * set. The size you get back when probing depends on what
1774	 * you ask for.
1775	 */
1776	switch (namelen) {
1777	case 0:
1778		node.sysctl_size = n * sizeof(uint64_t);
1779		n = -2; /* ALL */
1780		break;
1781	case 1:
1782		if (name[0] < 0 || name[0] >= n)
1783			return (ENOENT); /* ENOSUCHPROCESSOR */
1784		node.sysctl_size = sizeof(uint64_t);
1785		n = name[0];
1786		/*
1787		 * adjust these so that sysctl_lookup() will be happy
1788		 */
1789		name++;
1790		namelen--;
1791		break;
1792	default:
1793		return (EINVAL);
1794	}
1795
1796	cp_id = kmem_alloc(node.sysctl_size, KM_SLEEP);
1797	if (cp_id == NULL)
1798		return (ENOMEM);
1799	node.sysctl_data = cp_id;
1800	memset(cp_id, 0, node.sysctl_size);
1801
1802	for (CPU_INFO_FOREACH(cii, ci)) {
1803		if (n <= 0)
1804			cp_id[0] = cpu_index(ci);
1805		/*
1806		 * if a specific processor was requested and we just
1807		 * did it, we're done here
1808		 */
1809		if (n == 0)
1810			break;
1811		/*
1812		 * if doing "all", skip to next cp_id slot for next processor
1813		 */
1814		if (n == -2)
1815			cp_id++;
1816		/*
1817		 * if we're doing a specific processor, we're one
1818		 * processor closer
1819		 */
1820		if (n > 0)
1821			n--;
1822	}
1823
1824	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1825	kmem_free(node.sysctl_data, node.sysctl_size);
1826	return (error);
1827}
1828
1829/*
1830 * sysctl helper routine for hw.usermem and hw.usermem64. Values are
1831 * calculate on the fly taking into account integer overflow and the
1832 * current wired count.
1833 */
1834static int
1835sysctl_hw_usermem(SYSCTLFN_ARGS)
1836{
1837	u_int ui;
1838	u_quad_t uq;
1839	struct sysctlnode node;
1840
1841	node = *rnode;
1842	switch (rnode->sysctl_num) {
1843	case HW_USERMEM:
1844		if ((ui = physmem - uvmexp.wired) > (UINT_MAX / PAGE_SIZE))
1845			ui = UINT_MAX;
1846		else
1847			ui *= PAGE_SIZE;
1848		node.sysctl_data = &ui;
1849		break;
1850	case HW_USERMEM64:
1851		uq = (u_quad_t)(physmem - uvmexp.wired) * PAGE_SIZE;
1852		node.sysctl_data = &uq;
1853		break;
1854	default:
1855		return (EINVAL);
1856	}
1857
1858	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1859}
1860
1861/*
1862 * sysctl helper routine for kern.cnmagic node. Pulls the old value
1863 * out, encoded, and stuffs the new value in for decoding.
1864 */
1865static int
1866sysctl_hw_cnmagic(SYSCTLFN_ARGS)
1867{
1868	char magic[CNS_LEN];
1869	int error;
1870	struct sysctlnode node;
1871
1872	if (oldp)
1873		cn_get_magic(magic, CNS_LEN);
1874	node = *rnode;
1875	node.sysctl_data = &magic[0];
1876	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1877	if (error || newp == NULL)
1878		return (error);
1879
1880	return (cn_set_magic(magic));
1881}
1882
1883/*
1884 * ********************************************************************
1885 * section 3: public helper routines that are used for more than one
1886 * node
1887 * ********************************************************************
1888 */
1889
1890/*
1891 * sysctl helper routine for the kern.root_device node and some ports'
1892 * machdep.root_device nodes.
1893 */
1894int
1895sysctl_root_device(SYSCTLFN_ARGS)
1896{
1897	struct sysctlnode node;
1898
1899	node = *rnode;
1900	node.sysctl_data = root_device->dv_xname;
1901	node.sysctl_size = strlen(device_xname(root_device)) + 1;
1902	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1903}
1904
1905/*
1906 * sysctl helper routine for kern.consdev, dependent on the current
1907 * state of the console. Also used for machdep.console_device on some
1908 * ports.
1909 */
1910int
1911sysctl_consdev(SYSCTLFN_ARGS)
1912{
1913	dev_t consdev;
1914	uint32_t oconsdev;
1915	struct sysctlnode node;
1916
1917	if (cn_tab != NULL)
1918		consdev = cn_tab->cn_dev;
1919	else
1920		consdev = NODEV;
1921	node = *rnode;
1922	switch (*oldlenp) {
1923	case sizeof(consdev):
1924		node.sysctl_data = &consdev;
1925		node.sysctl_size = sizeof(consdev);
1926		break;
1927	case sizeof(oconsdev):
1928		oconsdev = (uint32_t)consdev;
1929		node.sysctl_data = &oconsdev;
1930		node.sysctl_size = sizeof(oconsdev);
1931		break;
1932	default:
1933		return EINVAL;
1934	}
1935	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1936}
1937
1938/*
1939 * ********************************************************************
1940 * section 4: support for some helpers
1941 * ********************************************************************
1942 */
1943
1944
1945/*
1946 * Fill in a kinfo_lwp structure for the specified lwp.
1947 */
1948static void
1949fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
1950{
1951	struct proc *p = l->l_proc;
1952	struct timeval tv;
1953
1954	KASSERT(lwp_locked(l, NULL));
1955
1956	memset(kl, 0, sizeof(*kl));
1957
1958	kl->l_forw = 0;
1959	kl->l_back = 0;
1960	kl->l_laddr = PTRTOUINT64(l);
1961	kl->l_addr = PTRTOUINT64(l->l_addr);
1962	kl->l_stat = l->l_stat;
1963	kl->l_lid = l->l_lid;
1964	kl->l_flag = L_INMEM;
1965	kl->l_flag |= sysctl_map_flags(sysctl_lwpprflagmap, l->l_prflag);
1966	kl->l_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag);
1967
1968	kl->l_swtime = l->l_swtime;
1969	kl->l_slptime = l->l_slptime;
1970	if (l->l_stat == LSONPROC)
1971		kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
1972	else
1973		kl->l_schedflags = 0;
1974	kl->l_priority = lwp_eprio(l);
1975	kl->l_usrpri = l->l_priority;
1976	if (l->l_wchan)
1977		strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
1978	kl->l_wchan = PTRTOUINT64(l->l_wchan);
1979	kl->l_cpuid = cpu_index(l->l_cpu);
1980	bintime2timeval(&l->l_rtime, &tv);
1981	kl->l_rtime_sec = tv.tv_sec;
1982	kl->l_rtime_usec = tv.tv_usec;
1983	kl->l_cpticks = l->l_cpticks;
1984	kl->l_pctcpu = l->l_pctcpu;
1985	kl->l_pid = p->p_pid;
1986	if (l->l_name == NULL)
1987		kl->l_name[0] = '\0';
1988	else
1989		strlcpy(kl->l_name, l->l_name, sizeof(kl->l_name));
1990}
1991