1 /* 2 * Copyright (c) 1993 Jan-Simon Pendry 3 * Copyright (c) 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * Jan-Simon Pendry. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)procfs_subr.c 8.6 (Berkeley) 5/14/95 38 * 39 * $FreeBSD: src/sys/miscfs/procfs/procfs_subr.c,v 1.26.2.3 2002/02/18 21:28:04 des Exp $ 40 * $DragonFly: src/sys/vfs/procfs/procfs_subr.c,v 1.18 2007/08/25 23:27:02 corecode Exp $ 41 */ 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/sysctl.h> 46 #include <sys/proc.h> 47 #include <sys/mount.h> 48 #include <sys/vnode.h> 49 #include <sys/malloc.h> 50 #include <sys/thread2.h> 51 52 #include <vfs/procfs/procfs.h> 53 54 #define PFS_HSIZE 256 55 #define PFS_HMASK (PFS_HSIZE - 1) 56 57 static struct pfsnode *pfshead[PFS_HSIZE]; 58 static int pfsvplock; 59 60 #define PFSHASH(pid) &pfshead[(pid) & PFS_HMASK] 61 62 /* 63 * Allocate a pfsnode/vnode pair. If no error occurs the returned vnode 64 * will be referenced and exclusively locked. 65 * 66 * The pid, pfs_type, and mount point uniquely identify a pfsnode. 67 * The mount point is needed because someone might mount this filesystem 68 * twice. 69 * 70 * All pfsnodes are maintained on a singly-linked list. new nodes are 71 * only allocated when they cannot be found on this list. entries on 72 * the list are removed when the vfs reclaim entry is called. 73 * 74 * A single lock is kept for the entire list. this is needed because the 75 * getnewvnode() function can block waiting for a vnode to become free, 76 * in which case there may be more than one process trying to get the same 77 * vnode. this lock is only taken if we are going to call getnewvnode, 78 * since the kernel itself is single-threaded. 79 * 80 * If an entry is found on the list, then call vget() to take a reference 81 * and obtain the lock. This will properly re-reference the vnode if it 82 * had gotten onto the free list. 83 */ 84 int 85 procfs_allocvp(struct mount *mp, struct vnode **vpp, long pid, pfstype pfs_type) 86 { 87 struct pfsnode *pfs; 88 struct vnode *vp; 89 struct pfsnode **pp; 90 int error; 91 92 pp = PFSHASH(pid); 93 loop: 94 for (pfs = *pp; pfs; pfs = pfs->pfs_next) { 95 if (pfs->pfs_pid == pid && pfs->pfs_type == pfs_type && 96 PFSTOV(pfs)->v_mount == mp) { 97 vp = PFSTOV(pfs); 98 vhold_interlocked(vp); 99 if (vget(vp, LK_EXCLUSIVE)) { 100 vdrop(vp); 101 goto loop; 102 } 103 104 /* 105 * Make sure the vnode is still in the cache after 106 * getting the interlock to avoid racing a free. 107 */ 108 for (pfs = *pp; pfs; pfs = pfs->pfs_next) { 109 if (PFSTOV(pfs) == vp && 110 pfs->pfs_pid == pid && 111 pfs->pfs_type == pfs_type && 112 PFSTOV(pfs)->v_mount == mp) { 113 break; 114 } 115 } 116 vdrop(vp); 117 if (pfs == NULL || PFSTOV(pfs) != vp) { 118 vput(vp); 119 goto loop; 120 121 } 122 KKASSERT(vp->v_data == pfs); 123 *vpp = vp; 124 return (0); 125 } 126 } 127 128 /* 129 * otherwise lock the vp list while we call getnewvnode 130 * since that can block. 131 */ 132 if (pfsvplock & PROCFS_LOCKED) { 133 pfsvplock |= PROCFS_WANT; 134 (void) tsleep((caddr_t) &pfsvplock, 0, "pfsavp", 0); 135 goto loop; 136 } 137 pfsvplock |= PROCFS_LOCKED; 138 139 /* 140 * Do the MALLOC before the getnewvnode since doing so afterward 141 * might cause a bogus v_data pointer to get dereferenced 142 * elsewhere if MALLOC should block. 143 * 144 * XXX this may not matter anymore since getnewvnode now returns 145 * a VX locked vnode. 146 */ 147 MALLOC(pfs, struct pfsnode *, sizeof(struct pfsnode), M_TEMP, M_WAITOK); 148 149 error = getnewvnode(VT_PROCFS, mp, vpp, 0, 0); 150 if (error) { 151 kfree(pfs, M_TEMP); 152 goto out; 153 } 154 vp = *vpp; 155 156 vp->v_data = pfs; 157 158 pfs->pfs_next = 0; 159 pfs->pfs_pid = (pid_t) pid; 160 pfs->pfs_type = pfs_type; 161 pfs->pfs_vnode = vp; 162 pfs->pfs_flags = 0; 163 pfs->pfs_lockowner = 0; 164 pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type); 165 166 switch (pfs_type) { 167 case Proot: /* /proc = dr-xr-xr-x */ 168 pfs->pfs_mode = (VREAD|VEXEC) | 169 (VREAD|VEXEC) >> 3 | 170 (VREAD|VEXEC) >> 6; 171 vp->v_type = VDIR; 172 vp->v_flag = VROOT; 173 break; 174 175 case Pcurproc: /* /proc/curproc = lr--r--r-- */ 176 pfs->pfs_mode = (VREAD) | 177 (VREAD >> 3) | 178 (VREAD >> 6); 179 vp->v_type = VLNK; 180 break; 181 182 case Pproc: 183 pfs->pfs_mode = (VREAD|VEXEC) | 184 (VREAD|VEXEC) >> 3 | 185 (VREAD|VEXEC) >> 6; 186 vp->v_type = VDIR; 187 break; 188 189 case Pfile: 190 pfs->pfs_mode = (VREAD|VEXEC) | 191 (VREAD|VEXEC) >> 3 | 192 (VREAD|VEXEC) >> 6; 193 vp->v_type = VLNK; 194 break; 195 196 case Pmem: 197 pfs->pfs_mode = (VREAD|VWRITE); 198 vp->v_type = VREG; 199 break; 200 201 case Pregs: 202 case Pfpregs: 203 case Pdbregs: 204 pfs->pfs_mode = (VREAD|VWRITE); 205 vp->v_type = VREG; 206 break; 207 208 case Pctl: 209 case Pnote: 210 case Pnotepg: 211 pfs->pfs_mode = (VWRITE); 212 vp->v_type = VREG; 213 break; 214 215 case Ptype: 216 case Pmap: 217 case Pstatus: 218 case Pcmdline: 219 case Prlimit: 220 pfs->pfs_mode = (VREAD) | 221 (VREAD >> 3) | 222 (VREAD >> 6); 223 vp->v_type = VREG; 224 break; 225 226 default: 227 panic("procfs_allocvp"); 228 } 229 230 /* add to procfs vnode list */ 231 pfs->pfs_next = *pp; 232 *pp = pfs; 233 234 out: 235 pfsvplock &= ~PROCFS_LOCKED; 236 237 if (pfsvplock & PROCFS_WANT) { 238 pfsvplock &= ~PROCFS_WANT; 239 wakeup((caddr_t) &pfsvplock); 240 } 241 242 return (error); 243 } 244 245 int 246 procfs_freevp(struct vnode *vp) 247 { 248 struct pfsnode **pfspp; 249 struct pfsnode *pfs; 250 251 pfs = VTOPFS(vp); 252 vp->v_data = NULL; 253 254 pfspp = PFSHASH(pfs->pfs_pid); 255 while (*pfspp != pfs && *pfspp) 256 pfspp = &(*pfspp)->pfs_next; 257 KKASSERT(*pfspp); 258 *pfspp = pfs->pfs_next; 259 pfs->pfs_next = NULL; 260 pfs->pfs_vnode = NULL; 261 kfree(pfs, M_TEMP); 262 return (0); 263 } 264 265 /* 266 * Try to find the calling pid. Note that pfind() 267 * now references the proc structure to be returned 268 * and needs to be released later with PRELE(). 269 */ 270 struct proc * 271 pfs_pfind(pid_t pfs_pid) 272 { 273 struct proc *p = NULL; 274 275 if (pfs_pid == 0) { 276 p = &proc0; 277 PHOLD(p); 278 } else { 279 p = pfind(pfs_pid); 280 } 281 282 /* 283 * Make sure the process is not in the middle of exiting (where 284 * a lot of its structural members may wind up being NULL). If it 285 * is we give up on it. 286 */ 287 if (p) { 288 lwkt_gettoken(&p->p_token); 289 if (p->p_flags & P_WEXIT) { 290 lwkt_reltoken(&p->p_token); 291 PRELE(p); 292 p = NULL; 293 } 294 } 295 return p; 296 } 297 298 void 299 pfs_pdone(struct proc *p) 300 { 301 if (p) { 302 lwkt_reltoken(&p->p_token); 303 PRELE(p); 304 } 305 } 306 307 int 308 procfs_rw(struct vop_read_args *ap) 309 { 310 struct vnode *vp = ap->a_vp; 311 struct uio *uio = ap->a_uio; 312 struct thread *curtd = uio->uio_td; 313 struct proc *curp; 314 struct pfsnode *pfs = VTOPFS(vp); 315 struct proc *p; 316 struct lwp *lp; 317 int rtval; 318 319 if (curtd == NULL) 320 return (EINVAL); 321 if ((curp = curtd->td_proc) == NULL) /* XXX */ 322 return (EINVAL); 323 324 lwkt_gettoken(&proc_token); 325 p = pfs_pfind(pfs->pfs_pid); 326 if (p == NULL) { 327 rtval = (EINVAL); 328 goto out; 329 } 330 if (p->p_pid == 1 && securelevel > 0 && uio->uio_rw == UIO_WRITE) { 331 rtval = (EACCES); 332 goto out; 333 } 334 /* XXX lwp */ 335 lp = FIRST_LWP_IN_PROC(p); 336 LWPHOLD(lp); 337 338 while (pfs->pfs_lockowner) { 339 tsleep(&pfs->pfs_lockowner, 0, "pfslck", 0); 340 } 341 pfs->pfs_lockowner = curproc->p_pid; 342 343 switch (pfs->pfs_type) { 344 case Pnote: 345 case Pnotepg: 346 rtval = procfs_donote(curp, lp, pfs, uio); 347 break; 348 349 case Pregs: 350 rtval = procfs_doregs(curp, lp, pfs, uio); 351 break; 352 353 case Pfpregs: 354 rtval = procfs_dofpregs(curp, lp, pfs, uio); 355 break; 356 357 case Pdbregs: 358 rtval = procfs_dodbregs(curp, lp, pfs, uio); 359 break; 360 361 case Pctl: 362 rtval = procfs_doctl(curp, lp, pfs, uio); 363 break; 364 365 case Pstatus: 366 rtval = procfs_dostatus(curp, lp, pfs, uio); 367 break; 368 369 case Pmap: 370 rtval = procfs_domap(curp, lp, pfs, uio); 371 break; 372 373 case Pmem: 374 rtval = procfs_domem(curp, lp, pfs, uio); 375 break; 376 377 case Ptype: 378 rtval = procfs_dotype(curp, lp, pfs, uio); 379 break; 380 381 case Pcmdline: 382 rtval = procfs_docmdline(curp, lp, pfs, uio); 383 break; 384 385 case Prlimit: 386 rtval = procfs_dorlimit(curp, lp, pfs, uio); 387 break; 388 389 default: 390 rtval = EOPNOTSUPP; 391 break; 392 } 393 LWPRELE(lp); 394 395 pfs->pfs_lockowner = 0; 396 wakeup(&pfs->pfs_lockowner); 397 398 out: 399 pfs_pdone(p); 400 lwkt_reltoken(&proc_token); 401 402 return rtval; 403 } 404 405 /* 406 * Get a string from userland into (buf). Strip a trailing 407 * nl character (to allow easy access from the shell). 408 * The buffer should be *buflenp + 1 chars long. vfs_getuserstr 409 * will automatically add a nul char at the end. 410 * 411 * Returns 0 on success or the following errors 412 * 413 * EINVAL: file offset is non-zero. 414 * EMSGSIZE: message is longer than kernel buffer 415 * EFAULT: user i/o buffer is not addressable 416 */ 417 int 418 vfs_getuserstr(struct uio *uio, char *buf, int *buflenp) 419 { 420 int xlen; 421 int error; 422 423 if (uio->uio_offset != 0) 424 return (EINVAL); 425 426 xlen = *buflenp; 427 428 /* must be able to read the whole string in one go */ 429 if (xlen < uio->uio_resid) 430 return (EMSGSIZE); 431 xlen = uio->uio_resid; 432 433 if ((error = uiomove(buf, xlen, uio)) != 0) 434 return (error); 435 436 /* allow multiple writes without seeks */ 437 uio->uio_offset = 0; 438 439 /* cleanup string and remove trailing newline */ 440 buf[xlen] = '\0'; 441 xlen = strlen(buf); 442 if (xlen > 0 && buf[xlen-1] == '\n') 443 buf[--xlen] = '\0'; 444 *buflenp = xlen; 445 446 return (0); 447 } 448 449 vfs_namemap_t * 450 vfs_findname(vfs_namemap_t *nm, char *buf, int buflen) 451 { 452 453 for (; nm->nm_name; nm++) 454 if (bcmp(buf, nm->nm_name, buflen+1) == 0) 455 return (nm); 456 457 return (0); 458 } 459 460 void 461 procfs_exit(struct thread *td) 462 { 463 struct pfsnode *pfs; 464 struct vnode *vp; 465 pid_t pid; 466 467 KKASSERT(td->td_proc); 468 pid = td->td_proc->p_pid; 469 470 /* 471 * NOTE: We can't just vgone() the vnode any more, not while 472 * it may potentially still be active. This will clean 473 * the vp and clear the mount and cause the new VOP subsystem 474 * to assert or panic when someone tries to do an operation 475 * on an open (exited) procfs descriptor. 476 * 477 * Prevent further operations on this pid by setting pfs_pid to -1. 478 * Note that a pfs_pid of 0 is used for nodes which do not track 479 * any particular pid. 480 * 481 * Use vx_get() to properly ref/lock a vp which may not have any 482 * refs and which may or may not already be reclaimed. vx_put() 483 * will then properly deactivate it and cause it to be recycled. 484 * 485 * The hash table can also get ripped out from under us when 486 * we block so take the easy way out and restart the scan. 487 */ 488 again: 489 pfs = *PFSHASH(pid); 490 while (pfs) { 491 if (pfs->pfs_pid == pid) { 492 vp = PFSTOV(pfs); 493 vx_get(vp); 494 pfs->pfs_pid |= PFS_DEAD; /* does not effect hash */ 495 vx_put(vp); 496 goto again; 497 } 498 pfs = pfs->pfs_next; 499 } 500 } 501 502