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