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