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.
|
28 */ 29 30#include <sys/param.h> 31#include <sys/systm.h> 32#include <sys/namei.h> 33#include <sys/proc.h> 34#include <sys/file.h> 35#include <sys/stat.h> 36#include <sys/filedesc.h> 37#include <sys/ioctl.h> 38#include <sys/kernel.h> 39#include <sys/mount.h> 40#include <sys/malloc.h> 41#include <sys/sysproto.h> 42 43#include <i386/ibcs2/ibcs2_types.h> 44#include <i386/ibcs2/ibcs2_fcntl.h> 45#include <i386/ibcs2/ibcs2_signal.h> 46#include <i386/ibcs2/ibcs2_proto.h> 47#include <i386/ibcs2/ibcs2_util.h> 48 49static void cvt_iflock2flock __P((struct ibcs2_flock *, struct flock *)); 50static void cvt_flock2iflock __P((struct flock *, struct ibcs2_flock *)); 51static int cvt_o_flags __P((int)); 52static int oflags2ioflags __P((int)); 53static int ioflags2oflags __P((int)); 54 55static int 56cvt_o_flags(flags) 57 int flags; 58{ 59 int r = 0; 60 61 /* convert mode into NetBSD mode */ 62 if (flags & IBCS2_O_WRONLY) r |= O_WRONLY; 63 if (flags & IBCS2_O_RDWR) r |= O_RDWR; 64 if (flags & (IBCS2_O_NDELAY | IBCS2_O_NONBLOCK)) r |= O_NONBLOCK; 65 if (flags & IBCS2_O_APPEND) r |= O_APPEND; 66 if (flags & IBCS2_O_SYNC) r |= O_FSYNC; 67 if (flags & IBCS2_O_CREAT) r |= O_CREAT; 68 if (flags & IBCS2_O_TRUNC) r |= O_TRUNC /* | O_CREAT ??? */; 69 if (flags & IBCS2_O_EXCL) r |= O_EXCL; 70 if (flags & IBCS2_O_RDONLY) r |= O_RDONLY; 71 if (flags & IBCS2_O_PRIV) r |= O_EXLOCK; 72 if (flags & IBCS2_O_NOCTTY) r |= O_NOCTTY; 73 return r; 74} 75 76static void 77cvt_flock2iflock(flp, iflp) 78 struct flock *flp; 79 struct ibcs2_flock *iflp; 80{ 81 switch (flp->l_type) { 82 case F_RDLCK: 83 iflp->l_type = IBCS2_F_RDLCK; 84 break; 85 case F_WRLCK: 86 iflp->l_type = IBCS2_F_WRLCK; 87 break; 88 case F_UNLCK: 89 iflp->l_type = IBCS2_F_UNLCK; 90 break; 91 } 92 iflp->l_whence = (short)flp->l_whence; 93 iflp->l_start = (ibcs2_off_t)flp->l_start; 94 iflp->l_len = (ibcs2_off_t)flp->l_len; 95 iflp->l_sysid = 0; 96 iflp->l_pid = (ibcs2_pid_t)flp->l_pid; 97} 98 99#ifdef DEBUG_IBCS2 100static void 101print_flock(struct flock *flp) 102{ 103 printf("flock: start=%x len=%x pid=%d type=%d whence=%d\n", 104 (int)flp->l_start, (int)flp->l_len, (int)flp->l_pid, 105 flp->l_type, flp->l_whence); 106} 107#endif 108 109static void 110cvt_iflock2flock(iflp, flp) 111 struct ibcs2_flock *iflp; 112 struct flock *flp; 113{ 114 flp->l_start = (off_t)iflp->l_start; 115 flp->l_len = (off_t)iflp->l_len; 116 flp->l_pid = (pid_t)iflp->l_pid; 117 switch (iflp->l_type) { 118 case IBCS2_F_RDLCK: 119 flp->l_type = F_RDLCK; 120 break; 121 case IBCS2_F_WRLCK: 122 flp->l_type = F_WRLCK; 123 break; 124 case IBCS2_F_UNLCK: 125 flp->l_type = F_UNLCK; 126 break; 127 } 128 flp->l_whence = iflp->l_whence; 129} 130 131/* convert iBCS2 mode into NetBSD mode */ 132static int 133ioflags2oflags(flags) 134 int flags; 135{ 136 int r = 0; 137 138 if (flags & IBCS2_O_RDONLY) r |= O_RDONLY; 139 if (flags & IBCS2_O_WRONLY) r |= O_WRONLY; 140 if (flags & IBCS2_O_RDWR) r |= O_RDWR; 141 if (flags & IBCS2_O_NDELAY) r |= O_NONBLOCK; 142 if (flags & IBCS2_O_APPEND) r |= O_APPEND; 143 if (flags & IBCS2_O_SYNC) r |= O_FSYNC; 144 if (flags & IBCS2_O_NONBLOCK) r |= O_NONBLOCK; 145 if (flags & IBCS2_O_CREAT) r |= O_CREAT; 146 if (flags & IBCS2_O_TRUNC) r |= O_TRUNC; 147 if (flags & IBCS2_O_EXCL) r |= O_EXCL; 148 if (flags & IBCS2_O_NOCTTY) r |= O_NOCTTY; 149 return r; 150} 151 152/* convert NetBSD mode into iBCS2 mode */ 153static int 154oflags2ioflags(flags) 155 int flags; 156{ 157 int r = 0; 158 159 if (flags & O_RDONLY) r |= IBCS2_O_RDONLY; 160 if (flags & O_WRONLY) r |= IBCS2_O_WRONLY; 161 if (flags & O_RDWR) r |= IBCS2_O_RDWR; 162 if (flags & O_NDELAY) r |= IBCS2_O_NONBLOCK; 163 if (flags & O_APPEND) r |= IBCS2_O_APPEND; 164 if (flags & O_FSYNC) r |= IBCS2_O_SYNC; 165 if (flags & O_NONBLOCK) r |= IBCS2_O_NONBLOCK; 166 if (flags & O_CREAT) r |= IBCS2_O_CREAT; 167 if (flags & O_TRUNC) r |= IBCS2_O_TRUNC; 168 if (flags & O_EXCL) r |= IBCS2_O_EXCL; 169 if (flags & O_NOCTTY) r |= IBCS2_O_NOCTTY; 170 return r; 171} 172 173int 174ibcs2_open(p, uap, retval) 175 struct proc *p; 176 struct ibcs2_open_args *uap; 177 int *retval; 178{ 179 int noctty = SCARG(uap, flags) & IBCS2_O_NOCTTY; 180 int ret; 181 caddr_t sg = stackgap_init(); 182 183 SCARG(uap, flags) = cvt_o_flags(SCARG(uap, flags)); 184 if (SCARG(uap, flags) & O_CREAT) 185 CHECKALTCREAT(p, &sg, SCARG(uap, path)); 186 else 187 CHECKALTEXIST(p, &sg, SCARG(uap, path)); 188 ret = open(p, (struct open_args *)uap, retval); 189
|
196 if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) { 197 struct filedesc *fdp = p->p_fd; 198 struct file *fp = fdp->fd_ofiles[*retval]; 199 200 /* ignore any error, just give it a try */ 201 if (fp->f_type == DTYPE_VNODE) 202 (fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t) 0, p); 203 } 204 return ret; 205} 206 207int 208ibcs2_creat(p, uap, retval) 209 struct proc *p; 210 struct ibcs2_creat_args *uap; 211 int *retval; 212{ 213 struct open_args cup; 214 caddr_t sg = stackgap_init(); 215 216 CHECKALTCREAT(p, &sg, SCARG(uap, path)); 217 SCARG(&cup, path) = SCARG(uap, path); 218 SCARG(&cup, mode) = SCARG(uap, mode); 219 SCARG(&cup, flags) = O_WRONLY | O_CREAT | O_TRUNC; 220 return open(p, &cup, retval); 221} 222 223int 224ibcs2_access(p, uap, retval) 225 struct proc *p; 226 struct ibcs2_access_args *uap; 227 int *retval; 228{ 229 struct access_args cup; 230 caddr_t sg = stackgap_init(); 231 232 CHECKALTEXIST(p, &sg, SCARG(uap, path)); 233 SCARG(&cup, path) = SCARG(uap, path); 234 SCARG(&cup, flags) = SCARG(uap, flags); 235 return access(p, &cup, retval); 236} 237 238int 239ibcs2_fcntl(p, uap, retval) 240 struct proc *p; 241 struct ibcs2_fcntl_args *uap; 242 int *retval; 243{ 244 int error; 245 struct fcntl_args fa; 246 struct flock *flp; 247 struct ibcs2_flock ifl; 248 249 switch(SCARG(uap, cmd)) { 250 case IBCS2_F_DUPFD: 251 SCARG(&fa, fd) = SCARG(uap, fd); 252 SCARG(&fa, cmd) = F_DUPFD; 253 SCARG(&fa, arg) = SCARG(uap, arg); 254 return fcntl(p, &fa, retval); 255 case IBCS2_F_GETFD: 256 SCARG(&fa, fd) = SCARG(uap, fd); 257 SCARG(&fa, cmd) = F_GETFD; 258 SCARG(&fa, arg) = SCARG(uap, arg); 259 return fcntl(p, &fa, retval); 260 case IBCS2_F_SETFD: 261 SCARG(&fa, fd) = SCARG(uap, fd); 262 SCARG(&fa, cmd) = F_SETFD; 263 SCARG(&fa, arg) = SCARG(uap, arg); 264 return fcntl(p, &fa, retval); 265 case IBCS2_F_GETFL: 266 SCARG(&fa, fd) = SCARG(uap, fd); 267 SCARG(&fa, cmd) = F_GETFL; 268 SCARG(&fa, arg) = SCARG(uap, arg); 269 error = fcntl(p, &fa, retval); 270 if (error) 271 return error; 272 *retval = oflags2ioflags(*retval); 273 return error; 274 case IBCS2_F_SETFL: 275 SCARG(&fa, fd) = SCARG(uap, fd); 276 SCARG(&fa, cmd) = F_SETFL; 277 SCARG(&fa, arg) = (void *)ioflags2oflags((int)SCARG(uap, arg)); 278 return fcntl(p, &fa, retval); 279 280 case IBCS2_F_GETLK: 281 { 282 caddr_t sg = stackgap_init(); 283 flp = stackgap_alloc(&sg, sizeof(*flp)); 284 error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&ifl, 285 ibcs2_flock_len); 286 if (error) 287 return error; 288 cvt_iflock2flock(&ifl, flp); 289 SCARG(&fa, fd) = SCARG(uap, fd); 290 SCARG(&fa, cmd) = F_GETLK; 291 SCARG(&fa, arg) = (void *)flp; 292 error = fcntl(p, &fa, retval); 293 if (error) 294 return error; 295 cvt_flock2iflock(flp, &ifl); 296 return copyout((caddr_t)&ifl, (caddr_t)SCARG(uap, arg), 297 ibcs2_flock_len); 298 } 299 300 case IBCS2_F_SETLK: 301 { 302 caddr_t sg = stackgap_init(); 303 flp = stackgap_alloc(&sg, sizeof(*flp)); 304 error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&ifl, 305 ibcs2_flock_len); 306 if (error) 307 return error; 308 cvt_iflock2flock(&ifl, flp); 309 SCARG(&fa, fd) = SCARG(uap, fd); 310 SCARG(&fa, cmd) = F_SETLK; 311 SCARG(&fa, arg) = (void *)flp; 312 313 return fcntl(p, &fa, retval); 314 } 315 316 case IBCS2_F_SETLKW: 317 { 318 caddr_t sg = stackgap_init(); 319 flp = stackgap_alloc(&sg, sizeof(*flp)); 320 error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&ifl, 321 ibcs2_flock_len); 322 if (error) 323 return error; 324 cvt_iflock2flock(&ifl, flp); 325 SCARG(&fa, fd) = SCARG(uap, fd); 326 SCARG(&fa, cmd) = F_SETLKW; 327 SCARG(&fa, arg) = (void *)flp; 328 return fcntl(p, &fa, retval); 329 } 330 } 331 return ENOSYS; 332}
|