procfs_vnops.c revision 1.124 1 /* $NetBSD: procfs_vnops.c,v 1.124 2005/08/30 20:08:01 xtraeme 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.124 2005/08/30 20:08:01 xtraeme 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(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)(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(void *);
166 #define procfs_create genfs_eopnotsupp
167 #define procfs_mknod genfs_eopnotsupp
168 int procfs_open(void *);
169 int procfs_close(void *);
170 int procfs_access(void *);
171 int procfs_getattr(void *);
172 int procfs_setattr(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(void *);
183 #define procfs_rename genfs_eopnotsupp
184 #define procfs_mkdir genfs_eopnotsupp
185 #define procfs_rmdir genfs_eopnotsupp
186 int procfs_symlink(void *);
187 int procfs_readdir(void *);
188 int procfs_readlink(void *);
189 #define procfs_abortop genfs_abortop
190 int procfs_inactive(void *);
191 int procfs_reclaim(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(void *);
197 int procfs_pathconf(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(const char *, size_t);
209
210 /*
211 * procfs vnode operations.
212 */
213 int (**procfs_vnodeop_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 vnode *vp = ap->a_vp;
370 struct pfsnode *pfs = VTOPFS(vp);
371
372 VOP_UNLOCK(vp, 0);
373 if (PFIND(pfs->pfs_pid) == NULL && (vp->v_flag & VXLOCK) == 0)
374 vgone(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 proc *a_p;
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 bf[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(bf, sizeof(bf), "%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, 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 proc *a_p;
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_p)) != 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 int i, error, wantpunlock, iscurproc = 0, isself = 0;
805
806 *vpp = NULL;
807 cnp->cn_flags &= ~PDIRUNLOCK;
808
809 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
810 return (EROFS);
811
812 if (cnp->cn_namelen == 1 && *pname == '.') {
813 *vpp = dvp;
814 VREF(dvp);
815 return (0);
816 }
817
818 wantpunlock = (~cnp->cn_flags & (LOCKPARENT | ISLASTCN));
819 pfs = VTOPFS(dvp);
820 switch (pfs->pfs_type) {
821 case PFSroot:
822 /*
823 * Shouldn't get here with .. in the root node.
824 */
825 if (cnp->cn_flags & ISDOTDOT)
826 return (EIO);
827
828 iscurproc = CNEQ(cnp, "curproc", 7);
829 isself = CNEQ(cnp, "self", 4);
830
831 if (iscurproc || isself) {
832 error = procfs_allocvp(dvp->v_mount, vpp, 0,
833 iscurproc ? PFScurproc : PFSself, -1);
834 if ((error == 0) && (wantpunlock)) {
835 VOP_UNLOCK(dvp, 0);
836 cnp->cn_flags |= PDIRUNLOCK;
837 }
838 return (error);
839 }
840
841 for (i = 0; i < nproc_root_targets; i++) {
842 pt = &proc_root_targets[i];
843 if (cnp->cn_namelen == pt->pt_namlen &&
844 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
845 (pt->pt_valid == NULL ||
846 (*pt->pt_valid)(p, dvp->v_mount)))
847 break;
848 }
849
850 if (i != nproc_root_targets) {
851 error = procfs_allocvp(dvp->v_mount, vpp, 0,
852 pt->pt_pfstype, -1);
853 if ((error == 0) && (wantpunlock)) {
854 VOP_UNLOCK(dvp, 0);
855 cnp->cn_flags |= PDIRUNLOCK;
856 }
857 return (error);
858 }
859
860 pid = (pid_t)atoi(pname, cnp->cn_namelen);
861
862 p = PFIND(pid);
863 if (p == NULL)
864 break;
865
866 error = procfs_allocvp(dvp->v_mount, vpp, pid, PFSproc, -1);
867 if ((error == 0) && (wantpunlock)) {
868 VOP_UNLOCK(dvp, 0);
869 cnp->cn_flags |= PDIRUNLOCK;
870 }
871 return (error);
872
873 case PFSproc:
874 /*
875 * do the .. dance. We unlock the directory, and then
876 * get the root dir. That will automatically return ..
877 * locked. Then if the caller wanted dvp locked, we
878 * re-lock.
879 */
880 if (cnp->cn_flags & ISDOTDOT) {
881 VOP_UNLOCK(dvp, 0);
882 cnp->cn_flags |= PDIRUNLOCK;
883 error = procfs_root(dvp->v_mount, vpp);
884 if ((error == 0) && (wantpunlock == 0) &&
885 ((error = vn_lock(dvp, LK_EXCLUSIVE)) == 0))
886 cnp->cn_flags &= ~PDIRUNLOCK;
887 return (error);
888 }
889
890 p = PFIND(pfs->pfs_pid);
891 if (p == NULL)
892 break;
893
894 for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
895 if (cnp->cn_namelen == pt->pt_namlen &&
896 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
897 (pt->pt_valid == NULL ||
898 (*pt->pt_valid)(p, dvp->v_mount)))
899 goto found;
900 }
901 break;
902
903 found:
904 if (pt->pt_pfstype == PFSfile) {
905 fvp = p->p_textvp;
906 /* We already checked that it exists. */
907 VREF(fvp);
908 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY);
909 if (wantpunlock) {
910 VOP_UNLOCK(dvp, 0);
911 cnp->cn_flags |= PDIRUNLOCK;
912 }
913 *vpp = fvp;
914 return (0);
915 }
916
917 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
918 pt->pt_pfstype, -1);
919 if ((error == 0) && (wantpunlock)) {
920 VOP_UNLOCK(dvp, 0);
921 cnp->cn_flags |= PDIRUNLOCK;
922 }
923 return (error);
924
925 case PFSfd: {
926 int fd;
927 struct file *fp;
928 /*
929 * do the .. dance. We unlock the directory, and then
930 * get the proc dir. That will automatically return ..
931 * locked. Then if the caller wanted dvp locked, we
932 * re-lock.
933 */
934 if (cnp->cn_flags & ISDOTDOT) {
935 VOP_UNLOCK(dvp, 0);
936 cnp->cn_flags |= PDIRUNLOCK;
937 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
938 PFSproc, -1);
939 if ((error == 0) && (wantpunlock == 0) &&
940 ((error = vn_lock(dvp, LK_EXCLUSIVE)) == 0))
941 cnp->cn_flags &= ~PDIRUNLOCK;
942 return (error);
943 }
944 fd = atoi(pname, cnp->cn_namelen);
945 p = PFIND(pfs->pfs_pid);
946 if (p == NULL || (fp = fd_getfile(p->p_fd, fd)) == NULL)
947 return ENOENT;
948 FILE_USE(fp);
949
950 switch (fp->f_type) {
951 case DTYPE_VNODE:
952 fvp = (struct vnode *)fp->f_data;
953
954 /* Don't show directories */
955 if (fvp->v_type == VDIR)
956 goto symlink;
957
958 VREF(fvp);
959 FILE_UNUSE(fp, p);
960 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY |
961 (p == curproc ? LK_CANRECURSE : 0));
962 *vpp = fvp;
963 error = 0;
964 break;
965 default:
966 symlink:
967 FILE_UNUSE(fp, p);
968 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
969 PFSfd, fd);
970 break;
971 }
972 if ((error == 0) && (wantpunlock)) {
973 VOP_UNLOCK(dvp, 0);
974 cnp->cn_flags |= PDIRUNLOCK;
975 }
976 return error;
977 }
978 default:
979 return (ENOTDIR);
980 }
981
982 return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
983 }
984
985 int
986 procfs_validfile(p, mp)
987 struct proc *p;
988 struct mount *mp;
989 {
990 return (p->p_textvp != NULL);
991 }
992
993 static int
994 procfs_validfile_linux(p, mp)
995 struct proc *p;
996 struct mount *mp;
997 {
998 int flags;
999
1000 flags = VFSTOPROC(mp)->pmnt_flags;
1001 return ((flags & PROCFSMNT_LINUXCOMPAT) &&
1002 (p == NULL || procfs_validfile(p, mp)));
1003 }
1004
1005 struct procfs_root_readdir_ctx {
1006 struct uio *uiop;
1007 off_t *cookies;
1008 int ncookies;
1009 off_t off;
1010 off_t startoff;
1011 int error;
1012 };
1013
1014 static int
1015 procfs_root_readdir_callback(struct proc *p, void *arg)
1016 {
1017 struct procfs_root_readdir_ctx *ctxp = arg;
1018 struct dirent d;
1019 struct uio *uiop;
1020 int error;
1021
1022 uiop = ctxp->uiop;
1023 if (uiop->uio_resid < UIO_MX)
1024 return -1; /* no space */
1025
1026 if (ctxp->off < ctxp->startoff) {
1027 ctxp->off++;
1028 return 0;
1029 }
1030
1031 memset(&d, 0, UIO_MX);
1032 d.d_reclen = UIO_MX;
1033 d.d_fileno = PROCFS_FILENO(p->p_pid, PFSproc, -1);
1034 d.d_namlen = snprintf(d.d_name,
1035 UIO_MX - offsetof(struct dirent, d_name), "%ld", (long)p->p_pid);
1036 d.d_type = DT_DIR;
1037
1038 proclist_unlock_read();
1039 error = uiomove(&d, UIO_MX, uiop);
1040 proclist_lock_read();
1041 if (error) {
1042 ctxp->error = error;
1043 return -1;
1044 }
1045
1046 ctxp->ncookies++;
1047 if (ctxp->cookies)
1048 *(ctxp->cookies)++ = ctxp->off + 1;
1049 ctxp->off++;
1050
1051 return 0;
1052 }
1053
1054 /*
1055 * readdir returns directory entries from pfsnode (vp).
1056 *
1057 * the strategy here with procfs is to generate a single
1058 * directory entry at a time (struct dirent) and then
1059 * copy that out to userland using uiomove. a more efficent
1060 * though more complex implementation, would try to minimize
1061 * the number of calls to uiomove(). for procfs, this is
1062 * hardly worth the added code complexity.
1063 *
1064 * this should just be done through read()
1065 */
1066 int
1067 procfs_readdir(v)
1068 void *v;
1069 {
1070 struct vop_readdir_args /* {
1071 struct vnode *a_vp;
1072 struct uio *a_uio;
1073 struct ucred *a_cred;
1074 int *a_eofflag;
1075 off_t **a_cookies;
1076 int *a_ncookies;
1077 } */ *ap = v;
1078 struct uio *uio = ap->a_uio;
1079 struct dirent d;
1080 struct pfsnode *pfs;
1081 off_t i;
1082 int error;
1083 off_t *cookies = NULL;
1084 int ncookies;
1085 struct vnode *vp;
1086 const struct proc_target *pt;
1087 struct procfs_root_readdir_ctx ctx;
1088
1089 vp = ap->a_vp;
1090 pfs = VTOPFS(vp);
1091
1092 if (uio->uio_resid < UIO_MX)
1093 return (EINVAL);
1094 if (uio->uio_offset < 0)
1095 return (EINVAL);
1096
1097 error = 0;
1098 i = uio->uio_offset;
1099 memset(&d, 0, UIO_MX);
1100 d.d_reclen = UIO_MX;
1101 ncookies = uio->uio_resid / UIO_MX;
1102
1103 switch (pfs->pfs_type) {
1104 /*
1105 * this is for the process-specific sub-directories.
1106 * all that is needed to is copy out all the entries
1107 * from the procent[] table (top of this file).
1108 */
1109 case PFSproc: {
1110 struct proc *p;
1111
1112 if (i >= nproc_targets)
1113 return 0;
1114
1115 p = PFIND(pfs->pfs_pid);
1116 if (p == NULL)
1117 break;
1118
1119 if (ap->a_ncookies) {
1120 ncookies = min(ncookies, (nproc_targets - i));
1121 cookies = malloc(ncookies * sizeof (off_t),
1122 M_TEMP, M_WAITOK);
1123 *ap->a_cookies = cookies;
1124 }
1125
1126 for (pt = &proc_targets[i];
1127 uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
1128 if (pt->pt_valid &&
1129 (*pt->pt_valid)(p, vp->v_mount) == 0)
1130 continue;
1131
1132 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
1133 pt->pt_pfstype, -1);
1134 d.d_namlen = pt->pt_namlen;
1135 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1136 d.d_type = pt->pt_type;
1137
1138 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1139 break;
1140 if (cookies)
1141 *cookies++ = i + 1;
1142 }
1143
1144 break;
1145 }
1146 case PFSfd: {
1147 struct proc *p;
1148 struct filedesc *fdp;
1149 struct file *fp;
1150 int lim, nc = 0;
1151
1152 p = PFIND(pfs->pfs_pid);
1153 if (p == NULL)
1154 return ESRCH;
1155
1156 fdp = p->p_fd;
1157
1158 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
1159 if (i >= lim)
1160 return 0;
1161
1162 if (ap->a_ncookies) {
1163 ncookies = min(ncookies, (fdp->fd_nfiles + 2 - i));
1164 cookies = malloc(ncookies * sizeof (off_t),
1165 M_TEMP, M_WAITOK);
1166 *ap->a_cookies = cookies;
1167 }
1168
1169 for (; i < 2 && uio->uio_resid >= UIO_MX; i++) {
1170 pt = &proc_targets[i];
1171 d.d_namlen = pt->pt_namlen;
1172 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
1173 pt->pt_pfstype, -1);
1174 (void)memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1175 d.d_type = pt->pt_type;
1176 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1177 break;
1178 if (cookies)
1179 *cookies++ = i + 1;
1180 nc++;
1181 }
1182 if (error) {
1183 ncookies = nc;
1184 break;
1185 }
1186 for (; uio->uio_resid >= UIO_MX && i < fdp->fd_nfiles; i++) {
1187 /* check the descriptor exists */
1188 if ((fp = fd_getfile(fdp, i - 2)) == NULL)
1189 continue;
1190 simple_unlock(&fp->f_slock);
1191
1192 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, PFSfd, i - 2);
1193 d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
1194 "%lld", (long long)(i - 2));
1195 d.d_type = VREG;
1196 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1197 break;
1198 if (cookies)
1199 *cookies++ = i + 1;
1200 nc++;
1201 }
1202 ncookies = nc;
1203 break;
1204 }
1205
1206 /*
1207 * this is for the root of the procfs filesystem
1208 * what is needed are special entries for "curproc"
1209 * and "self" followed by an entry for each process
1210 * on allproc.
1211 */
1212
1213 case PFSroot: {
1214 int nc = 0;
1215
1216 if (ap->a_ncookies) {
1217 /*
1218 * XXX Potentially allocating too much space here,
1219 * but I'm lazy. This loop needs some work.
1220 */
1221 cookies = malloc(ncookies * sizeof (off_t),
1222 M_TEMP, M_WAITOK);
1223 *ap->a_cookies = cookies;
1224 }
1225 error = 0;
1226 /* 0 ... 3 are static entries. */
1227 for (; i <= 3 && uio->uio_resid >= UIO_MX; i++) {
1228 switch (i) {
1229 case 0: /* `.' */
1230 case 1: /* `..' */
1231 d.d_fileno = PROCFS_FILENO(0, PFSroot, -1);
1232 d.d_namlen = i + 1;
1233 memcpy(d.d_name, "..", d.d_namlen);
1234 d.d_name[i + 1] = '\0';
1235 d.d_type = DT_DIR;
1236 break;
1237
1238 case 2:
1239 d.d_fileno = PROCFS_FILENO(0, PFScurproc, -1);
1240 d.d_namlen = sizeof("curproc") - 1;
1241 memcpy(d.d_name, "curproc", sizeof("curproc"));
1242 d.d_type = DT_LNK;
1243 break;
1244
1245 case 3:
1246 d.d_fileno = PROCFS_FILENO(0, PFSself, -1);
1247 d.d_namlen = sizeof("self") - 1;
1248 memcpy(d.d_name, "self", sizeof("self"));
1249 d.d_type = DT_LNK;
1250 break;
1251 }
1252
1253 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1254 break;
1255 nc++;
1256 if (cookies)
1257 *cookies++ = i + 1;
1258 }
1259 /* 4 ... are process entries. */
1260 ctx.uiop = uio;
1261 ctx.error = 0;
1262 ctx.off = 4;
1263 ctx.startoff = i;
1264 ctx.cookies = cookies;
1265 ctx.ncookies = nc;
1266 proclist_foreach_call(&allproc,
1267 procfs_root_readdir_callback, &ctx);
1268 cookies = ctx.cookies;
1269 nc = ctx.ncookies;
1270 error = ctx.error;
1271 if (error)
1272 break;
1273
1274 /* misc entries. */
1275 if (i < ctx.off)
1276 i = ctx.off;
1277 if (i >= ctx.off + nproc_root_targets)
1278 break;
1279 for (pt = &proc_root_targets[i - ctx.off];
1280 uio->uio_resid >= UIO_MX &&
1281 pt < &proc_root_targets[nproc_root_targets];
1282 pt++, i++) {
1283 if (pt->pt_valid &&
1284 (*pt->pt_valid)(NULL, vp->v_mount) == 0)
1285 continue;
1286 d.d_fileno = PROCFS_FILENO(0, pt->pt_pfstype, -1);
1287 d.d_namlen = pt->pt_namlen;
1288 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1289 d.d_type = pt->pt_type;
1290
1291 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1292 break;
1293 nc++;
1294 if (cookies)
1295 *cookies++ = i + 1;
1296 }
1297
1298 ncookies = nc;
1299 break;
1300 }
1301
1302 default:
1303 error = ENOTDIR;
1304 break;
1305 }
1306
1307 if (ap->a_ncookies) {
1308 if (error) {
1309 if (cookies)
1310 free(*ap->a_cookies, M_TEMP);
1311 *ap->a_ncookies = 0;
1312 *ap->a_cookies = NULL;
1313 } else
1314 *ap->a_ncookies = ncookies;
1315 }
1316 uio->uio_offset = i;
1317 return (error);
1318 }
1319
1320 /*
1321 * readlink reads the link of `curproc'
1322 */
1323 int
1324 procfs_readlink(v)
1325 void *v;
1326 {
1327 struct vop_readlink_args *ap = v;
1328 char bf[16]; /* should be enough */
1329 char *bp = bf;
1330 char *path = NULL;
1331 int len;
1332 int error = 0;
1333 struct pfsnode *pfs = VTOPFS(ap->a_vp);
1334
1335 if (pfs->pfs_fileno == PROCFS_FILENO(0, PFScurproc, -1))
1336 len = snprintf(bf, sizeof(bf), "%ld", (long)curproc->p_pid);
1337 else if (pfs->pfs_fileno == PROCFS_FILENO(0, PFSself, -1))
1338 len = snprintf(bf, sizeof(bf), "%s", "curproc");
1339 else {
1340 struct file *fp;
1341 struct proc *pown;
1342 struct vnode *vxp, *vp;
1343
1344 if ((error = procfs_getfp(pfs, &pown, &fp)) != 0)
1345 return error;
1346 FILE_USE(fp);
1347 switch (fp->f_type) {
1348 case DTYPE_VNODE:
1349 vxp = (struct vnode *)fp->f_data;
1350 if (vxp->v_type != VDIR) {
1351 FILE_UNUSE(fp, pown);
1352 return EINVAL;
1353 }
1354 if ((path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK))
1355 == NULL) {
1356 FILE_UNUSE(fp, pown);
1357 return ENOMEM;
1358 }
1359 bp = path + MAXPATHLEN;
1360 *--bp = '\0';
1361 vp = curproc->p_cwdi->cwdi_rdir;
1362 if (vp == NULL)
1363 vp = rootvnode;
1364 error = getcwd_common(vxp, vp, &bp, path,
1365 MAXPATHLEN / 2, 0, curproc);
1366 FILE_UNUSE(fp, pown);
1367 if (error) {
1368 free(path, M_TEMP);
1369 return error;
1370 }
1371 len = strlen(bp);
1372 break;
1373
1374 case DTYPE_MISC:
1375 len = snprintf(bf, sizeof(bf), "%s", "[misc]");
1376 break;
1377
1378 case DTYPE_KQUEUE:
1379 len = snprintf(bf, sizeof(bf), "%s", "[kqueue]");
1380 break;
1381
1382 default:
1383 return EINVAL;
1384 }
1385 }
1386
1387 error = uiomove(bp, len, ap->a_uio);
1388 if (path)
1389 free(path, M_TEMP);
1390 return error;
1391 }
1392
1393 /*
1394 * convert decimal ascii to int
1395 */
1396 static int
1397 atoi(b, len)
1398 const char *b;
1399 size_t len;
1400 {
1401 int p = 0;
1402
1403 while (len--) {
1404 char c = *b++;
1405 if (c < '0' || c > '9')
1406 return -1;
1407 p = 10 * p + (c - '0');
1408 }
1409
1410 return p;
1411 }
1412