2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1995 Scott Bartram
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_spx_hack.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/capsicum.h>
38 #include <sys/fcntl.h>
40 #include <sys/filedesc.h>
42 #include <sys/malloc.h>
43 #include <sys/mutex.h>
44 #include <sys/syscallsubr.h>
45 #include <sys/sysproto.h>
46 #include <sys/ttycom.h>
48 #include <i386/ibcs2/ibcs2_fcntl.h>
49 #include <i386/ibcs2/ibcs2_signal.h>
50 #include <i386/ibcs2/ibcs2_proto.h>
51 #include <i386/ibcs2/ibcs2_util.h>
53 static void cvt_iflock2flock(struct ibcs2_flock *, struct flock *);
54 static void cvt_flock2iflock(struct flock *, struct ibcs2_flock *);
55 static int cvt_o_flags(int);
56 static int oflags2ioflags(int);
57 static int ioflags2oflags(int);
65 /* convert mode into NetBSD mode */
66 if (flags & IBCS2_O_WRONLY) r |= O_WRONLY;
67 if (flags & IBCS2_O_RDWR) r |= O_RDWR;
68 if (flags & (IBCS2_O_NDELAY | IBCS2_O_NONBLOCK)) r |= O_NONBLOCK;
69 if (flags & IBCS2_O_APPEND) r |= O_APPEND;
70 if (flags & IBCS2_O_SYNC) r |= O_FSYNC;
71 if (flags & IBCS2_O_CREAT) r |= O_CREAT;
72 if (flags & IBCS2_O_TRUNC) r |= O_TRUNC /* | O_CREAT ??? */;
73 if (flags & IBCS2_O_EXCL) r |= O_EXCL;
74 if (flags & IBCS2_O_RDONLY) r |= O_RDONLY;
75 if (flags & IBCS2_O_PRIV) r |= O_EXLOCK;
76 if (flags & IBCS2_O_NOCTTY) r |= O_NOCTTY;
81 cvt_flock2iflock(flp, iflp)
83 struct ibcs2_flock *iflp;
85 switch (flp->l_type) {
87 iflp->l_type = IBCS2_F_RDLCK;
90 iflp->l_type = IBCS2_F_WRLCK;
93 iflp->l_type = IBCS2_F_UNLCK;
96 iflp->l_whence = (short)flp->l_whence;
97 iflp->l_start = (ibcs2_off_t)flp->l_start;
98 iflp->l_len = (ibcs2_off_t)flp->l_len;
99 iflp->l_sysid = flp->l_sysid;
100 iflp->l_pid = (ibcs2_pid_t)flp->l_pid;
105 print_flock(struct flock *flp)
107 printf("flock: start=%x len=%x pid=%d type=%d whence=%d\n",
108 (int)flp->l_start, (int)flp->l_len, (int)flp->l_pid,
109 flp->l_type, flp->l_whence);
114 cvt_iflock2flock(iflp, flp)
115 struct ibcs2_flock *iflp;
118 flp->l_start = (off_t)iflp->l_start;
119 flp->l_len = (off_t)iflp->l_len;
120 flp->l_pid = (pid_t)iflp->l_pid;
121 switch (iflp->l_type) {
123 flp->l_type = F_RDLCK;
126 flp->l_type = F_WRLCK;
129 flp->l_type = F_UNLCK;
132 flp->l_whence = iflp->l_whence;
133 flp->l_sysid = iflp->l_sysid;
136 /* convert iBCS2 mode into NetBSD mode */
138 ioflags2oflags(flags)
143 if (flags & IBCS2_O_RDONLY) r |= O_RDONLY;
144 if (flags & IBCS2_O_WRONLY) r |= O_WRONLY;
145 if (flags & IBCS2_O_RDWR) r |= O_RDWR;
146 if (flags & IBCS2_O_NDELAY) r |= O_NONBLOCK;
147 if (flags & IBCS2_O_APPEND) r |= O_APPEND;
148 if (flags & IBCS2_O_SYNC) r |= O_FSYNC;
149 if (flags & IBCS2_O_NONBLOCK) r |= O_NONBLOCK;
150 if (flags & IBCS2_O_CREAT) r |= O_CREAT;
151 if (flags & IBCS2_O_TRUNC) r |= O_TRUNC;
152 if (flags & IBCS2_O_EXCL) r |= O_EXCL;
153 if (flags & IBCS2_O_NOCTTY) r |= O_NOCTTY;
157 /* convert NetBSD mode into iBCS2 mode */
159 oflags2ioflags(flags)
164 if (flags & O_RDONLY) r |= IBCS2_O_RDONLY;
165 if (flags & O_WRONLY) r |= IBCS2_O_WRONLY;
166 if (flags & O_RDWR) r |= IBCS2_O_RDWR;
167 if (flags & O_NDELAY) r |= IBCS2_O_NONBLOCK;
168 if (flags & O_APPEND) r |= IBCS2_O_APPEND;
169 if (flags & O_FSYNC) r |= IBCS2_O_SYNC;
170 if (flags & O_NONBLOCK) r |= IBCS2_O_NONBLOCK;
171 if (flags & O_CREAT) r |= IBCS2_O_CREAT;
172 if (flags & O_TRUNC) r |= IBCS2_O_TRUNC;
173 if (flags & O_EXCL) r |= IBCS2_O_EXCL;
174 if (flags & O_NOCTTY) r |= IBCS2_O_NOCTTY;
181 struct ibcs2_open_args *uap;
185 int flags, noctty, ret;
188 noctty = uap->flags & IBCS2_O_NOCTTY;
189 flags = cvt_o_flags(uap->flags);
190 if (uap->flags & O_CREAT)
191 CHECKALTCREAT(td, uap->path, &path);
193 CHECKALTEXIST(td, uap->path, &path);
194 ret = kern_openat(td, AT_FDCWD, path, UIO_SYSSPACE, flags, uap->mode);
198 if (!strcmp(path, "/compat/ibcs2/dev/spx"))
202 #endif /* SPX_HACK */
205 if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
210 error = fget(td, td->td_retval[0],
211 cap_rights_init(&rights, CAP_IOCTL), &fp);
216 /* ignore any error, just give it a try */
217 if (fp->f_type == DTYPE_VNODE)
218 fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, td->td_ucred,
229 struct ibcs2_creat_args *uap;
234 CHECKALTCREAT(td, uap->path, &path);
235 error = kern_openat(td, AT_FDCWD, path, UIO_SYSSPACE,
236 O_WRONLY | O_CREAT | O_TRUNC, uap->mode);
242 ibcs2_access(td, uap)
244 struct ibcs2_access_args *uap;
249 CHECKALTEXIST(td, uap->path, &path);
250 error = kern_accessat(td, AT_FDCWD, path, UIO_SYSSPACE, 0, uap->amode);
258 struct ibcs2_fcntl_args *uap;
263 struct ibcs2_flock ifl;
265 arg = (intptr_t)uap->arg;
268 return (kern_fcntl(td, uap->fd, F_DUPFD, arg));
270 return (kern_fcntl(td, uap->fd, F_GETFD, arg));
272 return (kern_fcntl(td, uap->fd, F_SETFD, arg));
274 error = kern_fcntl(td, uap->fd, F_GETFL, arg);
277 td->td_retval[0] = oflags2ioflags(td->td_retval[0]);
280 return (kern_fcntl(td, uap->fd, F_SETFL,
281 ioflags2oflags(arg)));
285 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
289 cvt_iflock2flock(&ifl, &fl);
290 error = kern_fcntl(td, uap->fd, F_GETLK, (intptr_t)&fl);
293 cvt_flock2iflock(&fl, &ifl);
294 return copyout((caddr_t)&ifl, (caddr_t)uap->arg,
300 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
304 cvt_iflock2flock(&ifl, &fl);
305 return (kern_fcntl(td, uap->fd, F_SETLK, (intptr_t)&fl));
310 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
314 cvt_iflock2flock(&ifl, &fl);
315 return (kern_fcntl(td, uap->fd, F_SETLKW, (intptr_t)&fl));