xref: /netbsd-src/sys/kern/init_sysctl.c (revision 48fb7bfab72acd4281a53bbee5ccf3f809019e75)
1 /*	$NetBSD: init_sysctl.c,v 1.201 2014/02/25 18:30:11 pooka 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.201 2014/02/25 18:30:11 pooka 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 
639 	sysctl_createv(clog, 0, NULL, NULL,
640 		       CTLFLAG_PERMANENT,
641 		       CTLTYPE_STRING, "machine",
642 		       SYSCTL_DESCR("Machine class"),
643 		       NULL, 0, machine, 0,
644 		       CTL_HW, HW_MACHINE, CTL_EOL);
645 	sysctl_createv(clog, 0, NULL, NULL,
646 		       CTLFLAG_PERMANENT,
647 		       CTLTYPE_STRING, "model",
648 		       SYSCTL_DESCR("Machine model"),
649 		       NULL, 0, cpu_model, 0,
650 		       CTL_HW, HW_MODEL, CTL_EOL);
651 	sysctl_createv(clog, 0, NULL, NULL,
652 		       CTLFLAG_PERMANENT,
653 		       CTLTYPE_INT, "ncpu",
654 		       SYSCTL_DESCR("Number of CPUs configured"),
655 		       NULL, 0, &ncpu, 0,
656 		       CTL_HW, HW_NCPU, CTL_EOL);
657 	sysctl_createv(clog, 0, NULL, NULL,
658 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
659 		       CTLTYPE_INT, "byteorder",
660 		       SYSCTL_DESCR("System byte order"),
661 		       NULL, BYTE_ORDER, NULL, 0,
662 		       CTL_HW, HW_BYTEORDER, CTL_EOL);
663 	u = ((u_int)physmem > (UINT_MAX / PAGE_SIZE)) ?
664 		UINT_MAX : physmem * PAGE_SIZE;
665 	sysctl_createv(clog, 0, NULL, NULL,
666 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
667 		       CTLTYPE_INT, "physmem",
668 		       SYSCTL_DESCR("Bytes of physical memory"),
669 		       NULL, u, NULL, 0,
670 		       CTL_HW, HW_PHYSMEM, CTL_EOL);
671 	sysctl_createv(clog, 0, NULL, NULL,
672 		       CTLFLAG_PERMANENT,
673 		       CTLTYPE_INT, "usermem",
674 		       SYSCTL_DESCR("Bytes of non-kernel memory"),
675 		       sysctl_hw_usermem, 0, NULL, 0,
676 		       CTL_HW, HW_USERMEM, CTL_EOL);
677 	sysctl_createv(clog, 0, NULL, NULL,
678 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
679 		       CTLTYPE_INT, "pagesize",
680 		       SYSCTL_DESCR("Software page size"),
681 		       NULL, PAGE_SIZE, NULL, 0,
682 		       CTL_HW, HW_PAGESIZE, CTL_EOL);
683 	sysctl_createv(clog, 0, NULL, NULL,
684 		       CTLFLAG_PERMANENT,
685 		       CTLTYPE_STRING, "machine_arch",
686 		       SYSCTL_DESCR("Machine CPU class"),
687 		       NULL, 0, machine_arch, 0,
688 		       CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
689 	sysctl_createv(clog, 0, NULL, NULL,
690 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
691 		       CTLTYPE_INT, "alignbytes",
692 		       SYSCTL_DESCR("Alignment constraint for all possible "
693 				    "data types"),
694 		       NULL, ALIGNBYTES, NULL, 0,
695 		       CTL_HW, HW_ALIGNBYTES, CTL_EOL);
696 	sysctl_createv(clog, 0, NULL, NULL,
697 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
698 		       CTLTYPE_STRING, "cnmagic",
699 		       SYSCTL_DESCR("Console magic key sequence"),
700 		       sysctl_hw_cnmagic, 0, NULL, CNS_LEN,
701 		       CTL_HW, HW_CNMAGIC, CTL_EOL);
702 	q = (u_quad_t)physmem * PAGE_SIZE;
703 	sysctl_createv(clog, 0, NULL, NULL,
704 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
705 		       CTLTYPE_QUAD, "physmem64",
706 		       SYSCTL_DESCR("Bytes of physical memory"),
707 		       NULL, q, NULL, 0,
708 		       CTL_HW, HW_PHYSMEM64, CTL_EOL);
709 	sysctl_createv(clog, 0, NULL, NULL,
710 		       CTLFLAG_PERMANENT,
711 		       CTLTYPE_QUAD, "usermem64",
712 		       SYSCTL_DESCR("Bytes of non-kernel memory"),
713 		       sysctl_hw_usermem, 0, NULL, 0,
714 		       CTL_HW, HW_USERMEM64, CTL_EOL);
715 	sysctl_createv(clog, 0, NULL, NULL,
716 		       CTLFLAG_PERMANENT,
717 		       CTLTYPE_INT, "ncpuonline",
718 		       SYSCTL_DESCR("Number of CPUs online"),
719 		       NULL, 0, &ncpuonline, 0,
720 		       CTL_HW, HW_NCPUONLINE, CTL_EOL);
721 }
722 
723 #ifdef DEBUG
724 /*
725  * Debugging related system variables.
726  */
727 struct ctldebug /* debug0, */ /* debug1, */ debug2, debug3, debug4;
728 struct ctldebug debug5, debug6, debug7, debug8, debug9;
729 struct ctldebug debug10, debug11, debug12, debug13, debug14;
730 struct ctldebug debug15, debug16, debug17, debug18, debug19;
731 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
732 	&debug0, &debug1, &debug2, &debug3, &debug4,
733 	&debug5, &debug6, &debug7, &debug8, &debug9,
734 	&debug10, &debug11, &debug12, &debug13, &debug14,
735 	&debug15, &debug16, &debug17, &debug18, &debug19,
736 };
737 
738 /*
739  * this setup routine is a replacement for debug_sysctl()
740  *
741  * note that it creates several nodes per defined debug variable
742  */
743 SYSCTL_SETUP(sysctl_debug_setup, "sysctl debug subtree setup")
744 {
745 	struct ctldebug *cdp;
746 	char nodename[20];
747 	int i;
748 
749 	/*
750 	 * two ways here:
751 	 *
752 	 * the "old" way (debug.name -> value) which was emulated by
753 	 * the sysctl(8) binary
754 	 *
755 	 * the new way, which the sysctl(8) binary was actually using
756 
757 	 node	debug
758 	 node	debug.0
759 	 string debug.0.name
760 	 int	debug.0.value
761 	 int	debug.name
762 
763 	 */
764 
765 	for (i = 0; i < CTL_DEBUG_MAXID; i++) {
766 		cdp = debugvars[i];
767 		if (cdp->debugname == NULL || cdp->debugvar == NULL)
768 			continue;
769 
770 		snprintf(nodename, sizeof(nodename), "debug%d", i);
771 		sysctl_createv(clog, 0, NULL, NULL,
772 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
773 			       CTLTYPE_NODE, nodename, NULL,
774 			       NULL, 0, NULL, 0,
775 			       CTL_DEBUG, i, CTL_EOL);
776 		sysctl_createv(clog, 0, NULL, NULL,
777 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
778 			       CTLTYPE_STRING, "name", NULL,
779 			       /*XXXUNCONST*/
780 			       NULL, 0, __UNCONST(cdp->debugname), 0,
781 			       CTL_DEBUG, i, CTL_DEBUG_NAME, CTL_EOL);
782 		sysctl_createv(clog, 0, NULL, NULL,
783 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
784 			       CTLTYPE_INT, "value", NULL,
785 			       NULL, 0, cdp->debugvar, 0,
786 			       CTL_DEBUG, i, CTL_DEBUG_VALUE, CTL_EOL);
787 		sysctl_createv(clog, 0, NULL, NULL,
788 			       CTLFLAG_PERMANENT,
789 			       CTLTYPE_INT, cdp->debugname, NULL,
790 			       NULL, 0, cdp->debugvar, 0,
791 			       CTL_DEBUG, CTL_CREATE, CTL_EOL);
792 	}
793 }
794 #endif /* DEBUG */
795 
796 /*
797  * ********************************************************************
798  * section 2: private node-specific helper routines.
799  * ********************************************************************
800  */
801 
802 #ifdef DIAGNOSTIC
803 static int
804 sysctl_kern_trigger_panic(SYSCTLFN_ARGS)
805 {
806 	int newtrig, error;
807 	struct sysctlnode node;
808 
809 	newtrig = 0;
810 	node = *rnode;
811 	node.sysctl_data = &newtrig;
812 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
813 	if (error || newp == NULL)
814 		return (error);
815 
816 	if (newtrig != 0)
817 		panic("Panic triggered");
818 
819 	return (error);
820 }
821 #endif
822 
823 /*
824  * sysctl helper routine for kern.maxvnodes.  Drain vnodes if
825  * new value is lower than desiredvnodes and then calls reinit
826  * routines that needs to adjust to the new value.
827  */
828 static int
829 sysctl_kern_maxvnodes(SYSCTLFN_ARGS)
830 {
831 	int error, new_vnodes, old_vnodes, new_max;
832 	struct sysctlnode node;
833 
834 	new_vnodes = desiredvnodes;
835 	node = *rnode;
836 	node.sysctl_data = &new_vnodes;
837 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
838 	if (error || newp == NULL)
839 		return (error);
840 
841 	/*
842 	 * sysctl passes down unsigned values, require them
843 	 * to be positive
844 	 */
845 	if (new_vnodes <= 0)
846 		return (EINVAL);
847 
848 	/* Limits: 75% of kmem and physical memory. */
849 	new_max = calc_cache_size(vmem_size(kmem_arena, VMEM_FREE|VMEM_ALLOC),
850 	    75, 75) / VNODE_COST;
851 	if (new_vnodes > new_max)
852 		new_vnodes = new_max;
853 
854 	old_vnodes = desiredvnodes;
855 	desiredvnodes = new_vnodes;
856 	if (new_vnodes < old_vnodes) {
857 		error = vfs_drainvnodes(new_vnodes);
858 		if (error) {
859 			desiredvnodes = old_vnodes;
860 			return (error);
861 		}
862 	}
863 	vfs_reinit();
864 	nchreinit();
865 
866 	return (0);
867 }
868 
869 /*
870  * sysctl helper routine for rtc_offset - set time after changes
871  */
872 static int
873 sysctl_kern_rtc_offset(SYSCTLFN_ARGS)
874 {
875 	struct timespec ts, delta;
876 	int error, new_rtc_offset;
877 	struct sysctlnode node;
878 
879 	new_rtc_offset = rtc_offset;
880 	node = *rnode;
881 	node.sysctl_data = &new_rtc_offset;
882 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
883 	if (error || newp == NULL)
884 		return (error);
885 
886 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
887 	    KAUTH_REQ_SYSTEM_TIME_RTCOFFSET,
888 	    KAUTH_ARG(new_rtc_offset), NULL, NULL))
889 		return (EPERM);
890 	if (rtc_offset == new_rtc_offset)
891 		return (0);
892 
893 	/* if we change the offset, adjust the time */
894 	nanotime(&ts);
895 	delta.tv_sec = 60 * (new_rtc_offset - rtc_offset);
896 	delta.tv_nsec = 0;
897 	timespecadd(&ts, &delta, &ts);
898 	rtc_offset = new_rtc_offset;
899 	return (settime(l->l_proc, &ts));
900 }
901 
902 /*
903  * sysctl helper routine for kern.maxproc. Ensures that the new
904  * values are not too low or too high.
905  */
906 static int
907 sysctl_kern_maxproc(SYSCTLFN_ARGS)
908 {
909 	int error, nmaxproc;
910 	struct sysctlnode node;
911 
912 	nmaxproc = maxproc;
913 	node = *rnode;
914 	node.sysctl_data = &nmaxproc;
915 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
916 	if (error || newp == NULL)
917 		return (error);
918 
919 	if (nmaxproc < 0 || nmaxproc >= PID_MAX)
920 		return (EINVAL);
921 #ifdef __HAVE_CPU_MAXPROC
922 	if (nmaxproc > cpu_maxproc())
923 		return (EINVAL);
924 #endif
925 	maxproc = nmaxproc;
926 
927 	return (0);
928 }
929 
930 /*
931  * sysctl helper function for kern.hostid. The hostid is a long, but
932  * we export it as an int, so we need to give it a little help.
933  */
934 static int
935 sysctl_kern_hostid(SYSCTLFN_ARGS)
936 {
937 	int error, inthostid;
938 	struct sysctlnode node;
939 
940 	inthostid = hostid;  /* XXX assumes sizeof int <= sizeof long */
941 	node = *rnode;
942 	node.sysctl_data = &inthostid;
943 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
944 	if (error || newp == NULL)
945 		return (error);
946 
947 	hostid = (unsigned)inthostid;
948 
949 	return (0);
950 }
951 
952 /*
953  * sysctl helper routine for kern.clockrate. Assembles a struct on
954  * the fly to be returned to the caller.
955  */
956 static int
957 sysctl_kern_clockrate(SYSCTLFN_ARGS)
958 {
959 	struct clockinfo clkinfo;
960 	struct sysctlnode node;
961 
962 	clkinfo.tick = tick;
963 	clkinfo.tickadj = tickadj;
964 	clkinfo.hz = hz;
965 	clkinfo.profhz = profhz;
966 	clkinfo.stathz = stathz ? stathz : hz;
967 
968 	node = *rnode;
969 	node.sysctl_data = &clkinfo;
970 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
971 }
972 
973 /*
974  * sysctl helper routine for kern.msgbufsize and kern.msgbuf. For the
975  * former it merely checks the message buffer is set up. For the latter,
976  * it also copies out the data if necessary.
977  */
978 static int
979 sysctl_msgbuf(SYSCTLFN_ARGS)
980 {
981 	char *where = oldp;
982 	size_t len, maxlen;
983 	long beg, end;
984 	extern kmutex_t log_lock;
985 	int error;
986 
987 	if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
988 		msgbufenabled = 0;
989 		return (ENXIO);
990 	}
991 
992 	switch (rnode->sysctl_num) {
993 	case KERN_MSGBUFSIZE: {
994 		struct sysctlnode node = *rnode;
995 		int msg_bufs = (int)msgbufp->msg_bufs;
996 		node.sysctl_data = &msg_bufs;
997 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
998 	}
999 	case KERN_MSGBUF:
1000 		break;
1001 	default:
1002 		return (EOPNOTSUPP);
1003 	}
1004 
1005 	if (newp != NULL)
1006 		return (EPERM);
1007 
1008 	if (oldp == NULL) {
1009 		/* always return full buffer size */
1010 		*oldlenp = msgbufp->msg_bufs;
1011 		return (0);
1012 	}
1013 
1014 	sysctl_unlock();
1015 
1016 	/*
1017 	 * First, copy from the write pointer to the end of
1018 	 * message buffer.
1019 	 */
1020 	error = 0;
1021 	mutex_spin_enter(&log_lock);
1022 	maxlen = MIN(msgbufp->msg_bufs, *oldlenp);
1023 	beg = msgbufp->msg_bufx;
1024 	end = msgbufp->msg_bufs;
1025 	mutex_spin_exit(&log_lock);
1026 
1027 	while (maxlen > 0) {
1028 		len = MIN(end - beg, maxlen);
1029 		if (len == 0)
1030 			break;
1031 		/* XXX unlocked, but hardly matters. */
1032 		error = dcopyout(l, &msgbufp->msg_bufc[beg], where, len);
1033 		if (error)
1034 			break;
1035 		where += len;
1036 		maxlen -= len;
1037 
1038 		/*
1039 		 * ... then, copy from the beginning of message buffer to
1040 		 * the write pointer.
1041 		 */
1042 		beg = 0;
1043 		end = msgbufp->msg_bufx;
1044 	}
1045 
1046 	sysctl_relock();
1047 	return (error);
1048 }
1049 
1050 /*
1051  * sysctl helper routine for kern.defcorename. In the case of a new
1052  * string being assigned, check that it's not a zero-length string.
1053  * (XXX the check in -current doesn't work, but do we really care?)
1054  */
1055 static int
1056 sysctl_kern_defcorename(SYSCTLFN_ARGS)
1057 {
1058 	int error;
1059 	char *newcorename;
1060 	struct sysctlnode node;
1061 
1062 	newcorename = PNBUF_GET();
1063 	node = *rnode;
1064 	node.sysctl_data = &newcorename[0];
1065 	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
1066 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1067 	if (error || newp == NULL) {
1068 		goto done;
1069 	}
1070 
1071 	/*
1072 	 * when sysctl_lookup() deals with a string, it's guaranteed
1073 	 * to come back nul terminated. So there.  :)
1074 	 */
1075 	if (strlen(newcorename) == 0) {
1076 		error = EINVAL;
1077 	} else {
1078 		memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
1079 		error = 0;
1080 	}
1081 done:
1082 	PNBUF_PUT(newcorename);
1083 	return error;
1084 }
1085 
1086 /*
1087  * sysctl helper routine for kern.cp_time node. Adds up cpu time
1088  * across all cpus.
1089  */
1090 static int
1091 sysctl_kern_cptime(SYSCTLFN_ARGS)
1092 {
1093 	struct sysctlnode node = *rnode;
1094 	uint64_t *cp_time = NULL;
1095 	int error, n = ncpu, i;
1096 	struct cpu_info *ci;
1097 	CPU_INFO_ITERATOR cii;
1098 
1099 	/*
1100 	 * if you specifically pass a buffer that is the size of the
1101 	 * sum, or if you are probing for the size, you get the "sum"
1102 	 * of cp_time (and the size thereof) across all processors.
1103 	 *
1104 	 * alternately, you can pass an additional mib number and get
1105 	 * cp_time for that particular processor.
1106 	 */
1107 	switch (namelen) {
1108 	case 0:
1109 		if (*oldlenp == sizeof(uint64_t) * CPUSTATES || oldp == NULL) {
1110 			node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
1111 			n = -1; /* SUM */
1112 		}
1113 		else {
1114 			node.sysctl_size = n * sizeof(uint64_t) * CPUSTATES;
1115 			n = -2; /* ALL */
1116 		}
1117 		break;
1118 	case 1:
1119 		if (name[0] < 0 || name[0] >= n)
1120 			return (ENOENT); /* ENOSUCHPROCESSOR */
1121 		node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
1122 		n = name[0];
1123 		/*
1124 		 * adjust these so that sysctl_lookup() will be happy
1125 		 */
1126 		name++;
1127 		namelen--;
1128 		break;
1129 	default:
1130 		return (EINVAL);
1131 	}
1132 
1133 	cp_time = kmem_alloc(node.sysctl_size, KM_SLEEP);
1134 	if (cp_time == NULL)
1135 		return (ENOMEM);
1136 	node.sysctl_data = cp_time;
1137 	memset(cp_time, 0, node.sysctl_size);
1138 
1139 	for (CPU_INFO_FOREACH(cii, ci)) {
1140 		if (n <= 0) {
1141 			for (i = 0; i < CPUSTATES; i++) {
1142 				cp_time[i] += ci->ci_schedstate.spc_cp_time[i];
1143 			}
1144 		}
1145 		/*
1146 		 * if a specific processor was requested and we just
1147 		 * did it, we're done here
1148 		 */
1149 		if (n == 0)
1150 			break;
1151 		/*
1152 		 * if doing "all", skip to next cp_time set for next processor
1153 		 */
1154 		if (n == -2)
1155 			cp_time += CPUSTATES;
1156 		/*
1157 		 * if we're doing a specific processor, we're one
1158 		 * processor closer
1159 		 */
1160 		if (n > 0)
1161 			n--;
1162 	}
1163 
1164 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1165 	kmem_free(node.sysctl_data, node.sysctl_size);
1166 	return (error);
1167 }
1168 
1169 #if NPTY > 0
1170 /*
1171  * sysctl helper routine for kern.maxptys. Ensures that any new value
1172  * is acceptable to the pty subsystem.
1173  */
1174 static int
1175 sysctl_kern_maxptys(SYSCTLFN_ARGS)
1176 {
1177 	int pty_maxptys(int, int);		/* defined in kern/tty_pty.c */
1178 	int error, xmax;
1179 	struct sysctlnode node;
1180 
1181 	/* get current value of maxptys */
1182 	xmax = pty_maxptys(0, 0);
1183 
1184 	node = *rnode;
1185 	node.sysctl_data = &xmax;
1186 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1187 	if (error || newp == NULL)
1188 		return (error);
1189 
1190 	if (xmax != pty_maxptys(xmax, 1))
1191 		return (EINVAL);
1192 
1193 	return (0);
1194 }
1195 #endif /* NPTY > 0 */
1196 
1197 /*
1198  * sysctl helper routine to do kern.lwp.* work.
1199  */
1200 static int
1201 sysctl_kern_lwp(SYSCTLFN_ARGS)
1202 {
1203 	struct kinfo_lwp klwp;
1204 	struct proc *p;
1205 	struct lwp *l2, *l3;
1206 	char *where, *dp;
1207 	int pid, elem_size, elem_count;
1208 	int buflen, needed, error;
1209 	bool gotit;
1210 
1211 	if (namelen == 1 && name[0] == CTL_QUERY)
1212 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
1213 
1214 	dp = where = oldp;
1215 	buflen = where != NULL ? *oldlenp : 0;
1216 	error = needed = 0;
1217 
1218 	if (newp != NULL || namelen != 3)
1219 		return (EINVAL);
1220 	pid = name[0];
1221 	elem_size = name[1];
1222 	elem_count = name[2];
1223 
1224 	sysctl_unlock();
1225 	if (pid == -1) {
1226 		mutex_enter(proc_lock);
1227 		PROCLIST_FOREACH(p, &allproc) {
1228 			/* Grab a hold on the process. */
1229 			if (!rw_tryenter(&p->p_reflock, RW_READER)) {
1230 				continue;
1231 			}
1232 			mutex_exit(proc_lock);
1233 
1234 			mutex_enter(p->p_lock);
1235 			LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
1236 				if (buflen >= elem_size && elem_count > 0) {
1237 					lwp_lock(l2);
1238 					fill_lwp(l2, &klwp);
1239 					lwp_unlock(l2);
1240 					mutex_exit(p->p_lock);
1241 
1242 					/*
1243 					 * Copy out elem_size, but not
1244 					 * larger than the size of a
1245 					 * struct kinfo_proc2.
1246 					 */
1247 					error = dcopyout(l, &klwp, dp,
1248 					    min(sizeof(klwp), elem_size));
1249 					if (error) {
1250 						rw_exit(&p->p_reflock);
1251 						goto cleanup;
1252 					}
1253 					mutex_enter(p->p_lock);
1254 					LIST_FOREACH(l3, &p->p_lwps,
1255 					    l_sibling) {
1256 						if (l2 == l3)
1257 							break;
1258 					}
1259 					if (l3 == NULL) {
1260 						mutex_exit(p->p_lock);
1261 						rw_exit(&p->p_reflock);
1262 						error = EAGAIN;
1263 						goto cleanup;
1264 					}
1265 					dp += elem_size;
1266 					buflen -= elem_size;
1267 					elem_count--;
1268 				}
1269 				needed += elem_size;
1270 			}
1271 			mutex_exit(p->p_lock);
1272 
1273 			/* Drop reference to process. */
1274 			mutex_enter(proc_lock);
1275 			rw_exit(&p->p_reflock);
1276 		}
1277 		mutex_exit(proc_lock);
1278 	} else {
1279 		mutex_enter(proc_lock);
1280 		p = proc_find(pid);
1281 		if (p == NULL) {
1282 			error = ESRCH;
1283 			mutex_exit(proc_lock);
1284 			goto cleanup;
1285 		}
1286 		/* Grab a hold on the process. */
1287 		gotit = rw_tryenter(&p->p_reflock, RW_READER);
1288 		mutex_exit(proc_lock);
1289 		if (!gotit) {
1290 			error = ESRCH;
1291 			goto cleanup;
1292 		}
1293 
1294 		mutex_enter(p->p_lock);
1295 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
1296 			if (buflen >= elem_size && elem_count > 0) {
1297 				lwp_lock(l2);
1298 				fill_lwp(l2, &klwp);
1299 				lwp_unlock(l2);
1300 				mutex_exit(p->p_lock);
1301 				/*
1302 				 * Copy out elem_size, but not larger than
1303 				 * the size of a struct kinfo_proc2.
1304 				 */
1305 				error = dcopyout(l, &klwp, dp,
1306 				    min(sizeof(klwp), elem_size));
1307 				if (error) {
1308 					rw_exit(&p->p_reflock);
1309 					goto cleanup;
1310 				}
1311 				mutex_enter(p->p_lock);
1312 				LIST_FOREACH(l3, &p->p_lwps, l_sibling) {
1313 					if (l2 == l3)
1314 						break;
1315 				}
1316 				if (l3 == NULL) {
1317 					mutex_exit(p->p_lock);
1318 					rw_exit(&p->p_reflock);
1319 					error = EAGAIN;
1320 					goto cleanup;
1321 				}
1322 				dp += elem_size;
1323 				buflen -= elem_size;
1324 				elem_count--;
1325 			}
1326 			needed += elem_size;
1327 		}
1328 		mutex_exit(p->p_lock);
1329 
1330 		/* Drop reference to process. */
1331 		rw_exit(&p->p_reflock);
1332 	}
1333 
1334 	if (where != NULL) {
1335 		*oldlenp = dp - where;
1336 		if (needed > *oldlenp) {
1337 			sysctl_relock();
1338 			return (ENOMEM);
1339 		}
1340 	} else {
1341 		needed += KERN_LWPSLOP;
1342 		*oldlenp = needed;
1343 	}
1344 	error = 0;
1345  cleanup:
1346 	sysctl_relock();
1347 	return (error);
1348 }
1349 
1350 /*
1351  * sysctl helper routine for kern.forkfsleep node. Ensures that the
1352  * given value is not too large or two small, and is at least one
1353  * timer tick if not zero.
1354  */
1355 static int
1356 sysctl_kern_forkfsleep(SYSCTLFN_ARGS)
1357 {
1358 	/* userland sees value in ms, internally is in ticks */
1359 	extern int forkfsleep;		/* defined in kern/kern_fork.c */
1360 	int error, timo, lsleep;
1361 	struct sysctlnode node;
1362 
1363 	lsleep = forkfsleep * 1000 / hz;
1364 	node = *rnode;
1365 	node.sysctl_data = &lsleep;
1366 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1367 	if (error || newp == NULL)
1368 		return (error);
1369 
1370 	/* refuse negative values, and overly 'long time' */
1371 	if (lsleep < 0 || lsleep > MAXSLP * 1000)
1372 		return (EINVAL);
1373 
1374 	timo = mstohz(lsleep);
1375 
1376 	/* if the interval is >0 ms && <1 tick, use 1 tick */
1377 	if (lsleep != 0 && timo == 0)
1378 		forkfsleep = 1;
1379 	else
1380 		forkfsleep = timo;
1381 
1382 	return (0);
1383 }
1384 
1385 /*
1386  * sysctl helper routine for kern.root_partition
1387  */
1388 static int
1389 sysctl_kern_root_partition(SYSCTLFN_ARGS)
1390 {
1391 	int rootpart = DISKPART(rootdev);
1392 	struct sysctlnode node = *rnode;
1393 
1394 	node.sysctl_data = &rootpart;
1395 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1396 }
1397 
1398 /*
1399  * sysctl helper function for kern.drivers
1400  */
1401 static int
1402 sysctl_kern_drivers(SYSCTLFN_ARGS)
1403 {
1404 	int error;
1405 	size_t buflen;
1406 	struct kinfo_drivers kd;
1407 	char *start, *where;
1408 	const char *dname;
1409 	int i;
1410 	extern struct devsw_conv *devsw_conv;
1411 	extern int max_devsw_convs;
1412 
1413 	start = where = oldp;
1414 	buflen = *oldlenp;
1415 	if (where == NULL) {
1416 		*oldlenp = max_devsw_convs * sizeof kd;
1417 		return 0;
1418 	}
1419 
1420 	/*
1421 	 * An array of kinfo_drivers structures
1422 	 */
1423 	error = 0;
1424 	sysctl_unlock();
1425 	mutex_enter(&device_lock);
1426 	for (i = 0; i < max_devsw_convs; i++) {
1427 		dname = devsw_conv[i].d_name;
1428 		if (dname == NULL)
1429 			continue;
1430 		if (buflen < sizeof kd) {
1431 			error = ENOMEM;
1432 			break;
1433 		}
1434 		memset(&kd, 0, sizeof(kd));
1435 		kd.d_bmajor = devsw_conv[i].d_bmajor;
1436 		kd.d_cmajor = devsw_conv[i].d_cmajor;
1437 		strlcpy(kd.d_name, dname, sizeof kd.d_name);
1438 		mutex_exit(&device_lock);
1439 		error = dcopyout(l, &kd, where, sizeof kd);
1440 		mutex_enter(&device_lock);
1441 		if (error != 0)
1442 			break;
1443 		buflen -= sizeof kd;
1444 		where += sizeof kd;
1445 	}
1446 	mutex_exit(&device_lock);
1447 	sysctl_relock();
1448 	*oldlenp = where - start;
1449 	return error;
1450 }
1451 
1452 static int
1453 sysctl_security_setidcore(SYSCTLFN_ARGS)
1454 {
1455 	int newsize, error;
1456 	struct sysctlnode node;
1457 
1458 	node = *rnode;
1459 	node.sysctl_data = &newsize;
1460 	newsize = *(int *)rnode->sysctl_data;
1461 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1462 	if (error || newp == NULL)
1463 		return error;
1464 
1465 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
1466 	    0, NULL, NULL, NULL))
1467 		return (EPERM);
1468 
1469 	*(int *)rnode->sysctl_data = newsize;
1470 
1471 	return 0;
1472 }
1473 
1474 static int
1475 sysctl_security_setidcorename(SYSCTLFN_ARGS)
1476 {
1477 	int error;
1478 	char *newsetidcorename;
1479 	struct sysctlnode node;
1480 
1481 	newsetidcorename = PNBUF_GET();
1482 	node = *rnode;
1483 	node.sysctl_data = newsetidcorename;
1484 	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
1485 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1486 	if (error || newp == NULL) {
1487 		goto out;
1488 	}
1489 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
1490 	    0, NULL, NULL, NULL)) {
1491 		error = EPERM;
1492 		goto out;
1493 	}
1494 	if (strlen(newsetidcorename) == 0) {
1495 		error = EINVAL;
1496 		goto out;
1497 	}
1498 	memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
1499 out:
1500 	PNBUF_PUT(newsetidcorename);
1501 	return error;
1502 }
1503 
1504 /*
1505  * sysctl helper routine for kern.cp_id node. Maps cpus to their
1506  * cpuids.
1507  */
1508 static int
1509 sysctl_kern_cpid(SYSCTLFN_ARGS)
1510 {
1511 	struct sysctlnode node = *rnode;
1512 	uint64_t *cp_id = NULL;
1513 	int error, n = ncpu;
1514 	struct cpu_info *ci;
1515 	CPU_INFO_ITERATOR cii;
1516 
1517 	/*
1518 	 * Here you may either retrieve a single cpu id or the whole
1519 	 * set. The size you get back when probing depends on what
1520 	 * you ask for.
1521 	 */
1522 	switch (namelen) {
1523 	case 0:
1524 		node.sysctl_size = n * sizeof(uint64_t);
1525 		n = -2; /* ALL */
1526 		break;
1527 	case 1:
1528 		if (name[0] < 0 || name[0] >= n)
1529 			return (ENOENT); /* ENOSUCHPROCESSOR */
1530 		node.sysctl_size = sizeof(uint64_t);
1531 		n = name[0];
1532 		/*
1533 		 * adjust these so that sysctl_lookup() will be happy
1534 		 */
1535 		name++;
1536 		namelen--;
1537 		break;
1538 	default:
1539 		return (EINVAL);
1540 	}
1541 
1542 	cp_id = kmem_alloc(node.sysctl_size, KM_SLEEP);
1543 	if (cp_id == NULL)
1544 		return (ENOMEM);
1545 	node.sysctl_data = cp_id;
1546 	memset(cp_id, 0, node.sysctl_size);
1547 
1548 	for (CPU_INFO_FOREACH(cii, ci)) {
1549 		if (n <= 0)
1550 			cp_id[0] = cpu_index(ci);
1551 		/*
1552 		 * if a specific processor was requested and we just
1553 		 * did it, we're done here
1554 		 */
1555 		if (n == 0)
1556 			break;
1557 		/*
1558 		 * if doing "all", skip to next cp_id slot for next processor
1559 		 */
1560 		if (n == -2)
1561 			cp_id++;
1562 		/*
1563 		 * if we're doing a specific processor, we're one
1564 		 * processor closer
1565 		 */
1566 		if (n > 0)
1567 			n--;
1568 	}
1569 
1570 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1571 	kmem_free(node.sysctl_data, node.sysctl_size);
1572 	return (error);
1573 }
1574 
1575 /*
1576  * sysctl helper routine for hw.usermem and hw.usermem64. Values are
1577  * calculate on the fly taking into account integer overflow and the
1578  * current wired count.
1579  */
1580 static int
1581 sysctl_hw_usermem(SYSCTLFN_ARGS)
1582 {
1583 	u_int ui;
1584 	u_quad_t uq;
1585 	struct sysctlnode node;
1586 
1587 	node = *rnode;
1588 	switch (rnode->sysctl_num) {
1589 	case HW_USERMEM:
1590 		if ((ui = physmem - uvmexp.wired) > (UINT_MAX / PAGE_SIZE))
1591 			ui = UINT_MAX;
1592 		else
1593 			ui *= PAGE_SIZE;
1594 		node.sysctl_data = &ui;
1595 		break;
1596 	case HW_USERMEM64:
1597 		uq = (u_quad_t)(physmem - uvmexp.wired) * PAGE_SIZE;
1598 		node.sysctl_data = &uq;
1599 		break;
1600 	default:
1601 		return (EINVAL);
1602 	}
1603 
1604 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1605 }
1606 
1607 /*
1608  * sysctl helper routine for kern.cnmagic node. Pulls the old value
1609  * out, encoded, and stuffs the new value in for decoding.
1610  */
1611 static int
1612 sysctl_hw_cnmagic(SYSCTLFN_ARGS)
1613 {
1614 	char magic[CNS_LEN];
1615 	int error;
1616 	struct sysctlnode node;
1617 
1618 	if (oldp)
1619 		cn_get_magic(magic, CNS_LEN);
1620 	node = *rnode;
1621 	node.sysctl_data = &magic[0];
1622 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1623 	if (error || newp == NULL)
1624 		return (error);
1625 
1626 	return (cn_set_magic(magic));
1627 }
1628 
1629 /*
1630  * ********************************************************************
1631  * section 3: public helper routines that are used for more than one
1632  * node
1633  * ********************************************************************
1634  */
1635 
1636 /*
1637  * sysctl helper routine for the kern.root_device node and some ports'
1638  * machdep.root_device nodes.
1639  */
1640 int
1641 sysctl_root_device(SYSCTLFN_ARGS)
1642 {
1643 	struct sysctlnode node;
1644 
1645 	node = *rnode;
1646 	node.sysctl_data = __UNCONST(device_xname(root_device));
1647 	node.sysctl_size = strlen(device_xname(root_device)) + 1;
1648 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1649 }
1650 
1651 /*
1652  * sysctl helper routine for kern.consdev, dependent on the current
1653  * state of the console. Also used for machdep.console_device on some
1654  * ports.
1655  */
1656 int
1657 sysctl_consdev(SYSCTLFN_ARGS)
1658 {
1659 	dev_t consdev;
1660 	uint32_t oconsdev;
1661 	struct sysctlnode node;
1662 
1663 	if (cn_tab != NULL)
1664 		consdev = cn_tab->cn_dev;
1665 	else
1666 		consdev = NODEV;
1667 	node = *rnode;
1668 	switch (*oldlenp) {
1669 	case sizeof(consdev):
1670 		node.sysctl_data = &consdev;
1671 		node.sysctl_size = sizeof(consdev);
1672 		break;
1673 	case sizeof(oconsdev):
1674 		oconsdev = (uint32_t)consdev;
1675 		node.sysctl_data = &oconsdev;
1676 		node.sysctl_size = sizeof(oconsdev);
1677 		break;
1678 	default:
1679 		return EINVAL;
1680 	}
1681 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1682 }
1683 
1684 /*
1685  * ********************************************************************
1686  * section 4: support for some helpers
1687  * ********************************************************************
1688  */
1689 
1690 
1691 /*
1692  * Fill in a kinfo_lwp structure for the specified lwp.
1693  */
1694 static void
1695 fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
1696 {
1697 	struct proc *p = l->l_proc;
1698 	struct timeval tv;
1699 
1700 	KASSERT(lwp_locked(l, NULL));
1701 
1702 	memset(kl, 0, sizeof(*kl));
1703 
1704 	kl->l_forw = 0;
1705 	kl->l_back = 0;
1706 	kl->l_laddr = PTRTOUINT64(l);
1707 	kl->l_addr = PTRTOUINT64(l->l_addr);
1708 	kl->l_stat = l->l_stat;
1709 	kl->l_lid = l->l_lid;
1710 	kl->l_flag = L_INMEM;
1711 	kl->l_flag |= sysctl_map_flags(sysctl_lwpprflagmap, l->l_prflag);
1712 	kl->l_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag);
1713 
1714 	kl->l_swtime = l->l_swtime;
1715 	kl->l_slptime = l->l_slptime;
1716 	if (l->l_stat == LSONPROC)
1717 		kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
1718 	else
1719 		kl->l_schedflags = 0;
1720 	kl->l_priority = lwp_eprio(l);
1721 	kl->l_usrpri = l->l_priority;
1722 	if (l->l_wchan)
1723 		strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
1724 	kl->l_wchan = PTRTOUINT64(l->l_wchan);
1725 	kl->l_cpuid = cpu_index(l->l_cpu);
1726 	bintime2timeval(&l->l_rtime, &tv);
1727 	kl->l_rtime_sec = tv.tv_sec;
1728 	kl->l_rtime_usec = tv.tv_usec;
1729 	kl->l_cpticks = l->l_cpticks;
1730 	kl->l_pctcpu = l->l_pctcpu;
1731 	kl->l_pid = p->p_pid;
1732 	if (l->l_name == NULL)
1733 		kl->l_name[0] = '\0';
1734 	else
1735 		strlcpy(kl->l_name, l->l_name, sizeof(kl->l_name));
1736 }
1737