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