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