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