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