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