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