more timer stuff
[unix-history] / usr / src / sys / kern / vfs_vnops.c
CommitLineData
197da11b 1/* vfs_vnops.c 4.27 82/08/24 */
fc96c615
BJ
2
3#include "../h/param.h"
4#include "../h/systm.h"
5#include "../h/dir.h"
6#include "../h/user.h"
6459ebe0 7#include "../h/fs.h"
fc96c615
BJ
8#include "../h/file.h"
9#include "../h/conf.h"
10#include "../h/inode.h"
11#include "../h/reg.h"
12#include "../h/acct.h"
f5b8937b 13#include "../h/mount.h"
ae921915
BJ
14#include "../h/socket.h"
15#include "../h/socketvar.h"
f3156a73 16#include "../h/proc.h"
fc96c615 17
fc96c615 18/*
fdd90ee3 19 * Openi called to allow handler
fc96c615
BJ
20 * of special files to initialize and
21 * validate before actual IO.
22 */
6b4c7376 23openi(ip, mode)
fdd90ee3 24 register struct inode *ip;
fc96c615
BJ
25{
26 dev_t dev;
27 register unsigned int maj;
28
6459ebe0 29 dev = (dev_t)ip->i_rdev;
fc96c615 30 maj = major(dev);
fdd90ee3 31 switch (ip->i_mode&IFMT) {
fc96c615
BJ
32
33 case IFCHR:
fdd90ee3 34 if (maj >= nchrdev)
fc96c615 35 goto bad;
6b4c7376 36 (*cdevsw[maj].d_open)(dev, mode);
fc96c615
BJ
37 break;
38
39 case IFBLK:
fdd90ee3 40 if (maj >= nblkdev)
fc96c615 41 goto bad;
6b4c7376 42 (*bdevsw[maj].d_open)(dev, mode);
fc96c615
BJ
43 }
44 return;
45
46bad:
47 u.u_error = ENXIO;
48}
49
50/*
51 * Check mode permission on inode pointer.
52 * Mode is READ, WRITE or EXEC.
53 * In the case of WRITE, the
54 * read-only status of the file
55 * system is checked.
56 * Also in WRITE, prototype text
57 * segments cannot be written.
58 * The mode is shifted to select
59 * the owner/group/other fields.
60 * The super user is granted all
61 * permissions.
62 */
63access(ip, mode)
fdd90ee3
BJ
64 register struct inode *ip;
65 int mode;
fc96c615
BJ
66{
67 register m;
197da11b 68 register int *gp;
fc96c615
BJ
69
70 m = mode;
fdd90ee3 71 if (m == IWRITE) {
6459ebe0 72 if (ip->i_fs->fs_ronly != 0) {
fc96c615 73 u.u_error = EROFS;
fdd90ee3 74 return (1);
fc96c615
BJ
75 }
76 if (ip->i_flag&ITEXT) /* try to free text */
77 xrele(ip);
fdd90ee3 78 if (ip->i_flag & ITEXT) {
fc96c615 79 u.u_error = ETXTBSY;
fdd90ee3 80 return (1);
fc96c615
BJ
81 }
82 }
fdd90ee3
BJ
83 if (u.u_uid == 0)
84 return (0);
85 if (u.u_uid != ip->i_uid) {
fc96c615 86 m >>= 3;
197da11b
BJ
87 for (gp = u.u_groups; gp < &u.u_groups[NGROUPS]; gp++)
88 if (ip->i_gid != *gp)
89 goto found;
90 m >>= 3;
91found:
92 ;
fc96c615 93 }
fdd90ee3
BJ
94 if ((ip->i_mode&m) != 0)
95 return (0);
fc96c615 96 u.u_error = EACCES;
fdd90ee3 97 return (1);
fc96c615
BJ
98}
99
100/*
101 * Look up a pathname and test if
102 * the resultant inode is owned by the
103 * current user.
104 * If not, try for super-user.
105 * If permission is granted,
106 * return inode pointer.
107 */
108struct inode *
5485e062
BJ
109owner(follow)
110 int follow;
fc96c615
BJ
111{
112 register struct inode *ip;
113
5485e062 114 ip = namei(uchar, 0, follow);
fdd90ee3
BJ
115 if (ip == NULL)
116 return (NULL);
117 if (u.u_uid == ip->i_uid)
118 return (ip);
119 if (suser())
120 return (ip);
fc96c615 121 iput(ip);
fdd90ee3 122 return (NULL);
fc96c615
BJ
123}
124
125/*
126 * Test if the current user is the
127 * super user.
128 */
129suser()
130{
131
fdd90ee3 132 if (u.u_uid == 0) {
fc96c615 133 u.u_acflag |= ASU;
fdd90ee3 134 return (1);
fc96c615
BJ
135 }
136 u.u_error = EPERM;
fdd90ee3 137 return (0);
fc96c615 138}