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