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