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