procfs_vnops.c revision 1.78.2.2 1 /* $NetBSD: procfs_vnops.c,v 1.78.2.2 2001/04/09 01:58:10 nathanw Exp $ */
2
3 /*
4 * Copyright (c) 1993 Jan-Simon Pendry
5 * Copyright (c) 1993, 1995
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95
40 */
41
42 /*
43 * procfs vnode interface
44 */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/time.h>
49 #include <sys/kernel.h>
50 #include <sys/file.h>
51 #include <sys/lwp.h>
52 #include <sys/proc.h>
53 #include <sys/vnode.h>
54 #include <sys/namei.h>
55 #include <sys/malloc.h>
56 #include <sys/mount.h>
57 #include <sys/dirent.h>
58 #include <sys/resourcevar.h>
59 #include <sys/ptrace.h>
60 #include <sys/stat.h>
61
62 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
63
64 #include <machine/reg.h>
65
66 #include <miscfs/genfs/genfs.h>
67 #include <miscfs/procfs/procfs.h>
68
69 /*
70 * Vnode Operations.
71 *
72 */
73
74 static int procfs_validfile_linux __P((struct lwp *, struct mount *));
75
76 /*
77 * This is a list of the valid names in the
78 * process-specific sub-directories. It is
79 * used in procfs_lookup and procfs_readdir
80 */
81 const struct proc_target {
82 u_char pt_type;
83 u_char pt_namlen;
84 char *pt_name;
85 pfstype pt_pfstype;
86 int (*pt_valid) __P((struct lwp *, struct mount *));
87 } proc_targets[] = {
88 #define N(s) sizeof(s)-1, s
89 /* name type validp */
90 { DT_DIR, N("."), Pproc, NULL },
91 { DT_DIR, N(".."), Proot, NULL },
92 { DT_REG, N("file"), Pfile, procfs_validfile },
93 { DT_REG, N("mem"), Pmem, NULL },
94 { DT_REG, N("regs"), Pregs, procfs_validregs },
95 { DT_REG, N("fpregs"), Pfpregs, procfs_validfpregs },
96 { DT_REG, N("ctl"), Pctl, NULL },
97 { DT_REG, N("status"), Pstatus, NULL },
98 { DT_REG, N("note"), Pnote, NULL },
99 { DT_REG, N("notepg"), Pnotepg, NULL },
100 { DT_REG, N("map"), Pmap, procfs_validmap },
101 { DT_REG, N("maps"), Pmaps, procfs_validmap },
102 { DT_REG, N("cmdline"), Pcmdline, NULL },
103 { DT_REG, N("exe"), Pfile, procfs_validfile_linux },
104 #undef N
105 };
106 static int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]);
107
108 /*
109 * List of files in the root directory. Note: the validate function will
110 * be called with p == NULL for these ones.
111 */
112 struct proc_target proc_root_targets[] = {
113 #define N(s) sizeof(s)-1, s
114 /* name type validp */
115 { DT_REG, N("meminfo"), Pmeminfo, procfs_validfile_linux },
116 { DT_REG, N("cpuinfo"), Pcpuinfo, procfs_validfile_linux },
117 #undef N
118 };
119 static int nproc_root_targets =
120 sizeof(proc_root_targets) / sizeof(proc_root_targets[0]);
121
122 int procfs_lookup __P((void *));
123 #define procfs_create genfs_eopnotsupp_rele
124 #define procfs_mknod genfs_eopnotsupp_rele
125 int procfs_open __P((void *));
126 int procfs_close __P((void *));
127 int procfs_access __P((void *));
128 int procfs_getattr __P((void *));
129 int procfs_setattr __P((void *));
130 #define procfs_read procfs_rw
131 #define procfs_write procfs_rw
132 #define procfs_fcntl genfs_fcntl
133 #define procfs_ioctl genfs_enoioctl
134 #define procfs_poll genfs_poll
135 #define procfs_revoke genfs_revoke
136 #define procfs_mmap genfs_eopnotsupp
137 #define procfs_fsync genfs_nullop
138 #define procfs_seek genfs_nullop
139 #define procfs_remove genfs_eopnotsupp_rele
140 int procfs_link __P((void *));
141 #define procfs_rename genfs_eopnotsupp_rele
142 #define procfs_mkdir genfs_eopnotsupp_rele
143 #define procfs_rmdir genfs_eopnotsupp_rele
144 int procfs_symlink __P((void *));
145 int procfs_readdir __P((void *));
146 int procfs_readlink __P((void *));
147 #define procfs_abortop genfs_abortop
148 int procfs_inactive __P((void *));
149 int procfs_reclaim __P((void *));
150 #define procfs_lock genfs_lock
151 #define procfs_unlock genfs_unlock
152 int procfs_bmap __P((void *));
153 #define procfs_strategy genfs_badop
154 int procfs_print __P((void *));
155 int procfs_pathconf __P((void *));
156 #define procfs_islocked genfs_islocked
157 #define procfs_advlock genfs_einval
158 #define procfs_blkatoff genfs_eopnotsupp
159 #define procfs_valloc genfs_eopnotsupp
160 #define procfs_vfree genfs_nullop
161 #define procfs_truncate genfs_eopnotsupp
162 #define procfs_update genfs_nullop
163 #define procfs_bwrite genfs_eopnotsupp
164
165 static pid_t atopid __P((const char *, u_int));
166
167 /*
168 * procfs vnode operations.
169 */
170 int (**procfs_vnodeop_p) __P((void *));
171 const struct vnodeopv_entry_desc procfs_vnodeop_entries[] = {
172 { &vop_default_desc, vn_default_error },
173 { &vop_lookup_desc, procfs_lookup }, /* lookup */
174 { &vop_create_desc, procfs_create }, /* create */
175 { &vop_mknod_desc, procfs_mknod }, /* mknod */
176 { &vop_open_desc, procfs_open }, /* open */
177 { &vop_close_desc, procfs_close }, /* close */
178 { &vop_access_desc, procfs_access }, /* access */
179 { &vop_getattr_desc, procfs_getattr }, /* getattr */
180 { &vop_setattr_desc, procfs_setattr }, /* setattr */
181 { &vop_read_desc, procfs_read }, /* read */
182 { &vop_write_desc, procfs_write }, /* write */
183 { &vop_fcntl_desc, procfs_fcntl }, /* fcntl */
184 { &vop_ioctl_desc, procfs_ioctl }, /* ioctl */
185 { &vop_poll_desc, procfs_poll }, /* poll */
186 { &vop_revoke_desc, procfs_revoke }, /* revoke */
187 { &vop_mmap_desc, procfs_mmap }, /* mmap */
188 { &vop_fsync_desc, procfs_fsync }, /* fsync */
189 { &vop_seek_desc, procfs_seek }, /* seek */
190 { &vop_remove_desc, procfs_remove }, /* remove */
191 { &vop_link_desc, procfs_link }, /* link */
192 { &vop_rename_desc, procfs_rename }, /* rename */
193 { &vop_mkdir_desc, procfs_mkdir }, /* mkdir */
194 { &vop_rmdir_desc, procfs_rmdir }, /* rmdir */
195 { &vop_symlink_desc, procfs_symlink }, /* symlink */
196 { &vop_readdir_desc, procfs_readdir }, /* readdir */
197 { &vop_readlink_desc, procfs_readlink }, /* readlink */
198 { &vop_abortop_desc, procfs_abortop }, /* abortop */
199 { &vop_inactive_desc, procfs_inactive }, /* inactive */
200 { &vop_reclaim_desc, procfs_reclaim }, /* reclaim */
201 { &vop_lock_desc, procfs_lock }, /* lock */
202 { &vop_unlock_desc, procfs_unlock }, /* unlock */
203 { &vop_bmap_desc, procfs_bmap }, /* bmap */
204 { &vop_strategy_desc, procfs_strategy }, /* strategy */
205 { &vop_print_desc, procfs_print }, /* print */
206 { &vop_islocked_desc, procfs_islocked }, /* islocked */
207 { &vop_pathconf_desc, procfs_pathconf }, /* pathconf */
208 { &vop_advlock_desc, procfs_advlock }, /* advlock */
209 { &vop_blkatoff_desc, procfs_blkatoff }, /* blkatoff */
210 { &vop_valloc_desc, procfs_valloc }, /* valloc */
211 { &vop_vfree_desc, procfs_vfree }, /* vfree */
212 { &vop_truncate_desc, procfs_truncate }, /* truncate */
213 { &vop_update_desc, procfs_update }, /* update */
214 { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
215 };
216 const struct vnodeopv_desc procfs_vnodeop_opv_desc =
217 { &procfs_vnodeop_p, procfs_vnodeop_entries };
218 /*
219 * set things up for doing i/o on
220 * the pfsnode (vp). (vp) is locked
221 * on entry, and should be left locked
222 * on exit.
223 *
224 * for procfs we don't need to do anything
225 * in particular for i/o. all that is done
226 * is to support exclusive open on process
227 * memory images.
228 */
229 int
230 procfs_open(v)
231 void *v;
232 {
233 struct vop_open_args /* {
234 struct vnode *a_vp;
235 int a_mode;
236 struct ucred *a_cred;
237 struct proc *a_p;
238 } */ *ap = v;
239 struct pfsnode *pfs = VTOPFS(ap->a_vp);
240 struct proc *p1, *p2;
241 int error;
242
243 p1 = ap->a_p; /* tracer */
244 p2 = PFIND(pfs->pfs_pid); /* traced */
245
246 if (p2 == NULL)
247 return (ENOENT); /* was ESRCH, jsp */
248
249 switch (pfs->pfs_type) {
250 case Pmem:
251 if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) ||
252 ((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE)))
253 return (EBUSY);
254
255 if ((error = procfs_checkioperm(p1, p2)) != 0)
256 return (EPERM);
257
258 if (ap->a_mode & FWRITE)
259 pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
260
261 return (0);
262
263 default:
264 break;
265 }
266
267 return (0);
268 }
269
270 /*
271 * close the pfsnode (vp) after doing i/o.
272 * (vp) is not locked on entry or exit.
273 *
274 * nothing to do for procfs other than undo
275 * any exclusive open flag (see _open above).
276 */
277 int
278 procfs_close(v)
279 void *v;
280 {
281 struct vop_close_args /* {
282 struct vnode *a_vp;
283 int a_fflag;
284 struct ucred *a_cred;
285 struct proc *a_p;
286 } */ *ap = v;
287 struct pfsnode *pfs = VTOPFS(ap->a_vp);
288
289 switch (pfs->pfs_type) {
290 case Pmem:
291 if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
292 pfs->pfs_flags &= ~(FWRITE|O_EXCL);
293 break;
294
295 default:
296 break;
297 }
298
299 return (0);
300 }
301
302 /*
303 * do block mapping for pfsnode (vp).
304 * since we don't use the buffer cache
305 * for procfs this function should never
306 * be called. in any case, it's not clear
307 * what part of the kernel ever makes use
308 * of this function. for sanity, this is the
309 * usual no-op bmap, although returning
310 * (EIO) would be a reasonable alternative.
311 */
312 int
313 procfs_bmap(v)
314 void *v;
315 {
316 struct vop_bmap_args /* {
317 struct vnode *a_vp;
318 daddr_t a_bn;
319 struct vnode **a_vpp;
320 daddr_t *a_bnp;
321 int * a_runp;
322 } */ *ap = v;
323
324 if (ap->a_vpp != NULL)
325 *ap->a_vpp = ap->a_vp;
326 if (ap->a_bnp != NULL)
327 *ap->a_bnp = ap->a_bn;
328 if (ap->a_runp != NULL)
329 *ap->a_runp = 0;
330 return (0);
331 }
332
333 /*
334 * _inactive is called when the pfsnode
335 * is vrele'd and the reference count goes
336 * to zero. (vp) will be on the vnode free
337 * list, so to get it back vget() must be
338 * used.
339 *
340 * for procfs, check if the process is still
341 * alive and if it isn't then just throw away
342 * the vnode by calling vgone(). this may
343 * be overkill and a waste of time since the
344 * chances are that the process will still be
345 * there and PFIND is not free.
346 *
347 * (vp) is locked on entry, but must be unlocked on exit.
348 */
349 int
350 procfs_inactive(v)
351 void *v;
352 {
353 struct vop_inactive_args /* {
354 struct vnode *a_vp;
355 struct proc *a_p;
356 } */ *ap = v;
357 struct pfsnode *pfs = VTOPFS(ap->a_vp);
358
359 VOP_UNLOCK(ap->a_vp, 0);
360 if (PFIND(pfs->pfs_pid) == 0)
361 vgone(ap->a_vp);
362
363 return (0);
364 }
365
366 /*
367 * _reclaim is called when getnewvnode()
368 * wants to make use of an entry on the vnode
369 * free list. at this time the filesystem needs
370 * to free any private data and remove the node
371 * from any private lists.
372 */
373 int
374 procfs_reclaim(v)
375 void *v;
376 {
377 struct vop_reclaim_args /* {
378 struct vnode *a_vp;
379 } */ *ap = v;
380
381 return (procfs_freevp(ap->a_vp));
382 }
383
384 /*
385 * Return POSIX pathconf information applicable to special devices.
386 */
387 int
388 procfs_pathconf(v)
389 void *v;
390 {
391 struct vop_pathconf_args /* {
392 struct vnode *a_vp;
393 int a_name;
394 register_t *a_retval;
395 } */ *ap = v;
396
397 switch (ap->a_name) {
398 case _PC_LINK_MAX:
399 *ap->a_retval = LINK_MAX;
400 return (0);
401 case _PC_MAX_CANON:
402 *ap->a_retval = MAX_CANON;
403 return (0);
404 case _PC_MAX_INPUT:
405 *ap->a_retval = MAX_INPUT;
406 return (0);
407 case _PC_PIPE_BUF:
408 *ap->a_retval = PIPE_BUF;
409 return (0);
410 case _PC_CHOWN_RESTRICTED:
411 *ap->a_retval = 1;
412 return (0);
413 case _PC_VDISABLE:
414 *ap->a_retval = _POSIX_VDISABLE;
415 return (0);
416 case _PC_SYNC_IO:
417 *ap->a_retval = 1;
418 return (0);
419 default:
420 return (EINVAL);
421 }
422 /* NOTREACHED */
423 }
424
425 /*
426 * _print is used for debugging.
427 * just print a readable description
428 * of (vp).
429 */
430 int
431 procfs_print(v)
432 void *v;
433 {
434 struct vop_print_args /* {
435 struct vnode *a_vp;
436 } */ *ap = v;
437 struct pfsnode *pfs = VTOPFS(ap->a_vp);
438
439 printf("tag VT_PROCFS, type %d, pid %d, mode %x, flags %lx\n",
440 pfs->pfs_type, pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
441 return 0;
442 }
443
444 int
445 procfs_link(v)
446 void *v;
447 {
448 struct vop_link_args /* {
449 struct vnode *a_dvp;
450 struct vnode *a_vp;
451 struct componentname *a_cnp;
452 } */ *ap = v;
453
454 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
455 vput(ap->a_dvp);
456 return (EROFS);
457 }
458
459 int
460 procfs_symlink(v)
461 void *v;
462 {
463 struct vop_symlink_args /* {
464 struct vnode *a_dvp;
465 struct vnode **a_vpp;
466 struct componentname *a_cnp;
467 struct vattr *a_vap;
468 char *a_target;
469 } */ *ap = v;
470
471 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
472 vput(ap->a_dvp);
473 return (EROFS);
474 }
475
476 /*
477 * Invent attributes for pfsnode (vp) and store
478 * them in (vap).
479 * Directories lengths are returned as zero since
480 * any real length would require the genuine size
481 * to be computed, and nothing cares anyway.
482 *
483 * this is relatively minimal for procfs.
484 */
485 int
486 procfs_getattr(v)
487 void *v;
488 {
489 struct vop_getattr_args /* {
490 struct vnode *a_vp;
491 struct vattr *a_vap;
492 struct ucred *a_cred;
493 struct proc *a_p;
494 } */ *ap = v;
495 struct pfsnode *pfs = VTOPFS(ap->a_vp);
496 struct vattr *vap = ap->a_vap;
497 struct proc *procp;
498 struct timeval tv;
499 int error;
500
501 /* first check the process still exists */
502 switch (pfs->pfs_type) {
503 case Proot:
504 case Pcurproc:
505 case Pself:
506 procp = 0;
507 break;
508
509 default:
510 procp = PFIND(pfs->pfs_pid);
511 if (procp == 0)
512 return (ENOENT);
513 break;
514 }
515
516 error = 0;
517
518 /* start by zeroing out the attributes */
519 VATTR_NULL(vap);
520
521 /* next do all the common fields */
522 vap->va_type = ap->a_vp->v_type;
523 vap->va_mode = pfs->pfs_mode;
524 vap->va_fileid = pfs->pfs_fileno;
525 vap->va_flags = 0;
526 vap->va_blocksize = PAGE_SIZE;
527
528 /*
529 * Make all times be current TOD.
530 * It would be possible to get the process start
531 * time from the p_stat structure, but there's
532 * no "file creation" time stamp anyway, and the
533 * p_stat structure is not addressible if u. gets
534 * swapped out for that process.
535 */
536 microtime(&tv);
537 TIMEVAL_TO_TIMESPEC(&tv, &vap->va_ctime);
538 vap->va_atime = vap->va_mtime = vap->va_ctime;
539
540 switch (pfs->pfs_type) {
541 case Pmem:
542 case Pregs:
543 case Pfpregs:
544 /*
545 * If the process has exercised some setuid or setgid
546 * privilege, then rip away read/write permission so
547 * that only root can gain access.
548 */
549 if (procp->p_flag & P_SUGID)
550 vap->va_mode &= ~(S_IRUSR|S_IWUSR);
551 /* FALLTHROUGH */
552 case Pctl:
553 case Pstatus:
554 case Pnote:
555 case Pnotepg:
556 case Pmap:
557 case Pmaps:
558 case Pcmdline:
559 vap->va_nlink = 1;
560 vap->va_uid = procp->p_ucred->cr_uid;
561 vap->va_gid = procp->p_ucred->cr_gid;
562 break;
563 case Pmeminfo:
564 case Pcpuinfo:
565 vap->va_nlink = 1;
566 vap->va_uid = vap->va_gid = 0;
567 break;
568
569 default:
570 break;
571 }
572
573 /*
574 * now do the object specific fields
575 *
576 * The size could be set from struct reg, but it's hardly
577 * worth the trouble, and it puts some (potentially) machine
578 * dependent data into this machine-independent code. If it
579 * becomes important then this function should break out into
580 * a per-file stat function in the corresponding .c file.
581 */
582
583 switch (pfs->pfs_type) {
584 case Proot:
585 /*
586 * Set nlink to 1 to tell fts(3) we don't actually know.
587 */
588 vap->va_nlink = 1;
589 vap->va_uid = 0;
590 vap->va_gid = 0;
591 vap->va_bytes = vap->va_size = DEV_BSIZE;
592 break;
593
594 case Pcurproc: {
595 char buf[16]; /* should be enough */
596 vap->va_nlink = 1;
597 vap->va_uid = 0;
598 vap->va_gid = 0;
599 vap->va_bytes = vap->va_size =
600 sprintf(buf, "%ld", (long)curproc->l_proc->p_pid);
601 break;
602 }
603
604 case Pself:
605 vap->va_nlink = 1;
606 vap->va_uid = 0;
607 vap->va_gid = 0;
608 vap->va_bytes = vap->va_size = sizeof("curproc");
609 break;
610
611 case Pproc:
612 vap->va_nlink = 2;
613 vap->va_uid = procp->p_ucred->cr_uid;
614 vap->va_gid = procp->p_ucred->cr_gid;
615 vap->va_bytes = vap->va_size = DEV_BSIZE;
616 break;
617
618 case Pfile:
619 error = EOPNOTSUPP;
620 break;
621
622 case Pmem:
623 vap->va_bytes = vap->va_size =
624 ctob(procp->p_vmspace->vm_tsize +
625 procp->p_vmspace->vm_dsize +
626 procp->p_vmspace->vm_ssize);
627 break;
628
629 #if defined(PT_GETREGS) || defined(PT_SETREGS)
630 case Pregs:
631 vap->va_bytes = vap->va_size = sizeof(struct reg);
632 break;
633 #endif
634
635 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
636 case Pfpregs:
637 vap->va_bytes = vap->va_size = sizeof(struct fpreg);
638 break;
639 #endif
640
641 case Pctl:
642 case Pstatus:
643 case Pnote:
644 case Pnotepg:
645 case Pcmdline:
646 case Pmeminfo:
647 case Pcpuinfo:
648 vap->va_bytes = vap->va_size = 0;
649 break;
650 case Pmap:
651 case Pmaps:
652 /*
653 * Advise a larger blocksize for the map files, so that
654 * they may be read in one pass.
655 */
656 vap->va_blocksize = 4 * PAGE_SIZE;
657 break;
658
659 default:
660 panic("procfs_getattr");
661 }
662
663 return (error);
664 }
665
666 /*ARGSUSED*/
667 int
668 procfs_setattr(v)
669 void *v;
670 {
671 /*
672 * just fake out attribute setting
673 * it's not good to generate an error
674 * return, otherwise things like creat()
675 * will fail when they try to set the
676 * file length to 0. worse, this means
677 * that echo $note > /proc/$pid/note will fail.
678 */
679
680 return (0);
681 }
682
683 /*
684 * implement access checking.
685 *
686 * actually, the check for super-user is slightly
687 * broken since it will allow read access to write-only
688 * objects. this doesn't cause any particular trouble
689 * but does mean that the i/o entry points need to check
690 * that the operation really does make sense.
691 */
692 int
693 procfs_access(v)
694 void *v;
695 {
696 struct vop_access_args /* {
697 struct vnode *a_vp;
698 int a_mode;
699 struct ucred *a_cred;
700 struct proc *a_p;
701 } */ *ap = v;
702 struct vattr va;
703 int error;
704
705 if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_p)) != 0)
706 return (error);
707
708 return (vaccess(va.va_type, va.va_mode,
709 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
710 }
711
712 /*
713 * lookup. this is incredibly complicated in the
714 * general case, however for most pseudo-filesystems
715 * very little needs to be done.
716 *
717 * Locking isn't hard here, just poorly documented.
718 *
719 * If we're looking up ".", just vref the parent & return it.
720 *
721 * If we're looking up "..", unlock the parent, and lock "..". If everything
722 * went ok, and we're on the last component and the caller requested the
723 * parent locked, try to re-lock the parent. We do this to prevent lock
724 * races.
725 *
726 * For anything else, get the needed node. Then unlock the parent if not
727 * the last component or not LOCKPARENT (i.e. if we wouldn't re-lock the
728 * parent in the .. case).
729 *
730 * We try to exit with the parent locked in error cases.
731 */
732 int
733 procfs_lookup(v)
734 void *v;
735 {
736 struct vop_lookup_args /* {
737 struct vnode * a_dvp;
738 struct vnode ** a_vpp;
739 struct componentname * a_cnp;
740 } */ *ap = v;
741 struct componentname *cnp = ap->a_cnp;
742 struct vnode **vpp = ap->a_vpp;
743 struct vnode *dvp = ap->a_dvp;
744 const char *pname = cnp->cn_nameptr;
745 const struct proc_target *pt = NULL;
746 struct vnode *fvp;
747 pid_t pid;
748 struct pfsnode *pfs;
749 struct proc *p = NULL;
750 int i, error, wantpunlock, iscurproc = 0, isself = 0;
751
752 *vpp = NULL;
753 cnp->cn_flags &= ~PDIRUNLOCK;
754
755 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
756 return (EROFS);
757
758 if (cnp->cn_namelen == 1 && *pname == '.') {
759 *vpp = dvp;
760 VREF(dvp);
761 return (0);
762 }
763
764 wantpunlock = (~cnp->cn_flags & (LOCKPARENT | ISLASTCN));
765 pfs = VTOPFS(dvp);
766 switch (pfs->pfs_type) {
767 case Proot:
768 /*
769 * Shouldn't get here with .. in the root node.
770 */
771 if (cnp->cn_flags & ISDOTDOT)
772 return (EIO);
773
774 iscurproc = CNEQ(cnp, "curproc", 7);
775 isself = CNEQ(cnp, "self", 4);
776
777 if (iscurproc || isself) {
778 error = procfs_allocvp(dvp->v_mount, vpp, 0,
779 iscurproc ? Pcurproc : Pself);
780 if ((error == 0) && (wantpunlock)) {
781 VOP_UNLOCK(dvp, 0);
782 cnp->cn_flags |= PDIRUNLOCK;
783 }
784 return (error);
785 }
786
787 for (i = 0; i < nproc_root_targets; i++) {
788 pt = &proc_root_targets[i];
789 if (cnp->cn_namelen == pt->pt_namlen &&
790 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
791 (pt->pt_valid == NULL ||
792 (*pt->pt_valid)(LIST_FIRST(&p->p_lwps),
793 dvp->v_mount)))
794 break;
795 }
796
797 if (i != nproc_root_targets) {
798 error = procfs_allocvp(dvp->v_mount, vpp, 0,
799 pt->pt_pfstype);
800 if ((error == 0) && (wantpunlock)) {
801 VOP_UNLOCK(dvp, 0);
802 cnp->cn_flags |= PDIRUNLOCK;
803 }
804 return (error);
805 }
806
807 pid = atopid(pname, cnp->cn_namelen);
808 if (pid == NO_PID)
809 break;
810
811 p = PFIND(pid);
812 if (p == 0)
813 break;
814
815 error = procfs_allocvp(dvp->v_mount, vpp, pid, Pproc);
816 if ((error == 0) && (wantpunlock)) {
817 VOP_UNLOCK(dvp, 0);
818 cnp->cn_flags |= PDIRUNLOCK;
819 }
820 return (error);
821
822 case Pproc:
823 /*
824 * do the .. dance. We unlock the directory, and then
825 * get the root dir. That will automatically return ..
826 * locked. Then if the caller wanted dvp locked, we
827 * re-lock.
828 */
829 if (cnp->cn_flags & ISDOTDOT) {
830 VOP_UNLOCK(dvp, 0);
831 cnp->cn_flags |= PDIRUNLOCK;
832 error = procfs_root(dvp->v_mount, vpp);
833 if ((error == 0) && (wantpunlock == 0) &&
834 ((error = vn_lock(dvp, LK_EXCLUSIVE)) == 0))
835 cnp->cn_flags &= ~PDIRUNLOCK;
836 return (error);
837 }
838
839 p = PFIND(pfs->pfs_pid);
840 if (p == 0)
841 break;
842
843 for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
844 if (cnp->cn_namelen == pt->pt_namlen &&
845 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
846 (pt->pt_valid == NULL ||
847 (*pt->pt_valid)(LIST_FIRST(&p->p_lwps),
848 dvp->v_mount)))
849 goto found;
850 }
851 break;
852
853 found:
854 if (pt->pt_pfstype == Pfile) {
855 fvp = p->p_textvp;
856 /* We already checked that it exists. */
857 VREF(fvp);
858 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY);
859 if (wantpunlock) {
860 VOP_UNLOCK(dvp, 0);
861 cnp->cn_flags |= PDIRUNLOCK;
862 }
863 *vpp = fvp;
864 return (0);
865 }
866
867 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
868 pt->pt_pfstype);
869 if ((error == 0) && (wantpunlock)) {
870 VOP_UNLOCK(dvp, 0);
871 cnp->cn_flags |= PDIRUNLOCK;
872 }
873 return (error);
874
875 default:
876 return (ENOTDIR);
877 }
878
879 return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
880 }
881
882 int
883 procfs_validfile(l, mp)
884 struct lwp *l;
885 struct mount *mp;
886 {
887 return (l->l_proc->p_textvp != NULL);
888 }
889
890 static int
891 procfs_validfile_linux(l, mp)
892 struct lwp *l;
893 struct mount *mp;
894 {
895 int flags;
896
897 flags = VFSTOPROC(mp)->pmnt_flags;
898 return ((flags & PROCFSMNT_LINUXCOMPAT) &&
899 (l == NULL || procfs_validfile(l, mp)));
900 }
901
902 /*
903 * readdir returns directory entries from pfsnode (vp).
904 *
905 * the strategy here with procfs is to generate a single
906 * directory entry at a time (struct dirent) and then
907 * copy that out to userland using uiomove. a more efficent
908 * though more complex implementation, would try to minimize
909 * the number of calls to uiomove(). for procfs, this is
910 * hardly worth the added code complexity.
911 *
912 * this should just be done through read()
913 */
914 int
915 procfs_readdir(v)
916 void *v;
917 {
918 struct vop_readdir_args /* {
919 struct vnode *a_vp;
920 struct uio *a_uio;
921 struct ucred *a_cred;
922 int *a_eofflag;
923 off_t **a_cookies;
924 int *a_ncookies;
925 } */ *ap = v;
926 struct uio *uio = ap->a_uio;
927 struct dirent d;
928 struct pfsnode *pfs;
929 off_t i;
930 int error;
931 off_t *cookies = NULL;
932 int ncookies, left, skip, j;
933 struct vnode *vp;
934 const struct proc_target *pt;
935
936 vp = ap->a_vp;
937 pfs = VTOPFS(vp);
938
939 if (uio->uio_resid < UIO_MX)
940 return (EINVAL);
941 if (uio->uio_offset < 0)
942 return (EINVAL);
943
944 error = 0;
945 i = uio->uio_offset;
946 memset((caddr_t)&d, 0, UIO_MX);
947 d.d_reclen = UIO_MX;
948 ncookies = uio->uio_resid / UIO_MX;
949
950 switch (pfs->pfs_type) {
951 /*
952 * this is for the process-specific sub-directories.
953 * all that is needed to is copy out all the entries
954 * from the procent[] table (top of this file).
955 */
956 case Pproc: {
957 struct proc *p;
958
959 if (i >= nproc_targets)
960 return 0;
961
962 p = PFIND(pfs->pfs_pid);
963 if (p == NULL)
964 break;
965
966 if (ap->a_ncookies) {
967 ncookies = min(ncookies, (nproc_targets - i));
968 cookies = malloc(ncookies * sizeof (off_t),
969 M_TEMP, M_WAITOK);
970 *ap->a_cookies = cookies;
971 }
972
973 for (pt = &proc_targets[i];
974 uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
975 if (pt->pt_valid &&
976 (*pt->pt_valid)(LIST_FIRST(&p->p_lwps),
977 vp->v_mount) == 0)
978 continue;
979
980 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, pt->pt_pfstype);
981 d.d_namlen = pt->pt_namlen;
982 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
983 d.d_type = pt->pt_type;
984
985 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
986 break;
987 if (cookies)
988 *cookies++ = i + 1;
989 }
990
991 break;
992 }
993
994 /*
995 * this is for the root of the procfs filesystem
996 * what is needed are special entries for "curproc"
997 * and "self" followed by an entry for each process
998 * on allproc
999 #ifdef PROCFS_ZOMBIE
1000 * and deadproc and zombproc.
1001 #endif
1002 */
1003
1004 case Proot: {
1005 int pcnt = i, nc = 0;
1006 const struct proclist_desc *pd;
1007 volatile struct proc *p;
1008
1009 if (pcnt > 3)
1010 pcnt = 3;
1011 if (ap->a_ncookies) {
1012 /*
1013 * XXX Potentially allocating too much space here,
1014 * but I'm lazy. This loop needs some work.
1015 */
1016 cookies = malloc(ncookies * sizeof (off_t),
1017 M_TEMP, M_WAITOK);
1018 *ap->a_cookies = cookies;
1019 }
1020 /*
1021 * XXX: THIS LOOP ASSUMES THAT allproc IS THE FIRST
1022 * PROCLIST IN THE proclists!
1023 */
1024 proclist_lock_read();
1025 pd = proclists;
1026 #ifdef PROCFS_ZOMBIE
1027 again:
1028 #endif
1029 for (p = LIST_FIRST(pd->pd_list);
1030 p != NULL && uio->uio_resid >= UIO_MX; i++, pcnt++) {
1031 switch (i) {
1032 case 0: /* `.' */
1033 case 1: /* `..' */
1034 d.d_fileno = PROCFS_FILENO(0, Proot);
1035 d.d_namlen = i + 1;
1036 memcpy(d.d_name, "..", d.d_namlen);
1037 d.d_name[i + 1] = '\0';
1038 d.d_type = DT_DIR;
1039 break;
1040
1041 case 2:
1042 d.d_fileno = PROCFS_FILENO(0, Pcurproc);
1043 d.d_namlen = sizeof("curproc") - 1;
1044 memcpy(d.d_name, "curproc", sizeof("curproc"));
1045 d.d_type = DT_LNK;
1046 break;
1047
1048 case 3:
1049 d.d_fileno = PROCFS_FILENO(0, Pself);
1050 d.d_namlen = sizeof("self") - 1;
1051 memcpy(d.d_name, "self", sizeof("self"));
1052 d.d_type = DT_LNK;
1053 break;
1054
1055 default:
1056 while (pcnt < i) {
1057 pcnt++;
1058 p = LIST_NEXT(p, p_list);
1059 if (!p)
1060 goto done;
1061 }
1062 d.d_fileno = PROCFS_FILENO(p->p_pid, Pproc);
1063 d.d_namlen = sprintf(d.d_name, "%ld",
1064 (long)p->p_pid);
1065 d.d_type = DT_REG;
1066 p = p->p_list.le_next;
1067 break;
1068 }
1069
1070 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
1071 break;
1072 nc++;
1073 if (cookies)
1074 *cookies++ = i + 1;
1075 }
1076 done:
1077
1078 #ifdef PROCFS_ZOMBIE
1079 pd++;
1080 if (p == NULL && pd->pd_list != NULL)
1081 goto again;
1082 #endif
1083 proclist_unlock_read();
1084
1085 skip = i - pcnt;
1086 if (skip >= nproc_root_targets)
1087 break;
1088 left = nproc_root_targets - skip;
1089 for (j = 0, pt = &proc_root_targets[0];
1090 uio->uio_resid >= UIO_MX && j < left;
1091 pt++, j++, i++) {
1092 if (pt->pt_valid &&
1093 (*pt->pt_valid)(NULL, vp->v_mount) == 0)
1094 continue;
1095 d.d_fileno = PROCFS_FILENO(0, pt->pt_pfstype);
1096 d.d_namlen = pt->pt_namlen;
1097 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1098 d.d_type = pt->pt_type;
1099
1100 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
1101 break;
1102 nc++;
1103 if (cookies)
1104 *cookies++ = i + 1;
1105 }
1106
1107 ncookies = nc;
1108 break;
1109 }
1110
1111 default:
1112 error = ENOTDIR;
1113 break;
1114 }
1115
1116 if (ap->a_ncookies) {
1117 if (error) {
1118 if (cookies)
1119 free(*ap->a_cookies, M_TEMP);
1120 *ap->a_ncookies = 0;
1121 *ap->a_cookies = NULL;
1122 } else
1123 *ap->a_ncookies = ncookies;
1124 }
1125 uio->uio_offset = i;
1126 return (error);
1127 }
1128
1129 /*
1130 * readlink reads the link of `curproc'
1131 */
1132 int
1133 procfs_readlink(v)
1134 void *v;
1135 {
1136 struct vop_readlink_args *ap = v;
1137 char buf[16]; /* should be enough */
1138 int len;
1139
1140 if (VTOPFS(ap->a_vp)->pfs_fileno == PROCFS_FILENO(0, Pcurproc))
1141 len = sprintf(buf, "%ld", (long)curproc->l_proc->p_pid);
1142 else if (VTOPFS(ap->a_vp)->pfs_fileno == PROCFS_FILENO(0, Pself))
1143 len = sprintf(buf, "%s", "curproc");
1144 else
1145 return (EINVAL);
1146
1147 return (uiomove((caddr_t)buf, len, ap->a_uio));
1148 }
1149
1150 /*
1151 * convert decimal ascii to pid_t
1152 */
1153 static pid_t
1154 atopid(b, len)
1155 const char *b;
1156 u_int len;
1157 {
1158 pid_t p = 0;
1159
1160 while (len--) {
1161 char c = *b++;
1162 if (c < '0' || c > '9')
1163 return (NO_PID);
1164 p = 10 * p + (c - '0');
1165 if (p > PID_MAX)
1166 return (NO_PID);
1167 }
1168
1169 return (p);
1170 }
1171