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