init_sysctl.c revision 1.204.4.6 1 /* $NetBSD: init_sysctl.c,v 1.204.4.6 2017/08/28 17:53:07 skrll 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.204.4.6 2017/08/28 17:53:07 skrll 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_impl.h> /* For vfs_drainvnodes(). */
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
71 int security_setidcore_dump;
72 char security_setidcore_path[MAXPATHLEN] = "/var/crash/%n.core";
73 uid_t security_setidcore_owner = 0;
74 gid_t security_setidcore_group = 0;
75 mode_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
83 int kern_has_sysvmsg = 0;
84 int kern_has_sysvshm = 0;
85 int kern_has_sysvsem = 0;
86
87 static 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
97 static int dcopyout(struct lwp *, const void *, void *, size_t);
98
99 static int
100 dcopyout(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
111 static int sysctl_kern_trigger_panic(SYSCTLFN_PROTO);
112 #endif
113 static int sysctl_kern_maxvnodes(SYSCTLFN_PROTO);
114 static int sysctl_kern_messages(SYSCTLFN_PROTO);
115 static int sysctl_kern_rtc_offset(SYSCTLFN_PROTO);
116 static int sysctl_kern_maxproc(SYSCTLFN_PROTO);
117 static int sysctl_kern_hostid(SYSCTLFN_PROTO);
118 static int sysctl_kern_defcorename(SYSCTLFN_PROTO);
119 static int sysctl_kern_cptime(SYSCTLFN_PROTO);
120 #if NPTY > 0
121 static int sysctl_kern_maxptys(SYSCTLFN_PROTO);
122 #endif /* NPTY > 0 */
123 static int sysctl_kern_lwp(SYSCTLFN_PROTO);
124 static int sysctl_kern_forkfsleep(SYSCTLFN_PROTO);
125 static int sysctl_kern_root_partition(SYSCTLFN_PROTO);
126 static int sysctl_kern_drivers(SYSCTLFN_PROTO);
127 static int sysctl_security_setidcore(SYSCTLFN_PROTO);
128 static int sysctl_security_setidcorename(SYSCTLFN_PROTO);
129 static int sysctl_kern_cpid(SYSCTLFN_PROTO);
130 static int sysctl_hw_usermem(SYSCTLFN_PROTO);
131 static int sysctl_hw_cnmagic(SYSCTLFN_PROTO);
132
133 static 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 */
147 SYSCTL_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
603 SYSCTL_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 */
630 struct ctldebug /* debug0, */ /* debug1, */ debug2, debug3, debug4;
631 struct ctldebug debug5, debug6, debug7, debug8, debug9;
632 struct ctldebug debug10, debug11, debug12, debug13, debug14;
633 struct ctldebug debug15, debug16, debug17, debug18, debug19;
634 static 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 */
646 SYSCTL_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
706 static int
707 sysctl_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 */
731 static int
732 sysctl_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();
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
777 static int
778 sysctl_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 */
839 static int
840 sysctl_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 */
873 static int
874 sysctl_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 */
901 static int
902 sysctl_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 */
924 static int
925 sysctl_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 }
950 done:
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 */
959 static int
960 sysctl_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 node.sysctl_data = cp_time;
1004 memset(cp_time, 0, node.sysctl_size);
1005
1006 for (CPU_INFO_FOREACH(cii, ci)) {
1007 if (n <= 0) {
1008 for (i = 0; i < CPUSTATES; i++) {
1009 cp_time[i] += ci->ci_schedstate.spc_cp_time[i];
1010 }
1011 }
1012 /*
1013 * if a specific processor was requested and we just
1014 * did it, we're done here
1015 */
1016 if (n == 0)
1017 break;
1018 /*
1019 * if doing "all", skip to next cp_time set for next processor
1020 */
1021 if (n == -2)
1022 cp_time += CPUSTATES;
1023 /*
1024 * if we're doing a specific processor, we're one
1025 * processor closer
1026 */
1027 if (n > 0)
1028 n--;
1029 }
1030
1031 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1032 kmem_free(node.sysctl_data, node.sysctl_size);
1033 return (error);
1034 }
1035
1036 #if NPTY > 0
1037 /*
1038 * sysctl helper routine for kern.maxptys. Ensures that any new value
1039 * is acceptable to the pty subsystem.
1040 */
1041 static int
1042 sysctl_kern_maxptys(SYSCTLFN_ARGS)
1043 {
1044 int pty_maxptys(int, int); /* defined in kern/tty_pty.c */
1045 int error, xmax;
1046 struct sysctlnode node;
1047
1048 /* get current value of maxptys */
1049 xmax = pty_maxptys(0, 0);
1050
1051 node = *rnode;
1052 node.sysctl_data = &xmax;
1053 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1054 if (error || newp == NULL)
1055 return (error);
1056
1057 if (xmax != pty_maxptys(xmax, 1))
1058 return (EINVAL);
1059
1060 return (0);
1061 }
1062 #endif /* NPTY > 0 */
1063
1064 /*
1065 * sysctl helper routine to do kern.lwp.* work.
1066 */
1067 static int
1068 sysctl_kern_lwp(SYSCTLFN_ARGS)
1069 {
1070 struct kinfo_lwp klwp;
1071 struct proc *p;
1072 struct lwp *l2, *l3;
1073 char *where, *dp;
1074 int pid, elem_size, elem_count;
1075 int buflen, needed, error;
1076 bool gotit;
1077
1078 if (namelen == 1 && name[0] == CTL_QUERY)
1079 return (sysctl_query(SYSCTLFN_CALL(rnode)));
1080
1081 dp = where = oldp;
1082 buflen = where != NULL ? *oldlenp : 0;
1083 error = needed = 0;
1084
1085 if (newp != NULL || namelen != 3)
1086 return (EINVAL);
1087 pid = name[0];
1088 elem_size = name[1];
1089 elem_count = name[2];
1090
1091 sysctl_unlock();
1092 if (pid == -1) {
1093 mutex_enter(proc_lock);
1094 PROCLIST_FOREACH(p, &allproc) {
1095 /* Grab a hold on the process. */
1096 if (!rw_tryenter(&p->p_reflock, RW_READER)) {
1097 continue;
1098 }
1099 mutex_exit(proc_lock);
1100
1101 mutex_enter(p->p_lock);
1102 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
1103 if (buflen >= elem_size && elem_count > 0) {
1104 lwp_lock(l2);
1105 fill_lwp(l2, &klwp);
1106 lwp_unlock(l2);
1107 mutex_exit(p->p_lock);
1108
1109 /*
1110 * Copy out elem_size, but not
1111 * larger than the size of a
1112 * struct kinfo_proc2.
1113 */
1114 error = dcopyout(l, &klwp, dp,
1115 min(sizeof(klwp), elem_size));
1116 if (error) {
1117 rw_exit(&p->p_reflock);
1118 goto cleanup;
1119 }
1120 mutex_enter(p->p_lock);
1121 LIST_FOREACH(l3, &p->p_lwps,
1122 l_sibling) {
1123 if (l2 == l3)
1124 break;
1125 }
1126 if (l3 == NULL) {
1127 mutex_exit(p->p_lock);
1128 rw_exit(&p->p_reflock);
1129 error = EAGAIN;
1130 goto cleanup;
1131 }
1132 dp += elem_size;
1133 buflen -= elem_size;
1134 elem_count--;
1135 }
1136 needed += elem_size;
1137 }
1138 mutex_exit(p->p_lock);
1139
1140 /* Drop reference to process. */
1141 mutex_enter(proc_lock);
1142 rw_exit(&p->p_reflock);
1143 }
1144 mutex_exit(proc_lock);
1145 } else {
1146 mutex_enter(proc_lock);
1147 p = proc_find(pid);
1148 if (p == NULL) {
1149 error = ESRCH;
1150 mutex_exit(proc_lock);
1151 goto cleanup;
1152 }
1153 /* Grab a hold on the process. */
1154 gotit = rw_tryenter(&p->p_reflock, RW_READER);
1155 mutex_exit(proc_lock);
1156 if (!gotit) {
1157 error = ESRCH;
1158 goto cleanup;
1159 }
1160
1161 mutex_enter(p->p_lock);
1162 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
1163 if (buflen >= elem_size && elem_count > 0) {
1164 lwp_lock(l2);
1165 fill_lwp(l2, &klwp);
1166 lwp_unlock(l2);
1167 mutex_exit(p->p_lock);
1168 /*
1169 * Copy out elem_size, but not larger than
1170 * the size of a struct kinfo_proc2.
1171 */
1172 error = dcopyout(l, &klwp, dp,
1173 min(sizeof(klwp), elem_size));
1174 if (error) {
1175 rw_exit(&p->p_reflock);
1176 goto cleanup;
1177 }
1178 mutex_enter(p->p_lock);
1179 LIST_FOREACH(l3, &p->p_lwps, l_sibling) {
1180 if (l2 == l3)
1181 break;
1182 }
1183 if (l3 == NULL) {
1184 mutex_exit(p->p_lock);
1185 rw_exit(&p->p_reflock);
1186 error = EAGAIN;
1187 goto cleanup;
1188 }
1189 dp += elem_size;
1190 buflen -= elem_size;
1191 elem_count--;
1192 }
1193 needed += elem_size;
1194 }
1195 mutex_exit(p->p_lock);
1196
1197 /* Drop reference to process. */
1198 rw_exit(&p->p_reflock);
1199 }
1200
1201 if (where != NULL) {
1202 *oldlenp = dp - where;
1203 if (needed > *oldlenp) {
1204 sysctl_relock();
1205 return (ENOMEM);
1206 }
1207 } else {
1208 needed += KERN_LWPSLOP;
1209 *oldlenp = needed;
1210 }
1211 error = 0;
1212 cleanup:
1213 sysctl_relock();
1214 return (error);
1215 }
1216
1217 /*
1218 * sysctl helper routine for kern.forkfsleep node. Ensures that the
1219 * given value is not too large or two small, and is at least one
1220 * timer tick if not zero.
1221 */
1222 static int
1223 sysctl_kern_forkfsleep(SYSCTLFN_ARGS)
1224 {
1225 /* userland sees value in ms, internally is in ticks */
1226 extern int forkfsleep; /* defined in kern/kern_fork.c */
1227 int error, timo, lsleep;
1228 struct sysctlnode node;
1229
1230 lsleep = forkfsleep * 1000 / hz;
1231 node = *rnode;
1232 node.sysctl_data = &lsleep;
1233 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1234 if (error || newp == NULL)
1235 return (error);
1236
1237 /* refuse negative values, and overly 'long time' */
1238 if (lsleep < 0 || lsleep > MAXSLP * 1000)
1239 return (EINVAL);
1240
1241 timo = mstohz(lsleep);
1242
1243 /* if the interval is >0 ms && <1 tick, use 1 tick */
1244 if (lsleep != 0 && timo == 0)
1245 forkfsleep = 1;
1246 else
1247 forkfsleep = timo;
1248
1249 return (0);
1250 }
1251
1252 /*
1253 * sysctl helper routine for kern.root_partition
1254 */
1255 static int
1256 sysctl_kern_root_partition(SYSCTLFN_ARGS)
1257 {
1258 int rootpart = DISKPART(rootdev);
1259 struct sysctlnode node = *rnode;
1260
1261 node.sysctl_data = &rootpart;
1262 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1263 }
1264
1265 /*
1266 * sysctl helper function for kern.drivers
1267 */
1268 static int
1269 sysctl_kern_drivers(SYSCTLFN_ARGS)
1270 {
1271 int error;
1272 size_t buflen;
1273 struct kinfo_drivers kd;
1274 char *start, *where;
1275 const char *dname;
1276 int i;
1277 extern struct devsw_conv *devsw_conv;
1278 extern int max_devsw_convs;
1279
1280 start = where = oldp;
1281 buflen = *oldlenp;
1282 if (where == NULL) {
1283 *oldlenp = max_devsw_convs * sizeof kd;
1284 return 0;
1285 }
1286
1287 /*
1288 * An array of kinfo_drivers structures
1289 */
1290 error = 0;
1291 sysctl_unlock();
1292 mutex_enter(&device_lock);
1293 for (i = 0; i < max_devsw_convs; i++) {
1294 dname = devsw_conv[i].d_name;
1295 if (dname == NULL)
1296 continue;
1297 if (buflen < sizeof kd) {
1298 error = ENOMEM;
1299 break;
1300 }
1301 memset(&kd, 0, sizeof(kd));
1302 kd.d_bmajor = devsw_conv[i].d_bmajor;
1303 kd.d_cmajor = devsw_conv[i].d_cmajor;
1304 strlcpy(kd.d_name, dname, sizeof kd.d_name);
1305 mutex_exit(&device_lock);
1306 error = dcopyout(l, &kd, where, sizeof kd);
1307 mutex_enter(&device_lock);
1308 if (error != 0)
1309 break;
1310 buflen -= sizeof kd;
1311 where += sizeof kd;
1312 }
1313 mutex_exit(&device_lock);
1314 sysctl_relock();
1315 *oldlenp = where - start;
1316 return error;
1317 }
1318
1319 static int
1320 sysctl_security_setidcore(SYSCTLFN_ARGS)
1321 {
1322 int newsize, error;
1323 struct sysctlnode node;
1324
1325 node = *rnode;
1326 node.sysctl_data = &newsize;
1327 newsize = *(int *)rnode->sysctl_data;
1328 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1329 if (error || newp == NULL)
1330 return error;
1331
1332 if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
1333 0, NULL, NULL, NULL))
1334 return (EPERM);
1335
1336 *(int *)rnode->sysctl_data = newsize;
1337
1338 return 0;
1339 }
1340
1341 static int
1342 sysctl_security_setidcorename(SYSCTLFN_ARGS)
1343 {
1344 int error;
1345 char *newsetidcorename;
1346 struct sysctlnode node;
1347
1348 newsetidcorename = PNBUF_GET();
1349 node = *rnode;
1350 node.sysctl_data = newsetidcorename;
1351 memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
1352 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1353 if (error || newp == NULL) {
1354 goto out;
1355 }
1356 if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
1357 0, NULL, NULL, NULL)) {
1358 error = EPERM;
1359 goto out;
1360 }
1361 if (strlen(newsetidcorename) == 0) {
1362 error = EINVAL;
1363 goto out;
1364 }
1365 memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
1366 out:
1367 PNBUF_PUT(newsetidcorename);
1368 return error;
1369 }
1370
1371 /*
1372 * sysctl helper routine for kern.cp_id node. Maps cpus to their
1373 * cpuids.
1374 */
1375 static int
1376 sysctl_kern_cpid(SYSCTLFN_ARGS)
1377 {
1378 struct sysctlnode node = *rnode;
1379 uint64_t *cp_id = NULL;
1380 int error, n = ncpu;
1381 struct cpu_info *ci;
1382 CPU_INFO_ITERATOR cii;
1383
1384 /*
1385 * Here you may either retrieve a single cpu id or the whole
1386 * set. The size you get back when probing depends on what
1387 * you ask for.
1388 */
1389 switch (namelen) {
1390 case 0:
1391 node.sysctl_size = n * sizeof(uint64_t);
1392 n = -2; /* ALL */
1393 break;
1394 case 1:
1395 if (name[0] < 0 || name[0] >= n)
1396 return (ENOENT); /* ENOSUCHPROCESSOR */
1397 node.sysctl_size = sizeof(uint64_t);
1398 n = name[0];
1399 /*
1400 * adjust these so that sysctl_lookup() will be happy
1401 */
1402 name++;
1403 namelen--;
1404 break;
1405 default:
1406 return (EINVAL);
1407 }
1408
1409 cp_id = kmem_alloc(node.sysctl_size, KM_SLEEP);
1410 node.sysctl_data = cp_id;
1411 memset(cp_id, 0, node.sysctl_size);
1412
1413 for (CPU_INFO_FOREACH(cii, ci)) {
1414 if (n <= 0)
1415 cp_id[0] = cpu_index(ci);
1416 /*
1417 * if a specific processor was requested and we just
1418 * did it, we're done here
1419 */
1420 if (n == 0)
1421 break;
1422 /*
1423 * if doing "all", skip to next cp_id slot for next processor
1424 */
1425 if (n == -2)
1426 cp_id++;
1427 /*
1428 * if we're doing a specific processor, we're one
1429 * processor closer
1430 */
1431 if (n > 0)
1432 n--;
1433 }
1434
1435 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1436 kmem_free(node.sysctl_data, node.sysctl_size);
1437 return (error);
1438 }
1439
1440 /*
1441 * sysctl helper routine for hw.usermem and hw.usermem64. Values are
1442 * calculate on the fly taking into account integer overflow and the
1443 * current wired count.
1444 */
1445 static int
1446 sysctl_hw_usermem(SYSCTLFN_ARGS)
1447 {
1448 u_int ui;
1449 u_quad_t uq;
1450 struct sysctlnode node;
1451
1452 node = *rnode;
1453 switch (rnode->sysctl_num) {
1454 case HW_USERMEM:
1455 if ((ui = physmem - uvmexp.wired) > (UINT_MAX / PAGE_SIZE))
1456 ui = UINT_MAX;
1457 else
1458 ui *= PAGE_SIZE;
1459 node.sysctl_data = &ui;
1460 break;
1461 case HW_USERMEM64:
1462 uq = (u_quad_t)(physmem - uvmexp.wired) * PAGE_SIZE;
1463 node.sysctl_data = &uq;
1464 break;
1465 default:
1466 return (EINVAL);
1467 }
1468
1469 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1470 }
1471
1472 /*
1473 * sysctl helper routine for kern.cnmagic node. Pulls the old value
1474 * out, encoded, and stuffs the new value in for decoding.
1475 */
1476 static int
1477 sysctl_hw_cnmagic(SYSCTLFN_ARGS)
1478 {
1479 char magic[CNS_LEN];
1480 int error;
1481 struct sysctlnode node;
1482
1483 if (oldp)
1484 cn_get_magic(magic, CNS_LEN);
1485 node = *rnode;
1486 node.sysctl_data = &magic[0];
1487 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1488 if (error || newp == NULL)
1489 return (error);
1490
1491 return (cn_set_magic(magic));
1492 }
1493
1494 /*
1495 * ********************************************************************
1496 * section 3: public helper routines that are used for more than one
1497 * node
1498 * ********************************************************************
1499 */
1500
1501 /*
1502 * sysctl helper routine for the kern.root_device node and some ports'
1503 * machdep.root_device nodes.
1504 */
1505 int
1506 sysctl_root_device(SYSCTLFN_ARGS)
1507 {
1508 struct sysctlnode node;
1509
1510 node = *rnode;
1511 node.sysctl_data = __UNCONST(device_xname(root_device));
1512 node.sysctl_size = strlen(device_xname(root_device)) + 1;
1513 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1514 }
1515
1516 /*
1517 * sysctl helper routine for kern.consdev, dependent on the current
1518 * state of the console. Also used for machdep.console_device on some
1519 * ports.
1520 */
1521 int
1522 sysctl_consdev(SYSCTLFN_ARGS)
1523 {
1524 dev_t consdev;
1525 uint32_t oconsdev;
1526 struct sysctlnode node;
1527
1528 if (cn_tab != NULL)
1529 consdev = cn_tab->cn_dev;
1530 else
1531 consdev = NODEV;
1532 node = *rnode;
1533 switch (*oldlenp) {
1534 case sizeof(consdev):
1535 node.sysctl_data = &consdev;
1536 node.sysctl_size = sizeof(consdev);
1537 break;
1538 case sizeof(oconsdev):
1539 oconsdev = (uint32_t)consdev;
1540 node.sysctl_data = &oconsdev;
1541 node.sysctl_size = sizeof(oconsdev);
1542 break;
1543 default:
1544 return EINVAL;
1545 }
1546 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1547 }
1548
1549 /*
1550 * ********************************************************************
1551 * section 4: support for some helpers
1552 * ********************************************************************
1553 */
1554
1555
1556 /*
1557 * Fill in a kinfo_lwp structure for the specified lwp.
1558 */
1559 static void
1560 fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
1561 {
1562 struct proc *p = l->l_proc;
1563 struct timeval tv;
1564
1565 KASSERT(lwp_locked(l, NULL));
1566
1567 memset(kl, 0, sizeof(*kl));
1568
1569 kl->l_forw = 0;
1570 kl->l_back = 0;
1571 kl->l_laddr = PTRTOUINT64(l);
1572 kl->l_addr = PTRTOUINT64(l->l_addr);
1573 kl->l_stat = l->l_stat;
1574 kl->l_lid = l->l_lid;
1575 kl->l_flag = L_INMEM;
1576 kl->l_flag |= sysctl_map_flags(sysctl_lwpprflagmap, l->l_prflag);
1577 kl->l_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag);
1578
1579 kl->l_swtime = l->l_swtime;
1580 kl->l_slptime = l->l_slptime;
1581 if (l->l_stat == LSONPROC)
1582 kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
1583 else
1584 kl->l_schedflags = 0;
1585 kl->l_priority = lwp_eprio(l);
1586 kl->l_usrpri = l->l_priority;
1587 if (l->l_wchan)
1588 strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
1589 kl->l_wchan = PTRTOUINT64(l->l_wchan);
1590 kl->l_cpuid = cpu_index(l->l_cpu);
1591 bintime2timeval(&l->l_rtime, &tv);
1592 kl->l_rtime_sec = tv.tv_sec;
1593 kl->l_rtime_usec = tv.tv_usec;
1594 kl->l_cpticks = l->l_cpticks;
1595 kl->l_pctcpu = l->l_pctcpu;
1596 kl->l_pid = p->p_pid;
1597 if (l->l_name == NULL)
1598 kl->l_name[0] = '\0';
1599 else
1600 strlcpy(kl->l_name, l->l_name, sizeof(kl->l_name));
1601 }
1602