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