svr4_fcntl.c revision 49267
1/* 2 * Copyright (c) 1998 Mark Newton 3 * Copyright (c) 1994, 1997 Christos Zoulas. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Christos Zoulas. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 * $Id$ 32 */ 33#include <sys/param.h> 34#include <sys/systm.h> 35#include <sys/namei.h> 36#include <sys/proc.h> 37#include <sys/file.h> 38#include <sys/stat.h> 39#include <sys/filedesc.h> 40/*#include <sys/ioctl.h>*/ 41#include <sys/kernel.h> 42#include <sys/mount.h> 43#include <sys/malloc.h> 44#include <sys/vnode.h> 45#include <sys/unistd.h> 46 47#include <sys/sysproto.h> 48 49#include <svr4/svr4.h> 50#include <svr4/svr4_types.h> 51#include <svr4/svr4_signal.h> 52#include <svr4/svr4_proto.h> 53#include <svr4/svr4_util.h> 54#include <svr4/svr4_fcntl.h> 55 56static int svr4_to_bsd_flags __P((int)); 57static u_long svr4_to_bsd_cmd __P((u_long)); 58static int fd_revoke __P((struct proc *, int)); 59static int fd_truncate __P((struct proc *, int, struct flock *)); 60static int bsd_to_svr4_flags __P((int)); 61static void bsd_to_svr4_flock __P((struct flock *, struct svr4_flock *)); 62static void svr4_to_bsd_flock __P((struct svr4_flock *, struct flock *)); 63static void bsd_to_svr4_flock64 __P((struct flock *, struct svr4_flock64 *)); 64static void svr4_to_bsd_flock64 __P((struct svr4_flock64 *, struct flock *)); 65 66static u_long 67svr4_to_bsd_cmd(cmd) 68 u_long cmd; 69{ 70 switch (cmd) { 71 case SVR4_F_DUPFD: 72 return F_DUPFD; 73 case SVR4_F_GETFD: 74 return F_GETFD; 75 case SVR4_F_SETFD: 76 return F_SETFD; 77 case SVR4_F_GETFL: 78 return F_GETFL; 79 case SVR4_F_SETFL: 80 return F_SETFL; 81 case SVR4_F_GETLK: 82 return F_GETLK; 83 case SVR4_F_SETLK: 84 return F_SETLK; 85 case SVR4_F_SETLKW: 86 return F_SETLKW; 87 default: 88 return -1; 89 } 90} 91 92static int 93svr4_to_bsd_flags(l) 94 int l; 95{ 96 int r = 0; 97 r |= (l & SVR4_O_RDONLY) ? O_RDONLY : 0; 98 r |= (l & SVR4_O_WRONLY) ? O_WRONLY : 0; 99 r |= (l & SVR4_O_RDWR) ? O_RDWR : 0; 100 r |= (l & SVR4_O_NDELAY) ? O_NONBLOCK : 0; 101 r |= (l & SVR4_O_APPEND) ? O_APPEND : 0; 102 r |= (l & SVR4_O_SYNC) ? O_FSYNC : 0; 103 r |= (l & SVR4_O_NONBLOCK) ? O_NONBLOCK : 0; 104 r |= (l & SVR4_O_PRIV) ? O_EXLOCK : 0; 105 r |= (l & SVR4_O_CREAT) ? O_CREAT : 0; 106 r |= (l & SVR4_O_TRUNC) ? O_TRUNC : 0; 107 r |= (l & SVR4_O_EXCL) ? O_EXCL : 0; 108 r |= (l & SVR4_O_NOCTTY) ? O_NOCTTY : 0; 109 return r; 110} 111 112static int 113bsd_to_svr4_flags(l) 114 int l; 115{ 116 int r = 0; 117 r |= (l & O_RDONLY) ? SVR4_O_RDONLY : 0; 118 r |= (l & O_WRONLY) ? SVR4_O_WRONLY : 0; 119 r |= (l & O_RDWR) ? SVR4_O_RDWR : 0; 120 r |= (l & O_NDELAY) ? SVR4_O_NONBLOCK : 0; 121 r |= (l & O_APPEND) ? SVR4_O_APPEND : 0; 122 r |= (l & O_FSYNC) ? SVR4_O_SYNC : 0; 123 r |= (l & O_NONBLOCK) ? SVR4_O_NONBLOCK : 0; 124 r |= (l & O_EXLOCK) ? SVR4_O_PRIV : 0; 125 r |= (l & O_CREAT) ? SVR4_O_CREAT : 0; 126 r |= (l & O_TRUNC) ? SVR4_O_TRUNC : 0; 127 r |= (l & O_EXCL) ? SVR4_O_EXCL : 0; 128 r |= (l & O_NOCTTY) ? SVR4_O_NOCTTY : 0; 129 return r; 130} 131 132 133static void 134bsd_to_svr4_flock(iflp, oflp) 135 struct flock *iflp; 136 struct svr4_flock *oflp; 137{ 138 switch (iflp->l_type) { 139 case F_RDLCK: 140 oflp->l_type = SVR4_F_RDLCK; 141 break; 142 case F_WRLCK: 143 oflp->l_type = SVR4_F_WRLCK; 144 break; 145 case F_UNLCK: 146 oflp->l_type = SVR4_F_UNLCK; 147 break; 148 default: 149 oflp->l_type = -1; 150 break; 151 } 152 153 oflp->l_whence = (short) iflp->l_whence; 154 oflp->l_start = (svr4_off_t) iflp->l_start; 155 oflp->l_len = (svr4_off_t) iflp->l_len; 156 oflp->l_sysid = 0; 157 oflp->l_pid = (svr4_pid_t) iflp->l_pid; 158} 159 160 161static void 162svr4_to_bsd_flock(iflp, oflp) 163 struct svr4_flock *iflp; 164 struct flock *oflp; 165{ 166 switch (iflp->l_type) { 167 case SVR4_F_RDLCK: 168 oflp->l_type = F_RDLCK; 169 break; 170 case SVR4_F_WRLCK: 171 oflp->l_type = F_WRLCK; 172 break; 173 case SVR4_F_UNLCK: 174 oflp->l_type = F_UNLCK; 175 break; 176 default: 177 oflp->l_type = -1; 178 break; 179 } 180 181 oflp->l_whence = iflp->l_whence; 182 oflp->l_start = (off_t) iflp->l_start; 183 oflp->l_len = (off_t) iflp->l_len; 184 oflp->l_pid = (pid_t) iflp->l_pid; 185 186} 187 188static void 189bsd_to_svr4_flock64(iflp, oflp) 190 struct flock *iflp; 191 struct svr4_flock64 *oflp; 192{ 193 switch (iflp->l_type) { 194 case F_RDLCK: 195 oflp->l_type = SVR4_F_RDLCK; 196 break; 197 case F_WRLCK: 198 oflp->l_type = SVR4_F_WRLCK; 199 break; 200 case F_UNLCK: 201 oflp->l_type = SVR4_F_UNLCK; 202 break; 203 default: 204 oflp->l_type = -1; 205 break; 206 } 207 208 oflp->l_whence = (short) iflp->l_whence; 209 oflp->l_start = (svr4_off64_t) iflp->l_start; 210 oflp->l_len = (svr4_off64_t) iflp->l_len; 211 oflp->l_sysid = 0; 212 oflp->l_pid = (svr4_pid_t) iflp->l_pid; 213} 214 215 216static void 217svr4_to_bsd_flock64(iflp, oflp) 218 struct svr4_flock64 *iflp; 219 struct flock *oflp; 220{ 221 switch (iflp->l_type) { 222 case SVR4_F_RDLCK: 223 oflp->l_type = F_RDLCK; 224 break; 225 case SVR4_F_WRLCK: 226 oflp->l_type = F_WRLCK; 227 break; 228 case SVR4_F_UNLCK: 229 oflp->l_type = F_UNLCK; 230 break; 231 default: 232 oflp->l_type = -1; 233 break; 234 } 235 236 oflp->l_whence = iflp->l_whence; 237 oflp->l_start = (off_t) iflp->l_start; 238 oflp->l_len = (off_t) iflp->l_len; 239 oflp->l_pid = (pid_t) iflp->l_pid; 240 241} 242 243 244static int 245fd_revoke(p, fd) 246 struct proc *p; 247 int fd; 248{ 249 struct filedesc *fdp = p->p_fd; 250 struct file *fp; 251 struct vnode *vp; 252 struct vattr vattr; 253 int error, *retval; 254 255 retval = p->p_retval; 256 if ((u_int)fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL) 257 return EBADF; 258 259 switch (fp->f_type) { 260 case DTYPE_VNODE: 261 vp = (struct vnode *) fp->f_data; 262 263 case DTYPE_SOCKET: 264 return EINVAL; 265 266 default: 267 panic("svr4_fcntl(F_REVOKE)"); 268 /*NOTREACHED*/ 269 } 270 271 if (vp->v_type != VCHR && vp->v_type != VBLK) { 272 error = EINVAL; 273 goto out; 274 } 275 276 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0) 277 goto out; 278 279 if (p->p_ucred->cr_uid != vattr.va_uid && 280 (error = suser(p)) != 0) 281 goto out; 282 283 if (vp->v_usecount > 1 || (vp->v_flag & VALIASED)) 284 VOP_REVOKE(vp, REVOKEALL); 285out: 286 vrele(vp); 287 return error; 288} 289 290 291static int 292fd_truncate(p, fd, flp) 293 struct proc *p; 294 int fd; 295 struct flock *flp; 296{ 297 struct filedesc *fdp = p->p_fd; 298 struct file *fp; 299 off_t start, length; 300 struct vnode *vp; 301 struct vattr vattr; 302 int error, *retval; 303 struct ftruncate_args ft; 304 305 retval = p->p_retval; 306 307 /* 308 * We only support truncating the file. 309 */ 310 if ((u_int)fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL) 311 return EBADF; 312 313 vp = (struct vnode *)fp->f_data; 314 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) 315 return ESPIPE; 316 317 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0) 318 return error; 319 320 length = vattr.va_size; 321 322 switch (flp->l_whence) { 323 case SEEK_CUR: 324 start = fp->f_offset + flp->l_start; 325 break; 326 327 case SEEK_END: 328 start = flp->l_start + length; 329 break; 330 331 case SEEK_SET: 332 start = flp->l_start; 333 break; 334 335 default: 336 return EINVAL; 337 } 338 339 if (start + flp->l_len < length) { 340 /* We don't support free'ing in the middle of the file */ 341 return EINVAL; 342 } 343 344 SCARG(&ft, fd) = fd; 345 SCARG(&ft, length) = start; 346 347 return ftruncate(p, &ft); 348} 349 350int 351svr4_sys_open(p, uap) 352 register struct proc *p; 353 struct svr4_sys_open_args *uap; 354{ 355 int error, retval; 356 struct open_args cup; 357 358 caddr_t sg = stackgap_init(); 359 CHECKALTEXIST(p, &sg, SCARG(uap, path)); 360 361 (&cup)->path = uap->path; 362 (&cup)->flags = svr4_to_bsd_flags(uap->flags); 363 (&cup)->mode = uap->mode; 364 error = open(p, &cup); 365 366 if (error) { 367 /* uprintf("svr4_open(%s, 0x%0x, 0%o): %d\n", uap->path, 368 uap->flags, uap->mode, error);*/ 369 return error; 370 } 371 372 retval = p->p_retval[0]; 373 374 if (!(SCARG(&cup, flags) & O_NOCTTY) && SESS_LEADER(p) && 375 !(p->p_flag & P_CONTROLT)) { 376#if defined(NOTYET) 377 struct filedesc *fdp = p->p_fd; 378 struct file *fp = fdp->fd_ofiles[retval]; 379 380 /* ignore any error, just give it a try */ 381 if (fp->f_type == DTYPE_VNODE) 382 (fp->f_ops->fo_ioctl) (fp, TIOCSCTTY, (caddr_t) 0, p); 383#endif 384 } 385 return error; 386} 387 388int 389svr4_sys_open64(p, uap) 390 register struct proc *p; 391 struct svr4_sys_open64_args *uap; 392{ 393 return svr4_sys_open(p, (struct svr4_sys_open_args *)uap); 394} 395 396int 397svr4_sys_creat(p, uap) 398 register struct proc *p; 399 struct svr4_sys_creat_args *uap; 400{ 401 struct open_args cup; 402 403 caddr_t sg = stackgap_init(); 404 CHECKALTEXIST(p, &sg, SCARG(uap, path)); 405 406 SCARG(&cup, path) = SCARG(uap, path); 407 SCARG(&cup, mode) = SCARG(uap, mode); 408 SCARG(&cup, flags) = O_WRONLY | O_CREAT | O_TRUNC; 409 410 return open(p, &cup); 411} 412 413int 414svr4_sys_creat64(p, uap) 415 register struct proc *p; 416 struct svr4_sys_creat64_args *uap; 417{ 418 return svr4_sys_creat(p, (struct svr4_sys_creat_args *)uap); 419} 420 421int 422svr4_sys_llseek(p, v) 423 register struct proc *p; 424 struct svr4_sys_llseek_args *v; 425{ 426 struct svr4_sys_llseek_args *uap = v; 427 struct lseek_args ap; 428 429 SCARG(&ap, fd) = SCARG(uap, fd); 430 431#if BYTE_ORDER == BIG_ENDIAN 432 SCARG(&ap, offset) = (((long long) SCARG(uap, offset1)) << 32) | 433 SCARG(uap, offset2); 434#else 435 SCARG(&ap, offset) = (((long long) SCARG(uap, offset2)) << 32) | 436 SCARG(uap, offset1); 437#endif 438 SCARG(&ap, whence) = SCARG(uap, whence); 439 440 return lseek(p, &ap); 441} 442 443int 444svr4_sys_access(p, uap) 445 register struct proc *p; 446 struct svr4_sys_access_args *uap; 447{ 448 struct access_args cup; 449 int *retval; 450 451 caddr_t sg = stackgap_init(); 452 CHECKALTEXIST(p, &sg, SCARG(uap, path)); 453 454 retval = p->p_retval; 455 456 SCARG(&cup, path) = SCARG(uap, path); 457 SCARG(&cup, flags) = SCARG(uap, flags); 458 459 return access(p, &cup); 460} 461 462#if defined(NOTYET) 463int 464svr4_sys_pread(p, uap) 465 register struct proc *p; 466 struct svr4_sys_pread_args *uap; 467{ 468 struct pread_args pra; 469 470 /* 471 * Just translate the args structure and call the NetBSD 472 * pread(2) system call (offset type is 64-bit in NetBSD). 473 */ 474 SCARG(&pra, fd) = SCARG(uap, fd); 475 SCARG(&pra, buf) = SCARG(uap, buf); 476 SCARG(&pra, nbyte) = SCARG(uap, nbyte); 477 SCARG(&pra, offset) = SCARG(uap, off); 478 479 return pread(p, &pra); 480} 481#endif 482 483#if defined(NOTYET) 484int 485svr4_sys_pread64(p, v, retval) 486 register struct proc *p; 487 void *v; 488 register_t *retval; 489{ 490 491 struct svr4_sys_pread64_args *uap = v; 492 struct sys_pread_args pra; 493 494 /* 495 * Just translate the args structure and call the NetBSD 496 * pread(2) system call (offset type is 64-bit in NetBSD). 497 */ 498 SCARG(&pra, fd) = SCARG(uap, fd); 499 SCARG(&pra, buf) = SCARG(uap, buf); 500 SCARG(&pra, nbyte) = SCARG(uap, nbyte); 501 SCARG(&pra, offset) = SCARG(uap, off); 502 503 return (sys_pread(p, &pra, retval)); 504} 505#endif /* NOTYET */ 506 507#if defined(NOTYET) 508int 509svr4_sys_pwrite(p, uap) 510 register struct proc *p; 511 struct svr4_sys_pwrite_args *uap; 512{ 513 struct pwrite_args pwa; 514 515 /* 516 * Just translate the args structure and call the NetBSD 517 * pwrite(2) system call (offset type is 64-bit in NetBSD). 518 */ 519 SCARG(&pwa, fd) = SCARG(uap, fd); 520 SCARG(&pwa, buf) = SCARG(uap, buf); 521 SCARG(&pwa, nbyte) = SCARG(uap, nbyte); 522 SCARG(&pwa, offset) = SCARG(uap, off); 523 524 return pwrite(p, &pwa); 525} 526#endif 527 528#if defined(NOTYET) 529int 530svr4_sys_pwrite64(p, v, retval) 531 register struct proc *p; 532 void *v; 533 register_t *retval; 534{ 535 struct svr4_sys_pwrite64_args *uap = v; 536 struct sys_pwrite_args pwa; 537 538 /* 539 * Just translate the args structure and call the NetBSD 540 * pwrite(2) system call (offset type is 64-bit in NetBSD). 541 */ 542 SCARG(&pwa, fd) = SCARG(uap, fd); 543 SCARG(&pwa, buf) = SCARG(uap, buf); 544 SCARG(&pwa, nbyte) = SCARG(uap, nbyte); 545 SCARG(&pwa, offset) = SCARG(uap, off); 546 547 return (sys_pwrite(p, &pwa, retval)); 548} 549#endif /* NOTYET */ 550 551int 552svr4_sys_fcntl(p, uap) 553 register struct proc *p; 554 struct svr4_sys_fcntl_args *uap; 555{ 556 int error; 557 struct fcntl_args fa; 558 int *retval; 559 560 retval = p->p_retval; 561 562 SCARG(&fa, fd) = SCARG(uap, fd); 563 SCARG(&fa, cmd) = svr4_to_bsd_cmd(SCARG(uap, cmd)); 564 565 switch (SCARG(&fa, cmd)) { 566 case F_DUPFD: 567 case F_GETFD: 568 case F_SETFD: 569 SCARG(&fa, arg) = (long) SCARG(uap, arg); 570 return fcntl(p, &fa); 571 572 case F_GETFL: 573 SCARG(&fa, arg) = (long) SCARG(uap, arg); 574 error = fcntl(p, &fa); 575 if (error) 576 return error; 577 *retval = bsd_to_svr4_flags(*retval); 578 return error; 579 580 case F_SETFL: 581 { 582 /* 583 * we must save the O_ASYNC flag, as that is 584 * handled by ioctl(_, I_SETSIG, _) emulation. 585 */ 586 long cmd; 587 int flags; 588 589 DPRINTF(("Setting flags 0x%x\n", SCARG(uap, arg))); 590 cmd = SCARG(&fa, cmd); /* save it for a while */ 591 592 SCARG(&fa, cmd) = F_GETFL; 593 if ((error = fcntl(p, &fa)) != 0) 594 return error; 595 flags = *retval; 596 flags &= O_ASYNC; 597 flags |= svr4_to_bsd_flags((u_long) SCARG(uap, arg)); 598 SCARG(&fa, cmd) = cmd; 599 SCARG(&fa, arg) = (long) flags; 600 return fcntl(p, &fa); 601 } 602 603 case F_GETLK: 604 case F_SETLK: 605 case F_SETLKW: 606 { 607 struct svr4_flock ifl; 608 struct flock *flp, fl; 609 caddr_t sg = stackgap_init(); 610 611 flp = stackgap_alloc(&sg, sizeof(struct flock)); 612 SCARG(&fa, arg) = (long) flp; 613 614 error = copyin(SCARG(uap, arg), &ifl, sizeof ifl); 615 if (error) 616 return error; 617 618 svr4_to_bsd_flock(&ifl, &fl); 619 620 error = copyout(&fl, flp, sizeof fl); 621 if (error) 622 return error; 623 624 error = fcntl(p, &fa); 625 if (error || SCARG(&fa, cmd) != F_GETLK) 626 return error; 627 628 error = copyin(flp, &fl, sizeof fl); 629 if (error) 630 return error; 631 632 bsd_to_svr4_flock(&fl, &ifl); 633 634 return copyout(&ifl, SCARG(uap, arg), sizeof ifl); 635 } 636 case -1: 637 switch (SCARG(uap, cmd)) { 638 case SVR4_F_DUP2FD: 639 { 640 struct dup2_args du; 641 642 SCARG(&du, from) = SCARG(uap, fd); 643 SCARG(&du, to) = (int)SCARG(uap, arg); 644 error = dup2(p, &du); 645 if (error) 646 return error; 647 *retval = SCARG(&du, to); 648 return 0; 649 } 650 651 case SVR4_F_FREESP: 652 { 653 struct svr4_flock ifl; 654 struct flock fl; 655 656 error = copyin(SCARG(uap, arg), &ifl, 657 sizeof ifl); 658 if (error) 659 return error; 660 svr4_to_bsd_flock(&ifl, &fl); 661 return fd_truncate(p, SCARG(uap, fd), &fl); 662 } 663 664 case SVR4_F_GETLK64: 665 case SVR4_F_SETLK64: 666 case SVR4_F_SETLKW64: 667 { 668 struct svr4_flock64 ifl; 669 struct flock *flp, fl; 670 caddr_t sg = stackgap_init(); 671 672 flp = stackgap_alloc(&sg, sizeof(struct flock)); 673 SCARG(&fa, arg) = (long) flp; 674 675 error = copyin(SCARG(uap, arg), &ifl, 676 sizeof ifl); 677 if (error) 678 return error; 679 680 svr4_to_bsd_flock64(&ifl, &fl); 681 682 error = copyout(&fl, flp, sizeof fl); 683 if (error) 684 return error; 685 686 error = fcntl(p, &fa); 687 if (error || SCARG(&fa, cmd) != F_GETLK) 688 return error; 689 690 error = copyin(flp, &fl, sizeof fl); 691 if (error) 692 return error; 693 694 bsd_to_svr4_flock64(&fl, &ifl); 695 696 return copyout(&ifl, SCARG(uap, arg), 697 sizeof ifl); 698 } 699 700 case SVR4_F_FREESP64: 701 { 702 struct svr4_flock64 ifl; 703 struct flock fl; 704 705 error = copyin(SCARG(uap, arg), &ifl, 706 sizeof ifl); 707 if (error) 708 return error; 709 svr4_to_bsd_flock64(&ifl, &fl); 710 return fd_truncate(p, SCARG(uap, fd), &fl); 711 } 712 713 case SVR4_F_REVOKE: 714 return fd_revoke(p, SCARG(uap, fd)); 715 716 default: 717 return ENOSYS; 718 } 719 720 default: 721 return ENOSYS; 722 } 723} 724