1 /* $OpenBSD: kern_sysctl.c,v 1.358 2019/06/01 14:11:17 mpi Exp $ */ 2 /* $NetBSD: kern_sysctl.c,v 1.17 1996/05/20 17:49:05 mrg Exp $ */ 3 4 /*- 5 * Copyright (c) 1982, 1986, 1989, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Mike Karels at Berkeley Software Design, Inc. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)kern_sysctl.c 8.4 (Berkeley) 4/14/94 36 */ 37 38 /* 39 * sysctl system call. 40 */ 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/kernel.h> 45 #include <sys/malloc.h> 46 #include <sys/pool.h> 47 #include <sys/proc.h> 48 #include <sys/resourcevar.h> 49 #include <sys/signalvar.h> 50 #include <sys/fcntl.h> 51 #include <sys/file.h> 52 #include <sys/filedesc.h> 53 #include <sys/vnode.h> 54 #include <sys/unistd.h> 55 #include <sys/buf.h> 56 #include <sys/ioctl.h> 57 #include <sys/tty.h> 58 #include <sys/disklabel.h> 59 #include <sys/disk.h> 60 #include <sys/sysctl.h> 61 #include <sys/msgbuf.h> 62 #include <sys/vmmeter.h> 63 #include <sys/namei.h> 64 #include <sys/exec.h> 65 #include <sys/mbuf.h> 66 #include <sys/percpu.h> 67 #include <sys/sensors.h> 68 #include <sys/pipe.h> 69 #include <sys/eventvar.h> 70 #include <sys/socketvar.h> 71 #include <sys/socket.h> 72 #include <sys/domain.h> 73 #include <sys/protosw.h> 74 #include <sys/pledge.h> 75 #include <sys/timetc.h> 76 #include <sys/evcount.h> 77 #include <sys/un.h> 78 #include <sys/unpcb.h> 79 #include <sys/sched.h> 80 #include <sys/mount.h> 81 #include <sys/syscallargs.h> 82 #include <sys/witness.h> 83 84 #include <uvm/uvm_extern.h> 85 86 #include <dev/cons.h> 87 #include <dev/rndvar.h> 88 89 #include <net/route.h> 90 #include <netinet/in.h> 91 #include <netinet/ip.h> 92 #include <netinet/ip_var.h> 93 #include <netinet/in_pcb.h> 94 #include <netinet/ip6.h> 95 #include <netinet/tcp.h> 96 #include <netinet/tcp_timer.h> 97 #include <netinet/tcp_var.h> 98 #include <netinet/udp.h> 99 #include <netinet/udp_var.h> 100 #include <netinet6/ip6_var.h> 101 102 #ifdef DDB 103 #include <ddb/db_var.h> 104 #endif 105 106 #ifdef SYSVMSG 107 #include <sys/msg.h> 108 #endif 109 #ifdef SYSVSEM 110 #include <sys/sem.h> 111 #endif 112 #ifdef SYSVSHM 113 #include <sys/shm.h> 114 #endif 115 116 #include "audio.h" 117 #include "pf.h" 118 119 extern struct forkstat forkstat; 120 extern struct nchstats nchstats; 121 extern int nselcoll, fscale; 122 extern struct disklist_head disklist; 123 extern fixpt_t ccpu; 124 extern long numvnodes; 125 extern u_int net_livelocks; 126 #if NAUDIO > 0 127 extern int audio_record_enable; 128 #endif 129 130 int allowkmem; 131 132 extern void nmbclust_update(void); 133 134 int sysctl_diskinit(int, struct proc *); 135 int sysctl_proc_args(int *, u_int, void *, size_t *, struct proc *); 136 int sysctl_proc_cwd(int *, u_int, void *, size_t *, struct proc *); 137 int sysctl_proc_nobroadcastkill(int *, u_int, void *, size_t, void *, size_t *, 138 struct proc *); 139 int sysctl_proc_vmmap(int *, u_int, void *, size_t *, struct proc *); 140 int sysctl_intrcnt(int *, u_int, void *, size_t *); 141 int sysctl_sensors(int *, u_int, void *, size_t *, void *, size_t); 142 int sysctl_cptime2(int *, u_int, void *, size_t *, void *, size_t); 143 #if NAUDIO > 0 144 int sysctl_audio(int *, u_int, void *, size_t *, void *, size_t); 145 #endif 146 int sysctl_cpustats(int *, u_int, void *, size_t *, void *, size_t); 147 148 void fill_file(struct kinfo_file *, struct file *, struct filedesc *, int, 149 struct vnode *, struct process *, struct proc *, struct socket *, int); 150 void fill_kproc(struct process *, struct kinfo_proc *, struct proc *, int); 151 152 int (*cpu_cpuspeed)(int *); 153 154 /* 155 * Lock to avoid too many processes vslocking a large amount of memory 156 * at the same time. 157 */ 158 struct rwlock sysctl_lock = RWLOCK_INITIALIZER("sysctllk"); 159 struct rwlock sysctl_disklock = RWLOCK_INITIALIZER("sysctldlk"); 160 161 int 162 sys_sysctl(struct proc *p, void *v, register_t *retval) 163 { 164 struct sys_sysctl_args /* { 165 syscallarg(const int *) name; 166 syscallarg(u_int) namelen; 167 syscallarg(void *) old; 168 syscallarg(size_t *) oldlenp; 169 syscallarg(void *) new; 170 syscallarg(size_t) newlen; 171 } */ *uap = v; 172 int error, dolock = 1; 173 size_t savelen = 0, oldlen = 0; 174 sysctlfn *fn; 175 int name[CTL_MAXNAME]; 176 177 if (SCARG(uap, new) != NULL && 178 (error = suser(p))) 179 return (error); 180 /* 181 * all top-level sysctl names are non-terminal 182 */ 183 if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 2) 184 return (EINVAL); 185 error = copyin(SCARG(uap, name), name, 186 SCARG(uap, namelen) * sizeof(int)); 187 if (error) 188 return (error); 189 190 error = pledge_sysctl(p, SCARG(uap, namelen), 191 name, SCARG(uap, new)); 192 if (error) 193 return (error); 194 195 switch (name[0]) { 196 case CTL_KERN: 197 fn = kern_sysctl; 198 break; 199 case CTL_HW: 200 fn = hw_sysctl; 201 break; 202 case CTL_VM: 203 fn = uvm_sysctl; 204 break; 205 case CTL_NET: 206 fn = net_sysctl; 207 break; 208 case CTL_FS: 209 fn = fs_sysctl; 210 break; 211 case CTL_VFS: 212 fn = vfs_sysctl; 213 break; 214 case CTL_MACHDEP: 215 fn = cpu_sysctl; 216 break; 217 #ifdef DEBUG 218 case CTL_DEBUG: 219 fn = debug_sysctl; 220 break; 221 #endif 222 #ifdef DDB 223 case CTL_DDB: 224 fn = ddb_sysctl; 225 break; 226 #endif 227 default: 228 return (EOPNOTSUPP); 229 } 230 231 if (SCARG(uap, oldlenp) && 232 (error = copyin(SCARG(uap, oldlenp), &oldlen, sizeof(oldlen)))) 233 return (error); 234 if (SCARG(uap, old) != NULL) { 235 if ((error = rw_enter(&sysctl_lock, RW_WRITE|RW_INTR)) != 0) 236 return (error); 237 if (dolock) { 238 if (atop(oldlen) > uvmexp.wiredmax - uvmexp.wired) { 239 rw_exit_write(&sysctl_lock); 240 return (ENOMEM); 241 } 242 error = uvm_vslock(p, SCARG(uap, old), oldlen, 243 PROT_READ | PROT_WRITE); 244 if (error) { 245 rw_exit_write(&sysctl_lock); 246 return (error); 247 } 248 } 249 savelen = oldlen; 250 } 251 error = (*fn)(&name[1], SCARG(uap, namelen) - 1, SCARG(uap, old), 252 &oldlen, SCARG(uap, new), SCARG(uap, newlen), p); 253 if (SCARG(uap, old) != NULL) { 254 if (dolock) 255 uvm_vsunlock(p, SCARG(uap, old), savelen); 256 rw_exit_write(&sysctl_lock); 257 } 258 if (error) 259 return (error); 260 if (SCARG(uap, oldlenp)) 261 error = copyout(&oldlen, SCARG(uap, oldlenp), sizeof(oldlen)); 262 return (error); 263 } 264 265 /* 266 * Attributes stored in the kernel. 267 */ 268 char hostname[MAXHOSTNAMELEN]; 269 int hostnamelen; 270 char domainname[MAXHOSTNAMELEN]; 271 int domainnamelen; 272 long hostid; 273 char *disknames = NULL; 274 size_t disknameslen; 275 struct diskstats *diskstats = NULL; 276 size_t diskstatslen; 277 int securelevel; 278 279 /* 280 * kernel related system variables. 281 */ 282 int 283 kern_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, 284 size_t newlen, struct proc *p) 285 { 286 int error, level, inthostid, stackgap; 287 dev_t dev; 288 extern int somaxconn, sominconn; 289 extern int nosuidcoredump; 290 extern int maxlocksperuid; 291 extern int pool_debug; 292 extern int uvm_wxabort; 293 294 /* all sysctl names at this level are terminal except a ton of them */ 295 if (namelen != 1) { 296 switch (name[0]) { 297 case KERN_PROC: 298 case KERN_PROF: 299 case KERN_MALLOCSTATS: 300 case KERN_TTY: 301 case KERN_POOL: 302 case KERN_PROC_ARGS: 303 case KERN_PROC_CWD: 304 case KERN_PROC_NOBROADCASTKILL: 305 case KERN_PROC_VMMAP: 306 case KERN_SYSVIPC_INFO: 307 case KERN_SEMINFO: 308 case KERN_SHMINFO: 309 case KERN_INTRCNT: 310 case KERN_WATCHDOG: 311 case KERN_EVCOUNT: 312 case KERN_TIMECOUNTER: 313 case KERN_CPTIME2: 314 case KERN_FILE: 315 case KERN_WITNESS: 316 case KERN_AUDIO: 317 case KERN_CPUSTATS: 318 break; 319 default: 320 return (ENOTDIR); /* overloaded */ 321 } 322 } 323 324 switch (name[0]) { 325 case KERN_OSTYPE: 326 return (sysctl_rdstring(oldp, oldlenp, newp, ostype)); 327 case KERN_OSRELEASE: 328 return (sysctl_rdstring(oldp, oldlenp, newp, osrelease)); 329 case KERN_OSREV: 330 return (sysctl_rdint(oldp, oldlenp, newp, OpenBSD)); 331 case KERN_OSVERSION: 332 return (sysctl_rdstring(oldp, oldlenp, newp, osversion)); 333 case KERN_VERSION: 334 return (sysctl_rdstring(oldp, oldlenp, newp, version)); 335 case KERN_MAXVNODES: 336 return(sysctl_int(oldp, oldlenp, newp, newlen, &maxvnodes)); 337 case KERN_MAXPROC: 338 return (sysctl_int(oldp, oldlenp, newp, newlen, &maxprocess)); 339 case KERN_MAXFILES: 340 return (sysctl_int(oldp, oldlenp, newp, newlen, &maxfiles)); 341 case KERN_NFILES: 342 return (sysctl_rdint(oldp, oldlenp, newp, numfiles)); 343 case KERN_TTYCOUNT: 344 return (sysctl_rdint(oldp, oldlenp, newp, tty_count)); 345 case KERN_NUMVNODES: 346 return (sysctl_rdint(oldp, oldlenp, newp, numvnodes)); 347 case KERN_ARGMAX: 348 return (sysctl_rdint(oldp, oldlenp, newp, ARG_MAX)); 349 case KERN_NSELCOLL: 350 return (sysctl_rdint(oldp, oldlenp, newp, nselcoll)); 351 case KERN_SECURELVL: 352 level = securelevel; 353 if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &level)) || 354 newp == NULL) 355 return (error); 356 if ((securelevel > 0 || level < -1) && 357 level < securelevel && p->p_p->ps_pid != 1) 358 return (EPERM); 359 securelevel = level; 360 return (0); 361 case KERN_ALLOWKMEM: 362 if (securelevel > 0) 363 return (sysctl_rdint(oldp, oldlenp, newp, 364 allowkmem)); 365 return (sysctl_int(oldp, oldlenp, newp, newlen, 366 &allowkmem)); 367 case KERN_HOSTNAME: 368 error = sysctl_tstring(oldp, oldlenp, newp, newlen, 369 hostname, sizeof(hostname)); 370 if (newp && !error) 371 hostnamelen = newlen; 372 return (error); 373 case KERN_DOMAINNAME: 374 error = sysctl_tstring(oldp, oldlenp, newp, newlen, 375 domainname, sizeof(domainname)); 376 if (newp && !error) 377 domainnamelen = newlen; 378 return (error); 379 case KERN_HOSTID: 380 inthostid = hostid; /* XXX assumes sizeof long <= sizeof int */ 381 error = sysctl_int(oldp, oldlenp, newp, newlen, &inthostid); 382 hostid = inthostid; 383 return (error); 384 case KERN_CLOCKRATE: 385 return (sysctl_clockrate(oldp, oldlenp, newp)); 386 case KERN_BOOTTIME: { 387 struct timeval bt; 388 memset(&bt, 0, sizeof bt); 389 microboottime(&bt); 390 return (sysctl_rdstruct(oldp, oldlenp, newp, &bt, sizeof bt)); 391 } 392 #ifndef SMALL_KERNEL 393 case KERN_PROC: 394 return (sysctl_doproc(name + 1, namelen - 1, oldp, oldlenp)); 395 case KERN_PROC_ARGS: 396 return (sysctl_proc_args(name + 1, namelen - 1, oldp, oldlenp, 397 p)); 398 case KERN_PROC_CWD: 399 return (sysctl_proc_cwd(name + 1, namelen - 1, oldp, oldlenp, 400 p)); 401 case KERN_PROC_NOBROADCASTKILL: 402 return (sysctl_proc_nobroadcastkill(name + 1, namelen - 1, 403 newp, newlen, oldp, oldlenp, p)); 404 case KERN_PROC_VMMAP: 405 return (sysctl_proc_vmmap(name + 1, namelen - 1, oldp, oldlenp, 406 p)); 407 case KERN_FILE: 408 return (sysctl_file(name + 1, namelen - 1, oldp, oldlenp, p)); 409 #endif 410 case KERN_MBSTAT: { 411 extern struct cpumem *mbstat; 412 uint64_t counters[MBSTAT_COUNT]; 413 struct mbstat mbs; 414 unsigned int i; 415 416 memset(&mbs, 0, sizeof(mbs)); 417 counters_read(mbstat, counters, MBSTAT_COUNT); 418 for (i = 0; i < MBSTAT_TYPES; i++) 419 mbs.m_mtypes[i] = counters[i]; 420 421 mbs.m_drops = counters[MBSTAT_DROPS]; 422 mbs.m_wait = counters[MBSTAT_WAIT]; 423 mbs.m_drain = counters[MBSTAT_DRAIN]; 424 425 return (sysctl_rdstruct(oldp, oldlenp, newp, 426 &mbs, sizeof(mbs))); 427 } 428 #if defined(GPROF) || defined(DDBPROF) 429 case KERN_PROF: 430 return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp, 431 newp, newlen)); 432 #endif 433 case KERN_POSIX1: 434 return (sysctl_rdint(oldp, oldlenp, newp, _POSIX_VERSION)); 435 case KERN_NGROUPS: 436 return (sysctl_rdint(oldp, oldlenp, newp, NGROUPS_MAX)); 437 case KERN_JOB_CONTROL: 438 return (sysctl_rdint(oldp, oldlenp, newp, 1)); 439 case KERN_SAVED_IDS: 440 return (sysctl_rdint(oldp, oldlenp, newp, 1)); 441 case KERN_MAXPARTITIONS: 442 return (sysctl_rdint(oldp, oldlenp, newp, MAXPARTITIONS)); 443 case KERN_RAWPARTITION: 444 return (sysctl_rdint(oldp, oldlenp, newp, RAW_PART)); 445 case KERN_MAXTHREAD: 446 return (sysctl_int(oldp, oldlenp, newp, newlen, &maxthread)); 447 case KERN_NTHREADS: 448 return (sysctl_rdint(oldp, oldlenp, newp, nthreads)); 449 case KERN_SOMAXCONN: { 450 int val = somaxconn; 451 error = sysctl_int(oldp, oldlenp, newp, newlen, &val); 452 if (error) 453 return error; 454 if (val < 0 || val > SHRT_MAX) 455 return EINVAL; 456 somaxconn = val; 457 return 0; 458 } 459 case KERN_SOMINCONN: { 460 int val = sominconn; 461 error = sysctl_int(oldp, oldlenp, newp, newlen, &val); 462 if (error) 463 return error; 464 if (val < 0 || val > SHRT_MAX) 465 return EINVAL; 466 sominconn = val; 467 return 0; 468 } 469 case KERN_NOSUIDCOREDUMP: 470 return (sysctl_int(oldp, oldlenp, newp, newlen, &nosuidcoredump)); 471 case KERN_FSYNC: 472 return (sysctl_rdint(oldp, oldlenp, newp, 1)); 473 case KERN_SYSVMSG: 474 #ifdef SYSVMSG 475 return (sysctl_rdint(oldp, oldlenp, newp, 1)); 476 #else 477 return (sysctl_rdint(oldp, oldlenp, newp, 0)); 478 #endif 479 case KERN_SYSVSEM: 480 #ifdef SYSVSEM 481 return (sysctl_rdint(oldp, oldlenp, newp, 1)); 482 #else 483 return (sysctl_rdint(oldp, oldlenp, newp, 0)); 484 #endif 485 case KERN_SYSVSHM: 486 #ifdef SYSVSHM 487 return (sysctl_rdint(oldp, oldlenp, newp, 1)); 488 #else 489 return (sysctl_rdint(oldp, oldlenp, newp, 0)); 490 #endif 491 case KERN_MSGBUFSIZE: 492 case KERN_CONSBUFSIZE: { 493 struct msgbuf *mp; 494 mp = (name[0] == KERN_MSGBUFSIZE) ? msgbufp : consbufp; 495 /* 496 * deal with cases where the message buffer has 497 * become corrupted. 498 */ 499 if (!mp || mp->msg_magic != MSG_MAGIC) 500 return (ENXIO); 501 return (sysctl_rdint(oldp, oldlenp, newp, mp->msg_bufs)); 502 } 503 case KERN_CONSBUF: 504 if ((error = suser(p))) 505 return (error); 506 /* FALLTHROUGH */ 507 case KERN_MSGBUF: { 508 struct msgbuf *mp; 509 mp = (name[0] == KERN_MSGBUF) ? msgbufp : consbufp; 510 /* see note above */ 511 if (!mp || mp->msg_magic != MSG_MAGIC) 512 return (ENXIO); 513 return (sysctl_rdstruct(oldp, oldlenp, newp, mp, 514 mp->msg_bufs + offsetof(struct msgbuf, msg_bufc))); 515 } 516 case KERN_MALLOCSTATS: 517 return (sysctl_malloc(name + 1, namelen - 1, oldp, oldlenp, 518 newp, newlen, p)); 519 case KERN_CPTIME: 520 { 521 CPU_INFO_ITERATOR cii; 522 struct cpu_info *ci; 523 long cp_time[CPUSTATES]; 524 int i; 525 526 memset(cp_time, 0, sizeof(cp_time)); 527 528 CPU_INFO_FOREACH(cii, ci) { 529 for (i = 0; i < CPUSTATES; i++) 530 cp_time[i] += ci->ci_schedstate.spc_cp_time[i]; 531 } 532 533 for (i = 0; i < CPUSTATES; i++) 534 cp_time[i] /= ncpus; 535 536 return (sysctl_rdstruct(oldp, oldlenp, newp, &cp_time, 537 sizeof(cp_time))); 538 } 539 case KERN_NCHSTATS: 540 return (sysctl_rdstruct(oldp, oldlenp, newp, &nchstats, 541 sizeof(struct nchstats))); 542 case KERN_FORKSTAT: 543 return (sysctl_rdstruct(oldp, oldlenp, newp, &forkstat, 544 sizeof(struct forkstat))); 545 case KERN_TTY: 546 return (sysctl_tty(name + 1, namelen - 1, oldp, oldlenp, 547 newp, newlen)); 548 case KERN_FSCALE: 549 return (sysctl_rdint(oldp, oldlenp, newp, fscale)); 550 case KERN_CCPU: 551 return (sysctl_rdint(oldp, oldlenp, newp, ccpu)); 552 case KERN_NPROCS: 553 return (sysctl_rdint(oldp, oldlenp, newp, nprocesses)); 554 case KERN_POOL: 555 return (sysctl_dopool(name + 1, namelen - 1, oldp, oldlenp)); 556 case KERN_STACKGAPRANDOM: 557 stackgap = stackgap_random; 558 error = sysctl_int(oldp, oldlenp, newp, newlen, &stackgap); 559 if (error) 560 return (error); 561 /* 562 * Safety harness. 563 */ 564 if ((stackgap < ALIGNBYTES && stackgap != 0) || 565 !powerof2(stackgap) || stackgap >= MAXSSIZ) 566 return (EINVAL); 567 stackgap_random = stackgap; 568 return (0); 569 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM) 570 case KERN_SYSVIPC_INFO: 571 return (sysctl_sysvipc(name + 1, namelen - 1, oldp, oldlenp)); 572 #endif 573 case KERN_SPLASSERT: 574 return (sysctl_int(oldp, oldlenp, newp, newlen, 575 &splassert_ctl)); 576 #ifdef SYSVSEM 577 case KERN_SEMINFO: 578 return (sysctl_sysvsem(name + 1, namelen - 1, oldp, oldlenp, 579 newp, newlen)); 580 #endif 581 #ifdef SYSVSHM 582 case KERN_SHMINFO: 583 return (sysctl_sysvshm(name + 1, namelen - 1, oldp, oldlenp, 584 newp, newlen)); 585 #endif 586 #ifndef SMALL_KERNEL 587 case KERN_INTRCNT: 588 return (sysctl_intrcnt(name + 1, namelen - 1, oldp, oldlenp)); 589 case KERN_WATCHDOG: 590 return (sysctl_wdog(name + 1, namelen - 1, oldp, oldlenp, 591 newp, newlen)); 592 #endif 593 case KERN_MAXCLUSTERS: 594 error = sysctl_int(oldp, oldlenp, newp, newlen, &nmbclust); 595 if (!error) 596 nmbclust_update(); 597 return (error); 598 #ifndef SMALL_KERNEL 599 case KERN_EVCOUNT: 600 return (evcount_sysctl(name + 1, namelen - 1, oldp, oldlenp, 601 newp, newlen)); 602 #endif 603 case KERN_TIMECOUNTER: 604 return (sysctl_tc(name + 1, namelen - 1, oldp, oldlenp, 605 newp, newlen)); 606 case KERN_MAXLOCKSPERUID: 607 return (sysctl_int(oldp, oldlenp, newp, newlen, &maxlocksperuid)); 608 case KERN_CPTIME2: 609 return (sysctl_cptime2(name + 1, namelen -1, oldp, oldlenp, 610 newp, newlen)); 611 case KERN_CACHEPCT: { 612 u_int64_t dmapages; 613 int opct, pgs; 614 opct = bufcachepercent; 615 error = sysctl_int(oldp, oldlenp, newp, newlen, 616 &bufcachepercent); 617 if (error) 618 return(error); 619 if (bufcachepercent > 90 || bufcachepercent < 5) { 620 bufcachepercent = opct; 621 return (EINVAL); 622 } 623 dmapages = uvm_pagecount(&dma_constraint); 624 if (bufcachepercent != opct) { 625 pgs = bufcachepercent * dmapages / 100; 626 bufadjust(pgs); /* adjust bufpages */ 627 bufhighpages = bufpages; /* set high water mark */ 628 } 629 return(0); 630 } 631 case KERN_WXABORT: 632 return (sysctl_int(oldp, oldlenp, newp, newlen, &uvm_wxabort)); 633 case KERN_CONSDEV: 634 if (cn_tab != NULL) 635 dev = cn_tab->cn_dev; 636 else 637 dev = NODEV; 638 return sysctl_rdstruct(oldp, oldlenp, newp, &dev, sizeof(dev)); 639 case KERN_NETLIVELOCKS: 640 return (sysctl_rdint(oldp, oldlenp, newp, net_livelocks)); 641 case KERN_POOL_DEBUG: { 642 int old_pool_debug = pool_debug; 643 644 error = sysctl_int(oldp, oldlenp, newp, newlen, 645 &pool_debug); 646 if (error == 0 && pool_debug != old_pool_debug) 647 pool_reclaim_all(); 648 return (error); 649 } 650 #ifdef PTRACE 651 case KERN_GLOBAL_PTRACE: { 652 extern int global_ptrace; 653 654 return sysctl_int(oldp, oldlenp, newp, newlen, &global_ptrace); 655 } 656 #endif 657 #ifdef WITNESS 658 case KERN_WITNESSWATCH: 659 return witness_sysctl_watch(oldp, oldlenp, newp, newlen); 660 case KERN_WITNESS: 661 return witness_sysctl(name + 1, namelen - 1, oldp, oldlenp, 662 newp, newlen); 663 #endif 664 #if NAUDIO > 0 665 case KERN_AUDIO: 666 return (sysctl_audio(name + 1, namelen - 1, oldp, oldlenp, 667 newp, newlen)); 668 #endif 669 case KERN_CPUSTATS: 670 return (sysctl_cpustats(name + 1, namelen - 1, oldp, oldlenp, 671 newp, newlen)); 672 #if NPF > 0 673 case KERN_PFSTATUS: 674 return (pf_sysctl(oldp, oldlenp, newp, newlen)); 675 #endif 676 default: 677 return (EOPNOTSUPP); 678 } 679 /* NOTREACHED */ 680 } 681 682 /* 683 * hardware related system variables. 684 */ 685 char *hw_vendor, *hw_prod, *hw_uuid, *hw_serial, *hw_ver; 686 int allowpowerdown = 1; 687 688 int 689 hw_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, 690 size_t newlen, struct proc *p) 691 { 692 extern char machine[], cpu_model[]; 693 int err, cpuspeed; 694 695 /* all sysctl names at this level except sensors are terminal */ 696 if (name[0] != HW_SENSORS && namelen != 1) 697 return (ENOTDIR); /* overloaded */ 698 699 switch (name[0]) { 700 case HW_MACHINE: 701 return (sysctl_rdstring(oldp, oldlenp, newp, machine)); 702 case HW_MODEL: 703 return (sysctl_rdstring(oldp, oldlenp, newp, cpu_model)); 704 case HW_NCPU: 705 return (sysctl_rdint(oldp, oldlenp, newp, ncpus)); 706 case HW_NCPUFOUND: 707 return (sysctl_rdint(oldp, oldlenp, newp, ncpusfound)); 708 case HW_NCPUONLINE: 709 return (sysctl_rdint(oldp, oldlenp, newp, 710 sysctl_hwncpuonline())); 711 case HW_BYTEORDER: 712 return (sysctl_rdint(oldp, oldlenp, newp, BYTE_ORDER)); 713 case HW_PHYSMEM: 714 return (sysctl_rdint(oldp, oldlenp, newp, ptoa(physmem))); 715 case HW_USERMEM: 716 return (sysctl_rdint(oldp, oldlenp, newp, 717 ptoa(physmem - uvmexp.wired))); 718 case HW_PAGESIZE: 719 return (sysctl_rdint(oldp, oldlenp, newp, PAGE_SIZE)); 720 case HW_DISKNAMES: 721 err = sysctl_diskinit(0, p); 722 if (err) 723 return err; 724 if (disknames) 725 return (sysctl_rdstring(oldp, oldlenp, newp, 726 disknames)); 727 else 728 return (sysctl_rdstring(oldp, oldlenp, newp, "")); 729 case HW_DISKSTATS: 730 err = sysctl_diskinit(1, p); 731 if (err) 732 return err; 733 return (sysctl_rdstruct(oldp, oldlenp, newp, diskstats, 734 disk_count * sizeof(struct diskstats))); 735 case HW_DISKCOUNT: 736 return (sysctl_rdint(oldp, oldlenp, newp, disk_count)); 737 case HW_CPUSPEED: 738 if (!cpu_cpuspeed) 739 return (EOPNOTSUPP); 740 err = cpu_cpuspeed(&cpuspeed); 741 if (err) 742 return err; 743 return (sysctl_rdint(oldp, oldlenp, newp, cpuspeed)); 744 #ifndef SMALL_KERNEL 745 case HW_SENSORS: 746 return (sysctl_sensors(name + 1, namelen - 1, oldp, oldlenp, 747 newp, newlen)); 748 case HW_SETPERF: 749 return (sysctl_hwsetperf(oldp, oldlenp, newp, newlen)); 750 case HW_PERFPOLICY: 751 return (sysctl_hwperfpolicy(oldp, oldlenp, newp, newlen)); 752 #endif /* !SMALL_KERNEL */ 753 case HW_VENDOR: 754 if (hw_vendor) 755 return (sysctl_rdstring(oldp, oldlenp, newp, 756 hw_vendor)); 757 else 758 return (EOPNOTSUPP); 759 case HW_PRODUCT: 760 if (hw_prod) 761 return (sysctl_rdstring(oldp, oldlenp, newp, hw_prod)); 762 else 763 return (EOPNOTSUPP); 764 case HW_VERSION: 765 if (hw_ver) 766 return (sysctl_rdstring(oldp, oldlenp, newp, hw_ver)); 767 else 768 return (EOPNOTSUPP); 769 case HW_SERIALNO: 770 if (hw_serial) 771 return (sysctl_rdstring(oldp, oldlenp, newp, 772 hw_serial)); 773 else 774 return (EOPNOTSUPP); 775 case HW_UUID: 776 if (hw_uuid) 777 return (sysctl_rdstring(oldp, oldlenp, newp, hw_uuid)); 778 else 779 return (EOPNOTSUPP); 780 case HW_PHYSMEM64: 781 return (sysctl_rdquad(oldp, oldlenp, newp, 782 ptoa((psize_t)physmem))); 783 case HW_USERMEM64: 784 return (sysctl_rdquad(oldp, oldlenp, newp, 785 ptoa((psize_t)physmem - uvmexp.wired))); 786 case HW_ALLOWPOWERDOWN: 787 if (securelevel > 0) 788 return (sysctl_rdint(oldp, oldlenp, newp, 789 allowpowerdown)); 790 return (sysctl_int(oldp, oldlenp, newp, newlen, 791 &allowpowerdown)); 792 #ifdef __HAVE_CPU_TOPOLOGY 793 case HW_SMT: 794 return (sysctl_hwsmt(oldp, oldlenp, newp, newlen)); 795 #endif 796 default: 797 return (EOPNOTSUPP); 798 } 799 /* NOTREACHED */ 800 } 801 802 #ifdef DEBUG 803 /* 804 * Debugging related system variables. 805 */ 806 extern struct ctldebug debug0, debug1; 807 struct ctldebug debug2, debug3, debug4; 808 struct ctldebug debug5, debug6, debug7, debug8, debug9; 809 struct ctldebug debug10, debug11, debug12, debug13, debug14; 810 struct ctldebug debug15, debug16, debug17, debug18, debug19; 811 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = { 812 &debug0, &debug1, &debug2, &debug3, &debug4, 813 &debug5, &debug6, &debug7, &debug8, &debug9, 814 &debug10, &debug11, &debug12, &debug13, &debug14, 815 &debug15, &debug16, &debug17, &debug18, &debug19, 816 }; 817 int 818 debug_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, 819 size_t newlen, struct proc *p) 820 { 821 struct ctldebug *cdp; 822 823 /* all sysctl names at this level are name and field */ 824 if (namelen != 2) 825 return (ENOTDIR); /* overloaded */ 826 if (name[0] < 0 || name[0] >= nitems(debugvars)) 827 return (EOPNOTSUPP); 828 cdp = debugvars[name[0]]; 829 if (cdp->debugname == 0) 830 return (EOPNOTSUPP); 831 switch (name[1]) { 832 case CTL_DEBUG_NAME: 833 return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname)); 834 case CTL_DEBUG_VALUE: 835 return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar)); 836 default: 837 return (EOPNOTSUPP); 838 } 839 /* NOTREACHED */ 840 } 841 #endif /* DEBUG */ 842 843 /* 844 * Reads, or writes that lower the value 845 */ 846 int 847 sysctl_int_lower(void *oldp, size_t *oldlenp, void *newp, size_t newlen, int *valp) 848 { 849 unsigned int oval = *valp, val = *valp; 850 int error; 851 852 if (newp == NULL) 853 return (sysctl_rdint(oldp, oldlenp, newp, *valp)); 854 855 if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &val))) 856 return (error); 857 if (val > oval) 858 return (EPERM); /* do not allow raising */ 859 *(unsigned int *)valp = val; 860 return (0); 861 } 862 863 /* 864 * Validate parameters and get old / set new parameters 865 * for an integer-valued sysctl function. 866 */ 867 int 868 sysctl_int(void *oldp, size_t *oldlenp, void *newp, size_t newlen, int *valp) 869 { 870 int error = 0; 871 int val; 872 873 if (oldp && *oldlenp < sizeof(int)) 874 return (ENOMEM); 875 if (newp && newlen != sizeof(int)) 876 return (EINVAL); 877 *oldlenp = sizeof(int); 878 val = *valp; 879 if (oldp) 880 error = copyout(&val, oldp, sizeof(int)); 881 if (error == 0 && newp) 882 error = copyin(newp, &val, sizeof(int)); 883 if (error == 0) 884 *valp = val; 885 return (error); 886 } 887 888 /* 889 * As above, but read-only. 890 */ 891 int 892 sysctl_rdint(void *oldp, size_t *oldlenp, void *newp, int val) 893 { 894 int error = 0; 895 896 if (oldp && *oldlenp < sizeof(int)) 897 return (ENOMEM); 898 if (newp) 899 return (EPERM); 900 *oldlenp = sizeof(int); 901 if (oldp) 902 error = copyout((caddr_t)&val, oldp, sizeof(int)); 903 return (error); 904 } 905 906 /* 907 * Array of integer values. 908 */ 909 int 910 sysctl_int_arr(int **valpp, int *name, u_int namelen, void *oldp, 911 size_t *oldlenp, void *newp, size_t newlen) 912 { 913 if (namelen > 1) 914 return (ENOTDIR); 915 if (name[0] < 0 || valpp[name[0]] == NULL) 916 return (EOPNOTSUPP); 917 return (sysctl_int(oldp, oldlenp, newp, newlen, valpp[name[0]])); 918 } 919 920 /* 921 * Validate parameters and get old / set new parameters 922 * for an integer-valued sysctl function. 923 */ 924 int 925 sysctl_quad(void *oldp, size_t *oldlenp, void *newp, size_t newlen, 926 int64_t *valp) 927 { 928 int error = 0; 929 930 if (oldp && *oldlenp < sizeof(int64_t)) 931 return (ENOMEM); 932 if (newp && newlen != sizeof(int64_t)) 933 return (EINVAL); 934 *oldlenp = sizeof(int64_t); 935 if (oldp) 936 error = copyout(valp, oldp, sizeof(int64_t)); 937 if (error == 0 && newp) 938 error = copyin(newp, valp, sizeof(int64_t)); 939 return (error); 940 } 941 942 /* 943 * As above, but read-only. 944 */ 945 int 946 sysctl_rdquad(void *oldp, size_t *oldlenp, void *newp, int64_t val) 947 { 948 int error = 0; 949 950 if (oldp && *oldlenp < sizeof(int64_t)) 951 return (ENOMEM); 952 if (newp) 953 return (EPERM); 954 *oldlenp = sizeof(int64_t); 955 if (oldp) 956 error = copyout((caddr_t)&val, oldp, sizeof(int64_t)); 957 return (error); 958 } 959 960 /* 961 * Validate parameters and get old / set new parameters 962 * for a string-valued sysctl function. 963 */ 964 int 965 sysctl_string(void *oldp, size_t *oldlenp, void *newp, size_t newlen, char *str, 966 size_t maxlen) 967 { 968 return sysctl__string(oldp, oldlenp, newp, newlen, str, maxlen, 0); 969 } 970 971 int 972 sysctl_tstring(void *oldp, size_t *oldlenp, void *newp, size_t newlen, 973 char *str, size_t maxlen) 974 { 975 return sysctl__string(oldp, oldlenp, newp, newlen, str, maxlen, 1); 976 } 977 978 int 979 sysctl__string(void *oldp, size_t *oldlenp, void *newp, size_t newlen, 980 char *str, size_t maxlen, int trunc) 981 { 982 size_t len; 983 int error = 0; 984 985 len = strlen(str) + 1; 986 if (oldp && *oldlenp < len) { 987 if (trunc == 0 || *oldlenp == 0) 988 return (ENOMEM); 989 } 990 if (newp && newlen >= maxlen) 991 return (EINVAL); 992 if (oldp) { 993 if (trunc && *oldlenp < len) { 994 len = *oldlenp; 995 error = copyout(str, oldp, len - 1); 996 if (error == 0) 997 error = copyout("", (char *)oldp + len - 1, 1); 998 } else { 999 error = copyout(str, oldp, len); 1000 } 1001 } 1002 *oldlenp = len; 1003 if (error == 0 && newp) { 1004 error = copyin(newp, str, newlen); 1005 str[newlen] = 0; 1006 } 1007 return (error); 1008 } 1009 1010 /* 1011 * As above, but read-only. 1012 */ 1013 int 1014 sysctl_rdstring(void *oldp, size_t *oldlenp, void *newp, const char *str) 1015 { 1016 size_t len; 1017 int error = 0; 1018 1019 len = strlen(str) + 1; 1020 if (oldp && *oldlenp < len) 1021 return (ENOMEM); 1022 if (newp) 1023 return (EPERM); 1024 *oldlenp = len; 1025 if (oldp) 1026 error = copyout(str, oldp, len); 1027 return (error); 1028 } 1029 1030 /* 1031 * Validate parameters and get old / set new parameters 1032 * for a structure oriented sysctl function. 1033 */ 1034 int 1035 sysctl_struct(void *oldp, size_t *oldlenp, void *newp, size_t newlen, void *sp, 1036 size_t len) 1037 { 1038 int error = 0; 1039 1040 if (oldp && *oldlenp < len) 1041 return (ENOMEM); 1042 if (newp && newlen > len) 1043 return (EINVAL); 1044 if (oldp) { 1045 *oldlenp = len; 1046 error = copyout(sp, oldp, len); 1047 } 1048 if (error == 0 && newp) 1049 error = copyin(newp, sp, len); 1050 return (error); 1051 } 1052 1053 /* 1054 * Validate parameters and get old parameters 1055 * for a structure oriented sysctl function. 1056 */ 1057 int 1058 sysctl_rdstruct(void *oldp, size_t *oldlenp, void *newp, const void *sp, 1059 size_t len) 1060 { 1061 int error = 0; 1062 1063 if (oldp && *oldlenp < len) 1064 return (ENOMEM); 1065 if (newp) 1066 return (EPERM); 1067 *oldlenp = len; 1068 if (oldp) 1069 error = copyout(sp, oldp, len); 1070 return (error); 1071 } 1072 1073 #ifndef SMALL_KERNEL 1074 void 1075 fill_file(struct kinfo_file *kf, struct file *fp, struct filedesc *fdp, 1076 int fd, struct vnode *vp, struct process *pr, struct proc *p, 1077 struct socket *so, int show_pointers) 1078 { 1079 struct vattr va; 1080 1081 memset(kf, 0, sizeof(*kf)); 1082 1083 kf->fd_fd = fd; /* might not really be an fd */ 1084 1085 if (fp != NULL) { 1086 if (show_pointers) 1087 kf->f_fileaddr = PTRTOINT64(fp); 1088 kf->f_flag = fp->f_flag; 1089 kf->f_iflags = fp->f_iflags; 1090 kf->f_type = fp->f_type; 1091 kf->f_count = fp->f_count; 1092 if (show_pointers) 1093 kf->f_ucred = PTRTOINT64(fp->f_cred); 1094 kf->f_uid = fp->f_cred->cr_uid; 1095 kf->f_gid = fp->f_cred->cr_gid; 1096 if (show_pointers) 1097 kf->f_ops = PTRTOINT64(fp->f_ops); 1098 if (show_pointers) 1099 kf->f_data = PTRTOINT64(fp->f_data); 1100 kf->f_usecount = 0; 1101 1102 if (suser(p) == 0 || p->p_ucred->cr_uid == fp->f_cred->cr_uid) { 1103 kf->f_offset = fp->f_offset; 1104 mtx_enter(&fp->f_mtx); 1105 kf->f_rxfer = fp->f_rxfer; 1106 kf->f_rwfer = fp->f_wxfer; 1107 kf->f_seek = fp->f_seek; 1108 kf->f_rbytes = fp->f_rbytes; 1109 kf->f_wbytes = fp->f_wbytes; 1110 mtx_leave(&fp->f_mtx); 1111 } else 1112 kf->f_offset = -1; 1113 } else if (vp != NULL) { 1114 /* fake it */ 1115 kf->f_type = DTYPE_VNODE; 1116 kf->f_flag = FREAD; 1117 if (fd == KERN_FILE_TRACE) 1118 kf->f_flag |= FWRITE; 1119 } else if (so != NULL) { 1120 /* fake it */ 1121 kf->f_type = DTYPE_SOCKET; 1122 } 1123 1124 /* information about the object associated with this file */ 1125 switch (kf->f_type) { 1126 case DTYPE_VNODE: 1127 if (fp != NULL) 1128 vp = (struct vnode *)fp->f_data; 1129 1130 if (show_pointers) 1131 kf->v_un = PTRTOINT64(vp->v_un.vu_socket); 1132 kf->v_type = vp->v_type; 1133 kf->v_tag = vp->v_tag; 1134 kf->v_flag = vp->v_flag; 1135 if (show_pointers) 1136 kf->v_data = PTRTOINT64(vp->v_data); 1137 if (show_pointers) 1138 kf->v_mount = PTRTOINT64(vp->v_mount); 1139 if (vp->v_mount) 1140 strlcpy(kf->f_mntonname, 1141 vp->v_mount->mnt_stat.f_mntonname, 1142 sizeof(kf->f_mntonname)); 1143 1144 if (VOP_GETATTR(vp, &va, p->p_ucred, p) == 0) { 1145 kf->va_fileid = va.va_fileid; 1146 kf->va_mode = MAKEIMODE(va.va_type, va.va_mode); 1147 kf->va_size = va.va_size; 1148 kf->va_rdev = va.va_rdev; 1149 kf->va_fsid = va.va_fsid & 0xffffffff; 1150 kf->va_nlink = va.va_nlink; 1151 } 1152 break; 1153 1154 case DTYPE_SOCKET: { 1155 if (so == NULL) 1156 so = (struct socket *)fp->f_data; 1157 1158 kf->so_type = so->so_type; 1159 kf->so_state = so->so_state; 1160 if (show_pointers) 1161 kf->so_pcb = PTRTOINT64(so->so_pcb); 1162 else 1163 kf->so_pcb = -1; 1164 kf->so_protocol = so->so_proto->pr_protocol; 1165 kf->so_family = so->so_proto->pr_domain->dom_family; 1166 kf->so_rcv_cc = so->so_rcv.sb_cc; 1167 kf->so_snd_cc = so->so_snd.sb_cc; 1168 if (isspliced(so)) { 1169 if (show_pointers) 1170 kf->so_splice = 1171 PTRTOINT64(so->so_sp->ssp_socket); 1172 kf->so_splicelen = so->so_sp->ssp_len; 1173 } else if (issplicedback(so)) 1174 kf->so_splicelen = -1; 1175 if (!so->so_pcb) 1176 break; 1177 switch (kf->so_family) { 1178 case AF_INET: { 1179 struct inpcb *inpcb = so->so_pcb; 1180 1181 if (show_pointers) 1182 kf->inp_ppcb = PTRTOINT64(inpcb->inp_ppcb); 1183 kf->inp_lport = inpcb->inp_lport; 1184 kf->inp_laddru[0] = inpcb->inp_laddr.s_addr; 1185 kf->inp_fport = inpcb->inp_fport; 1186 kf->inp_faddru[0] = inpcb->inp_faddr.s_addr; 1187 kf->inp_rtableid = inpcb->inp_rtableid; 1188 if (so->so_type == SOCK_RAW) 1189 kf->inp_proto = inpcb->inp_ip.ip_p; 1190 if (so->so_proto->pr_protocol == IPPROTO_TCP) { 1191 struct tcpcb *tcpcb = (void *)inpcb->inp_ppcb; 1192 kf->t_rcv_wnd = tcpcb->rcv_wnd; 1193 kf->t_snd_wnd = tcpcb->snd_wnd; 1194 kf->t_snd_cwnd = tcpcb->snd_cwnd; 1195 kf->t_state = tcpcb->t_state; 1196 } 1197 break; 1198 } 1199 case AF_INET6: { 1200 struct inpcb *inpcb = so->so_pcb; 1201 1202 if (show_pointers) 1203 kf->inp_ppcb = PTRTOINT64(inpcb->inp_ppcb); 1204 kf->inp_lport = inpcb->inp_lport; 1205 kf->inp_laddru[0] = inpcb->inp_laddr6.s6_addr32[0]; 1206 kf->inp_laddru[1] = inpcb->inp_laddr6.s6_addr32[1]; 1207 kf->inp_laddru[2] = inpcb->inp_laddr6.s6_addr32[2]; 1208 kf->inp_laddru[3] = inpcb->inp_laddr6.s6_addr32[3]; 1209 kf->inp_fport = inpcb->inp_fport; 1210 kf->inp_faddru[0] = inpcb->inp_faddr6.s6_addr32[0]; 1211 kf->inp_faddru[1] = inpcb->inp_faddr6.s6_addr32[1]; 1212 kf->inp_faddru[2] = inpcb->inp_faddr6.s6_addr32[2]; 1213 kf->inp_faddru[3] = inpcb->inp_faddr6.s6_addr32[3]; 1214 kf->inp_rtableid = inpcb->inp_rtableid; 1215 if (so->so_type == SOCK_RAW) 1216 kf->inp_proto = inpcb->inp_ipv6.ip6_nxt; 1217 if (so->so_proto->pr_protocol == IPPROTO_TCP) { 1218 struct tcpcb *tcpcb = (void *)inpcb->inp_ppcb; 1219 kf->t_rcv_wnd = tcpcb->rcv_wnd; 1220 kf->t_snd_wnd = tcpcb->snd_wnd; 1221 kf->t_state = tcpcb->t_state; 1222 } 1223 break; 1224 } 1225 case AF_UNIX: { 1226 struct unpcb *unpcb = so->so_pcb; 1227 1228 kf->f_msgcount = unpcb->unp_msgcount; 1229 if (show_pointers) { 1230 kf->unp_conn = PTRTOINT64(unpcb->unp_conn); 1231 kf->unp_refs = PTRTOINT64( 1232 SLIST_FIRST(&unpcb->unp_refs)); 1233 kf->unp_nextref = PTRTOINT64( 1234 SLIST_NEXT(unpcb, unp_nextref)); 1235 kf->v_un = PTRTOINT64(unpcb->unp_vnode); 1236 kf->unp_addr = PTRTOINT64(unpcb->unp_addr); 1237 } 1238 if (unpcb->unp_addr != NULL) { 1239 struct sockaddr_un *un = mtod(unpcb->unp_addr, 1240 struct sockaddr_un *); 1241 memcpy(kf->unp_path, un->sun_path, un->sun_len 1242 - offsetof(struct sockaddr_un,sun_path)); 1243 } 1244 break; 1245 } 1246 } 1247 break; 1248 } 1249 1250 case DTYPE_PIPE: { 1251 struct pipe *pipe = (struct pipe *)fp->f_data; 1252 1253 if (show_pointers) 1254 kf->pipe_peer = PTRTOINT64(pipe->pipe_peer); 1255 kf->pipe_state = pipe->pipe_state; 1256 break; 1257 } 1258 1259 case DTYPE_KQUEUE: { 1260 struct kqueue *kqi = (struct kqueue *)fp->f_data; 1261 1262 kf->kq_count = kqi->kq_count; 1263 kf->kq_state = kqi->kq_state; 1264 break; 1265 } 1266 } 1267 1268 /* per-process information for KERN_FILE_BY[PU]ID */ 1269 if (pr != NULL) { 1270 kf->p_pid = pr->ps_pid; 1271 kf->p_uid = pr->ps_ucred->cr_uid; 1272 kf->p_gid = pr->ps_ucred->cr_gid; 1273 kf->p_tid = -1; 1274 strlcpy(kf->p_comm, pr->ps_comm, sizeof(kf->p_comm)); 1275 } 1276 if (fdp != NULL) { 1277 fdplock(fdp); 1278 kf->fd_ofileflags = fdp->fd_ofileflags[fd]; 1279 fdpunlock(fdp); 1280 } 1281 } 1282 1283 /* 1284 * Get file structures. 1285 */ 1286 int 1287 sysctl_file(int *name, u_int namelen, char *where, size_t *sizep, 1288 struct proc *p) 1289 { 1290 struct kinfo_file *kf; 1291 struct filedesc *fdp; 1292 struct file *fp; 1293 struct process *pr; 1294 size_t buflen, elem_size, elem_count, outsize; 1295 char *dp = where; 1296 int arg, i, error = 0, needed = 0, matched; 1297 u_int op; 1298 int show_pointers; 1299 1300 if (namelen > 4) 1301 return (ENOTDIR); 1302 if (namelen < 4 || name[2] > sizeof(*kf)) 1303 return (EINVAL); 1304 1305 buflen = where != NULL ? *sizep : 0; 1306 op = name[0]; 1307 arg = name[1]; 1308 elem_size = name[2]; 1309 elem_count = name[3]; 1310 outsize = MIN(sizeof(*kf), elem_size); 1311 1312 if (elem_size < 1) 1313 return (EINVAL); 1314 1315 show_pointers = suser(curproc) == 0; 1316 1317 kf = malloc(sizeof(*kf), M_TEMP, M_WAITOK); 1318 1319 #define FILLIT2(fp, fdp, i, vp, pr, so) do { \ 1320 if (buflen >= elem_size && elem_count > 0) { \ 1321 fill_file(kf, fp, fdp, i, vp, pr, p, so, show_pointers);\ 1322 error = copyout(kf, dp, outsize); \ 1323 if (error) \ 1324 break; \ 1325 dp += elem_size; \ 1326 buflen -= elem_size; \ 1327 elem_count--; \ 1328 } \ 1329 needed += elem_size; \ 1330 } while (0) 1331 #define FILLIT(fp, fdp, i, vp, pr) \ 1332 FILLIT2(fp, fdp, i, vp, pr, NULL) 1333 #define FILLSO(so) \ 1334 FILLIT2(NULL, NULL, 0, NULL, NULL, so) 1335 1336 switch (op) { 1337 case KERN_FILE_BYFILE: 1338 /* use the inp-tables to pick up closed connections, too */ 1339 if (arg == DTYPE_SOCKET) { 1340 struct inpcb *inp; 1341 1342 NET_LOCK(); 1343 TAILQ_FOREACH(inp, &tcbtable.inpt_queue, inp_queue) 1344 FILLSO(inp->inp_socket); 1345 TAILQ_FOREACH(inp, &udbtable.inpt_queue, inp_queue) 1346 FILLSO(inp->inp_socket); 1347 TAILQ_FOREACH(inp, &rawcbtable.inpt_queue, inp_queue) 1348 FILLSO(inp->inp_socket); 1349 #ifdef INET6 1350 TAILQ_FOREACH(inp, &rawin6pcbtable.inpt_queue, 1351 inp_queue) 1352 FILLSO(inp->inp_socket); 1353 #endif 1354 NET_UNLOCK(); 1355 } 1356 fp = NULL; 1357 while ((fp = fd_iterfile(fp, p)) != NULL) { 1358 if ((arg == 0 || fp->f_type == arg)) { 1359 int af, skip = 0; 1360 if (arg == DTYPE_SOCKET && fp->f_type == arg) { 1361 af = ((struct socket *)fp->f_data)-> 1362 so_proto->pr_domain->dom_family; 1363 if (af == AF_INET || af == AF_INET6) 1364 skip = 1; 1365 } 1366 if (!skip) 1367 FILLIT(fp, NULL, 0, NULL, NULL); 1368 } 1369 } 1370 break; 1371 case KERN_FILE_BYPID: 1372 /* A arg of -1 indicates all processes */ 1373 if (arg < -1) { 1374 error = EINVAL; 1375 break; 1376 } 1377 matched = 0; 1378 LIST_FOREACH(pr, &allprocess, ps_list) { 1379 /* 1380 * skip system, exiting, embryonic and undead 1381 * processes 1382 */ 1383 if (pr->ps_flags & (PS_SYSTEM | PS_EMBRYO | PS_EXITING)) 1384 continue; 1385 if (arg > 0 && pr->ps_pid != (pid_t)arg) { 1386 /* not the pid we are looking for */ 1387 continue; 1388 } 1389 matched = 1; 1390 fdp = pr->ps_fd; 1391 if (pr->ps_textvp) 1392 FILLIT(NULL, NULL, KERN_FILE_TEXT, pr->ps_textvp, pr); 1393 if (fdp->fd_cdir) 1394 FILLIT(NULL, NULL, KERN_FILE_CDIR, fdp->fd_cdir, pr); 1395 if (fdp->fd_rdir) 1396 FILLIT(NULL, NULL, KERN_FILE_RDIR, fdp->fd_rdir, pr); 1397 if (pr->ps_tracevp) 1398 FILLIT(NULL, NULL, KERN_FILE_TRACE, pr->ps_tracevp, pr); 1399 for (i = 0; i < fdp->fd_nfiles; i++) { 1400 if ((fp = fd_getfile(fdp, i)) == NULL) 1401 continue; 1402 FILLIT(fp, fdp, i, NULL, pr); 1403 FRELE(fp, p); 1404 } 1405 } 1406 if (!matched) 1407 error = ESRCH; 1408 break; 1409 case KERN_FILE_BYUID: 1410 LIST_FOREACH(pr, &allprocess, ps_list) { 1411 /* 1412 * skip system, exiting, embryonic and undead 1413 * processes 1414 */ 1415 if (pr->ps_flags & (PS_SYSTEM | PS_EMBRYO | PS_EXITING)) 1416 continue; 1417 if (arg >= 0 && pr->ps_ucred->cr_uid != (uid_t)arg) { 1418 /* not the uid we are looking for */ 1419 continue; 1420 } 1421 fdp = pr->ps_fd; 1422 if (fdp->fd_cdir) 1423 FILLIT(NULL, NULL, KERN_FILE_CDIR, fdp->fd_cdir, pr); 1424 if (fdp->fd_rdir) 1425 FILLIT(NULL, NULL, KERN_FILE_RDIR, fdp->fd_rdir, pr); 1426 if (pr->ps_tracevp) 1427 FILLIT(NULL, NULL, KERN_FILE_TRACE, pr->ps_tracevp, pr); 1428 for (i = 0; i < fdp->fd_nfiles; i++) { 1429 if ((fp = fd_getfile(fdp, i)) == NULL) 1430 continue; 1431 FILLIT(fp, fdp, i, NULL, pr); 1432 FRELE(fp, p); 1433 } 1434 } 1435 break; 1436 default: 1437 error = EINVAL; 1438 break; 1439 } 1440 free(kf, M_TEMP, sizeof(*kf)); 1441 1442 if (!error) { 1443 if (where == NULL) 1444 needed += KERN_FILESLOP * elem_size; 1445 else if (*sizep < needed) 1446 error = ENOMEM; 1447 *sizep = needed; 1448 } 1449 1450 return (error); 1451 } 1452 1453 /* 1454 * try over estimating by 5 procs 1455 */ 1456 #define KERN_PROCSLOP 5 1457 1458 int 1459 sysctl_doproc(int *name, u_int namelen, char *where, size_t *sizep) 1460 { 1461 struct kinfo_proc *kproc = NULL; 1462 struct proc *p; 1463 struct process *pr; 1464 char *dp; 1465 int arg, buflen, doingzomb, elem_size, elem_count; 1466 int error, needed, op; 1467 int dothreads = 0; 1468 int show_pointers; 1469 1470 dp = where; 1471 buflen = where != NULL ? *sizep : 0; 1472 needed = error = 0; 1473 1474 if (namelen != 4 || name[2] < 0 || name[3] < 0 || 1475 name[2] > sizeof(*kproc)) 1476 return (EINVAL); 1477 op = name[0]; 1478 arg = name[1]; 1479 elem_size = name[2]; 1480 elem_count = name[3]; 1481 1482 dothreads = op & KERN_PROC_SHOW_THREADS; 1483 op &= ~KERN_PROC_SHOW_THREADS; 1484 1485 show_pointers = suser(curproc) == 0; 1486 1487 if (where != NULL) 1488 kproc = malloc(sizeof(*kproc), M_TEMP, M_WAITOK); 1489 1490 pr = LIST_FIRST(&allprocess); 1491 doingzomb = 0; 1492 again: 1493 for (; pr != NULL; pr = LIST_NEXT(pr, ps_list)) { 1494 /* XXX skip processes in the middle of being zapped */ 1495 if (pr->ps_pgrp == NULL) 1496 continue; 1497 1498 /* 1499 * Skip embryonic processes. 1500 */ 1501 if (pr->ps_flags & PS_EMBRYO) 1502 continue; 1503 1504 /* 1505 * TODO - make more efficient (see notes below). 1506 */ 1507 switch (op) { 1508 1509 case KERN_PROC_PID: 1510 /* could do this with just a lookup */ 1511 if (pr->ps_pid != (pid_t)arg) 1512 continue; 1513 break; 1514 1515 case KERN_PROC_PGRP: 1516 /* could do this by traversing pgrp */ 1517 if (pr->ps_pgrp->pg_id != (pid_t)arg) 1518 continue; 1519 break; 1520 1521 case KERN_PROC_SESSION: 1522 if (pr->ps_session->s_leader == NULL || 1523 pr->ps_session->s_leader->ps_pid != (pid_t)arg) 1524 continue; 1525 break; 1526 1527 case KERN_PROC_TTY: 1528 if ((pr->ps_flags & PS_CONTROLT) == 0 || 1529 pr->ps_session->s_ttyp == NULL || 1530 pr->ps_session->s_ttyp->t_dev != (dev_t)arg) 1531 continue; 1532 break; 1533 1534 case KERN_PROC_UID: 1535 if (pr->ps_ucred->cr_uid != (uid_t)arg) 1536 continue; 1537 break; 1538 1539 case KERN_PROC_RUID: 1540 if (pr->ps_ucred->cr_ruid != (uid_t)arg) 1541 continue; 1542 break; 1543 1544 case KERN_PROC_ALL: 1545 if (pr->ps_flags & PS_SYSTEM) 1546 continue; 1547 break; 1548 1549 case KERN_PROC_KTHREAD: 1550 /* no filtering */ 1551 break; 1552 1553 default: 1554 error = EINVAL; 1555 goto err; 1556 } 1557 1558 if (buflen >= elem_size && elem_count > 0) { 1559 fill_kproc(pr, kproc, NULL, show_pointers); 1560 error = copyout(kproc, dp, elem_size); 1561 if (error) 1562 goto err; 1563 dp += elem_size; 1564 buflen -= elem_size; 1565 elem_count--; 1566 } 1567 needed += elem_size; 1568 1569 /* Skip per-thread entries if not required by op */ 1570 if (!dothreads) 1571 continue; 1572 1573 TAILQ_FOREACH(p, &pr->ps_threads, p_thr_link) { 1574 if (buflen >= elem_size && elem_count > 0) { 1575 fill_kproc(pr, kproc, p, show_pointers); 1576 error = copyout(kproc, dp, elem_size); 1577 if (error) 1578 goto err; 1579 dp += elem_size; 1580 buflen -= elem_size; 1581 elem_count--; 1582 } 1583 needed += elem_size; 1584 } 1585 } 1586 if (doingzomb == 0) { 1587 pr = LIST_FIRST(&zombprocess); 1588 doingzomb++; 1589 goto again; 1590 } 1591 if (where != NULL) { 1592 *sizep = dp - where; 1593 if (needed > *sizep) { 1594 error = ENOMEM; 1595 goto err; 1596 } 1597 } else { 1598 needed += KERN_PROCSLOP * elem_size; 1599 *sizep = needed; 1600 } 1601 err: 1602 if (kproc) 1603 free(kproc, M_TEMP, sizeof(*kproc)); 1604 return (error); 1605 } 1606 1607 /* 1608 * Fill in a kproc structure for the specified process. 1609 */ 1610 void 1611 fill_kproc(struct process *pr, struct kinfo_proc *ki, struct proc *p, 1612 int show_pointers) 1613 { 1614 struct session *s = pr->ps_session; 1615 struct tty *tp; 1616 struct vmspace *vm = pr->ps_vmspace; 1617 struct timespec ut, st; 1618 int isthread; 1619 1620 isthread = p != NULL; 1621 if (!isthread) 1622 p = pr->ps_mainproc; /* XXX */ 1623 1624 FILL_KPROC(ki, strlcpy, p, pr, pr->ps_ucred, pr->ps_pgrp, 1625 p, pr, s, vm, pr->ps_limit, pr->ps_sigacts, isthread, 1626 show_pointers); 1627 1628 /* stuff that's too painful to generalize into the macros */ 1629 if (pr->ps_pptr) 1630 ki->p_ppid = pr->ps_pptr->ps_pid; 1631 if (s->s_leader) 1632 ki->p_sid = s->s_leader->ps_pid; 1633 1634 if ((pr->ps_flags & PS_CONTROLT) && (tp = s->s_ttyp)) { 1635 ki->p_tdev = tp->t_dev; 1636 ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : -1; 1637 if (show_pointers) 1638 ki->p_tsess = PTRTOINT64(tp->t_session); 1639 } else { 1640 ki->p_tdev = NODEV; 1641 ki->p_tpgid = -1; 1642 } 1643 1644 /* fixups that can only be done in the kernel */ 1645 if ((pr->ps_flags & PS_ZOMBIE) == 0) { 1646 if ((pr->ps_flags & PS_EMBRYO) == 0 && vm != NULL) 1647 ki->p_vm_rssize = vm_resident_count(vm); 1648 calctsru(isthread ? &p->p_tu : &pr->ps_tu, &ut, &st, NULL); 1649 ki->p_uutime_sec = ut.tv_sec; 1650 ki->p_uutime_usec = ut.tv_nsec/1000; 1651 ki->p_ustime_sec = st.tv_sec; 1652 ki->p_ustime_usec = st.tv_nsec/1000; 1653 1654 #ifdef MULTIPROCESSOR 1655 if (p->p_cpu != NULL) 1656 ki->p_cpuid = CPU_INFO_UNIT(p->p_cpu); 1657 #endif 1658 } 1659 1660 /* get %cpu and schedule state: just one thread or sum of all? */ 1661 if (isthread) { 1662 ki->p_pctcpu = p->p_pctcpu; 1663 ki->p_stat = p->p_stat; 1664 } else { 1665 ki->p_pctcpu = 0; 1666 ki->p_stat = (pr->ps_flags & PS_ZOMBIE) ? SDEAD : SIDL; 1667 TAILQ_FOREACH(p, &pr->ps_threads, p_thr_link) { 1668 ki->p_pctcpu += p->p_pctcpu; 1669 /* find best state: ONPROC > RUN > STOP > SLEEP > .. */ 1670 if (p->p_stat == SONPROC || ki->p_stat == SONPROC) 1671 ki->p_stat = SONPROC; 1672 else if (p->p_stat == SRUN || ki->p_stat == SRUN) 1673 ki->p_stat = SRUN; 1674 else if (p->p_stat == SSTOP || ki->p_stat == SSTOP) 1675 ki->p_stat = SSTOP; 1676 else if (p->p_stat == SSLEEP) 1677 ki->p_stat = SSLEEP; 1678 } 1679 } 1680 } 1681 1682 int 1683 sysctl_proc_args(int *name, u_int namelen, void *oldp, size_t *oldlenp, 1684 struct proc *cp) 1685 { 1686 struct process *vpr; 1687 pid_t pid; 1688 struct ps_strings pss; 1689 struct iovec iov; 1690 struct uio uio; 1691 int error, cnt, op; 1692 size_t limit; 1693 char **rargv, **vargv; /* reader vs. victim */ 1694 char *rarg, *varg, *buf; 1695 struct vmspace *vm; 1696 vaddr_t ps_strings; 1697 1698 if (namelen > 2) 1699 return (ENOTDIR); 1700 if (namelen < 2) 1701 return (EINVAL); 1702 1703 pid = name[0]; 1704 op = name[1]; 1705 1706 switch (op) { 1707 case KERN_PROC_ARGV: 1708 case KERN_PROC_NARGV: 1709 case KERN_PROC_ENV: 1710 case KERN_PROC_NENV: 1711 break; 1712 default: 1713 return (EOPNOTSUPP); 1714 } 1715 1716 if ((vpr = prfind(pid)) == NULL) 1717 return (ESRCH); 1718 1719 if (oldp == NULL) { 1720 if (op == KERN_PROC_NARGV || op == KERN_PROC_NENV) 1721 *oldlenp = sizeof(int); 1722 else 1723 *oldlenp = ARG_MAX; /* XXX XXX XXX */ 1724 return (0); 1725 } 1726 1727 /* Either system process or exiting/zombie */ 1728 if (vpr->ps_flags & (PS_SYSTEM | PS_EXITING)) 1729 return (EINVAL); 1730 1731 /* Execing - danger. */ 1732 if ((vpr->ps_flags & PS_INEXEC)) 1733 return (EBUSY); 1734 1735 /* Only owner or root can get env */ 1736 if ((op == KERN_PROC_NENV || op == KERN_PROC_ENV) && 1737 (vpr->ps_ucred->cr_uid != cp->p_ucred->cr_uid && 1738 (error = suser(cp)) != 0)) 1739 return (error); 1740 1741 ps_strings = vpr->ps_strings; 1742 vm = vpr->ps_vmspace; 1743 vm->vm_refcnt++; 1744 vpr = NULL; 1745 1746 buf = malloc(PAGE_SIZE, M_TEMP, M_WAITOK); 1747 1748 iov.iov_base = &pss; 1749 iov.iov_len = sizeof(pss); 1750 uio.uio_iov = &iov; 1751 uio.uio_iovcnt = 1; 1752 uio.uio_offset = (off_t)ps_strings; 1753 uio.uio_resid = sizeof(pss); 1754 uio.uio_segflg = UIO_SYSSPACE; 1755 uio.uio_rw = UIO_READ; 1756 uio.uio_procp = cp; 1757 1758 if ((error = uvm_io(&vm->vm_map, &uio, 0)) != 0) 1759 goto out; 1760 1761 if (op == KERN_PROC_NARGV) { 1762 error = sysctl_rdint(oldp, oldlenp, NULL, pss.ps_nargvstr); 1763 goto out; 1764 } 1765 if (op == KERN_PROC_NENV) { 1766 error = sysctl_rdint(oldp, oldlenp, NULL, pss.ps_nenvstr); 1767 goto out; 1768 } 1769 1770 if (op == KERN_PROC_ARGV) { 1771 cnt = pss.ps_nargvstr; 1772 vargv = pss.ps_argvstr; 1773 } else { 1774 cnt = pss.ps_nenvstr; 1775 vargv = pss.ps_envstr; 1776 } 1777 1778 /* -1 to have space for a terminating NUL */ 1779 limit = *oldlenp - 1; 1780 *oldlenp = 0; 1781 1782 rargv = oldp; 1783 1784 /* 1785 * *oldlenp - number of bytes copied out into readers buffer. 1786 * limit - maximal number of bytes allowed into readers buffer. 1787 * rarg - pointer into readers buffer where next arg will be stored. 1788 * rargv - pointer into readers buffer where the next rarg pointer 1789 * will be stored. 1790 * vargv - pointer into victim address space where the next argument 1791 * will be read. 1792 */ 1793 1794 /* space for cnt pointers and a NULL */ 1795 rarg = (char *)(rargv + cnt + 1); 1796 *oldlenp += (cnt + 1) * sizeof(char **); 1797 1798 while (cnt > 0 && *oldlenp < limit) { 1799 size_t len, vstrlen; 1800 1801 /* Write to readers argv */ 1802 if ((error = copyout(&rarg, rargv, sizeof(rarg))) != 0) 1803 goto out; 1804 1805 /* read the victim argv */ 1806 iov.iov_base = &varg; 1807 iov.iov_len = sizeof(varg); 1808 uio.uio_iov = &iov; 1809 uio.uio_iovcnt = 1; 1810 uio.uio_offset = (off_t)(vaddr_t)vargv; 1811 uio.uio_resid = sizeof(varg); 1812 uio.uio_segflg = UIO_SYSSPACE; 1813 uio.uio_rw = UIO_READ; 1814 uio.uio_procp = cp; 1815 if ((error = uvm_io(&vm->vm_map, &uio, 0)) != 0) 1816 goto out; 1817 1818 if (varg == NULL) 1819 break; 1820 1821 /* 1822 * read the victim arg. We must jump through hoops to avoid 1823 * crossing a page boundary too much and returning an error. 1824 */ 1825 more: 1826 len = PAGE_SIZE - (((vaddr_t)varg) & PAGE_MASK); 1827 /* leave space for the terminating NUL */ 1828 iov.iov_base = buf; 1829 iov.iov_len = len; 1830 uio.uio_iov = &iov; 1831 uio.uio_iovcnt = 1; 1832 uio.uio_offset = (off_t)(vaddr_t)varg; 1833 uio.uio_resid = len; 1834 uio.uio_segflg = UIO_SYSSPACE; 1835 uio.uio_rw = UIO_READ; 1836 uio.uio_procp = cp; 1837 if ((error = uvm_io(&vm->vm_map, &uio, 0)) != 0) 1838 goto out; 1839 1840 for (vstrlen = 0; vstrlen < len; vstrlen++) { 1841 if (buf[vstrlen] == '\0') 1842 break; 1843 } 1844 1845 /* Don't overflow readers buffer. */ 1846 if (*oldlenp + vstrlen + 1 >= limit) { 1847 error = ENOMEM; 1848 goto out; 1849 } 1850 1851 if ((error = copyout(buf, rarg, vstrlen)) != 0) 1852 goto out; 1853 1854 *oldlenp += vstrlen; 1855 rarg += vstrlen; 1856 1857 /* The string didn't end in this page? */ 1858 if (vstrlen == len) { 1859 varg += vstrlen; 1860 goto more; 1861 } 1862 1863 /* End of string. Terminate it with a NUL */ 1864 buf[0] = '\0'; 1865 if ((error = copyout(buf, rarg, 1)) != 0) 1866 goto out; 1867 *oldlenp += 1; 1868 rarg += 1; 1869 1870 vargv++; 1871 rargv++; 1872 cnt--; 1873 } 1874 1875 if (*oldlenp >= limit) { 1876 error = ENOMEM; 1877 goto out; 1878 } 1879 1880 /* Write the terminating null */ 1881 rarg = NULL; 1882 error = copyout(&rarg, rargv, sizeof(rarg)); 1883 1884 out: 1885 uvmspace_free(vm); 1886 free(buf, M_TEMP, PAGE_SIZE); 1887 return (error); 1888 } 1889 1890 int 1891 sysctl_proc_cwd(int *name, u_int namelen, void *oldp, size_t *oldlenp, 1892 struct proc *cp) 1893 { 1894 struct process *findpr; 1895 struct vnode *vp; 1896 pid_t pid; 1897 int error; 1898 size_t lenused, len; 1899 char *path, *bp, *bend; 1900 1901 if (namelen > 1) 1902 return (ENOTDIR); 1903 if (namelen < 1) 1904 return (EINVAL); 1905 1906 pid = name[0]; 1907 if ((findpr = prfind(pid)) == NULL) 1908 return (ESRCH); 1909 1910 if (oldp == NULL) { 1911 *oldlenp = MAXPATHLEN * 4; 1912 return (0); 1913 } 1914 1915 /* Either system process or exiting/zombie */ 1916 if (findpr->ps_flags & (PS_SYSTEM | PS_EXITING)) 1917 return (EINVAL); 1918 1919 /* Only owner or root can get cwd */ 1920 if (findpr->ps_ucred->cr_uid != cp->p_ucred->cr_uid && 1921 (error = suser(cp)) != 0) 1922 return (error); 1923 1924 len = *oldlenp; 1925 if (len > MAXPATHLEN * 4) 1926 len = MAXPATHLEN * 4; 1927 else if (len < 2) 1928 return (ERANGE); 1929 *oldlenp = 0; 1930 1931 /* snag a reference to the vnode before we can sleep */ 1932 vp = findpr->ps_fd->fd_cdir; 1933 vref(vp); 1934 1935 path = malloc(len, M_TEMP, M_WAITOK); 1936 1937 bp = &path[len]; 1938 bend = bp; 1939 *(--bp) = '\0'; 1940 1941 /* Same as sys__getcwd */ 1942 error = vfs_getcwd_common(vp, NULL, 1943 &bp, path, len / 2, GETCWD_CHECK_ACCESS, cp); 1944 if (error == 0) { 1945 *oldlenp = lenused = bend - bp; 1946 error = copyout(bp, oldp, lenused); 1947 } 1948 1949 vrele(vp); 1950 free(path, M_TEMP, len); 1951 1952 return (error); 1953 } 1954 1955 int 1956 sysctl_proc_nobroadcastkill(int *name, u_int namelen, void *newp, size_t newlen, 1957 void *oldp, size_t *oldlenp, struct proc *cp) 1958 { 1959 struct process *findpr; 1960 pid_t pid; 1961 int error, flag; 1962 1963 if (namelen > 1) 1964 return (ENOTDIR); 1965 if (namelen < 1) 1966 return (EINVAL); 1967 1968 pid = name[0]; 1969 if ((findpr = prfind(pid)) == NULL) 1970 return (ESRCH); 1971 1972 /* Either system process or exiting/zombie */ 1973 if (findpr->ps_flags & (PS_SYSTEM | PS_EXITING)) 1974 return (EINVAL); 1975 1976 /* Only root can change PS_NOBROADCASTKILL */ 1977 if (newp != 0 && (error = suser(cp)) != 0) 1978 return (error); 1979 1980 /* get the PS_NOBROADCASTKILL flag */ 1981 flag = findpr->ps_flags & PS_NOBROADCASTKILL ? 1 : 0; 1982 1983 error = sysctl_int(oldp, oldlenp, newp, newlen, &flag); 1984 if (error == 0 && newp) { 1985 if (flag) 1986 atomic_setbits_int(&findpr->ps_flags, 1987 PS_NOBROADCASTKILL); 1988 else 1989 atomic_clearbits_int(&findpr->ps_flags, 1990 PS_NOBROADCASTKILL); 1991 } 1992 1993 return (error); 1994 } 1995 1996 /* Arbitrary but reasonable limit for one iteration. */ 1997 #define VMMAP_MAXLEN MAXPHYS 1998 1999 int 2000 sysctl_proc_vmmap(int *name, u_int namelen, void *oldp, size_t *oldlenp, 2001 struct proc *cp) 2002 { 2003 struct process *findpr; 2004 pid_t pid; 2005 int error; 2006 size_t oldlen, len; 2007 struct kinfo_vmentry *kve, *ukve; 2008 u_long *ustart, start; 2009 2010 if (namelen > 1) 2011 return (ENOTDIR); 2012 if (namelen < 1) 2013 return (EINVAL); 2014 2015 /* Provide max buffer length as hint. */ 2016 if (oldp == NULL) { 2017 if (oldlenp == NULL) 2018 return (EINVAL); 2019 else { 2020 *oldlenp = VMMAP_MAXLEN; 2021 return (0); 2022 } 2023 } 2024 2025 pid = name[0]; 2026 if (pid == cp->p_p->ps_pid) { 2027 /* Self process mapping. */ 2028 findpr = cp->p_p; 2029 } else if (pid > 0) { 2030 if ((findpr = prfind(pid)) == NULL) 2031 return (ESRCH); 2032 2033 /* Either system process or exiting/zombie */ 2034 if (findpr->ps_flags & (PS_SYSTEM | PS_EXITING)) 2035 return (EINVAL); 2036 2037 #if 1 2038 /* XXX Allow only root for now */ 2039 if ((error = suser(cp)) != 0) 2040 return (error); 2041 #else 2042 /* Only owner or root can get vmmap */ 2043 if (findpr->ps_ucred->cr_uid != cp->p_ucred->cr_uid && 2044 (error = suser(cp)) != 0) 2045 return (error); 2046 #endif 2047 } else { 2048 /* Only root can get kernel_map */ 2049 if ((error = suser(cp)) != 0) 2050 return (error); 2051 findpr = NULL; 2052 } 2053 2054 /* Check the given size. */ 2055 oldlen = *oldlenp; 2056 if (oldlen == 0 || oldlen % sizeof(*kve) != 0) 2057 return (EINVAL); 2058 2059 /* Deny huge allocation. */ 2060 if (oldlen > VMMAP_MAXLEN) 2061 return (EINVAL); 2062 2063 /* 2064 * Iterate from the given address passed as the first element's 2065 * kve_start via oldp. 2066 */ 2067 ukve = (struct kinfo_vmentry *)oldp; 2068 ustart = &ukve->kve_start; 2069 error = copyin(ustart, &start, sizeof(start)); 2070 if (error != 0) 2071 return (error); 2072 2073 /* Allocate wired memory to not block. */ 2074 kve = malloc(oldlen, M_TEMP, M_WAITOK); 2075 2076 /* Set the base address and read entries. */ 2077 kve[0].kve_start = start; 2078 len = oldlen; 2079 error = fill_vmmap(findpr, kve, &len); 2080 if (error != 0 && error != ENOMEM) 2081 goto done; 2082 if (len == 0) 2083 goto done; 2084 2085 KASSERT(len <= oldlen); 2086 KASSERT((len % sizeof(struct kinfo_vmentry)) == 0); 2087 2088 error = copyout(kve, oldp, len); 2089 2090 done: 2091 *oldlenp = len; 2092 2093 free(kve, M_TEMP, oldlen); 2094 2095 return (error); 2096 } 2097 #endif 2098 2099 /* 2100 * Initialize disknames/diskstats for export by sysctl. If update is set, 2101 * then we simply update the disk statistics information. 2102 */ 2103 int 2104 sysctl_diskinit(int update, struct proc *p) 2105 { 2106 struct diskstats *sdk; 2107 struct disk *dk; 2108 const char *duid; 2109 int i, tlen, l; 2110 2111 if ((i = rw_enter(&sysctl_disklock, RW_WRITE|RW_INTR)) != 0) 2112 return i; 2113 2114 if (disk_change) { 2115 for (dk = TAILQ_FIRST(&disklist), tlen = 0; dk; 2116 dk = TAILQ_NEXT(dk, dk_link)) { 2117 if (dk->dk_name) 2118 tlen += strlen(dk->dk_name); 2119 tlen += 18; /* label uid + separators */ 2120 } 2121 tlen++; 2122 2123 if (disknames) 2124 free(disknames, M_SYSCTL, disknameslen); 2125 if (diskstats) 2126 free(diskstats, M_SYSCTL, diskstatslen); 2127 diskstats = NULL; 2128 disknames = NULL; 2129 diskstats = mallocarray(disk_count, sizeof(struct diskstats), 2130 M_SYSCTL, M_WAITOK|M_ZERO); 2131 diskstatslen = disk_count * sizeof(struct diskstats); 2132 disknames = malloc(tlen, M_SYSCTL, M_WAITOK|M_ZERO); 2133 disknameslen = tlen; 2134 disknames[0] = '\0'; 2135 2136 for (dk = TAILQ_FIRST(&disklist), i = 0, l = 0; dk; 2137 dk = TAILQ_NEXT(dk, dk_link), i++) { 2138 duid = NULL; 2139 if (dk->dk_label && !duid_iszero(dk->dk_label->d_uid)) 2140 duid = duid_format(dk->dk_label->d_uid); 2141 snprintf(disknames + l, tlen - l, "%s:%s,", 2142 dk->dk_name ? dk->dk_name : "", 2143 duid ? duid : ""); 2144 l += strlen(disknames + l); 2145 sdk = diskstats + i; 2146 strlcpy(sdk->ds_name, dk->dk_name, 2147 sizeof(sdk->ds_name)); 2148 mtx_enter(&dk->dk_mtx); 2149 sdk->ds_busy = dk->dk_busy; 2150 sdk->ds_rxfer = dk->dk_rxfer; 2151 sdk->ds_wxfer = dk->dk_wxfer; 2152 sdk->ds_seek = dk->dk_seek; 2153 sdk->ds_rbytes = dk->dk_rbytes; 2154 sdk->ds_wbytes = dk->dk_wbytes; 2155 sdk->ds_attachtime = dk->dk_attachtime; 2156 sdk->ds_timestamp = dk->dk_timestamp; 2157 sdk->ds_time = dk->dk_time; 2158 mtx_leave(&dk->dk_mtx); 2159 } 2160 2161 /* Eliminate trailing comma */ 2162 if (l != 0) 2163 disknames[l - 1] = '\0'; 2164 disk_change = 0; 2165 } else if (update) { 2166 /* Just update, number of drives hasn't changed */ 2167 for (dk = TAILQ_FIRST(&disklist), i = 0; dk; 2168 dk = TAILQ_NEXT(dk, dk_link), i++) { 2169 sdk = diskstats + i; 2170 strlcpy(sdk->ds_name, dk->dk_name, 2171 sizeof(sdk->ds_name)); 2172 mtx_enter(&dk->dk_mtx); 2173 sdk->ds_busy = dk->dk_busy; 2174 sdk->ds_rxfer = dk->dk_rxfer; 2175 sdk->ds_wxfer = dk->dk_wxfer; 2176 sdk->ds_seek = dk->dk_seek; 2177 sdk->ds_rbytes = dk->dk_rbytes; 2178 sdk->ds_wbytes = dk->dk_wbytes; 2179 sdk->ds_attachtime = dk->dk_attachtime; 2180 sdk->ds_timestamp = dk->dk_timestamp; 2181 sdk->ds_time = dk->dk_time; 2182 mtx_leave(&dk->dk_mtx); 2183 } 2184 } 2185 rw_exit_write(&sysctl_disklock); 2186 return 0; 2187 } 2188 2189 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM) 2190 int 2191 sysctl_sysvipc(int *name, u_int namelen, void *where, size_t *sizep) 2192 { 2193 #ifdef SYSVSEM 2194 struct sem_sysctl_info *semsi; 2195 #endif 2196 #ifdef SYSVSHM 2197 struct shm_sysctl_info *shmsi; 2198 #endif 2199 size_t infosize, dssize, tsize, buflen, bufsiz; 2200 int i, nds, error, ret; 2201 void *buf; 2202 2203 if (namelen != 1) 2204 return (EINVAL); 2205 2206 buflen = *sizep; 2207 2208 switch (*name) { 2209 case KERN_SYSVIPC_MSG_INFO: 2210 #ifdef SYSVMSG 2211 return (sysctl_sysvmsg(name, namelen, where, sizep)); 2212 #else 2213 return (EOPNOTSUPP); 2214 #endif 2215 case KERN_SYSVIPC_SEM_INFO: 2216 #ifdef SYSVSEM 2217 infosize = sizeof(semsi->seminfo); 2218 nds = seminfo.semmni; 2219 dssize = sizeof(semsi->semids[0]); 2220 break; 2221 #else 2222 return (EOPNOTSUPP); 2223 #endif 2224 case KERN_SYSVIPC_SHM_INFO: 2225 #ifdef SYSVSHM 2226 infosize = sizeof(shmsi->shminfo); 2227 nds = shminfo.shmmni; 2228 dssize = sizeof(shmsi->shmids[0]); 2229 break; 2230 #else 2231 return (EOPNOTSUPP); 2232 #endif 2233 default: 2234 return (EINVAL); 2235 } 2236 tsize = infosize + (nds * dssize); 2237 2238 /* Return just the total size required. */ 2239 if (where == NULL) { 2240 *sizep = tsize; 2241 return (0); 2242 } 2243 2244 /* Not enough room for even the info struct. */ 2245 if (buflen < infosize) { 2246 *sizep = 0; 2247 return (ENOMEM); 2248 } 2249 bufsiz = min(tsize, buflen); 2250 buf = malloc(bufsiz, M_TEMP, M_WAITOK|M_ZERO); 2251 2252 switch (*name) { 2253 #ifdef SYSVSEM 2254 case KERN_SYSVIPC_SEM_INFO: 2255 semsi = (struct sem_sysctl_info *)buf; 2256 semsi->seminfo = seminfo; 2257 break; 2258 #endif 2259 #ifdef SYSVSHM 2260 case KERN_SYSVIPC_SHM_INFO: 2261 shmsi = (struct shm_sysctl_info *)buf; 2262 shmsi->shminfo = shminfo; 2263 break; 2264 #endif 2265 } 2266 buflen -= infosize; 2267 2268 ret = 0; 2269 if (buflen > 0) { 2270 /* Fill in the IPC data structures. */ 2271 for (i = 0; i < nds; i++) { 2272 if (buflen < dssize) { 2273 ret = ENOMEM; 2274 break; 2275 } 2276 switch (*name) { 2277 #ifdef SYSVSEM 2278 case KERN_SYSVIPC_SEM_INFO: 2279 if (sema[i] != NULL) 2280 memcpy(&semsi->semids[i], sema[i], 2281 dssize); 2282 else 2283 memset(&semsi->semids[i], 0, dssize); 2284 break; 2285 #endif 2286 #ifdef SYSVSHM 2287 case KERN_SYSVIPC_SHM_INFO: 2288 if (shmsegs[i] != NULL) 2289 memcpy(&shmsi->shmids[i], shmsegs[i], 2290 dssize); 2291 else 2292 memset(&shmsi->shmids[i], 0, dssize); 2293 break; 2294 #endif 2295 } 2296 buflen -= dssize; 2297 } 2298 } 2299 *sizep -= buflen; 2300 error = copyout(buf, where, *sizep); 2301 free(buf, M_TEMP, bufsiz); 2302 /* If copyout succeeded, use return code set earlier. */ 2303 return (error ? error : ret); 2304 } 2305 #endif /* SYSVMSG || SYSVSEM || SYSVSHM */ 2306 2307 #ifndef SMALL_KERNEL 2308 2309 int 2310 sysctl_intrcnt(int *name, u_int namelen, void *oldp, size_t *oldlenp) 2311 { 2312 return (evcount_sysctl(name, namelen, oldp, oldlenp, NULL, 0)); 2313 } 2314 2315 2316 int 2317 sysctl_sensors(int *name, u_int namelen, void *oldp, size_t *oldlenp, 2318 void *newp, size_t newlen) 2319 { 2320 struct ksensor *ks; 2321 struct sensor *us; 2322 struct ksensordev *ksd; 2323 struct sensordev *usd; 2324 int dev, numt, ret; 2325 enum sensor_type type; 2326 2327 if (namelen != 1 && namelen != 3) 2328 return (ENOTDIR); 2329 2330 dev = name[0]; 2331 if (namelen == 1) { 2332 ret = sensordev_get(dev, &ksd); 2333 if (ret) 2334 return (ret); 2335 2336 /* Grab a copy, to clear the kernel pointers */ 2337 usd = malloc(sizeof(*usd), M_TEMP, M_WAITOK|M_ZERO); 2338 usd->num = ksd->num; 2339 strlcpy(usd->xname, ksd->xname, sizeof(usd->xname)); 2340 memcpy(usd->maxnumt, ksd->maxnumt, sizeof(usd->maxnumt)); 2341 usd->sensors_count = ksd->sensors_count; 2342 2343 ret = sysctl_rdstruct(oldp, oldlenp, newp, usd, 2344 sizeof(struct sensordev)); 2345 2346 free(usd, M_TEMP, sizeof(*usd)); 2347 return (ret); 2348 } 2349 2350 type = name[1]; 2351 numt = name[2]; 2352 2353 ret = sensor_find(dev, type, numt, &ks); 2354 if (ret) 2355 return (ret); 2356 2357 /* Grab a copy, to clear the kernel pointers */ 2358 us = malloc(sizeof(*us), M_TEMP, M_WAITOK|M_ZERO); 2359 memcpy(us->desc, ks->desc, sizeof(us->desc)); 2360 us->tv = ks->tv; 2361 us->value = ks->value; 2362 us->type = ks->type; 2363 us->status = ks->status; 2364 us->numt = ks->numt; 2365 us->flags = ks->flags; 2366 2367 ret = sysctl_rdstruct(oldp, oldlenp, newp, us, 2368 sizeof(struct sensor)); 2369 free(us, M_TEMP, sizeof(*us)); 2370 return (ret); 2371 } 2372 #endif /* SMALL_KERNEL */ 2373 2374 int 2375 sysctl_cptime2(int *name, u_int namelen, void *oldp, size_t *oldlenp, 2376 void *newp, size_t newlen) 2377 { 2378 CPU_INFO_ITERATOR cii; 2379 struct cpu_info *ci; 2380 int found = 0; 2381 2382 if (namelen != 1) 2383 return (ENOTDIR); 2384 2385 CPU_INFO_FOREACH(cii, ci) { 2386 if (name[0] == CPU_INFO_UNIT(ci)) { 2387 found = 1; 2388 break; 2389 } 2390 } 2391 if (!found) 2392 return (ENOENT); 2393 2394 return (sysctl_rdstruct(oldp, oldlenp, newp, 2395 &ci->ci_schedstate.spc_cp_time, 2396 sizeof(ci->ci_schedstate.spc_cp_time))); 2397 } 2398 2399 #if NAUDIO > 0 2400 int 2401 sysctl_audio(int *name, u_int namelen, void *oldp, size_t *oldlenp, 2402 void *newp, size_t newlen) 2403 { 2404 if (namelen != 1) 2405 return (ENOTDIR); 2406 2407 if (name[0] != KERN_AUDIO_RECORD) 2408 return (ENOENT); 2409 2410 return (sysctl_int(oldp, oldlenp, newp, newlen, &audio_record_enable)); 2411 } 2412 #endif 2413 2414 int 2415 sysctl_cpustats(int *name, u_int namelen, void *oldp, size_t *oldlenp, 2416 void *newp, size_t newlen) 2417 { 2418 CPU_INFO_ITERATOR cii; 2419 struct cpustats cs; 2420 struct cpu_info *ci; 2421 int found = 0; 2422 2423 if (namelen != 1) 2424 return (ENOTDIR); 2425 2426 CPU_INFO_FOREACH(cii, ci) { 2427 if (name[0] == CPU_INFO_UNIT(ci)) { 2428 found = 1; 2429 break; 2430 } 2431 } 2432 if (!found) 2433 return (ENOENT); 2434 2435 memcpy(&cs.cs_time, &ci->ci_schedstate.spc_cp_time, sizeof(cs.cs_time)); 2436 cs.cs_flags = 0; 2437 if (cpu_is_online(ci)) 2438 cs.cs_flags |= CPUSTATS_ONLINE; 2439 2440 return (sysctl_rdstruct(oldp, oldlenp, newp, &cs, sizeof(cs))); 2441 } 2442