procfs_vnops.c revision 1.94 1 /* $NetBSD: procfs_vnops.c,v 1.94 2003/02/25 21:00:32 jrf 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.94 2003/02/25 21:00:32 jrf 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 if (pid > PID_MAX)
818 break;
819
820 p = PFIND(pid);
821 if (p == NULL)
822 break;
823
824 error = procfs_allocvp(dvp->v_mount, vpp, pid, Pproc, -1);
825 if ((error == 0) && (wantpunlock)) {
826 VOP_UNLOCK(dvp, 0);
827 cnp->cn_flags |= PDIRUNLOCK;
828 }
829 return (error);
830
831 case Pproc:
832 /*
833 * do the .. dance. We unlock the directory, and then
834 * get the root dir. That will automatically return ..
835 * locked. Then if the caller wanted dvp locked, we
836 * re-lock.
837 */
838 if (cnp->cn_flags & ISDOTDOT) {
839 VOP_UNLOCK(dvp, 0);
840 cnp->cn_flags |= PDIRUNLOCK;
841 error = procfs_root(dvp->v_mount, vpp);
842 if ((error == 0) && (wantpunlock == 0) &&
843 ((error = vn_lock(dvp, LK_EXCLUSIVE)) == 0))
844 cnp->cn_flags &= ~PDIRUNLOCK;
845 return (error);
846 }
847
848 p = PFIND(pfs->pfs_pid);
849 if (p == NULL)
850 break;
851
852 for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
853 if (cnp->cn_namelen == pt->pt_namlen &&
854 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
855 (pt->pt_valid == NULL ||
856 (*pt->pt_valid)(p, dvp->v_mount)))
857 goto found;
858 }
859 break;
860
861 found:
862 if (pt->pt_pfstype == Pfile) {
863 fvp = p->p_textvp;
864 /* We already checked that it exists. */
865 VREF(fvp);
866 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY);
867 if (wantpunlock) {
868 VOP_UNLOCK(dvp, 0);
869 cnp->cn_flags |= PDIRUNLOCK;
870 }
871 *vpp = fvp;
872 return (0);
873 }
874
875 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
876 pt->pt_pfstype, -1);
877 if ((error == 0) && (wantpunlock)) {
878 VOP_UNLOCK(dvp, 0);
879 cnp->cn_flags |= PDIRUNLOCK;
880 }
881 return (error);
882
883 case Pfd: {
884 int fd;
885 struct file *fp;
886 /*
887 * do the .. dance. We unlock the directory, and then
888 * get the proc dir. That will automatically return ..
889 * locked. Then if the caller wanted dvp locked, we
890 * re-lock.
891 */
892 if (cnp->cn_flags & ISDOTDOT) {
893 VOP_UNLOCK(dvp, 0);
894 cnp->cn_flags |= PDIRUNLOCK;
895 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
896 Pproc, -1);
897 if ((error == 0) && (wantpunlock == 0) &&
898 ((error = vn_lock(dvp, LK_EXCLUSIVE)) == 0))
899 cnp->cn_flags &= ~PDIRUNLOCK;
900 return (error);
901 }
902 fd = atoi(pname, cnp->cn_namelen);
903 if (fd == -1)
904 return EINVAL;
905 p = PFIND(pfs->pfs_pid);
906 if (p == NULL)
907 return ESRCH;
908 if ((fp = p->p_fd->fd_ofiles[fd]) == NULL)
909 return ENOENT;
910 switch (fp->f_type) {
911 case DTYPE_VNODE:
912 fvp = (struct vnode *)fp->f_data;
913 VREF(fvp);
914 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY |
915 (p == curproc ? LK_CANRECURSE : 0));
916 *vpp = fvp;
917 error = 0;
918 break;
919 default:
920 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
921 Pfd, fd);
922 break;
923 }
924 if ((error == 0) && (wantpunlock)) {
925 VOP_UNLOCK(dvp, 0);
926 cnp->cn_flags |= PDIRUNLOCK;
927 }
928 return error;
929 }
930 default:
931 return (ENOTDIR);
932 }
933
934 return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
935 }
936
937 int
938 procfs_validfile(p, mp)
939 struct proc *p;
940 struct mount *mp;
941 {
942 return (p->p_textvp != NULL);
943 }
944
945 static int
946 procfs_validfile_linux(p, mp)
947 struct proc *p;
948 struct mount *mp;
949 {
950 int flags;
951
952 flags = VFSTOPROC(mp)->pmnt_flags;
953 return ((flags & PROCFSMNT_LINUXCOMPAT) &&
954 (p == NULL || procfs_validfile(p, mp)));
955 }
956
957 /*
958 * readdir returns directory entries from pfsnode (vp).
959 *
960 * the strategy here with procfs is to generate a single
961 * directory entry at a time (struct dirent) and then
962 * copy that out to userland using uiomove. a more efficent
963 * though more complex implementation, would try to minimize
964 * the number of calls to uiomove(). for procfs, this is
965 * hardly worth the added code complexity.
966 *
967 * this should just be done through read()
968 */
969 int
970 procfs_readdir(v)
971 void *v;
972 {
973 struct vop_readdir_args /* {
974 struct vnode *a_vp;
975 struct uio *a_uio;
976 struct ucred *a_cred;
977 int *a_eofflag;
978 off_t **a_cookies;
979 int *a_ncookies;
980 } */ *ap = v;
981 struct uio *uio = ap->a_uio;
982 struct dirent d;
983 struct pfsnode *pfs;
984 off_t i;
985 int error;
986 off_t *cookies = NULL;
987 int ncookies, left, skip, j;
988 struct vnode *vp;
989 const struct proc_target *pt;
990
991 vp = ap->a_vp;
992 pfs = VTOPFS(vp);
993
994 if (uio->uio_resid < UIO_MX)
995 return (EINVAL);
996 if (uio->uio_offset < 0)
997 return (EINVAL);
998
999 error = 0;
1000 i = uio->uio_offset;
1001 memset((caddr_t)&d, 0, UIO_MX);
1002 d.d_reclen = UIO_MX;
1003 ncookies = uio->uio_resid / UIO_MX;
1004
1005 switch (pfs->pfs_type) {
1006 /*
1007 * this is for the process-specific sub-directories.
1008 * all that is needed to is copy out all the entries
1009 * from the procent[] table (top of this file).
1010 */
1011 case Pproc: {
1012 struct proc *p;
1013
1014 if (i >= nproc_targets)
1015 return 0;
1016
1017 p = PFIND(pfs->pfs_pid);
1018 if (p == NULL)
1019 break;
1020
1021 if (ap->a_ncookies) {
1022 ncookies = min(ncookies, (nproc_targets - i));
1023 cookies = malloc(ncookies * sizeof (off_t),
1024 M_TEMP, M_WAITOK);
1025 *ap->a_cookies = cookies;
1026 }
1027
1028 for (pt = &proc_targets[i];
1029 uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
1030 if (pt->pt_valid &&
1031 (*pt->pt_valid)(p, vp->v_mount) == 0)
1032 continue;
1033
1034 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
1035 pt->pt_pfstype, -1);
1036 d.d_namlen = pt->pt_namlen;
1037 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1038 d.d_type = pt->pt_type;
1039
1040 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
1041 break;
1042 if (cookies)
1043 *cookies++ = i + 1;
1044 }
1045
1046 break;
1047 }
1048 case Pfd: {
1049 struct proc *p;
1050 struct filedesc *fdp;
1051 int lim, last, nc = 0;
1052
1053 p = PFIND(pfs->pfs_pid);
1054 if (p == NULL)
1055 return ESRCH;
1056
1057 fdp = p->p_fd;
1058
1059 lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
1060 last = min(fdp->fd_nfiles, lim);
1061 if (i >= lim)
1062 return 0;
1063
1064 if (ap->a_ncookies) {
1065 ncookies = min(ncookies, (fdp->fd_nfiles + 2 - i));
1066 cookies = malloc(ncookies * sizeof (off_t),
1067 M_TEMP, M_WAITOK);
1068 *ap->a_cookies = cookies;
1069 }
1070
1071 for (; i < 2 && uio->uio_resid >= UIO_MX; i++) {
1072 pt = &proc_targets[i];
1073 d.d_fileno = (pfs->pfs_pid << 8) + i;
1074 d.d_namlen = pt->pt_namlen;
1075 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
1076 pt->pt_pfstype, -1);
1077 (void)memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1078 d.d_type = pt->pt_type;
1079 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
1080 break;
1081 if (cookies)
1082 *cookies++ = i + 1;
1083 nc++;
1084 }
1085 if (error) {
1086 ncookies = nc;
1087 break;
1088 }
1089 for (; uio->uio_resid >= UIO_MX && i < fdp->fd_nfiles; i++) {
1090 if (fdp->fd_ofiles[i - 2] == NULL)
1091 continue;
1092 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, Pfd, i - 2);
1093 d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
1094 "%lld", (long long)(i - 2));
1095 d.d_type = VREG;
1096 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
1097 break;
1098 if (cookies)
1099 *cookies++ = i + 1;
1100 nc++;
1101 }
1102 ncookies = nc;
1103 break;
1104 }
1105
1106 /*
1107 * this is for the root of the procfs filesystem
1108 * what is needed are special entries for "curproc"
1109 * and "self" followed by an entry for each process
1110 * on allproc
1111 #ifdef PROCFS_ZOMBIE
1112 * and deadproc and zombproc.
1113 #endif
1114 */
1115
1116 case Proot: {
1117 int pcnt = i, nc = 0;
1118 const struct proclist_desc *pd;
1119 volatile struct proc *p;
1120
1121 if (pcnt > 3)
1122 pcnt = 3;
1123 if (ap->a_ncookies) {
1124 /*
1125 * XXX Potentially allocating too much space here,
1126 * but I'm lazy. This loop needs some work.
1127 */
1128 cookies = malloc(ncookies * sizeof (off_t),
1129 M_TEMP, M_WAITOK);
1130 *ap->a_cookies = cookies;
1131 }
1132 /*
1133 * XXX: THIS LOOP ASSUMES THAT allproc IS THE FIRST
1134 * PROCLIST IN THE proclists!
1135 */
1136 proclist_lock_read();
1137 pd = proclists;
1138 #ifdef PROCFS_ZOMBIE
1139 again:
1140 #endif
1141 for (p = LIST_FIRST(pd->pd_list);
1142 p != NULL && uio->uio_resid >= UIO_MX; i++, pcnt++) {
1143 switch (i) {
1144 case 0: /* `.' */
1145 case 1: /* `..' */
1146 d.d_fileno = PROCFS_FILENO(0, Proot, -1);
1147 d.d_namlen = i + 1;
1148 memcpy(d.d_name, "..", d.d_namlen);
1149 d.d_name[i + 1] = '\0';
1150 d.d_type = DT_DIR;
1151 break;
1152
1153 case 2:
1154 d.d_fileno = PROCFS_FILENO(0, Pcurproc, -1);
1155 d.d_namlen = sizeof("curproc") - 1;
1156 memcpy(d.d_name, "curproc", sizeof("curproc"));
1157 d.d_type = DT_LNK;
1158 break;
1159
1160 case 3:
1161 d.d_fileno = PROCFS_FILENO(0, Pself, -1);
1162 d.d_namlen = sizeof("self") - 1;
1163 memcpy(d.d_name, "self", sizeof("self"));
1164 d.d_type = DT_LNK;
1165 break;
1166
1167 default:
1168 while (pcnt < i) {
1169 pcnt++;
1170 p = LIST_NEXT(p, p_list);
1171 if (!p)
1172 goto done;
1173 }
1174 d.d_fileno = PROCFS_FILENO(p->p_pid, Pproc, -1);
1175 d.d_namlen = sprintf(d.d_name, "%ld",
1176 (long)p->p_pid);
1177 d.d_type = DT_DIR;
1178 p = p->p_list.le_next;
1179 break;
1180 }
1181
1182 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
1183 break;
1184 nc++;
1185 if (cookies)
1186 *cookies++ = i + 1;
1187 }
1188 done:
1189
1190 #ifdef PROCFS_ZOMBIE
1191 pd++;
1192 if (p == NULL && pd->pd_list != NULL)
1193 goto again;
1194 #endif
1195 proclist_unlock_read();
1196
1197 skip = i - pcnt;
1198 if (skip >= nproc_root_targets)
1199 break;
1200 left = nproc_root_targets - skip;
1201 for (j = 0, pt = &proc_root_targets[0];
1202 uio->uio_resid >= UIO_MX && j < left;
1203 pt++, j++, i++) {
1204 if (pt->pt_valid &&
1205 (*pt->pt_valid)(NULL, vp->v_mount) == 0)
1206 continue;
1207 d.d_fileno = PROCFS_FILENO(0, pt->pt_pfstype, -1);
1208 d.d_namlen = pt->pt_namlen;
1209 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
1210 d.d_type = pt->pt_type;
1211
1212 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
1213 break;
1214 nc++;
1215 if (cookies)
1216 *cookies++ = i + 1;
1217 }
1218
1219 ncookies = nc;
1220 break;
1221 }
1222
1223 default:
1224 error = ENOTDIR;
1225 break;
1226 }
1227
1228 if (ap->a_ncookies) {
1229 if (error) {
1230 if (cookies)
1231 free(*ap->a_cookies, M_TEMP);
1232 *ap->a_ncookies = 0;
1233 *ap->a_cookies = NULL;
1234 } else
1235 *ap->a_ncookies = ncookies;
1236 }
1237 uio->uio_offset = i;
1238 return (error);
1239 }
1240
1241 /*
1242 * readlink reads the link of `curproc'
1243 */
1244 int
1245 procfs_readlink(v)
1246 void *v;
1247 {
1248 struct vop_readlink_args *ap = v;
1249 char buf[16]; /* should be enough */
1250 int len;
1251
1252 if (VTOPFS(ap->a_vp)->pfs_fileno == PROCFS_FILENO(0, Pcurproc, -1))
1253 len = sprintf(buf, "%ld", (long)curproc->p_pid);
1254 else if (VTOPFS(ap->a_vp)->pfs_fileno == PROCFS_FILENO(0, Pself, -1))
1255 len = sprintf(buf, "%s", "curproc");
1256 else
1257 return (EINVAL);
1258
1259 return (uiomove((caddr_t)buf, len, ap->a_uio));
1260 }
1261
1262 /*
1263 * convert decimal ascii to int
1264 */
1265 static int
1266 atoi(b, len)
1267 const char *b;
1268 size_t len;
1269 {
1270 int p = 0;
1271
1272 while (len--) {
1273 char c = *b++;
1274 if (c < '0' || c > '9')
1275 return -1;
1276 p = 10 * p + (c - '0');
1277 }
1278
1279 return p;
1280 }
1281