procfs_vnops.c revision 1.154.2.4 1 /* $NetBSD: procfs_vnops.c,v 1.154.2.4 2007/06/17 21:31:44 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.4 2007/06/17 21:31:44 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 if (rvp == NULL)
591 rvp = rootvnode;
592 mutex_exit(&target->p_mutex); /* XXXSMP */
593 if (vp == NULL || getcwd_common(vp, rvp, bp ? &bp : NULL, path,
594 len / 2, 0, caller) != 0) {
595 vp = NULL;
596 if (bpp) {
597 /*
598 if (t == PFSexe) {
599 snprintf(path, len, "%s/%d/file"
600 mp->mnt_stat.f_mntonname, pfs->pfs_pid);
601 } else */ {
602 bp = *bpp;
603 *--bp = '/';
604 }
605 }
606 }
607 mutex_enter(&target->p_mutex); /* XXXSMP */
608
609 if (bpp)
610 *bpp = bp;
611
612 return (vp);
613 }
614
615 /*
616 * Invent attributes for pfsnode (vp) and store
617 * them in (vap).
618 * Directories lengths are returned as zero since
619 * any real length would require the genuine size
620 * to be computed, and nothing cares anyway.
621 *
622 * this is relatively minimal for procfs.
623 */
624 int
625 procfs_getattr(v)
626 void *v;
627 {
628 struct vop_getattr_args /* {
629 struct vnode *a_vp;
630 struct vattr *a_vap;
631 kauth_cred_t a_cred;
632 struct lwp *a_l;
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 PFScwd:
657 case PFSchroot:
658 case PFSexe:
659 MALLOC(path, char *, MAXPATHLEN + 4, M_TEMP,
660 M_WAITOK|M_CANFAIL);
661 if (path == NULL && procp != NULL) {
662 procfs_proc_unlock(procp);
663 return (ENOMEM);
664 }
665 break;
666
667 default:
668 path = NULL;
669 break;
670 }
671
672 if (procp != NULL) {
673 mutex_enter(&procp->p_mutex);
674 error = kauth_authorize_process(kauth_cred_get(),
675 KAUTH_PROCESS_CANSEE, procp, NULL, NULL, NULL);
676 mutex_exit(&procp->p_mutex);
677 if (error != 0) {
678 procfs_proc_unlock(procp);
679 if (path != NULL)
680 free(path, M_TEMP);
681 return (ENOENT);
682 }
683 }
684
685 error = 0;
686
687 /* start by zeroing out the attributes */
688 VATTR_NULL(vap);
689
690 /* next do all the common fields */
691 vap->va_type = ap->a_vp->v_type;
692 vap->va_mode = pfs->pfs_mode;
693 vap->va_fileid = pfs->pfs_fileno;
694 vap->va_flags = 0;
695 vap->va_blocksize = PAGE_SIZE;
696
697 /*
698 * Make all times be current TOD.
699 *
700 * It would be possible to get the process start
701 * time from the p_stats structure, but there's
702 * no "file creation" time stamp anyway, and the
703 * p_stats structure is not addressable if u. gets
704 * swapped out for that process.
705 */
706 getnanotime(&vap->va_ctime);
707 vap->va_atime = vap->va_mtime = vap->va_ctime;
708 if (procp)
709 TIMEVAL_TO_TIMESPEC(&procp->p_stats->p_start,
710 &vap->va_birthtime);
711 else
712 getnanotime(&vap->va_birthtime);
713
714 switch (pfs->pfs_type) {
715 case PFSmem:
716 case PFSregs:
717 case PFSfpregs:
718 #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
719 PROCFS_MACHDEP_PROTECT_CASES
720 #endif
721 /*
722 * If the process has exercised some setuid or setgid
723 * privilege, then rip away read/write permission so
724 * that only root can gain access.
725 */
726 if (procp->p_flag & PK_SUGID)
727 vap->va_mode &= ~(S_IRUSR|S_IWUSR);
728 /* FALLTHROUGH */
729 case PFSctl:
730 case PFSstatus:
731 case PFSstat:
732 case PFSnote:
733 case PFSnotepg:
734 case PFSmap:
735 case PFSmaps:
736 case PFScmdline:
737 case PFSemul:
738 case PFSstatm:
739 vap->va_nlink = 1;
740 vap->va_uid = kauth_cred_geteuid(procp->p_cred);
741 vap->va_gid = kauth_cred_getegid(procp->p_cred);
742 break;
743 case PFSmeminfo:
744 case PFSdevices:
745 case PFScpuinfo:
746 case PFSuptime:
747 case PFSmounts:
748 case PFScpustat:
749 case PFSloadavg:
750 vap->va_nlink = 1;
751 vap->va_uid = vap->va_gid = 0;
752 break;
753
754 default:
755 break;
756 }
757
758 /*
759 * now do the object specific fields
760 *
761 * The size could be set from struct reg, but it's hardly
762 * worth the trouble, and it puts some (potentially) machine
763 * dependent data into this machine-independent code. If it
764 * becomes important then this function should break out into
765 * a per-file stat function in the corresponding .c file.
766 */
767
768 switch (pfs->pfs_type) {
769 case PFSroot:
770 /*
771 * Set nlink to 1 to tell fts(3) we don't actually know.
772 */
773 vap->va_nlink = 1;
774 vap->va_uid = 0;
775 vap->va_gid = 0;
776 vap->va_bytes = vap->va_size = DEV_BSIZE;
777 break;
778
779 case PFSself:
780 case PFScurproc: {
781 char bf[16]; /* should be enough */
782 vap->va_nlink = 1;
783 vap->va_uid = 0;
784 vap->va_gid = 0;
785 vap->va_bytes = vap->va_size =
786 snprintf(bf, sizeof(bf), "%ld", (long)curproc->p_pid);
787 break;
788 }
789
790 case PFSfd:
791 if (pfs->pfs_fd != -1) {
792 struct file *fp;
793
794 fp = fd_getfile(procp->p_fd, pfs->pfs_fd);
795 if (fp == NULL) {
796 error = EBADF;
797 break;
798 }
799 FILE_USE(fp);
800 vap->va_nlink = 1;
801 vap->va_uid = kauth_cred_geteuid(fp->f_cred);
802 vap->va_gid = kauth_cred_getegid(fp->f_cred);
803 switch (fp->f_type) {
804 case DTYPE_VNODE:
805 vap->va_bytes = vap->va_size =
806 ((struct vnode *)fp->f_data)->v_size;
807 break;
808 default:
809 vap->va_bytes = vap->va_size = 0;
810 break;
811 }
812 FILE_UNUSE(fp, curlwp);
813 break;
814 }
815 /*FALLTHROUGH*/
816 case PFSproc:
817 vap->va_nlink = 2;
818 vap->va_uid = kauth_cred_geteuid(procp->p_cred);
819 vap->va_gid = kauth_cred_getegid(procp->p_cred);
820 vap->va_bytes = vap->va_size = DEV_BSIZE;
821 break;
822
823 case PFSfile:
824 error = EOPNOTSUPP;
825 break;
826
827 case PFSmem:
828 vap->va_bytes = vap->va_size =
829 ctob(procp->p_vmspace->vm_tsize +
830 procp->p_vmspace->vm_dsize +
831 procp->p_vmspace->vm_ssize);
832 break;
833
834 #if defined(PT_GETREGS) || defined(PT_SETREGS)
835 case PFSregs:
836 vap->va_bytes = vap->va_size = sizeof(struct reg);
837 break;
838 #endif
839
840 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
841 case PFSfpregs:
842 vap->va_bytes = vap->va_size = sizeof(struct fpreg);
843 break;
844 #endif
845
846 case PFSctl:
847 case PFSstatus:
848 case PFSstat:
849 case PFSnote:
850 case PFSnotepg:
851 case PFScmdline:
852 case PFSmeminfo:
853 case PFSdevices:
854 case PFScpuinfo:
855 case PFSuptime:
856 case PFSmounts:
857 case PFScpustat:
858 case PFSloadavg:
859 case PFSstatm:
860 vap->va_bytes = vap->va_size = 0;
861 break;
862 case PFSmap:
863 case PFSmaps:
864 /*
865 * Advise a larger blocksize for the map files, so that
866 * they may be read in one pass.
867 */
868 vap->va_blocksize = 4 * PAGE_SIZE;
869 vap->va_bytes = vap->va_size = 0;
870 break;
871
872 case PFScwd:
873 case PFSchroot:
874 case PFSexe: {
875 char *bp;
876
877 vap->va_nlink = 1;
878 vap->va_uid = 0;
879 vap->va_gid = 0;
880 bp = path + MAXPATHLEN;
881 *--bp = '\0';
882 mutex_enter(&procp->p_mutex);
883 (void)procfs_dir(pfs->pfs_type, curlwp, procp, &bp, path,
884 MAXPATHLEN);
885 mutex_exit(&procp->p_mutex);
886 vap->va_bytes = vap->va_size = strlen(bp);
887 break;
888 }
889
890 case PFSemul:
891 vap->va_bytes = vap->va_size = strlen(procp->p_emul->e_name);
892 break;
893
894 #ifdef __HAVE_PROCFS_MACHDEP
895 PROCFS_MACHDEP_NODETYPE_CASES
896 error = procfs_machdep_getattr(ap->a_vp, vap, procp);
897 break;
898 #endif
899
900 default:
901 panic("procfs_getattr");
902 }
903
904 if (procp != NULL)
905 procfs_proc_unlock(procp);
906 if (path != NULL)
907 free(path, M_TEMP);
908
909 return (error);
910 }
911
912 /*ARGSUSED*/
913 int
914 procfs_setattr(void *v)
915 {
916 /*
917 * just fake out attribute setting
918 * it's not good to generate an error
919 * return, otherwise things like creat()
920 * will fail when they try to set the
921 * file length to 0. worse, this means
922 * that echo $note > /proc/$pid/note will fail.
923 */
924
925 return (0);
926 }
927
928 /*
929 * implement access checking.
930 *
931 * actually, the check for super-user is slightly
932 * broken since it will allow read access to write-only
933 * objects. this doesn't cause any particular trouble
934 * but does mean that the i/o entry points need to check
935 * that the operation really does make sense.
936 */
937 int
938 procfs_access(v)
939 void *v;
940 {
941 struct vop_access_args /* {
942 struct vnode *a_vp;
943 int a_mode;
944 kauth_cred_t a_cred;
945 struct lwp *a_l;
946 } */ *ap = v;
947 struct vattr va;
948 int error;
949
950 if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_l)) != 0)
951 return (error);
952
953 return (vaccess(va.va_type, va.va_mode,
954 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
955 }
956
957 /*
958 * lookup. this is incredibly complicated in the
959 * general case, however for most pseudo-filesystems
960 * very little needs to be done.
961 *
962 * Locking isn't hard here, just poorly documented.
963 *
964 * If we're looking up ".", just vref the parent & return it.
965 *
966 * If we're looking up "..", unlock the parent, and lock "..". If everything
967 * went ok, and we're on the last component and the caller requested the
968 * parent locked, try to re-lock the parent. We do this to prevent lock
969 * races.
970 *
971 * For anything else, get the needed node. Then unlock the parent if not
972 * the last component or not LOCKPARENT (i.e. if we wouldn't re-lock the
973 * parent in the .. case).
974 *
975 * We try to exit with the parent locked in error cases.
976 */
977 int
978 procfs_lookup(v)
979 void *v;
980 {
981 struct vop_lookup_args /* {
982 struct vnode * a_dvp;
983 struct vnode ** a_vpp;
984 struct componentname * a_cnp;
985 } */ *ap = v;
986 struct componentname *cnp = ap->a_cnp;
987 struct vnode **vpp = ap->a_vpp;
988 struct vnode *dvp = ap->a_dvp;
989 const char *pname = cnp->cn_nameptr;
990 const struct proc_target *pt = NULL;
991 struct vnode *fvp;
992 pid_t pid, vnpid;
993 struct pfsnode *pfs;
994 struct proc *p = NULL;
995 struct lwp *l = NULL;
996 int i, error;
997 pfstype type;
998
999 *vpp = NULL;
1000
1001 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
1002 return (EROFS);
1003
1004 if (cnp->cn_namelen == 1 && *pname == '.') {
1005 *vpp = dvp;
1006 VREF(dvp);
1007 return (0);
1008 }
1009
1010 pfs = VTOPFS(dvp);
1011 switch (pfs->pfs_type) {
1012 case PFSroot:
1013 /*
1014 * Shouldn't get here with .. in the root node.
1015 */
1016 if (cnp->cn_flags & ISDOTDOT)
1017 return (EIO);
1018
1019 for (i = 0; i < nproc_root_targets; i++) {
1020 pt = &proc_root_targets[i];
1021 /*
1022 * check for node match. proc is always NULL here,
1023 * so call pt_valid with constant NULL lwp.
1024 */
1025 if (cnp->cn_namelen == pt->pt_namlen &&
1026 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
1027 (pt->pt_valid == NULL ||
1028 (*pt->pt_valid)(NULL, dvp->v_mount)))
1029 break;
1030 }
1031
1032 if (i != nproc_root_targets) {
1033 error = procfs_allocvp(dvp->v_mount, vpp, 0,
1034 pt->pt_pfstype, -1, NULL);
1035 return (error);
1036 }
1037
1038 if (CNEQ(cnp, "curproc", 7)) {
1039 pid = curproc->p_pid;
1040 vnpid = 0;
1041 type = PFScurproc;
1042 } else if (CNEQ(cnp, "self", 4)) {
1043 pid = curproc->p_pid;
1044 vnpid = 0;
1045 type = PFSself;
1046 } else {
1047 pid = (pid_t)atoi(pname, cnp->cn_namelen);
1048 vnpid = pid;
1049 type = PFSproc;
1050 }
1051
1052 if (procfs_proc_lock(pid, &p, ESRCH) != 0)
1053 break;
1054 error = procfs_allocvp(dvp->v_mount, vpp, vnpid, type, -1, p);
1055 procfs_proc_unlock(p);
1056 return (error);
1057
1058 case PFSproc:
1059 /*
1060 * do the .. dance. We unlock the directory, and then
1061 * get the root dir. That will automatically return ..
1062 * locked. Then if the caller wanted dvp locked, we
1063 * re-lock.
1064 */
1065 if (cnp->cn_flags & ISDOTDOT) {
1066 VOP_UNLOCK(dvp, 0);
1067 error = procfs_root(dvp->v_mount, vpp);
1068 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1069 return (error);
1070 }
1071
1072 if (procfs_proc_lock(pfs->pfs_pid, &p, ESRCH) != 0)
1073 break;
1074
1075 for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
1076 struct lwp *plwp;
1077 int found;
1078
1079 mutex_enter(&p->p_smutex);
1080 plwp = proc_representative_lwp(p, NULL, 1);
1081 lwp_addref(plwp);
1082 mutex_exit(&p->p_smutex);
1083 found = cnp->cn_namelen == pt->pt_namlen &&
1084 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
1085 (pt->pt_valid == NULL
1086 || (*pt->pt_valid)(plwp, dvp->v_mount));
1087 lwp_delref(plwp);
1088 if (found)
1089 break;
1090 }
1091 if (i == nproc_targets) {
1092 procfs_proc_unlock(p);
1093 break;
1094 }
1095 if (pt->pt_pfstype == PFSfile) {
1096 fvp = p->p_textvp;
1097 /* We already checked that it exists. */
1098 VREF(fvp);
1099 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY);
1100 *vpp = fvp;
1101 procfs_proc_unlock(p);
1102 return (0);
1103 }
1104
1105 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
1106 pt->pt_pfstype, -1, p);
1107 procfs_proc_unlock(p);
1108 return (error);
1109
1110 case PFSfd: {
1111 int fd;
1112 struct file *fp;
1113
1114 if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ENOENT)) != 0)
1115 return error;
1116
1117 /*
1118 * do the .. dance. We unlock the directory, and then
1119 * get the proc dir. That will automatically return ..
1120 * locked. Then re-lock the directory.
1121 */
1122 if (cnp->cn_flags & ISDOTDOT) {
1123 VOP_UNLOCK(dvp, 0);
1124 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
1125 PFSproc, -1, p);
1126 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1127 procfs_proc_unlock(p);
1128 return (error);
1129 }
1130 fd = atoi(pname, cnp->cn_namelen);
1131
1132 mutex_enter(&p->p_mutex);
1133 fp = fd_getfile(p->p_fd, fd);
1134 mutex_exit(&p->p_mutex);
1135 if (fp == NULL) {
1136 procfs_proc_unlock(p);
1137 return ENOENT;
1138 }
1139
1140 FILE_USE(fp);
1141
1142 switch (fp->f_type) {
1143 case DTYPE_VNODE:
1144 fvp = (struct vnode *)fp->f_data;
1145
1146 /* Don't show directories */
1147 if (fvp->v_type == VDIR)
1148 goto symlink;
1149
1150 VREF(fvp);
1151 FILE_UNUSE(fp, l);
1152 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY |
1153 (p == curproc ? LK_CANRECURSE : 0));
1154 *vpp = fvp;
1155 error = 0;
1156 break;
1157 default:
1158 symlink:
1159 FILE_UNUSE(fp, l);
1160 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
1161 PFSfd, fd, p);
1162 break;
1163 }
1164 procfs_proc_unlock(p);
1165 return error;
1166 }
1167 default:
1168 return (ENOTDIR);
1169 }
1170
1171 return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
1172 }
1173
1174 int
1175 procfs_validfile(struct lwp *l, struct mount *mp)
1176 {
1177 return l != NULL && l->l_proc != NULL && l->l_proc->p_textvp != NULL;
1178 }
1179
1180 static int
1181 procfs_validfile_linux(l, mp)
1182 struct lwp *l;
1183 struct mount *mp;
1184 {
1185 int flags;
1186
1187 flags = VFSTOPROC(mp)->pmnt_flags;
1188 return (flags & PROCFSMNT_LINUXCOMPAT) &&
1189 (l == NULL || l->l_proc == NULL || procfs_validfile(l, mp));
1190 }
1191
1192 struct procfs_root_readdir_ctx {
1193 struct uio *uiop;
1194 off_t *cookies;
1195 int ncookies;
1196 off_t off;
1197 off_t startoff;
1198 int error;
1199 };
1200
1201 static int
1202 procfs_root_readdir_callback(struct proc *p, void *arg)
1203 {
1204 struct procfs_root_readdir_ctx *ctxp = arg;
1205 struct dirent d;
1206 struct uio *uiop;
1207 int error;
1208
1209 uiop = ctxp->uiop;
1210 if (uiop->uio_resid < UIO_MX)
1211 return -1; /* no space */
1212
1213 if (ctxp->off < ctxp->startoff) {
1214 ctxp->off++;
1215 return 0;
1216 }
1217
1218 if (kauth_authorize_process(kauth_cred_get(),
1219 KAUTH_PROCESS_CANSEE, p, NULL, NULL, NULL) != 0)
1220 return 0;
1221
1222 memset(&d, 0, UIO_MX);
1223 d.d_reclen = UIO_MX;
1224 d.d_fileno = PROCFS_FILENO(p->p_pid, PFSproc, -1);
1225 d.d_namlen = snprintf(d.d_name,
1226 UIO_MX - offsetof(struct dirent, d_name), "%ld", (long)p->p_pid);
1227 d.d_type = DT_DIR;
1228
1229 mutex_exit(&proclist_lock);
1230 error = uiomove(&d, UIO_MX, uiop);
1231 mutex_enter(&proclist_lock);
1232 if (error) {
1233 ctxp->error = error;
1234 return -1;
1235 }
1236
1237 ctxp->ncookies++;
1238 if (ctxp->cookies)
1239 *(ctxp->cookies)++ = ctxp->off + 1;
1240 ctxp->off++;
1241
1242 return 0;
1243 }
1244
1245 /*
1246 * readdir returns directory entries from pfsnode (vp).
1247 *
1248 * the strategy here with procfs is to generate a single
1249 * directory entry at a time (struct dirent) and then
1250 * copy that out to userland using uiomove. a more efficent
1251 * though more complex implementation, would try to minimize
1252 * the number of calls to uiomove(). for procfs, this is
1253 * hardly worth the added code complexity.
1254 *
1255 * this should just be done through read()
1256 */
1257 int
1258 procfs_readdir(v)
1259 void *v;
1260 {
1261 struct vop_readdir_args /* {
1262 struct vnode *a_vp;
1263 struct uio *a_uio;
1264 kauth_cred_t a_cred;
1265 int *a_eofflag;
1266 off_t **a_cookies;
1267 int *a_ncookies;
1268 } */ *ap = v;
1269 struct uio *uio = ap->a_uio;
1270 struct dirent d;
1271 struct pfsnode *pfs;
1272 off_t i;
1273 int error;
1274 off_t *cookies = NULL;
1275 int ncookies;
1276 struct vnode *vp;
1277 const struct proc_target *pt;
1278 struct procfs_root_readdir_ctx ctx;
1279 struct lwp *l;
1280
1281 vp = ap->a_vp;
1282 pfs = VTOPFS(vp);
1283
1284 if (uio->uio_resid < UIO_MX)
1285 return (EINVAL);
1286 if (uio->uio_offset < 0)
1287 return (EINVAL);
1288
1289 error = 0;
1290 i = uio->uio_offset;
1291 memset(&d, 0, UIO_MX);
1292 d.d_reclen = UIO_MX;
1293 ncookies = uio->uio_resid / UIO_MX;
1294
1295 switch (pfs->pfs_type) {
1296 /*
1297 * this is for the process-specific sub-directories.
1298 * all that is needed to is copy out all the entries
1299 * from the procent[] table (top of this file).
1300 */
1301 case PFSproc: {
1302 struct proc *p;
1303
1304 if (i >= nproc_targets)
1305 return 0;
1306
1307 if (procfs_proc_lock(pfs->pfs_pid, &p, ESRCH) != 0)
1308 break;
1309
1310 if (ap->a_ncookies) {
1311 ncookies = min(ncookies, (nproc_targets - i));
1312 cookies = malloc(ncookies * sizeof (off_t),
1313 M_TEMP, M_WAITOK);
1314 *ap->a_cookies = cookies;
1315 }
1316
1317 for (pt = &proc_targets[i];
1318 uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
1319 if (pt->pt_valid) {
1320 /* XXXSMP locking */
1321 mutex_enter(&p->p_smutex);
1322 l = proc_representative_lwp(p, NULL, 1);
1323 mutex_exit(&p->p_smutex);
1324 if ((*pt->pt_valid)(l, vp->v_mount) == 0)
1325 continue;
1326 }
1327
1328 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
1329 pt->pt_pfstype, -1);
1330 d.d_namlen = pt->pt_namlen;
1331 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1332 d.d_type = pt->pt_type;
1333
1334 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1335 break;
1336 if (cookies)
1337 *cookies++ = i + 1;
1338 }
1339
1340 procfs_proc_unlock(p);
1341 break;
1342 }
1343 case PFSfd: {
1344 struct proc *p;
1345 struct filedesc *fdp;
1346 struct file *fp;
1347 int lim, nc = 0;
1348
1349 if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ESRCH)) != 0)
1350 return error;
1351
1352 if (kauth_authorize_process(kauth_cred_get(),
1353 KAUTH_PROCESS_CANSEE, p, NULL, NULL, NULL) != 0) {
1354 procfs_proc_unlock(p);
1355 return ESRCH;
1356 }
1357
1358 fdp = p->p_fd; /* XXXSMP */
1359
1360 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
1361 if (i >= lim) {
1362 procfs_proc_unlock(p);
1363 return 0;
1364 }
1365
1366 if (ap->a_ncookies) {
1367 ncookies = min(ncookies, (fdp->fd_nfiles + 2 - i));
1368 cookies = malloc(ncookies * sizeof (off_t),
1369 M_TEMP, M_WAITOK);
1370 *ap->a_cookies = cookies;
1371 }
1372
1373 for (; i < 2 && uio->uio_resid >= UIO_MX; i++) {
1374 pt = &proc_targets[i];
1375 d.d_namlen = pt->pt_namlen;
1376 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
1377 pt->pt_pfstype, -1);
1378 (void)memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1379 d.d_type = pt->pt_type;
1380 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1381 break;
1382 if (cookies)
1383 *cookies++ = i + 1;
1384 nc++;
1385 }
1386 if (error) {
1387 ncookies = nc;
1388 break;
1389 }
1390 for (; uio->uio_resid >= UIO_MX && i < fdp->fd_nfiles; i++) {
1391 /* check the descriptor exists */
1392 if ((fp = fd_getfile(fdp, i - 2)) == NULL)
1393 continue;
1394 mutex_exit(&fp->f_lock);
1395
1396 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, PFSfd, i - 2);
1397 d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
1398 "%lld", (long long)(i - 2));
1399 d.d_type = VREG;
1400 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1401 break;
1402 if (cookies)
1403 *cookies++ = i + 1;
1404 nc++;
1405 }
1406 ncookies = nc;
1407 procfs_proc_unlock(p);
1408 break;
1409 }
1410
1411 /*
1412 * this is for the root of the procfs filesystem
1413 * what is needed are special entries for "curproc"
1414 * and "self" followed by an entry for each process
1415 * on allproc.
1416 */
1417
1418 case PFSroot: {
1419 int nc = 0;
1420
1421 if (ap->a_ncookies) {
1422 /*
1423 * XXX Potentially allocating too much space here,
1424 * but I'm lazy. This loop needs some work.
1425 */
1426 cookies = malloc(ncookies * sizeof (off_t),
1427 M_TEMP, M_WAITOK);
1428 *ap->a_cookies = cookies;
1429 }
1430 error = 0;
1431 /* 0 ... 3 are static entries. */
1432 for (; i <= 3 && uio->uio_resid >= UIO_MX; i++) {
1433 switch (i) {
1434 case 0: /* `.' */
1435 case 1: /* `..' */
1436 d.d_fileno = PROCFS_FILENO(0, PFSroot, -1);
1437 d.d_namlen = i + 1;
1438 memcpy(d.d_name, "..", d.d_namlen);
1439 d.d_name[i + 1] = '\0';
1440 d.d_type = DT_DIR;
1441 break;
1442
1443 case 2:
1444 d.d_fileno = PROCFS_FILENO(0, PFScurproc, -1);
1445 d.d_namlen = sizeof("curproc") - 1;
1446 memcpy(d.d_name, "curproc", sizeof("curproc"));
1447 d.d_type = DT_LNK;
1448 break;
1449
1450 case 3:
1451 d.d_fileno = PROCFS_FILENO(0, PFSself, -1);
1452 d.d_namlen = sizeof("self") - 1;
1453 memcpy(d.d_name, "self", sizeof("self"));
1454 d.d_type = DT_LNK;
1455 break;
1456 }
1457
1458 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1459 break;
1460 nc++;
1461 if (cookies)
1462 *cookies++ = i + 1;
1463 }
1464 /* 4 ... are process entries. */
1465 ctx.uiop = uio;
1466 ctx.error = 0;
1467 ctx.off = 4;
1468 ctx.startoff = i;
1469 ctx.cookies = cookies;
1470 ctx.ncookies = nc;
1471 proclist_foreach_call(&allproc,
1472 procfs_root_readdir_callback, &ctx);
1473 cookies = ctx.cookies;
1474 nc = ctx.ncookies;
1475 error = ctx.error;
1476 if (error)
1477 break;
1478
1479 /* misc entries. */
1480 if (i < ctx.off)
1481 i = ctx.off;
1482 if (i >= ctx.off + nproc_root_targets)
1483 break;
1484 for (pt = &proc_root_targets[i - ctx.off];
1485 uio->uio_resid >= UIO_MX &&
1486 pt < &proc_root_targets[nproc_root_targets];
1487 pt++, i++) {
1488 if (pt->pt_valid &&
1489 (*pt->pt_valid)(NULL, vp->v_mount) == 0)
1490 continue;
1491 d.d_fileno = PROCFS_FILENO(0, pt->pt_pfstype, -1);
1492 d.d_namlen = pt->pt_namlen;
1493 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1494 d.d_type = pt->pt_type;
1495
1496 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
1497 break;
1498 nc++;
1499 if (cookies)
1500 *cookies++ = i + 1;
1501 }
1502
1503 ncookies = nc;
1504 break;
1505 }
1506
1507 default:
1508 error = ENOTDIR;
1509 break;
1510 }
1511
1512 if (ap->a_ncookies) {
1513 if (error) {
1514 if (cookies)
1515 free(*ap->a_cookies, M_TEMP);
1516 *ap->a_ncookies = 0;
1517 *ap->a_cookies = NULL;
1518 } else
1519 *ap->a_ncookies = ncookies;
1520 }
1521 uio->uio_offset = i;
1522 return (error);
1523 }
1524
1525 /*
1526 * readlink reads the link of `curproc' and others
1527 */
1528 int
1529 procfs_readlink(v)
1530 void *v;
1531 {
1532 struct vop_readlink_args *ap = v;
1533 char bf[16]; /* should be enough */
1534 char *bp = bf;
1535 char *path = NULL;
1536 int len;
1537 int error = 0;
1538 struct pfsnode *pfs = VTOPFS(ap->a_vp);
1539 struct proc *pown;
1540
1541 if (pfs->pfs_fileno == PROCFS_FILENO(0, PFScurproc, -1))
1542 len = snprintf(bf, sizeof(bf), "%ld", (long)curproc->p_pid);
1543 else if (pfs->pfs_fileno == PROCFS_FILENO(0, PFSself, -1))
1544 len = snprintf(bf, sizeof(bf), "%s", "curproc");
1545 else if (pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFScwd, -1) ||
1546 pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFSchroot, -1) ||
1547 pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFSexe, -1)) {
1548 if ((error = procfs_proc_lock(pfs->pfs_pid, &pown, ESRCH)) != 0)
1549 return error;
1550 MALLOC(path, char *, MAXPATHLEN + 4, M_TEMP,
1551 M_WAITOK|M_CANFAIL);
1552 if (path == NULL) {
1553 procfs_proc_unlock(pown);
1554 return (ENOMEM);
1555 }
1556 bp = path + MAXPATHLEN;
1557 *--bp = '\0';
1558 mutex_enter(&pown->p_mutex);
1559 (void)procfs_dir(PROCFS_TYPE(pfs->pfs_fileno), curlwp, pown,
1560 &bp, path, MAXPATHLEN);
1561 mutex_exit(&pown->p_mutex);
1562 procfs_proc_unlock(pown);
1563 len = strlen(bp);
1564 } else {
1565 struct file *fp;
1566 struct vnode *vxp, *vp;
1567
1568 if ((error = procfs_proc_lock(pfs->pfs_pid, &pown, ESRCH)) != 0)
1569 return error;
1570
1571 mutex_enter(&pown->p_mutex);
1572 fp = fd_getfile(pown->p_fd, pfs->pfs_fd);
1573 mutex_exit(&pown->p_mutex);
1574 if (fp == NULL) {
1575 procfs_proc_unlock(pown);
1576 return EBADF;
1577 }
1578
1579 FILE_USE(fp);
1580 switch (fp->f_type) {
1581 case DTYPE_VNODE:
1582 vxp = (struct vnode *)fp->f_data;
1583 if (vxp->v_type != VDIR) {
1584 FILE_UNUSE(fp, curlwp);
1585 error = EINVAL;
1586 break;
1587 }
1588 if ((path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK))
1589 == NULL) {
1590 FILE_UNUSE(fp, curlwp);
1591 error = ENOMEM;
1592 break;
1593 }
1594 bp = path + MAXPATHLEN;
1595 *--bp = '\0';
1596 vp = curproc->p_cwdi->cwdi_rdir; /* XXXSMP */
1597 if (vp == NULL)
1598 vp = rootvnode;
1599 error = getcwd_common(vxp, vp, &bp, path,
1600 MAXPATHLEN / 2, 0, curlwp);
1601 FILE_UNUSE(fp, curlwp);
1602 if (error)
1603 break;
1604 len = strlen(bp);
1605 break;
1606
1607 case DTYPE_MISC:
1608 len = snprintf(bf, sizeof(bf), "%s", "[misc]");
1609 break;
1610
1611 case DTYPE_KQUEUE:
1612 len = snprintf(bf, sizeof(bf), "%s", "[kqueue]");
1613 break;
1614
1615 default:
1616 error = EINVAL;
1617 break;
1618 }
1619 procfs_proc_unlock(pown);
1620 }
1621
1622 if (error == 0)
1623 error = uiomove(bp, len, ap->a_uio);
1624 if (path)
1625 free(path, M_TEMP);
1626 return error;
1627 }
1628
1629 /*
1630 * convert decimal ascii to int
1631 */
1632 static int
1633 atoi(b, len)
1634 const char *b;
1635 size_t len;
1636 {
1637 int p = 0;
1638
1639 while (len--) {
1640 char c = *b++;
1641 if (c < '0' || c > '9')
1642 return -1;
1643 p = 10 * p + (c - '0');
1644 }
1645
1646 return p;
1647 }
1648