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