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