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