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 * $FreeBSD: releng/5.1/sys/i386/ibcs2/ibcs2_fcntl.c 107849 2002-12-14 01:56:26Z alfred $
28 */
29
30 #include "opt_spx_hack.h"
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/fcntl.h>
35 #include <sys/file.h>
36 #include <sys/filedesc.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/sysproto.h>
40 #include <sys/ttycom.h>
41
42 #include <i386/ibcs2/ibcs2_fcntl.h>
43 #include <i386/ibcs2/ibcs2_signal.h>
44 #include <i386/ibcs2/ibcs2_proto.h>
45 #include <i386/ibcs2/ibcs2_util.h>
46
47 static void cvt_iflock2flock(struct ibcs2_flock *, struct flock *);
48 static void cvt_flock2iflock(struct flock *, struct ibcs2_flock *);
49 static int cvt_o_flags(int);
50 static int oflags2ioflags(int);
51 static int ioflags2oflags(int);
52
53 static int
54 cvt_o_flags(flags)
55 int flags;
56 {
57 int r = 0;
58
59 /* convert mode into NetBSD mode */
60 if (flags & IBCS2_O_WRONLY) r |= O_WRONLY;
61 if (flags & IBCS2_O_RDWR) r |= O_RDWR;
62 if (flags & (IBCS2_O_NDELAY | IBCS2_O_NONBLOCK)) r |= O_NONBLOCK;
63 if (flags & IBCS2_O_APPEND) r |= O_APPEND;
64 if (flags & IBCS2_O_SYNC) r |= O_FSYNC;
65 if (flags & IBCS2_O_CREAT) r |= O_CREAT;
66 if (flags & IBCS2_O_TRUNC) r |= O_TRUNC /* | O_CREAT ??? */;
67 if (flags & IBCS2_O_EXCL) r |= O_EXCL;
68 if (flags & IBCS2_O_RDONLY) r |= O_RDONLY;
69 if (flags & IBCS2_O_PRIV) r |= O_EXLOCK;
70 if (flags & IBCS2_O_NOCTTY) r |= O_NOCTTY;
71 return r;
72 }
73
74 static void
75 cvt_flock2iflock(flp, iflp)
76 struct flock *flp;
77 struct ibcs2_flock *iflp;
78 {
79 switch (flp->l_type) {
80 case F_RDLCK:
81 iflp->l_type = IBCS2_F_RDLCK;
82 break;
83 case F_WRLCK:
84 iflp->l_type = IBCS2_F_WRLCK;
85 break;
86 case F_UNLCK:
87 iflp->l_type = IBCS2_F_UNLCK;
88 break;
89 }
90 iflp->l_whence = (short)flp->l_whence;
91 iflp->l_start = (ibcs2_off_t)flp->l_start;
92 iflp->l_len = (ibcs2_off_t)flp->l_len;
93 iflp->l_sysid = 0;
94 iflp->l_pid = (ibcs2_pid_t)flp->l_pid;
95 }
96
97 #ifdef DEBUG_IBCS2
98 static void
99 print_flock(struct flock *flp)
100 {
101 printf("flock: start=%x len=%x pid=%d type=%d whence=%d\n",
102 (int)flp->l_start, (int)flp->l_len, (int)flp->l_pid,
103 flp->l_type, flp->l_whence);
104 }
105 #endif
106
107 static void
108 cvt_iflock2flock(iflp, flp)
109 struct ibcs2_flock *iflp;
110 struct flock *flp;
111 {
112 flp->l_start = (off_t)iflp->l_start;
113 flp->l_len = (off_t)iflp->l_len;
114 flp->l_pid = (pid_t)iflp->l_pid;
115 switch (iflp->l_type) {
116 case IBCS2_F_RDLCK:
117 flp->l_type = F_RDLCK;
118 break;
119 case IBCS2_F_WRLCK:
120 flp->l_type = F_WRLCK;
121 break;
122 case IBCS2_F_UNLCK:
123 flp->l_type = F_UNLCK;
124 break;
125 }
126 flp->l_whence = iflp->l_whence;
127 }
128
129 /* convert iBCS2 mode into NetBSD mode */
130 static int
131 ioflags2oflags(flags)
132 int flags;
133 {
134 int r = 0;
135
136 if (flags & IBCS2_O_RDONLY) r |= O_RDONLY;
137 if (flags & IBCS2_O_WRONLY) r |= O_WRONLY;
138 if (flags & IBCS2_O_RDWR) r |= O_RDWR;
139 if (flags & IBCS2_O_NDELAY) r |= O_NONBLOCK;
140 if (flags & IBCS2_O_APPEND) r |= O_APPEND;
141 if (flags & IBCS2_O_SYNC) r |= O_FSYNC;
142 if (flags & IBCS2_O_NONBLOCK) r |= O_NONBLOCK;
143 if (flags & IBCS2_O_CREAT) r |= O_CREAT;
144 if (flags & IBCS2_O_TRUNC) r |= O_TRUNC;
145 if (flags & IBCS2_O_EXCL) r |= O_EXCL;
146 if (flags & IBCS2_O_NOCTTY) r |= O_NOCTTY;
147 return r;
148 }
149
150 /* convert NetBSD mode into iBCS2 mode */
151 static int
152 oflags2ioflags(flags)
153 int flags;
154 {
155 int r = 0;
156
157 if (flags & O_RDONLY) r |= IBCS2_O_RDONLY;
158 if (flags & O_WRONLY) r |= IBCS2_O_WRONLY;
159 if (flags & O_RDWR) r |= IBCS2_O_RDWR;
160 if (flags & O_NDELAY) r |= IBCS2_O_NONBLOCK;
161 if (flags & O_APPEND) r |= IBCS2_O_APPEND;
162 if (flags & O_FSYNC) r |= IBCS2_O_SYNC;
163 if (flags & O_NONBLOCK) r |= IBCS2_O_NONBLOCK;
164 if (flags & O_CREAT) r |= IBCS2_O_CREAT;
165 if (flags & O_TRUNC) r |= IBCS2_O_TRUNC;
166 if (flags & O_EXCL) r |= IBCS2_O_EXCL;
167 if (flags & O_NOCTTY) r |= IBCS2_O_NOCTTY;
168 return r;
169 }
170
171 int
172 ibcs2_open(td, uap)
173 struct thread *td;
174 struct ibcs2_open_args *uap;
175 {
176 struct proc *p = td->td_proc;
177 int noctty = uap->flags & IBCS2_O_NOCTTY;
178 int ret;
179 caddr_t sg = stackgap_init();
180
181 uap->flags = cvt_o_flags(uap->flags);
182 if (uap->flags & O_CREAT)
183 CHECKALTCREAT(td, &sg, uap->path);
184 else
185 CHECKALTEXIST(td, &sg, uap->path);
186 ret = open(td, (struct open_args *)uap);
187
188 #ifdef SPX_HACK
189 if (ret == ENXIO) {
190 if (!strcmp(uap->path, "/compat/ibcs2/dev/spx"))
191 ret = spx_open(td, uap);
192 } else
193 #endif /* SPX_HACK */
194 PROC_LOCK(p);
195 if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
196 struct file *fp;
197 int error;
198
199 error = fget(td, td->td_retval[0], &fp);
200 PROC_UNLOCK(p);
201 if (error)
202 return (EBADF);
203
204 /* ignore any error, just give it a try */
205 if (fp->f_type == DTYPE_VNODE)
206 fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, td->td_ucred,
207 td);
208 fdrop(fp, td);
209 } else
210 PROC_UNLOCK(p);
211 return ret;
212 }
213
214 int
215 ibcs2_creat(td, uap)
216 struct thread *td;
217 struct ibcs2_creat_args *uap;
218 {
219 struct open_args cup;
220 caddr_t sg = stackgap_init();
221
222 CHECKALTCREAT(td, &sg, uap->path);
223 cup.path = uap->path;
224 cup.mode = uap->mode;
225 cup.flags = O_WRONLY | O_CREAT | O_TRUNC;
226 return open(td, &cup);
227 }
228
229 int
230 ibcs2_access(td, uap)
231 struct thread *td;
232 struct ibcs2_access_args *uap;
233 {
234 struct access_args cup;
235 caddr_t sg = stackgap_init();
236
237 CHECKALTEXIST(td, &sg, uap->path);
238 cup.path = uap->path;
239 cup.flags = uap->flags;
240 return access(td, &cup);
241 }
242
243 int
244 ibcs2_fcntl(td, uap)
245 struct thread *td;
246 struct ibcs2_fcntl_args *uap;
247 {
248 int error;
249 struct fcntl_args fa;
250 struct flock *flp;
251 struct ibcs2_flock ifl;
252
253 switch(uap->cmd) {
254 case IBCS2_F_DUPFD:
255 fa.fd = uap->fd;
256 fa.cmd = F_DUPFD;
257 fa.arg = (/* XXX */ int)uap->arg;
258 return fcntl(td, &fa);
259 case IBCS2_F_GETFD:
260 fa.fd = uap->fd;
261 fa.cmd = F_GETFD;
262 fa.arg = (/* XXX */ int)uap->arg;
263 return fcntl(td, &fa);
264 case IBCS2_F_SETFD:
265 fa.fd = uap->fd;
266 fa.cmd = F_SETFD;
267 fa.arg = (/* XXX */ int)uap->arg;
268 return fcntl(td, &fa);
269 case IBCS2_F_GETFL:
270 fa.fd = uap->fd;
271 fa.cmd = F_GETFL;
272 fa.arg = (/* XXX */ int)uap->arg;
273 error = fcntl(td, &fa);
274 if (error)
275 return error;
276 td->td_retval[0] = oflags2ioflags(td->td_retval[0]);
277 return error;
278 case IBCS2_F_SETFL:
279 fa.fd = uap->fd;
280 fa.cmd = F_SETFL;
281 fa.arg = (/* XXX */ int)
282 ioflags2oflags((int)uap->arg);
283 return fcntl(td, &fa);
284
285 case IBCS2_F_GETLK:
286 {
287 caddr_t sg = stackgap_init();
288 flp = stackgap_alloc(&sg, sizeof(*flp));
289 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
290 ibcs2_flock_len);
291 if (error)
292 return error;
293 cvt_iflock2flock(&ifl, flp);
294 fa.fd = uap->fd;
295 fa.cmd = F_GETLK;
296 fa.arg = (/* XXX */ int)flp;
297 error = fcntl(td, &fa);
298 if (error)
299 return error;
300 cvt_flock2iflock(flp, &ifl);
301 return copyout((caddr_t)&ifl, (caddr_t)uap->arg,
302 ibcs2_flock_len);
303 }
304
305 case IBCS2_F_SETLK:
306 {
307 caddr_t sg = stackgap_init();
308 flp = stackgap_alloc(&sg, sizeof(*flp));
309 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
310 ibcs2_flock_len);
311 if (error)
312 return error;
313 cvt_iflock2flock(&ifl, flp);
314 fa.fd = uap->fd;
315 fa.cmd = F_SETLK;
316 fa.arg = (/* XXX */ int)flp;
317
318 return fcntl(td, &fa);
319 }
320
321 case IBCS2_F_SETLKW:
322 {
323 caddr_t sg = stackgap_init();
324 flp = stackgap_alloc(&sg, sizeof(*flp));
325 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
326 ibcs2_flock_len);
327 if (error)
328 return error;
329 cvt_iflock2flock(&ifl, flp);
330 fa.fd = uap->fd;
331 fa.cmd = F_SETLKW;
332 fa.arg = (/* XXX */ int)flp;
333 return fcntl(td, &fa);
334 }
335 }
336 return ENOSYS;
337 }
Cache object: 6098506681cc182fbf554d0e9016b4a1
|