1 /*-
2 * Copyright (c) 1995 Scott Bartram
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "opt_spx_hack.h"
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/fcntl.h>
36 #include <sys/file.h>
37 #include <sys/filedesc.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/syscallsubr.h>
41 #include <sys/sysproto.h>
42 #include <sys/ttycom.h>
43
44 #include <i386/ibcs2/ibcs2_fcntl.h>
45 #include <i386/ibcs2/ibcs2_signal.h>
46 #include <i386/ibcs2/ibcs2_proto.h>
47 #include <i386/ibcs2/ibcs2_util.h>
48
49 static void cvt_iflock2flock(struct ibcs2_flock *, struct flock *);
50 static void cvt_flock2iflock(struct flock *, struct ibcs2_flock *);
51 static int cvt_o_flags(int);
52 static int oflags2ioflags(int);
53 static int ioflags2oflags(int);
54
55 static int
56 cvt_o_flags(flags)
57 int flags;
58 {
59 int r = 0;
60
61 /* convert mode into NetBSD mode */
62 if (flags & IBCS2_O_WRONLY) r |= O_WRONLY;
63 if (flags & IBCS2_O_RDWR) r |= O_RDWR;
64 if (flags & (IBCS2_O_NDELAY | IBCS2_O_NONBLOCK)) r |= O_NONBLOCK;
65 if (flags & IBCS2_O_APPEND) r |= O_APPEND;
66 if (flags & IBCS2_O_SYNC) r |= O_FSYNC;
67 if (flags & IBCS2_O_CREAT) r |= O_CREAT;
68 if (flags & IBCS2_O_TRUNC) r |= O_TRUNC /* | O_CREAT ??? */;
69 if (flags & IBCS2_O_EXCL) r |= O_EXCL;
70 if (flags & IBCS2_O_RDONLY) r |= O_RDONLY;
71 if (flags & IBCS2_O_PRIV) r |= O_EXLOCK;
72 if (flags & IBCS2_O_NOCTTY) r |= O_NOCTTY;
73 return r;
74 }
75
76 static void
77 cvt_flock2iflock(flp, iflp)
78 struct flock *flp;
79 struct ibcs2_flock *iflp;
80 {
81 switch (flp->l_type) {
82 case F_RDLCK:
83 iflp->l_type = IBCS2_F_RDLCK;
84 break;
85 case F_WRLCK:
86 iflp->l_type = IBCS2_F_WRLCK;
87 break;
88 case F_UNLCK:
89 iflp->l_type = IBCS2_F_UNLCK;
90 break;
91 }
92 iflp->l_whence = (short)flp->l_whence;
93 iflp->l_start = (ibcs2_off_t)flp->l_start;
94 iflp->l_len = (ibcs2_off_t)flp->l_len;
95 iflp->l_sysid = 0;
96 iflp->l_pid = (ibcs2_pid_t)flp->l_pid;
97 }
98
99 #ifdef DEBUG_IBCS2
100 static void
101 print_flock(struct flock *flp)
102 {
103 printf("flock: start=%x len=%x pid=%d type=%d whence=%d\n",
104 (int)flp->l_start, (int)flp->l_len, (int)flp->l_pid,
105 flp->l_type, flp->l_whence);
106 }
107 #endif
108
109 static void
110 cvt_iflock2flock(iflp, flp)
111 struct ibcs2_flock *iflp;
112 struct flock *flp;
113 {
114 flp->l_start = (off_t)iflp->l_start;
115 flp->l_len = (off_t)iflp->l_len;
116 flp->l_pid = (pid_t)iflp->l_pid;
117 switch (iflp->l_type) {
118 case IBCS2_F_RDLCK:
119 flp->l_type = F_RDLCK;
120 break;
121 case IBCS2_F_WRLCK:
122 flp->l_type = F_WRLCK;
123 break;
124 case IBCS2_F_UNLCK:
125 flp->l_type = F_UNLCK;
126 break;
127 }
128 flp->l_whence = iflp->l_whence;
129 }
130
131 /* convert iBCS2 mode into NetBSD mode */
132 static int
133 ioflags2oflags(flags)
134 int flags;
135 {
136 int r = 0;
137
138 if (flags & IBCS2_O_RDONLY) r |= O_RDONLY;
139 if (flags & IBCS2_O_WRONLY) r |= O_WRONLY;
140 if (flags & IBCS2_O_RDWR) r |= O_RDWR;
141 if (flags & IBCS2_O_NDELAY) r |= O_NONBLOCK;
142 if (flags & IBCS2_O_APPEND) r |= O_APPEND;
143 if (flags & IBCS2_O_SYNC) r |= O_FSYNC;
144 if (flags & IBCS2_O_NONBLOCK) r |= O_NONBLOCK;
145 if (flags & IBCS2_O_CREAT) r |= O_CREAT;
146 if (flags & IBCS2_O_TRUNC) r |= O_TRUNC;
147 if (flags & IBCS2_O_EXCL) r |= O_EXCL;
148 if (flags & IBCS2_O_NOCTTY) r |= O_NOCTTY;
149 return r;
150 }
151
152 /* convert NetBSD mode into iBCS2 mode */
153 static int
154 oflags2ioflags(flags)
155 int flags;
156 {
157 int r = 0;
158
159 if (flags & O_RDONLY) r |= IBCS2_O_RDONLY;
160 if (flags & O_WRONLY) r |= IBCS2_O_WRONLY;
161 if (flags & O_RDWR) r |= IBCS2_O_RDWR;
162 if (flags & O_NDELAY) r |= IBCS2_O_NONBLOCK;
163 if (flags & O_APPEND) r |= IBCS2_O_APPEND;
164 if (flags & O_FSYNC) r |= IBCS2_O_SYNC;
165 if (flags & O_NONBLOCK) r |= IBCS2_O_NONBLOCK;
166 if (flags & O_CREAT) r |= IBCS2_O_CREAT;
167 if (flags & O_TRUNC) r |= IBCS2_O_TRUNC;
168 if (flags & O_EXCL) r |= IBCS2_O_EXCL;
169 if (flags & O_NOCTTY) r |= IBCS2_O_NOCTTY;
170 return r;
171 }
172
173 int
174 ibcs2_open(td, uap)
175 struct thread *td;
176 struct ibcs2_open_args *uap;
177 {
178 struct proc *p = td->td_proc;
179 int noctty = uap->flags & IBCS2_O_NOCTTY;
180 int ret;
181 caddr_t sg = stackgap_init();
182
183 uap->flags = cvt_o_flags(uap->flags);
184 if (uap->flags & O_CREAT)
185 CHECKALTCREAT(td, &sg, uap->path);
186 else
187 CHECKALTEXIST(td, &sg, uap->path);
188 ret = open(td, (struct open_args *)uap);
189
190 #ifdef SPX_HACK
191 if (ret == ENXIO) {
192 if (!strcmp(uap->path, "/compat/ibcs2/dev/spx"))
193 ret = spx_open(td, uap);
194 } else
195 #endif /* SPX_HACK */
196 PROC_LOCK(p);
197 if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
198 struct file *fp;
199 int error;
200
201 error = fget(td, td->td_retval[0], &fp);
202 PROC_UNLOCK(p);
203 if (error)
204 return (EBADF);
205
206 /* ignore any error, just give it a try */
207 if (fp->f_type == DTYPE_VNODE)
208 fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, td->td_ucred,
209 td);
210 fdrop(fp, td);
211 } else
212 PROC_UNLOCK(p);
213 return ret;
214 }
215
216 int
217 ibcs2_creat(td, uap)
218 struct thread *td;
219 struct ibcs2_creat_args *uap;
220 {
221 struct open_args cup;
222 caddr_t sg = stackgap_init();
223
224 CHECKALTCREAT(td, &sg, uap->path);
225 cup.path = uap->path;
226 cup.mode = uap->mode;
227 cup.flags = O_WRONLY | O_CREAT | O_TRUNC;
228 return open(td, &cup);
229 }
230
231 int
232 ibcs2_access(td, uap)
233 struct thread *td;
234 struct ibcs2_access_args *uap;
235 {
236 struct access_args cup;
237 caddr_t sg = stackgap_init();
238
239 CHECKALTEXIST(td, &sg, uap->path);
240 cup.path = uap->path;
241 cup.flags = uap->flags;
242 return access(td, &cup);
243 }
244
245 int
246 ibcs2_fcntl(td, uap)
247 struct thread *td;
248 struct ibcs2_fcntl_args *uap;
249 {
250 intptr_t arg;
251 int error;
252 struct flock fl;
253 struct ibcs2_flock ifl;
254
255 arg = (intptr_t)uap->arg;
256 switch(uap->cmd) {
257 case IBCS2_F_DUPFD:
258 return (kern_fcntl(td, uap->fd, F_DUPFD, arg));
259 case IBCS2_F_GETFD:
260 return (kern_fcntl(td, uap->fd, F_GETFD, arg));
261 case IBCS2_F_SETFD:
262 return (kern_fcntl(td, uap->fd, F_SETFD, arg));
263 case IBCS2_F_GETFL:
264 error = kern_fcntl(td, uap->fd, F_GETFL, arg);
265 if (error)
266 return error;
267 td->td_retval[0] = oflags2ioflags(td->td_retval[0]);
268 return error;
269 case IBCS2_F_SETFL:
270 return (kern_fcntl(td, uap->fd, F_SETFL,
271 ioflags2oflags(arg)));
272
273 case IBCS2_F_GETLK:
274 {
275 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
276 ibcs2_flock_len);
277 if (error)
278 return error;
279 cvt_iflock2flock(&ifl, &fl);
280 error = kern_fcntl(td, uap->fd, F_GETLK, (intptr_t)&fl);
281 if (error)
282 return error;
283 cvt_flock2iflock(&fl, &ifl);
284 return copyout((caddr_t)&ifl, (caddr_t)uap->arg,
285 ibcs2_flock_len);
286 }
287
288 case IBCS2_F_SETLK:
289 {
290 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
291 ibcs2_flock_len);
292 if (error)
293 return error;
294 cvt_iflock2flock(&ifl, &fl);
295 return (kern_fcntl(td, uap->fd, F_SETLK, (intptr_t)&fl));
296 }
297
298 case IBCS2_F_SETLKW:
299 {
300 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
301 ibcs2_flock_len);
302 if (error)
303 return error;
304 cvt_iflock2flock(&ifl, &fl);
305 return (kern_fcntl(td, uap->fd, F_SETLKW, (intptr_t)&fl));
306 }
307 }
308 return ENOSYS;
309 }
Cache object: 3369c608f0d9e374e7e1281d8e26a175
|