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