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