procfs_vnops.c revision 1.197 1 /* $NetBSD: procfs_vnops.c,v 1.197 2017/05/26 14:21:01 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1993, 1995
34 * The Regents of the University of California. All rights reserved.
35 *
36 * This code is derived from software contributed to Berkeley by
37 * Jan-Simon Pendry.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 * @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95
64 */
65
66 /*
67 * Copyright (c) 1993 Jan-Simon Pendry
68 *
69 * This code is derived from software contributed to Berkeley by
70 * Jan-Simon Pendry.
71 *
72 * Redistribution and use in source and binary forms, with or without
73 * modification, are permitted provided that the following conditions
74 * are met:
75 * 1. Redistributions of source code must retain the above copyright
76 * notice, this list of conditions and the following disclaimer.
77 * 2. Redistributions in binary form must reproduce the above copyright
78 * notice, this list of conditions and the following disclaimer in the
79 * documentation and/or other materials provided with the distribution.
80 * 3. All advertising materials mentioning features or use of this software
81 * must display the following acknowledgement:
82 * This product includes software developed by the University of
83 * California, Berkeley and its contributors.
84 * 4. Neither the name of the University nor the names of its contributors
85 * may be used to endorse or promote products derived from this software
86 * without specific prior written permission.
87 *
88 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
89 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
90 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
91 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
92 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
93 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
94 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
95 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
96 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
97 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
98 * SUCH DAMAGE.
99 *
100 * @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95
101 */
102
103 /*
104 * procfs vnode interface
105 */
106
107 #include <sys/cdefs.h>
108 __KERNEL_RCSID(0, "$NetBSD: procfs_vnops.c,v 1.197 2017/05/26 14:21:01 riastradh Exp $");
109
110 #include <sys/param.h>
111 #include <sys/systm.h>
112 #include <sys/time.h>
113 #include <sys/kernel.h>
114 #include <sys/file.h>
115 #include <sys/filedesc.h>
116 #include <sys/proc.h>
117 #include <sys/vnode.h>
118 #include <sys/namei.h>
119 #include <sys/malloc.h>
120 #include <sys/mount.h>
121 #include <sys/dirent.h>
122 #include <sys/resourcevar.h>
123 #include <sys/stat.h>
124 #include <sys/ptrace.h>
125 #include <sys/kauth.h>
126 #include <sys/exec.h>
127
128 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
129
130 #include <machine/reg.h>
131
132 #include <miscfs/genfs/genfs.h>
133 #include <miscfs/procfs/procfs.h>
134
135 /*
136 * Vnode Operations.
137 *
138 */
139
140 static int procfs_validfile_linux(struct lwp *, struct mount *);
141 static int procfs_root_readdir_callback(struct proc *, void *);
142 static void procfs_dir(pfstype, struct lwp *, struct proc *, char **, char *,
143 size_t);
144
145 /*
146 * This is a list of the valid names in the
147 * process-specific sub-directories. It is
148 * used in procfs_lookup and procfs_readdir
149 */
150 static const struct proc_target {
151 u_char pt_type;
152 u_char pt_namlen;
153 const char *pt_name;
154 pfstype pt_pfstype;
155 int (*pt_valid)(struct lwp *, struct mount *);
156 } proc_targets[] = {
157 #define N(s) sizeof(s)-1, s
158 /* name type validp */
159 { DT_DIR, N("."), PFSproc, NULL },
160 { DT_DIR, N(".."), PFSroot, NULL },
161 { DT_DIR, N("fd"), PFSfd, NULL },
162 { DT_REG, N("file"), PFSfile, procfs_validfile },
163 { DT_REG, N("auxv"), PFSauxv, procfs_validauxv },
164 { DT_REG, N("mem"), PFSmem, NULL },
165 { DT_REG, N("regs"), PFSregs, procfs_validregs },
166 { DT_REG, N("fpregs"), PFSfpregs, procfs_validfpregs },
167 { DT_REG, N("ctl"), PFSctl, NULL },
168 { DT_REG, N("stat"), PFSstat, procfs_validfile_linux },
169 { DT_REG, N("status"), PFSstatus, NULL },
170 { DT_REG, N("note"), PFSnote, NULL },
171 { DT_REG, N("notepg"), PFSnotepg, NULL },
172 { DT_REG, N("map"), PFSmap, procfs_validmap },
173 { DT_REG, N("maps"), PFSmaps, procfs_validmap },
174 { DT_REG, N("cmdline"), PFScmdline, NULL },
175 { DT_REG, N("exe"), PFSexe, procfs_validfile },
176 { DT_LNK, N("cwd"), PFScwd, NULL },
177 { DT_LNK, N("root"), PFSchroot, NULL },
178 { DT_LNK, N("emul"), PFSemul, NULL },
179 { DT_REG, N("statm"), PFSstatm, procfs_validfile_linux },
180 { DT_DIR, N("task"), PFStask, procfs_validfile_linux },
181 #ifdef __HAVE_PROCFS_MACHDEP
182 PROCFS_MACHDEP_NODETYPE_DEFNS
183 #endif
184 #undef N
185 };
186 static const int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]);
187
188 /*
189 * List of files in the root directory. Note: the validate function will
190 * be called with p == NULL for these ones.
191 */
192 static const struct proc_target proc_root_targets[] = {
193 #define N(s) sizeof(s)-1, s
194 /* name type validp */
195 { DT_REG, N("meminfo"), PFSmeminfo, procfs_validfile_linux },
196 { DT_REG, N("cpuinfo"), PFScpuinfo, procfs_validfile_linux },
197 { DT_REG, N("uptime"), PFSuptime, procfs_validfile_linux },
198 { DT_REG, N("mounts"), PFSmounts, procfs_validfile_linux },
199 { DT_REG, N("devices"), PFSdevices, procfs_validfile_linux },
200 { DT_REG, N("stat"), PFScpustat, procfs_validfile_linux },
201 { DT_REG, N("loadavg"), PFSloadavg, procfs_validfile_linux },
202 { DT_REG, N("version"), PFSversion, procfs_validfile_linux },
203 #undef N
204 };
205 static const int nproc_root_targets =
206 sizeof(proc_root_targets) / sizeof(proc_root_targets[0]);
207
208 int procfs_lookup(void *);
209 #define procfs_create genfs_eopnotsupp
210 #define procfs_mknod genfs_eopnotsupp
211 int procfs_open(void *);
212 int procfs_close(void *);
213 int procfs_access(void *);
214 int procfs_getattr(void *);
215 int procfs_setattr(void *);
216 #define procfs_read procfs_rw
217 #define procfs_write procfs_rw
218 #define procfs_fcntl genfs_fcntl
219 #define procfs_ioctl genfs_enoioctl
220 #define procfs_poll genfs_poll
221 #define procfs_revoke genfs_revoke
222 #define procfs_fsync genfs_nullop
223 #define procfs_seek genfs_nullop
224 #define procfs_remove genfs_eopnotsupp
225 int procfs_link(void *);
226 #define procfs_rename genfs_eopnotsupp
227 #define procfs_mkdir genfs_eopnotsupp
228 #define procfs_rmdir genfs_eopnotsupp
229 int procfs_symlink(void *);
230 int procfs_readdir(void *);
231 int procfs_readlink(void *);
232 #define procfs_abortop genfs_abortop
233 int procfs_inactive(void *);
234 int procfs_reclaim(void *);
235 #define procfs_lock genfs_lock
236 #define procfs_unlock genfs_unlock
237 #define procfs_bmap genfs_badop
238 #define procfs_strategy genfs_badop
239 int procfs_print(void *);
240 int procfs_pathconf(void *);
241 #define procfs_islocked genfs_islocked
242 #define procfs_advlock genfs_einval
243 #define procfs_bwrite genfs_eopnotsupp
244 #define procfs_putpages genfs_null_putpages
245
246 static int atoi(const char *, size_t);
247
248 /*
249 * procfs vnode operations.
250 */
251 int (**procfs_vnodeop_p)(void *);
252 const struct vnodeopv_entry_desc procfs_vnodeop_entries[] = {
253 { &vop_default_desc, vn_default_error },
254 { &vop_lookup_desc, procfs_lookup }, /* lookup */
255 { &vop_create_desc, procfs_create }, /* create */
256 { &vop_mknod_desc, procfs_mknod }, /* mknod */
257 { &vop_open_desc, procfs_open }, /* open */
258 { &vop_close_desc, procfs_close }, /* close */
259 { &vop_access_desc, procfs_access }, /* access */
260 { &vop_getattr_desc, procfs_getattr }, /* getattr */
261 { &vop_setattr_desc, procfs_setattr }, /* setattr */
262 { &vop_read_desc, procfs_read }, /* read */
263 { &vop_write_desc, procfs_write }, /* write */
264 { &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */
265 { &vop_fdiscard_desc, genfs_eopnotsupp }, /* fdiscard */
266 { &vop_fcntl_desc, procfs_fcntl }, /* fcntl */
267 { &vop_ioctl_desc, procfs_ioctl }, /* ioctl */
268 { &vop_poll_desc, procfs_poll }, /* poll */
269 { &vop_revoke_desc, procfs_revoke }, /* revoke */
270 { &vop_fsync_desc, procfs_fsync }, /* fsync */
271 { &vop_seek_desc, procfs_seek }, /* seek */
272 { &vop_remove_desc, procfs_remove }, /* remove */
273 { &vop_link_desc, procfs_link }, /* link */
274 { &vop_rename_desc, procfs_rename }, /* rename */
275 { &vop_mkdir_desc, procfs_mkdir }, /* mkdir */
276 { &vop_rmdir_desc, procfs_rmdir }, /* rmdir */
277 { &vop_symlink_desc, procfs_symlink }, /* symlink */
278 { &vop_readdir_desc, procfs_readdir }, /* readdir */
279 { &vop_readlink_desc, procfs_readlink }, /* readlink */
280 { &vop_abortop_desc, procfs_abortop }, /* abortop */
281 { &vop_inactive_desc, procfs_inactive }, /* inactive */
282 { &vop_reclaim_desc, procfs_reclaim }, /* reclaim */
283 { &vop_lock_desc, procfs_lock }, /* lock */
284 { &vop_unlock_desc, procfs_unlock }, /* unlock */
285 { &vop_bmap_desc, procfs_bmap }, /* bmap */
286 { &vop_strategy_desc, procfs_strategy }, /* strategy */
287 { &vop_print_desc, procfs_print }, /* print */
288 { &vop_islocked_desc, procfs_islocked }, /* islocked */
289 { &vop_pathconf_desc, procfs_pathconf }, /* pathconf */
290 { &vop_advlock_desc, procfs_advlock }, /* advlock */
291 { &vop_putpages_desc, procfs_putpages }, /* putpages */
292 { NULL, NULL }
293 };
294 const struct vnodeopv_desc procfs_vnodeop_opv_desc =
295 { &procfs_vnodeop_p, procfs_vnodeop_entries };
296 /*
297 * set things up for doing i/o on
298 * the pfsnode (vp). (vp) is locked
299 * on entry, and should be left locked
300 * on exit.
301 *
302 * for procfs we don't need to do anything
303 * in particular for i/o. all that is done
304 * is to support exclusive open on process
305 * memory images.
306 */
307 int
308 procfs_open(void *v)
309 {
310 struct vop_open_args /* {
311 struct vnode *a_vp;
312 int a_mode;
313 kauth_cred_t a_cred;
314 } */ *ap = v;
315 struct pfsnode *pfs = VTOPFS(ap->a_vp);
316 struct lwp *l1;
317 struct proc *p2;
318 int error;
319
320 if ((error = procfs_proc_lock(pfs->pfs_pid, &p2, ENOENT)) != 0)
321 return error;
322
323 l1 = curlwp; /* tracer */
324
325 #define M2K(m) (((m) & FREAD) && ((m) & FWRITE) ? \
326 KAUTH_REQ_PROCESS_PROCFS_RW : \
327 (m) & FWRITE ? KAUTH_REQ_PROCESS_PROCFS_WRITE : \
328 KAUTH_REQ_PROCESS_PROCFS_READ)
329
330 mutex_enter(p2->p_lock);
331 error = kauth_authorize_process(l1->l_cred, KAUTH_PROCESS_PROCFS,
332 p2, pfs, KAUTH_ARG(M2K(ap->a_mode)), NULL);
333 mutex_exit(p2->p_lock);
334 if (error) {
335 procfs_proc_unlock(p2);
336 return (error);
337 }
338
339 #undef M2K
340
341 switch (pfs->pfs_type) {
342 case PFSmem:
343 if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) ||
344 ((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE))) {
345 error = EBUSY;
346 break;
347 }
348
349 if (!proc_isunder(p2, l1)) {
350 error = EPERM;
351 break;
352 }
353
354 if (ap->a_mode & FWRITE)
355 pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
356
357 break;
358
359 case PFSregs:
360 case PFSfpregs:
361 if (!proc_isunder(p2, l1)) {
362 error = EPERM;
363 break;
364 }
365 break;
366
367 default:
368 break;
369 }
370
371 procfs_proc_unlock(p2);
372 return (error);
373 }
374
375 /*
376 * close the pfsnode (vp) after doing i/o.
377 * (vp) is not locked on entry or exit.
378 *
379 * nothing to do for procfs other than undo
380 * any exclusive open flag (see _open above).
381 */
382 int
383 procfs_close(void *v)
384 {
385 struct vop_close_args /* {
386 struct vnode *a_vp;
387 int a_fflag;
388 kauth_cred_t a_cred;
389 } */ *ap = v;
390 struct pfsnode *pfs = VTOPFS(ap->a_vp);
391
392 switch (pfs->pfs_type) {
393 case PFSmem:
394 if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
395 pfs->pfs_flags &= ~(FWRITE|O_EXCL);
396 break;
397
398 default:
399 break;
400 }
401
402 return (0);
403 }
404
405 /*
406 * _inactive is called when the pfsnode
407 * is vrele'd and the reference count goes
408 * to zero. (vp) will be on the vnode free
409 * list, so to get it back vget() must be
410 * used.
411 *
412 * (vp) is locked on entry, but must be unlocked on exit.
413 */
414 int
415 procfs_inactive(void *v)
416 {
417 struct vop_inactive_v2_args /* {
418 struct vnode *a_vp;
419 bool *a_recycle;
420 } */ *ap = v;
421 struct vnode *vp = ap->a_vp;
422 struct pfsnode *pfs = VTOPFS(vp);
423
424 mutex_enter(proc_lock);
425 *ap->a_recycle = (proc_find(pfs->pfs_pid) == NULL);
426 mutex_exit(proc_lock);
427
428 return (0);
429 }
430
431 /*
432 * _reclaim is called when getnewvnode()
433 * wants to make use of an entry on the vnode
434 * free list. at this time the filesystem needs
435 * to free any private data and remove the node
436 * from any private lists.
437 */
438 int
439 procfs_reclaim(void *v)
440 {
441 struct vop_reclaim_v2_args /* {
442 struct vnode *a_vp;
443 } */ *ap = v;
444 struct vnode *vp = ap->a_vp;
445 struct pfsnode *pfs = VTOPFS(vp);
446
447 VOP_UNLOCK(vp);
448
449 /*
450 * To interlock with procfs_revoke_vnodes().
451 */
452 mutex_enter(vp->v_interlock);
453 vp->v_data = NULL;
454 mutex_exit(vp->v_interlock);
455 kmem_free(pfs, sizeof(*pfs));
456 return 0;
457 }
458
459 /*
460 * Return POSIX pathconf information applicable to special devices.
461 */
462 int
463 procfs_pathconf(void *v)
464 {
465 struct vop_pathconf_args /* {
466 struct vnode *a_vp;
467 int a_name;
468 register_t *a_retval;
469 } */ *ap = v;
470
471 switch (ap->a_name) {
472 case _PC_LINK_MAX:
473 *ap->a_retval = LINK_MAX;
474 return (0);
475 case _PC_MAX_CANON:
476 *ap->a_retval = MAX_CANON;
477 return (0);
478 case _PC_MAX_INPUT:
479 *ap->a_retval = MAX_INPUT;
480 return (0);
481 case _PC_PIPE_BUF:
482 *ap->a_retval = PIPE_BUF;
483 return (0);
484 case _PC_CHOWN_RESTRICTED:
485 *ap->a_retval = 1;
486 return (0);
487 case _PC_VDISABLE:
488 *ap->a_retval = _POSIX_VDISABLE;
489 return (0);
490 case _PC_SYNC_IO:
491 *ap->a_retval = 1;
492 return (0);
493 default:
494 return (EINVAL);
495 }
496 /* NOTREACHED */
497 }
498
499 /*
500 * _print is used for debugging.
501 * just print a readable description
502 * of (vp).
503 */
504 int
505 procfs_print(void *v)
506 {
507 struct vop_print_args /* {
508 struct vnode *a_vp;
509 } */ *ap = v;
510 struct pfsnode *pfs = VTOPFS(ap->a_vp);
511
512 printf("tag VT_PROCFS, type %d, pid %d, mode %x, flags %lx\n",
513 pfs->pfs_type, pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
514 return 0;
515 }
516
517 int
518 procfs_link(void *v)
519 {
520 struct vop_link_v2_args /* {
521 struct vnode *a_dvp;
522 struct vnode *a_vp;
523 struct componentname *a_cnp;
524 } */ *ap = v;
525
526 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
527 return (EROFS);
528 }
529
530 int
531 procfs_symlink(void *v)
532 {
533 struct vop_symlink_v3_args /* {
534 struct vnode *a_dvp;
535 struct vnode **a_vpp;
536 struct componentname *a_cnp;
537 struct vattr *a_vap;
538 char *a_target;
539 } */ *ap = v;
540
541 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
542 return (EROFS);
543 }
544
545 /*
546 * Works out the path to (and vnode of) the target process's current
547 * working directory or chroot. If the caller is in a chroot and
548 * can't "reach" the target's cwd or root (or some other error
549 * occurs), a "/" is returned for the path and a NULL pointer is
550 * returned for the vnode.
551 */
552 static void
553 procfs_dir(pfstype t, struct lwp *caller, struct proc *target, char **bpp,
554 char *path, size_t len)
555 {
556 struct cwdinfo *cwdi;
557 struct vnode *vp, *rvp;
558 char *bp;
559
560 cwdi = caller->l_proc->p_cwdi;
561 rw_enter(&cwdi->cwdi_lock, RW_READER);
562
563 rvp = cwdi->cwdi_rdir;
564 bp = bpp ? *bpp : NULL;
565
566 switch (t) {
567 case PFScwd:
568 vp = target->p_cwdi->cwdi_cdir;
569 break;
570 case PFSchroot:
571 vp = target->p_cwdi->cwdi_rdir;
572 break;
573 case PFSexe:
574 vp = target->p_textvp;
575 break;
576 default:
577 rw_exit(&cwdi->cwdi_lock);
578 return;
579 }
580
581 /*
582 * XXX: this horrible kludge avoids locking panics when
583 * attempting to lookup links that point to within procfs
584 */
585 if (vp != NULL && vp->v_tag == VT_PROCFS) {
586 if (bpp) {
587 *--bp = '/';
588 *bpp = bp;
589 }
590 rw_exit(&cwdi->cwdi_lock);
591 return;
592 }
593
594 if (rvp == NULL)
595 rvp = rootvnode;
596 if (vp == NULL || getcwd_common(vp, rvp, bp ? &bp : NULL, path,
597 len / 2, 0, caller) != 0) {
598 vp = NULL;
599 if (bpp) {
600 /*
601 if (t == PFSexe) {
602 snprintf(path, len, "%s/%d/file"
603 mp->mnt_stat.f_mntonname, pfs->pfs_pid);
604 } else */ {
605 bp = *bpp;
606 *--bp = '/';
607 }
608 }
609 }
610
611 if (bpp)
612 *bpp = bp;
613
614 rw_exit(&cwdi->cwdi_lock);
615 }
616
617 /*
618 * Invent attributes for pfsnode (vp) and store
619 * them in (vap).
620 * Directories lengths are returned as zero since
621 * any real length would require the genuine size
622 * to be computed, and nothing cares anyway.
623 *
624 * this is relatively minimal for procfs.
625 */
626 int
627 procfs_getattr(void *v)
628 {
629 struct vop_getattr_args /* {
630 struct vnode *a_vp;
631 struct vattr *a_vap;
632 kauth_cred_t a_cred;
633 } */ *ap = v;
634 struct pfsnode *pfs = VTOPFS(ap->a_vp);
635 struct vattr *vap = ap->a_vap;
636 struct proc *procp;
637 char *path;
638 int error;
639
640 /* first check the process still exists */
641 switch (pfs->pfs_type) {
642 case PFSroot:
643 case PFScurproc:
644 case PFSself:
645 procp = NULL;
646 break;
647
648 default:
649 error = procfs_proc_lock(pfs->pfs_pid, &procp, ENOENT);
650 if (error != 0)
651 return (error);
652 break;
653 }
654
655 switch (pfs->pfs_type) {
656 case PFStask:
657 if (pfs->pfs_fd == -1) {
658 path = NULL;
659 break;
660 }
661 /*FALLTHROUGH*/
662 case PFScwd:
663 case PFSchroot:
664 case PFSexe:
665 path = malloc(MAXPATHLEN + 4, M_TEMP, M_WAITOK|M_CANFAIL);
666 if (path == NULL && procp != NULL) {
667 procfs_proc_unlock(procp);
668 return (ENOMEM);
669 }
670 break;
671
672 default:
673 path = NULL;
674 break;
675 }
676
677 if (procp != NULL) {
678 mutex_enter(procp->p_lock);
679 error = kauth_authorize_process(kauth_cred_get(),
680 KAUTH_PROCESS_CANSEE, procp,
681 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
682 mutex_exit(procp->p_lock);
683 if (error != 0) {
684 procfs_proc_unlock(procp);
685 if (path != NULL)
686 free(path, M_TEMP);
687 return (ENOENT);
688 }
689 }
690
691 error = 0;
692
693 /* start by zeroing out the attributes */
694 vattr_null(vap);
695
696 /* next do all the common fields */
697 vap->va_type = ap->a_vp->v_type;
698 vap->va_mode = pfs->pfs_mode;
699 vap->va_fileid = pfs->pfs_fileno;
700 vap->va_flags = 0;
701 vap->va_blocksize = PAGE_SIZE;
702
703 /*
704 * Make all times be current TOD.
705 *
706 * It would be possible to get the process start
707 * time from the p_stats structure, but there's
708 * no "file creation" time stamp anyway, and the
709 * p_stats structure is not addressable if u. gets
710 * swapped out for that process.
711 */
712 getnanotime(&vap->va_ctime);
713 vap->va_atime = vap->va_mtime = vap->va_ctime;
714 if (procp)
715 TIMEVAL_TO_TIMESPEC(&procp->p_stats->p_start,
716 &vap->va_birthtime);
717 else
718 getnanotime(&vap->va_birthtime);
719
720 switch (pfs->pfs_type) {
721 case PFSmem:
722 case PFSregs:
723 case PFSfpregs:
724 #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
725 PROCFS_MACHDEP_PROTECT_CASES
726 #endif
727 /*
728 * If the process has exercised some setuid or setgid
729 * privilege, then rip away read/write permission so
730 * that only root can gain access.
731 */
732 if (procp->p_flag & PK_SUGID)
733 vap->va_mode &= ~(S_IRUSR|S_IWUSR);
734 /* FALLTHROUGH */
735 case PFSctl:
736 case PFSstatus:
737 case PFSstat:
738 case PFSnote:
739 case PFSnotepg:
740 case PFScmdline:
741 case PFSemul:
742 case PFSstatm:
743
744 case PFSmap:
745 case PFSmaps:
746 case PFSauxv:
747 vap->va_nlink = 1;
748 vap->va_uid = kauth_cred_geteuid(procp->p_cred);
749 vap->va_gid = kauth_cred_getegid(procp->p_cred);
750 break;
751 case PFSmeminfo:
752 case PFSdevices:
753 case PFScpuinfo:
754 case PFSuptime:
755 case PFSmounts:
756 case PFScpustat:
757 case PFSloadavg:
758 case PFSversion:
759 vap->va_nlink = 1;
760 vap->va_uid = vap->va_gid = 0;
761 break;
762
763 case PFSproc:
764 case PFStask:
765 case PFSexe:
766 case PFSfile:
767 case PFSself:
768 case PFScurproc:
769 case PFScwd:
770 case PFSroot:
771 case PFSchroot:
772 case PFSfd:
773 break;
774
775 default:
776 panic("%s: %d/1", __func__, pfs->pfs_type);
777 }
778
779 /*
780 * now do the object specific fields
781 *
782 * The size could be set from struct reg, but it's hardly
783 * worth the trouble, and it puts some (potentially) machine
784 * dependent data into this machine-independent code. If it
785 * becomes important then this function should break out into
786 * a per-file stat function in the corresponding .c file.
787 */
788
789 switch (pfs->pfs_type) {
790 case PFSroot:
791 /*
792 * Set nlink to 1 to tell fts(3) we don't actually know.
793 */
794 vap->va_nlink = 1;
795 vap->va_uid = 0;
796 vap->va_gid = 0;
797 vap->va_bytes = vap->va_size = DEV_BSIZE;
798 break;
799
800 case PFSself:
801 case PFScurproc: {
802 char bf[16]; /* should be enough */
803 vap->va_nlink = 1;
804 vap->va_uid = 0;
805 vap->va_gid = 0;
806 vap->va_bytes = vap->va_size =
807 snprintf(bf, sizeof(bf), "%ld", (long)curproc->p_pid);
808 break;
809 }
810 case PFStask:
811 if (pfs->pfs_fd != -1) {
812 char bf[4]; /* should be enough */
813 vap->va_nlink = 1;
814 vap->va_uid = 0;
815 vap->va_gid = 0;
816 vap->va_bytes = vap->va_size =
817 snprintf(bf, sizeof(bf), "..");
818 break;
819 }
820 /*FALLTHROUGH*/
821 case PFSfd:
822 if (pfs->pfs_fd != -1) {
823 file_t *fp;
824
825 fp = fd_getfile2(procp, pfs->pfs_fd);
826 if (fp == NULL) {
827 error = EBADF;
828 break;
829 }
830 vap->va_nlink = 1;
831 vap->va_uid = kauth_cred_geteuid(fp->f_cred);
832 vap->va_gid = kauth_cred_getegid(fp->f_cred);
833 switch (fp->f_type) {
834 case DTYPE_VNODE:
835 vap->va_bytes = vap->va_size =
836 fp->f_vnode->v_size;
837 break;
838 default:
839 vap->va_bytes = vap->va_size = 0;
840 break;
841 }
842 closef(fp);
843 break;
844 }
845 /*FALLTHROUGH*/
846 case PFSproc:
847 vap->va_nlink = 2;
848 vap->va_uid = kauth_cred_geteuid(procp->p_cred);
849 vap->va_gid = kauth_cred_getegid(procp->p_cred);
850 vap->va_bytes = vap->va_size = DEV_BSIZE;
851 break;
852
853 case PFSfile:
854 error = EOPNOTSUPP;
855 break;
856
857 case PFSmem:
858 vap->va_bytes = vap->va_size =
859 ctob(procp->p_vmspace->vm_tsize +
860 procp->p_vmspace->vm_dsize +
861 procp->p_vmspace->vm_ssize);
862 break;
863
864 case PFSauxv:
865 vap->va_bytes = vap->va_size = procp->p_execsw->es_arglen;
866 break;
867
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 PFSctl:
882 case PFSstatus:
883 case PFSstat:
884 case PFSnote:
885 case PFSnotepg:
886 case PFScmdline:
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 PFSmap:
899 case PFSmaps:
900 /*
901 * Advise a larger blocksize for the map files, so that
902 * they may be read in one pass.
903 */
904 vap->va_blocksize = 4 * PAGE_SIZE;
905 vap->va_bytes = vap->va_size = 0;
906 break;
907
908 case PFScwd:
909 case PFSchroot:
910 case PFSexe: {
911 char *bp;
912
913 vap->va_nlink = 1;
914 vap->va_uid = 0;
915 vap->va_gid = 0;
916 bp = path + MAXPATHLEN;
917 *--bp = '\0';
918 procfs_dir(pfs->pfs_type, curlwp, procp, &bp, path,
919 MAXPATHLEN);
920 vap->va_bytes = vap->va_size = strlen(bp);
921 break;
922 }
923
924 case PFSemul:
925 vap->va_bytes = vap->va_size = strlen(procp->p_emul->e_name);
926 break;
927
928 #ifdef __HAVE_PROCFS_MACHDEP
929 PROCFS_MACHDEP_NODETYPE_CASES
930 error = procfs_machdep_getattr(ap->a_vp, vap, procp);
931 break;
932 #endif
933
934 default:
935 panic("%s: %d/2", __func__, pfs->pfs_type);
936 }
937
938 if (procp != NULL)
939 procfs_proc_unlock(procp);
940 if (path != NULL)
941 free(path, M_TEMP);
942
943 return (error);
944 }
945
946 /*ARGSUSED*/
947 int
948 procfs_setattr(void *v)
949 {
950 /*
951 * just fake out attribute setting
952 * it's not good to generate an error
953 * return, otherwise things like creat()
954 * will fail when they try to set the
955 * file length to 0. worse, this means
956 * that echo $note > /proc/$pid/note will fail.
957 */
958
959 return (0);
960 }
961
962 /*
963 * implement access checking.
964 *
965 * actually, the check for super-user is slightly
966 * broken since it will allow read access to write-only
967 * objects. this doesn't cause any particular trouble
968 * but does mean that the i/o entry points need to check
969 * that the operation really does make sense.
970 */
971 int
972 procfs_access(void *v)
973 {
974 struct vop_access_args /* {
975 struct vnode *a_vp;
976 int a_mode;
977 kauth_cred_t a_cred;
978 } */ *ap = v;
979 struct vattr va;
980 int error;
981
982 if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred)) != 0)
983 return (error);
984
985 return kauth_authorize_vnode(ap->a_cred,
986 KAUTH_ACCESS_ACTION(ap->a_mode, ap->a_vp->v_type, va.va_mode),
987 ap->a_vp, NULL, genfs_can_access(va.va_type, va.va_mode,
988 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
989 }
990
991 /*
992 * lookup. this is incredibly complicated in the
993 * general case, however for most pseudo-filesystems
994 * very little needs to be done.
995 *
996 * Locking isn't hard here, just poorly documented.
997 *
998 * If we're looking up ".", just vref the parent & return it.
999 *
1000 * If we're looking up "..", unlock the parent, and lock "..". If everything
1001 * went ok, and we're on the last component and the caller requested the
1002 * parent locked, try to re-lock the parent. We do this to prevent lock
1003 * races.
1004 *
1005 * For anything else, get the needed node. Then unlock the parent if not
1006 * the last component or not LOCKPARENT (i.e. if we wouldn't re-lock the
1007 * parent in the .. case).
1008 *
1009 * We try to exit with the parent locked in error cases.
1010 */
1011 int
1012 procfs_lookup(void *v)
1013 {
1014 struct vop_lookup_v2_args /* {
1015 struct vnode * a_dvp;
1016 struct vnode ** a_vpp;
1017 struct componentname * a_cnp;
1018 } */ *ap = v;
1019 struct componentname *cnp = ap->a_cnp;
1020 struct vnode **vpp = ap->a_vpp;
1021 struct vnode *dvp = ap->a_dvp;
1022 const char *pname = cnp->cn_nameptr;
1023 const struct proc_target *pt = NULL;
1024 struct vnode *fvp;
1025 pid_t pid, vnpid;
1026 struct pfsnode *pfs;
1027 struct proc *p = NULL;
1028 struct lwp *plwp;
1029 int i, error;
1030 pfstype type;
1031
1032 *vpp = NULL;
1033
1034 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
1035 return (EROFS);
1036
1037 if (cnp->cn_namelen == 1 && *pname == '.') {
1038 *vpp = dvp;
1039 vref(dvp);
1040 return (0);
1041 }
1042
1043 pfs = VTOPFS(dvp);
1044 switch (pfs->pfs_type) {
1045 case PFSroot:
1046 /*
1047 * Shouldn't get here with .. in the root node.
1048 */
1049 if (cnp->cn_flags & ISDOTDOT)
1050 return (EIO);
1051
1052 for (i = 0; i < nproc_root_targets; i++) {
1053 pt = &proc_root_targets[i];
1054 /*
1055 * check for node match. proc is always NULL here,
1056 * so call pt_valid with constant NULL lwp.
1057 */
1058 if (cnp->cn_namelen == pt->pt_namlen &&
1059 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
1060 (pt->pt_valid == NULL ||
1061 (*pt->pt_valid)(NULL, dvp->v_mount)))
1062 break;
1063 }
1064
1065 if (i != nproc_root_targets) {
1066 error = procfs_allocvp(dvp->v_mount, vpp, 0,
1067 pt->pt_pfstype, -1);
1068 return (error);
1069 }
1070
1071 if (CNEQ(cnp, "curproc", 7)) {
1072 pid = curproc->p_pid;
1073 vnpid = 0;
1074 type = PFScurproc;
1075 } else if (CNEQ(cnp, "self", 4)) {
1076 pid = curproc->p_pid;
1077 vnpid = 0;
1078 type = PFSself;
1079 } else {
1080 pid = (pid_t)atoi(pname, cnp->cn_namelen);
1081 vnpid = pid;
1082 type = PFSproc;
1083 }
1084
1085 if (procfs_proc_lock(pid, &p, ESRCH) != 0)
1086 break;
1087 error = procfs_allocvp(dvp->v_mount, vpp, vnpid, type, -1);
1088 procfs_proc_unlock(p);
1089 return (error);
1090
1091 case PFSproc:
1092 if (cnp->cn_flags & ISDOTDOT) {
1093 error = procfs_allocvp(dvp->v_mount, vpp, 0, PFSroot,
1094 -1);
1095 return (error);
1096 }
1097
1098 if (procfs_proc_lock(pfs->pfs_pid, &p, ESRCH) != 0)
1099 break;
1100
1101 mutex_enter(p->p_lock);
1102 LIST_FOREACH(plwp, &p->p_lwps, l_sibling) {
1103 if (plwp->l_stat != LSZOMB)
1104 break;
1105 }
1106 /* Process is exiting if no-LWPS or all LWPs are LSZOMB */
1107 if (plwp == NULL) {
1108 mutex_exit(p->p_lock);
1109 procfs_proc_unlock(p);
1110 return ESRCH;
1111 }
1112
1113 lwp_addref(plwp);
1114 mutex_exit(p->p_lock);
1115
1116 for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
1117 int found;
1118
1119 found = cnp->cn_namelen == pt->pt_namlen &&
1120 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
1121 (pt->pt_valid == NULL
1122 || (*pt->pt_valid)(plwp, dvp->v_mount));
1123 if (found)
1124 break;
1125 }
1126 lwp_delref(plwp);
1127
1128 if (i == nproc_targets) {
1129 procfs_proc_unlock(p);
1130 break;
1131 }
1132 if (pt->pt_pfstype == PFSfile) {
1133 fvp = p->p_textvp;
1134 /* We already checked that it exists. */
1135 vref(fvp);
1136 procfs_proc_unlock(p);
1137 *vpp = fvp;
1138 return (0);
1139 }
1140
1141 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
1142 pt->pt_pfstype, -1);
1143 procfs_proc_unlock(p);
1144 return (error);
1145
1146 case PFSfd: {
1147 int fd;
1148 file_t *fp;
1149
1150 if ((error = procfs_proc_lock(pfs->pfs_pid, &p, 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(pfs->pfs_pid, &p, ENOENT)) != 0)
1187 return error;
1188
1189 if (cnp->cn_flags & ISDOTDOT) {
1190 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
1191 PFSproc, -1);
1192 procfs_proc_unlock(p);
1193 return (error);
1194 }
1195 xpid = atoi(pname, cnp->cn_namelen);
1196
1197 if (xpid != pfs->pfs_pid) {
1198 procfs_proc_unlock(p);
1199 return ENOENT;
1200 }
1201 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
1202 PFStask, 0);
1203 procfs_proc_unlock(p);
1204 return error;
1205 }
1206 default:
1207 return (ENOTDIR);
1208 }
1209
1210 return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
1211 }
1212
1213 int
1214 procfs_validfile(struct lwp *l, struct mount *mp)
1215 {
1216 return l != NULL && l->l_proc != NULL && l->l_proc->p_textvp != NULL;
1217 }
1218
1219 static int
1220 procfs_validfile_linux(struct lwp *l, struct mount *mp)
1221 {
1222 int flags;
1223
1224 flags = VFSTOPROC(mp)->pmnt_flags;
1225 return (flags & PROCFSMNT_LINUXCOMPAT) &&
1226 (l == NULL || l->l_proc == NULL || procfs_validfile(l, mp));
1227 }
1228
1229 struct procfs_root_readdir_ctx {
1230 struct uio *uiop;
1231 off_t *cookies;
1232 int ncookies;
1233 off_t off;
1234 off_t startoff;
1235 int error;
1236 };
1237
1238 static int
1239 procfs_root_readdir_callback(struct proc *p, void *arg)
1240 {
1241 struct procfs_root_readdir_ctx *ctxp = arg;
1242 struct dirent d;
1243 struct uio *uiop;
1244 int error;
1245
1246 uiop = ctxp->uiop;
1247 if (uiop->uio_resid < UIO_MX)
1248 return -1; /* no space */
1249
1250 if (ctxp->off < ctxp->startoff) {
1251 ctxp->off++;
1252 return 0;
1253 }
1254
1255 if (kauth_authorize_process(kauth_cred_get(),
1256 KAUTH_PROCESS_CANSEE, p,
1257 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL) != 0)
1258 return 0;
1259
1260 memset(&d, 0, UIO_MX);
1261 d.d_reclen = UIO_MX;
1262 d.d_fileno = PROCFS_FILENO(p->p_pid, PFSproc, -1);
1263 d.d_namlen = snprintf(d.d_name,
1264 UIO_MX - offsetof(struct dirent, d_name), "%ld", (long)p->p_pid);
1265 d.d_type = DT_DIR;
1266
1267 mutex_exit(proc_lock);
1268 error = uiomove(&d, UIO_MX, uiop);
1269 mutex_enter(proc_lock);
1270 if (error) {
1271 ctxp->error = error;
1272 return -1;
1273 }
1274
1275 ctxp->ncookies++;
1276 if (ctxp->cookies)
1277 *(ctxp->cookies)++ = ctxp->off + 1;
1278 ctxp->off++;
1279
1280 return 0;
1281 }
1282
1283 /*
1284 * readdir returns directory entries from pfsnode (vp).
1285 *
1286 * the strategy here with procfs is to generate a single
1287 * directory entry at a time (struct dirent) and then
1288 * copy that out to userland using uiomove. a more efficent
1289 * though more complex implementation, would try to minimize
1290 * the number of calls to uiomove(). for procfs, this is
1291 * hardly worth the added code complexity.
1292 *
1293 * this should just be done through read()
1294 */
1295 int
1296 procfs_readdir(void *v)
1297 {
1298 struct vop_readdir_args /* {
1299 struct vnode *a_vp;
1300 struct uio *a_uio;
1301 kauth_cred_t a_cred;
1302 int *a_eofflag;
1303 off_t **a_cookies;
1304 int *a_ncookies;
1305 } */ *ap = v;
1306 struct uio *uio = ap->a_uio;
1307 struct dirent d;
1308 struct pfsnode *pfs;
1309 off_t i;
1310 int error;
1311 off_t *cookies = NULL;
1312 int ncookies;
1313 struct vnode *vp;
1314 const struct proc_target *pt;
1315 struct procfs_root_readdir_ctx ctx;
1316 struct lwp *l;
1317 int nfd;
1318
1319 vp = ap->a_vp;
1320 pfs = VTOPFS(vp);
1321
1322 if (uio->uio_resid < UIO_MX)
1323 return (EINVAL);
1324 if (uio->uio_offset < 0)
1325 return (EINVAL);
1326
1327 error = 0;
1328 i = uio->uio_offset;
1329 memset(&d, 0, UIO_MX);
1330 d.d_reclen = UIO_MX;
1331 ncookies = uio->uio_resid / UIO_MX;
1332
1333 switch (pfs->pfs_type) {
1334 /*
1335 * this is for the process-specific sub-directories.
1336 * all that is needed to is copy out all the entries
1337 * from the procent[] table (top of this file).
1338 */
1339 case PFSproc: {
1340 struct proc *p;
1341
1342 if (i >= nproc_targets)
1343 return 0;
1344
1345 if (procfs_proc_lock(pfs->pfs_pid, &p, ESRCH) != 0)
1346 break;
1347
1348 if (ap->a_ncookies) {
1349 ncookies = min(ncookies, (nproc_targets - i));
1350 cookies = malloc(ncookies * sizeof (off_t),
1351 M_TEMP, M_WAITOK);
1352 *ap->a_cookies = cookies;
1353 }
1354
1355 for (pt = &proc_targets[i];
1356 uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
1357 if (pt->pt_valid) {
1358 /* XXXSMP LWP can disappear */
1359 mutex_enter(p->p_lock);
1360 l = LIST_FIRST(&p->p_lwps);
1361 KASSERT(l != NULL);
1362 mutex_exit(p->p_lock);
1363 if ((*pt->pt_valid)(l, vp->v_mount) == 0)
1364 continue;
1365 }
1366
1367 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
1368 pt->pt_pfstype, -1);
1369 d.d_namlen = pt->pt_namlen;
1370 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1371 d.d_type = pt->pt_type;
1372
1373 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1374 break;
1375 if (cookies)
1376 *cookies++ = i + 1;
1377 }
1378
1379 procfs_proc_unlock(p);
1380 break;
1381 }
1382 case PFSfd: {
1383 struct proc *p;
1384 file_t *fp;
1385 int lim, nc = 0;
1386
1387 if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ESRCH)) != 0)
1388 return error;
1389
1390 /* XXX Should this be by file as well? */
1391 if (kauth_authorize_process(kauth_cred_get(),
1392 KAUTH_PROCESS_CANSEE, p,
1393 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_OPENFILES), NULL,
1394 NULL) != 0) {
1395 procfs_proc_unlock(p);
1396 return ESRCH;
1397 }
1398
1399 nfd = p->p_fd->fd_dt->dt_nfiles;
1400
1401 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
1402 if (i >= lim) {
1403 procfs_proc_unlock(p);
1404 return 0;
1405 }
1406
1407 if (ap->a_ncookies) {
1408 ncookies = min(ncookies, (nfd + 2 - i));
1409 cookies = malloc(ncookies * sizeof (off_t),
1410 M_TEMP, M_WAITOK);
1411 *ap->a_cookies = cookies;
1412 }
1413
1414 for (; i < 2 && uio->uio_resid >= UIO_MX; i++) {
1415 pt = &proc_targets[i];
1416 d.d_namlen = pt->pt_namlen;
1417 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
1418 pt->pt_pfstype, -1);
1419 (void)memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1420 d.d_type = pt->pt_type;
1421 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1422 break;
1423 if (cookies)
1424 *cookies++ = i + 1;
1425 nc++;
1426 }
1427 if (error) {
1428 ncookies = nc;
1429 break;
1430 }
1431 for (; uio->uio_resid >= UIO_MX && i < nfd; i++) {
1432 /* check the descriptor exists */
1433 if ((fp = fd_getfile2(p, i - 2)) == NULL)
1434 continue;
1435 closef(fp);
1436
1437 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, PFSfd, i - 2);
1438 d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
1439 "%lld", (long long)(i - 2));
1440 d.d_type = VREG;
1441 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1442 break;
1443 if (cookies)
1444 *cookies++ = i + 1;
1445 nc++;
1446 }
1447 ncookies = nc;
1448 procfs_proc_unlock(p);
1449 break;
1450 }
1451 case PFStask: {
1452 struct proc *p;
1453 int nc = 0;
1454
1455 if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ESRCH)) != 0)
1456 return error;
1457
1458 nfd = 3; /* ., .., pid */
1459
1460 if (ap->a_ncookies) {
1461 ncookies = min(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 pfsnode *pfs = VTOPFS(ap->a_vp);
1629 struct proc *pown;
1630
1631 if (pfs->pfs_fileno == PROCFS_FILENO(0, PFScurproc, -1))
1632 len = snprintf(bf, sizeof(bf), "%ld", (long)curproc->p_pid);
1633 else if (pfs->pfs_fileno == PROCFS_FILENO(0, PFSself, -1))
1634 len = snprintf(bf, sizeof(bf), "%s", "curproc");
1635 else if (pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFStask, 0))
1636 len = snprintf(bf, sizeof(bf), "..");
1637 else if (pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFScwd, -1) ||
1638 pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFSchroot, -1) ||
1639 pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFSexe, -1)) {
1640 if ((error = procfs_proc_lock(pfs->pfs_pid, &pown, ESRCH)) != 0)
1641 return error;
1642 path = malloc(MAXPATHLEN + 4, M_TEMP, M_WAITOK|M_CANFAIL);
1643 if (path == NULL) {
1644 procfs_proc_unlock(pown);
1645 return (ENOMEM);
1646 }
1647 bp = path + MAXPATHLEN;
1648 *--bp = '\0';
1649 procfs_dir(PROCFS_TYPE(pfs->pfs_fileno), curlwp, pown,
1650 &bp, path, MAXPATHLEN);
1651 procfs_proc_unlock(pown);
1652 len = strlen(bp);
1653 } else {
1654 file_t *fp;
1655 struct vnode *vxp, *vp;
1656
1657 if ((error = procfs_proc_lock(pfs->pfs_pid, &pown, ESRCH)) != 0)
1658 return error;
1659
1660 fp = fd_getfile2(pown, pfs->pfs_fd);
1661 if (fp == NULL) {
1662 procfs_proc_unlock(pown);
1663 return EBADF;
1664 }
1665
1666 switch (fp->f_type) {
1667 case DTYPE_VNODE:
1668 vxp = fp->f_vnode;
1669 if (vxp->v_type != VDIR) {
1670 error = EINVAL;
1671 break;
1672 }
1673 if ((path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK))
1674 == NULL) {
1675 error = ENOMEM;
1676 break;
1677 }
1678 bp = path + MAXPATHLEN;
1679 *--bp = '\0';
1680
1681 /*
1682 * XXX: kludge to avoid locking against ourselves
1683 * in getcwd()
1684 */
1685 if (vxp->v_tag == VT_PROCFS) {
1686 *--bp = '/';
1687 } else {
1688 rw_enter(&curproc->p_cwdi->cwdi_lock,
1689 RW_READER);
1690 vp = curproc->p_cwdi->cwdi_rdir;
1691 if (vp == NULL)
1692 vp = rootvnode;
1693 error = getcwd_common(vxp, vp, &bp, path,
1694 MAXPATHLEN / 2, 0, curlwp);
1695 rw_exit(&curproc->p_cwdi->cwdi_lock);
1696 }
1697 if (error)
1698 break;
1699 len = strlen(bp);
1700 break;
1701
1702 case DTYPE_MISC:
1703 len = snprintf(bf, sizeof(bf), "%s", "[misc]");
1704 break;
1705
1706 case DTYPE_KQUEUE:
1707 len = snprintf(bf, sizeof(bf), "%s", "[kqueue]");
1708 break;
1709
1710 case DTYPE_SEM:
1711 len = snprintf(bf, sizeof(bf), "%s", "[ksem]");
1712 break;
1713
1714 default:
1715 error = EINVAL;
1716 break;
1717 }
1718 closef(fp);
1719 procfs_proc_unlock(pown);
1720 }
1721
1722 if (error == 0)
1723 error = uiomove(bp, len, ap->a_uio);
1724 if (path)
1725 free(path, M_TEMP);
1726 return error;
1727 }
1728
1729 /*
1730 * convert decimal ascii to int
1731 */
1732 static int
1733 atoi(const char *b, size_t len)
1734 {
1735 int p = 0;
1736
1737 while (len--) {
1738 char c = *b++;
1739 if (c < '0' || c > '9')
1740 return -1;
1741 p = 10 * p + (c - '0');
1742 }
1743
1744 return p;
1745 }
1746