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