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