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