procfs_vnops.c revision 1.78.2.3 1 /* $NetBSD: procfs_vnops.c,v 1.78.2.3 2001/06/21 20:07:44 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 break;
625
626 default:
627 panic("procfs_getattr");
628 }
629
630 return (error);
631 }
632
633 /*ARGSUSED*/
634 int
635 procfs_setattr(v)
636 void *v;
637 {
638 /*
639 * just fake out attribute setting
640 * it's not good to generate an error
641 * return, otherwise things like creat()
642 * will fail when they try to set the
643 * file length to 0. worse, this means
644 * that echo $note > /proc/$pid/note will fail.
645 */
646
647 return (0);
648 }
649
650 /*
651 * implement access checking.
652 *
653 * actually, the check for super-user is slightly
654 * broken since it will allow read access to write-only
655 * objects. this doesn't cause any particular trouble
656 * but does mean that the i/o entry points need to check
657 * that the operation really does make sense.
658 */
659 int
660 procfs_access(v)
661 void *v;
662 {
663 struct vop_access_args /* {
664 struct vnode *a_vp;
665 int a_mode;
666 struct ucred *a_cred;
667 struct proc *a_p;
668 } */ *ap = v;
669 struct vattr va;
670 int error;
671
672 if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_p)) != 0)
673 return (error);
674
675 return (vaccess(va.va_type, va.va_mode,
676 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
677 }
678
679 /*
680 * lookup. this is incredibly complicated in the
681 * general case, however for most pseudo-filesystems
682 * very little needs to be done.
683 *
684 * Locking isn't hard here, just poorly documented.
685 *
686 * If we're looking up ".", just vref the parent & return it.
687 *
688 * If we're looking up "..", unlock the parent, and lock "..". If everything
689 * went ok, and we're on the last component and the caller requested the
690 * parent locked, try to re-lock the parent. We do this to prevent lock
691 * races.
692 *
693 * For anything else, get the needed node. Then unlock the parent if not
694 * the last component or not LOCKPARENT (i.e. if we wouldn't re-lock the
695 * parent in the .. case).
696 *
697 * We try to exit with the parent locked in error cases.
698 */
699 int
700 procfs_lookup(v)
701 void *v;
702 {
703 struct vop_lookup_args /* {
704 struct vnode * a_dvp;
705 struct vnode ** a_vpp;
706 struct componentname * a_cnp;
707 } */ *ap = v;
708 struct componentname *cnp = ap->a_cnp;
709 struct vnode **vpp = ap->a_vpp;
710 struct vnode *dvp = ap->a_dvp;
711 const char *pname = cnp->cn_nameptr;
712 const struct proc_target *pt = NULL;
713 struct vnode *fvp;
714 pid_t pid;
715 struct pfsnode *pfs;
716 struct proc *p = NULL;
717 int i, error, wantpunlock, iscurproc = 0, isself = 0;
718
719 *vpp = NULL;
720 cnp->cn_flags &= ~PDIRUNLOCK;
721
722 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
723 return (EROFS);
724
725 if (cnp->cn_namelen == 1 && *pname == '.') {
726 *vpp = dvp;
727 VREF(dvp);
728 return (0);
729 }
730
731 wantpunlock = (~cnp->cn_flags & (LOCKPARENT | ISLASTCN));
732 pfs = VTOPFS(dvp);
733 switch (pfs->pfs_type) {
734 case Proot:
735 /*
736 * Shouldn't get here with .. in the root node.
737 */
738 if (cnp->cn_flags & ISDOTDOT)
739 return (EIO);
740
741 iscurproc = CNEQ(cnp, "curproc", 7);
742 isself = CNEQ(cnp, "self", 4);
743
744 if (iscurproc || isself) {
745 error = procfs_allocvp(dvp->v_mount, vpp, 0,
746 iscurproc ? Pcurproc : Pself);
747 if ((error == 0) && (wantpunlock)) {
748 VOP_UNLOCK(dvp, 0);
749 cnp->cn_flags |= PDIRUNLOCK;
750 }
751 return (error);
752 }
753
754 for (i = 0; i < nproc_root_targets; i++) {
755 pt = &proc_root_targets[i];
756 if (cnp->cn_namelen == pt->pt_namlen &&
757 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
758 (pt->pt_valid == NULL ||
759 (*pt->pt_valid)(LIST_FIRST(&p->p_lwps),
760 dvp->v_mount)))
761 break;
762 }
763
764 if (i != nproc_root_targets) {
765 error = procfs_allocvp(dvp->v_mount, vpp, 0,
766 pt->pt_pfstype);
767 if ((error == 0) && (wantpunlock)) {
768 VOP_UNLOCK(dvp, 0);
769 cnp->cn_flags |= PDIRUNLOCK;
770 }
771 return (error);
772 }
773
774 pid = atopid(pname, cnp->cn_namelen);
775 if (pid == NO_PID)
776 break;
777
778 p = PFIND(pid);
779 if (p == 0)
780 break;
781
782 error = procfs_allocvp(dvp->v_mount, vpp, pid, Pproc);
783 if ((error == 0) && (wantpunlock)) {
784 VOP_UNLOCK(dvp, 0);
785 cnp->cn_flags |= PDIRUNLOCK;
786 }
787 return (error);
788
789 case Pproc:
790 /*
791 * do the .. dance. We unlock the directory, and then
792 * get the root dir. That will automatically return ..
793 * locked. Then if the caller wanted dvp locked, we
794 * re-lock.
795 */
796 if (cnp->cn_flags & ISDOTDOT) {
797 VOP_UNLOCK(dvp, 0);
798 cnp->cn_flags |= PDIRUNLOCK;
799 error = procfs_root(dvp->v_mount, vpp);
800 if ((error == 0) && (wantpunlock == 0) &&
801 ((error = vn_lock(dvp, LK_EXCLUSIVE)) == 0))
802 cnp->cn_flags &= ~PDIRUNLOCK;
803 return (error);
804 }
805
806 p = PFIND(pfs->pfs_pid);
807 if (p == 0)
808 break;
809
810 for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
811 if (cnp->cn_namelen == pt->pt_namlen &&
812 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
813 (pt->pt_valid == NULL ||
814 (*pt->pt_valid)(LIST_FIRST(&p->p_lwps),
815 dvp->v_mount)))
816 goto found;
817 }
818 break;
819
820 found:
821 if (pt->pt_pfstype == Pfile) {
822 fvp = p->p_textvp;
823 /* We already checked that it exists. */
824 VREF(fvp);
825 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY);
826 if (wantpunlock) {
827 VOP_UNLOCK(dvp, 0);
828 cnp->cn_flags |= PDIRUNLOCK;
829 }
830 *vpp = fvp;
831 return (0);
832 }
833
834 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
835 pt->pt_pfstype);
836 if ((error == 0) && (wantpunlock)) {
837 VOP_UNLOCK(dvp, 0);
838 cnp->cn_flags |= PDIRUNLOCK;
839 }
840 return (error);
841
842 default:
843 return (ENOTDIR);
844 }
845
846 return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
847 }
848
849 int
850 procfs_validfile(l, mp)
851 struct lwp *l;
852 struct mount *mp;
853 {
854 return (l->l_proc->p_textvp != NULL);
855 }
856
857 static int
858 procfs_validfile_linux(l, mp)
859 struct lwp *l;
860 struct mount *mp;
861 {
862 int flags;
863
864 flags = VFSTOPROC(mp)->pmnt_flags;
865 return ((flags & PROCFSMNT_LINUXCOMPAT) &&
866 (l == NULL || procfs_validfile(l, mp)));
867 }
868
869 /*
870 * readdir returns directory entries from pfsnode (vp).
871 *
872 * the strategy here with procfs is to generate a single
873 * directory entry at a time (struct dirent) and then
874 * copy that out to userland using uiomove. a more efficent
875 * though more complex implementation, would try to minimize
876 * the number of calls to uiomove(). for procfs, this is
877 * hardly worth the added code complexity.
878 *
879 * this should just be done through read()
880 */
881 int
882 procfs_readdir(v)
883 void *v;
884 {
885 struct vop_readdir_args /* {
886 struct vnode *a_vp;
887 struct uio *a_uio;
888 struct ucred *a_cred;
889 int *a_eofflag;
890 off_t **a_cookies;
891 int *a_ncookies;
892 } */ *ap = v;
893 struct uio *uio = ap->a_uio;
894 struct dirent d;
895 struct pfsnode *pfs;
896 off_t i;
897 int error;
898 off_t *cookies = NULL;
899 int ncookies, left, skip, j;
900 struct vnode *vp;
901 const struct proc_target *pt;
902
903 vp = ap->a_vp;
904 pfs = VTOPFS(vp);
905
906 if (uio->uio_resid < UIO_MX)
907 return (EINVAL);
908 if (uio->uio_offset < 0)
909 return (EINVAL);
910
911 error = 0;
912 i = uio->uio_offset;
913 memset((caddr_t)&d, 0, UIO_MX);
914 d.d_reclen = UIO_MX;
915 ncookies = uio->uio_resid / UIO_MX;
916
917 switch (pfs->pfs_type) {
918 /*
919 * this is for the process-specific sub-directories.
920 * all that is needed to is copy out all the entries
921 * from the procent[] table (top of this file).
922 */
923 case Pproc: {
924 struct proc *p;
925
926 if (i >= nproc_targets)
927 return 0;
928
929 p = PFIND(pfs->pfs_pid);
930 if (p == NULL)
931 break;
932
933 if (ap->a_ncookies) {
934 ncookies = min(ncookies, (nproc_targets - i));
935 cookies = malloc(ncookies * sizeof (off_t),
936 M_TEMP, M_WAITOK);
937 *ap->a_cookies = cookies;
938 }
939
940 for (pt = &proc_targets[i];
941 uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
942 if (pt->pt_valid &&
943 (*pt->pt_valid)(LIST_FIRST(&p->p_lwps),
944 vp->v_mount) == 0)
945 continue;
946
947 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, pt->pt_pfstype);
948 d.d_namlen = pt->pt_namlen;
949 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
950 d.d_type = pt->pt_type;
951
952 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
953 break;
954 if (cookies)
955 *cookies++ = i + 1;
956 }
957
958 break;
959 }
960
961 /*
962 * this is for the root of the procfs filesystem
963 * what is needed are special entries for "curproc"
964 * and "self" followed by an entry for each process
965 * on allproc
966 #ifdef PROCFS_ZOMBIE
967 * and deadproc and zombproc.
968 #endif
969 */
970
971 case Proot: {
972 int pcnt = i, nc = 0;
973 const struct proclist_desc *pd;
974 volatile struct proc *p;
975
976 if (pcnt > 3)
977 pcnt = 3;
978 if (ap->a_ncookies) {
979 /*
980 * XXX Potentially allocating too much space here,
981 * but I'm lazy. This loop needs some work.
982 */
983 cookies = malloc(ncookies * sizeof (off_t),
984 M_TEMP, M_WAITOK);
985 *ap->a_cookies = cookies;
986 }
987 /*
988 * XXX: THIS LOOP ASSUMES THAT allproc IS THE FIRST
989 * PROCLIST IN THE proclists!
990 */
991 proclist_lock_read();
992 pd = proclists;
993 #ifdef PROCFS_ZOMBIE
994 again:
995 #endif
996 for (p = LIST_FIRST(pd->pd_list);
997 p != NULL && uio->uio_resid >= UIO_MX; i++, pcnt++) {
998 switch (i) {
999 case 0: /* `.' */
1000 case 1: /* `..' */
1001 d.d_fileno = PROCFS_FILENO(0, Proot);
1002 d.d_namlen = i + 1;
1003 memcpy(d.d_name, "..", d.d_namlen);
1004 d.d_name[i + 1] = '\0';
1005 d.d_type = DT_DIR;
1006 break;
1007
1008 case 2:
1009 d.d_fileno = PROCFS_FILENO(0, Pcurproc);
1010 d.d_namlen = sizeof("curproc") - 1;
1011 memcpy(d.d_name, "curproc", sizeof("curproc"));
1012 d.d_type = DT_LNK;
1013 break;
1014
1015 case 3:
1016 d.d_fileno = PROCFS_FILENO(0, Pself);
1017 d.d_namlen = sizeof("self") - 1;
1018 memcpy(d.d_name, "self", sizeof("self"));
1019 d.d_type = DT_LNK;
1020 break;
1021
1022 default:
1023 while (pcnt < i) {
1024 pcnt++;
1025 p = LIST_NEXT(p, p_list);
1026 if (!p)
1027 goto done;
1028 }
1029 d.d_fileno = PROCFS_FILENO(p->p_pid, Pproc);
1030 d.d_namlen = sprintf(d.d_name, "%ld",
1031 (long)p->p_pid);
1032 d.d_type = DT_DIR;
1033 p = p->p_list.le_next;
1034 break;
1035 }
1036
1037 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
1038 break;
1039 nc++;
1040 if (cookies)
1041 *cookies++ = i + 1;
1042 }
1043 done:
1044
1045 #ifdef PROCFS_ZOMBIE
1046 pd++;
1047 if (p == NULL && pd->pd_list != NULL)
1048 goto again;
1049 #endif
1050 proclist_unlock_read();
1051
1052 skip = i - pcnt;
1053 if (skip >= nproc_root_targets)
1054 break;
1055 left = nproc_root_targets - skip;
1056 for (j = 0, pt = &proc_root_targets[0];
1057 uio->uio_resid >= UIO_MX && j < left;
1058 pt++, j++, i++) {
1059 if (pt->pt_valid &&
1060 (*pt->pt_valid)(NULL, vp->v_mount) == 0)
1061 continue;
1062 d.d_fileno = PROCFS_FILENO(0, pt->pt_pfstype);
1063 d.d_namlen = pt->pt_namlen;
1064 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1065 d.d_type = pt->pt_type;
1066
1067 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
1068 break;
1069 nc++;
1070 if (cookies)
1071 *cookies++ = i + 1;
1072 }
1073
1074 ncookies = nc;
1075 break;
1076 }
1077
1078 default:
1079 error = ENOTDIR;
1080 break;
1081 }
1082
1083 if (ap->a_ncookies) {
1084 if (error) {
1085 if (cookies)
1086 free(*ap->a_cookies, M_TEMP);
1087 *ap->a_ncookies = 0;
1088 *ap->a_cookies = NULL;
1089 } else
1090 *ap->a_ncookies = ncookies;
1091 }
1092 uio->uio_offset = i;
1093 return (error);
1094 }
1095
1096 /*
1097 * readlink reads the link of `curproc'
1098 */
1099 int
1100 procfs_readlink(v)
1101 void *v;
1102 {
1103 struct vop_readlink_args *ap = v;
1104 char buf[16]; /* should be enough */
1105 int len;
1106
1107 if (VTOPFS(ap->a_vp)->pfs_fileno == PROCFS_FILENO(0, Pcurproc))
1108 len = sprintf(buf, "%ld", (long)curproc->l_proc->p_pid);
1109 else if (VTOPFS(ap->a_vp)->pfs_fileno == PROCFS_FILENO(0, Pself))
1110 len = sprintf(buf, "%s", "curproc");
1111 else
1112 return (EINVAL);
1113
1114 return (uiomove((caddr_t)buf, len, ap->a_uio));
1115 }
1116
1117 /*
1118 * convert decimal ascii to pid_t
1119 */
1120 static pid_t
1121 atopid(b, len)
1122 const char *b;
1123 u_int len;
1124 {
1125 pid_t p = 0;
1126
1127 while (len--) {
1128 char c = *b++;
1129 if (c < '0' || c > '9')
1130 return (NO_PID);
1131 p = 10 * p + (c - '0');
1132 if (p > PID_MAX)
1133 return (NO_PID);
1134 }
1135
1136 return (p);
1137 }
1138