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