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