procfs_vnops.c revision 1.106.2.8 1 /* $NetBSD: procfs_vnops.c,v 1.106.2.8 2004/10/27 06:48:24 skrll 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.106.2.8 2004/10/27 06:48:24 skrll 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 lwp *, 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 lwp *, 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 lwp *a_l;
281 } */ *ap = v;
282 struct pfsnode *pfs = VTOPFS(ap->a_vp);
283 struct lwp *l1;
284 struct proc *p2;
285 int error;
286
287 l1 = ap->a_l; /* tracer */
288 p2 = PFIND(pfs->pfs_pid); /* traced */
289
290 if (p2 == NULL)
291 return (ENOENT); /* was ESRCH, jsp */
292
293 switch (pfs->pfs_type) {
294 case PFSmem:
295 if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) ||
296 ((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE)))
297 return (EBUSY);
298
299 if ((error = process_checkioperm(l1, p2)) != 0)
300 return (error);
301
302 if (ap->a_mode & FWRITE)
303 pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
304
305 return (0);
306
307 default:
308 break;
309 }
310
311 return (0);
312 }
313
314 /*
315 * close the pfsnode (vp) after doing i/o.
316 * (vp) is not locked on entry or exit.
317 *
318 * nothing to do for procfs other than undo
319 * any exclusive open flag (see _open above).
320 */
321 int
322 procfs_close(v)
323 void *v;
324 {
325 struct vop_close_args /* {
326 struct vnode *a_vp;
327 int a_fflag;
328 struct ucred *a_cred;
329 struct lwp *a_l;
330 } */ *ap = v;
331 struct pfsnode *pfs = VTOPFS(ap->a_vp);
332
333 switch (pfs->pfs_type) {
334 case PFSmem:
335 if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
336 pfs->pfs_flags &= ~(FWRITE|O_EXCL);
337 break;
338
339 default:
340 break;
341 }
342
343 return (0);
344 }
345
346 /*
347 * _inactive is called when the pfsnode
348 * is vrele'd and the reference count goes
349 * to zero. (vp) will be on the vnode free
350 * list, so to get it back vget() must be
351 * used.
352 *
353 * for procfs, check if the process is still
354 * alive and if it isn't then just throw away
355 * the vnode by calling vgone(). this may
356 * be overkill and a waste of time since the
357 * chances are that the process will still be
358 * there and PFIND is not free.
359 *
360 * (vp) is locked on entry, but must be unlocked on exit.
361 */
362 int
363 procfs_inactive(v)
364 void *v;
365 {
366 struct vop_inactive_args /* {
367 struct vnode *a_vp;
368 struct proc *a_p;
369 } */ *ap = v;
370 struct pfsnode *pfs = VTOPFS(ap->a_vp);
371
372 VOP_UNLOCK(ap->a_vp, 0);
373 if (PFIND(pfs->pfs_pid) == NULL)
374 vgone(ap->a_vp);
375
376 return (0);
377 }
378
379 /*
380 * _reclaim is called when getnewvnode()
381 * wants to make use of an entry on the vnode
382 * free list. at this time the filesystem needs
383 * to free any private data and remove the node
384 * from any private lists.
385 */
386 int
387 procfs_reclaim(v)
388 void *v;
389 {
390 struct vop_reclaim_args /* {
391 struct vnode *a_vp;
392 } */ *ap = v;
393
394 return (procfs_freevp(ap->a_vp));
395 }
396
397 /*
398 * Return POSIX pathconf information applicable to special devices.
399 */
400 int
401 procfs_pathconf(v)
402 void *v;
403 {
404 struct vop_pathconf_args /* {
405 struct vnode *a_vp;
406 int a_name;
407 register_t *a_retval;
408 } */ *ap = v;
409
410 switch (ap->a_name) {
411 case _PC_LINK_MAX:
412 *ap->a_retval = LINK_MAX;
413 return (0);
414 case _PC_MAX_CANON:
415 *ap->a_retval = MAX_CANON;
416 return (0);
417 case _PC_MAX_INPUT:
418 *ap->a_retval = MAX_INPUT;
419 return (0);
420 case _PC_PIPE_BUF:
421 *ap->a_retval = PIPE_BUF;
422 return (0);
423 case _PC_CHOWN_RESTRICTED:
424 *ap->a_retval = 1;
425 return (0);
426 case _PC_VDISABLE:
427 *ap->a_retval = _POSIX_VDISABLE;
428 return (0);
429 case _PC_SYNC_IO:
430 *ap->a_retval = 1;
431 return (0);
432 default:
433 return (EINVAL);
434 }
435 /* NOTREACHED */
436 }
437
438 /*
439 * _print is used for debugging.
440 * just print a readable description
441 * of (vp).
442 */
443 int
444 procfs_print(v)
445 void *v;
446 {
447 struct vop_print_args /* {
448 struct vnode *a_vp;
449 } */ *ap = v;
450 struct pfsnode *pfs = VTOPFS(ap->a_vp);
451
452 printf("tag VT_PROCFS, type %d, pid %d, mode %x, flags %lx\n",
453 pfs->pfs_type, pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
454 return 0;
455 }
456
457 int
458 procfs_link(v)
459 void *v;
460 {
461 struct vop_link_args /* {
462 struct vnode *a_dvp;
463 struct vnode *a_vp;
464 struct componentname *a_cnp;
465 } */ *ap = v;
466
467 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
468 vput(ap->a_dvp);
469 return (EROFS);
470 }
471
472 int
473 procfs_symlink(v)
474 void *v;
475 {
476 struct vop_symlink_args /* {
477 struct vnode *a_dvp;
478 struct vnode **a_vpp;
479 struct componentname *a_cnp;
480 struct vattr *a_vap;
481 char *a_target;
482 } */ *ap = v;
483
484 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
485 vput(ap->a_dvp);
486 return (EROFS);
487 }
488
489 /*
490 * Invent attributes for pfsnode (vp) and store
491 * them in (vap).
492 * Directories lengths are returned as zero since
493 * any real length would require the genuine size
494 * to be computed, and nothing cares anyway.
495 *
496 * this is relatively minimal for procfs.
497 */
498 int
499 procfs_getattr(v)
500 void *v;
501 {
502 struct vop_getattr_args /* {
503 struct vnode *a_vp;
504 struct vattr *a_vap;
505 struct ucred *a_cred;
506 struct lwp *a_l;
507 } */ *ap = v;
508 struct pfsnode *pfs = VTOPFS(ap->a_vp);
509 struct vattr *vap = ap->a_vap;
510 struct proc *procp;
511 int error;
512
513 /* first check the process still exists */
514 switch (pfs->pfs_type) {
515 case PFSroot:
516 case PFScurproc:
517 case PFSself:
518 procp = 0;
519 break;
520
521 default:
522 procp = PFIND(pfs->pfs_pid);
523 if (procp == NULL)
524 return (ENOENT);
525 break;
526 }
527
528 error = 0;
529
530 /* start by zeroing out the attributes */
531 VATTR_NULL(vap);
532
533 /* next do all the common fields */
534 vap->va_type = ap->a_vp->v_type;
535 vap->va_mode = pfs->pfs_mode;
536 vap->va_fileid = pfs->pfs_fileno;
537 vap->va_flags = 0;
538 vap->va_blocksize = PAGE_SIZE;
539
540 /*
541 * Make all times be current TOD. Avoid microtime(9), it's slow.
542 * We don't guard the read from time(9) with splclock(9) since we
543 * don't actually need to be THAT sure the access is atomic.
544 *
545 * It would be possible to get the process start
546 * time from the p_stat structure, but there's
547 * no "file creation" time stamp anyway, and the
548 * p_stat structure is not addressible if u. gets
549 * swapped out for that process.
550 */
551 TIMEVAL_TO_TIMESPEC(&time, &vap->va_ctime);
552 vap->va_atime = vap->va_mtime = vap->va_ctime;
553
554 switch (pfs->pfs_type) {
555 case PFSmem:
556 case PFSregs:
557 case PFSfpregs:
558 #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
559 PROCFS_MACHDEP_PROTECT_CASES
560 #endif
561 /*
562 * If the process has exercised some setuid or setgid
563 * privilege, then rip away read/write permission so
564 * that only root can gain access.
565 */
566 if (procp->p_flag & P_SUGID)
567 vap->va_mode &= ~(S_IRUSR|S_IWUSR);
568 /* FALLTHROUGH */
569 case PFSctl:
570 case PFSstatus:
571 case PFSstat:
572 case PFSnote:
573 case PFSnotepg:
574 case PFSmap:
575 case PFSmaps:
576 case PFScmdline:
577 vap->va_nlink = 1;
578 vap->va_uid = procp->p_ucred->cr_uid;
579 vap->va_gid = procp->p_ucred->cr_gid;
580 break;
581 case PFSmeminfo:
582 case PFScpuinfo:
583 case PFSuptime:
584 case PFSmounts:
585 vap->va_nlink = 1;
586 vap->va_uid = vap->va_gid = 0;
587 break;
588
589 default:
590 break;
591 }
592
593 /*
594 * now do the object specific fields
595 *
596 * The size could be set from struct reg, but it's hardly
597 * worth the trouble, and it puts some (potentially) machine
598 * dependent data into this machine-independent code. If it
599 * becomes important then this function should break out into
600 * a per-file stat function in the corresponding .c file.
601 */
602
603 switch (pfs->pfs_type) {
604 case PFSroot:
605 /*
606 * Set nlink to 1 to tell fts(3) we don't actually know.
607 */
608 vap->va_nlink = 1;
609 vap->va_uid = 0;
610 vap->va_gid = 0;
611 vap->va_bytes = vap->va_size = DEV_BSIZE;
612 break;
613
614 case PFScurproc: {
615 char buf[16]; /* should be enough */
616 vap->va_nlink = 1;
617 vap->va_uid = 0;
618 vap->va_gid = 0;
619 vap->va_bytes = vap->va_size =
620 snprintf(buf, sizeof(buf), "%ld", (long)curproc->p_pid);
621 break;
622 }
623
624 case PFSself:
625 vap->va_nlink = 1;
626 vap->va_uid = 0;
627 vap->va_gid = 0;
628 vap->va_bytes = vap->va_size = sizeof("curproc") - 1;
629 break;
630
631 case PFSfd:
632 if (pfs->pfs_fd != -1) {
633 struct file *fp;
634 struct proc *pown;
635
636 if ((error = procfs_getfp(pfs, &pown, &fp)) != 0)
637 return error;
638 FILE_USE(fp);
639 vap->va_nlink = 1;
640 vap->va_uid = fp->f_cred->cr_uid;
641 vap->va_gid = fp->f_cred->cr_gid;
642 switch (fp->f_type) {
643 case DTYPE_VNODE:
644 vap->va_bytes = vap->va_size =
645 ((struct vnode *)fp->f_data)->v_size;
646 break;
647 default:
648 vap->va_bytes = vap->va_size = 0;
649 break;
650 }
651 FILE_UNUSE(fp, proc_representative_lwp(pown));
652 break;
653 }
654 /*FALLTHROUGH*/
655 case PFSproc:
656 vap->va_nlink = 2;
657 vap->va_uid = procp->p_ucred->cr_uid;
658 vap->va_gid = procp->p_ucred->cr_gid;
659 vap->va_bytes = vap->va_size = DEV_BSIZE;
660 break;
661
662 case PFSfile:
663 error = EOPNOTSUPP;
664 break;
665
666 case PFSmem:
667 vap->va_bytes = vap->va_size =
668 ctob(procp->p_vmspace->vm_tsize +
669 procp->p_vmspace->vm_dsize +
670 procp->p_vmspace->vm_ssize);
671 break;
672
673 #if defined(PT_GETREGS) || defined(PT_SETREGS)
674 case PFSregs:
675 vap->va_bytes = vap->va_size = sizeof(struct reg);
676 break;
677 #endif
678
679 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
680 case PFSfpregs:
681 vap->va_bytes = vap->va_size = sizeof(struct fpreg);
682 break;
683 #endif
684
685 case PFSctl:
686 case PFSstatus:
687 case PFSstat:
688 case PFSnote:
689 case PFSnotepg:
690 case PFScmdline:
691 case PFSmeminfo:
692 case PFScpuinfo:
693 case PFSuptime:
694 case PFSmounts:
695 vap->va_bytes = vap->va_size = 0;
696 break;
697 case PFSmap:
698 case PFSmaps:
699 /*
700 * Advise a larger blocksize for the map files, so that
701 * they may be read in one pass.
702 */
703 vap->va_blocksize = 4 * PAGE_SIZE;
704 vap->va_bytes = vap->va_size = 0;
705 break;
706
707 #ifdef __HAVE_PROCFS_MACHDEP
708 PROCFS_MACHDEP_NODETYPE_CASES
709 error = procfs_machdep_getattr(ap->a_vp, vap, procp);
710 break;
711 #endif
712
713 default:
714 panic("procfs_getattr");
715 }
716
717 return (error);
718 }
719
720 /*ARGSUSED*/
721 int
722 procfs_setattr(v)
723 void *v;
724 {
725 /*
726 * just fake out attribute setting
727 * it's not good to generate an error
728 * return, otherwise things like creat()
729 * will fail when they try to set the
730 * file length to 0. worse, this means
731 * that echo $note > /proc/$pid/note will fail.
732 */
733
734 return (0);
735 }
736
737 /*
738 * implement access checking.
739 *
740 * actually, the check for super-user is slightly
741 * broken since it will allow read access to write-only
742 * objects. this doesn't cause any particular trouble
743 * but does mean that the i/o entry points need to check
744 * that the operation really does make sense.
745 */
746 int
747 procfs_access(v)
748 void *v;
749 {
750 struct vop_access_args /* {
751 struct vnode *a_vp;
752 int a_mode;
753 struct ucred *a_cred;
754 struct lwp *a_l;
755 } */ *ap = v;
756 struct vattr va;
757 int error;
758
759 if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_l)) != 0)
760 return (error);
761
762 return (vaccess(va.va_type, va.va_mode,
763 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
764 }
765
766 /*
767 * lookup. this is incredibly complicated in the
768 * general case, however for most pseudo-filesystems
769 * very little needs to be done.
770 *
771 * Locking isn't hard here, just poorly documented.
772 *
773 * If we're looking up ".", just vref the parent & return it.
774 *
775 * If we're looking up "..", unlock the parent, and lock "..". If everything
776 * went ok, and we're on the last component and the caller requested the
777 * parent locked, try to re-lock the parent. We do this to prevent lock
778 * races.
779 *
780 * For anything else, get the needed node. Then unlock the parent if not
781 * the last component or not LOCKPARENT (i.e. if we wouldn't re-lock the
782 * parent in the .. case).
783 *
784 * We try to exit with the parent locked in error cases.
785 */
786 int
787 procfs_lookup(v)
788 void *v;
789 {
790 struct vop_lookup_args /* {
791 struct vnode * a_dvp;
792 struct vnode ** a_vpp;
793 struct componentname * a_cnp;
794 } */ *ap = v;
795 struct componentname *cnp = ap->a_cnp;
796 struct vnode **vpp = ap->a_vpp;
797 struct vnode *dvp = ap->a_dvp;
798 const char *pname = cnp->cn_nameptr;
799 const struct proc_target *pt = NULL;
800 struct vnode *fvp;
801 pid_t pid;
802 struct pfsnode *pfs;
803 struct proc *p = NULL;
804 struct lwp *l = NULL;
805 int i, error, wantpunlock, iscurproc = 0, isself = 0;
806
807 *vpp = NULL;
808 cnp->cn_flags &= ~PDIRUNLOCK;
809
810 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
811 return (EROFS);
812
813 if (cnp->cn_namelen == 1 && *pname == '.') {
814 *vpp = dvp;
815 VREF(dvp);
816 return (0);
817 }
818
819 wantpunlock = (~cnp->cn_flags & (LOCKPARENT | ISLASTCN));
820 pfs = VTOPFS(dvp);
821 switch (pfs->pfs_type) {
822 case PFSroot:
823 /*
824 * Shouldn't get here with .. in the root node.
825 */
826 if (cnp->cn_flags & ISDOTDOT)
827 return (EIO);
828
829 iscurproc = CNEQ(cnp, "curproc", 7);
830 isself = CNEQ(cnp, "self", 4);
831
832 if (iscurproc || isself) {
833 error = procfs_allocvp(dvp->v_mount, vpp, 0,
834 iscurproc ? PFScurproc : PFSself, -1);
835 if ((error == 0) && (wantpunlock)) {
836 VOP_UNLOCK(dvp, 0);
837 cnp->cn_flags |= PDIRUNLOCK;
838 }
839 return (error);
840 }
841
842 for (i = 0; i < nproc_root_targets; i++) {
843 pt = &proc_root_targets[i];
844 if (cnp->cn_namelen == pt->pt_namlen &&
845 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
846 (pt->pt_valid == NULL ||
847 (*pt->pt_valid)(cnp->cn_lwp, dvp->v_mount)))
848 break;
849 }
850
851 if (i != nproc_root_targets) {
852 error = procfs_allocvp(dvp->v_mount, vpp, 0,
853 pt->pt_pfstype, -1);
854 if ((error == 0) && (wantpunlock)) {
855 VOP_UNLOCK(dvp, 0);
856 cnp->cn_flags |= PDIRUNLOCK;
857 }
858 return (error);
859 }
860
861 pid = (pid_t)atoi(pname, cnp->cn_namelen);
862
863 p = PFIND(pid);
864 if (p == NULL)
865 break;
866
867 error = procfs_allocvp(dvp->v_mount, vpp, pid, PFSproc, -1);
868 if ((error == 0) && (wantpunlock)) {
869 VOP_UNLOCK(dvp, 0);
870 cnp->cn_flags |= PDIRUNLOCK;
871 }
872 return (error);
873
874 case PFSproc:
875 /*
876 * do the .. dance. We unlock the directory, and then
877 * get the root dir. That will automatically return ..
878 * locked. Then if the caller wanted dvp locked, we
879 * re-lock.
880 */
881 if (cnp->cn_flags & ISDOTDOT) {
882 VOP_UNLOCK(dvp, 0);
883 cnp->cn_flags |= PDIRUNLOCK;
884 error = procfs_root(dvp->v_mount, vpp);
885 if ((error == 0) && (wantpunlock == 0) &&
886 ((error = vn_lock(dvp, LK_EXCLUSIVE)) == 0))
887 cnp->cn_flags &= ~PDIRUNLOCK;
888 return (error);
889 }
890
891 p = PFIND(pfs->pfs_pid);
892 if (p == NULL)
893 break;
894 l = proc_representative_lwp(p);
895
896 for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
897 if (cnp->cn_namelen == pt->pt_namlen &&
898 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
899 (pt->pt_valid == NULL ||
900 (*pt->pt_valid)(cnp->cn_lwp, dvp->v_mount)))
901 goto found;
902 }
903 break;
904
905 found:
906 if (pt->pt_pfstype == PFSfile) {
907 fvp = p->p_textvp;
908 /* We already checked that it exists. */
909 VREF(fvp);
910 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY);
911 if (wantpunlock) {
912 VOP_UNLOCK(dvp, 0);
913 cnp->cn_flags |= PDIRUNLOCK;
914 }
915 *vpp = fvp;
916 return (0);
917 }
918
919 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
920 pt->pt_pfstype, -1);
921 if ((error == 0) && (wantpunlock)) {
922 VOP_UNLOCK(dvp, 0);
923 cnp->cn_flags |= PDIRUNLOCK;
924 }
925 return (error);
926
927 case PFSfd: {
928 int fd;
929 struct file *fp;
930 /*
931 * do the .. dance. We unlock the directory, and then
932 * get the proc dir. That will automatically return ..
933 * locked. Then if the caller wanted dvp locked, we
934 * re-lock.
935 */
936 if (cnp->cn_flags & ISDOTDOT) {
937 VOP_UNLOCK(dvp, 0);
938 cnp->cn_flags |= PDIRUNLOCK;
939 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
940 PFSproc, -1);
941 if ((error == 0) && (wantpunlock == 0) &&
942 ((error = vn_lock(dvp, LK_EXCLUSIVE)) == 0))
943 cnp->cn_flags &= ~PDIRUNLOCK;
944 return (error);
945 }
946 fd = atoi(pname, cnp->cn_namelen);
947 p = PFIND(pfs->pfs_pid);
948 if (p == NULL || (fp = fd_getfile(p->p_fd, fd)) == NULL)
949 return ENOENT;
950 FILE_USE(fp);
951
952 switch (fp->f_type) {
953 case DTYPE_VNODE:
954 fvp = (struct vnode *)fp->f_data;
955
956 /* Don't show directories */
957 if (fvp->v_type == VDIR)
958 goto symlink;
959
960 VREF(fvp);
961 FILE_UNUSE(fp, l);
962 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY |
963 (p == curproc ? LK_CANRECURSE : 0));
964 *vpp = fvp;
965 error = 0;
966 break;
967 default:
968 symlink:
969 FILE_UNUSE(fp, l);
970 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
971 PFSfd, fd);
972 break;
973 }
974 if ((error == 0) && (wantpunlock)) {
975 VOP_UNLOCK(dvp, 0);
976 cnp->cn_flags |= PDIRUNLOCK;
977 }
978 return error;
979 }
980 default:
981 return (ENOTDIR);
982 }
983
984 return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
985 }
986
987 int
988 procfs_validfile(l, mp)
989 struct lwp *l;
990 struct mount *mp;
991 {
992 return (l->l_proc->p_textvp != NULL);
993 }
994
995 static int
996 procfs_validfile_linux(l, mp)
997 struct lwp *l;
998 struct mount *mp;
999 {
1000 int flags;
1001
1002 flags = VFSTOPROC(mp)->pmnt_flags;
1003 return ((flags & PROCFSMNT_LINUXCOMPAT) &&
1004 (l == NULL || l->l_proc == NULL || procfs_validfile(l, mp)));
1005 }
1006
1007 struct procfs_root_readdir_ctx {
1008 struct uio *uiop;
1009 off_t *cookies;
1010 int ncookies;
1011 off_t off;
1012 off_t startoff;
1013 int error;
1014 };
1015
1016 static int
1017 procfs_root_readdir_callback(struct proc *p, void *arg)
1018 {
1019 struct procfs_root_readdir_ctx *ctxp = arg;
1020 struct dirent d;
1021 struct uio *uiop;
1022 int error;
1023
1024 uiop = ctxp->uiop;
1025 if (uiop->uio_resid < UIO_MX)
1026 return -1; /* no space */
1027
1028 if (ctxp->off < ctxp->startoff) {
1029 ctxp->off++;
1030 return 0;
1031 }
1032
1033 memset(&d, 0, UIO_MX);
1034 d.d_reclen = UIO_MX;
1035 d.d_fileno = PROCFS_FILENO(p->p_pid, PFSproc, -1);
1036 d.d_namlen = snprintf(d.d_name,
1037 UIO_MX - offsetof(struct dirent, d_name), "%ld", (long)p->p_pid);
1038 d.d_type = DT_DIR;
1039
1040 proclist_unlock_read();
1041 error = uiomove(&d, UIO_MX, uiop);
1042 proclist_lock_read();
1043 if (error) {
1044 ctxp->error = error;
1045 return -1;
1046 }
1047
1048 ctxp->ncookies++;
1049 if (ctxp->cookies)
1050 *(ctxp->cookies)++ = ctxp->off + 1;
1051 ctxp->off++;
1052
1053 return 0;
1054 }
1055
1056 /*
1057 * readdir returns directory entries from pfsnode (vp).
1058 *
1059 * the strategy here with procfs is to generate a single
1060 * directory entry at a time (struct dirent) and then
1061 * copy that out to userland using uiomove. a more efficent
1062 * though more complex implementation, would try to minimize
1063 * the number of calls to uiomove(). for procfs, this is
1064 * hardly worth the added code complexity.
1065 *
1066 * this should just be done through read()
1067 */
1068 int
1069 procfs_readdir(v)
1070 void *v;
1071 {
1072 struct vop_readdir_args /* {
1073 struct vnode *a_vp;
1074 struct uio *a_uio;
1075 struct ucred *a_cred;
1076 int *a_eofflag;
1077 off_t **a_cookies;
1078 int *a_ncookies;
1079 } */ *ap = v;
1080 struct uio *uio = ap->a_uio;
1081 struct dirent d;
1082 struct pfsnode *pfs;
1083 off_t i;
1084 int error;
1085 off_t *cookies = NULL;
1086 int ncookies;
1087 struct vnode *vp;
1088 const struct proc_target *pt;
1089 struct procfs_root_readdir_ctx ctx;
1090
1091 vp = ap->a_vp;
1092 pfs = VTOPFS(vp);
1093
1094 if (uio->uio_resid < UIO_MX)
1095 return (EINVAL);
1096 if (uio->uio_offset < 0)
1097 return (EINVAL);
1098
1099 error = 0;
1100 i = uio->uio_offset;
1101 memset(&d, 0, UIO_MX);
1102 d.d_reclen = UIO_MX;
1103 ncookies = uio->uio_resid / UIO_MX;
1104
1105 switch (pfs->pfs_type) {
1106 /*
1107 * this is for the process-specific sub-directories.
1108 * all that is needed to is copy out all the entries
1109 * from the procent[] table (top of this file).
1110 */
1111 case PFSproc: {
1112 struct proc *p;
1113
1114 if (i >= nproc_targets)
1115 return 0;
1116
1117 p = PFIND(pfs->pfs_pid);
1118 if (p == NULL)
1119 break;
1120
1121 if (ap->a_ncookies) {
1122 ncookies = min(ncookies, (nproc_targets - i));
1123 cookies = malloc(ncookies * sizeof (off_t),
1124 M_TEMP, M_WAITOK);
1125 *ap->a_cookies = cookies;
1126 }
1127
1128 for (pt = &proc_targets[i];
1129 uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
1130 if (pt->pt_valid &&
1131 (*pt->pt_valid)(proc_representative_lwp(p), vp->v_mount) == 0)
1132 continue;
1133
1134 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
1135 pt->pt_pfstype, -1);
1136 d.d_namlen = pt->pt_namlen;
1137 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1138 d.d_type = pt->pt_type;
1139
1140 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1141 break;
1142 if (cookies)
1143 *cookies++ = i + 1;
1144 }
1145
1146 break;
1147 }
1148 case PFSfd: {
1149 struct proc *p;
1150 struct filedesc *fdp;
1151 struct file *fp;
1152 int lim, nc = 0;
1153
1154 p = PFIND(pfs->pfs_pid);
1155 if (p == NULL)
1156 return ESRCH;
1157
1158 fdp = p->p_fd;
1159
1160 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
1161 if (i >= lim)
1162 return 0;
1163
1164 if (ap->a_ncookies) {
1165 ncookies = min(ncookies, (fdp->fd_nfiles + 2 - i));
1166 cookies = malloc(ncookies * sizeof (off_t),
1167 M_TEMP, M_WAITOK);
1168 *ap->a_cookies = cookies;
1169 }
1170
1171 for (; i < 2 && uio->uio_resid >= UIO_MX; i++) {
1172 pt = &proc_targets[i];
1173 d.d_namlen = pt->pt_namlen;
1174 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
1175 pt->pt_pfstype, -1);
1176 (void)memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1177 d.d_type = pt->pt_type;
1178 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1179 break;
1180 if (cookies)
1181 *cookies++ = i + 1;
1182 nc++;
1183 }
1184 if (error) {
1185 ncookies = nc;
1186 break;
1187 }
1188 for (; uio->uio_resid >= UIO_MX && i < fdp->fd_nfiles; i++) {
1189 /* check the descriptor exists */
1190 if ((fp = fd_getfile(fdp, i - 2)) == NULL)
1191 continue;
1192 simple_unlock(&fp->f_slock);
1193
1194 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, PFSfd, i - 2);
1195 d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
1196 "%lld", (long long)(i - 2));
1197 d.d_type = VREG;
1198 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1199 break;
1200 if (cookies)
1201 *cookies++ = i + 1;
1202 nc++;
1203 }
1204 ncookies = nc;
1205 break;
1206 }
1207
1208 /*
1209 * this is for the root of the procfs filesystem
1210 * what is needed are special entries for "curproc"
1211 * and "self" followed by an entry for each process
1212 * on allproc.
1213 */
1214
1215 case PFSroot: {
1216 int nc = 0;
1217
1218 if (ap->a_ncookies) {
1219 /*
1220 * XXX Potentially allocating too much space here,
1221 * but I'm lazy. This loop needs some work.
1222 */
1223 cookies = malloc(ncookies * sizeof (off_t),
1224 M_TEMP, M_WAITOK);
1225 *ap->a_cookies = cookies;
1226 }
1227 error = 0;
1228 /* 0 ... 3 are static entries. */
1229 for (; i <= 3 && uio->uio_resid >= UIO_MX; i++) {
1230 switch (i) {
1231 case 0: /* `.' */
1232 case 1: /* `..' */
1233 d.d_fileno = PROCFS_FILENO(0, PFSroot, -1);
1234 d.d_namlen = i + 1;
1235 memcpy(d.d_name, "..", d.d_namlen);
1236 d.d_name[i + 1] = '\0';
1237 d.d_type = DT_DIR;
1238 break;
1239
1240 case 2:
1241 d.d_fileno = PROCFS_FILENO(0, PFScurproc, -1);
1242 d.d_namlen = sizeof("curproc") - 1;
1243 memcpy(d.d_name, "curproc", sizeof("curproc"));
1244 d.d_type = DT_LNK;
1245 break;
1246
1247 case 3:
1248 d.d_fileno = PROCFS_FILENO(0, PFSself, -1);
1249 d.d_namlen = sizeof("self") - 1;
1250 memcpy(d.d_name, "self", sizeof("self"));
1251 d.d_type = DT_LNK;
1252 break;
1253 }
1254
1255 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1256 break;
1257 nc++;
1258 if (cookies)
1259 *cookies++ = i + 1;
1260 }
1261 /* 4 ... are process entries. */
1262 ctx.uiop = uio;
1263 ctx.error = 0;
1264 ctx.off = 4;
1265 ctx.startoff = i;
1266 ctx.cookies = cookies;
1267 ctx.ncookies = nc;
1268 proclist_foreach_call(&allproc,
1269 procfs_root_readdir_callback, &ctx);
1270 cookies = ctx.cookies;
1271 nc = ctx.ncookies;
1272 error = ctx.error;
1273 if (error)
1274 break;
1275
1276 /* misc entries. */
1277 if (i < ctx.off)
1278 i = ctx.off;
1279 if (i >= ctx.off + nproc_root_targets)
1280 break;
1281 for (pt = &proc_root_targets[i - ctx.off];
1282 uio->uio_resid >= UIO_MX &&
1283 pt < &proc_root_targets[nproc_root_targets];
1284 pt++, i++) {
1285 if (pt->pt_valid &&
1286 (*pt->pt_valid)(NULL, vp->v_mount) == 0)
1287 continue;
1288 d.d_fileno = PROCFS_FILENO(0, pt->pt_pfstype, -1);
1289 d.d_namlen = pt->pt_namlen;
1290 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1291 d.d_type = pt->pt_type;
1292
1293 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1294 break;
1295 nc++;
1296 if (cookies)
1297 *cookies++ = i + 1;
1298 }
1299
1300 ncookies = nc;
1301 break;
1302 }
1303
1304 default:
1305 error = ENOTDIR;
1306 break;
1307 }
1308
1309 if (ap->a_ncookies) {
1310 if (error) {
1311 if (cookies)
1312 free(*ap->a_cookies, M_TEMP);
1313 *ap->a_ncookies = 0;
1314 *ap->a_cookies = NULL;
1315 } else
1316 *ap->a_ncookies = ncookies;
1317 }
1318 uio->uio_offset = i;
1319 return (error);
1320 }
1321
1322 /*
1323 * readlink reads the link of `curproc'
1324 */
1325 int
1326 procfs_readlink(v)
1327 void *v;
1328 {
1329 struct vop_readlink_args *ap = v;
1330 char buf[16]; /* should be enough */
1331 char *bp = buf;
1332 char *path = NULL;
1333 int len;
1334 int error = 0;
1335 struct pfsnode *pfs = VTOPFS(ap->a_vp);
1336
1337 if (pfs->pfs_fileno == PROCFS_FILENO(0, PFScurproc, -1))
1338 len = snprintf(buf, sizeof(buf), "%ld", (long)curproc->p_pid);
1339 else if (pfs->pfs_fileno == PROCFS_FILENO(0, PFSself, -1))
1340 len = snprintf(buf, sizeof(buf), "%s", "curproc");
1341 else {
1342 struct file *fp;
1343 struct proc *pown;
1344 struct vnode *vxp, *vp;
1345
1346 if ((error = procfs_getfp(pfs, &pown, &fp)) != 0)
1347 return error;
1348 FILE_USE(fp);
1349 switch (fp->f_type) {
1350 case DTYPE_VNODE:
1351 vxp = (struct vnode *)fp->f_data;
1352 if (vxp->v_type != VDIR) {
1353 FILE_UNUSE(fp, proc_representative_lwp(pown));
1354 return EINVAL;
1355 }
1356 if ((path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK))
1357 == NULL) {
1358 FILE_UNUSE(fp, proc_representative_lwp(pown));
1359 return ENOMEM;
1360 }
1361 bp = path + MAXPATHLEN;
1362 *--bp = '\0';
1363 vp = curproc->p_cwdi->cwdi_rdir;
1364 if (vp == NULL)
1365 vp = rootvnode;
1366 error = getcwd_common(vxp, vp, &bp, path,
1367 MAXPATHLEN / 2, 0, curlwp);
1368 FILE_UNUSE(fp, proc_representative_lwp(pown));
1369 if (error) {
1370 free(path, M_TEMP);
1371 return error;
1372 }
1373 len = strlen(bp);
1374 break;
1375
1376 case DTYPE_MISC:
1377 len = snprintf(buf, sizeof(buf), "%s", "[misc]");
1378 break;
1379
1380 case DTYPE_KQUEUE:
1381 len = snprintf(buf, sizeof(buf), "%s", "[kqueue]");
1382 break;
1383
1384 default:
1385 return EINVAL;
1386 }
1387 }
1388
1389 error = uiomove(bp, len, ap->a_uio);
1390 if (path)
1391 free(path, M_TEMP);
1392 return error;
1393 }
1394
1395 /*
1396 * convert decimal ascii to int
1397 */
1398 static int
1399 atoi(b, len)
1400 const char *b;
1401 size_t len;
1402 {
1403 int p = 0;
1404
1405 while (len--) {
1406 char c = *b++;
1407 if (c < '0' || c > '9')
1408 return -1;
1409 p = 10 * p + (c - '0');
1410 }
1411
1412 return p;
1413 }
1414