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