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.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_spx_hack.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/capsicum.h>
36 #include <sys/fcntl.h>
38 #include <sys/filedesc.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/syscallsubr.h>
43 #include <sys/sysproto.h>
44 #include <sys/ttycom.h>
46 #include <i386/ibcs2/ibcs2_fcntl.h>
47 #include <i386/ibcs2/ibcs2_signal.h>
48 #include <i386/ibcs2/ibcs2_proto.h>
49 #include <i386/ibcs2/ibcs2_util.h>
51 static void cvt_iflock2flock(struct ibcs2_flock *, struct flock *);
52 static void cvt_flock2iflock(struct flock *, struct ibcs2_flock *);
53 static int cvt_o_flags(int);
54 static int oflags2ioflags(int);
55 static int ioflags2oflags(int);
63 /* convert mode into NetBSD mode */
64 if (flags & IBCS2_O_WRONLY) r |= O_WRONLY;
65 if (flags & IBCS2_O_RDWR) r |= O_RDWR;
66 if (flags & (IBCS2_O_NDELAY | IBCS2_O_NONBLOCK)) r |= O_NONBLOCK;
67 if (flags & IBCS2_O_APPEND) r |= O_APPEND;
68 if (flags & IBCS2_O_SYNC) r |= O_FSYNC;
69 if (flags & IBCS2_O_CREAT) r |= O_CREAT;
70 if (flags & IBCS2_O_TRUNC) r |= O_TRUNC /* | O_CREAT ??? */;
71 if (flags & IBCS2_O_EXCL) r |= O_EXCL;
72 if (flags & IBCS2_O_RDONLY) r |= O_RDONLY;
73 if (flags & IBCS2_O_PRIV) r |= O_EXLOCK;
74 if (flags & IBCS2_O_NOCTTY) r |= O_NOCTTY;
79 cvt_flock2iflock(flp, iflp)
81 struct ibcs2_flock *iflp;
83 switch (flp->l_type) {
85 iflp->l_type = IBCS2_F_RDLCK;
88 iflp->l_type = IBCS2_F_WRLCK;
91 iflp->l_type = IBCS2_F_UNLCK;
94 iflp->l_whence = (short)flp->l_whence;
95 iflp->l_start = (ibcs2_off_t)flp->l_start;
96 iflp->l_len = (ibcs2_off_t)flp->l_len;
97 iflp->l_sysid = flp->l_sysid;
98 iflp->l_pid = (ibcs2_pid_t)flp->l_pid;
103 print_flock(struct flock *flp)
105 printf("flock: start=%x len=%x pid=%d type=%d whence=%d\n",
106 (int)flp->l_start, (int)flp->l_len, (int)flp->l_pid,
107 flp->l_type, flp->l_whence);
112 cvt_iflock2flock(iflp, flp)
113 struct ibcs2_flock *iflp;
116 flp->l_start = (off_t)iflp->l_start;
117 flp->l_len = (off_t)iflp->l_len;
118 flp->l_pid = (pid_t)iflp->l_pid;
119 switch (iflp->l_type) {
121 flp->l_type = F_RDLCK;
124 flp->l_type = F_WRLCK;
127 flp->l_type = F_UNLCK;
130 flp->l_whence = iflp->l_whence;
131 flp->l_sysid = iflp->l_sysid;
134 /* convert iBCS2 mode into NetBSD mode */
136 ioflags2oflags(flags)
141 if (flags & IBCS2_O_RDONLY) r |= O_RDONLY;
142 if (flags & IBCS2_O_WRONLY) r |= O_WRONLY;
143 if (flags & IBCS2_O_RDWR) r |= O_RDWR;
144 if (flags & IBCS2_O_NDELAY) r |= O_NONBLOCK;
145 if (flags & IBCS2_O_APPEND) r |= O_APPEND;
146 if (flags & IBCS2_O_SYNC) r |= O_FSYNC;
147 if (flags & IBCS2_O_NONBLOCK) r |= O_NONBLOCK;
148 if (flags & IBCS2_O_CREAT) r |= O_CREAT;
149 if (flags & IBCS2_O_TRUNC) r |= O_TRUNC;
150 if (flags & IBCS2_O_EXCL) r |= O_EXCL;
151 if (flags & IBCS2_O_NOCTTY) r |= O_NOCTTY;
155 /* convert NetBSD mode into iBCS2 mode */
157 oflags2ioflags(flags)
162 if (flags & O_RDONLY) r |= IBCS2_O_RDONLY;
163 if (flags & O_WRONLY) r |= IBCS2_O_WRONLY;
164 if (flags & O_RDWR) r |= IBCS2_O_RDWR;
165 if (flags & O_NDELAY) r |= IBCS2_O_NONBLOCK;
166 if (flags & O_APPEND) r |= IBCS2_O_APPEND;
167 if (flags & O_FSYNC) r |= IBCS2_O_SYNC;
168 if (flags & O_NONBLOCK) r |= IBCS2_O_NONBLOCK;
169 if (flags & O_CREAT) r |= IBCS2_O_CREAT;
170 if (flags & O_TRUNC) r |= IBCS2_O_TRUNC;
171 if (flags & O_EXCL) r |= IBCS2_O_EXCL;
172 if (flags & O_NOCTTY) r |= IBCS2_O_NOCTTY;
179 struct ibcs2_open_args *uap;
183 int flags, noctty, ret;
186 noctty = uap->flags & IBCS2_O_NOCTTY;
187 flags = cvt_o_flags(uap->flags);
188 if (uap->flags & O_CREAT)
189 CHECKALTCREAT(td, uap->path, &path);
191 CHECKALTEXIST(td, uap->path, &path);
192 ret = kern_openat(td, AT_FDCWD, path, UIO_SYSSPACE, flags, uap->mode);
196 if (!strcmp(path, "/compat/ibcs2/dev/spx"))
200 #endif /* SPX_HACK */
203 if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
208 error = fget(td, td->td_retval[0],
209 cap_rights_init(&rights, CAP_IOCTL), &fp);
214 /* ignore any error, just give it a try */
215 if (fp->f_type == DTYPE_VNODE)
216 fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, td->td_ucred,
227 struct ibcs2_creat_args *uap;
232 CHECKALTCREAT(td, uap->path, &path);
233 error = kern_openat(td, AT_FDCWD, path, UIO_SYSSPACE,
234 O_WRONLY | O_CREAT | O_TRUNC, uap->mode);
240 ibcs2_access(td, uap)
242 struct ibcs2_access_args *uap;
247 CHECKALTEXIST(td, uap->path, &path);
248 error = kern_accessat(td, AT_FDCWD, path, UIO_SYSSPACE, 0, uap->amode);
256 struct ibcs2_fcntl_args *uap;
261 struct ibcs2_flock ifl;
263 arg = (intptr_t)uap->arg;
266 return (kern_fcntl(td, uap->fd, F_DUPFD, arg));
268 return (kern_fcntl(td, uap->fd, F_GETFD, arg));
270 return (kern_fcntl(td, uap->fd, F_SETFD, arg));
272 error = kern_fcntl(td, uap->fd, F_GETFL, arg);
275 td->td_retval[0] = oflags2ioflags(td->td_retval[0]);
278 return (kern_fcntl(td, uap->fd, F_SETFL,
279 ioflags2oflags(arg)));
283 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
287 cvt_iflock2flock(&ifl, &fl);
288 error = kern_fcntl(td, uap->fd, F_GETLK, (intptr_t)&fl);
291 cvt_flock2iflock(&fl, &ifl);
292 return copyout((caddr_t)&ifl, (caddr_t)uap->arg,
298 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
302 cvt_iflock2flock(&ifl, &fl);
303 return (kern_fcntl(td, uap->fd, F_SETLK, (intptr_t)&fl));
308 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
312 cvt_iflock2flock(&ifl, &fl);
313 return (kern_fcntl(td, uap->fd, F_SETLKW, (intptr_t)&fl));