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