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