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