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