procfs_vnops.c revision 1.95 1 /* $NetBSD: procfs_vnops.c,v 1.95 2003/04/05 23:32:52 dsl 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.95 2003/04/05 23:32:52 dsl 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 const struct proc_target {
85 u_char pt_type;
86 u_char pt_namlen;
87 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 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 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 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_rele
132 #define procfs_mknod genfs_eopnotsupp_rele
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_rele
147 int procfs_link __P((void *));
148 #define procfs_rename genfs_eopnotsupp_rele
149 #define procfs_mkdir genfs_eopnotsupp_rele
150 #define procfs_rmdir genfs_eopnotsupp_rele
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 if ((error = procfs_getfp(pfs, &fp)) != 0)
597 return error;
598 vap->va_nlink = 1;
599 vap->va_uid = fp->f_cred->cr_uid;
600 vap->va_gid = fp->f_cred->cr_gid;
601 switch (fp->f_type) {
602 case DTYPE_VNODE:
603 vap->va_bytes = vap->va_size =
604 ((struct vnode *)fp->f_data)->v_size;
605 break;
606 default:
607 vap->va_bytes = vap->va_size = 0;
608 break;
609 }
610 break;
611 }
612 /*FALLTHROUGH*/
613 case Pproc:
614 vap->va_nlink = 2;
615 vap->va_uid = procp->p_ucred->cr_uid;
616 vap->va_gid = procp->p_ucred->cr_gid;
617 vap->va_bytes = vap->va_size = DEV_BSIZE;
618 break;
619
620 case Pfile:
621 error = EOPNOTSUPP;
622 break;
623
624 case Pmem:
625 vap->va_bytes = vap->va_size =
626 ctob(procp->p_vmspace->vm_tsize +
627 procp->p_vmspace->vm_dsize +
628 procp->p_vmspace->vm_ssize);
629 break;
630
631 #if defined(PT_GETREGS) || defined(PT_SETREGS)
632 case Pregs:
633 vap->va_bytes = vap->va_size = sizeof(struct reg);
634 break;
635 #endif
636
637 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
638 case Pfpregs:
639 vap->va_bytes = vap->va_size = sizeof(struct fpreg);
640 break;
641 #endif
642
643 case Pctl:
644 case Pstatus:
645 case Pnote:
646 case Pnotepg:
647 case Pcmdline:
648 case Pmeminfo:
649 case Pcpuinfo:
650 case Puptime:
651 vap->va_bytes = vap->va_size = 0;
652 break;
653 case Pmap:
654 case Pmaps:
655 /*
656 * Advise a larger blocksize for the map files, so that
657 * they may be read in one pass.
658 */
659 vap->va_blocksize = 4 * PAGE_SIZE;
660 vap->va_bytes = vap->va_size = 0;
661 break;
662
663 #ifdef __HAVE_PROCFS_MACHDEP
664 PROCFS_MACHDEP_NODETYPE_CASES
665 error = procfs_machdep_getattr(ap->a_vp, vap, procp);
666 break;
667 #endif
668
669 default:
670 panic("procfs_getattr");
671 }
672
673 return (error);
674 }
675
676 /*ARGSUSED*/
677 int
678 procfs_setattr(v)
679 void *v;
680 {
681 /*
682 * just fake out attribute setting
683 * it's not good to generate an error
684 * return, otherwise things like creat()
685 * will fail when they try to set the
686 * file length to 0. worse, this means
687 * that echo $note > /proc/$pid/note will fail.
688 */
689
690 return (0);
691 }
692
693 /*
694 * implement access checking.
695 *
696 * actually, the check for super-user is slightly
697 * broken since it will allow read access to write-only
698 * objects. this doesn't cause any particular trouble
699 * but does mean that the i/o entry points need to check
700 * that the operation really does make sense.
701 */
702 int
703 procfs_access(v)
704 void *v;
705 {
706 struct vop_access_args /* {
707 struct vnode *a_vp;
708 int a_mode;
709 struct ucred *a_cred;
710 struct proc *a_p;
711 } */ *ap = v;
712 struct vattr va;
713 int error;
714
715 if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_p)) != 0)
716 return (error);
717
718 return (vaccess(va.va_type, va.va_mode,
719 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
720 }
721
722 /*
723 * lookup. this is incredibly complicated in the
724 * general case, however for most pseudo-filesystems
725 * very little needs to be done.
726 *
727 * Locking isn't hard here, just poorly documented.
728 *
729 * If we're looking up ".", just vref the parent & return it.
730 *
731 * If we're looking up "..", unlock the parent, and lock "..". If everything
732 * went ok, and we're on the last component and the caller requested the
733 * parent locked, try to re-lock the parent. We do this to prevent lock
734 * races.
735 *
736 * For anything else, get the needed node. Then unlock the parent if not
737 * the last component or not LOCKPARENT (i.e. if we wouldn't re-lock the
738 * parent in the .. case).
739 *
740 * We try to exit with the parent locked in error cases.
741 */
742 int
743 procfs_lookup(v)
744 void *v;
745 {
746 struct vop_lookup_args /* {
747 struct vnode * a_dvp;
748 struct vnode ** a_vpp;
749 struct componentname * a_cnp;
750 } */ *ap = v;
751 struct componentname *cnp = ap->a_cnp;
752 struct vnode **vpp = ap->a_vpp;
753 struct vnode *dvp = ap->a_dvp;
754 const char *pname = cnp->cn_nameptr;
755 const struct proc_target *pt = NULL;
756 struct vnode *fvp;
757 pid_t pid;
758 struct pfsnode *pfs;
759 struct proc *p = NULL;
760 int i, error, wantpunlock, iscurproc = 0, isself = 0;
761
762 *vpp = NULL;
763 cnp->cn_flags &= ~PDIRUNLOCK;
764
765 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
766 return (EROFS);
767
768 if (cnp->cn_namelen == 1 && *pname == '.') {
769 *vpp = dvp;
770 VREF(dvp);
771 return (0);
772 }
773
774 wantpunlock = (~cnp->cn_flags & (LOCKPARENT | ISLASTCN));
775 pfs = VTOPFS(dvp);
776 switch (pfs->pfs_type) {
777 case Proot:
778 /*
779 * Shouldn't get here with .. in the root node.
780 */
781 if (cnp->cn_flags & ISDOTDOT)
782 return (EIO);
783
784 iscurproc = CNEQ(cnp, "curproc", 7);
785 isself = CNEQ(cnp, "self", 4);
786
787 if (iscurproc || isself) {
788 error = procfs_allocvp(dvp->v_mount, vpp, 0,
789 iscurproc ? Pcurproc : Pself, -1);
790 if ((error == 0) && (wantpunlock)) {
791 VOP_UNLOCK(dvp, 0);
792 cnp->cn_flags |= PDIRUNLOCK;
793 }
794 return (error);
795 }
796
797 for (i = 0; i < nproc_root_targets; i++) {
798 pt = &proc_root_targets[i];
799 if (cnp->cn_namelen == pt->pt_namlen &&
800 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
801 (pt->pt_valid == NULL ||
802 (*pt->pt_valid)(p, dvp->v_mount)))
803 break;
804 }
805
806 if (i != nproc_root_targets) {
807 error = procfs_allocvp(dvp->v_mount, vpp, 0,
808 pt->pt_pfstype, -1);
809 if ((error == 0) && (wantpunlock)) {
810 VOP_UNLOCK(dvp, 0);
811 cnp->cn_flags |= PDIRUNLOCK;
812 }
813 return (error);
814 }
815
816 pid = (pid_t)atoi(pname, cnp->cn_namelen);
817
818 p = PFIND(pid);
819 if (p == NULL)
820 break;
821
822 error = procfs_allocvp(dvp->v_mount, vpp, pid, Pproc, -1);
823 if ((error == 0) && (wantpunlock)) {
824 VOP_UNLOCK(dvp, 0);
825 cnp->cn_flags |= PDIRUNLOCK;
826 }
827 return (error);
828
829 case Pproc:
830 /*
831 * do the .. dance. We unlock the directory, and then
832 * get the root dir. That will automatically return ..
833 * locked. Then if the caller wanted dvp locked, we
834 * re-lock.
835 */
836 if (cnp->cn_flags & ISDOTDOT) {
837 VOP_UNLOCK(dvp, 0);
838 cnp->cn_flags |= PDIRUNLOCK;
839 error = procfs_root(dvp->v_mount, vpp);
840 if ((error == 0) && (wantpunlock == 0) &&
841 ((error = vn_lock(dvp, LK_EXCLUSIVE)) == 0))
842 cnp->cn_flags &= ~PDIRUNLOCK;
843 return (error);
844 }
845
846 p = PFIND(pfs->pfs_pid);
847 if (p == NULL)
848 break;
849
850 for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
851 if (cnp->cn_namelen == pt->pt_namlen &&
852 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
853 (pt->pt_valid == NULL ||
854 (*pt->pt_valid)(p, dvp->v_mount)))
855 goto found;
856 }
857 break;
858
859 found:
860 if (pt->pt_pfstype == Pfile) {
861 fvp = p->p_textvp;
862 /* We already checked that it exists. */
863 VREF(fvp);
864 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY);
865 if (wantpunlock) {
866 VOP_UNLOCK(dvp, 0);
867 cnp->cn_flags |= PDIRUNLOCK;
868 }
869 *vpp = fvp;
870 return (0);
871 }
872
873 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
874 pt->pt_pfstype, -1);
875 if ((error == 0) && (wantpunlock)) {
876 VOP_UNLOCK(dvp, 0);
877 cnp->cn_flags |= PDIRUNLOCK;
878 }
879 return (error);
880
881 case Pfd: {
882 int fd;
883 struct file *fp;
884 /*
885 * do the .. dance. We unlock the directory, and then
886 * get the proc dir. That will automatically return ..
887 * locked. Then if the caller wanted dvp locked, we
888 * re-lock.
889 */
890 if (cnp->cn_flags & ISDOTDOT) {
891 VOP_UNLOCK(dvp, 0);
892 cnp->cn_flags |= PDIRUNLOCK;
893 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
894 Pproc, -1);
895 if ((error == 0) && (wantpunlock == 0) &&
896 ((error = vn_lock(dvp, LK_EXCLUSIVE)) == 0))
897 cnp->cn_flags &= ~PDIRUNLOCK;
898 return (error);
899 }
900 fd = atoi(pname, cnp->cn_namelen);
901 if (fd == -1)
902 return EINVAL;
903 p = PFIND(pfs->pfs_pid);
904 if (p == NULL)
905 return ESRCH;
906 if ((fp = p->p_fd->fd_ofiles[fd]) == NULL)
907 return ENOENT;
908 switch (fp->f_type) {
909 case DTYPE_VNODE:
910 fvp = (struct vnode *)fp->f_data;
911 VREF(fvp);
912 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY |
913 (p == curproc ? LK_CANRECURSE : 0));
914 *vpp = fvp;
915 error = 0;
916 break;
917 default:
918 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
919 Pfd, fd);
920 break;
921 }
922 if ((error == 0) && (wantpunlock)) {
923 VOP_UNLOCK(dvp, 0);
924 cnp->cn_flags |= PDIRUNLOCK;
925 }
926 return error;
927 }
928 default:
929 return (ENOTDIR);
930 }
931
932 return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
933 }
934
935 int
936 procfs_validfile(p, mp)
937 struct proc *p;
938 struct mount *mp;
939 {
940 return (p->p_textvp != NULL);
941 }
942
943 static int
944 procfs_validfile_linux(p, mp)
945 struct proc *p;
946 struct mount *mp;
947 {
948 int flags;
949
950 flags = VFSTOPROC(mp)->pmnt_flags;
951 return ((flags & PROCFSMNT_LINUXCOMPAT) &&
952 (p == NULL || procfs_validfile(p, mp)));
953 }
954
955 /*
956 * readdir returns directory entries from pfsnode (vp).
957 *
958 * the strategy here with procfs is to generate a single
959 * directory entry at a time (struct dirent) and then
960 * copy that out to userland using uiomove. a more efficent
961 * though more complex implementation, would try to minimize
962 * the number of calls to uiomove(). for procfs, this is
963 * hardly worth the added code complexity.
964 *
965 * this should just be done through read()
966 */
967 int
968 procfs_readdir(v)
969 void *v;
970 {
971 struct vop_readdir_args /* {
972 struct vnode *a_vp;
973 struct uio *a_uio;
974 struct ucred *a_cred;
975 int *a_eofflag;
976 off_t **a_cookies;
977 int *a_ncookies;
978 } */ *ap = v;
979 struct uio *uio = ap->a_uio;
980 struct dirent d;
981 struct pfsnode *pfs;
982 off_t i;
983 int error;
984 off_t *cookies = NULL;
985 int ncookies, left, skip, j;
986 struct vnode *vp;
987 const struct proc_target *pt;
988
989 vp = ap->a_vp;
990 pfs = VTOPFS(vp);
991
992 if (uio->uio_resid < UIO_MX)
993 return (EINVAL);
994 if (uio->uio_offset < 0)
995 return (EINVAL);
996
997 error = 0;
998 i = uio->uio_offset;
999 memset((caddr_t)&d, 0, UIO_MX);
1000 d.d_reclen = UIO_MX;
1001 ncookies = uio->uio_resid / UIO_MX;
1002
1003 switch (pfs->pfs_type) {
1004 /*
1005 * this is for the process-specific sub-directories.
1006 * all that is needed to is copy out all the entries
1007 * from the procent[] table (top of this file).
1008 */
1009 case Pproc: {
1010 struct proc *p;
1011
1012 if (i >= nproc_targets)
1013 return 0;
1014
1015 p = PFIND(pfs->pfs_pid);
1016 if (p == NULL)
1017 break;
1018
1019 if (ap->a_ncookies) {
1020 ncookies = min(ncookies, (nproc_targets - i));
1021 cookies = malloc(ncookies * sizeof (off_t),
1022 M_TEMP, M_WAITOK);
1023 *ap->a_cookies = cookies;
1024 }
1025
1026 for (pt = &proc_targets[i];
1027 uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
1028 if (pt->pt_valid &&
1029 (*pt->pt_valid)(p, vp->v_mount) == 0)
1030 continue;
1031
1032 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
1033 pt->pt_pfstype, -1);
1034 d.d_namlen = pt->pt_namlen;
1035 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1036 d.d_type = pt->pt_type;
1037
1038 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
1039 break;
1040 if (cookies)
1041 *cookies++ = i + 1;
1042 }
1043
1044 break;
1045 }
1046 case Pfd: {
1047 struct proc *p;
1048 struct filedesc *fdp;
1049 int lim, last, nc = 0;
1050
1051 p = PFIND(pfs->pfs_pid);
1052 if (p == NULL)
1053 return ESRCH;
1054
1055 fdp = p->p_fd;
1056
1057 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
1058 last = min(fdp->fd_nfiles, lim);
1059 if (i >= lim)
1060 return 0;
1061
1062 if (ap->a_ncookies) {
1063 ncookies = min(ncookies, (fdp->fd_nfiles + 2 - i));
1064 cookies = malloc(ncookies * sizeof (off_t),
1065 M_TEMP, M_WAITOK);
1066 *ap->a_cookies = cookies;
1067 }
1068
1069 for (; i < 2 && uio->uio_resid >= UIO_MX; i++) {
1070 pt = &proc_targets[i];
1071 d.d_fileno = (pfs->pfs_pid << 8) + i;
1072 d.d_namlen = pt->pt_namlen;
1073 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
1074 pt->pt_pfstype, -1);
1075 (void)memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1076 d.d_type = pt->pt_type;
1077 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
1078 break;
1079 if (cookies)
1080 *cookies++ = i + 1;
1081 nc++;
1082 }
1083 if (error) {
1084 ncookies = nc;
1085 break;
1086 }
1087 for (; uio->uio_resid >= UIO_MX && i < fdp->fd_nfiles; i++) {
1088 if (fdp->fd_ofiles[i - 2] == NULL)
1089 continue;
1090 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, Pfd, i - 2);
1091 d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
1092 "%lld", (long long)(i - 2));
1093 d.d_type = VREG;
1094 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
1095 break;
1096 if (cookies)
1097 *cookies++ = i + 1;
1098 nc++;
1099 }
1100 ncookies = nc;
1101 break;
1102 }
1103
1104 /*
1105 * this is for the root of the procfs filesystem
1106 * what is needed are special entries for "curproc"
1107 * and "self" followed by an entry for each process
1108 * on allproc
1109 #ifdef PROCFS_ZOMBIE
1110 * and deadproc and zombproc.
1111 #endif
1112 */
1113
1114 case Proot: {
1115 int pcnt = i, nc = 0;
1116 const struct proclist_desc *pd;
1117 volatile struct proc *p;
1118
1119 if (pcnt > 3)
1120 pcnt = 3;
1121 if (ap->a_ncookies) {
1122 /*
1123 * XXX Potentially allocating too much space here,
1124 * but I'm lazy. This loop needs some work.
1125 */
1126 cookies = malloc(ncookies * sizeof (off_t),
1127 M_TEMP, M_WAITOK);
1128 *ap->a_cookies = cookies;
1129 }
1130 /*
1131 * XXX: THIS LOOP ASSUMES THAT allproc IS THE FIRST
1132 * PROCLIST IN THE proclists!
1133 */
1134 proclist_lock_read();
1135 pd = proclists;
1136 #ifdef PROCFS_ZOMBIE
1137 again:
1138 #endif
1139 for (p = LIST_FIRST(pd->pd_list);
1140 p != NULL && uio->uio_resid >= UIO_MX; i++, pcnt++) {
1141 switch (i) {
1142 case 0: /* `.' */
1143 case 1: /* `..' */
1144 d.d_fileno = PROCFS_FILENO(0, Proot, -1);
1145 d.d_namlen = i + 1;
1146 memcpy(d.d_name, "..", d.d_namlen);
1147 d.d_name[i + 1] = '\0';
1148 d.d_type = DT_DIR;
1149 break;
1150
1151 case 2:
1152 d.d_fileno = PROCFS_FILENO(0, Pcurproc, -1);
1153 d.d_namlen = sizeof("curproc") - 1;
1154 memcpy(d.d_name, "curproc", sizeof("curproc"));
1155 d.d_type = DT_LNK;
1156 break;
1157
1158 case 3:
1159 d.d_fileno = PROCFS_FILENO(0, Pself, -1);
1160 d.d_namlen = sizeof("self") - 1;
1161 memcpy(d.d_name, "self", sizeof("self"));
1162 d.d_type = DT_LNK;
1163 break;
1164
1165 default:
1166 while (pcnt < i) {
1167 pcnt++;
1168 p = LIST_NEXT(p, p_list);
1169 if (!p)
1170 goto done;
1171 }
1172 d.d_fileno = PROCFS_FILENO(p->p_pid, Pproc, -1);
1173 d.d_namlen = sprintf(d.d_name, "%ld",
1174 (long)p->p_pid);
1175 d.d_type = DT_DIR;
1176 p = p->p_list.le_next;
1177 break;
1178 }
1179
1180 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
1181 break;
1182 nc++;
1183 if (cookies)
1184 *cookies++ = i + 1;
1185 }
1186 done:
1187
1188 #ifdef PROCFS_ZOMBIE
1189 pd++;
1190 if (p == NULL && pd->pd_list != NULL)
1191 goto again;
1192 #endif
1193 proclist_unlock_read();
1194
1195 skip = i - pcnt;
1196 if (skip >= nproc_root_targets)
1197 break;
1198 left = nproc_root_targets - skip;
1199 for (j = 0, pt = &proc_root_targets[0];
1200 uio->uio_resid >= UIO_MX && j < left;
1201 pt++, j++, i++) {
1202 if (pt->pt_valid &&
1203 (*pt->pt_valid)(NULL, vp->v_mount) == 0)
1204 continue;
1205 d.d_fileno = PROCFS_FILENO(0, pt->pt_pfstype, -1);
1206 d.d_namlen = pt->pt_namlen;
1207 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1208 d.d_type = pt->pt_type;
1209
1210 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
1211 break;
1212 nc++;
1213 if (cookies)
1214 *cookies++ = i + 1;
1215 }
1216
1217 ncookies = nc;
1218 break;
1219 }
1220
1221 default:
1222 error = ENOTDIR;
1223 break;
1224 }
1225
1226 if (ap->a_ncookies) {
1227 if (error) {
1228 if (cookies)
1229 free(*ap->a_cookies, M_TEMP);
1230 *ap->a_ncookies = 0;
1231 *ap->a_cookies = NULL;
1232 } else
1233 *ap->a_ncookies = ncookies;
1234 }
1235 uio->uio_offset = i;
1236 return (error);
1237 }
1238
1239 /*
1240 * readlink reads the link of `curproc'
1241 */
1242 int
1243 procfs_readlink(v)
1244 void *v;
1245 {
1246 struct vop_readlink_args *ap = v;
1247 char buf[16]; /* should be enough */
1248 int len;
1249
1250 if (VTOPFS(ap->a_vp)->pfs_fileno == PROCFS_FILENO(0, Pcurproc, -1))
1251 len = sprintf(buf, "%ld", (long)curproc->p_pid);
1252 else if (VTOPFS(ap->a_vp)->pfs_fileno == PROCFS_FILENO(0, Pself, -1))
1253 len = sprintf(buf, "%s", "curproc");
1254 else
1255 return (EINVAL);
1256
1257 return (uiomove((caddr_t)buf, len, ap->a_uio));
1258 }
1259
1260 /*
1261 * convert decimal ascii to int
1262 */
1263 static int
1264 atoi(b, len)
1265 const char *b;
1266 size_t len;
1267 {
1268 int p = 0;
1269
1270 while (len--) {
1271 char c = *b++;
1272 if (c < '0' || c > '9')
1273 return -1;
1274 p = 10 * p + (c - '0');
1275 }
1276
1277 return p;
1278 }
1279