procfs_vnops.c revision 1.83.2.1 1 /* $NetBSD: procfs_vnops.c,v 1.83.2.1 2001/09/18 19:13:56 fvdl 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/proc.h>
52 #include <sys/vnode.h>
53 #include <sys/namei.h>
54 #include <sys/malloc.h>
55 #include <sys/mount.h>
56 #include <sys/dirent.h>
57 #include <sys/resourcevar.h>
58 #include <sys/ptrace.h>
59 #include <sys/stat.h>
60
61 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
62
63 #include <machine/reg.h>
64
65 #include <miscfs/genfs/genfs.h>
66 #include <miscfs/procfs/procfs.h>
67
68 /*
69 * Vnode Operations.
70 *
71 */
72
73 static int procfs_validfile_linux __P((struct proc *, struct mount *));
74
75 /*
76 * This is a list of the valid names in the
77 * process-specific sub-directories. It is
78 * used in procfs_lookup and procfs_readdir
79 */
80 const struct proc_target {
81 u_char pt_type;
82 u_char pt_namlen;
83 char *pt_name;
84 pfstype pt_pfstype;
85 int (*pt_valid) __P((struct proc *, struct mount *));
86 } proc_targets[] = {
87 #define N(s) sizeof(s)-1, s
88 /* name type validp */
89 { DT_DIR, N("."), Pproc, NULL },
90 { DT_DIR, N(".."), Proot, NULL },
91 { DT_REG, N("file"), Pfile, procfs_validfile },
92 { DT_REG, N("mem"), Pmem, NULL },
93 { DT_REG, N("regs"), Pregs, procfs_validregs },
94 { DT_REG, N("fpregs"), Pfpregs, procfs_validfpregs },
95 { DT_REG, N("ctl"), Pctl, NULL },
96 { DT_REG, N("status"), Pstatus, NULL },
97 { DT_REG, N("note"), Pnote, NULL },
98 { DT_REG, N("notepg"), Pnotepg, NULL },
99 { DT_REG, N("map"), Pmap, procfs_validmap },
100 { DT_REG, N("maps"), Pmaps, procfs_validmap },
101 { DT_REG, N("cmdline"), Pcmdline, NULL },
102 { DT_REG, N("exe"), Pfile, procfs_validfile_linux },
103 #undef N
104 };
105 static int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]);
106
107 /*
108 * List of files in the root directory. Note: the validate function will
109 * be called with p == NULL for these ones.
110 */
111 struct proc_target proc_root_targets[] = {
112 #define N(s) sizeof(s)-1, s
113 /* name type validp */
114 { DT_REG, N("meminfo"), Pmeminfo, procfs_validfile_linux },
115 { DT_REG, N("cpuinfo"), Pcpuinfo, procfs_validfile_linux },
116 #undef N
117 };
118 static int nproc_root_targets =
119 sizeof(proc_root_targets) / sizeof(proc_root_targets[0]);
120
121 int procfs_lookup __P((void *));
122 #define procfs_create genfs_eopnotsupp_rele
123 #define procfs_mknod genfs_eopnotsupp_rele
124 int procfs_open __P((void *));
125 int procfs_close __P((void *));
126 int procfs_access __P((void *));
127 int procfs_getattr __P((void *));
128 int procfs_setattr __P((void *));
129 #define procfs_read procfs_rw
130 #define procfs_write procfs_rw
131 #define procfs_fcntl genfs_fcntl
132 #define procfs_ioctl genfs_enoioctl
133 #define procfs_poll genfs_poll
134 #define procfs_revoke genfs_revoke
135 #define procfs_fsync genfs_nullop
136 #define procfs_seek genfs_nullop
137 #define procfs_remove genfs_eopnotsupp_rele
138 int procfs_link __P((void *));
139 #define procfs_rename genfs_eopnotsupp_rele
140 #define procfs_mkdir genfs_eopnotsupp_rele
141 #define procfs_rmdir genfs_eopnotsupp_rele
142 int procfs_symlink __P((void *));
143 int procfs_readdir __P((void *));
144 int procfs_readlink __P((void *));
145 #define procfs_abortop genfs_abortop
146 int procfs_inactive __P((void *));
147 int procfs_reclaim __P((void *));
148 #define procfs_lock genfs_lock
149 #define procfs_unlock genfs_unlock
150 #define procfs_bmap genfs_badop
151 #define procfs_strategy genfs_badop
152 int procfs_print __P((void *));
153 int procfs_pathconf __P((void *));
154 #define procfs_islocked genfs_islocked
155 #define procfs_advlock genfs_einval
156 #define procfs_blkatoff genfs_eopnotsupp
157 #define procfs_valloc genfs_eopnotsupp
158 #define procfs_vfree genfs_nullop
159 #define procfs_truncate genfs_eopnotsupp
160 #define procfs_update genfs_nullop
161 #define procfs_bwrite genfs_eopnotsupp
162
163 static pid_t atopid __P((const char *, u_int));
164
165 /*
166 * procfs vnode operations.
167 */
168 int (**procfs_vnodeop_p) __P((void *));
169 const struct vnodeopv_entry_desc procfs_vnodeop_entries[] = {
170 { &vop_default_desc, vn_default_error },
171 { &vop_lookup_desc, procfs_lookup }, /* lookup */
172 { &vop_create_desc, procfs_create }, /* create */
173 { &vop_mknod_desc, procfs_mknod }, /* mknod */
174 { &vop_open_desc, procfs_open }, /* open */
175 { &vop_close_desc, procfs_close }, /* close */
176 { &vop_access_desc, procfs_access }, /* access */
177 { &vop_getattr_desc, procfs_getattr }, /* getattr */
178 { &vop_setattr_desc, procfs_setattr }, /* setattr */
179 { &vop_read_desc, procfs_read }, /* read */
180 { &vop_write_desc, procfs_write }, /* write */
181 { &vop_fcntl_desc, procfs_fcntl }, /* fcntl */
182 { &vop_ioctl_desc, procfs_ioctl }, /* ioctl */
183 { &vop_poll_desc, procfs_poll }, /* poll */
184 { &vop_revoke_desc, procfs_revoke }, /* revoke */
185 { &vop_fsync_desc, procfs_fsync }, /* fsync */
186 { &vop_seek_desc, procfs_seek }, /* seek */
187 { &vop_remove_desc, procfs_remove }, /* remove */
188 { &vop_link_desc, procfs_link }, /* link */
189 { &vop_rename_desc, procfs_rename }, /* rename */
190 { &vop_mkdir_desc, procfs_mkdir }, /* mkdir */
191 { &vop_rmdir_desc, procfs_rmdir }, /* rmdir */
192 { &vop_symlink_desc, procfs_symlink }, /* symlink */
193 { &vop_readdir_desc, procfs_readdir }, /* readdir */
194 { &vop_readlink_desc, procfs_readlink }, /* readlink */
195 { &vop_abortop_desc, procfs_abortop }, /* abortop */
196 { &vop_inactive_desc, procfs_inactive }, /* inactive */
197 { &vop_reclaim_desc, procfs_reclaim }, /* reclaim */
198 { &vop_lock_desc, procfs_lock }, /* lock */
199 { &vop_unlock_desc, procfs_unlock }, /* unlock */
200 { &vop_bmap_desc, procfs_bmap }, /* bmap */
201 { &vop_strategy_desc, procfs_strategy }, /* strategy */
202 { &vop_print_desc, procfs_print }, /* print */
203 { &vop_islocked_desc, procfs_islocked }, /* islocked */
204 { &vop_pathconf_desc, procfs_pathconf }, /* pathconf */
205 { &vop_advlock_desc, procfs_advlock }, /* advlock */
206 { &vop_blkatoff_desc, procfs_blkatoff }, /* blkatoff */
207 { &vop_valloc_desc, procfs_valloc }, /* valloc */
208 { &vop_vfree_desc, procfs_vfree }, /* vfree */
209 { &vop_truncate_desc, procfs_truncate }, /* truncate */
210 { &vop_update_desc, procfs_update }, /* update */
211 { NULL, NULL }
212 };
213 const struct vnodeopv_desc procfs_vnodeop_opv_desc =
214 { &procfs_vnodeop_p, procfs_vnodeop_entries };
215 /*
216 * set things up for doing i/o on
217 * the pfsnode (vp). (vp) is locked
218 * on entry, and should be left locked
219 * on exit.
220 *
221 * for procfs we don't need to do anything
222 * in particular for i/o. all that is done
223 * is to support exclusive open on process
224 * memory images.
225 */
226 int
227 procfs_open(v)
228 void *v;
229 {
230 struct vop_open_args /* {
231 struct vnode *a_vp;
232 int a_mode;
233 struct ucred *a_cred;
234 struct proc *a_p;
235 struct vnode *a_vpp;
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->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)(p, 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)(p, dvp->v_mount)))
815 goto found;
816 }
817 break;
818
819 found:
820 if (pt->pt_pfstype == Pfile) {
821 fvp = p->p_textvp;
822 /* We already checked that it exists. */
823 VREF(fvp);
824 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY);
825 if (wantpunlock) {
826 VOP_UNLOCK(dvp, 0);
827 cnp->cn_flags |= PDIRUNLOCK;
828 }
829 *vpp = fvp;
830 return (0);
831 }
832
833 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
834 pt->pt_pfstype);
835 if ((error == 0) && (wantpunlock)) {
836 VOP_UNLOCK(dvp, 0);
837 cnp->cn_flags |= PDIRUNLOCK;
838 }
839 return (error);
840
841 default:
842 return (ENOTDIR);
843 }
844
845 return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
846 }
847
848 int
849 procfs_validfile(p, mp)
850 struct proc *p;
851 struct mount *mp;
852 {
853 return (p->p_textvp != NULL);
854 }
855
856 static int
857 procfs_validfile_linux(p, mp)
858 struct proc *p;
859 struct mount *mp;
860 {
861 int flags;
862
863 flags = VFSTOPROC(mp)->pmnt_flags;
864 return ((flags & PROCFSMNT_LINUXCOMPAT) &&
865 (p == NULL || procfs_validfile(p, mp)));
866 }
867
868 /*
869 * readdir returns directory entries from pfsnode (vp).
870 *
871 * the strategy here with procfs is to generate a single
872 * directory entry at a time (struct dirent) and then
873 * copy that out to userland using uiomove. a more efficent
874 * though more complex implementation, would try to minimize
875 * the number of calls to uiomove(). for procfs, this is
876 * hardly worth the added code complexity.
877 *
878 * this should just be done through read()
879 */
880 int
881 procfs_readdir(v)
882 void *v;
883 {
884 struct vop_readdir_args /* {
885 struct vnode *a_vp;
886 struct uio *a_uio;
887 struct ucred *a_cred;
888 int *a_eofflag;
889 off_t **a_cookies;
890 int *a_ncookies;
891 } */ *ap = v;
892 struct uio *uio = ap->a_uio;
893 struct dirent d;
894 struct pfsnode *pfs;
895 off_t i;
896 int error;
897 off_t *cookies = NULL;
898 int ncookies, left, skip, j;
899 struct vnode *vp;
900 const struct proc_target *pt;
901
902 vp = ap->a_vp;
903 pfs = VTOPFS(vp);
904
905 if (uio->uio_resid < UIO_MX)
906 return (EINVAL);
907 if (uio->uio_offset < 0)
908 return (EINVAL);
909
910 error = 0;
911 i = uio->uio_offset;
912 memset((caddr_t)&d, 0, UIO_MX);
913 d.d_reclen = UIO_MX;
914 ncookies = uio->uio_resid / UIO_MX;
915
916 switch (pfs->pfs_type) {
917 /*
918 * this is for the process-specific sub-directories.
919 * all that is needed to is copy out all the entries
920 * from the procent[] table (top of this file).
921 */
922 case Pproc: {
923 struct proc *p;
924
925 if (i >= nproc_targets)
926 return 0;
927
928 p = PFIND(pfs->pfs_pid);
929 if (p == NULL)
930 break;
931
932 if (ap->a_ncookies) {
933 ncookies = min(ncookies, (nproc_targets - i));
934 cookies = malloc(ncookies * sizeof (off_t),
935 M_TEMP, M_WAITOK);
936 *ap->a_cookies = cookies;
937 }
938
939 for (pt = &proc_targets[i];
940 uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
941 if (pt->pt_valid &&
942 (*pt->pt_valid)(p, vp->v_mount) == 0)
943 continue;
944
945 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, pt->pt_pfstype);
946 d.d_namlen = pt->pt_namlen;
947 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
948 d.d_type = pt->pt_type;
949
950 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
951 break;
952 if (cookies)
953 *cookies++ = i + 1;
954 }
955
956 break;
957 }
958
959 /*
960 * this is for the root of the procfs filesystem
961 * what is needed are special entries for "curproc"
962 * and "self" followed by an entry for each process
963 * on allproc
964 #ifdef PROCFS_ZOMBIE
965 * and deadproc and zombproc.
966 #endif
967 */
968
969 case Proot: {
970 int pcnt = i, nc = 0;
971 const struct proclist_desc *pd;
972 volatile struct proc *p;
973
974 if (pcnt > 3)
975 pcnt = 3;
976 if (ap->a_ncookies) {
977 /*
978 * XXX Potentially allocating too much space here,
979 * but I'm lazy. This loop needs some work.
980 */
981 cookies = malloc(ncookies * sizeof (off_t),
982 M_TEMP, M_WAITOK);
983 *ap->a_cookies = cookies;
984 }
985 /*
986 * XXX: THIS LOOP ASSUMES THAT allproc IS THE FIRST
987 * PROCLIST IN THE proclists!
988 */
989 proclist_lock_read();
990 pd = proclists;
991 #ifdef PROCFS_ZOMBIE
992 again:
993 #endif
994 for (p = LIST_FIRST(pd->pd_list);
995 p != NULL && uio->uio_resid >= UIO_MX; i++, pcnt++) {
996 switch (i) {
997 case 0: /* `.' */
998 case 1: /* `..' */
999 d.d_fileno = PROCFS_FILENO(0, Proot);
1000 d.d_namlen = i + 1;
1001 memcpy(d.d_name, "..", d.d_namlen);
1002 d.d_name[i + 1] = '\0';
1003 d.d_type = DT_DIR;
1004 break;
1005
1006 case 2:
1007 d.d_fileno = PROCFS_FILENO(0, Pcurproc);
1008 d.d_namlen = sizeof("curproc") - 1;
1009 memcpy(d.d_name, "curproc", sizeof("curproc"));
1010 d.d_type = DT_LNK;
1011 break;
1012
1013 case 3:
1014 d.d_fileno = PROCFS_FILENO(0, Pself);
1015 d.d_namlen = sizeof("self") - 1;
1016 memcpy(d.d_name, "self", sizeof("self"));
1017 d.d_type = DT_LNK;
1018 break;
1019
1020 default:
1021 while (pcnt < i) {
1022 pcnt++;
1023 p = LIST_NEXT(p, p_list);
1024 if (!p)
1025 goto done;
1026 }
1027 d.d_fileno = PROCFS_FILENO(p->p_pid, Pproc);
1028 d.d_namlen = sprintf(d.d_name, "%ld",
1029 (long)p->p_pid);
1030 d.d_type = DT_DIR;
1031 p = p->p_list.le_next;
1032 break;
1033 }
1034
1035 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
1036 break;
1037 nc++;
1038 if (cookies)
1039 *cookies++ = i + 1;
1040 }
1041 done:
1042
1043 #ifdef PROCFS_ZOMBIE
1044 pd++;
1045 if (p == NULL && pd->pd_list != NULL)
1046 goto again;
1047 #endif
1048 proclist_unlock_read();
1049
1050 skip = i - pcnt;
1051 if (skip >= nproc_root_targets)
1052 break;
1053 left = nproc_root_targets - skip;
1054 for (j = 0, pt = &proc_root_targets[0];
1055 uio->uio_resid >= UIO_MX && j < left;
1056 pt++, j++, i++) {
1057 if (pt->pt_valid &&
1058 (*pt->pt_valid)(NULL, vp->v_mount) == 0)
1059 continue;
1060 d.d_fileno = PROCFS_FILENO(0, pt->pt_pfstype);
1061 d.d_namlen = pt->pt_namlen;
1062 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1063 d.d_type = pt->pt_type;
1064
1065 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
1066 break;
1067 nc++;
1068 if (cookies)
1069 *cookies++ = i + 1;
1070 }
1071
1072 ncookies = nc;
1073 break;
1074 }
1075
1076 default:
1077 error = ENOTDIR;
1078 break;
1079 }
1080
1081 if (ap->a_ncookies) {
1082 if (error) {
1083 if (cookies)
1084 free(*ap->a_cookies, M_TEMP);
1085 *ap->a_ncookies = 0;
1086 *ap->a_cookies = NULL;
1087 } else
1088 *ap->a_ncookies = ncookies;
1089 }
1090 uio->uio_offset = i;
1091 return (error);
1092 }
1093
1094 /*
1095 * readlink reads the link of `curproc'
1096 */
1097 int
1098 procfs_readlink(v)
1099 void *v;
1100 {
1101 struct vop_readlink_args *ap = v;
1102 char buf[16]; /* should be enough */
1103 int len;
1104
1105 if (VTOPFS(ap->a_vp)->pfs_fileno == PROCFS_FILENO(0, Pcurproc))
1106 len = sprintf(buf, "%ld", (long)curproc->p_pid);
1107 else if (VTOPFS(ap->a_vp)->pfs_fileno == PROCFS_FILENO(0, Pself))
1108 len = sprintf(buf, "%s", "curproc");
1109 else
1110 return (EINVAL);
1111
1112 return (uiomove((caddr_t)buf, len, ap->a_uio));
1113 }
1114
1115 /*
1116 * convert decimal ascii to pid_t
1117 */
1118 static pid_t
1119 atopid(b, len)
1120 const char *b;
1121 u_int len;
1122 {
1123 pid_t p = 0;
1124
1125 while (len--) {
1126 char c = *b++;
1127 if (c < '0' || c > '9')
1128 return (NO_PID);
1129 p = 10 * p + (c - '0');
1130 if (p > PID_MAX)
1131 return (NO_PID);
1132 }
1133
1134 return (p);
1135 }
1136