xref: /csrg-svn/sys/kern/vfs_vnops.c (revision 7654)
1 /*	vfs_vnops.c	4.26	82/08/03	*/
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/dir.h"
6 #include "../h/user.h"
7 #include "../h/fs.h"
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"
13 #include "../h/mount.h"
14 #include "../h/socket.h"
15 #include "../h/socketvar.h"
16 #include "../h/proc.h"
17 
18 /*
19  * Openi called to allow handler
20  * of special files to initialize and
21  * validate before actual IO.
22  */
23 openi(ip, mode)
24 	register struct inode *ip;
25 {
26 	dev_t dev;
27 	register unsigned int maj;
28 
29 	dev = (dev_t)ip->i_rdev;
30 	maj = major(dev);
31 	switch (ip->i_mode&IFMT) {
32 
33 	case IFCHR:
34 		if (maj >= nchrdev)
35 			goto bad;
36 		(*cdevsw[maj].d_open)(dev, mode);
37 		break;
38 
39 	case IFBLK:
40 		if (maj >= nblkdev)
41 			goto bad;
42 		(*bdevsw[maj].d_open)(dev, mode);
43 	}
44 	return;
45 
46 bad:
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  */
63 access(ip, mode)
64 	register struct inode *ip;
65 	int mode;
66 {
67 	register m;
68 
69 	m = mode;
70 	if (m == IWRITE) {
71 		if (ip->i_fs->fs_ronly != 0) {
72 			u.u_error = EROFS;
73 			return (1);
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 		if (ip->i_gid >= NGRPS ||
87 		    (u.u_grps[ip->i_gid/(sizeof(int)*8)] &
88 		     (1 << ip->i_gid%(sizeof(int)*8)) == 0))
89 			m >>= 3;
90 	}
91 	if ((ip->i_mode&m) != 0)
92 		return (0);
93 	u.u_error = EACCES;
94 	return (1);
95 }
96 
97 /*
98  * Look up a pathname and test if
99  * the resultant inode is owned by the
100  * current user.
101  * If not, try for super-user.
102  * If permission is granted,
103  * return inode pointer.
104  */
105 struct inode *
106 owner(follow)
107 	int follow;
108 {
109 	register struct inode *ip;
110 
111 	ip = namei(uchar, 0, follow);
112 	if (ip == NULL)
113 		return (NULL);
114 	if (u.u_uid == ip->i_uid)
115 		return (ip);
116 	if (suser())
117 		return (ip);
118 	iput(ip);
119 	return (NULL);
120 }
121 
122 /*
123  * Test if the current user is the
124  * super user.
125  */
126 suser()
127 {
128 
129 	if (u.u_uid == 0) {
130 		u.u_acflag |= ASU;
131 		return (1);
132 	}
133 	u.u_error = EPERM;
134 	return (0);
135 }
136