procfs_vnops.c revision 1.11 1 /*
2 * Copyright (c) 1993 The Regents of the University of California.
3 * Copyright (c) 1993 Jan-Simon Pendry
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Jan-Simon Pendry.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * From:
38 * Id: procfs_vnops.c,v 4.2 1994/01/02 15:28:44 jsp Exp
39 *
40 * $Id: procfs_vnops.c,v 1.11 1994/01/05 21:56:02 cgd Exp $
41 */
42
43 /*
44 * procfs vnode interface
45 */
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/time.h>
50 #include <sys/kernel.h>
51 #include <sys/file.h>
52 #include <sys/proc.h>
53 #include <sys/vnode.h>
54 #include <sys/namei.h>
55 #include <sys/malloc.h>
56 #include <sys/resourcevar.h>
57 #include <miscfs/procfs/procfs.h>
58 #include <vm/vm.h> /* for page_size */
59
60 #include <machine/reg.h>
61
62 /*
63 * Vnode Operations.
64 *
65 */
66
67 /*
68 * This is a list of the valid names in the
69 * process-specific sub-directories. It is
70 * used in procfs_lookup and procfs_readdir
71 */
72 static struct pfsnames {
73 u_short d_namlen;
74 char d_name[PROCFS_NAMELEN];
75 pfstype d_pfstype;
76 } procent[] = {
77 #define N(s) sizeof(s)-1, s
78 /* namlen, nam, type */
79 { N("file"), Pfile },
80 { N("mem"), Pmem },
81 { N("regs"), Pregs },
82 { N("ctl"), Pctl },
83 { N("status"), Pstatus },
84 { N("note"), Pnote },
85 { N("notepg"), Pnotepg },
86 #undef N
87 };
88 #define Nprocent (sizeof(procent)/sizeof(procent[0]))
89
90 static pid_t atopid __P((const char *, u_int));
91
92 /*
93 * set things up for doing i/o on
94 * the pfsnode (vp). (vp) is locked
95 * on entry, and should be left locked
96 * on exit.
97 *
98 * for procfs we don't need to do anything
99 * in particular for i/o. all that is done
100 * is to support exclusive open on process
101 * memory images.
102 */
103 procfs_open(vp, mode, cred, p)
104 struct vnode *vp;
105 int mode;
106 struct ucred *cred;
107 struct proc *p;
108 {
109 struct pfsnode *pfs = VTOPFS(vp);
110
111 switch (pfs->pfs_type) {
112 case Pmem:
113 if (PFIND(pfs->pfs_pid) == 0)
114 return (ENOENT); /* was ESRCH, jsp */
115
116 if ((pfs->pfs_flags & FWRITE) && (mode & O_EXCL) ||
117 (pfs->pfs_flags & O_EXCL) && (mode & FWRITE))
118 return (EBUSY);
119
120
121 if (mode & FWRITE)
122 pfs->pfs_flags = (mode & (FWRITE|O_EXCL));
123
124 return (0);
125
126 default:
127 break;
128 }
129
130 return (0);
131 }
132
133 /*
134 * close the pfsnode (vp) after doing i/o.
135 * (vp) is not locked on entry or exit.
136 *
137 * nothing to do for procfs other than undo
138 * any exclusive open flag (see _open above).
139 */
140 procfs_close(vp, flag, cred, p)
141 struct vnode *vp;
142 int flag;
143 struct ucred *cred;
144 struct proc *p;
145 {
146 struct pfsnode *pfs = VTOPFS(vp);
147
148 switch (pfs->pfs_type) {
149 case Pmem:
150 if ((flag & FWRITE) && (pfs->pfs_flags & O_EXCL))
151 pfs->pfs_flags &= ~(FWRITE|O_EXCL);
152 break;
153 }
154
155 return (0);
156 }
157
158 /*
159 * do an ioctl operation on pfsnode (vp).
160 * (vp) is not locked on entry or exit.
161 */
162 procfs_ioctl(vp, com, data, fflag, cred, p)
163 struct vnode *vp;
164 int com;
165 caddr_t data;
166 int fflag;
167 struct ucred *cred;
168 struct proc *p;
169 {
170
171 return (ENOTTY);
172 }
173
174 /*
175 * do block mapping for pfsnode (vp).
176 * since we don't use the buffer cache
177 * for procfs this function should never
178 * be called. in any case, it's not clear
179 * what part of the kernel ever makes use
180 * of this function. for sanity, this is the
181 * usual no-op bmap, although returning
182 * (EIO) would be a reasonable alternative.
183 */
184 procfs_bmap(vp, bn, vpp, bnp)
185 struct vnode *vp;
186 daddr_t bn;
187 struct vnode **vpp;
188 daddr_t *bnp;
189 {
190
191 if (vpp != NULL)
192 *vpp = vp;
193 if (bnp != NULL)
194 *bnp = bn;
195 return (0);
196 }
197
198 /*
199 * _inactive is called when the pfsnode
200 * is vrele'd and the reference count goes
201 * to zero. (vp) will be on the vnode free
202 * list, so to get it back vget() must be
203 * used.
204 *
205 * for procfs, check if the process is still
206 * alive and if it isn't then just throw away
207 * the vnode by calling vgone(). this may
208 * be overkill and a waste of time since the
209 * chances are that the process will still be
210 * there and PFIND is not free.
211 *
212 * (vp) is not locked on entry or exit.
213 */
214 procfs_inactive(vp, p)
215 struct vnode *vp;
216 struct proc *p;
217 {
218 struct pfsnode *pfs = VTOPFS(vp);
219
220 if (PFIND(pfs->pfs_pid) == 0)
221 vgone(vp);
222
223 return (0);
224 }
225
226 /*
227 * _reclaim is called when getnewvnode()
228 * wants to make use of an entry on the vnode
229 * free list. at this time the filesystem needs
230 * to free any private data and remove the node
231 * from any private lists.
232 */
233 procfs_reclaim(vp)
234 struct vnode *vp;
235 {
236 int error;
237
238 error = procfs_freevp(vp);
239 return (error);
240 }
241
242 /*
243 * _print is used for debugging.
244 * just print a readable description
245 * of (vp).
246 */
247 procfs_print(vp)
248 struct vnode *vp;
249 {
250 struct pfsnode *pfs = VTOPFS(vp);
251
252 printf("tag VT_PROCFS, pid %d, mode %x, flags %x\n",
253 pfs->pfs_pid,
254 pfs->pfs_mode, pfs->pfs_flags);
255 }
256
257 /*
258 * _abortop is called when operations such as
259 * rename and create fail. this entry is responsible
260 * for undoing any side-effects caused by the lookup.
261 * this will always include freeing the pathname buffer.
262 */
263 procfs_abortop(ndp)
264 struct nameidata *ndp;
265 {
266
267 if ((ndp->ni_nameiop & (HASBUF | SAVESTART)) == HASBUF)
268 FREE(ndp->ni_pnbuf, M_NAMEI);
269 return (0);
270 }
271
272 /*
273 * generic entry point for unsupported operations
274 */
275 procfs_badop()
276 {
277
278 return (EIO);
279 }
280
281 /*
282 * Invent attributes for pfsnode (vp) and store
283 * them in (vap).
284 * Directories lengths are returned as zero since
285 * any real length would require the genuine size
286 * to be computed, and nothing cares anyway.
287 *
288 * this is relatively minimal for procfs.
289 */
290 procfs_getattr(vp, vap, cred, p)
291 struct vnode *vp;
292 struct vattr *vap;
293 struct ucred *cred;
294 struct proc *p;
295 {
296 struct pfsnode *pfs = VTOPFS(vp);
297 struct proc *procp;
298 int error;
299
300 /* first check the process still exists */
301 procp = PFIND(pfs->pfs_pid);
302 if (procp == 0)
303 return (ENOENT);
304
305 error = 0;
306
307 /* start by zeroing out the attributes */
308 VATTR_NULL(vap);
309
310 /* next do all the common fields */
311 vap->va_type = vp->v_type;
312 vap->va_mode = pfs->pfs_mode;
313 vap->va_fileid = pfs->pfs_fileno;
314 vap->va_flags = 0;
315 vap->va_blocksize = page_size;
316 vap->va_bytes = vap->va_size = 0;
317
318 /*
319 * Make all times be current TOD.
320 * It would be possible to get the process start
321 * time from the p_stat structure, but there's
322 * no "file creation" time stamp anyway, and the
323 * p_stat structure is not addressible if u. gets
324 * swapped out for that process.
325 */
326 microtime(&vap->va_ctime);
327 vap->va_atime = vap->va_mtime = vap->va_ctime;
328
329 /*
330 * now do the object specific fields
331 *
332 * The size could be set from struct reg, but it's hardly
333 * worth the trouble, and it puts some (potentially) machine
334 * dependent data into this machine-independent code. If it
335 * becomes important then this function should break out into
336 * a per-file stat function in the corresponding .c file.
337 */
338
339 switch (pfs->pfs_type) {
340 case Proot:
341 vap->va_nlink = 2;
342 vap->va_uid = 0;
343 vap->va_gid = 0;
344 break;
345
346 case Pproc:
347 vap->va_nlink = 2;
348 vap->va_uid = procp->p_ucred->cr_uid;
349 vap->va_gid = procp->p_ucred->cr_gid;
350 break;
351
352 case Pfile:
353 error = EOPNOTSUPP;
354 break;
355
356 case Pmem:
357 vap->va_nlink = 1;
358 vap->va_bytes = vap->va_size =
359 ctob(procp->p_vmspace->vm_tsize +
360 procp->p_vmspace->vm_dsize +
361 procp->p_vmspace->vm_ssize);
362 vap->va_uid = procp->p_ucred->cr_uid;
363 vap->va_gid = procp->p_ucred->cr_gid;
364 break;
365
366 case Pregs:
367 vap->va_bytes = vap->va_size = sizeof(struct reg);
368 /* fall through */
369 case Pctl:
370 case Pstatus:
371 case Pnote:
372 case Pnotepg:
373 vap->va_nlink = 1;
374 vap->va_uid = procp->p_ucred->cr_uid;
375 vap->va_gid = procp->p_ucred->cr_gid;
376 break;
377
378 default:
379 panic("procfs_getattr");
380 }
381
382 return (error);
383 }
384
385 procfs_setattr(vp, vap, cred, p)
386 struct vnode *vp;
387 struct vattr *vap;
388 struct ucred *cred;
389 struct proc *p;
390 {
391 /*
392 * just fake out attribute setting
393 * it's not good to generate an error
394 * return, otherwise things like creat()
395 * will fail when they try to set the
396 * file length to 0. worse, this means
397 * that echo $note > /proc/$pid/note will fail.
398 */
399
400 return (0);
401 }
402
403 /*
404 * implement access checking.
405 *
406 * something very similar to this code is duplicated
407 * throughout the 4bsd kernel and should be moved
408 * into kern/vfs_subr.c sometime.
409 *
410 * actually, the check for super-user is slightly
411 * broken since it will allow read access to write-only
412 * objects. this doesn't cause any particular trouble
413 * but does mean that the i/o entry points need to check
414 * that the operation really does make sense.
415 */
416 procfs_access(vp, mode, cred, p)
417 struct vnode *vp;
418 int mode;
419 struct ucred *cred;
420 struct proc *p;
421 {
422 struct vattr *vap;
423 struct vattr vattr;
424 int error;
425
426 /*
427 * If you're the super-user,
428 * you always get access.
429 */
430 if (cred->cr_uid == (uid_t) 0)
431 return (0);
432 vap = &vattr;
433 if (error = VOP_GETATTR(vp, vap, cred, p))
434 return (error);
435
436 /*
437 * Access check is based on only one of owner, group, public.
438 * If not owner, then check group. If not a member of the
439 * group, then check public access.
440 */
441 if (cred->cr_uid != vap->va_uid) {
442 gid_t *gp;
443 int i;
444
445 mode >>= 3;
446 gp = cred->cr_groups;
447 for (i = 0; i < cred->cr_ngroups; i++, gp++)
448 if (vap->va_gid == *gp)
449 goto found;
450 mode >>= 3;
451 found:
452 ;
453 }
454
455 if ((vap->va_mode & mode) == mode)
456 return (0);
457
458 return (EACCES);
459 }
460
461 /*
462 * lookup. this is incredibly complicated in the
463 * general case, however for most pseudo-filesystems
464 * very little needs to be done.
465 *
466 * (dvp) is the directory in which the lookup takes place.
467 * (ndp) contains all the information about the type of
468 * lookup being done.
469 *
470 * (dvp) is locked on entry.
471 * the job of lookup is to set ndp->ni_dvp, and ndp->ni_vp.
472 * (this changes in 4.4 where all we want is the equivalent
473 * of ndp->ni_vp.)
474 *
475 * unless you want to get a migraine, just make sure your
476 * filesystem doesn't do any locking of its own. otherwise
477 * read and inwardly digest ufs_lookup().
478 */
479 procfs_lookup(dvp, ndp, p)
480 struct vnode *dvp;
481 struct nameidata *ndp;
482 struct proc *p;
483 {
484 char *pname = ndp->ni_ptr;
485 int error = 0;
486 int flag;
487 pid_t pid;
488 struct vnode *nvp;
489 struct pfsnode *pfs;
490 struct proc *procp;
491 int mode;
492 pfstype pfs_type;
493 int i;
494
495 if (ndp->ni_namelen == 1 && *pname == '.') {
496 ndp->ni_vp = dvp;
497 ndp->ni_dvp = dvp;
498 VREF(dvp);
499 return (0);
500 }
501
502 ndp->ni_dvp = dvp;
503 ndp->ni_vp = NULL;
504
505 pfs = VTOPFS(dvp);
506 switch (pfs->pfs_type) {
507 case Proot:
508 if (ndp->ni_isdotdot)
509 return (EIO);
510
511 if (NDEQ(ndp, "curproc", 7))
512 pid = p->p_pid;
513 else
514 pid = atopid(pname, ndp->ni_namelen);
515 if (pid == NO_PID)
516 return (ENOENT);
517
518 procp = PFIND(pid);
519 if (procp == 0)
520 return (ENOENT);
521
522 error = procfs_allocvp(dvp->v_mount, &nvp, pid, Pproc);
523 if (error)
524 return (error);
525
526 nvp->v_type = VDIR;
527 pfs = VTOPFS(nvp);
528
529 ndp->ni_vp = nvp;
530 return (0);
531
532 case Pproc:
533 if (ndp->ni_isdotdot) {
534 ndp->ni_dvp = dvp;
535 error = procfs_root(dvp->v_mount, &ndp->ni_vp);
536 return (error);
537 }
538
539 procp = PFIND(pfs->pfs_pid);
540 if (procp == 0)
541 return (ENOENT);
542
543 for (i = 0; i < Nprocent; i++) {
544 struct pfsnames *dp = &procent[i];
545
546 if (ndp->ni_namelen == dp->d_namlen &&
547 bcmp(pname, dp->d_name, dp->d_namlen) == 0) {
548 pfs_type = dp->d_pfstype;
549 goto found;
550 }
551 }
552 return (ENOENT);
553
554 found:
555 if (pfs_type == Pfile) {
556 nvp = procfs_findtextvp(procp);
557 if (nvp) {
558 VREF(nvp);
559 VOP_LOCK(nvp);
560 } else {
561 error = ENXIO;
562 }
563 } else {
564 error = procfs_allocvp(dvp->v_mount, &nvp,
565 pfs->pfs_pid, pfs_type);
566 if (error)
567 return (error);
568
569 nvp->v_type = VREG;
570 pfs = VTOPFS(nvp);
571 }
572 ndp->ni_vp = nvp;
573 return (error);
574
575 default:
576 return (ENOTDIR);
577 }
578 }
579
580 /*
581 * readdir returns directory entries from pfsnode (vp).
582 *
583 * the strategy here with procfs is to generate a single
584 * directory entry at a time (struct pfsdent) and then
585 * copy that out to userland using uiomove. a more efficent
586 * though more complex implementation, would try to minimize
587 * the number of calls to uiomove(). for procfs, this is
588 * hardly worth the added code complexity.
589 *
590 * this should just be done through read()
591 */
592 procfs_readdir(vp, uio, cred, eofflagp, cookies, ncookies)
593 struct vnode *vp;
594 struct uio *uio;
595 struct ucred *cred;
596 int *eofflagp;
597 u_int *cookies;
598 int ncookies;
599 {
600 struct pfsdent d;
601 struct pfsdent *dp = &d;
602 struct pfsnode *pfs;
603 int error;
604 int count;
605 int i;
606
607 pfs = VTOPFS(vp);
608
609 if (uio->uio_resid < UIO_MX)
610 return (EINVAL);
611 if (uio->uio_offset & (UIO_MX-1))
612 return (EINVAL);
613 if (uio->uio_offset < 0)
614 return (EINVAL);
615
616 error = 0;
617 count = 0;
618 i = uio->uio_offset / UIO_MX;
619
620 switch (pfs->pfs_type) {
621 /*
622 * this is for the process-specific sub-directories.
623 * all that is needed to is copy out all the entries
624 * from the procent[] table (top of this file).
625 */
626 case Pproc: {
627 while (uio->uio_resid >= UIO_MX) {
628 struct pfsnames *dt;
629
630 if (i >= Nprocent) {
631 *eofflagp = 1;
632 break;
633 }
634
635 dt = &procent[i];
636 dp->d_reclen = UIO_MX;
637 dp->d_fileno = PROCFS_FILENO(pfs->pfs_pid, dt->d_pfstype);
638 dp->d_namlen = dt->d_namlen;
639 bcopy(dt->d_name, dp->d_name, sizeof(dt->d_name)-1);
640 error = uiomove((caddr_t) dp, UIO_MX, uio);
641 if (error)
642 break;
643 count += UIO_MX;
644 i++;
645 }
646
647 break;
648
649 }
650
651 /*
652 * this is for the root of the procfs filesystem
653 * what is needed is a special entry for "curproc"
654 * followed by an entry for each process on allproc
655 #ifdef PROCFS_ZOMBIE
656 * and zombproc.
657 #endif
658 */
659
660 case Proot: {
661 int pcnt;
662 #ifdef PROCFS_ZOMBIE
663 int doingzomb = 0;
664 #endif
665 struct proc *p;
666
667 p = (struct proc *) allproc;
668
669 #define PROCFS_XFILES 1 /* number of other entries, like "curproc" */
670 pcnt = PROCFS_XFILES;
671
672 while (p && uio->uio_resid >= UIO_MX) {
673 bzero((char *) dp, UIO_MX);
674 dp->d_reclen = UIO_MX;
675
676 switch (i) {
677 case 0:
678 /* ship out entry for "curproc" */
679 dp->d_fileno = PROCFS_FILENO(PID_MAX+1, Pproc);
680 dp->d_namlen = 7;
681 bcopy("curproc", dp->d_name, dp->d_namlen+1);
682 break;
683
684 default:
685 if (pcnt >= i) {
686 dp->d_fileno = PROCFS_FILENO(p->p_pid, Pproc);
687 dp->d_namlen = sprintf(dp->d_name, "%ld", (long) p->p_pid);
688 }
689
690 p = p->p_nxt;
691
692 #ifdef PROCFS_ZOMBIE
693 if (p == 0 && doingzomb == 0) {
694 doingzomb = 1;
695 p = zombproc;
696 }
697 #endif
698
699 if (pcnt++ < i)
700 continue;
701
702 break;
703 }
704 error = uiomove((caddr_t) dp, UIO_MX, uio);
705 if (error)
706 break;
707 count += UIO_MX;
708 i++;
709 }
710
711 break;
712
713 }
714
715 default:
716 error = ENOTDIR;
717 break;
718 }
719
720 uio->uio_offset = i * UIO_MX;
721 if (count == 0)
722 *eofflagp = 1;
723
724 return (error);
725 }
726
727 /*
728 * convert decimal ascii to pid_t
729 */
730 static pid_t
731 atopid(b, len)
732 const char *b;
733 u_int len;
734 {
735 pid_t p = 0;
736
737 while (len--) {
738 char c = *b++;
739 if (c < '0' || c > '9')
740 return (NO_PID);
741 p = 10 * p + (c - '0');
742 if (p > PID_MAX)
743 return (NO_PID);
744 }
745
746 return (p);
747 }
748
749 /*
750 * procfs vnode operations.
751 */
752 struct vnodeops procfs_vnodeops = {
753 procfs_lookup, /* lookup */
754 procfs_create, /* create */
755 procfs_mknod, /* mknod */
756 procfs_open, /* open */
757 procfs_close, /* close */
758 procfs_access, /* access */
759 procfs_getattr, /* getattr */
760 procfs_setattr, /* setattr */
761 procfs_read, /* read */
762 procfs_write, /* write */
763 procfs_ioctl, /* ioctl */
764 procfs_select, /* select */
765 procfs_mmap, /* mmap */
766 procfs_fsync, /* fsync */
767 procfs_seek, /* seek */
768 procfs_remove, /* remove */
769 procfs_link, /* link */
770 procfs_rename, /* rename */
771 procfs_mkdir, /* mkdir */
772 procfs_rmdir, /* rmdir */
773 procfs_symlink, /* symlink */
774 procfs_readdir, /* readdir */
775 procfs_readlink, /* readlink */
776 procfs_abortop, /* abortop */
777 procfs_inactive, /* inactive */
778 procfs_reclaim, /* reclaim */
779 procfs_lock, /* lock */
780 procfs_unlock, /* unlock */
781 procfs_bmap, /* bmap */
782 procfs_strategy, /* strategy */
783 procfs_print, /* print */
784 procfs_islocked, /* islocked */
785 procfs_advlock, /* advlock */
786 };
787