1 /* $NetBSD: procfs_vnops.c,v 1.120 2004/10/04 08:40:18 yamt Exp $ */ 2 3 /* 4 * Copyright (c) 1993, 1995 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Jan-Simon Pendry. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95 35 */ 36 37 /* 38 * Copyright (c) 1993 Jan-Simon Pendry 39 * 40 * This code is derived from software contributed to Berkeley by 41 * Jan-Simon Pendry. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. All advertising materials mentioning features or use of this software 52 * must display the following acknowledgement: 53 * This product includes software developed by the University of 54 * California, Berkeley and its contributors. 55 * 4. Neither the name of the University nor the names of its contributors 56 * may be used to endorse or promote products derived from this software 57 * without specific prior written permission. 58 * 59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 69 * SUCH DAMAGE. 70 * 71 * @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95 72 */ 73 74 /* 75 * procfs vnode interface 76 */ 77 78 #include <sys/cdefs.h> 79 __KERNEL_RCSID(0, "$NetBSD: procfs_vnops.c,v 1.120 2004/10/04 08:40:18 yamt Exp $"); 80 81 #include <sys/param.h> 82 #include <sys/systm.h> 83 #include <sys/time.h> 84 #include <sys/kernel.h> 85 #include <sys/file.h> 86 #include <sys/filedesc.h> 87 #include <sys/proc.h> 88 #include <sys/vnode.h> 89 #include <sys/namei.h> 90 #include <sys/malloc.h> 91 #include <sys/mount.h> 92 #include <sys/dirent.h> 93 #include <sys/resourcevar.h> 94 #include <sys/stat.h> 95 #include <sys/ptrace.h> 96 97 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */ 98 99 #include <machine/reg.h> 100 101 #include <miscfs/genfs/genfs.h> 102 #include <miscfs/procfs/procfs.h> 103 104 /* 105 * Vnode Operations. 106 * 107 */ 108 109 static int procfs_validfile_linux __P((struct proc *, struct mount *)); 110 static int procfs_root_readdir_callback(struct proc *, void *); 111 112 /* 113 * This is a list of the valid names in the 114 * process-specific sub-directories. It is 115 * used in procfs_lookup and procfs_readdir 116 */ 117 static const struct proc_target { 118 u_char pt_type; 119 u_char pt_namlen; 120 const char *pt_name; 121 pfstype pt_pfstype; 122 int (*pt_valid) __P((struct proc *, struct mount *)); 123 } proc_targets[] = { 124 #define N(s) sizeof(s)-1, s 125 /* name type validp */ 126 { DT_DIR, N("."), PFSproc, NULL }, 127 { DT_DIR, N(".."), PFSroot, NULL }, 128 { DT_DIR, N("fd"), PFSfd, NULL }, 129 { DT_REG, N("file"), PFSfile, procfs_validfile }, 130 { DT_REG, N("mem"), PFSmem, NULL }, 131 { DT_REG, N("regs"), PFSregs, procfs_validregs }, 132 { DT_REG, N("fpregs"), PFSfpregs, procfs_validfpregs }, 133 { DT_REG, N("ctl"), PFSctl, NULL }, 134 { DT_REG, N("stat"), PFSstat, procfs_validfile_linux }, 135 { DT_REG, N("status"), PFSstatus, NULL }, 136 { DT_REG, N("note"), PFSnote, NULL }, 137 { DT_REG, N("notepg"), PFSnotepg, NULL }, 138 { DT_REG, N("map"), PFSmap, procfs_validmap }, 139 { DT_REG, N("maps"), PFSmaps, procfs_validmap }, 140 { DT_REG, N("cmdline"), PFScmdline, NULL }, 141 { DT_REG, N("exe"), PFSfile, procfs_validfile_linux }, 142 #ifdef __HAVE_PROCFS_MACHDEP 143 PROCFS_MACHDEP_NODETYPE_DEFNS 144 #endif 145 #undef N 146 }; 147 static const int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]); 148 149 /* 150 * List of files in the root directory. Note: the validate function will 151 * be called with p == NULL for these ones. 152 */ 153 static const struct proc_target proc_root_targets[] = { 154 #define N(s) sizeof(s)-1, s 155 /* name type validp */ 156 { DT_REG, N("meminfo"), PFSmeminfo, procfs_validfile_linux }, 157 { DT_REG, N("cpuinfo"), PFScpuinfo, procfs_validfile_linux }, 158 { DT_REG, N("uptime"), PFSuptime, procfs_validfile_linux }, 159 { DT_REG, N("mounts"), PFSmounts, procfs_validfile_linux }, 160 #undef N 161 }; 162 static const int nproc_root_targets = 163 sizeof(proc_root_targets) / sizeof(proc_root_targets[0]); 164 165 int procfs_lookup __P((void *)); 166 #define procfs_create genfs_eopnotsupp 167 #define procfs_mknod genfs_eopnotsupp 168 int procfs_open __P((void *)); 169 int procfs_close __P((void *)); 170 int procfs_access __P((void *)); 171 int procfs_getattr __P((void *)); 172 int procfs_setattr __P((void *)); 173 #define procfs_read procfs_rw 174 #define procfs_write procfs_rw 175 #define procfs_fcntl genfs_fcntl 176 #define procfs_ioctl genfs_enoioctl 177 #define procfs_poll genfs_poll 178 #define procfs_revoke genfs_revoke 179 #define procfs_fsync genfs_nullop 180 #define procfs_seek genfs_nullop 181 #define procfs_remove genfs_eopnotsupp 182 int procfs_link __P((void *)); 183 #define procfs_rename genfs_eopnotsupp 184 #define procfs_mkdir genfs_eopnotsupp 185 #define procfs_rmdir genfs_eopnotsupp 186 int procfs_symlink __P((void *)); 187 int procfs_readdir __P((void *)); 188 int procfs_readlink __P((void *)); 189 #define procfs_abortop genfs_abortop 190 int procfs_inactive __P((void *)); 191 int procfs_reclaim __P((void *)); 192 #define procfs_lock genfs_lock 193 #define procfs_unlock genfs_unlock 194 #define procfs_bmap genfs_badop 195 #define procfs_strategy genfs_badop 196 int procfs_print __P((void *)); 197 int procfs_pathconf __P((void *)); 198 #define procfs_islocked genfs_islocked 199 #define procfs_advlock genfs_einval 200 #define procfs_blkatoff genfs_eopnotsupp 201 #define procfs_valloc genfs_eopnotsupp 202 #define procfs_vfree genfs_nullop 203 #define procfs_truncate genfs_eopnotsupp 204 #define procfs_update genfs_nullop 205 #define procfs_bwrite genfs_eopnotsupp 206 #define procfs_putpages genfs_null_putpages 207 208 static int atoi __P((const char *, size_t)); 209 210 /* 211 * procfs vnode operations. 212 */ 213 int (**procfs_vnodeop_p) __P((void *)); 214 const struct vnodeopv_entry_desc procfs_vnodeop_entries[] = { 215 { &vop_default_desc, vn_default_error }, 216 { &vop_lookup_desc, procfs_lookup }, /* lookup */ 217 { &vop_create_desc, procfs_create }, /* create */ 218 { &vop_mknod_desc, procfs_mknod }, /* mknod */ 219 { &vop_open_desc, procfs_open }, /* open */ 220 { &vop_close_desc, procfs_close }, /* close */ 221 { &vop_access_desc, procfs_access }, /* access */ 222 { &vop_getattr_desc, procfs_getattr }, /* getattr */ 223 { &vop_setattr_desc, procfs_setattr }, /* setattr */ 224 { &vop_read_desc, procfs_read }, /* read */ 225 { &vop_write_desc, procfs_write }, /* write */ 226 { &vop_fcntl_desc, procfs_fcntl }, /* fcntl */ 227 { &vop_ioctl_desc, procfs_ioctl }, /* ioctl */ 228 { &vop_poll_desc, procfs_poll }, /* poll */ 229 { &vop_revoke_desc, procfs_revoke }, /* revoke */ 230 { &vop_fsync_desc, procfs_fsync }, /* fsync */ 231 { &vop_seek_desc, procfs_seek }, /* seek */ 232 { &vop_remove_desc, procfs_remove }, /* remove */ 233 { &vop_link_desc, procfs_link }, /* link */ 234 { &vop_rename_desc, procfs_rename }, /* rename */ 235 { &vop_mkdir_desc, procfs_mkdir }, /* mkdir */ 236 { &vop_rmdir_desc, procfs_rmdir }, /* rmdir */ 237 { &vop_symlink_desc, procfs_symlink }, /* symlink */ 238 { &vop_readdir_desc, procfs_readdir }, /* readdir */ 239 { &vop_readlink_desc, procfs_readlink }, /* readlink */ 240 { &vop_abortop_desc, procfs_abortop }, /* abortop */ 241 { &vop_inactive_desc, procfs_inactive }, /* inactive */ 242 { &vop_reclaim_desc, procfs_reclaim }, /* reclaim */ 243 { &vop_lock_desc, procfs_lock }, /* lock */ 244 { &vop_unlock_desc, procfs_unlock }, /* unlock */ 245 { &vop_bmap_desc, procfs_bmap }, /* bmap */ 246 { &vop_strategy_desc, procfs_strategy }, /* strategy */ 247 { &vop_print_desc, procfs_print }, /* print */ 248 { &vop_islocked_desc, procfs_islocked }, /* islocked */ 249 { &vop_pathconf_desc, procfs_pathconf }, /* pathconf */ 250 { &vop_advlock_desc, procfs_advlock }, /* advlock */ 251 { &vop_blkatoff_desc, procfs_blkatoff }, /* blkatoff */ 252 { &vop_valloc_desc, procfs_valloc }, /* valloc */ 253 { &vop_vfree_desc, procfs_vfree }, /* vfree */ 254 { &vop_truncate_desc, procfs_truncate }, /* truncate */ 255 { &vop_update_desc, procfs_update }, /* update */ 256 { &vop_putpages_desc, procfs_putpages }, /* putpages */ 257 { NULL, NULL } 258 }; 259 const struct vnodeopv_desc procfs_vnodeop_opv_desc = 260 { &procfs_vnodeop_p, procfs_vnodeop_entries }; 261 /* 262 * set things up for doing i/o on 263 * the pfsnode (vp). (vp) is locked 264 * on entry, and should be left locked 265 * on exit. 266 * 267 * for procfs we don't need to do anything 268 * in particular for i/o. all that is done 269 * is to support exclusive open on process 270 * memory images. 271 */ 272 int 273 procfs_open(v) 274 void *v; 275 { 276 struct vop_open_args /* { 277 struct vnode *a_vp; 278 int a_mode; 279 struct ucred *a_cred; 280 struct proc *a_p; 281 } */ *ap = v; 282 struct pfsnode *pfs = VTOPFS(ap->a_vp); 283 struct proc *p1, *p2; 284 int error; 285 286 p1 = ap->a_p; /* tracer */ 287 p2 = PFIND(pfs->pfs_pid); /* traced */ 288 289 if (p2 == NULL) 290 return (ENOENT); /* was ESRCH, jsp */ 291 292 switch (pfs->pfs_type) { 293 case PFSmem: 294 if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) || 295 ((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE))) 296 return (EBUSY); 297 298 if ((error = process_checkioperm(p1, p2)) != 0) 299 return (error); 300 301 if (ap->a_mode & FWRITE) 302 pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL); 303 304 return (0); 305 306 default: 307 break; 308 } 309 310 return (0); 311 } 312 313 /* 314 * close the pfsnode (vp) after doing i/o. 315 * (vp) is not locked on entry or exit. 316 * 317 * nothing to do for procfs other than undo 318 * any exclusive open flag (see _open above). 319 */ 320 int 321 procfs_close(v) 322 void *v; 323 { 324 struct vop_close_args /* { 325 struct vnode *a_vp; 326 int a_fflag; 327 struct ucred *a_cred; 328 struct proc *a_p; 329 } */ *ap = v; 330 struct pfsnode *pfs = VTOPFS(ap->a_vp); 331 332 switch (pfs->pfs_type) { 333 case PFSmem: 334 if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL)) 335 pfs->pfs_flags &= ~(FWRITE|O_EXCL); 336 break; 337 338 default: 339 break; 340 } 341 342 return (0); 343 } 344 345 /* 346 * _inactive is called when the pfsnode 347 * is vrele'd and the reference count goes 348 * to zero. (vp) will be on the vnode free 349 * list, so to get it back vget() must be 350 * used. 351 * 352 * for procfs, check if the process is still 353 * alive and if it isn't then just throw away 354 * the vnode by calling vgone(). this may 355 * be overkill and a waste of time since the 356 * chances are that the process will still be 357 * there and PFIND is not free. 358 * 359 * (vp) is locked on entry, but must be unlocked on exit. 360 */ 361 int 362 procfs_inactive(v) 363 void *v; 364 { 365 struct vop_inactive_args /* { 366 struct vnode *a_vp; 367 struct proc *a_p; 368 } */ *ap = v; 369 struct pfsnode *pfs = VTOPFS(ap->a_vp); 370 371 VOP_UNLOCK(ap->a_vp, 0); 372 if (PFIND(pfs->pfs_pid) == NULL) 373 vgone(ap->a_vp); 374 375 return (0); 376 } 377 378 /* 379 * _reclaim is called when getnewvnode() 380 * wants to make use of an entry on the vnode 381 * free list. at this time the filesystem needs 382 * to free any private data and remove the node 383 * from any private lists. 384 */ 385 int 386 procfs_reclaim(v) 387 void *v; 388 { 389 struct vop_reclaim_args /* { 390 struct vnode *a_vp; 391 } */ *ap = v; 392 393 return (procfs_freevp(ap->a_vp)); 394 } 395 396 /* 397 * Return POSIX pathconf information applicable to special devices. 398 */ 399 int 400 procfs_pathconf(v) 401 void *v; 402 { 403 struct vop_pathconf_args /* { 404 struct vnode *a_vp; 405 int a_name; 406 register_t *a_retval; 407 } */ *ap = v; 408 409 switch (ap->a_name) { 410 case _PC_LINK_MAX: 411 *ap->a_retval = LINK_MAX; 412 return (0); 413 case _PC_MAX_CANON: 414 *ap->a_retval = MAX_CANON; 415 return (0); 416 case _PC_MAX_INPUT: 417 *ap->a_retval = MAX_INPUT; 418 return (0); 419 case _PC_PIPE_BUF: 420 *ap->a_retval = PIPE_BUF; 421 return (0); 422 case _PC_CHOWN_RESTRICTED: 423 *ap->a_retval = 1; 424 return (0); 425 case _PC_VDISABLE: 426 *ap->a_retval = _POSIX_VDISABLE; 427 return (0); 428 case _PC_SYNC_IO: 429 *ap->a_retval = 1; 430 return (0); 431 default: 432 return (EINVAL); 433 } 434 /* NOTREACHED */ 435 } 436 437 /* 438 * _print is used for debugging. 439 * just print a readable description 440 * of (vp). 441 */ 442 int 443 procfs_print(v) 444 void *v; 445 { 446 struct vop_print_args /* { 447 struct vnode *a_vp; 448 } */ *ap = v; 449 struct pfsnode *pfs = VTOPFS(ap->a_vp); 450 451 printf("tag VT_PROCFS, type %d, pid %d, mode %x, flags %lx\n", 452 pfs->pfs_type, pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags); 453 return 0; 454 } 455 456 int 457 procfs_link(v) 458 void *v; 459 { 460 struct vop_link_args /* { 461 struct vnode *a_dvp; 462 struct vnode *a_vp; 463 struct componentname *a_cnp; 464 } */ *ap = v; 465 466 VOP_ABORTOP(ap->a_dvp, ap->a_cnp); 467 vput(ap->a_dvp); 468 return (EROFS); 469 } 470 471 int 472 procfs_symlink(v) 473 void *v; 474 { 475 struct vop_symlink_args /* { 476 struct vnode *a_dvp; 477 struct vnode **a_vpp; 478 struct componentname *a_cnp; 479 struct vattr *a_vap; 480 char *a_target; 481 } */ *ap = v; 482 483 VOP_ABORTOP(ap->a_dvp, ap->a_cnp); 484 vput(ap->a_dvp); 485 return (EROFS); 486 } 487 488 /* 489 * Invent attributes for pfsnode (vp) and store 490 * them in (vap). 491 * Directories lengths are returned as zero since 492 * any real length would require the genuine size 493 * to be computed, and nothing cares anyway. 494 * 495 * this is relatively minimal for procfs. 496 */ 497 int 498 procfs_getattr(v) 499 void *v; 500 { 501 struct vop_getattr_args /* { 502 struct vnode *a_vp; 503 struct vattr *a_vap; 504 struct ucred *a_cred; 505 struct proc *a_p; 506 } */ *ap = v; 507 struct pfsnode *pfs = VTOPFS(ap->a_vp); 508 struct vattr *vap = ap->a_vap; 509 struct proc *procp; 510 int error; 511 512 /* first check the process still exists */ 513 switch (pfs->pfs_type) { 514 case PFSroot: 515 case PFScurproc: 516 case PFSself: 517 procp = 0; 518 break; 519 520 default: 521 procp = PFIND(pfs->pfs_pid); 522 if (procp == NULL) 523 return (ENOENT); 524 break; 525 } 526 527 error = 0; 528 529 /* start by zeroing out the attributes */ 530 VATTR_NULL(vap); 531 532 /* next do all the common fields */ 533 vap->va_type = ap->a_vp->v_type; 534 vap->va_mode = pfs->pfs_mode; 535 vap->va_fileid = pfs->pfs_fileno; 536 vap->va_flags = 0; 537 vap->va_blocksize = PAGE_SIZE; 538 539 /* 540 * Make all times be current TOD. Avoid microtime(9), it's slow. 541 * We don't guard the read from time(9) with splclock(9) since we 542 * don't actually need to be THAT sure the access is atomic. 543 * 544 * It would be possible to get the process start 545 * time from the p_stat structure, but there's 546 * no "file creation" time stamp anyway, and the 547 * p_stat structure is not addressible if u. gets 548 * swapped out for that process. 549 */ 550 TIMEVAL_TO_TIMESPEC(&time, &vap->va_ctime); 551 vap->va_atime = vap->va_mtime = vap->va_ctime; 552 553 switch (pfs->pfs_type) { 554 case PFSmem: 555 case PFSregs: 556 case PFSfpregs: 557 #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES) 558 PROCFS_MACHDEP_PROTECT_CASES 559 #endif 560 /* 561 * If the process has exercised some setuid or setgid 562 * privilege, then rip away read/write permission so 563 * that only root can gain access. 564 */ 565 if (procp->p_flag & P_SUGID) 566 vap->va_mode &= ~(S_IRUSR|S_IWUSR); 567 /* FALLTHROUGH */ 568 case PFSctl: 569 case PFSstatus: 570 case PFSstat: 571 case PFSnote: 572 case PFSnotepg: 573 case PFSmap: 574 case PFSmaps: 575 case PFScmdline: 576 vap->va_nlink = 1; 577 vap->va_uid = procp->p_ucred->cr_uid; 578 vap->va_gid = procp->p_ucred->cr_gid; 579 break; 580 case PFSmeminfo: 581 case PFScpuinfo: 582 case PFSuptime: 583 case PFSmounts: 584 vap->va_nlink = 1; 585 vap->va_uid = vap->va_gid = 0; 586 break; 587 588 default: 589 break; 590 } 591 592 /* 593 * now do the object specific fields 594 * 595 * The size could be set from struct reg, but it's hardly 596 * worth the trouble, and it puts some (potentially) machine 597 * dependent data into this machine-independent code. If it 598 * becomes important then this function should break out into 599 * a per-file stat function in the corresponding .c file. 600 */ 601 602 switch (pfs->pfs_type) { 603 case PFSroot: 604 /* 605 * Set nlink to 1 to tell fts(3) we don't actually know. 606 */ 607 vap->va_nlink = 1; 608 vap->va_uid = 0; 609 vap->va_gid = 0; 610 vap->va_bytes = vap->va_size = DEV_BSIZE; 611 break; 612 613 case PFScurproc: { 614 char buf[16]; /* should be enough */ 615 vap->va_nlink = 1; 616 vap->va_uid = 0; 617 vap->va_gid = 0; 618 vap->va_bytes = vap->va_size = 619 snprintf(buf, sizeof(buf), "%ld", (long)curproc->p_pid); 620 break; 621 } 622 623 case PFSself: 624 vap->va_nlink = 1; 625 vap->va_uid = 0; 626 vap->va_gid = 0; 627 vap->va_bytes = vap->va_size = sizeof("curproc") - 1; 628 break; 629 630 case PFSfd: 631 if (pfs->pfs_fd != -1) { 632 struct file *fp; 633 struct proc *pown; 634 635 if ((error = procfs_getfp(pfs, &pown, &fp)) != 0) 636 return error; 637 FILE_USE(fp); 638 vap->va_nlink = 1; 639 vap->va_uid = fp->f_cred->cr_uid; 640 vap->va_gid = fp->f_cred->cr_gid; 641 switch (fp->f_type) { 642 case DTYPE_VNODE: 643 vap->va_bytes = vap->va_size = 644 ((struct vnode *)fp->f_data)->v_size; 645 break; 646 default: 647 vap->va_bytes = vap->va_size = 0; 648 break; 649 } 650 FILE_UNUSE(fp, pown); 651 break; 652 } 653 /*FALLTHROUGH*/ 654 case PFSproc: 655 vap->va_nlink = 2; 656 vap->va_uid = procp->p_ucred->cr_uid; 657 vap->va_gid = procp->p_ucred->cr_gid; 658 vap->va_bytes = vap->va_size = DEV_BSIZE; 659 break; 660 661 case PFSfile: 662 error = EOPNOTSUPP; 663 break; 664 665 case PFSmem: 666 vap->va_bytes = vap->va_size = 667 ctob(procp->p_vmspace->vm_tsize + 668 procp->p_vmspace->vm_dsize + 669 procp->p_vmspace->vm_ssize); 670 break; 671 672 #if defined(PT_GETREGS) || defined(PT_SETREGS) 673 case PFSregs: 674 vap->va_bytes = vap->va_size = sizeof(struct reg); 675 break; 676 #endif 677 678 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS) 679 case PFSfpregs: 680 vap->va_bytes = vap->va_size = sizeof(struct fpreg); 681 break; 682 #endif 683 684 case PFSctl: 685 case PFSstatus: 686 case PFSstat: 687 case PFSnote: 688 case PFSnotepg: 689 case PFScmdline: 690 case PFSmeminfo: 691 case PFScpuinfo: 692 case PFSuptime: 693 case PFSmounts: 694 vap->va_bytes = vap->va_size = 0; 695 break; 696 case PFSmap: 697 case PFSmaps: 698 /* 699 * Advise a larger blocksize for the map files, so that 700 * they may be read in one pass. 701 */ 702 vap->va_blocksize = 4 * PAGE_SIZE; 703 vap->va_bytes = vap->va_size = 0; 704 break; 705 706 #ifdef __HAVE_PROCFS_MACHDEP 707 PROCFS_MACHDEP_NODETYPE_CASES 708 error = procfs_machdep_getattr(ap->a_vp, vap, procp); 709 break; 710 #endif 711 712 default: 713 panic("procfs_getattr"); 714 } 715 716 return (error); 717 } 718 719 /*ARGSUSED*/ 720 int 721 procfs_setattr(v) 722 void *v; 723 { 724 /* 725 * just fake out attribute setting 726 * it's not good to generate an error 727 * return, otherwise things like creat() 728 * will fail when they try to set the 729 * file length to 0. worse, this means 730 * that echo $note > /proc/$pid/note will fail. 731 */ 732 733 return (0); 734 } 735 736 /* 737 * implement access checking. 738 * 739 * actually, the check for super-user is slightly 740 * broken since it will allow read access to write-only 741 * objects. this doesn't cause any particular trouble 742 * but does mean that the i/o entry points need to check 743 * that the operation really does make sense. 744 */ 745 int 746 procfs_access(v) 747 void *v; 748 { 749 struct vop_access_args /* { 750 struct vnode *a_vp; 751 int a_mode; 752 struct ucred *a_cred; 753 struct proc *a_p; 754 } */ *ap = v; 755 struct vattr va; 756 int error; 757 758 if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_p)) != 0) 759 return (error); 760 761 return (vaccess(va.va_type, va.va_mode, 762 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred)); 763 } 764 765 /* 766 * lookup. this is incredibly complicated in the 767 * general case, however for most pseudo-filesystems 768 * very little needs to be done. 769 * 770 * Locking isn't hard here, just poorly documented. 771 * 772 * If we're looking up ".", just vref the parent & return it. 773 * 774 * If we're looking up "..", unlock the parent, and lock "..". If everything 775 * went ok, and we're on the last component and the caller requested the 776 * parent locked, try to re-lock the parent. We do this to prevent lock 777 * races. 778 * 779 * For anything else, get the needed node. Then unlock the parent if not 780 * the last component or not LOCKPARENT (i.e. if we wouldn't re-lock the 781 * parent in the .. case). 782 * 783 * We try to exit with the parent locked in error cases. 784 */ 785 int 786 procfs_lookup(v) 787 void *v; 788 { 789 struct vop_lookup_args /* { 790 struct vnode * a_dvp; 791 struct vnode ** a_vpp; 792 struct componentname * a_cnp; 793 } */ *ap = v; 794 struct componentname *cnp = ap->a_cnp; 795 struct vnode **vpp = ap->a_vpp; 796 struct vnode *dvp = ap->a_dvp; 797 const char *pname = cnp->cn_nameptr; 798 const struct proc_target *pt = NULL; 799 struct vnode *fvp; 800 pid_t pid; 801 struct pfsnode *pfs; 802 struct proc *p = NULL; 803 int i, error, wantpunlock, iscurproc = 0, isself = 0; 804 805 *vpp = NULL; 806 cnp->cn_flags &= ~PDIRUNLOCK; 807 808 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) 809 return (EROFS); 810 811 if (cnp->cn_namelen == 1 && *pname == '.') { 812 *vpp = dvp; 813 VREF(dvp); 814 return (0); 815 } 816 817 wantpunlock = (~cnp->cn_flags & (LOCKPARENT | ISLASTCN)); 818 pfs = VTOPFS(dvp); 819 switch (pfs->pfs_type) { 820 case PFSroot: 821 /* 822 * Shouldn't get here with .. in the root node. 823 */ 824 if (cnp->cn_flags & ISDOTDOT) 825 return (EIO); 826 827 iscurproc = CNEQ(cnp, "curproc", 7); 828 isself = CNEQ(cnp, "self", 4); 829 830 if (iscurproc || isself) { 831 error = procfs_allocvp(dvp->v_mount, vpp, 0, 832 iscurproc ? PFScurproc : PFSself, -1); 833 if ((error == 0) && (wantpunlock)) { 834 VOP_UNLOCK(dvp, 0); 835 cnp->cn_flags |= PDIRUNLOCK; 836 } 837 return (error); 838 } 839 840 for (i = 0; i < nproc_root_targets; i++) { 841 pt = &proc_root_targets[i]; 842 if (cnp->cn_namelen == pt->pt_namlen && 843 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 && 844 (pt->pt_valid == NULL || 845 (*pt->pt_valid)(p, dvp->v_mount))) 846 break; 847 } 848 849 if (i != nproc_root_targets) { 850 error = procfs_allocvp(dvp->v_mount, vpp, 0, 851 pt->pt_pfstype, -1); 852 if ((error == 0) && (wantpunlock)) { 853 VOP_UNLOCK(dvp, 0); 854 cnp->cn_flags |= PDIRUNLOCK; 855 } 856 return (error); 857 } 858 859 pid = (pid_t)atoi(pname, cnp->cn_namelen); 860 861 p = PFIND(pid); 862 if (p == NULL) 863 break; 864 865 error = procfs_allocvp(dvp->v_mount, vpp, pid, PFSproc, -1); 866 if ((error == 0) && (wantpunlock)) { 867 VOP_UNLOCK(dvp, 0); 868 cnp->cn_flags |= PDIRUNLOCK; 869 } 870 return (error); 871 872 case PFSproc: 873 /* 874 * do the .. dance. We unlock the directory, and then 875 * get the root dir. That will automatically return .. 876 * locked. Then if the caller wanted dvp locked, we 877 * re-lock. 878 */ 879 if (cnp->cn_flags & ISDOTDOT) { 880 VOP_UNLOCK(dvp, 0); 881 cnp->cn_flags |= PDIRUNLOCK; 882 error = procfs_root(dvp->v_mount, vpp); 883 if ((error == 0) && (wantpunlock == 0) && 884 ((error = vn_lock(dvp, LK_EXCLUSIVE)) == 0)) 885 cnp->cn_flags &= ~PDIRUNLOCK; 886 return (error); 887 } 888 889 p = PFIND(pfs->pfs_pid); 890 if (p == NULL) 891 break; 892 893 for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) { 894 if (cnp->cn_namelen == pt->pt_namlen && 895 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 && 896 (pt->pt_valid == NULL || 897 (*pt->pt_valid)(p, dvp->v_mount))) 898 goto found; 899 } 900 break; 901 902 found: 903 if (pt->pt_pfstype == PFSfile) { 904 fvp = p->p_textvp; 905 /* We already checked that it exists. */ 906 VREF(fvp); 907 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY); 908 if (wantpunlock) { 909 VOP_UNLOCK(dvp, 0); 910 cnp->cn_flags |= PDIRUNLOCK; 911 } 912 *vpp = fvp; 913 return (0); 914 } 915 916 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid, 917 pt->pt_pfstype, -1); 918 if ((error == 0) && (wantpunlock)) { 919 VOP_UNLOCK(dvp, 0); 920 cnp->cn_flags |= PDIRUNLOCK; 921 } 922 return (error); 923 924 case PFSfd: { 925 int fd; 926 struct file *fp; 927 /* 928 * do the .. dance. We unlock the directory, and then 929 * get the proc dir. That will automatically return .. 930 * locked. Then if the caller wanted dvp locked, we 931 * re-lock. 932 */ 933 if (cnp->cn_flags & ISDOTDOT) { 934 VOP_UNLOCK(dvp, 0); 935 cnp->cn_flags |= PDIRUNLOCK; 936 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid, 937 PFSproc, -1); 938 if ((error == 0) && (wantpunlock == 0) && 939 ((error = vn_lock(dvp, LK_EXCLUSIVE)) == 0)) 940 cnp->cn_flags &= ~PDIRUNLOCK; 941 return (error); 942 } 943 fd = atoi(pname, cnp->cn_namelen); 944 p = PFIND(pfs->pfs_pid); 945 if (p == NULL || (fp = fd_getfile(p->p_fd, fd)) == NULL) 946 return ENOENT; 947 FILE_USE(fp); 948 949 switch (fp->f_type) { 950 case DTYPE_VNODE: 951 fvp = (struct vnode *)fp->f_data; 952 953 /* Don't show directories */ 954 if (fvp->v_type == VDIR) 955 goto symlink; 956 957 VREF(fvp); 958 FILE_UNUSE(fp, p); 959 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY | 960 (p == curproc ? LK_CANRECURSE : 0)); 961 *vpp = fvp; 962 error = 0; 963 break; 964 default: 965 symlink: 966 FILE_UNUSE(fp, p); 967 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid, 968 PFSfd, fd); 969 break; 970 } 971 if ((error == 0) && (wantpunlock)) { 972 VOP_UNLOCK(dvp, 0); 973 cnp->cn_flags |= PDIRUNLOCK; 974 } 975 return error; 976 } 977 default: 978 return (ENOTDIR); 979 } 980 981 return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS); 982 } 983 984 int 985 procfs_validfile(p, mp) 986 struct proc *p; 987 struct mount *mp; 988 { 989 return (p->p_textvp != NULL); 990 } 991 992 static int 993 procfs_validfile_linux(p, mp) 994 struct proc *p; 995 struct mount *mp; 996 { 997 int flags; 998 999 flags = VFSTOPROC(mp)->pmnt_flags; 1000 return ((flags & PROCFSMNT_LINUXCOMPAT) && 1001 (p == NULL || procfs_validfile(p, mp))); 1002 } 1003 1004 struct procfs_root_readdir_ctx { 1005 struct uio *uiop; 1006 off_t *cookies; 1007 int ncookies; 1008 off_t off; 1009 off_t startoff; 1010 int error; 1011 }; 1012 1013 static int 1014 procfs_root_readdir_callback(struct proc *p, void *arg) 1015 { 1016 struct procfs_root_readdir_ctx *ctxp = arg; 1017 struct dirent d; 1018 struct uio *uiop; 1019 int error; 1020 1021 uiop = ctxp->uiop; 1022 if (uiop->uio_resid < UIO_MX) 1023 return -1; /* no space */ 1024 1025 if (ctxp->off < ctxp->startoff) { 1026 ctxp->off++; 1027 return 0; 1028 } 1029 1030 memset(&d, 0, UIO_MX); 1031 d.d_reclen = UIO_MX; 1032 d.d_fileno = PROCFS_FILENO(p->p_pid, PFSproc, -1); 1033 d.d_namlen = snprintf(d.d_name, 1034 UIO_MX - offsetof(struct dirent, d_name), "%ld", (long)p->p_pid); 1035 d.d_type = DT_DIR; 1036 1037 proclist_unlock_read(); 1038 error = uiomove(&d, UIO_MX, uiop); 1039 proclist_lock_read(); 1040 if (error) { 1041 ctxp->error = error; 1042 return -1; 1043 } 1044 1045 ctxp->ncookies++; 1046 if (ctxp->cookies) 1047 *(ctxp->cookies)++ = ctxp->off + 1; 1048 ctxp->off++; 1049 1050 return 0; 1051 } 1052 1053 /* 1054 * readdir returns directory entries from pfsnode (vp). 1055 * 1056 * the strategy here with procfs is to generate a single 1057 * directory entry at a time (struct dirent) and then 1058 * copy that out to userland using uiomove. a more efficent 1059 * though more complex implementation, would try to minimize 1060 * the number of calls to uiomove(). for procfs, this is 1061 * hardly worth the added code complexity. 1062 * 1063 * this should just be done through read() 1064 */ 1065 int 1066 procfs_readdir(v) 1067 void *v; 1068 { 1069 struct vop_readdir_args /* { 1070 struct vnode *a_vp; 1071 struct uio *a_uio; 1072 struct ucred *a_cred; 1073 int *a_eofflag; 1074 off_t **a_cookies; 1075 int *a_ncookies; 1076 } */ *ap = v; 1077 struct uio *uio = ap->a_uio; 1078 struct dirent d; 1079 struct pfsnode *pfs; 1080 off_t i; 1081 int error; 1082 off_t *cookies = NULL; 1083 int ncookies; 1084 struct vnode *vp; 1085 const struct proc_target *pt; 1086 struct procfs_root_readdir_ctx ctx; 1087 1088 vp = ap->a_vp; 1089 pfs = VTOPFS(vp); 1090 1091 if (uio->uio_resid < UIO_MX) 1092 return (EINVAL); 1093 if (uio->uio_offset < 0) 1094 return (EINVAL); 1095 1096 error = 0; 1097 i = uio->uio_offset; 1098 memset(&d, 0, UIO_MX); 1099 d.d_reclen = UIO_MX; 1100 ncookies = uio->uio_resid / UIO_MX; 1101 1102 switch (pfs->pfs_type) { 1103 /* 1104 * this is for the process-specific sub-directories. 1105 * all that is needed to is copy out all the entries 1106 * from the procent[] table (top of this file). 1107 */ 1108 case PFSproc: { 1109 struct proc *p; 1110 1111 if (i >= nproc_targets) 1112 return 0; 1113 1114 p = PFIND(pfs->pfs_pid); 1115 if (p == NULL) 1116 break; 1117 1118 if (ap->a_ncookies) { 1119 ncookies = min(ncookies, (nproc_targets - i)); 1120 cookies = malloc(ncookies * sizeof (off_t), 1121 M_TEMP, M_WAITOK); 1122 *ap->a_cookies = cookies; 1123 } 1124 1125 for (pt = &proc_targets[i]; 1126 uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) { 1127 if (pt->pt_valid && 1128 (*pt->pt_valid)(p, vp->v_mount) == 0) 1129 continue; 1130 1131 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, 1132 pt->pt_pfstype, -1); 1133 d.d_namlen = pt->pt_namlen; 1134 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1); 1135 d.d_type = pt->pt_type; 1136 1137 if ((error = uiomove(&d, UIO_MX, uio)) != 0) 1138 break; 1139 if (cookies) 1140 *cookies++ = i + 1; 1141 } 1142 1143 break; 1144 } 1145 case PFSfd: { 1146 struct proc *p; 1147 struct filedesc *fdp; 1148 struct file *fp; 1149 int lim, nc = 0; 1150 1151 p = PFIND(pfs->pfs_pid); 1152 if (p == NULL) 1153 return ESRCH; 1154 1155 fdp = p->p_fd; 1156 1157 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles); 1158 if (i >= lim) 1159 return 0; 1160 1161 if (ap->a_ncookies) { 1162 ncookies = min(ncookies, (fdp->fd_nfiles + 2 - i)); 1163 cookies = malloc(ncookies * sizeof (off_t), 1164 M_TEMP, M_WAITOK); 1165 *ap->a_cookies = cookies; 1166 } 1167 1168 for (; i < 2 && uio->uio_resid >= UIO_MX; i++) { 1169 pt = &proc_targets[i]; 1170 d.d_namlen = pt->pt_namlen; 1171 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, 1172 pt->pt_pfstype, -1); 1173 (void)memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1); 1174 d.d_type = pt->pt_type; 1175 if ((error = uiomove(&d, UIO_MX, uio)) != 0) 1176 break; 1177 if (cookies) 1178 *cookies++ = i + 1; 1179 nc++; 1180 } 1181 if (error) { 1182 ncookies = nc; 1183 break; 1184 } 1185 for (; uio->uio_resid >= UIO_MX && i < fdp->fd_nfiles; i++) { 1186 /* check the descriptor exists */ 1187 if ((fp = fd_getfile(fdp, i - 2)) == NULL) 1188 continue; 1189 simple_unlock(&fp->f_slock); 1190 1191 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, PFSfd, i - 2); 1192 d.d_namlen = snprintf(d.d_name, sizeof(d.d_name), 1193 "%lld", (long long)(i - 2)); 1194 d.d_type = VREG; 1195 if ((error = uiomove(&d, UIO_MX, uio)) != 0) 1196 break; 1197 if (cookies) 1198 *cookies++ = i + 1; 1199 nc++; 1200 } 1201 ncookies = nc; 1202 break; 1203 } 1204 1205 /* 1206 * this is for the root of the procfs filesystem 1207 * what is needed are special entries for "curproc" 1208 * and "self" followed by an entry for each process 1209 * on allproc. 1210 */ 1211 1212 case PFSroot: { 1213 int nc = 0; 1214 1215 if (ap->a_ncookies) { 1216 /* 1217 * XXX Potentially allocating too much space here, 1218 * but I'm lazy. This loop needs some work. 1219 */ 1220 cookies = malloc(ncookies * sizeof (off_t), 1221 M_TEMP, M_WAITOK); 1222 *ap->a_cookies = cookies; 1223 } 1224 error = 0; 1225 /* 0 ... 3 are static entries. */ 1226 for (; i <= 3 && uio->uio_resid >= UIO_MX; i++) { 1227 switch (i) { 1228 case 0: /* `.' */ 1229 case 1: /* `..' */ 1230 d.d_fileno = PROCFS_FILENO(0, PFSroot, -1); 1231 d.d_namlen = i + 1; 1232 memcpy(d.d_name, "..", d.d_namlen); 1233 d.d_name[i + 1] = '\0'; 1234 d.d_type = DT_DIR; 1235 break; 1236 1237 case 2: 1238 d.d_fileno = PROCFS_FILENO(0, PFScurproc, -1); 1239 d.d_namlen = sizeof("curproc") - 1; 1240 memcpy(d.d_name, "curproc", sizeof("curproc")); 1241 d.d_type = DT_LNK; 1242 break; 1243 1244 case 3: 1245 d.d_fileno = PROCFS_FILENO(0, PFSself, -1); 1246 d.d_namlen = sizeof("self") - 1; 1247 memcpy(d.d_name, "self", sizeof("self")); 1248 d.d_type = DT_LNK; 1249 break; 1250 } 1251 1252 if ((error = uiomove(&d, UIO_MX, uio)) != 0) 1253 break; 1254 nc++; 1255 if (cookies) 1256 *cookies++ = i + 1; 1257 } 1258 /* 4 ... are process entries. */ 1259 ctx.uiop = uio; 1260 ctx.error = 0; 1261 ctx.off = 4; 1262 ctx.startoff = i; 1263 ctx.cookies = cookies; 1264 ctx.ncookies = nc; 1265 proclist_foreach_call(&allproc, 1266 procfs_root_readdir_callback, &ctx); 1267 cookies = ctx.cookies; 1268 nc = ctx.ncookies; 1269 error = ctx.error; 1270 if (error) 1271 break; 1272 1273 /* misc entries. */ 1274 if (i < ctx.off) 1275 i = ctx.off; 1276 if (i >= ctx.off + nproc_root_targets) 1277 break; 1278 for (pt = &proc_root_targets[i - ctx.off]; 1279 uio->uio_resid >= UIO_MX && 1280 pt < &proc_root_targets[nproc_root_targets]; 1281 pt++, i++) { 1282 if (pt->pt_valid && 1283 (*pt->pt_valid)(NULL, vp->v_mount) == 0) 1284 continue; 1285 d.d_fileno = PROCFS_FILENO(0, pt->pt_pfstype, -1); 1286 d.d_namlen = pt->pt_namlen; 1287 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1); 1288 d.d_type = pt->pt_type; 1289 1290 if ((error = uiomove(&d, UIO_MX, uio)) != 0) 1291 break; 1292 nc++; 1293 if (cookies) 1294 *cookies++ = i + 1; 1295 } 1296 1297 ncookies = nc; 1298 break; 1299 } 1300 1301 default: 1302 error = ENOTDIR; 1303 break; 1304 } 1305 1306 if (ap->a_ncookies) { 1307 if (error) { 1308 if (cookies) 1309 free(*ap->a_cookies, M_TEMP); 1310 *ap->a_ncookies = 0; 1311 *ap->a_cookies = NULL; 1312 } else 1313 *ap->a_ncookies = ncookies; 1314 } 1315 uio->uio_offset = i; 1316 return (error); 1317 } 1318 1319 /* 1320 * readlink reads the link of `curproc' 1321 */ 1322 int 1323 procfs_readlink(v) 1324 void *v; 1325 { 1326 struct vop_readlink_args *ap = v; 1327 char buf[16]; /* should be enough */ 1328 char *bp = buf; 1329 char *path = NULL; 1330 int len; 1331 int error = 0; 1332 struct pfsnode *pfs = VTOPFS(ap->a_vp); 1333 1334 if (pfs->pfs_fileno == PROCFS_FILENO(0, PFScurproc, -1)) 1335 len = snprintf(buf, sizeof(buf), "%ld", (long)curproc->p_pid); 1336 else if (pfs->pfs_fileno == PROCFS_FILENO(0, PFSself, -1)) 1337 len = snprintf(buf, sizeof(buf), "%s", "curproc"); 1338 else { 1339 struct file *fp; 1340 struct proc *pown; 1341 struct vnode *vxp, *vp; 1342 1343 if ((error = procfs_getfp(pfs, &pown, &fp)) != 0) 1344 return error; 1345 FILE_USE(fp); 1346 switch (fp->f_type) { 1347 case DTYPE_VNODE: 1348 vxp = (struct vnode *)fp->f_data; 1349 if (vxp->v_type != VDIR) { 1350 FILE_UNUSE(fp, pown); 1351 return EINVAL; 1352 } 1353 if ((path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK)) 1354 == NULL) { 1355 FILE_UNUSE(fp, pown); 1356 return ENOMEM; 1357 } 1358 bp = path + MAXPATHLEN; 1359 *--bp = '\0'; 1360 vp = curproc->p_cwdi->cwdi_rdir; 1361 if (vp == NULL) 1362 vp = rootvnode; 1363 error = getcwd_common(vxp, vp, &bp, path, 1364 MAXPATHLEN / 2, 0, curproc); 1365 FILE_UNUSE(fp, pown); 1366 if (error) { 1367 free(path, M_TEMP); 1368 return error; 1369 } 1370 len = strlen(bp); 1371 break; 1372 1373 case DTYPE_MISC: 1374 len = snprintf(buf, sizeof(buf), "%s", "[misc]"); 1375 break; 1376 1377 case DTYPE_KQUEUE: 1378 len = snprintf(buf, sizeof(buf), "%s", "[kqueue]"); 1379 break; 1380 1381 default: 1382 return EINVAL; 1383 } 1384 } 1385 1386 error = uiomove(bp, len, ap->a_uio); 1387 if (path) 1388 free(path, M_TEMP); 1389 return error; 1390 } 1391 1392 /* 1393 * convert decimal ascii to int 1394 */ 1395 static int 1396 atoi(b, len) 1397 const char *b; 1398 size_t len; 1399 { 1400 int p = 0; 1401 1402 while (len--) { 1403 char c = *b++; 1404 if (c < '0' || c > '9') 1405 return -1; 1406 p = 10 * p + (c - '0'); 1407 } 1408 1409 return p; 1410 } 1411