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