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