2 * Copyright (c) 1995 Scott Bartram
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
27 * $FreeBSD: src/sys/i386/ibcs2/ibcs2_fcntl.c,v 1.14 1999/09/19 17:00:14 green Exp $
28 * $DragonFly: src/sys/emulation/ibcs2/i386/Attic/ibcs2_fcntl.c,v 1.11 2005/12/10 16:06:20 swildner Exp $
31 #include "opt_spx_hack.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/fcntl.h>
37 #include <sys/filedesc.h>
38 #include <sys/ttycom.h>
39 #include <sys/sysproto.h>
41 #include <sys/file2.h>
43 #include "ibcs2_fcntl.h"
44 #include "ibcs2_signal.h"
45 #include "ibcs2_proto.h"
46 #include "ibcs2_util.h"
48 static void cvt_iflock2flock (struct ibcs2_flock *, struct flock *);
49 static void cvt_flock2iflock (struct flock *, struct ibcs2_flock *);
50 static int cvt_o_flags (int);
51 static int oflags2ioflags (int);
52 static int ioflags2oflags (int);
55 cvt_o_flags(int flags)
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;
75 cvt_flock2iflock(struct flock *flp, struct ibcs2_flock *iflp)
77 switch (flp->l_type) {
79 iflp->l_type = IBCS2_F_RDLCK;
82 iflp->l_type = IBCS2_F_WRLCK;
85 iflp->l_type = IBCS2_F_UNLCK;
88 iflp->l_whence = (short)flp->l_whence;
89 iflp->l_start = (ibcs2_off_t)flp->l_start;
90 iflp->l_len = (ibcs2_off_t)flp->l_len;
92 iflp->l_pid = (ibcs2_pid_t)flp->l_pid;
97 print_flock(struct flock *flp)
99 printf("flock: start=%x len=%x pid=%d type=%d whence=%d\n",
100 (int)flp->l_start, (int)flp->l_len, (int)flp->l_pid,
101 flp->l_type, flp->l_whence);
106 cvt_iflock2flock(struct ibcs2_flock *iflp, struct flock *flp)
108 flp->l_start = (off_t)iflp->l_start;
109 flp->l_len = (off_t)iflp->l_len;
110 flp->l_pid = (pid_t)iflp->l_pid;
111 switch (iflp->l_type) {
113 flp->l_type = F_RDLCK;
116 flp->l_type = F_WRLCK;
119 flp->l_type = F_UNLCK;
122 flp->l_whence = iflp->l_whence;
125 /* convert iBCS2 mode into NetBSD mode */
127 ioflags2oflags(int flags)
131 if (flags & IBCS2_O_RDONLY) r |= O_RDONLY;
132 if (flags & IBCS2_O_WRONLY) r |= O_WRONLY;
133 if (flags & IBCS2_O_RDWR) r |= O_RDWR;
134 if (flags & IBCS2_O_NDELAY) r |= O_NONBLOCK;
135 if (flags & IBCS2_O_APPEND) r |= O_APPEND;
136 if (flags & IBCS2_O_SYNC) r |= O_FSYNC;
137 if (flags & IBCS2_O_NONBLOCK) r |= O_NONBLOCK;
138 if (flags & IBCS2_O_CREAT) r |= O_CREAT;
139 if (flags & IBCS2_O_TRUNC) r |= O_TRUNC;
140 if (flags & IBCS2_O_EXCL) r |= O_EXCL;
141 if (flags & IBCS2_O_NOCTTY) r |= O_NOCTTY;
145 /* convert NetBSD mode into iBCS2 mode */
147 oflags2ioflags(int flags)
151 if (flags & O_RDONLY) r |= IBCS2_O_RDONLY;
152 if (flags & O_WRONLY) r |= IBCS2_O_WRONLY;
153 if (flags & O_RDWR) r |= IBCS2_O_RDWR;
154 if (flags & O_NDELAY) r |= IBCS2_O_NONBLOCK;
155 if (flags & O_APPEND) r |= IBCS2_O_APPEND;
156 if (flags & O_FSYNC) r |= IBCS2_O_SYNC;
157 if (flags & O_NONBLOCK) r |= IBCS2_O_NONBLOCK;
158 if (flags & O_CREAT) r |= IBCS2_O_CREAT;
159 if (flags & O_TRUNC) r |= IBCS2_O_TRUNC;
160 if (flags & O_EXCL) r |= IBCS2_O_EXCL;
161 if (flags & O_NOCTTY) r |= IBCS2_O_NOCTTY;
166 ibcs2_open(struct ibcs2_open_args *uap)
168 struct thread *td = curthread; /* XXX */
170 int noctty = SCARG(uap, flags) & IBCS2_O_NOCTTY;
172 caddr_t sg = stackgap_init();
174 SCARG(uap, flags) = cvt_o_flags(SCARG(uap, flags));
175 if (SCARG(uap, flags) & O_CREAT)
176 CHECKALTCREAT(&sg, SCARG(uap, path));
178 CHECKALTEXIST(&sg, SCARG(uap, path));
179 ret = open((struct open_args *)uap);
184 if (!strcmp(SCARG(uap, path), "/compat/ibcs2/dev/spx"))
187 #endif /* SPX_HACK */
188 if (!ret && !noctty && p && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
189 struct filedesc *fdp = p->p_fd;
190 struct file *fp = fdp->fd_files[uap->sysmsg_result].fp;
192 /* ignore any error, just give it a try */
193 if (fp->f_type == DTYPE_VNODE)
194 fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, td);
200 ibcs2_creat(struct ibcs2_creat_args *uap)
202 struct open_args cup;
203 caddr_t sg = stackgap_init();
205 CHECKALTCREAT(&sg, SCARG(uap, path));
206 SCARG(&cup, path) = SCARG(uap, path);
207 SCARG(&cup, mode) = SCARG(uap, mode);
208 SCARG(&cup, flags) = O_WRONLY | O_CREAT | O_TRUNC;
213 ibcs2_access(struct ibcs2_access_args *uap)
215 struct access_args cup;
216 caddr_t sg = stackgap_init();
218 CHECKALTEXIST(&sg, SCARG(uap, path));
219 SCARG(&cup, path) = SCARG(uap, path);
220 SCARG(&cup, flags) = SCARG(uap, flags);
225 ibcs2_fcntl(struct ibcs2_fcntl_args *uap)
228 struct fcntl_args fa;
230 struct ibcs2_flock ifl;
232 switch(SCARG(uap, cmd)) {
234 SCARG(&fa, fd) = SCARG(uap, fd);
235 SCARG(&fa, cmd) = F_DUPFD;
236 SCARG(&fa, arg) = (/* XXX */ int)SCARG(uap, arg);
239 SCARG(&fa, fd) = SCARG(uap, fd);
240 SCARG(&fa, cmd) = F_GETFD;
241 SCARG(&fa, arg) = (/* XXX */ int)SCARG(uap, arg);
244 SCARG(&fa, fd) = SCARG(uap, fd);
245 SCARG(&fa, cmd) = F_SETFD;
246 SCARG(&fa, arg) = (/* XXX */ int)SCARG(uap, arg);
249 SCARG(&fa, fd) = SCARG(uap, fd);
250 SCARG(&fa, cmd) = F_GETFL;
251 SCARG(&fa, arg) = (/* XXX */ int)SCARG(uap, arg);
255 uap->sysmsg_result = oflags2ioflags(fa.sysmsg_result);
258 SCARG(&fa, fd) = SCARG(uap, fd);
259 SCARG(&fa, cmd) = F_SETFL;
260 SCARG(&fa, arg) = (/* XXX */ int)
261 ioflags2oflags((int)SCARG(uap, arg));
265 caddr_t sg = stackgap_init();
266 flp = stackgap_alloc(&sg, sizeof(*flp));
267 error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&ifl,
271 cvt_iflock2flock(&ifl, flp);
272 SCARG(&fa, fd) = SCARG(uap, fd);
273 SCARG(&fa, cmd) = F_GETLK;
274 SCARG(&fa, arg) = (/* XXX */ int)flp;
276 uap->sysmsg_result = fa.sysmsg_result;
279 cvt_flock2iflock(flp, &ifl);
280 return copyout((caddr_t)&ifl, (caddr_t)SCARG(uap, arg),
286 caddr_t sg = stackgap_init();
287 flp = stackgap_alloc(&sg, sizeof(*flp));
288 error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&ifl,
292 cvt_iflock2flock(&ifl, flp);
293 SCARG(&fa, fd) = SCARG(uap, fd);
294 SCARG(&fa, cmd) = F_SETLK;
295 SCARG(&fa, arg) = (/* XXX */ int)flp;
300 caddr_t sg = stackgap_init();
301 flp = stackgap_alloc(&sg, sizeof(*flp));
302 error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&ifl,
306 cvt_iflock2flock(&ifl, flp);
307 SCARG(&fa, fd) = SCARG(uap, fd);
308 SCARG(&fa, cmd) = F_SETLKW;
309 SCARG(&fa, arg) = (/* XXX */ int)flp;
316 uap->sysmsg_result = fa.sysmsg_result;