procfs_vnops.c revision 1.212 1 /* $NetBSD: procfs_vnops.c,v 1.212 2020/04/29 01:56:54 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2006, 2007, 2008, 2020 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.212 2020/04/29 01:56:54 thorpej Exp $");
109
110 #include <sys/param.h>
111 #include <sys/atomic.h>
112 #include <sys/systm.h>
113 #include <sys/time.h>
114 #include <sys/kernel.h>
115 #include <sys/file.h>
116 #include <sys/filedesc.h>
117 #include <sys/proc.h>
118 #include <sys/vnode.h>
119 #include <sys/namei.h>
120 #include <sys/malloc.h>
121 #include <sys/mount.h>
122 #include <sys/dirent.h>
123 #include <sys/resourcevar.h>
124 #include <sys/stat.h>
125 #include <sys/ptrace.h>
126 #include <sys/kauth.h>
127 #include <sys/exec.h>
128
129 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
130
131 #include <machine/reg.h>
132
133 #include <miscfs/genfs/genfs.h>
134 #include <miscfs/procfs/procfs.h>
135
136 /*
137 * Vnode Operations.
138 *
139 */
140
141 static int procfs_validfile_linux(struct lwp *, struct mount *);
142 static int procfs_root_readdir_callback(struct proc *, void *);
143 static void procfs_dir(pfstype, struct lwp *, struct proc *, char **, char *,
144 size_t);
145
146 /*
147 * This is a list of the valid names in the
148 * process-specific sub-directories. It is
149 * used in procfs_lookup and procfs_readdir
150 */
151 static const struct proc_target {
152 u_char pt_type;
153 u_char pt_namlen;
154 const char *pt_name;
155 pfstype pt_pfstype;
156 int (*pt_valid)(struct lwp *, struct mount *);
157 } proc_targets[] = {
158 #define N(s) sizeof(s)-1, s
159 /* name type validp */
160 { DT_DIR, N("."), PFSproc, NULL },
161 { DT_DIR, N(".."), PFSroot, NULL },
162 { DT_DIR, N("fd"), PFSfd, NULL },
163 { DT_DIR, N("task"), PFStask, procfs_validfile_linux },
164 { DT_LNK, N("cwd"), PFScwd, NULL },
165 { DT_LNK, N("emul"), PFSemul, NULL },
166 { DT_LNK, N("root"), PFSchroot, NULL },
167 { DT_REG, N("auxv"), PFSauxv, procfs_validauxv },
168 { DT_REG, N("cmdline"), PFScmdline, NULL },
169 { DT_REG, N("environ"), PFSenviron, NULL },
170 { DT_REG, N("exe"), PFSexe, procfs_validfile },
171 { DT_REG, N("file"), PFSfile, procfs_validfile },
172 { DT_REG, N("fpregs"), PFSfpregs, procfs_validfpregs },
173 { DT_REG, N("limit"), PFSlimit, NULL },
174 { DT_REG, N("map"), PFSmap, procfs_validmap },
175 { DT_REG, N("maps"), PFSmaps, procfs_validmap },
176 { DT_REG, N("mem"), PFSmem, NULL },
177 { DT_REG, N("note"), PFSnote, NULL },
178 { DT_REG, N("notepg"), PFSnotepg, NULL },
179 { DT_REG, N("regs"), PFSregs, procfs_validregs },
180 { DT_REG, N("stat"), PFSstat, procfs_validfile_linux },
181 { DT_REG, N("statm"), PFSstatm, procfs_validfile_linux },
182 { DT_REG, N("status"), PFSstatus, NULL },
183 #ifdef __HAVE_PROCFS_MACHDEP
184 PROCFS_MACHDEP_NODETYPE_DEFNS
185 #endif
186 #undef N
187 };
188 static const int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]);
189
190 /*
191 * List of files in the root directory. Note: the validate function will
192 * be called with p == NULL for these ones.
193 */
194 static const struct proc_target proc_root_targets[] = {
195 #define N(s) sizeof(s)-1, s
196 /* name type validp */
197 { DT_REG, N("meminfo"), PFSmeminfo, procfs_validfile_linux },
198 { DT_REG, N("cpuinfo"), PFScpuinfo, procfs_validfile_linux },
199 { DT_REG, N("uptime"), PFSuptime, procfs_validfile_linux },
200 { DT_REG, N("mounts"), PFSmounts, procfs_validfile_linux },
201 { DT_REG, N("devices"), PFSdevices, procfs_validfile_linux },
202 { DT_REG, N("stat"), PFScpustat, procfs_validfile_linux },
203 { DT_REG, N("loadavg"), PFSloadavg, procfs_validfile_linux },
204 { DT_REG, N("version"), PFSversion, procfs_validfile_linux },
205 #undef N
206 };
207 static const int nproc_root_targets =
208 sizeof(proc_root_targets) / sizeof(proc_root_targets[0]);
209
210 int procfs_lookup(void *);
211 #define procfs_create genfs_eopnotsupp
212 #define procfs_mknod genfs_eopnotsupp
213 int procfs_open(void *);
214 int procfs_close(void *);
215 int procfs_access(void *);
216 int procfs_getattr(void *);
217 int procfs_setattr(void *);
218 #define procfs_read procfs_rw
219 #define procfs_write procfs_rw
220 #define procfs_fcntl genfs_fcntl
221 #define procfs_ioctl genfs_enoioctl
222 #define procfs_poll genfs_poll
223 #define procfs_kqfilter genfs_kqfilter
224 #define procfs_revoke genfs_revoke
225 #define procfs_fsync genfs_nullop
226 #define procfs_seek genfs_nullop
227 #define procfs_remove genfs_eopnotsupp
228 int procfs_link(void *);
229 #define procfs_rename genfs_eopnotsupp
230 #define procfs_mkdir genfs_eopnotsupp
231 #define procfs_rmdir genfs_eopnotsupp
232 int procfs_symlink(void *);
233 int procfs_readdir(void *);
234 int procfs_readlink(void *);
235 #define procfs_abortop genfs_abortop
236 int procfs_inactive(void *);
237 int procfs_reclaim(void *);
238 #define procfs_lock genfs_lock
239 #define procfs_unlock genfs_unlock
240 #define procfs_bmap genfs_badop
241 #define procfs_strategy genfs_badop
242 int procfs_print(void *);
243 int procfs_pathconf(void *);
244 #define procfs_islocked genfs_islocked
245 #define procfs_advlock genfs_einval
246 #define procfs_bwrite genfs_eopnotsupp
247 int procfs_getpages(void *);
248 #define procfs_putpages genfs_null_putpages
249
250 static int atoi(const char *, size_t);
251
252 /*
253 * procfs vnode operations.
254 */
255 int (**procfs_vnodeop_p)(void *);
256 const struct vnodeopv_entry_desc procfs_vnodeop_entries[] = {
257 { &vop_default_desc, vn_default_error },
258 { &vop_lookup_desc, procfs_lookup }, /* lookup */
259 { &vop_create_desc, procfs_create }, /* create */
260 { &vop_mknod_desc, procfs_mknod }, /* mknod */
261 { &vop_open_desc, procfs_open }, /* open */
262 { &vop_close_desc, procfs_close }, /* close */
263 { &vop_access_desc, procfs_access }, /* access */
264 { &vop_getattr_desc, procfs_getattr }, /* getattr */
265 { &vop_setattr_desc, procfs_setattr }, /* setattr */
266 { &vop_read_desc, procfs_read }, /* read */
267 { &vop_write_desc, procfs_write }, /* write */
268 { &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */
269 { &vop_fdiscard_desc, genfs_eopnotsupp }, /* fdiscard */
270 { &vop_fcntl_desc, procfs_fcntl }, /* fcntl */
271 { &vop_ioctl_desc, procfs_ioctl }, /* ioctl */
272 { &vop_poll_desc, procfs_poll }, /* poll */
273 { &vop_kqfilter_desc, procfs_kqfilter }, /* kqfilter */
274 { &vop_revoke_desc, procfs_revoke }, /* revoke */
275 { &vop_fsync_desc, procfs_fsync }, /* fsync */
276 { &vop_seek_desc, procfs_seek }, /* seek */
277 { &vop_remove_desc, procfs_remove }, /* remove */
278 { &vop_link_desc, procfs_link }, /* link */
279 { &vop_rename_desc, procfs_rename }, /* rename */
280 { &vop_mkdir_desc, procfs_mkdir }, /* mkdir */
281 { &vop_rmdir_desc, procfs_rmdir }, /* rmdir */
282 { &vop_symlink_desc, procfs_symlink }, /* symlink */
283 { &vop_readdir_desc, procfs_readdir }, /* readdir */
284 { &vop_readlink_desc, procfs_readlink }, /* readlink */
285 { &vop_abortop_desc, procfs_abortop }, /* abortop */
286 { &vop_inactive_desc, procfs_inactive }, /* inactive */
287 { &vop_reclaim_desc, procfs_reclaim }, /* reclaim */
288 { &vop_lock_desc, procfs_lock }, /* lock */
289 { &vop_unlock_desc, procfs_unlock }, /* unlock */
290 { &vop_bmap_desc, procfs_bmap }, /* bmap */
291 { &vop_strategy_desc, procfs_strategy }, /* strategy */
292 { &vop_print_desc, procfs_print }, /* print */
293 { &vop_islocked_desc, procfs_islocked }, /* islocked */
294 { &vop_pathconf_desc, procfs_pathconf }, /* pathconf */
295 { &vop_advlock_desc, procfs_advlock }, /* advlock */
296 { &vop_getpages_desc, procfs_getpages }, /* getpages */
297 { &vop_putpages_desc, procfs_putpages }, /* putpages */
298 { NULL, NULL }
299 };
300 const struct vnodeopv_desc procfs_vnodeop_opv_desc =
301 { &procfs_vnodeop_p, procfs_vnodeop_entries };
302 /*
303 * set things up for doing i/o on
304 * the pfsnode (vp). (vp) is locked
305 * on entry, and should be left locked
306 * on exit.
307 *
308 * for procfs we don't need to do anything
309 * in particular for i/o. all that is done
310 * is to support exclusive open on process
311 * memory images.
312 */
313 int
314 procfs_open(void *v)
315 {
316 struct vop_open_args /* {
317 struct vnode *a_vp;
318 int a_mode;
319 kauth_cred_t a_cred;
320 } */ *ap = v;
321 struct vnode *vp = ap->a_vp;
322 struct pfsnode *pfs = VTOPFS(vp);
323 struct lwp *l1;
324 struct proc *p2;
325 int error;
326
327 if ((error =
328 procfs_proc_lock(vp->v_mount, pfs->pfs_pid, &p2, ENOENT)) != 0)
329 return error;
330
331 l1 = curlwp; /* tracer */
332
333 #define M2K(m) (((m) & FREAD) && ((m) & FWRITE) ? \
334 KAUTH_REQ_PROCESS_PROCFS_RW : \
335 (m) & FWRITE ? KAUTH_REQ_PROCESS_PROCFS_WRITE : \
336 KAUTH_REQ_PROCESS_PROCFS_READ)
337
338 mutex_enter(p2->p_lock);
339 error = kauth_authorize_process(l1->l_cred, KAUTH_PROCESS_PROCFS,
340 p2, pfs, KAUTH_ARG(M2K(ap->a_mode)), NULL);
341 mutex_exit(p2->p_lock);
342 if (error) {
343 procfs_proc_unlock(p2);
344 return (error);
345 }
346
347 #undef M2K
348
349 switch (pfs->pfs_type) {
350 case PFSmem:
351 if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) ||
352 ((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE))) {
353 error = EBUSY;
354 break;
355 }
356
357 if (!proc_isunder(p2, l1)) {
358 error = EPERM;
359 break;
360 }
361
362 if (ap->a_mode & FWRITE)
363 pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
364
365 break;
366
367 case PFSregs:
368 case PFSfpregs:
369 if (!proc_isunder(p2, l1)) {
370 error = EPERM;
371 break;
372 }
373 break;
374
375 default:
376 break;
377 }
378
379 procfs_proc_unlock(p2);
380 return (error);
381 }
382
383 /*
384 * close the pfsnode (vp) after doing i/o.
385 * (vp) is not locked on entry or exit.
386 *
387 * nothing to do for procfs other than undo
388 * any exclusive open flag (see _open above).
389 */
390 int
391 procfs_close(void *v)
392 {
393 struct vop_close_args /* {
394 struct vnode *a_vp;
395 int a_fflag;
396 kauth_cred_t a_cred;
397 } */ *ap = v;
398 struct pfsnode *pfs = VTOPFS(ap->a_vp);
399
400 switch (pfs->pfs_type) {
401 case PFSmem:
402 if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
403 pfs->pfs_flags &= ~(FWRITE|O_EXCL);
404 break;
405
406 default:
407 break;
408 }
409
410 return (0);
411 }
412
413 /*
414 * _inactive is called when the pfsnode
415 * is vrele'd and the reference count goes
416 * to zero. (vp) will be on the vnode free
417 * list, so to get it back vget() must be
418 * used.
419 *
420 * (vp) is locked on entry, but must be unlocked on exit.
421 */
422 int
423 procfs_inactive(void *v)
424 {
425 struct vop_inactive_v2_args /* {
426 struct vnode *a_vp;
427 bool *a_recycle;
428 } */ *ap = v;
429 struct vnode *vp = ap->a_vp;
430 struct pfsnode *pfs = VTOPFS(vp);
431
432 mutex_enter(proc_lock);
433 *ap->a_recycle = (procfs_proc_find(vp->v_mount, pfs->pfs_pid) == NULL);
434 mutex_exit(proc_lock);
435
436 return (0);
437 }
438
439 /*
440 * _reclaim is called when getnewvnode()
441 * wants to make use of an entry on the vnode
442 * free list. at this time the filesystem needs
443 * to free any private data and remove the node
444 * from any private lists.
445 */
446 int
447 procfs_reclaim(void *v)
448 {
449 struct vop_reclaim_v2_args /* {
450 struct vnode *a_vp;
451 } */ *ap = v;
452 struct vnode *vp = ap->a_vp;
453 struct pfsnode *pfs = VTOPFS(vp);
454
455 VOP_UNLOCK(vp);
456
457 /*
458 * To interlock with procfs_revoke_vnodes().
459 */
460 mutex_enter(vp->v_interlock);
461 vp->v_data = NULL;
462 mutex_exit(vp->v_interlock);
463 kmem_free(pfs, sizeof(*pfs));
464 return 0;
465 }
466
467 /*
468 * Return POSIX pathconf information applicable to special devices.
469 */
470 int
471 procfs_pathconf(void *v)
472 {
473 struct vop_pathconf_args /* {
474 struct vnode *a_vp;
475 int a_name;
476 register_t *a_retval;
477 } */ *ap = v;
478
479 switch (ap->a_name) {
480 case _PC_LINK_MAX:
481 *ap->a_retval = LINK_MAX;
482 return (0);
483 case _PC_MAX_CANON:
484 *ap->a_retval = MAX_CANON;
485 return (0);
486 case _PC_MAX_INPUT:
487 *ap->a_retval = MAX_INPUT;
488 return (0);
489 case _PC_PIPE_BUF:
490 *ap->a_retval = PIPE_BUF;
491 return (0);
492 case _PC_CHOWN_RESTRICTED:
493 *ap->a_retval = 1;
494 return (0);
495 case _PC_VDISABLE:
496 *ap->a_retval = _POSIX_VDISABLE;
497 return (0);
498 case _PC_SYNC_IO:
499 *ap->a_retval = 1;
500 return (0);
501 default:
502 return (EINVAL);
503 }
504 /* NOTREACHED */
505 }
506
507 /*
508 * _print is used for debugging.
509 * just print a readable description
510 * of (vp).
511 */
512 int
513 procfs_print(void *v)
514 {
515 struct vop_print_args /* {
516 struct vnode *a_vp;
517 } */ *ap = v;
518 struct pfsnode *pfs = VTOPFS(ap->a_vp);
519
520 printf("tag VT_PROCFS, type %d, pid %d, mode %x, flags %lx\n",
521 pfs->pfs_type, pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
522 return 0;
523 }
524
525 int
526 procfs_link(void *v)
527 {
528 struct vop_link_v2_args /* {
529 struct vnode *a_dvp;
530 struct vnode *a_vp;
531 struct componentname *a_cnp;
532 } */ *ap = v;
533
534 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
535 return (EROFS);
536 }
537
538 int
539 procfs_symlink(void *v)
540 {
541 struct vop_symlink_v3_args /* {
542 struct vnode *a_dvp;
543 struct vnode **a_vpp;
544 struct componentname *a_cnp;
545 struct vattr *a_vap;
546 char *a_target;
547 } */ *ap = v;
548
549 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
550 return (EROFS);
551 }
552
553 /*
554 * Works out the path to the target process's current
555 * working directory or chroot. If the caller is in a chroot and
556 * can't "reach" the target's cwd or root (or some other error
557 * occurs), a "/" is returned for the path.
558 */
559 static void
560 procfs_dir(pfstype t, struct lwp *caller, struct proc *target, char **bpp,
561 char *path, size_t len)
562 {
563 struct cwdinfo *cwdi;
564 struct vnode *vp, *rvp;
565 char *bp;
566
567 /*
568 * Lock target cwdi and take a reference to the vnode
569 * we are interested in to prevent it from disappearing
570 * before getcwd_common() below.
571 */
572 rw_enter(&target->p_cwdi->cwdi_lock, RW_READER);
573 switch (t) {
574 case PFScwd:
575 vp = target->p_cwdi->cwdi_cdir;
576 break;
577 case PFSchroot:
578 vp = target->p_cwdi->cwdi_rdir;
579 break;
580 default:
581 rw_exit(&target->p_cwdi->cwdi_lock);
582 return;
583 }
584 if (vp != NULL)
585 vref(vp);
586 rw_exit(&target->p_cwdi->cwdi_lock);
587
588 cwdi = caller->l_proc->p_cwdi;
589 rw_enter(&cwdi->cwdi_lock, RW_READER);
590
591 rvp = cwdi->cwdi_rdir;
592 bp = bpp ? *bpp : NULL;
593
594 /*
595 * XXX: this horrible kludge avoids locking panics when
596 * attempting to lookup links that point to within procfs
597 */
598 if (vp != NULL && vp->v_tag == VT_PROCFS) {
599 if (bpp) {
600 *--bp = '/';
601 *bpp = bp;
602 }
603 vrele(vp);
604 rw_exit(&cwdi->cwdi_lock);
605 return;
606 }
607
608 if (rvp == NULL)
609 rvp = rootvnode;
610 if (vp == NULL || getcwd_common(vp, rvp, bp ? &bp : NULL, path,
611 len / 2, 0, caller) != 0) {
612 if (bpp) {
613 bp = *bpp;
614 *--bp = '/';
615 }
616 }
617
618 if (bpp)
619 *bpp = bp;
620
621 if (vp != NULL)
622 vrele(vp);
623 rw_exit(&cwdi->cwdi_lock);
624 }
625
626 /*
627 * Invent attributes for pfsnode (vp) and store
628 * them in (vap).
629 * Directories lengths are returned as zero since
630 * any real length would require the genuine size
631 * to be computed, and nothing cares anyway.
632 *
633 * this is relatively minimal for procfs.
634 */
635 int
636 procfs_getattr(void *v)
637 {
638 struct vop_getattr_args /* {
639 struct vnode *a_vp;
640 struct vattr *a_vap;
641 kauth_cred_t a_cred;
642 } */ *ap = v;
643 struct vnode *vp = ap->a_vp;
644 struct pfsnode *pfs = VTOPFS(vp);
645 struct vattr *vap = ap->a_vap;
646 struct proc *procp;
647 char *path, *bp, bf[16];
648 int error;
649
650 /* first check the process still exists */
651 switch (pfs->pfs_type) {
652 case PFSroot:
653 case PFScurproc:
654 case PFSself:
655 procp = NULL;
656 break;
657
658 default:
659 error =
660 procfs_proc_lock(vp->v_mount, pfs->pfs_pid, &procp, ENOENT);
661 if (error != 0)
662 return (error);
663 break;
664 }
665
666 switch (pfs->pfs_type) {
667 case PFStask:
668 if (pfs->pfs_fd == -1) {
669 path = NULL;
670 break;
671 }
672 /*FALLTHROUGH*/
673 case PFScwd:
674 case PFSchroot:
675 path = malloc(MAXPATHLEN + 4, M_TEMP, M_WAITOK);
676 if (path == NULL && procp != NULL) {
677 procfs_proc_unlock(procp);
678 return (ENOMEM);
679 }
680 break;
681
682 default:
683 path = NULL;
684 break;
685 }
686
687 if (procp != NULL) {
688 mutex_enter(procp->p_lock);
689 error = kauth_authorize_process(kauth_cred_get(),
690 KAUTH_PROCESS_CANSEE, procp,
691 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
692 mutex_exit(procp->p_lock);
693 if (error != 0) {
694 procfs_proc_unlock(procp);
695 if (path != NULL)
696 free(path, M_TEMP);
697 return (ENOENT);
698 }
699 }
700
701 error = 0;
702
703 /* start by zeroing out the attributes */
704 vattr_null(vap);
705
706 /* next do all the common fields */
707 vap->va_type = ap->a_vp->v_type;
708 vap->va_mode = pfs->pfs_mode;
709 vap->va_fileid = pfs->pfs_fileno;
710 vap->va_flags = 0;
711 vap->va_blocksize = PAGE_SIZE;
712
713 /*
714 * Make all times be current TOD.
715 *
716 * It would be possible to get the process start
717 * time from the p_stats structure, but there's
718 * no "file creation" time stamp anyway, and the
719 * p_stats structure is not addressable if u. gets
720 * swapped out for that process.
721 */
722 getnanotime(&vap->va_ctime);
723 vap->va_atime = vap->va_mtime = vap->va_ctime;
724 if (procp)
725 TIMEVAL_TO_TIMESPEC(&procp->p_stats->p_start,
726 &vap->va_birthtime);
727 else
728 getnanotime(&vap->va_birthtime);
729
730 switch (pfs->pfs_type) {
731 case PFSmem:
732 case PFSregs:
733 case PFSfpregs:
734 #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
735 PROCFS_MACHDEP_PROTECT_CASES
736 #endif
737 /*
738 * If the process has exercised some setuid or setgid
739 * privilege, then rip away read/write permission so
740 * that only root can gain access.
741 */
742 if (procp->p_flag & PK_SUGID)
743 vap->va_mode &= ~(S_IRUSR|S_IWUSR);
744 /* FALLTHROUGH */
745 case PFSstatus:
746 case PFSstat:
747 case PFSnote:
748 case PFSnotepg:
749 case PFScmdline:
750 case PFSenviron:
751 case PFSemul:
752 case PFSstatm:
753
754 case PFSmap:
755 case PFSmaps:
756 case PFSlimit:
757 case PFSauxv:
758 vap->va_nlink = 1;
759 vap->va_uid = kauth_cred_geteuid(procp->p_cred);
760 vap->va_gid = kauth_cred_getegid(procp->p_cred);
761 break;
762 case PFScwd:
763 case PFSchroot:
764 case PFSmeminfo:
765 case PFSdevices:
766 case PFScpuinfo:
767 case PFSuptime:
768 case PFSmounts:
769 case PFScpustat:
770 case PFSloadavg:
771 case PFSversion:
772 case PFSexe:
773 case PFSself:
774 case PFScurproc:
775 case PFSroot:
776 vap->va_nlink = 1;
777 vap->va_uid = vap->va_gid = 0;
778 break;
779
780 case PFSproc:
781 case PFStask:
782 case PFSfile:
783 case PFSfd:
784 break;
785
786 default:
787 panic("%s: %d/1", __func__, pfs->pfs_type);
788 }
789
790 /*
791 * now do the object specific fields
792 *
793 * The size could be set from struct reg, but it's hardly
794 * worth the trouble, and it puts some (potentially) machine
795 * dependent data into this machine-independent code. If it
796 * becomes important then this function should break out into
797 * a per-file stat function in the corresponding .c file.
798 */
799
800 switch (pfs->pfs_type) {
801 case PFSroot:
802 vap->va_bytes = vap->va_size = DEV_BSIZE;
803 break;
804
805 case PFSself:
806 case PFScurproc:
807 vap->va_bytes = vap->va_size =
808 snprintf(bf, sizeof(bf), "%ld", (long)curproc->p_pid);
809 break;
810 case PFStask:
811 if (pfs->pfs_fd != -1) {
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 #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 PFSenviron:
885 case PFSmeminfo:
886 case PFSdevices:
887 case PFScpuinfo:
888 case PFSuptime:
889 case PFSmounts:
890 case PFScpustat:
891 case PFSloadavg:
892 case PFSstatm:
893 case PFSversion:
894 vap->va_bytes = vap->va_size = 0;
895 break;
896 case PFSlimit:
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 bp = path + MAXPATHLEN;
910 *--bp = '\0';
911 procfs_dir(pfs->pfs_type, curlwp, procp, &bp, path,
912 MAXPATHLEN);
913 vap->va_bytes = vap->va_size = strlen(bp);
914 break;
915
916 case PFSexe:
917 vap->va_bytes = vap->va_size = strlen(procp->p_path);
918 break;
919
920 case PFSemul:
921 vap->va_bytes = vap->va_size = strlen(procp->p_emul->e_name);
922 break;
923
924 #ifdef __HAVE_PROCFS_MACHDEP
925 PROCFS_MACHDEP_NODETYPE_CASES
926 error = procfs_machdep_getattr(ap->a_vp, vap, procp);
927 break;
928 #endif
929
930 default:
931 panic("%s: %d/2", __func__, pfs->pfs_type);
932 }
933
934 if (procp != NULL)
935 procfs_proc_unlock(procp);
936 if (path != NULL)
937 free(path, M_TEMP);
938
939 return (error);
940 }
941
942 /*ARGSUSED*/
943 int
944 procfs_setattr(void *v)
945 {
946 /*
947 * just fake out attribute setting
948 * it's not good to generate an error
949 * return, otherwise things like creat()
950 * will fail when they try to set the
951 * file length to 0. worse, this means
952 * that echo $note > /proc/$pid/note will fail.
953 */
954
955 return (0);
956 }
957
958 /*
959 * implement access checking.
960 *
961 * actually, the check for super-user is slightly
962 * broken since it will allow read access to write-only
963 * objects. this doesn't cause any particular trouble
964 * but does mean that the i/o entry points need to check
965 * that the operation really does make sense.
966 */
967 int
968 procfs_access(void *v)
969 {
970 struct vop_access_args /* {
971 struct vnode *a_vp;
972 int a_mode;
973 kauth_cred_t a_cred;
974 } */ *ap = v;
975 struct vattr va;
976 int error;
977
978 if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred)) != 0)
979 return (error);
980
981 return kauth_authorize_vnode(ap->a_cred,
982 KAUTH_ACCESS_ACTION(ap->a_mode, ap->a_vp->v_type, va.va_mode),
983 ap->a_vp, NULL, genfs_can_access(va.va_type, va.va_mode,
984 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
985 }
986
987 /*
988 * lookup. this is incredibly complicated in the
989 * general case, however for most pseudo-filesystems
990 * very little needs to be done.
991 *
992 * Locking isn't hard here, just poorly documented.
993 *
994 * If we're looking up ".", just vref the parent & return it.
995 *
996 * If we're looking up "..", unlock the parent, and lock "..". If everything
997 * went ok, and we're on the last component and the caller requested the
998 * parent locked, try to re-lock the parent. We do this to prevent lock
999 * races.
1000 *
1001 * For anything else, get the needed node. Then unlock the parent if not
1002 * the last component or not LOCKPARENT (i.e. if we wouldn't re-lock the
1003 * parent in the .. case).
1004 *
1005 * We try to exit with the parent locked in error cases.
1006 */
1007 int
1008 procfs_lookup(void *v)
1009 {
1010 struct vop_lookup_v2_args /* {
1011 struct vnode * a_dvp;
1012 struct vnode ** a_vpp;
1013 struct componentname * a_cnp;
1014 } */ *ap = v;
1015 struct componentname *cnp = ap->a_cnp;
1016 struct vnode **vpp = ap->a_vpp;
1017 struct vnode *dvp = ap->a_dvp;
1018 const char *pname = cnp->cn_nameptr;
1019 const struct proc_target *pt = NULL;
1020 struct vnode *fvp;
1021 pid_t pid, vnpid;
1022 struct pfsnode *pfs;
1023 struct proc *p = NULL;
1024 struct lwp *plwp;
1025 int i, error;
1026 pfstype type;
1027
1028 *vpp = NULL;
1029
1030 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
1031 return (EROFS);
1032
1033 if (cnp->cn_namelen == 1 && *pname == '.') {
1034 *vpp = dvp;
1035 vref(dvp);
1036 return (0);
1037 }
1038
1039 pfs = VTOPFS(dvp);
1040 switch (pfs->pfs_type) {
1041 case PFSroot:
1042 /*
1043 * Shouldn't get here with .. in the root node.
1044 */
1045 if (cnp->cn_flags & ISDOTDOT)
1046 return (EIO);
1047
1048 for (i = 0; i < nproc_root_targets; i++) {
1049 pt = &proc_root_targets[i];
1050 /*
1051 * check for node match. proc is always NULL here,
1052 * so call pt_valid with constant NULL lwp.
1053 */
1054 if (cnp->cn_namelen == pt->pt_namlen &&
1055 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
1056 (pt->pt_valid == NULL ||
1057 (*pt->pt_valid)(NULL, dvp->v_mount)))
1058 break;
1059 }
1060
1061 if (i != nproc_root_targets) {
1062 error = procfs_allocvp(dvp->v_mount, vpp, 0,
1063 pt->pt_pfstype, -1);
1064 return (error);
1065 }
1066
1067 if (CNEQ(cnp, "curproc", 7)) {
1068 pid = curproc->p_pid;
1069 vnpid = 0;
1070 type = PFScurproc;
1071 } else if (CNEQ(cnp, "self", 4)) {
1072 pid = curproc->p_pid;
1073 vnpid = 0;
1074 type = PFSself;
1075 } else {
1076 pid = (pid_t)atoi(pname, cnp->cn_namelen);
1077 vnpid = pid;
1078 type = PFSproc;
1079 }
1080
1081 if (procfs_proc_lock(dvp->v_mount, pid, &p, ESRCH) != 0)
1082 break;
1083 error = procfs_allocvp(dvp->v_mount, vpp, vnpid, type, -1);
1084 procfs_proc_unlock(p);
1085 return (error);
1086
1087 case PFSproc:
1088 if (cnp->cn_flags & ISDOTDOT) {
1089 error = procfs_allocvp(dvp->v_mount, vpp, 0, PFSroot,
1090 -1);
1091 return (error);
1092 }
1093
1094 if (procfs_proc_lock(dvp->v_mount, pfs->pfs_pid, &p,
1095 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(dvp->v_mount, pfs->pfs_pid, &p,
1148 ENOENT)) != 0)
1149 return error;
1150
1151 if (cnp->cn_flags & ISDOTDOT) {
1152 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
1153 PFSproc, -1);
1154 procfs_proc_unlock(p);
1155 return (error);
1156 }
1157 fd = atoi(pname, cnp->cn_namelen);
1158
1159 fp = fd_getfile2(p, fd);
1160 if (fp == NULL) {
1161 procfs_proc_unlock(p);
1162 return ENOENT;
1163 }
1164 fvp = fp->f_vnode;
1165
1166 /* Don't show directories */
1167 if (fp->f_type == DTYPE_VNODE && fvp->v_type != VDIR) {
1168 vref(fvp);
1169 closef(fp);
1170 procfs_proc_unlock(p);
1171 *vpp = fvp;
1172 return 0;
1173 }
1174
1175 closef(fp);
1176 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
1177 PFSfd, fd);
1178 procfs_proc_unlock(p);
1179 return error;
1180 }
1181 case PFStask: {
1182 int xpid;
1183
1184 if ((error = procfs_proc_lock(dvp->v_mount, pfs->pfs_pid, &p,
1185 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 return procfs_use_linux_compat(mp) &&
1222 (l == NULL || l->l_proc == NULL || procfs_validfile(l, mp));
1223 }
1224
1225 struct procfs_root_readdir_ctx {
1226 struct uio *uiop;
1227 off_t *cookies;
1228 int ncookies;
1229 off_t off;
1230 off_t startoff;
1231 int error;
1232 };
1233
1234 static int
1235 procfs_root_readdir_callback(struct proc *p, void *arg)
1236 {
1237 struct procfs_root_readdir_ctx *ctxp = arg;
1238 struct dirent d;
1239 struct uio *uiop;
1240 int error;
1241
1242 uiop = ctxp->uiop;
1243 if (uiop->uio_resid < UIO_MX)
1244 return -1; /* no space */
1245
1246 if (ctxp->off < ctxp->startoff) {
1247 ctxp->off++;
1248 return 0;
1249 }
1250
1251 if (kauth_authorize_process(kauth_cred_get(),
1252 KAUTH_PROCESS_CANSEE, p,
1253 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL) != 0)
1254 return 0;
1255
1256 memset(&d, 0, UIO_MX);
1257 d.d_reclen = UIO_MX;
1258 d.d_fileno = PROCFS_FILENO(p->p_pid, PFSproc, -1);
1259 d.d_namlen = snprintf(d.d_name,
1260 UIO_MX - offsetof(struct dirent, d_name), "%ld", (long)p->p_pid);
1261 d.d_type = DT_DIR;
1262
1263 mutex_exit(proc_lock);
1264 error = uiomove(&d, UIO_MX, uiop);
1265 mutex_enter(proc_lock);
1266 if (error) {
1267 ctxp->error = error;
1268 return -1;
1269 }
1270
1271 ctxp->ncookies++;
1272 if (ctxp->cookies)
1273 *(ctxp->cookies)++ = ctxp->off + 1;
1274 ctxp->off++;
1275
1276 return 0;
1277 }
1278
1279 /*
1280 * readdir returns directory entries from pfsnode (vp).
1281 *
1282 * the strategy here with procfs is to generate a single
1283 * directory entry at a time (struct dirent) and then
1284 * copy that out to userland using uiomove. a more efficent
1285 * though more complex implementation, would try to minimize
1286 * the number of calls to uiomove(). for procfs, this is
1287 * hardly worth the added code complexity.
1288 *
1289 * this should just be done through read()
1290 */
1291 int
1292 procfs_readdir(void *v)
1293 {
1294 struct vop_readdir_args /* {
1295 struct vnode *a_vp;
1296 struct uio *a_uio;
1297 kauth_cred_t a_cred;
1298 int *a_eofflag;
1299 off_t **a_cookies;
1300 int *a_ncookies;
1301 } */ *ap = v;
1302 struct uio *uio = ap->a_uio;
1303 struct dirent d;
1304 struct pfsnode *pfs;
1305 off_t i;
1306 int error;
1307 off_t *cookies = NULL;
1308 int ncookies;
1309 struct vnode *vp;
1310 const struct proc_target *pt;
1311 struct procfs_root_readdir_ctx ctx;
1312 struct lwp *l;
1313 int nfd;
1314
1315 vp = ap->a_vp;
1316 pfs = VTOPFS(vp);
1317
1318 if (uio->uio_resid < UIO_MX)
1319 return (EINVAL);
1320 if (uio->uio_offset < 0)
1321 return (EINVAL);
1322
1323 error = 0;
1324 i = uio->uio_offset;
1325 memset(&d, 0, UIO_MX);
1326 d.d_reclen = UIO_MX;
1327 ncookies = uio->uio_resid / UIO_MX;
1328
1329 switch (pfs->pfs_type) {
1330 /*
1331 * this is for the process-specific sub-directories.
1332 * all that is needed to is copy out all the entries
1333 * from the procent[] table (top of this file).
1334 */
1335 case PFSproc: {
1336 struct proc *p;
1337
1338 if (i >= nproc_targets)
1339 return 0;
1340
1341 if (procfs_proc_lock(vp->v_mount, pfs->pfs_pid, &p, ESRCH) != 0)
1342 break;
1343
1344 if (ap->a_ncookies) {
1345 ncookies = uimin(ncookies, (nproc_targets - i));
1346 cookies = malloc(ncookies * sizeof (off_t),
1347 M_TEMP, M_WAITOK);
1348 *ap->a_cookies = cookies;
1349 }
1350
1351 for (pt = &proc_targets[i];
1352 uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
1353 if (pt->pt_valid) {
1354 /* XXXSMP LWP can disappear */
1355 mutex_enter(p->p_lock);
1356 l = LIST_FIRST(&p->p_lwps);
1357 KASSERT(l != NULL);
1358 mutex_exit(p->p_lock);
1359 if ((*pt->pt_valid)(l, vp->v_mount) == 0)
1360 continue;
1361 }
1362
1363 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
1364 pt->pt_pfstype, -1);
1365 d.d_namlen = pt->pt_namlen;
1366 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1367 d.d_type = pt->pt_type;
1368
1369 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1370 break;
1371 if (cookies)
1372 *cookies++ = i + 1;
1373 }
1374
1375 procfs_proc_unlock(p);
1376 break;
1377 }
1378 case PFSfd: {
1379 struct proc *p;
1380 file_t *fp;
1381 int lim, nc = 0;
1382
1383 if ((error = procfs_proc_lock(vp->v_mount, pfs->pfs_pid, &p,
1384 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 = atomic_load_consume(&p->p_fd->fd_dt)->dt_nfiles;
1397
1398 lim = uimin((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 = uimin(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(vp->v_mount, pfs->pfs_pid, &p,
1453 ESRCH)) != 0)
1454 return error;
1455
1456 nfd = 3; /* ., .., pid */
1457
1458 if (ap->a_ncookies) {
1459 ncookies = uimin(ncookies, (nfd + 2 - i));
1460 cookies = malloc(ncookies * sizeof (off_t),
1461 M_TEMP, M_WAITOK);
1462 *ap->a_cookies = cookies;
1463 }
1464
1465 for (; i < 2 && uio->uio_resid >= UIO_MX; i++) {
1466 pt = &proc_targets[i];
1467 d.d_namlen = pt->pt_namlen;
1468 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
1469 pt->pt_pfstype, -1);
1470 (void)memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1471 d.d_type = pt->pt_type;
1472 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1473 break;
1474 if (cookies)
1475 *cookies++ = i + 1;
1476 nc++;
1477 }
1478 if (error) {
1479 ncookies = nc;
1480 break;
1481 }
1482 for (; uio->uio_resid >= UIO_MX && i < nfd; i++) {
1483 /* check the descriptor exists */
1484 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, PFStask,
1485 i - 2);
1486 d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
1487 "%ld", (long)pfs->pfs_pid);
1488 d.d_type = DT_LNK;
1489 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1490 break;
1491 if (cookies)
1492 *cookies++ = i + 1;
1493 nc++;
1494 }
1495 ncookies = nc;
1496 procfs_proc_unlock(p);
1497 break;
1498 }
1499
1500 /*
1501 * this is for the root of the procfs filesystem
1502 * what is needed are special entries for "curproc"
1503 * and "self" followed by an entry for each process
1504 * on allproc.
1505 */
1506
1507 case PFSroot: {
1508 int nc = 0;
1509
1510 if (ap->a_ncookies) {
1511 /*
1512 * XXX Potentially allocating too much space here,
1513 * but I'm lazy. This loop needs some work.
1514 */
1515 cookies = malloc(ncookies * sizeof (off_t),
1516 M_TEMP, M_WAITOK);
1517 *ap->a_cookies = cookies;
1518 }
1519 error = 0;
1520 /* 0 ... 3 are static entries. */
1521 for (; i <= 3 && uio->uio_resid >= UIO_MX; i++) {
1522 switch (i) {
1523 case 0: /* `.' */
1524 case 1: /* `..' */
1525 d.d_fileno = PROCFS_FILENO(0, PFSroot, -1);
1526 d.d_namlen = i + 1;
1527 memcpy(d.d_name, "..", d.d_namlen);
1528 d.d_name[i + 1] = '\0';
1529 d.d_type = DT_DIR;
1530 break;
1531
1532 case 2:
1533 d.d_fileno = PROCFS_FILENO(0, PFScurproc, -1);
1534 d.d_namlen = sizeof("curproc") - 1;
1535 memcpy(d.d_name, "curproc", sizeof("curproc"));
1536 d.d_type = DT_LNK;
1537 break;
1538
1539 case 3:
1540 d.d_fileno = PROCFS_FILENO(0, PFSself, -1);
1541 d.d_namlen = sizeof("self") - 1;
1542 memcpy(d.d_name, "self", sizeof("self"));
1543 d.d_type = DT_LNK;
1544 break;
1545 }
1546
1547 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1548 break;
1549 nc++;
1550 if (cookies)
1551 *cookies++ = i + 1;
1552 }
1553 /* 4 ... are process entries. */
1554 ctx.uiop = uio;
1555 ctx.error = 0;
1556 ctx.off = 4;
1557 ctx.startoff = i;
1558 ctx.cookies = cookies;
1559 ctx.ncookies = nc;
1560 proclist_foreach_call(&allproc,
1561 procfs_root_readdir_callback, &ctx);
1562 cookies = ctx.cookies;
1563 nc = ctx.ncookies;
1564 error = ctx.error;
1565 if (error)
1566 break;
1567
1568 /* misc entries. */
1569 if (i < ctx.off)
1570 i = ctx.off;
1571 if (i >= ctx.off + nproc_root_targets)
1572 break;
1573 for (pt = &proc_root_targets[i - ctx.off];
1574 uio->uio_resid >= UIO_MX &&
1575 pt < &proc_root_targets[nproc_root_targets];
1576 pt++, i++) {
1577 if (pt->pt_valid &&
1578 (*pt->pt_valid)(NULL, vp->v_mount) == 0)
1579 continue;
1580 d.d_fileno = PROCFS_FILENO(0, pt->pt_pfstype, -1);
1581 d.d_namlen = pt->pt_namlen;
1582 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1583 d.d_type = pt->pt_type;
1584
1585 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1586 break;
1587 nc++;
1588 if (cookies)
1589 *cookies++ = i + 1;
1590 }
1591
1592 ncookies = nc;
1593 break;
1594 }
1595
1596 default:
1597 error = ENOTDIR;
1598 break;
1599 }
1600
1601 if (ap->a_ncookies) {
1602 if (error) {
1603 if (cookies)
1604 free(*ap->a_cookies, M_TEMP);
1605 *ap->a_ncookies = 0;
1606 *ap->a_cookies = NULL;
1607 } else
1608 *ap->a_ncookies = ncookies;
1609 }
1610 uio->uio_offset = i;
1611 return (error);
1612 }
1613
1614 /*
1615 * readlink reads the link of `curproc' and others
1616 */
1617 int
1618 procfs_readlink(void *v)
1619 {
1620 struct vop_readlink_args *ap = v;
1621 char bf[16]; /* should be enough */
1622 char *bp = bf;
1623 char *path = NULL;
1624 int len = 0;
1625 int error = 0;
1626 struct vnode *vp = ap->a_vp;
1627 struct pfsnode *pfs = VTOPFS(vp);
1628 struct proc *pown = NULL;
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, PFSexe, -1)) {
1637 if ((error = procfs_proc_lock(vp->v_mount, pfs->pfs_pid, &pown,
1638 ESRCH)) != 0)
1639 return error;
1640 bp = pown->p_path;
1641 len = strlen(bp);
1642 } else if (pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFScwd, -1) ||
1643 pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFSchroot, -1)) {
1644 if ((error = procfs_proc_lock(vp->v_mount, pfs->pfs_pid, &pown,
1645 ESRCH)) != 0)
1646 return error;
1647 path = malloc(MAXPATHLEN + 4, M_TEMP, M_WAITOK);
1648 if (path == NULL) {
1649 procfs_proc_unlock(pown);
1650 return (ENOMEM);
1651 }
1652 bp = path + MAXPATHLEN;
1653 *--bp = '\0';
1654 procfs_dir(PROCFS_TYPE(pfs->pfs_fileno), curlwp, pown,
1655 &bp, path, MAXPATHLEN);
1656 len = strlen(bp);
1657 } else {
1658 file_t *fp;
1659 struct vnode *vxp;
1660
1661 if ((error = procfs_proc_lock(vp->v_mount, pfs->pfs_pid, &pown,
1662 ESRCH)) != 0)
1663 return error;
1664
1665 fp = fd_getfile2(pown, pfs->pfs_fd);
1666 if (fp == NULL) {
1667 procfs_proc_unlock(pown);
1668 return EBADF;
1669 }
1670
1671 switch (fp->f_type) {
1672 case DTYPE_VNODE:
1673 vxp = fp->f_vnode;
1674 if (vxp->v_type != VDIR) {
1675 error = EINVAL;
1676 break;
1677 }
1678 if ((path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK))
1679 == NULL) {
1680 error = ENOMEM;
1681 break;
1682 }
1683 bp = path + MAXPATHLEN;
1684 *--bp = '\0';
1685
1686 /*
1687 * XXX: kludge to avoid locking against ourselves
1688 * in getcwd()
1689 */
1690 if (vxp->v_tag == VT_PROCFS) {
1691 *--bp = '/';
1692 } else {
1693 rw_enter(&curproc->p_cwdi->cwdi_lock,
1694 RW_READER);
1695 vp = curproc->p_cwdi->cwdi_rdir;
1696 if (vp == NULL)
1697 vp = rootvnode;
1698 error = getcwd_common(vxp, vp, &bp, path,
1699 MAXPATHLEN / 2, 0, curlwp);
1700 rw_exit(&curproc->p_cwdi->cwdi_lock);
1701 }
1702 if (error)
1703 break;
1704 len = strlen(bp);
1705 break;
1706
1707 case DTYPE_MISC:
1708 len = snprintf(bf, sizeof(bf), "%s", "[misc]");
1709 break;
1710
1711 case DTYPE_KQUEUE:
1712 len = snprintf(bf, sizeof(bf), "%s", "[kqueue]");
1713 break;
1714
1715 case DTYPE_SEM:
1716 len = snprintf(bf, sizeof(bf), "%s", "[ksem]");
1717 break;
1718
1719 default:
1720 error = EINVAL;
1721 break;
1722 }
1723 closef(fp);
1724 }
1725
1726 if (error == 0)
1727 error = uiomove(bp, len, ap->a_uio);
1728 if (pown)
1729 procfs_proc_unlock(pown);
1730 if (path)
1731 free(path, M_TEMP);
1732 return error;
1733 }
1734
1735 int
1736 procfs_getpages(void *v)
1737 {
1738 struct vop_getpages_args /* {
1739 struct vnode *a_vp;
1740 voff_t a_offset;
1741 struct vm_page **a_m;
1742 int *a_count;
1743 int a_centeridx;
1744 vm_prot_t a_access_type;
1745 int a_advice;
1746 int a_flags;
1747 } */ *ap = v;
1748
1749 if ((ap->a_flags & PGO_LOCKED) == 0)
1750 rw_exit(ap->a_vp->v_uobj.vmobjlock);
1751
1752 return (EFAULT);
1753 }
1754
1755 /*
1756 * convert decimal ascii to int
1757 */
1758 static int
1759 atoi(const char *b, size_t len)
1760 {
1761 int p = 0;
1762
1763 while (len--) {
1764 char c = *b++;
1765 if (c < '0' || c > '9')
1766 return -1;
1767 p = 10 * p + (c - '0');
1768 }
1769
1770 return p;
1771 }
1772