1 /* $NetBSD: init_sysctl.c,v 1.230 2026/02/15 21:47:18 christos 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.230 2026/02/15 21:47:18 christos 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 NULL, _POSIX_MONOTONIC_CLOCK, NULL, 0, 406 CTL_KERN, KERN_MONOTONIC_CLOCK, CTL_EOL); 407 sysctl_createv(clog, 0, NULL, NULL, 408 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE, 409 CTLTYPE_INT, "labelsector", 410 SYSCTL_DESCR("Sector number containing the disklabel"), 411 NULL, LABELSECTOR, NULL, 0, 412 CTL_KERN, KERN_LABELSECTOR, CTL_EOL); 413 sysctl_createv(clog, 0, NULL, NULL, 414 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE, 415 CTLTYPE_INT, "labeloffset", 416 SYSCTL_DESCR("Offset of the disklabel within the " 417 "sector"), 418 NULL, LABELOFFSET, NULL, 0, 419 CTL_KERN, KERN_LABELOFFSET, CTL_EOL); 420 sysctl_createv(clog, 0, NULL, NULL, 421 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE, 422 CTLTYPE_INT, "labelusesmbr", 423 SYSCTL_DESCR("disklabel is inside MBR partition"), 424 NULL, LABELUSESMBR, NULL, 0, 425 CTL_KERN, CTL_CREATE, CTL_EOL); 426 sysctl_createv(clog, 0, NULL, NULL, 427 CTLFLAG_PERMANENT, 428 CTLTYPE_NODE, "lwp", 429 SYSCTL_DESCR("System-wide LWP information"), 430 sysctl_kern_lwp, 0, NULL, 0, 431 CTL_KERN, KERN_LWP, CTL_EOL); 432 sysctl_createv(clog, 0, NULL, NULL, 433 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 434 CTLTYPE_INT, "forkfsleep", 435 SYSCTL_DESCR("Milliseconds to sleep on fork failure due " 436 "to process limits"), 437 sysctl_kern_forkfsleep, 0, NULL, 0, 438 CTL_KERN, KERN_FORKFSLEEP, CTL_EOL); 439 sysctl_createv(clog, 0, NULL, NULL, 440 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE, 441 CTLTYPE_INT, "posix_threads", 442 SYSCTL_DESCR("Version of IEEE Std 1003.1 and its " 443 "Threads option to which the system " 444 "attempts to conform"), 445 NULL, _POSIX_THREADS, NULL, 0, 446 CTL_KERN, KERN_POSIX_THREADS, CTL_EOL); 447 sysctl_createv(clog, 0, NULL, NULL, 448 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE, 449 CTLTYPE_INT, "posix_semaphores", 450 SYSCTL_DESCR("Version of IEEE Std 1003.1 and its " 451 "Semaphores option to which the system " 452 "attempts to conform"), NULL, 453 _POSIX_SEMAPHORES, NULL, 0, 454 CTL_KERN, KERN_POSIX_SEMAPHORES, CTL_EOL); 455 sysctl_createv(clog, 0, NULL, NULL, 456 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE, 457 CTLTYPE_INT, "posix_barriers", 458 SYSCTL_DESCR("Version of IEEE Std 1003.1 and its " 459 "Barriers option to which the system " 460 "attempts to conform"), 461 NULL, _POSIX_BARRIERS, NULL, 0, 462 CTL_KERN, KERN_POSIX_BARRIERS, CTL_EOL); 463 sysctl_createv(clog, 0, NULL, NULL, 464 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE, 465 CTLTYPE_INT, "posix_timers", 466 SYSCTL_DESCR("Version of IEEE Std 1003.1 and its " 467 "Timers option to which the system " 468 "attempts to conform"), 469 NULL, _POSIX_TIMERS, NULL, 0, 470 CTL_KERN, KERN_POSIX_TIMERS, CTL_EOL); 471 sysctl_createv(clog, 0, NULL, NULL, 472 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE, 473 CTLTYPE_INT, "posix_spin_locks", 474 SYSCTL_DESCR("Version of IEEE Std 1003.1 and its Spin " 475 "Locks option to which the system attempts " 476 "to conform"), 477 NULL, _POSIX_SPIN_LOCKS, NULL, 0, 478 CTL_KERN, KERN_POSIX_SPIN_LOCKS, CTL_EOL); 479 sysctl_createv(clog, 0, NULL, NULL, 480 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE, 481 CTLTYPE_INT, "posix_reader_writer_locks", 482 SYSCTL_DESCR("Version of IEEE Std 1003.1 and its " 483 "Read-Write Locks option to which the " 484 "system attempts to conform"), 485 NULL, _POSIX_READER_WRITER_LOCKS, NULL, 0, 486 CTL_KERN, KERN_POSIX_READER_WRITER_LOCKS, CTL_EOL); 487 sysctl_createv(clog, 0, NULL, NULL, 488 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 489 CTLTYPE_INT, "dump_on_panic", 490 SYSCTL_DESCR("Perform a crash dump on system panic"), 491 NULL, 0, &dumponpanic, 0, 492 CTL_KERN, KERN_DUMP_ON_PANIC, CTL_EOL); 493 sysctl_createv(clog, 0, NULL, NULL, 494 CTLFLAG_PERMANENT, 495 CTLTYPE_INT, "root_partition", 496 SYSCTL_DESCR("Root partition on the root device"), 497 sysctl_kern_root_partition, 0, NULL, 0, 498 CTL_KERN, KERN_ROOT_PARTITION, CTL_EOL); 499 sysctl_createv(clog, 0, NULL, NULL, 500 CTLFLAG_PERMANENT, 501 CTLTYPE_STRUCT, "drivers", 502 SYSCTL_DESCR("List of all drivers with block and " 503 "character device numbers"), 504 sysctl_kern_drivers, 0, NULL, 0, 505 CTL_KERN, KERN_DRIVERS, CTL_EOL); 506 sysctl_createv(clog, 0, NULL, NULL, 507 CTLFLAG_PERMANENT, 508 CTLTYPE_STRUCT, "cp_id", 509 SYSCTL_DESCR("Mapping of CPU number to CPU id"), 510 sysctl_kern_cpid, 0, NULL, 0, 511 CTL_KERN, KERN_CP_ID, CTL_EOL); 512 sysctl_createv(clog, 0, NULL, &rnode, 513 CTLFLAG_PERMANENT, 514 CTLTYPE_NODE, "coredump", 515 SYSCTL_DESCR("Coredump settings."), 516 NULL, 0, NULL, 0, 517 CTL_KERN, CTL_CREATE, CTL_EOL); 518 sysctl_createv(clog, 0, &rnode, &rnode, 519 CTLFLAG_PERMANENT, 520 CTLTYPE_NODE, "setid", 521 SYSCTL_DESCR("Set-id processes' coredump settings."), 522 NULL, 0, NULL, 0, 523 CTL_CREATE, CTL_EOL); 524 sysctl_createv(clog, 0, &rnode, NULL, 525 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 526 CTLTYPE_INT, "dump", 527 SYSCTL_DESCR("Allow set-id processes to dump core."), 528 sysctl_security_setidcore, 0, &security_setidcore_dump, 529 sizeof(security_setidcore_dump), 530 CTL_CREATE, CTL_EOL); 531 sysctl_createv(clog, 0, &rnode, NULL, 532 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 533 CTLTYPE_STRING, "path", 534 SYSCTL_DESCR("Path pattern for set-id coredumps."), 535 sysctl_security_setidcorename, 0, 536 security_setidcore_path, 537 sizeof(security_setidcore_path), 538 CTL_CREATE, CTL_EOL); 539 sysctl_createv(clog, 0, &rnode, NULL, 540 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 541 CTLTYPE_INT, "owner", 542 SYSCTL_DESCR("Owner id for set-id processes' cores."), 543 sysctl_security_setidcore, 0, &security_setidcore_owner, 544 0, 545 CTL_CREATE, CTL_EOL); 546 sysctl_createv(clog, 0, &rnode, NULL, 547 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 548 CTLTYPE_INT, "group", 549 SYSCTL_DESCR("Group id for set-id processes' cores."), 550 sysctl_security_setidcore, 0, &security_setidcore_group, 551 0, 552 CTL_CREATE, CTL_EOL); 553 sysctl_createv(clog, 0, &rnode, NULL, 554 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 555 CTLTYPE_INT, "mode", 556 SYSCTL_DESCR("Mode for set-id processes' cores."), 557 sysctl_security_setidcore, 0, &security_setidcore_mode, 558 0, 559 CTL_CREATE, CTL_EOL); 560 sysctl_createv(clog, 0, NULL, NULL, 561 CTLFLAG_IMMEDIATE|CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 562 CTLTYPE_INT, "no_sa_support", 563 SYSCTL_DESCR("0 if the kernel supports SA, otherwise " 564 "it doesn't"), 565 NULL, 1, NULL, 0, 566 CTL_KERN, CTL_CREATE, CTL_EOL); 567 sysctl_createv(clog, 0, NULL, NULL, 568 CTLFLAG_PERMANENT, 569 CTLTYPE_STRING, "configname", 570 SYSCTL_DESCR("Name of config file"), 571 NULL, 0, __UNCONST(kernel_ident), 0, 572 CTL_KERN, CTL_CREATE, CTL_EOL); 573 sysctl_createv(clog, 0, NULL, NULL, 574 CTLFLAG_PERMANENT, 575 CTLTYPE_STRING, "buildinfo", 576 SYSCTL_DESCR("Information from build environment"), 577 NULL, 0, __UNCONST(buildinfo), 0, 578 CTL_KERN, CTL_CREATE, CTL_EOL); 579 sysctl_createv(clog, 0, NULL, NULL, 580 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 581 CTLTYPE_INT, "messages", 582 SYSCTL_DESCR("Kernel message verbosity"), 583 sysctl_kern_messages, 0, NULL, 0, 584 CTL_KERN, CTL_CREATE, CTL_EOL); 585 } 586 587 SYSCTL_SETUP(sysctl_hw_misc_setup, "sysctl hw subtree misc setup") 588 { 589 590 sysctl_createv(clog, 0, NULL, NULL, 591 CTLFLAG_PERMANENT, 592 CTLTYPE_INT, "usermem", 593 SYSCTL_DESCR("Bytes of non-kernel memory"), 594 sysctl_hw_usermem, 0, NULL, 0, 595 CTL_HW, HW_USERMEM, CTL_EOL); 596 sysctl_createv(clog, 0, NULL, NULL, 597 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX, 598 CTLTYPE_STRING, "cnmagic", 599 SYSCTL_DESCR("Console magic key sequence"), 600 sysctl_hw_cnmagic, 0, NULL, CNS_LEN, 601 CTL_HW, HW_CNMAGIC, CTL_EOL); 602 sysctl_createv(clog, 0, NULL, NULL, 603 CTLFLAG_PERMANENT, 604 CTLTYPE_QUAD, "usermem64", 605 SYSCTL_DESCR("Bytes of non-kernel memory"), 606 sysctl_hw_usermem, 0, NULL, 0, 607 CTL_HW, HW_USERMEM64, CTL_EOL); 608 } 609 610 #ifdef DEBUG 611 /* 612 * Debugging related system variables. 613 */ 614 struct ctldebug /* debug0, */ /* debug1, */ debug2, debug3, debug4; 615 struct ctldebug debug5, debug6, debug7, debug8, debug9; 616 struct ctldebug debug10, debug11, debug12, debug13, debug14; 617 struct ctldebug debug15, debug16, debug17, debug18, debug19; 618 static struct ctldebug *debugvars[] = { 619 &debug0, &debug1, &debug2, &debug3, &debug4, 620 &debug5, &debug6, &debug7, &debug8, &debug9, 621 &debug10, &debug11, &debug12, &debug13, &debug14, 622 &debug15, &debug16, &debug17, &debug18, &debug19, 623 }; 624 625 /* 626 * this setup routine is a replacement for debug_sysctl() 627 * 628 * note that it creates several nodes per defined debug variable 629 */ 630 SYSCTL_SETUP(sysctl_debug_setup, "sysctl debug subtree setup") 631 { 632 struct ctldebug *cdp; 633 char nodename[20]; 634 int i; 635 636 /* 637 * two ways here: 638 * 639 * the "old" way (debug.name -> value) which was emulated by 640 * the sysctl(8) binary 641 * 642 * the new way, which the sysctl(8) binary was actually using 643 644 node debug 645 node debug.0 646 string debug.0.name 647 int debug.0.value 648 int debug.name 649 650 */ 651 652 for (i = 0; i < __arraycount(debugvars); i++) { 653 cdp = debugvars[i]; 654 if (cdp->debugname == NULL || cdp->debugvar == NULL) 655 continue; 656 657 snprintf(nodename, sizeof(nodename), "debug%d", i); 658 sysctl_createv(clog, 0, NULL, NULL, 659 CTLFLAG_PERMANENT|CTLFLAG_HIDDEN, 660 CTLTYPE_NODE, nodename, NULL, 661 NULL, 0, NULL, 0, 662 CTL_DEBUG, i, CTL_EOL); 663 sysctl_createv(clog, 0, NULL, NULL, 664 CTLFLAG_PERMANENT|CTLFLAG_HIDDEN, 665 CTLTYPE_STRING, "name", NULL, 666 /*XXXUNCONST*/ 667 NULL, 0, __UNCONST(cdp->debugname), 0, 668 CTL_DEBUG, i, CTL_DEBUG_NAME, CTL_EOL); 669 sysctl_createv(clog, 0, NULL, NULL, 670 CTLFLAG_PERMANENT|CTLFLAG_HIDDEN, 671 CTLTYPE_INT, "value", NULL, 672 NULL, 0, cdp->debugvar, 0, 673 CTL_DEBUG, i, CTL_DEBUG_VALUE, CTL_EOL); 674 sysctl_createv(clog, 0, NULL, NULL, 675 CTLFLAG_PERMANENT, 676 CTLTYPE_INT, cdp->debugname, NULL, 677 NULL, 0, cdp->debugvar, 0, 678 CTL_DEBUG, CTL_CREATE, CTL_EOL); 679 } 680 } 681 #endif /* DEBUG */ 682 683 /* 684 * ******************************************************************** 685 * section 2: private node-specific helper routines. 686 * ******************************************************************** 687 */ 688 689 /* 690 * sysctl helper routine for kern.maxvnodes. Drain vnodes if 691 * new value is lower than desiredvnodes and then calls reinit 692 * routines that needs to adjust to the new value. 693 */ 694 static int 695 sysctl_kern_maxvnodes(SYSCTLFN_ARGS) 696 { 697 int error, new_vnodes, old_vnodes, new_max; 698 struct sysctlnode node; 699 700 new_vnodes = desiredvnodes; 701 node = *rnode; 702 node.sysctl_data = &new_vnodes; 703 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 704 if (error || newp == NULL) 705 return (error); 706 707 /* 708 * sysctl passes down unsigned values, require them 709 * to be positive 710 */ 711 if (new_vnodes <= 0) 712 return (EINVAL); 713 714 /* Limits: 75% of kmem and physical memory. */ 715 new_max = calc_cache_size(vmem_size(kmem_arena, VMEM_FREE|VMEM_ALLOC), 716 75, 75) / VNODE_COST; 717 if (new_vnodes > new_max) 718 new_vnodes = new_max; 719 720 old_vnodes = desiredvnodes; 721 desiredvnodes = new_vnodes; 722 error = vfs_drainvnodes(); 723 if (error) { 724 desiredvnodes = old_vnodes; 725 return (error); 726 } 727 vfs_reinit(); 728 729 return (0); 730 } 731 732 /* 733 * sysctl helper routine for kern.messages. 734 * Alters boothowto to display kernel messages in increasing verbosity 735 * from 0 to 4. 736 */ 737 738 #define MAXMESSAGES 4 739 static int 740 sysctl_kern_messages(SYSCTLFN_ARGS) 741 { 742 int error, messageverbose, messagemask, newboothowto; 743 struct sysctlnode node; 744 745 messagemask = (AB_NORMAL|AB_QUIET|AB_SILENT|AB_VERBOSE|AB_DEBUG); 746 switch (boothowto & messagemask) { 747 case AB_SILENT: 748 messageverbose = 0; 749 break; 750 case AB_QUIET: 751 messageverbose = 1; 752 break; 753 case AB_VERBOSE: 754 messageverbose = 3; 755 break; 756 case AB_DEBUG: 757 messageverbose = 4; 758 break; 759 case AB_NORMAL: 760 default: 761 messageverbose = 2; 762 } 763 764 node = *rnode; 765 node.sysctl_data = &messageverbose; 766 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 767 if (error || newp == NULL) 768 return (error); 769 if (messageverbose < 0 || messageverbose > MAXMESSAGES) 770 return EINVAL; 771 772 /* Set boothowto */ 773 newboothowto = boothowto & ~messagemask; 774 775 switch (messageverbose) { 776 case 0: 777 newboothowto |= AB_SILENT; 778 break; 779 case 1: 780 newboothowto |= AB_QUIET; 781 break; 782 case 3: 783 newboothowto |= AB_VERBOSE; 784 break; 785 case 4: 786 newboothowto |= AB_DEBUG; 787 break; 788 case 2: 789 default: /* Messages default to normal. */ 790 break; 791 } 792 793 boothowto = newboothowto; 794 795 return (0); 796 } 797 798 /* 799 * sysctl helper routine for the kern.boottime node 800 */ 801 static int 802 sysctl_kern_boottime(SYSCTLFN_ARGS) 803 { 804 struct sysctlnode node; 805 struct timespec ts; 806 807 getnanoboottime(&ts); 808 node = *rnode; 809 node.sysctl_data = &ts; 810 return (sysctl_lookup(SYSCTLFN_CALL(&node))); 811 } 812 813 /* 814 * sysctl helper routine for rtc_offset - set time after changes 815 */ 816 static int 817 sysctl_kern_rtc_offset(SYSCTLFN_ARGS) 818 { 819 struct timespec ts, delta; 820 int error, new_rtc_offset; 821 struct sysctlnode node; 822 823 new_rtc_offset = rtc_offset; 824 node = *rnode; 825 node.sysctl_data = &new_rtc_offset; 826 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 827 if (error || newp == NULL) 828 return (error); 829 830 if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME, 831 KAUTH_REQ_SYSTEM_TIME_RTCOFFSET, 832 KAUTH_ARG(new_rtc_offset), NULL, NULL)) 833 return (EPERM); 834 if (rtc_offset == new_rtc_offset) 835 return (0); 836 837 /* if we change the offset, adjust the time */ 838 nanotime(&ts); 839 delta.tv_sec = 60 * (new_rtc_offset - rtc_offset); 840 delta.tv_nsec = 0; 841 timespecadd(&ts, &delta, &ts); 842 rtc_offset = new_rtc_offset; 843 return (settime(l->l_proc, &ts)); 844 } 845 846 /* 847 * sysctl helper routine for kern.maxproc. Ensures that the new 848 * values are not too low or too high. 849 */ 850 static int 851 sysctl_kern_maxproc(SYSCTLFN_ARGS) 852 { 853 int error, nmaxproc; 854 struct sysctlnode node; 855 856 nmaxproc = maxproc; 857 node = *rnode; 858 node.sysctl_data = &nmaxproc; 859 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 860 if (error || newp == NULL) 861 return (error); 862 863 if (nmaxproc < 0 || nmaxproc >= PID_MAX) 864 return (EINVAL); 865 #ifdef __HAVE_CPU_MAXPROC 866 if (nmaxproc > cpu_maxproc()) 867 return (EINVAL); 868 #endif 869 error = 0; 870 #ifdef __HAVE_MAXPROC_HOOK 871 error = cpu_maxproc_hook(nmaxproc); 872 #endif 873 if (error) 874 return error; 875 876 maxproc = nmaxproc; 877 878 return (0); 879 } 880 881 /* 882 * sysctl helper function for kern.hostid. The hostid is a long, but 883 * we export it as an int, so we need to give it a little help. 884 */ 885 static int 886 sysctl_kern_hostid(SYSCTLFN_ARGS) 887 { 888 int error, inthostid; 889 struct sysctlnode node; 890 891 inthostid = hostid; /* XXX assumes sizeof int <= sizeof long */ 892 node = *rnode; 893 node.sysctl_data = &inthostid; 894 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 895 if (error || newp == NULL) 896 return (error); 897 898 hostid = (unsigned)inthostid; 899 900 return (0); 901 } 902 903 /* 904 * sysctl helper routine for kern.defcorename. In the case of a new 905 * string being assigned, check that it's not a zero-length string. 906 * (XXX the check in -current doesn't work, but do we really care?) 907 */ 908 static int 909 sysctl_kern_defcorename(SYSCTLFN_ARGS) 910 { 911 int error; 912 char *newcorename; 913 struct sysctlnode node; 914 915 newcorename = PNBUF_GET(); 916 node = *rnode; 917 node.sysctl_data = &newcorename[0]; 918 memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN); 919 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 920 if (error || newp == NULL) { 921 goto done; 922 } 923 924 /* 925 * when sysctl_lookup() deals with a string, it's guaranteed 926 * to come back nul terminated. So there. :) 927 */ 928 if (strlen(newcorename) == 0) { 929 error = EINVAL; 930 } else { 931 memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN); 932 error = 0; 933 } 934 done: 935 PNBUF_PUT(newcorename); 936 return error; 937 } 938 939 /* 940 * sysctl helper routine for kern.cp_time node. Adds up cpu time 941 * across all cpus. 942 */ 943 static int 944 sysctl_kern_cptime(SYSCTLFN_ARGS) 945 { 946 struct sysctlnode node = *rnode; 947 uint64_t *cp_time = NULL; 948 int error, n = ncpu, i; 949 struct cpu_info *ci; 950 CPU_INFO_ITERATOR cii; 951 952 /* 953 * if you specifically pass a buffer that is the size of the 954 * sum, or if you are probing for the size, you get the "sum" 955 * of cp_time (and the size thereof) across all processors. 956 * 957 * alternately, you can pass an additional mib number and get 958 * cp_time for that particular processor. 959 */ 960 switch (namelen) { 961 case 0: 962 if (*oldlenp == sizeof(uint64_t) * CPUSTATES || oldp == NULL) { 963 node.sysctl_size = sizeof(uint64_t) * CPUSTATES; 964 n = -1; /* SUM */ 965 } 966 else { 967 node.sysctl_size = n * sizeof(uint64_t) * CPUSTATES; 968 n = -2; /* ALL */ 969 } 970 break; 971 case 1: 972 if (name[0] < 0 || name[0] >= n) 973 return (ENOENT); /* ENOSUCHPROCESSOR */ 974 node.sysctl_size = sizeof(uint64_t) * CPUSTATES; 975 n = name[0]; 976 /* 977 * adjust these so that sysctl_lookup() will be happy 978 */ 979 name++; 980 namelen--; 981 break; 982 default: 983 return (EINVAL); 984 } 985 986 cp_time = kmem_alloc(node.sysctl_size, KM_SLEEP); 987 node.sysctl_data = cp_time; 988 memset(cp_time, 0, node.sysctl_size); 989 990 for (CPU_INFO_FOREACH(cii, ci)) { 991 if (n <= 0) { 992 for (i = 0; i < CPUSTATES; i++) { 993 cp_time[i] += ci->ci_schedstate.spc_cp_time[i]; 994 } 995 } 996 /* 997 * if a specific processor was requested and we just 998 * did it, we're done here 999 */ 1000 if (n == 0) 1001 break; 1002 /* 1003 * if doing "all", skip to next cp_time set for next processor 1004 */ 1005 if (n == -2) 1006 cp_time += CPUSTATES; 1007 /* 1008 * if we're doing a specific processor, we're one 1009 * processor closer 1010 */ 1011 if (n > 0) 1012 n--; 1013 } 1014 1015 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1016 kmem_free(node.sysctl_data, node.sysctl_size); 1017 return (error); 1018 } 1019 1020 #if NPTY > 0 1021 /* 1022 * sysctl helper routine for kern.maxptys. Ensures that any new value 1023 * is acceptable to the pty subsystem. 1024 */ 1025 static int 1026 sysctl_kern_maxptys(SYSCTLFN_ARGS) 1027 { 1028 int pty_maxptys(int, int); /* defined in kern/tty_pty.c */ 1029 int error, xmax; 1030 struct sysctlnode node; 1031 1032 /* get current value of maxptys */ 1033 xmax = pty_maxptys(0, 0); 1034 1035 node = *rnode; 1036 node.sysctl_data = &xmax; 1037 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1038 if (error || newp == NULL) 1039 return (error); 1040 1041 if (xmax != pty_maxptys(xmax, 1)) 1042 return (EINVAL); 1043 1044 return (0); 1045 } 1046 #endif /* NPTY > 0 */ 1047 1048 /* 1049 * sysctl helper routine to do kern.lwp.* work. 1050 */ 1051 static int 1052 sysctl_kern_lwp(SYSCTLFN_ARGS) 1053 { 1054 struct kinfo_lwp klwp; 1055 struct proc *p; 1056 struct lwp *l2, *l3; 1057 char *where, *dp; 1058 int pid, elem_size, elem_count; 1059 int buflen, needed, error; 1060 bool gotit; 1061 1062 hash_value_ensure_initialized(); 1063 1064 if (namelen == 1 && name[0] == CTL_QUERY) 1065 return (sysctl_query(SYSCTLFN_CALL(rnode))); 1066 1067 dp = where = oldp; 1068 buflen = where != NULL ? *oldlenp : 0; 1069 error = needed = 0; 1070 1071 if (newp != NULL || namelen != 3) 1072 return (EINVAL); 1073 pid = name[0]; 1074 elem_size = name[1]; 1075 elem_count = name[2]; 1076 1077 sysctl_unlock(); 1078 if (pid == -1) { 1079 mutex_enter(&proc_lock); 1080 PROCLIST_FOREACH(p, &allproc) { 1081 /* Grab a hold on the process. */ 1082 if (!rw_tryenter(&p->p_reflock, RW_READER)) { 1083 continue; 1084 } 1085 mutex_exit(&proc_lock); 1086 1087 mutex_enter(p->p_lock); 1088 LIST_FOREACH(l2, &p->p_lwps, l_sibling) { 1089 if (buflen >= elem_size && elem_count > 0) { 1090 lwp_lock(l2); 1091 fill_lwp(l2, &klwp); 1092 lwp_unlock(l2); 1093 mutex_exit(p->p_lock); 1094 1095 /* 1096 * Copy out elem_size, but not 1097 * larger than the size of a 1098 * struct kinfo_proc2. 1099 */ 1100 error = dcopyout(l, &klwp, dp, 1101 uimin(sizeof(klwp), elem_size)); 1102 if (error) { 1103 rw_exit(&p->p_reflock); 1104 goto cleanup; 1105 } 1106 mutex_enter(p->p_lock); 1107 LIST_FOREACH(l3, &p->p_lwps, 1108 l_sibling) { 1109 if (l2 == l3) 1110 break; 1111 } 1112 if (l3 == NULL) { 1113 mutex_exit(p->p_lock); 1114 rw_exit(&p->p_reflock); 1115 error = EAGAIN; 1116 goto cleanup; 1117 } 1118 dp += elem_size; 1119 buflen -= elem_size; 1120 elem_count--; 1121 } 1122 needed += elem_size; 1123 } 1124 mutex_exit(p->p_lock); 1125 1126 /* Drop reference to process. */ 1127 mutex_enter(&proc_lock); 1128 rw_exit(&p->p_reflock); 1129 } 1130 mutex_exit(&proc_lock); 1131 } else { 1132 mutex_enter(&proc_lock); 1133 p = proc_find(pid); 1134 if (p == NULL) { 1135 error = ESRCH; 1136 mutex_exit(&proc_lock); 1137 goto cleanup; 1138 } 1139 /* Grab a hold on the process. */ 1140 gotit = rw_tryenter(&p->p_reflock, RW_READER); 1141 mutex_exit(&proc_lock); 1142 if (!gotit) { 1143 error = ESRCH; 1144 goto cleanup; 1145 } 1146 1147 mutex_enter(p->p_lock); 1148 LIST_FOREACH(l2, &p->p_lwps, l_sibling) { 1149 if (buflen >= elem_size && elem_count > 0) { 1150 lwp_lock(l2); 1151 fill_lwp(l2, &klwp); 1152 lwp_unlock(l2); 1153 mutex_exit(p->p_lock); 1154 /* 1155 * Copy out elem_size, but not larger than 1156 * the size of a struct kinfo_proc2. 1157 */ 1158 error = dcopyout(l, &klwp, dp, 1159 uimin(sizeof(klwp), elem_size)); 1160 if (error) { 1161 rw_exit(&p->p_reflock); 1162 goto cleanup; 1163 } 1164 mutex_enter(p->p_lock); 1165 LIST_FOREACH(l3, &p->p_lwps, l_sibling) { 1166 if (l2 == l3) 1167 break; 1168 } 1169 if (l3 == NULL) { 1170 mutex_exit(p->p_lock); 1171 rw_exit(&p->p_reflock); 1172 error = EAGAIN; 1173 goto cleanup; 1174 } 1175 dp += elem_size; 1176 buflen -= elem_size; 1177 elem_count--; 1178 } 1179 needed += elem_size; 1180 } 1181 mutex_exit(p->p_lock); 1182 1183 /* Drop reference to process. */ 1184 rw_exit(&p->p_reflock); 1185 } 1186 1187 if (where != NULL) { 1188 *oldlenp = dp - where; 1189 if (needed > *oldlenp) { 1190 sysctl_relock(); 1191 return (ENOMEM); 1192 } 1193 } else { 1194 needed += KERN_LWPSLOP; 1195 *oldlenp = needed; 1196 } 1197 error = 0; 1198 cleanup: 1199 sysctl_relock(); 1200 return (error); 1201 } 1202 1203 /* 1204 * sysctl helper routine for kern.forkfsleep node. Ensures that the 1205 * given value is not too large or two small, and is at least one 1206 * timer tick if not zero. 1207 */ 1208 static int 1209 sysctl_kern_forkfsleep(SYSCTLFN_ARGS) 1210 { 1211 /* userland sees value in ms, internally is in ticks */ 1212 extern int forkfsleep; /* defined in kern/kern_fork.c */ 1213 int error, timo, lsleep; 1214 struct sysctlnode node; 1215 1216 lsleep = forkfsleep * 1000 / hz; 1217 node = *rnode; 1218 node.sysctl_data = &lsleep; 1219 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1220 if (error || newp == NULL) 1221 return (error); 1222 1223 /* refuse negative values, and overly 'long time' */ 1224 if (lsleep < 0 || lsleep > MAXSLP * 1000) 1225 return (EINVAL); 1226 1227 timo = mstohz(lsleep); 1228 1229 /* if the interval is >0 ms && <1 tick, use 1 tick */ 1230 if (lsleep != 0 && timo == 0) 1231 forkfsleep = 1; 1232 else 1233 forkfsleep = timo; 1234 1235 return (0); 1236 } 1237 1238 /* 1239 * sysctl helper routine for kern.root_partition 1240 */ 1241 static int 1242 sysctl_kern_root_partition(SYSCTLFN_ARGS) 1243 { 1244 int rootpart = DISKPART(rootdev); 1245 struct sysctlnode node = *rnode; 1246 1247 node.sysctl_data = &rootpart; 1248 return (sysctl_lookup(SYSCTLFN_CALL(&node))); 1249 } 1250 1251 /* 1252 * sysctl helper function for kern.drivers 1253 */ 1254 static int 1255 sysctl_kern_drivers(SYSCTLFN_ARGS) 1256 { 1257 int error; 1258 size_t buflen; 1259 struct kinfo_drivers kd; 1260 char *start, *where; 1261 const char *dname; 1262 int i; 1263 extern struct devsw_conv *devsw_conv; 1264 extern int max_devsw_convs; 1265 1266 start = where = oldp; 1267 buflen = *oldlenp; 1268 if (where == NULL) { 1269 *oldlenp = max_devsw_convs * sizeof kd; 1270 return 0; 1271 } 1272 1273 /* 1274 * An array of kinfo_drivers structures 1275 */ 1276 error = 0; 1277 sysctl_unlock(); 1278 mutex_enter(&device_lock); 1279 for (i = 0; i < max_devsw_convs; i++) { 1280 dname = devsw_conv[i].d_name; 1281 if (dname == NULL) 1282 continue; 1283 if (buflen < sizeof kd) { 1284 error = ENOMEM; 1285 break; 1286 } 1287 memset(&kd, 0, sizeof(kd)); 1288 kd.d_bmajor = devsw_conv[i].d_bmajor; 1289 kd.d_cmajor = devsw_conv[i].d_cmajor; 1290 strlcpy(kd.d_name, dname, sizeof kd.d_name); 1291 mutex_exit(&device_lock); 1292 error = dcopyout(l, &kd, where, sizeof kd); 1293 mutex_enter(&device_lock); 1294 if (error != 0) 1295 break; 1296 buflen -= sizeof kd; 1297 where += sizeof kd; 1298 } 1299 mutex_exit(&device_lock); 1300 sysctl_relock(); 1301 *oldlenp = where - start; 1302 return error; 1303 } 1304 1305 static int 1306 sysctl_security_setidcore(SYSCTLFN_ARGS) 1307 { 1308 int newsize, error; 1309 struct sysctlnode node; 1310 1311 node = *rnode; 1312 node.sysctl_data = &newsize; 1313 newsize = *(int *)rnode->sysctl_data; 1314 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1315 if (error || newp == NULL) 1316 return error; 1317 1318 if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE, 1319 0, NULL, NULL, NULL)) 1320 return (EPERM); 1321 1322 *(int *)rnode->sysctl_data = newsize; 1323 1324 return 0; 1325 } 1326 1327 static int 1328 sysctl_security_setidcorename(SYSCTLFN_ARGS) 1329 { 1330 int error; 1331 char *newsetidcorename; 1332 struct sysctlnode node; 1333 1334 newsetidcorename = PNBUF_GET(); 1335 node = *rnode; 1336 node.sysctl_data = newsetidcorename; 1337 memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN); 1338 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1339 if (error || newp == NULL) { 1340 goto out; 1341 } 1342 if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE, 1343 0, NULL, NULL, NULL)) { 1344 error = EPERM; 1345 goto out; 1346 } 1347 if (strlen(newsetidcorename) == 0) { 1348 error = EINVAL; 1349 goto out; 1350 } 1351 memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN); 1352 out: 1353 PNBUF_PUT(newsetidcorename); 1354 return error; 1355 } 1356 1357 /* 1358 * sysctl helper routine for kern.cp_id node. Maps cpus to their 1359 * cpuids. 1360 */ 1361 static int 1362 sysctl_kern_cpid(SYSCTLFN_ARGS) 1363 { 1364 struct sysctlnode node = *rnode; 1365 uint64_t *cp_id = NULL; 1366 int error, n = ncpu; 1367 struct cpu_info *ci; 1368 CPU_INFO_ITERATOR cii; 1369 1370 /* 1371 * Here you may either retrieve a single cpu id or the whole 1372 * set. The size you get back when probing depends on what 1373 * you ask for. 1374 */ 1375 switch (namelen) { 1376 case 0: 1377 node.sysctl_size = n * sizeof(uint64_t); 1378 n = -2; /* ALL */ 1379 break; 1380 case 1: 1381 if (name[0] < 0 || name[0] >= n) 1382 return (ENOENT); /* ENOSUCHPROCESSOR */ 1383 node.sysctl_size = sizeof(uint64_t); 1384 n = name[0]; 1385 /* 1386 * adjust these so that sysctl_lookup() will be happy 1387 */ 1388 name++; 1389 namelen--; 1390 break; 1391 default: 1392 return (EINVAL); 1393 } 1394 1395 cp_id = kmem_alloc(node.sysctl_size, KM_SLEEP); 1396 node.sysctl_data = cp_id; 1397 memset(cp_id, 0, node.sysctl_size); 1398 1399 for (CPU_INFO_FOREACH(cii, ci)) { 1400 if (n <= 0) 1401 cp_id[0] = cpu_index(ci); 1402 /* 1403 * if a specific processor was requested and we just 1404 * did it, we're done here 1405 */ 1406 if (n == 0) 1407 break; 1408 /* 1409 * if doing "all", skip to next cp_id slot for next processor 1410 */ 1411 if (n == -2) 1412 cp_id++; 1413 /* 1414 * if we're doing a specific processor, we're one 1415 * processor closer 1416 */ 1417 if (n > 0) 1418 n--; 1419 } 1420 1421 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1422 kmem_free(node.sysctl_data, node.sysctl_size); 1423 return (error); 1424 } 1425 1426 /* 1427 * sysctl helper routine for hw.usermem and hw.usermem64. Values are 1428 * calculate on the fly taking into account integer overflow and the 1429 * current wired count. 1430 */ 1431 static int 1432 sysctl_hw_usermem(SYSCTLFN_ARGS) 1433 { 1434 u_int ui; 1435 u_quad_t uq; 1436 struct sysctlnode node; 1437 1438 node = *rnode; 1439 switch (rnode->sysctl_num) { 1440 case HW_USERMEM: 1441 if ((ui = physmem - uvmexp.wired) > (UINT_MAX / PAGE_SIZE)) 1442 ui = UINT_MAX; 1443 else 1444 ui *= PAGE_SIZE; 1445 node.sysctl_data = &ui; 1446 break; 1447 case HW_USERMEM64: 1448 uq = (u_quad_t)(physmem - uvmexp.wired) * PAGE_SIZE; 1449 node.sysctl_data = &uq; 1450 break; 1451 default: 1452 return (EINVAL); 1453 } 1454 1455 return (sysctl_lookup(SYSCTLFN_CALL(&node))); 1456 } 1457 1458 /* 1459 * sysctl helper routine for kern.cnmagic node. Pulls the old value 1460 * out, encoded, and stuffs the new value in for decoding. 1461 */ 1462 static int 1463 sysctl_hw_cnmagic(SYSCTLFN_ARGS) 1464 { 1465 char magic[CNS_LEN]; 1466 int error; 1467 struct sysctlnode node; 1468 1469 if (oldp) 1470 cn_get_magic(magic, CNS_LEN); 1471 node = *rnode; 1472 node.sysctl_data = &magic[0]; 1473 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 1474 if (error || newp == NULL) 1475 return (error); 1476 1477 return (cn_set_magic(magic)); 1478 } 1479 1480 /* 1481 * ******************************************************************** 1482 * section 3: public helper routines that are used for more than one 1483 * node 1484 * ******************************************************************** 1485 */ 1486 1487 /* 1488 * sysctl helper routine for the kern.root_device node and some ports' 1489 * machdep.root_device nodes. 1490 */ 1491 int 1492 sysctl_root_device(SYSCTLFN_ARGS) 1493 { 1494 struct sysctlnode node; 1495 1496 node = *rnode; 1497 node.sysctl_data = __UNCONST(device_xname(root_device)); 1498 node.sysctl_size = strlen(device_xname(root_device)) + 1; 1499 return (sysctl_lookup(SYSCTLFN_CALL(&node))); 1500 } 1501 1502 /* 1503 * sysctl helper routine for kern.consdev, dependent on the current 1504 * state of the console. Also used for machdep.console_device on some 1505 * ports. 1506 */ 1507 int 1508 sysctl_consdev(SYSCTLFN_ARGS) 1509 { 1510 dev_t consdev; 1511 uint32_t oconsdev; 1512 struct sysctlnode node; 1513 1514 if (cn_tab != NULL) 1515 consdev = cn_tab->cn_dev; 1516 else 1517 consdev = NODEV; 1518 node = *rnode; 1519 switch (*oldlenp) { 1520 case sizeof(consdev): 1521 node.sysctl_data = &consdev; 1522 node.sysctl_size = sizeof(consdev); 1523 break; 1524 case sizeof(oconsdev): 1525 oconsdev = (uint32_t)consdev; 1526 node.sysctl_data = &oconsdev; 1527 node.sysctl_size = sizeof(oconsdev); 1528 break; 1529 default: 1530 return EINVAL; 1531 } 1532 return (sysctl_lookup(SYSCTLFN_CALL(&node))); 1533 } 1534 1535 /* 1536 * ******************************************************************** 1537 * section 4: support for some helpers 1538 * ******************************************************************** 1539 */ 1540 1541 1542 /* 1543 * Fill in a kinfo_lwp structure for the specified lwp. 1544 */ 1545 static void 1546 fill_lwp(struct lwp *l, struct kinfo_lwp *kl) 1547 { 1548 const bool allowaddr = get_expose_address(curproc); 1549 struct proc *p = l->l_proc; 1550 struct timeval tv; 1551 1552 KASSERT(lwp_locked(l, NULL)); 1553 1554 memset(kl, 0, sizeof(*kl)); 1555 1556 kl->l_forw = 0; 1557 kl->l_back = 0; 1558 COND_SET_VALUE(kl->l_laddr, PTRTOUINT64(l), allowaddr); 1559 COND_SET_VALUE(kl->l_addr, PTRTOUINT64(l->l_addr), allowaddr); 1560 kl->l_stat = l->l_stat; 1561 kl->l_lid = l->l_lid; 1562 kl->l_flag = L_INMEM; 1563 kl->l_flag |= sysctl_map_flags(sysctl_lwpprflagmap, l->l_prflag); 1564 kl->l_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag); 1565 1566 kl->l_swtime = l->l_swtime; 1567 kl->l_slptime = l->l_slptime; 1568 if (l->l_stat == LSONPROC) 1569 kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags; 1570 else 1571 kl->l_schedflags = 0; 1572 kl->l_priority = lwp_eprio(l); 1573 kl->l_usrpri = l->l_priority; 1574 if (l->l_wchan) 1575 strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg)); 1576 COND_SET_VALUE(kl->l_wchan, PTRTOUINT64(l->l_wchan), allowaddr); 1577 kl->l_cpuid = cpu_index(l->l_cpu); 1578 bintime2timeval(&l->l_rtime, &tv); 1579 kl->l_rtime_sec = tv.tv_sec; 1580 kl->l_rtime_usec = tv.tv_usec; 1581 kl->l_cpticks = l->l_cpticks; 1582 kl->l_pctcpu = l->l_pctcpu; 1583 kl->l_pid = p->p_pid; 1584 if (l->l_name == NULL) 1585 kl->l_name[0] = '\0'; 1586 else 1587 strlcpy(kl->l_name, l->l_name, sizeof(kl->l_name)); 1588 } 1589