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