ibcs2_fcntl.c revision 177633
175107Sfenner/*- 275107Sfenner * Copyright (c) 1995 Scott Bartram 375107Sfenner * All rights reserved. 475107Sfenner * 575107Sfenner * Redistribution and use in source and binary forms, with or without 675107Sfenner * modification, are permitted provided that the following conditions 775107Sfenner * are met: 875107Sfenner * 1. Redistributions of source code must retain the above copyright 9214518Srpaulo * notice, this list of conditions and the following disclaimer. 1075107Sfenner * 2. Redistributions in binary form must reproduce the above copyright 1175107Sfenner * notice, this list of conditions and the following disclaimer in the 1275107Sfenner * documentation and/or other materials provided with the distribution. 1375107Sfenner * 3. The name of the author may not be used to endorse or promote products 1498530Sfenner * derived from this software without specific prior written permission 1598530Sfenner * 1675107Sfenner * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1775107Sfenner * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1875107Sfenner * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1975107Sfenner * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2075107Sfenner * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2175107Sfenner * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2275107Sfenner * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2375107Sfenner * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2498530Sfenner * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2598530Sfenner * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2698530Sfenner */ 2798530Sfenner 2898530Sfenner#include <sys/cdefs.h> 2998530Sfenner__FBSDID("$FreeBSD: head/sys/i386/ibcs2/ibcs2_fcntl.c 177633 2008-03-26 15:23:12Z dfr $"); 3098530Sfenner 3175107Sfenner#include "opt_spx_hack.h" 3275107Sfenner 3375107Sfenner#include <sys/param.h> 3475107Sfenner#include <sys/systm.h> 3575107Sfenner#include <sys/fcntl.h> 36#include <sys/file.h> 37#include <sys/filedesc.h> 38#include <sys/lock.h> 39#include <sys/malloc.h> 40#include <sys/mutex.h> 41#include <sys/syscallsubr.h> 42#include <sys/sysproto.h> 43#include <sys/ttycom.h> 44 45#include <i386/ibcs2/ibcs2_fcntl.h> 46#include <i386/ibcs2/ibcs2_signal.h> 47#include <i386/ibcs2/ibcs2_proto.h> 48#include <i386/ibcs2/ibcs2_util.h> 49 50static void cvt_iflock2flock(struct ibcs2_flock *, struct flock *); 51static void cvt_flock2iflock(struct flock *, struct ibcs2_flock *); 52static int cvt_o_flags(int); 53static int oflags2ioflags(int); 54static int ioflags2oflags(int); 55 56static int 57cvt_o_flags(flags) 58 int flags; 59{ 60 int r = 0; 61 62 /* convert mode into NetBSD mode */ 63 if (flags & IBCS2_O_WRONLY) r |= O_WRONLY; 64 if (flags & IBCS2_O_RDWR) r |= O_RDWR; 65 if (flags & (IBCS2_O_NDELAY | IBCS2_O_NONBLOCK)) r |= O_NONBLOCK; 66 if (flags & IBCS2_O_APPEND) r |= O_APPEND; 67 if (flags & IBCS2_O_SYNC) r |= O_FSYNC; 68 if (flags & IBCS2_O_CREAT) r |= O_CREAT; 69 if (flags & IBCS2_O_TRUNC) r |= O_TRUNC /* | O_CREAT ??? */; 70 if (flags & IBCS2_O_EXCL) r |= O_EXCL; 71 if (flags & IBCS2_O_RDONLY) r |= O_RDONLY; 72 if (flags & IBCS2_O_PRIV) r |= O_EXLOCK; 73 if (flags & IBCS2_O_NOCTTY) r |= O_NOCTTY; 74 return r; 75} 76 77static void 78cvt_flock2iflock(flp, iflp) 79 struct flock *flp; 80 struct ibcs2_flock *iflp; 81{ 82 switch (flp->l_type) { 83 case F_RDLCK: 84 iflp->l_type = IBCS2_F_RDLCK; 85 break; 86 case F_WRLCK: 87 iflp->l_type = IBCS2_F_WRLCK; 88 break; 89 case F_UNLCK: 90 iflp->l_type = IBCS2_F_UNLCK; 91 break; 92 } 93 iflp->l_whence = (short)flp->l_whence; 94 iflp->l_start = (ibcs2_off_t)flp->l_start; 95 iflp->l_len = (ibcs2_off_t)flp->l_len; 96 iflp->l_sysid = flp->l_sysid; 97 iflp->l_pid = (ibcs2_pid_t)flp->l_pid; 98} 99 100#ifdef DEBUG_IBCS2 101static void 102print_flock(struct flock *flp) 103{ 104 printf("flock: start=%x len=%x pid=%d type=%d whence=%d\n", 105 (int)flp->l_start, (int)flp->l_len, (int)flp->l_pid, 106 flp->l_type, flp->l_whence); 107} 108#endif 109 110static void 111cvt_iflock2flock(iflp, flp) 112 struct ibcs2_flock *iflp; 113 struct flock *flp; 114{ 115 flp->l_start = (off_t)iflp->l_start; 116 flp->l_len = (off_t)iflp->l_len; 117 flp->l_pid = (pid_t)iflp->l_pid; 118 switch (iflp->l_type) { 119 case IBCS2_F_RDLCK: 120 flp->l_type = F_RDLCK; 121 break; 122 case IBCS2_F_WRLCK: 123 flp->l_type = F_WRLCK; 124 break; 125 case IBCS2_F_UNLCK: 126 flp->l_type = F_UNLCK; 127 break; 128 } 129 flp->l_whence = iflp->l_whence; 130 flp->l_sysid = iflp->l_sysid; 131} 132 133/* convert iBCS2 mode into NetBSD mode */ 134static int 135ioflags2oflags(flags) 136 int flags; 137{ 138 int r = 0; 139 140 if (flags & IBCS2_O_RDONLY) r |= O_RDONLY; 141 if (flags & IBCS2_O_WRONLY) r |= O_WRONLY; 142 if (flags & IBCS2_O_RDWR) r |= O_RDWR; 143 if (flags & IBCS2_O_NDELAY) r |= O_NONBLOCK; 144 if (flags & IBCS2_O_APPEND) r |= O_APPEND; 145 if (flags & IBCS2_O_SYNC) r |= O_FSYNC; 146 if (flags & IBCS2_O_NONBLOCK) r |= O_NONBLOCK; 147 if (flags & IBCS2_O_CREAT) r |= O_CREAT; 148 if (flags & IBCS2_O_TRUNC) r |= O_TRUNC; 149 if (flags & IBCS2_O_EXCL) r |= O_EXCL; 150 if (flags & IBCS2_O_NOCTTY) r |= O_NOCTTY; 151 return r; 152} 153 154/* convert NetBSD mode into iBCS2 mode */ 155static int 156oflags2ioflags(flags) 157 int flags; 158{ 159 int r = 0; 160 161 if (flags & O_RDONLY) r |= IBCS2_O_RDONLY; 162 if (flags & O_WRONLY) r |= IBCS2_O_WRONLY; 163 if (flags & O_RDWR) r |= IBCS2_O_RDWR; 164 if (flags & O_NDELAY) r |= IBCS2_O_NONBLOCK; 165 if (flags & O_APPEND) r |= IBCS2_O_APPEND; 166 if (flags & O_FSYNC) r |= IBCS2_O_SYNC; 167 if (flags & O_NONBLOCK) r |= IBCS2_O_NONBLOCK; 168 if (flags & O_CREAT) r |= IBCS2_O_CREAT; 169 if (flags & O_TRUNC) r |= IBCS2_O_TRUNC; 170 if (flags & O_EXCL) r |= IBCS2_O_EXCL; 171 if (flags & O_NOCTTY) r |= IBCS2_O_NOCTTY; 172 return r; 173} 174 175int 176ibcs2_open(td, uap) 177 struct thread *td; 178 struct ibcs2_open_args *uap; 179{ 180 struct proc *p; 181 char *path; 182 int flags, noctty, ret; 183 184 p = td->td_proc; 185 noctty = uap->flags & IBCS2_O_NOCTTY; 186 flags = cvt_o_flags(uap->flags); 187 if (uap->flags & O_CREAT) 188 CHECKALTCREAT(td, uap->path, &path); 189 else 190 CHECKALTEXIST(td, uap->path, &path); 191 ret = kern_open(td, path, UIO_SYSSPACE, flags, uap->mode); 192 193#ifdef SPX_HACK 194 if (ret == ENXIO) { 195 if (!strcmp(path, "/compat/ibcs2/dev/spx")) 196 ret = spx_open(td); 197 free(path, M_TEMP); 198 } else 199#endif /* SPX_HACK */ 200 free(path, M_TEMP); 201 PROC_LOCK(p); 202 if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) { 203 struct file *fp; 204 int error; 205 206 error = fget(td, td->td_retval[0], &fp); 207 PROC_UNLOCK(p); 208 if (error) 209 return (EBADF); 210 211 /* ignore any error, just give it a try */ 212 if (fp->f_type == DTYPE_VNODE) 213 fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, td->td_ucred, 214 td); 215 fdrop(fp, td); 216 } else 217 PROC_UNLOCK(p); 218 return ret; 219} 220 221int 222ibcs2_creat(td, uap) 223 struct thread *td; 224 struct ibcs2_creat_args *uap; 225{ 226 char *path; 227 int error; 228 229 CHECKALTCREAT(td, uap->path, &path); 230 error = kern_open(td, path, UIO_SYSSPACE, O_WRONLY | O_CREAT | O_TRUNC, 231 uap->mode); 232 free(path, M_TEMP); 233 return (error); 234} 235 236int 237ibcs2_access(td, uap) 238 struct thread *td; 239 struct ibcs2_access_args *uap; 240{ 241 char *path; 242 int error; 243 244 CHECKALTEXIST(td, uap->path, &path); 245 error = kern_access(td, path, UIO_SYSSPACE, uap->flags); 246 free(path, M_TEMP); 247 return (error); 248} 249 250int 251ibcs2_fcntl(td, uap) 252 struct thread *td; 253 struct ibcs2_fcntl_args *uap; 254{ 255 intptr_t arg; 256 int error; 257 struct flock fl; 258 struct ibcs2_flock ifl; 259 260 arg = (intptr_t)uap->arg; 261 switch(uap->cmd) { 262 case IBCS2_F_DUPFD: 263 return (kern_fcntl(td, uap->fd, F_DUPFD, arg)); 264 case IBCS2_F_GETFD: 265 return (kern_fcntl(td, uap->fd, F_GETFD, arg)); 266 case IBCS2_F_SETFD: 267 return (kern_fcntl(td, uap->fd, F_SETFD, arg)); 268 case IBCS2_F_GETFL: 269 error = kern_fcntl(td, uap->fd, F_GETFL, arg); 270 if (error) 271 return error; 272 td->td_retval[0] = oflags2ioflags(td->td_retval[0]); 273 return error; 274 case IBCS2_F_SETFL: 275 return (kern_fcntl(td, uap->fd, F_SETFL, 276 ioflags2oflags(arg))); 277 278 case IBCS2_F_GETLK: 279 { 280 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl, 281 ibcs2_flock_len); 282 if (error) 283 return error; 284 cvt_iflock2flock(&ifl, &fl); 285 error = kern_fcntl(td, uap->fd, F_GETLK, (intptr_t)&fl); 286 if (error) 287 return error; 288 cvt_flock2iflock(&fl, &ifl); 289 return copyout((caddr_t)&ifl, (caddr_t)uap->arg, 290 ibcs2_flock_len); 291 } 292 293 case IBCS2_F_SETLK: 294 { 295 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl, 296 ibcs2_flock_len); 297 if (error) 298 return error; 299 cvt_iflock2flock(&ifl, &fl); 300 return (kern_fcntl(td, uap->fd, F_SETLK, (intptr_t)&fl)); 301 } 302 303 case IBCS2_F_SETLKW: 304 { 305 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl, 306 ibcs2_flock_len); 307 if (error) 308 return error; 309 cvt_iflock2flock(&ifl, &fl); 310 return (kern_fcntl(td, uap->fd, F_SETLKW, (intptr_t)&fl)); 311 } 312 } 313 return ENOSYS; 314} 315