The thread/proc pointer argument in the VFS subsystem originally existed
[dragonfly.git] / sys / emulation / ibcs2 / i386 / ibcs2_fcntl.c
CommitLineData
984263bc
MD
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: src/sys/i386/ibcs2/ibcs2_fcntl.c,v 1.14 1999/09/19 17:00:14 green Exp $
87de5057 28 * $DragonFly: src/sys/emulation/ibcs2/i386/Attic/ibcs2_fcntl.c,v 1.12 2006/05/06 02:43:11 dillon Exp $
984263bc
MD
29 */
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/ttycom.h>
39#include <sys/sysproto.h>
dadab5e9
MD
40#include <sys/proc.h>
41#include <sys/file2.h>
984263bc 42
1f2de5d4
MD
43#include "ibcs2_fcntl.h"
44#include "ibcs2_signal.h"
45#include "ibcs2_proto.h"
46#include "ibcs2_util.h"
984263bc 47
b1efe3b4
RG
48static void cvt_iflock2flock (struct ibcs2_flock *, struct flock *);
49static void cvt_flock2iflock (struct flock *, struct ibcs2_flock *);
50static int cvt_o_flags (int);
51static int oflags2ioflags (int);
52static int ioflags2oflags (int);
984263bc
MD
53
54static int
2da2a8af 55cvt_o_flags(int flags)
984263bc
MD
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
74static void
2da2a8af 75cvt_flock2iflock(struct flock *flp, struct ibcs2_flock *iflp)
984263bc
MD
76{
77 switch (flp->l_type) {
78 case F_RDLCK:
79 iflp->l_type = IBCS2_F_RDLCK;
80 break;
81 case F_WRLCK:
82 iflp->l_type = IBCS2_F_WRLCK;
83 break;
84 case F_UNLCK:
85 iflp->l_type = IBCS2_F_UNLCK;
86 break;
87 }
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;
91 iflp->l_sysid = 0;
92 iflp->l_pid = (ibcs2_pid_t)flp->l_pid;
93}
94
95#ifdef DEBUG_IBCS2
96static void
97print_flock(struct flock *flp)
98{
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);
102}
103#endif
104
105static void
2da2a8af 106cvt_iflock2flock(struct ibcs2_flock *iflp, struct flock *flp)
984263bc
MD
107{
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) {
112 case IBCS2_F_RDLCK:
113 flp->l_type = F_RDLCK;
114 break;
115 case IBCS2_F_WRLCK:
116 flp->l_type = F_WRLCK;
117 break;
118 case IBCS2_F_UNLCK:
119 flp->l_type = F_UNLCK;
120 break;
121 }
122 flp->l_whence = iflp->l_whence;
123}
124
125/* convert iBCS2 mode into NetBSD mode */
126static int
2da2a8af 127ioflags2oflags(int flags)
984263bc
MD
128{
129 int r = 0;
130
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;
142 return r;
143}
144
145/* convert NetBSD mode into iBCS2 mode */
146static int
2da2a8af 147oflags2ioflags(int flags)
984263bc
MD
148{
149 int r = 0;
150
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;
162 return r;
163}
164
165int
41c20dac 166ibcs2_open(struct ibcs2_open_args *uap)
984263bc 167{
dadab5e9
MD
168 struct thread *td = curthread; /* XXX */
169 struct proc *p;
984263bc
MD
170 int noctty = SCARG(uap, flags) & IBCS2_O_NOCTTY;
171 int ret;
172 caddr_t sg = stackgap_init();
173
174 SCARG(uap, flags) = cvt_o_flags(SCARG(uap, flags));
175 if (SCARG(uap, flags) & O_CREAT)
41c20dac 176 CHECKALTCREAT(&sg, SCARG(uap, path));
984263bc 177 else
41c20dac
MD
178 CHECKALTEXIST(&sg, SCARG(uap, path));
179 ret = open((struct open_args *)uap);
984263bc 180
7b95be2a 181 p = td->td_proc;
984263bc
MD
182#ifdef SPX_HACK
183 if (ret == ENXIO) {
184 if (!strcmp(SCARG(uap, path), "/compat/ibcs2/dev/spx"))
41c20dac 185 ret = spx_open(uap);
984263bc
MD
186 } else
187#endif /* SPX_HACK */
dadab5e9 188 if (!ret && !noctty && p && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
984263bc 189 struct filedesc *fdp = p->p_fd;
0679adc4 190 struct file *fp = fdp->fd_files[uap->sysmsg_result].fp;
984263bc
MD
191
192 /* ignore any error, just give it a try */
193 if (fp->f_type == DTYPE_VNODE)
87de5057 194 fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, p->p_ucred);
984263bc
MD
195 }
196 return ret;
197}
198
199int
41c20dac 200ibcs2_creat(struct ibcs2_creat_args *uap)
984263bc
MD
201{
202 struct open_args cup;
203 caddr_t sg = stackgap_init();
204
41c20dac 205 CHECKALTCREAT(&sg, SCARG(uap, path));
984263bc
MD
206 SCARG(&cup, path) = SCARG(uap, path);
207 SCARG(&cup, mode) = SCARG(uap, mode);
208 SCARG(&cup, flags) = O_WRONLY | O_CREAT | O_TRUNC;
41c20dac 209 return open(&cup);
984263bc
MD
210}
211
212int
41c20dac 213ibcs2_access(struct ibcs2_access_args *uap)
984263bc
MD
214{
215 struct access_args cup;
216 caddr_t sg = stackgap_init();
217
41c20dac 218 CHECKALTEXIST(&sg, SCARG(uap, path));
984263bc
MD
219 SCARG(&cup, path) = SCARG(uap, path);
220 SCARG(&cup, flags) = SCARG(uap, flags);
41c20dac 221 return access(&cup);
984263bc
MD
222}
223
224int
41c20dac 225ibcs2_fcntl(struct ibcs2_fcntl_args *uap)
984263bc
MD
226{
227 int error;
228 struct fcntl_args fa;
229 struct flock *flp;
230 struct ibcs2_flock ifl;
231
232 switch(SCARG(uap, cmd)) {
233 case IBCS2_F_DUPFD:
234 SCARG(&fa, fd) = SCARG(uap, fd);
235 SCARG(&fa, cmd) = F_DUPFD;
236 SCARG(&fa, arg) = (/* XXX */ int)SCARG(uap, arg);
90b9818c 237 break;
984263bc
MD
238 case IBCS2_F_GETFD:
239 SCARG(&fa, fd) = SCARG(uap, fd);
240 SCARG(&fa, cmd) = F_GETFD;
241 SCARG(&fa, arg) = (/* XXX */ int)SCARG(uap, arg);
90b9818c 242 break;
984263bc
MD
243 case IBCS2_F_SETFD:
244 SCARG(&fa, fd) = SCARG(uap, fd);
245 SCARG(&fa, cmd) = F_SETFD;
246 SCARG(&fa, arg) = (/* XXX */ int)SCARG(uap, arg);
90b9818c 247 break;
984263bc
MD
248 case IBCS2_F_GETFL:
249 SCARG(&fa, fd) = SCARG(uap, fd);
250 SCARG(&fa, cmd) = F_GETFL;
251 SCARG(&fa, arg) = (/* XXX */ int)SCARG(uap, arg);
41c20dac 252 error = fcntl(&fa);
984263bc
MD
253 if (error)
254 return error;
c7114eea 255 uap->sysmsg_result = oflags2ioflags(fa.sysmsg_result);
984263bc
MD
256 return error;
257 case IBCS2_F_SETFL:
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));
90b9818c 262 break;
984263bc
MD
263 case IBCS2_F_GETLK:
264 {
265 caddr_t sg = stackgap_init();
266 flp = stackgap_alloc(&sg, sizeof(*flp));
267 error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&ifl,
268 ibcs2_flock_len);
269 if (error)
270 return error;
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;
41c20dac 275 error = fcntl(&fa);
c7114eea 276 uap->sysmsg_result = fa.sysmsg_result;
984263bc
MD
277 if (error)
278 return error;
279 cvt_flock2iflock(flp, &ifl);
280 return copyout((caddr_t)&ifl, (caddr_t)SCARG(uap, arg),
281 ibcs2_flock_len);
282 }
90b9818c 283 break;
984263bc
MD
284 case IBCS2_F_SETLK:
285 {
286 caddr_t sg = stackgap_init();
287 flp = stackgap_alloc(&sg, sizeof(*flp));
288 error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&ifl,
289 ibcs2_flock_len);
290 if (error)
291 return error;
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;
984263bc 296 }
90b9818c 297 break;
984263bc
MD
298 case IBCS2_F_SETLKW:
299 {
300 caddr_t sg = stackgap_init();
301 flp = stackgap_alloc(&sg, sizeof(*flp));
302 error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&ifl,
303 ibcs2_flock_len);
304 if (error)
305 return error;
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;
984263bc 310 }
90b9818c
MD
311 break;
312 default:
313 return ENOSYS;
984263bc 314 }
90b9818c 315 error = fcntl(&fa);
c7114eea 316 uap->sysmsg_result = fa.sysmsg_result;
90b9818c 317 return(error);
984263bc 318}