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.10 2004/08/28 19:02:27 dillon 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 51 #include <vfs/procfs/procfs.h> 52 53 #define PFS_HSIZE 256 54 #define PFS_HMASK (PFS_HSIZE - 1) 55 56 static struct pfsnode *pfshead[PFS_HSIZE]; 57 static int pfsvplock; 58 59 #define PFSHASH(pid) &pfshead[(pid) & PFS_HMASK] 60 61 /* 62 * Allocate a pfsnode/vnode pair. If no error occurs the returned vnode 63 * will be referenced and exclusively locked. 64 * 65 * The pid, pfs_type, and mount point uniquely identify a pfsnode. 66 * The mount point is needed because someone might mount this filesystem 67 * twice. 68 * 69 * All pfsnodes are maintained on a singly-linked list. new nodes are 70 * only allocated when they cannot be found on this list. entries on 71 * the list are removed when the vfs reclaim entry is called. 72 * 73 * A single lock is kept for the entire list. this is needed because the 74 * getnewvnode() function can block waiting for a vnode to become free, 75 * in which case there may be more than one process trying to get the same 76 * vnode. this lock is only taken if we are going to call getnewvnode, 77 * since the kernel itself is single-threaded. 78 * 79 * If an entry is found on the list, then call vget() to take a reference 80 * and obtain the lock. This will properly re-reference the vnode if it 81 * had gotten onto the free list. 82 */ 83 int 84 procfs_allocvp(struct mount *mp, struct vnode **vpp, long pid, pfstype pfs_type) 85 { 86 struct thread *td = curthread; /* XXX */ 87 struct pfsnode *pfs; 88 struct vnode *vp; 89 struct pfsnode **pp; 90 int error; 91 lwkt_tokref vlock; 92 93 pp = PFSHASH(pid); 94 loop: 95 for (pfs = *pp; pfs; pfs = pfs->pfs_next) { 96 if (pfs->pfs_pid == pid && pfs->pfs_type == pfs_type && 97 PFSTOV(pfs)->v_mount == mp) { 98 vp = PFSTOV(pfs); 99 lwkt_gettoken(&vlock, vp->v_interlock); 100 101 /* 102 * Make sure the vnode is still in the cache after 103 * getting the interlock to avoid racing a free. 104 */ 105 for (pfs = *pp; pfs; pfs = pfs->pfs_next) { 106 if (PFSTOV(pfs) == vp && 107 pfs->pfs_pid == pid && 108 pfs->pfs_type == pfs_type && 109 PFSTOV(pfs)->v_mount == mp) { 110 break; 111 } 112 } 113 if (pfs == NULL) { 114 lwkt_reltoken(&vlock); 115 goto loop; 116 117 } 118 if (vget(vp, &vlock, LK_EXCLUSIVE | LK_INTERLOCK, td)) 119 goto loop; 120 *vpp = vp; 121 return (0); 122 } 123 } 124 125 /* 126 * otherwise lock the vp list while we call getnewvnode 127 * since that can block. 128 */ 129 if (pfsvplock & PROCFS_LOCKED) { 130 pfsvplock |= PROCFS_WANT; 131 (void) tsleep((caddr_t) &pfsvplock, 0, "pfsavp", 0); 132 goto loop; 133 } 134 pfsvplock |= PROCFS_LOCKED; 135 136 /* 137 * Do the MALLOC before the getnewvnode since doing so afterward 138 * might cause a bogus v_data pointer to get dereferenced 139 * elsewhere if MALLOC should block. 140 */ 141 MALLOC(pfs, struct pfsnode *, sizeof(struct pfsnode), M_TEMP, M_WAITOK); 142 143 error = getnewvnode(VT_PROCFS, mp, mp->mnt_vn_ops, vpp, 0, 0); 144 if (error) { 145 free(pfs, M_TEMP); 146 goto out; 147 } 148 vp = *vpp; 149 150 if (lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL, td)) 151 panic("procfs_allocvp: unexpected lock vailure"); 152 153 vp->v_data = pfs; 154 155 pfs->pfs_next = 0; 156 pfs->pfs_pid = (pid_t) pid; 157 pfs->pfs_type = pfs_type; 158 pfs->pfs_vnode = vp; 159 pfs->pfs_flags = 0; 160 pfs->pfs_lockowner = 0; 161 pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type); 162 163 switch (pfs_type) { 164 case Proot: /* /proc = dr-xr-xr-x */ 165 pfs->pfs_mode = (VREAD|VEXEC) | 166 (VREAD|VEXEC) >> 3 | 167 (VREAD|VEXEC) >> 6; 168 vp->v_type = VDIR; 169 vp->v_flag = VROOT; 170 break; 171 172 case Pcurproc: /* /proc/curproc = lr--r--r-- */ 173 pfs->pfs_mode = (VREAD) | 174 (VREAD >> 3) | 175 (VREAD >> 6); 176 vp->v_type = VLNK; 177 break; 178 179 case Pproc: 180 pfs->pfs_mode = (VREAD|VEXEC) | 181 (VREAD|VEXEC) >> 3 | 182 (VREAD|VEXEC) >> 6; 183 vp->v_type = VDIR; 184 break; 185 186 case Pfile: 187 pfs->pfs_mode = (VREAD|VEXEC) | 188 (VREAD|VEXEC) >> 3 | 189 (VREAD|VEXEC) >> 6; 190 vp->v_type = VLNK; 191 break; 192 193 case Pmem: 194 pfs->pfs_mode = (VREAD|VWRITE); 195 vp->v_type = VREG; 196 break; 197 198 case Pregs: 199 case Pfpregs: 200 case Pdbregs: 201 pfs->pfs_mode = (VREAD|VWRITE); 202 vp->v_type = VREG; 203 break; 204 205 case Pctl: 206 case Pnote: 207 case Pnotepg: 208 pfs->pfs_mode = (VWRITE); 209 vp->v_type = VREG; 210 break; 211 212 case Ptype: 213 case Pmap: 214 case Pstatus: 215 case Pcmdline: 216 case Prlimit: 217 pfs->pfs_mode = (VREAD) | 218 (VREAD >> 3) | 219 (VREAD >> 6); 220 vp->v_type = VREG; 221 break; 222 223 default: 224 panic("procfs_allocvp"); 225 } 226 227 /* add to procfs vnode list */ 228 pfs->pfs_next = *pp; 229 *pp = pfs; 230 231 out: 232 pfsvplock &= ~PROCFS_LOCKED; 233 234 if (pfsvplock & PROCFS_WANT) { 235 pfsvplock &= ~PROCFS_WANT; 236 wakeup((caddr_t) &pfsvplock); 237 } 238 239 return (error); 240 } 241 242 int 243 procfs_freevp(struct vnode *vp) 244 { 245 struct pfsnode **pfspp; 246 struct pfsnode *pfs; 247 248 pfs = VTOPFS(vp); 249 vp->v_data = NULL; 250 251 pfspp = PFSHASH(pfs->pfs_pid); 252 while (*pfspp != pfs && *pfspp) 253 pfspp = &(*pfspp)->pfs_next; 254 KKASSERT(*pfspp); 255 *pfspp = pfs->pfs_next; 256 pfs->pfs_next = NULL; 257 free(pfs, M_TEMP); 258 return (0); 259 } 260 261 int 262 procfs_rw(struct vop_read_args *ap) 263 { 264 struct vnode *vp = ap->a_vp; 265 struct uio *uio = ap->a_uio; 266 struct thread *curtd = uio->uio_td; 267 struct proc *curp; 268 struct pfsnode *pfs = VTOPFS(vp); 269 struct proc *p; 270 int rtval; 271 272 if (curtd == NULL) 273 return (EINVAL); 274 if ((curp = curtd->td_proc) == NULL) /* XXX */ 275 return (EINVAL); 276 277 p = PFIND(pfs->pfs_pid); 278 if (p == NULL) 279 return (EINVAL); 280 if (p->p_pid == 1 && securelevel > 0 && uio->uio_rw == UIO_WRITE) 281 return (EACCES); 282 283 while (pfs->pfs_lockowner) { 284 tsleep(&pfs->pfs_lockowner, 0, "pfslck", 0); 285 } 286 pfs->pfs_lockowner = curproc->p_pid; 287 288 switch (pfs->pfs_type) { 289 case Pnote: 290 case Pnotepg: 291 rtval = procfs_donote(curp, p, pfs, uio); 292 break; 293 294 case Pregs: 295 rtval = procfs_doregs(curp, p, pfs, uio); 296 break; 297 298 case Pfpregs: 299 rtval = procfs_dofpregs(curp, p, pfs, uio); 300 break; 301 302 case Pdbregs: 303 rtval = procfs_dodbregs(curp, p, pfs, uio); 304 break; 305 306 case Pctl: 307 rtval = procfs_doctl(curp, p, pfs, uio); 308 break; 309 310 case Pstatus: 311 rtval = procfs_dostatus(curp, p, pfs, uio); 312 break; 313 314 case Pmap: 315 rtval = procfs_domap(curp, p, pfs, uio); 316 break; 317 318 case Pmem: 319 rtval = procfs_domem(curp, p, pfs, uio); 320 break; 321 322 case Ptype: 323 rtval = procfs_dotype(curp, p, pfs, uio); 324 break; 325 326 case Pcmdline: 327 rtval = procfs_docmdline(curp, p, pfs, uio); 328 break; 329 330 case Prlimit: 331 rtval = procfs_dorlimit(curp, p, pfs, uio); 332 break; 333 334 default: 335 rtval = EOPNOTSUPP; 336 break; 337 } 338 pfs->pfs_lockowner = 0; 339 wakeup(&pfs->pfs_lockowner); 340 return rtval; 341 } 342 343 /* 344 * Get a string from userland into (buf). Strip a trailing 345 * nl character (to allow easy access from the shell). 346 * The buffer should be *buflenp + 1 chars long. vfs_getuserstr 347 * will automatically add a nul char at the end. 348 * 349 * Returns 0 on success or the following errors 350 * 351 * EINVAL: file offset is non-zero. 352 * EMSGSIZE: message is longer than kernel buffer 353 * EFAULT: user i/o buffer is not addressable 354 */ 355 int 356 vfs_getuserstr(struct uio *uio, char *buf, int *buflenp) 357 { 358 int xlen; 359 int error; 360 361 if (uio->uio_offset != 0) 362 return (EINVAL); 363 364 xlen = *buflenp; 365 366 /* must be able to read the whole string in one go */ 367 if (xlen < uio->uio_resid) 368 return (EMSGSIZE); 369 xlen = uio->uio_resid; 370 371 if ((error = uiomove(buf, xlen, uio)) != 0) 372 return (error); 373 374 /* allow multiple writes without seeks */ 375 uio->uio_offset = 0; 376 377 /* cleanup string and remove trailing newline */ 378 buf[xlen] = '\0'; 379 xlen = strlen(buf); 380 if (xlen > 0 && buf[xlen-1] == '\n') 381 buf[--xlen] = '\0'; 382 *buflenp = xlen; 383 384 return (0); 385 } 386 387 vfs_namemap_t * 388 vfs_findname(vfs_namemap_t *nm, char *buf, int buflen) 389 { 390 391 for (; nm->nm_name; nm++) 392 if (bcmp(buf, nm->nm_name, buflen+1) == 0) 393 return (nm); 394 395 return (0); 396 } 397 398 void 399 procfs_exit(struct thread *td) 400 { 401 struct pfsnode *pfs; 402 pid_t pid; 403 404 KKASSERT(td->td_proc); 405 pid = td->td_proc->p_pid; 406 407 /* 408 * The reason for this loop is not obvious -- basicly, 409 * procfs_freevp(), which is called via vgone() (eventually), 410 * removes the specified procfs node from the pfshead list. 411 * It does this by *pfsp = pfs->pfs_next, meaning that it 412 * overwrites the node. So when we do pfs = pfs->next, we 413 * end up skipping the node that replaces the one that was 414 * vgone'd. Since it may have been the last one on the list, 415 * it may also have been set to null -- but *our* pfs pointer, 416 * here, doesn't see this. So the loop starts from the beginning 417 * again. 418 * 419 * This is not a for() loop because the final event 420 * would be "pfs = pfs->pfs_next"; in the case where 421 * pfs is set to pfshead again, that would mean that 422 * pfshead is skipped over. 423 * 424 */ 425 again: 426 pfs = *PFSHASH(pid); 427 while (pfs) { 428 if (pfs->pfs_pid == pid) { 429 vgone(PFSTOV(pfs)); 430 goto again; 431 } 432 pfs = pfs->pfs_next; 433 } 434 } 435 436