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