1 /*- 2 * Copyright (c) 1994-1995 Søren Schmidt 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 * in this position and unchanged. 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. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_compat.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/capability.h> 37 #include <sys/conf.h> 38 #include <sys/dirent.h> 39 #include <sys/fcntl.h> 40 #include <sys/file.h> 41 #include <sys/filedesc.h> 42 #include <sys/lock.h> 43 #include <sys/malloc.h> 44 #include <sys/mount.h> 45 #include <sys/mutex.h> 46 #include <sys/namei.h> 47 #include <sys/proc.h> 48 #include <sys/stat.h> 49 #include <sys/sx.h> 50 #include <sys/syscallsubr.h> 51 #include <sys/sysproto.h> 52 #include <sys/tty.h> 53 #include <sys/unistd.h> 54 #include <sys/vnode.h> 55 56 #include <security/mac/mac_framework.h> 57 58 #include <ufs/ufs/extattr.h> 59 #include <ufs/ufs/quota.h> 60 #include <ufs/ufs/ufsmount.h> 61 62 #ifdef COMPAT_LINUX32 63 #include <machine/../linux32/linux.h> 64 #include <machine/../linux32/linux32_proto.h> 65 #else 66 #include <machine/../linux/linux.h> 67 #include <machine/../linux/linux_proto.h> 68 #endif 69 #include <compat/linux/linux_util.h> 70 #include <compat/linux/linux_file.h> 71 72 /* XXX */ 73 int do_pipe(struct thread *td, int fildes[2], int flags); 74 75 int 76 linux_creat(struct thread *td, struct linux_creat_args *args) 77 { 78 char *path; 79 int error; 80 81 LCONVPATHEXIST(td, args->path, &path); 82 83 #ifdef DEBUG 84 if (ldebug(creat)) 85 printf(ARGS(creat, "%s, %d"), path, args->mode); 86 #endif 87 error = kern_open(td, path, UIO_SYSSPACE, O_WRONLY | O_CREAT | O_TRUNC, 88 args->mode); 89 LFREEPATH(path); 90 return (error); 91 } 92 93 94 static int 95 linux_common_open(struct thread *td, int dirfd, char *path, int l_flags, int mode) 96 { 97 struct proc *p = td->td_proc; 98 struct file *fp; 99 int fd; 100 int bsd_flags, error; 101 102 bsd_flags = 0; 103 switch (l_flags & LINUX_O_ACCMODE) { 104 case LINUX_O_WRONLY: 105 bsd_flags |= O_WRONLY; 106 break; 107 case LINUX_O_RDWR: 108 bsd_flags |= O_RDWR; 109 break; 110 default: 111 bsd_flags |= O_RDONLY; 112 } 113 if (l_flags & LINUX_O_NDELAY) 114 bsd_flags |= O_NONBLOCK; 115 if (l_flags & LINUX_O_APPEND) 116 bsd_flags |= O_APPEND; 117 if (l_flags & LINUX_O_SYNC) 118 bsd_flags |= O_FSYNC; 119 if (l_flags & LINUX_O_NONBLOCK) 120 bsd_flags |= O_NONBLOCK; 121 if (l_flags & LINUX_FASYNC) 122 bsd_flags |= O_ASYNC; 123 if (l_flags & LINUX_O_CREAT) 124 bsd_flags |= O_CREAT; 125 if (l_flags & LINUX_O_TRUNC) 126 bsd_flags |= O_TRUNC; 127 if (l_flags & LINUX_O_EXCL) 128 bsd_flags |= O_EXCL; 129 if (l_flags & LINUX_O_NOCTTY) 130 bsd_flags |= O_NOCTTY; 131 if (l_flags & LINUX_O_DIRECT) 132 bsd_flags |= O_DIRECT; 133 if (l_flags & LINUX_O_NOFOLLOW) 134 bsd_flags |= O_NOFOLLOW; 135 if (l_flags & LINUX_O_DIRECTORY) 136 bsd_flags |= O_DIRECTORY; 137 /* XXX LINUX_O_NOATIME: unable to be easily implemented. */ 138 139 error = kern_openat(td, dirfd, path, UIO_SYSSPACE, bsd_flags, mode); 140 141 if (!error) { 142 fd = td->td_retval[0]; 143 /* 144 * XXX In between kern_open() and fget(), another process 145 * having the same filedesc could use that fd without 146 * checking below. 147 */ 148 error = fget(td, fd, CAP_IOCTL, &fp); 149 if (!error) { 150 sx_slock(&proctree_lock); 151 PROC_LOCK(p); 152 if (!(bsd_flags & O_NOCTTY) && 153 SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) { 154 PROC_UNLOCK(p); 155 sx_unlock(&proctree_lock); 156 if (fp->f_type == DTYPE_VNODE) 157 (void) fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, 158 td->td_ucred, td); 159 } else { 160 PROC_UNLOCK(p); 161 sx_sunlock(&proctree_lock); 162 } 163 fdrop(fp, td); 164 /* 165 * XXX as above, fdrop()/kern_close() pair is racy. 166 */ 167 if (error) 168 kern_close(td, fd); 169 } 170 } 171 172 #ifdef DEBUG 173 if (ldebug(open)) 174 printf(LMSG("open returns error %d"), error); 175 #endif 176 LFREEPATH(path); 177 return (error); 178 } 179 180 int 181 linux_openat(struct thread *td, struct linux_openat_args *args) 182 { 183 char *path; 184 int dfd; 185 186 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 187 if (args->flags & LINUX_O_CREAT) 188 LCONVPATH_AT(td, args->filename, &path, 1, dfd); 189 else 190 LCONVPATH_AT(td, args->filename, &path, 0, dfd); 191 #ifdef DEBUG 192 if (ldebug(openat)) 193 printf(ARGS(openat, "%i, %s, 0x%x, 0x%x"), args->dfd, 194 path, args->flags, args->mode); 195 #endif 196 return (linux_common_open(td, dfd, path, args->flags, args->mode)); 197 } 198 199 int 200 linux_open(struct thread *td, struct linux_open_args *args) 201 { 202 char *path; 203 204 if (args->flags & LINUX_O_CREAT) 205 LCONVPATHCREAT(td, args->path, &path); 206 else 207 LCONVPATHEXIST(td, args->path, &path); 208 209 #ifdef DEBUG 210 if (ldebug(open)) 211 printf(ARGS(open, "%s, 0x%x, 0x%x"), 212 path, args->flags, args->mode); 213 #endif 214 215 return (linux_common_open(td, AT_FDCWD, path, args->flags, args->mode)); 216 } 217 218 int 219 linux_lseek(struct thread *td, struct linux_lseek_args *args) 220 { 221 222 struct lseek_args /* { 223 int fd; 224 int pad; 225 off_t offset; 226 int whence; 227 } */ tmp_args; 228 int error; 229 230 #ifdef DEBUG 231 if (ldebug(lseek)) 232 printf(ARGS(lseek, "%d, %ld, %d"), 233 args->fdes, (long)args->off, args->whence); 234 #endif 235 tmp_args.fd = args->fdes; 236 tmp_args.offset = (off_t)args->off; 237 tmp_args.whence = args->whence; 238 error = sys_lseek(td, &tmp_args); 239 return error; 240 } 241 242 int 243 linux_llseek(struct thread *td, struct linux_llseek_args *args) 244 { 245 struct lseek_args bsd_args; 246 int error; 247 off_t off; 248 249 #ifdef DEBUG 250 if (ldebug(llseek)) 251 printf(ARGS(llseek, "%d, %d:%d, %d"), 252 args->fd, args->ohigh, args->olow, args->whence); 253 #endif 254 off = (args->olow) | (((off_t) args->ohigh) << 32); 255 256 bsd_args.fd = args->fd; 257 bsd_args.offset = off; 258 bsd_args.whence = args->whence; 259 260 if ((error = sys_lseek(td, &bsd_args))) 261 return error; 262 263 if ((error = copyout(td->td_retval, args->res, sizeof (off_t)))) 264 return error; 265 266 td->td_retval[0] = 0; 267 return 0; 268 } 269 270 int 271 linux_readdir(struct thread *td, struct linux_readdir_args *args) 272 { 273 struct linux_getdents_args lda; 274 275 lda.fd = args->fd; 276 lda.dent = args->dent; 277 lda.count = 1; 278 return linux_getdents(td, &lda); 279 } 280 281 /* 282 * Note that linux_getdents(2) and linux_getdents64(2) have the same 283 * arguments. They only differ in the definition of struct dirent they 284 * operate on. We use this to common the code, with the exception of 285 * accessing struct dirent. Note that linux_readdir(2) is implemented 286 * by means of linux_getdents(2). In this case we never operate on 287 * struct dirent64 and thus don't need to handle it... 288 */ 289 290 struct l_dirent { 291 l_ulong d_ino; 292 l_off_t d_off; 293 l_ushort d_reclen; 294 char d_name[LINUX_NAME_MAX + 1]; 295 }; 296 297 struct l_dirent64 { 298 uint64_t d_ino; 299 int64_t d_off; 300 l_ushort d_reclen; 301 u_char d_type; 302 char d_name[LINUX_NAME_MAX + 1]; 303 }; 304 305 /* 306 * Linux uses the last byte in the dirent buffer to store d_type, 307 * at least glibc-2.7 requires it. That is why l_dirent is padded with 2 bytes. 308 */ 309 #define LINUX_RECLEN(namlen) \ 310 roundup((offsetof(struct l_dirent, d_name) + (namlen) + 2), \ 311 sizeof(l_ulong)) 312 313 #define LINUX_RECLEN64(namlen) \ 314 roundup((offsetof(struct l_dirent64, d_name) + (namlen) + 1), \ 315 sizeof(uint64_t)) 316 317 #define LINUX_MAXRECLEN max(LINUX_RECLEN(LINUX_NAME_MAX), \ 318 LINUX_RECLEN64(LINUX_NAME_MAX)) 319 #define LINUX_DIRBLKSIZ 512 320 321 static int 322 getdents_common(struct thread *td, struct linux_getdents64_args *args, 323 int is64bit) 324 { 325 struct dirent *bdp; 326 struct vnode *vp; 327 caddr_t inp, buf; /* BSD-format */ 328 int len, reclen; /* BSD-format */ 329 caddr_t outp; /* Linux-format */ 330 int resid, linuxreclen=0; /* Linux-format */ 331 caddr_t lbuf; /* Linux-format */ 332 struct file *fp; 333 struct uio auio; 334 struct iovec aiov; 335 off_t off; 336 struct l_dirent *linux_dirent; 337 struct l_dirent64 *linux_dirent64; 338 int buflen, error, eofflag, nbytes, justone; 339 u_long *cookies = NULL, *cookiep; 340 int ncookies; 341 342 nbytes = args->count; 343 if (nbytes == 1) { 344 /* readdir(2) case. Always struct dirent. */ 345 if (is64bit) 346 return (EINVAL); 347 nbytes = sizeof(*linux_dirent); 348 justone = 1; 349 } else 350 justone = 0; 351 352 if ((error = getvnode(td->td_proc->p_fd, args->fd, CAP_READ, &fp)) != 0) 353 return (error); 354 355 if ((fp->f_flag & FREAD) == 0) { 356 fdrop(fp, td); 357 return (EBADF); 358 } 359 360 off = foffset_lock(fp, 0); 361 vp = fp->f_vnode; 362 if (vp->v_type != VDIR) { 363 foffset_unlock(fp, off, 0); 364 fdrop(fp, td); 365 return (EINVAL); 366 } 367 368 369 buflen = max(LINUX_DIRBLKSIZ, nbytes); 370 buflen = min(buflen, MAXBSIZE); 371 buf = malloc(buflen, M_TEMP, M_WAITOK); 372 lbuf = malloc(LINUX_MAXRECLEN, M_TEMP, M_WAITOK | M_ZERO); 373 vn_lock(vp, LK_SHARED | LK_RETRY); 374 375 aiov.iov_base = buf; 376 aiov.iov_len = buflen; 377 auio.uio_iov = &aiov; 378 auio.uio_iovcnt = 1; 379 auio.uio_rw = UIO_READ; 380 auio.uio_segflg = UIO_SYSSPACE; 381 auio.uio_td = td; 382 auio.uio_resid = buflen; 383 auio.uio_offset = off; 384 385 #ifdef MAC 386 /* 387 * Do directory search MAC check using non-cached credentials. 388 */ 389 if ((error = mac_vnode_check_readdir(td->td_ucred, vp))) 390 goto out; 391 #endif /* MAC */ 392 if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies, 393 &cookies))) 394 goto out; 395 396 inp = buf; 397 outp = (caddr_t)args->dirent; 398 resid = nbytes; 399 if ((len = buflen - auio.uio_resid) <= 0) 400 goto eof; 401 402 cookiep = cookies; 403 404 if (cookies) { 405 /* 406 * When using cookies, the vfs has the option of reading from 407 * a different offset than that supplied (UFS truncates the 408 * offset to a block boundary to make sure that it never reads 409 * partway through a directory entry, even if the directory 410 * has been compacted). 411 */ 412 while (len > 0 && ncookies > 0 && *cookiep <= off) { 413 bdp = (struct dirent *) inp; 414 len -= bdp->d_reclen; 415 inp += bdp->d_reclen; 416 cookiep++; 417 ncookies--; 418 } 419 } 420 421 while (len > 0) { 422 if (cookiep && ncookies == 0) 423 break; 424 bdp = (struct dirent *) inp; 425 reclen = bdp->d_reclen; 426 if (reclen & 3) { 427 error = EFAULT; 428 goto out; 429 } 430 431 if (bdp->d_fileno == 0) { 432 inp += reclen; 433 if (cookiep) { 434 off = *cookiep++; 435 ncookies--; 436 } else 437 off += reclen; 438 439 len -= reclen; 440 continue; 441 } 442 443 linuxreclen = (is64bit) 444 ? LINUX_RECLEN64(bdp->d_namlen) 445 : LINUX_RECLEN(bdp->d_namlen); 446 447 if (reclen > len || resid < linuxreclen) { 448 outp++; 449 break; 450 } 451 452 if (justone) { 453 /* readdir(2) case. */ 454 linux_dirent = (struct l_dirent*)lbuf; 455 linux_dirent->d_ino = bdp->d_fileno; 456 linux_dirent->d_off = (l_off_t)linuxreclen; 457 linux_dirent->d_reclen = (l_ushort)bdp->d_namlen; 458 strlcpy(linux_dirent->d_name, bdp->d_name, 459 linuxreclen - offsetof(struct l_dirent, d_name)); 460 error = copyout(linux_dirent, outp, linuxreclen); 461 } 462 if (is64bit) { 463 linux_dirent64 = (struct l_dirent64*)lbuf; 464 linux_dirent64->d_ino = bdp->d_fileno; 465 linux_dirent64->d_off = (cookiep) 466 ? (l_off_t)*cookiep 467 : (l_off_t)(off + reclen); 468 linux_dirent64->d_reclen = (l_ushort)linuxreclen; 469 linux_dirent64->d_type = bdp->d_type; 470 strlcpy(linux_dirent64->d_name, bdp->d_name, 471 linuxreclen - offsetof(struct l_dirent64, d_name)); 472 error = copyout(linux_dirent64, outp, linuxreclen); 473 } else if (!justone) { 474 linux_dirent = (struct l_dirent*)lbuf; 475 linux_dirent->d_ino = bdp->d_fileno; 476 linux_dirent->d_off = (cookiep) 477 ? (l_off_t)*cookiep 478 : (l_off_t)(off + reclen); 479 linux_dirent->d_reclen = (l_ushort)linuxreclen; 480 /* 481 * Copy d_type to last byte of l_dirent buffer 482 */ 483 lbuf[linuxreclen-1] = bdp->d_type; 484 strlcpy(linux_dirent->d_name, bdp->d_name, 485 linuxreclen - offsetof(struct l_dirent, d_name)-1); 486 error = copyout(linux_dirent, outp, linuxreclen); 487 } 488 489 if (error) 490 goto out; 491 492 inp += reclen; 493 if (cookiep) { 494 off = *cookiep++; 495 ncookies--; 496 } else 497 off += reclen; 498 499 outp += linuxreclen; 500 resid -= linuxreclen; 501 len -= reclen; 502 if (justone) 503 break; 504 } 505 506 if (outp == (caddr_t)args->dirent) { 507 nbytes = resid; 508 goto eof; 509 } 510 511 if (justone) 512 nbytes = resid + linuxreclen; 513 514 eof: 515 td->td_retval[0] = nbytes - resid; 516 517 out: 518 if (cookies) 519 free(cookies, M_TEMP); 520 521 VOP_UNLOCK(vp, 0); 522 foffset_unlock(fp, off, 0); 523 fdrop(fp, td); 524 free(buf, M_TEMP); 525 free(lbuf, M_TEMP); 526 return (error); 527 } 528 529 int 530 linux_getdents(struct thread *td, struct linux_getdents_args *args) 531 { 532 533 #ifdef DEBUG 534 if (ldebug(getdents)) 535 printf(ARGS(getdents, "%d, *, %d"), args->fd, args->count); 536 #endif 537 538 return (getdents_common(td, (struct linux_getdents64_args*)args, 0)); 539 } 540 541 int 542 linux_getdents64(struct thread *td, struct linux_getdents64_args *args) 543 { 544 545 #ifdef DEBUG 546 if (ldebug(getdents64)) 547 printf(ARGS(getdents64, "%d, *, %d"), args->fd, args->count); 548 #endif 549 550 return (getdents_common(td, args, 1)); 551 } 552 553 /* 554 * These exist mainly for hooks for doing /compat/linux translation. 555 */ 556 557 int 558 linux_access(struct thread *td, struct linux_access_args *args) 559 { 560 char *path; 561 int error; 562 563 /* linux convention */ 564 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK)) 565 return (EINVAL); 566 567 LCONVPATHEXIST(td, args->path, &path); 568 569 #ifdef DEBUG 570 if (ldebug(access)) 571 printf(ARGS(access, "%s, %d"), path, args->amode); 572 #endif 573 error = kern_access(td, path, UIO_SYSSPACE, args->amode); 574 LFREEPATH(path); 575 576 return (error); 577 } 578 579 int 580 linux_faccessat(struct thread *td, struct linux_faccessat_args *args) 581 { 582 char *path; 583 int error, dfd, flag; 584 585 if (args->flag & ~LINUX_AT_EACCESS) 586 return (EINVAL); 587 /* linux convention */ 588 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK)) 589 return (EINVAL); 590 591 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 592 LCONVPATHEXIST_AT(td, args->filename, &path, dfd); 593 594 #ifdef DEBUG 595 if (ldebug(access)) 596 printf(ARGS(access, "%s, %d"), path, args->amode); 597 #endif 598 599 flag = (args->flag & LINUX_AT_EACCESS) == 0 ? 0 : AT_EACCESS; 600 error = kern_accessat(td, dfd, path, UIO_SYSSPACE, flag, args->amode); 601 LFREEPATH(path); 602 603 return (error); 604 } 605 606 int 607 linux_unlink(struct thread *td, struct linux_unlink_args *args) 608 { 609 char *path; 610 int error; 611 struct stat st; 612 613 LCONVPATHEXIST(td, args->path, &path); 614 615 #ifdef DEBUG 616 if (ldebug(unlink)) 617 printf(ARGS(unlink, "%s"), path); 618 #endif 619 620 error = kern_unlink(td, path, UIO_SYSSPACE); 621 if (error == EPERM) 622 /* Introduce POSIX noncompliant behaviour of Linux */ 623 if (kern_stat(td, path, UIO_SYSSPACE, &st) == 0) 624 if (S_ISDIR(st.st_mode)) 625 error = EISDIR; 626 LFREEPATH(path); 627 return (error); 628 } 629 630 int 631 linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args) 632 { 633 char *path; 634 int error, dfd; 635 struct stat st; 636 637 if (args->flag & ~LINUX_AT_REMOVEDIR) 638 return (EINVAL); 639 640 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 641 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd); 642 643 #ifdef DEBUG 644 if (ldebug(unlinkat)) 645 printf(ARGS(unlinkat, "%s"), path); 646 #endif 647 648 if (args->flag & LINUX_AT_REMOVEDIR) 649 error = kern_rmdirat(td, dfd, path, UIO_SYSSPACE); 650 else 651 error = kern_unlinkat(td, dfd, path, UIO_SYSSPACE, 0); 652 if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) { 653 /* Introduce POSIX noncompliant behaviour of Linux */ 654 if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path, 655 UIO_SYSSPACE, &st) == 0 && S_ISDIR(st.st_mode)) 656 error = EISDIR; 657 } 658 LFREEPATH(path); 659 return (error); 660 } 661 int 662 linux_chdir(struct thread *td, struct linux_chdir_args *args) 663 { 664 char *path; 665 int error; 666 667 LCONVPATHEXIST(td, args->path, &path); 668 669 #ifdef DEBUG 670 if (ldebug(chdir)) 671 printf(ARGS(chdir, "%s"), path); 672 #endif 673 error = kern_chdir(td, path, UIO_SYSSPACE); 674 LFREEPATH(path); 675 return (error); 676 } 677 678 int 679 linux_chmod(struct thread *td, struct linux_chmod_args *args) 680 { 681 char *path; 682 int error; 683 684 LCONVPATHEXIST(td, args->path, &path); 685 686 #ifdef DEBUG 687 if (ldebug(chmod)) 688 printf(ARGS(chmod, "%s, %d"), path, args->mode); 689 #endif 690 error = kern_chmod(td, path, UIO_SYSSPACE, args->mode); 691 LFREEPATH(path); 692 return (error); 693 } 694 695 int 696 linux_fchmodat(struct thread *td, struct linux_fchmodat_args *args) 697 { 698 char *path; 699 int error, dfd; 700 701 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 702 LCONVPATHEXIST_AT(td, args->filename, &path, dfd); 703 704 #ifdef DEBUG 705 if (ldebug(fchmodat)) 706 printf(ARGS(fchmodat, "%s, %d"), path, args->mode); 707 #endif 708 709 error = kern_fchmodat(td, dfd, path, UIO_SYSSPACE, args->mode, 0); 710 LFREEPATH(path); 711 return (error); 712 } 713 714 int 715 linux_mkdir(struct thread *td, struct linux_mkdir_args *args) 716 { 717 char *path; 718 int error; 719 720 LCONVPATHCREAT(td, args->path, &path); 721 722 #ifdef DEBUG 723 if (ldebug(mkdir)) 724 printf(ARGS(mkdir, "%s, %d"), path, args->mode); 725 #endif 726 error = kern_mkdir(td, path, UIO_SYSSPACE, args->mode); 727 LFREEPATH(path); 728 return (error); 729 } 730 731 int 732 linux_mkdirat(struct thread *td, struct linux_mkdirat_args *args) 733 { 734 char *path; 735 int error, dfd; 736 737 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 738 LCONVPATHCREAT_AT(td, args->pathname, &path, dfd); 739 740 #ifdef DEBUG 741 if (ldebug(mkdirat)) 742 printf(ARGS(mkdirat, "%s, %d"), path, args->mode); 743 #endif 744 error = kern_mkdirat(td, dfd, path, UIO_SYSSPACE, args->mode); 745 LFREEPATH(path); 746 return (error); 747 } 748 749 int 750 linux_rmdir(struct thread *td, struct linux_rmdir_args *args) 751 { 752 char *path; 753 int error; 754 755 LCONVPATHEXIST(td, args->path, &path); 756 757 #ifdef DEBUG 758 if (ldebug(rmdir)) 759 printf(ARGS(rmdir, "%s"), path); 760 #endif 761 error = kern_rmdir(td, path, UIO_SYSSPACE); 762 LFREEPATH(path); 763 return (error); 764 } 765 766 int 767 linux_rename(struct thread *td, struct linux_rename_args *args) 768 { 769 char *from, *to; 770 int error; 771 772 LCONVPATHEXIST(td, args->from, &from); 773 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */ 774 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD); 775 if (to == NULL) { 776 LFREEPATH(from); 777 return (error); 778 } 779 780 #ifdef DEBUG 781 if (ldebug(rename)) 782 printf(ARGS(rename, "%s, %s"), from, to); 783 #endif 784 error = kern_rename(td, from, to, UIO_SYSSPACE); 785 LFREEPATH(from); 786 LFREEPATH(to); 787 return (error); 788 } 789 790 int 791 linux_renameat(struct thread *td, struct linux_renameat_args *args) 792 { 793 char *from, *to; 794 int error, olddfd, newdfd; 795 796 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd; 797 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd; 798 LCONVPATHEXIST_AT(td, args->oldname, &from, olddfd); 799 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */ 800 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd); 801 if (to == NULL) { 802 LFREEPATH(from); 803 return (error); 804 } 805 806 #ifdef DEBUG 807 if (ldebug(renameat)) 808 printf(ARGS(renameat, "%s, %s"), from, to); 809 #endif 810 error = kern_renameat(td, olddfd, from, newdfd, to, UIO_SYSSPACE); 811 LFREEPATH(from); 812 LFREEPATH(to); 813 return (error); 814 } 815 816 int 817 linux_symlink(struct thread *td, struct linux_symlink_args *args) 818 { 819 char *path, *to; 820 int error; 821 822 LCONVPATHEXIST(td, args->path, &path); 823 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */ 824 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD); 825 if (to == NULL) { 826 LFREEPATH(path); 827 return (error); 828 } 829 830 #ifdef DEBUG 831 if (ldebug(symlink)) 832 printf(ARGS(symlink, "%s, %s"), path, to); 833 #endif 834 error = kern_symlink(td, path, to, UIO_SYSSPACE); 835 LFREEPATH(path); 836 LFREEPATH(to); 837 return (error); 838 } 839 840 int 841 linux_symlinkat(struct thread *td, struct linux_symlinkat_args *args) 842 { 843 char *path, *to; 844 int error, dfd; 845 846 dfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd; 847 LCONVPATHEXIST_AT(td, args->oldname, &path, dfd); 848 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */ 849 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, dfd); 850 if (to == NULL) { 851 LFREEPATH(path); 852 return (error); 853 } 854 855 #ifdef DEBUG 856 if (ldebug(symlinkat)) 857 printf(ARGS(symlinkat, "%s, %s"), path, to); 858 #endif 859 860 error = kern_symlinkat(td, path, dfd, to, UIO_SYSSPACE); 861 LFREEPATH(path); 862 LFREEPATH(to); 863 return (error); 864 } 865 866 int 867 linux_readlink(struct thread *td, struct linux_readlink_args *args) 868 { 869 char *name; 870 int error; 871 872 LCONVPATHEXIST(td, args->name, &name); 873 874 #ifdef DEBUG 875 if (ldebug(readlink)) 876 printf(ARGS(readlink, "%s, %p, %d"), name, (void *)args->buf, 877 args->count); 878 #endif 879 error = kern_readlink(td, name, UIO_SYSSPACE, args->buf, UIO_USERSPACE, 880 args->count); 881 LFREEPATH(name); 882 return (error); 883 } 884 885 int 886 linux_readlinkat(struct thread *td, struct linux_readlinkat_args *args) 887 { 888 char *name; 889 int error, dfd; 890 891 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 892 LCONVPATHEXIST_AT(td, args->path, &name, dfd); 893 894 #ifdef DEBUG 895 if (ldebug(readlinkat)) 896 printf(ARGS(readlinkat, "%s, %p, %d"), name, (void *)args->buf, 897 args->bufsiz); 898 #endif 899 900 error = kern_readlinkat(td, dfd, name, UIO_SYSSPACE, args->buf, 901 UIO_USERSPACE, args->bufsiz); 902 LFREEPATH(name); 903 return (error); 904 } 905 906 int 907 linux_truncate(struct thread *td, struct linux_truncate_args *args) 908 { 909 char *path; 910 int error; 911 912 LCONVPATHEXIST(td, args->path, &path); 913 914 #ifdef DEBUG 915 if (ldebug(truncate)) 916 printf(ARGS(truncate, "%s, %ld"), path, (long)args->length); 917 #endif 918 919 error = kern_truncate(td, path, UIO_SYSSPACE, args->length); 920 LFREEPATH(path); 921 return (error); 922 } 923 924 int 925 linux_truncate64(struct thread *td, struct linux_truncate64_args *args) 926 { 927 char *path; 928 int error; 929 930 LCONVPATHEXIST(td, args->path, &path); 931 932 #ifdef DEBUG 933 if (ldebug(truncate64)) 934 printf(ARGS(truncate64, "%s, %jd"), path, args->length); 935 #endif 936 937 error = kern_truncate(td, path, UIO_SYSSPACE, args->length); 938 LFREEPATH(path); 939 return (error); 940 } 941 int 942 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args) 943 { 944 struct ftruncate_args /* { 945 int fd; 946 int pad; 947 off_t length; 948 } */ nuap; 949 950 nuap.fd = args->fd; 951 nuap.length = args->length; 952 return (sys_ftruncate(td, &nuap)); 953 } 954 955 int 956 linux_link(struct thread *td, struct linux_link_args *args) 957 { 958 char *path, *to; 959 int error; 960 961 LCONVPATHEXIST(td, args->path, &path); 962 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */ 963 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD); 964 if (to == NULL) { 965 LFREEPATH(path); 966 return (error); 967 } 968 969 #ifdef DEBUG 970 if (ldebug(link)) 971 printf(ARGS(link, "%s, %s"), path, to); 972 #endif 973 error = kern_link(td, path, to, UIO_SYSSPACE); 974 LFREEPATH(path); 975 LFREEPATH(to); 976 return (error); 977 } 978 979 int 980 linux_linkat(struct thread *td, struct linux_linkat_args *args) 981 { 982 char *path, *to; 983 int error, olddfd, newdfd, follow; 984 985 if (args->flag & ~LINUX_AT_SYMLINK_FOLLOW) 986 return (EINVAL); 987 988 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd; 989 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd; 990 LCONVPATHEXIST_AT(td, args->oldname, &path, olddfd); 991 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */ 992 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd); 993 if (to == NULL) { 994 LFREEPATH(path); 995 return (error); 996 } 997 998 #ifdef DEBUG 999 if (ldebug(linkat)) 1000 printf(ARGS(linkat, "%i, %s, %i, %s, %i"), args->olddfd, path, 1001 args->newdfd, to, args->flag); 1002 #endif 1003 1004 follow = (args->flag & LINUX_AT_SYMLINK_FOLLOW) == 0 ? NOFOLLOW : 1005 FOLLOW; 1006 error = kern_linkat(td, olddfd, newdfd, path, to, UIO_SYSSPACE, follow); 1007 LFREEPATH(path); 1008 LFREEPATH(to); 1009 return (error); 1010 } 1011 1012 int 1013 linux_fdatasync(td, uap) 1014 struct thread *td; 1015 struct linux_fdatasync_args *uap; 1016 { 1017 struct fsync_args bsd; 1018 1019 bsd.fd = uap->fd; 1020 return sys_fsync(td, &bsd); 1021 } 1022 1023 int 1024 linux_pread(td, uap) 1025 struct thread *td; 1026 struct linux_pread_args *uap; 1027 { 1028 struct pread_args bsd; 1029 struct vnode *vp; 1030 int error; 1031 1032 bsd.fd = uap->fd; 1033 bsd.buf = uap->buf; 1034 bsd.nbyte = uap->nbyte; 1035 bsd.offset = uap->offset; 1036 1037 error = sys_pread(td, &bsd); 1038 1039 if (error == 0) { 1040 /* This seems to violate POSIX but linux does it */ 1041 if ((error = fgetvp(td, uap->fd, CAP_READ, &vp)) != 0) 1042 return (error); 1043 if (vp->v_type == VDIR) { 1044 vrele(vp); 1045 return (EISDIR); 1046 } 1047 vrele(vp); 1048 } 1049 1050 return (error); 1051 } 1052 1053 int 1054 linux_pwrite(td, uap) 1055 struct thread *td; 1056 struct linux_pwrite_args *uap; 1057 { 1058 struct pwrite_args bsd; 1059 1060 bsd.fd = uap->fd; 1061 bsd.buf = uap->buf; 1062 bsd.nbyte = uap->nbyte; 1063 bsd.offset = uap->offset; 1064 return sys_pwrite(td, &bsd); 1065 } 1066 1067 int 1068 linux_mount(struct thread *td, struct linux_mount_args *args) 1069 { 1070 struct ufs_args ufs; 1071 char fstypename[MFSNAMELEN]; 1072 char mntonname[MNAMELEN], mntfromname[MNAMELEN]; 1073 int error; 1074 int fsflags; 1075 void *fsdata; 1076 1077 error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1, 1078 NULL); 1079 if (error) 1080 return (error); 1081 error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL); 1082 if (error) 1083 return (error); 1084 error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL); 1085 if (error) 1086 return (error); 1087 1088 #ifdef DEBUG 1089 if (ldebug(mount)) 1090 printf(ARGS(mount, "%s, %s, %s"), 1091 fstypename, mntfromname, mntonname); 1092 #endif 1093 1094 if (strcmp(fstypename, "ext2") == 0) { 1095 strcpy(fstypename, "ext2fs"); 1096 fsdata = &ufs; 1097 ufs.fspec = mntfromname; 1098 #define DEFAULT_ROOTID -2 1099 ufs.export.ex_root = DEFAULT_ROOTID; 1100 ufs.export.ex_flags = 1101 args->rwflag & LINUX_MS_RDONLY ? MNT_EXRDONLY : 0; 1102 } else if (strcmp(fstypename, "proc") == 0) { 1103 strcpy(fstypename, "linprocfs"); 1104 fsdata = NULL; 1105 } else if (strcmp(fstypename, "vfat") == 0) { 1106 strcpy(fstypename, "msdosfs"); 1107 fsdata = NULL; 1108 } else { 1109 return (ENODEV); 1110 } 1111 1112 fsflags = 0; 1113 1114 if ((args->rwflag & 0xffff0000) == 0xc0ed0000) { 1115 /* 1116 * Linux SYNC flag is not included; the closest equivalent 1117 * FreeBSD has is !ASYNC, which is our default. 1118 */ 1119 if (args->rwflag & LINUX_MS_RDONLY) 1120 fsflags |= MNT_RDONLY; 1121 if (args->rwflag & LINUX_MS_NOSUID) 1122 fsflags |= MNT_NOSUID; 1123 if (args->rwflag & LINUX_MS_NOEXEC) 1124 fsflags |= MNT_NOEXEC; 1125 if (args->rwflag & LINUX_MS_REMOUNT) 1126 fsflags |= MNT_UPDATE; 1127 } 1128 1129 if (strcmp(fstypename, "linprocfs") == 0) { 1130 error = kernel_vmount(fsflags, 1131 "fstype", fstypename, 1132 "fspath", mntonname, 1133 NULL); 1134 } else if (strcmp(fstypename, "msdosfs") == 0) { 1135 error = kernel_vmount(fsflags, 1136 "fstype", fstypename, 1137 "fspath", mntonname, 1138 "from", mntfromname, 1139 NULL); 1140 } else 1141 error = EOPNOTSUPP; 1142 return (error); 1143 } 1144 1145 int 1146 linux_oldumount(struct thread *td, struct linux_oldumount_args *args) 1147 { 1148 struct linux_umount_args args2; 1149 1150 args2.path = args->path; 1151 args2.flags = 0; 1152 return (linux_umount(td, &args2)); 1153 } 1154 1155 int 1156 linux_umount(struct thread *td, struct linux_umount_args *args) 1157 { 1158 struct unmount_args bsd; 1159 1160 bsd.path = args->path; 1161 bsd.flags = args->flags; /* XXX correct? */ 1162 return (sys_unmount(td, &bsd)); 1163 } 1164 1165 /* 1166 * fcntl family of syscalls 1167 */ 1168 1169 struct l_flock { 1170 l_short l_type; 1171 l_short l_whence; 1172 l_off_t l_start; 1173 l_off_t l_len; 1174 l_pid_t l_pid; 1175 } 1176 #if defined(__amd64__) && defined(COMPAT_LINUX32) 1177 __packed 1178 #endif 1179 ; 1180 1181 static void 1182 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock) 1183 { 1184 switch (linux_flock->l_type) { 1185 case LINUX_F_RDLCK: 1186 bsd_flock->l_type = F_RDLCK; 1187 break; 1188 case LINUX_F_WRLCK: 1189 bsd_flock->l_type = F_WRLCK; 1190 break; 1191 case LINUX_F_UNLCK: 1192 bsd_flock->l_type = F_UNLCK; 1193 break; 1194 default: 1195 bsd_flock->l_type = -1; 1196 break; 1197 } 1198 bsd_flock->l_whence = linux_flock->l_whence; 1199 bsd_flock->l_start = (off_t)linux_flock->l_start; 1200 bsd_flock->l_len = (off_t)linux_flock->l_len; 1201 bsd_flock->l_pid = (pid_t)linux_flock->l_pid; 1202 bsd_flock->l_sysid = 0; 1203 } 1204 1205 static void 1206 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock) 1207 { 1208 switch (bsd_flock->l_type) { 1209 case F_RDLCK: 1210 linux_flock->l_type = LINUX_F_RDLCK; 1211 break; 1212 case F_WRLCK: 1213 linux_flock->l_type = LINUX_F_WRLCK; 1214 break; 1215 case F_UNLCK: 1216 linux_flock->l_type = LINUX_F_UNLCK; 1217 break; 1218 } 1219 linux_flock->l_whence = bsd_flock->l_whence; 1220 linux_flock->l_start = (l_off_t)bsd_flock->l_start; 1221 linux_flock->l_len = (l_off_t)bsd_flock->l_len; 1222 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid; 1223 } 1224 1225 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1226 struct l_flock64 { 1227 l_short l_type; 1228 l_short l_whence; 1229 l_loff_t l_start; 1230 l_loff_t l_len; 1231 l_pid_t l_pid; 1232 } 1233 #if defined(__amd64__) && defined(COMPAT_LINUX32) 1234 __packed 1235 #endif 1236 ; 1237 1238 static void 1239 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock) 1240 { 1241 switch (linux_flock->l_type) { 1242 case LINUX_F_RDLCK: 1243 bsd_flock->l_type = F_RDLCK; 1244 break; 1245 case LINUX_F_WRLCK: 1246 bsd_flock->l_type = F_WRLCK; 1247 break; 1248 case LINUX_F_UNLCK: 1249 bsd_flock->l_type = F_UNLCK; 1250 break; 1251 default: 1252 bsd_flock->l_type = -1; 1253 break; 1254 } 1255 bsd_flock->l_whence = linux_flock->l_whence; 1256 bsd_flock->l_start = (off_t)linux_flock->l_start; 1257 bsd_flock->l_len = (off_t)linux_flock->l_len; 1258 bsd_flock->l_pid = (pid_t)linux_flock->l_pid; 1259 bsd_flock->l_sysid = 0; 1260 } 1261 1262 static void 1263 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock) 1264 { 1265 switch (bsd_flock->l_type) { 1266 case F_RDLCK: 1267 linux_flock->l_type = LINUX_F_RDLCK; 1268 break; 1269 case F_WRLCK: 1270 linux_flock->l_type = LINUX_F_WRLCK; 1271 break; 1272 case F_UNLCK: 1273 linux_flock->l_type = LINUX_F_UNLCK; 1274 break; 1275 } 1276 linux_flock->l_whence = bsd_flock->l_whence; 1277 linux_flock->l_start = (l_loff_t)bsd_flock->l_start; 1278 linux_flock->l_len = (l_loff_t)bsd_flock->l_len; 1279 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid; 1280 } 1281 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1282 1283 static int 1284 fcntl_common(struct thread *td, struct linux_fcntl64_args *args) 1285 { 1286 struct l_flock linux_flock; 1287 struct flock bsd_flock; 1288 struct file *fp; 1289 long arg; 1290 int error, result; 1291 1292 switch (args->cmd) { 1293 case LINUX_F_DUPFD: 1294 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg)); 1295 1296 case LINUX_F_GETFD: 1297 return (kern_fcntl(td, args->fd, F_GETFD, 0)); 1298 1299 case LINUX_F_SETFD: 1300 return (kern_fcntl(td, args->fd, F_SETFD, args->arg)); 1301 1302 case LINUX_F_GETFL: 1303 error = kern_fcntl(td, args->fd, F_GETFL, 0); 1304 result = td->td_retval[0]; 1305 td->td_retval[0] = 0; 1306 if (result & O_RDONLY) 1307 td->td_retval[0] |= LINUX_O_RDONLY; 1308 if (result & O_WRONLY) 1309 td->td_retval[0] |= LINUX_O_WRONLY; 1310 if (result & O_RDWR) 1311 td->td_retval[0] |= LINUX_O_RDWR; 1312 if (result & O_NDELAY) 1313 td->td_retval[0] |= LINUX_O_NONBLOCK; 1314 if (result & O_APPEND) 1315 td->td_retval[0] |= LINUX_O_APPEND; 1316 if (result & O_FSYNC) 1317 td->td_retval[0] |= LINUX_O_SYNC; 1318 if (result & O_ASYNC) 1319 td->td_retval[0] |= LINUX_FASYNC; 1320 #ifdef LINUX_O_NOFOLLOW 1321 if (result & O_NOFOLLOW) 1322 td->td_retval[0] |= LINUX_O_NOFOLLOW; 1323 #endif 1324 #ifdef LINUX_O_DIRECT 1325 if (result & O_DIRECT) 1326 td->td_retval[0] |= LINUX_O_DIRECT; 1327 #endif 1328 return (error); 1329 1330 case LINUX_F_SETFL: 1331 arg = 0; 1332 if (args->arg & LINUX_O_NDELAY) 1333 arg |= O_NONBLOCK; 1334 if (args->arg & LINUX_O_APPEND) 1335 arg |= O_APPEND; 1336 if (args->arg & LINUX_O_SYNC) 1337 arg |= O_FSYNC; 1338 if (args->arg & LINUX_FASYNC) 1339 arg |= O_ASYNC; 1340 #ifdef LINUX_O_NOFOLLOW 1341 if (args->arg & LINUX_O_NOFOLLOW) 1342 arg |= O_NOFOLLOW; 1343 #endif 1344 #ifdef LINUX_O_DIRECT 1345 if (args->arg & LINUX_O_DIRECT) 1346 arg |= O_DIRECT; 1347 #endif 1348 return (kern_fcntl(td, args->fd, F_SETFL, arg)); 1349 1350 case LINUX_F_GETLK: 1351 error = copyin((void *)args->arg, &linux_flock, 1352 sizeof(linux_flock)); 1353 if (error) 1354 return (error); 1355 linux_to_bsd_flock(&linux_flock, &bsd_flock); 1356 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock); 1357 if (error) 1358 return (error); 1359 bsd_to_linux_flock(&bsd_flock, &linux_flock); 1360 return (copyout(&linux_flock, (void *)args->arg, 1361 sizeof(linux_flock))); 1362 1363 case LINUX_F_SETLK: 1364 error = copyin((void *)args->arg, &linux_flock, 1365 sizeof(linux_flock)); 1366 if (error) 1367 return (error); 1368 linux_to_bsd_flock(&linux_flock, &bsd_flock); 1369 return (kern_fcntl(td, args->fd, F_SETLK, 1370 (intptr_t)&bsd_flock)); 1371 1372 case LINUX_F_SETLKW: 1373 error = copyin((void *)args->arg, &linux_flock, 1374 sizeof(linux_flock)); 1375 if (error) 1376 return (error); 1377 linux_to_bsd_flock(&linux_flock, &bsd_flock); 1378 return (kern_fcntl(td, args->fd, F_SETLKW, 1379 (intptr_t)&bsd_flock)); 1380 1381 case LINUX_F_GETOWN: 1382 return (kern_fcntl(td, args->fd, F_GETOWN, 0)); 1383 1384 case LINUX_F_SETOWN: 1385 /* 1386 * XXX some Linux applications depend on F_SETOWN having no 1387 * significant effect for pipes (SIGIO is not delivered for 1388 * pipes under Linux-2.2.35 at least). 1389 */ 1390 error = fget(td, args->fd, CAP_FCNTL, &fp); 1391 if (error) 1392 return (error); 1393 if (fp->f_type == DTYPE_PIPE) { 1394 fdrop(fp, td); 1395 return (EINVAL); 1396 } 1397 fdrop(fp, td); 1398 1399 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg)); 1400 } 1401 1402 return (EINVAL); 1403 } 1404 1405 int 1406 linux_fcntl(struct thread *td, struct linux_fcntl_args *args) 1407 { 1408 struct linux_fcntl64_args args64; 1409 1410 #ifdef DEBUG 1411 if (ldebug(fcntl)) 1412 printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd); 1413 #endif 1414 1415 args64.fd = args->fd; 1416 args64.cmd = args->cmd; 1417 args64.arg = args->arg; 1418 return (fcntl_common(td, &args64)); 1419 } 1420 1421 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1422 int 1423 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args) 1424 { 1425 struct l_flock64 linux_flock; 1426 struct flock bsd_flock; 1427 int error; 1428 1429 #ifdef DEBUG 1430 if (ldebug(fcntl64)) 1431 printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd); 1432 #endif 1433 1434 switch (args->cmd) { 1435 case LINUX_F_GETLK64: 1436 error = copyin((void *)args->arg, &linux_flock, 1437 sizeof(linux_flock)); 1438 if (error) 1439 return (error); 1440 linux_to_bsd_flock64(&linux_flock, &bsd_flock); 1441 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock); 1442 if (error) 1443 return (error); 1444 bsd_to_linux_flock64(&bsd_flock, &linux_flock); 1445 return (copyout(&linux_flock, (void *)args->arg, 1446 sizeof(linux_flock))); 1447 1448 case LINUX_F_SETLK64: 1449 error = copyin((void *)args->arg, &linux_flock, 1450 sizeof(linux_flock)); 1451 if (error) 1452 return (error); 1453 linux_to_bsd_flock64(&linux_flock, &bsd_flock); 1454 return (kern_fcntl(td, args->fd, F_SETLK, 1455 (intptr_t)&bsd_flock)); 1456 1457 case LINUX_F_SETLKW64: 1458 error = copyin((void *)args->arg, &linux_flock, 1459 sizeof(linux_flock)); 1460 if (error) 1461 return (error); 1462 linux_to_bsd_flock64(&linux_flock, &bsd_flock); 1463 return (kern_fcntl(td, args->fd, F_SETLKW, 1464 (intptr_t)&bsd_flock)); 1465 } 1466 1467 return (fcntl_common(td, args)); 1468 } 1469 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1470 1471 int 1472 linux_chown(struct thread *td, struct linux_chown_args *args) 1473 { 1474 char *path; 1475 int error; 1476 1477 LCONVPATHEXIST(td, args->path, &path); 1478 1479 #ifdef DEBUG 1480 if (ldebug(chown)) 1481 printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid); 1482 #endif 1483 error = kern_chown(td, path, UIO_SYSSPACE, args->uid, args->gid); 1484 LFREEPATH(path); 1485 return (error); 1486 } 1487 1488 int 1489 linux_fchownat(struct thread *td, struct linux_fchownat_args *args) 1490 { 1491 char *path; 1492 int error, dfd, flag; 1493 1494 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW) 1495 return (EINVAL); 1496 1497 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 1498 LCONVPATHEXIST_AT(td, args->filename, &path, dfd); 1499 1500 #ifdef DEBUG 1501 if (ldebug(fchownat)) 1502 printf(ARGS(fchownat, "%s, %d, %d"), path, args->uid, args->gid); 1503 #endif 1504 1505 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 : 1506 AT_SYMLINK_NOFOLLOW; 1507 error = kern_fchownat(td, dfd, path, UIO_SYSSPACE, args->uid, args->gid, 1508 flag); 1509 LFREEPATH(path); 1510 return (error); 1511 } 1512 1513 int 1514 linux_lchown(struct thread *td, struct linux_lchown_args *args) 1515 { 1516 char *path; 1517 int error; 1518 1519 LCONVPATHEXIST(td, args->path, &path); 1520 1521 #ifdef DEBUG 1522 if (ldebug(lchown)) 1523 printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid); 1524 #endif 1525 error = kern_lchown(td, path, UIO_SYSSPACE, args->uid, args->gid); 1526 LFREEPATH(path); 1527 return (error); 1528 } 1529 1530 static int 1531 convert_fadvice(int advice) 1532 { 1533 switch (advice) { 1534 case LINUX_POSIX_FADV_NORMAL: 1535 return (POSIX_FADV_NORMAL); 1536 case LINUX_POSIX_FADV_RANDOM: 1537 return (POSIX_FADV_RANDOM); 1538 case LINUX_POSIX_FADV_SEQUENTIAL: 1539 return (POSIX_FADV_SEQUENTIAL); 1540 case LINUX_POSIX_FADV_WILLNEED: 1541 return (POSIX_FADV_WILLNEED); 1542 case LINUX_POSIX_FADV_DONTNEED: 1543 return (POSIX_FADV_DONTNEED); 1544 case LINUX_POSIX_FADV_NOREUSE: 1545 return (POSIX_FADV_NOREUSE); 1546 default: 1547 return (-1); 1548 } 1549 } 1550 1551 int 1552 linux_fadvise64(struct thread *td, struct linux_fadvise64_args *args) 1553 { 1554 int advice; 1555 1556 advice = convert_fadvice(args->advice); 1557 if (advice == -1) 1558 return (EINVAL); 1559 return (kern_posix_fadvise(td, args->fd, args->offset, args->len, 1560 advice)); 1561 } 1562 1563 int 1564 linux_fadvise64_64(struct thread *td, struct linux_fadvise64_64_args *args) 1565 { 1566 int advice; 1567 1568 advice = convert_fadvice(args->advice); 1569 if (advice == -1) 1570 return (EINVAL); 1571 return (kern_posix_fadvise(td, args->fd, args->offset, args->len, 1572 advice)); 1573 } 1574 1575 int 1576 linux_pipe(struct thread *td, struct linux_pipe_args *args) 1577 { 1578 int fildes[2]; 1579 int error; 1580 1581 #ifdef DEBUG 1582 if (ldebug(pipe)) 1583 printf(ARGS(pipe, "*")); 1584 #endif 1585 1586 error = do_pipe(td, fildes, 0); 1587 if (error) 1588 return (error); 1589 1590 /* XXX: Close descriptors on error. */ 1591 return (copyout(fildes, args->pipefds, sizeof(fildes))); 1592 } 1593 1594 int 1595 linux_pipe2(struct thread *td, struct linux_pipe2_args *args) 1596 { 1597 int fildes[2]; 1598 int error, flags; 1599 1600 #ifdef DEBUG 1601 if (ldebug(pipe2)) 1602 printf(ARGS(pipe2, "*, %d"), args->flags); 1603 #endif 1604 1605 if ((args->flags & ~(LINUX_O_NONBLOCK | LINUX_O_CLOEXEC)) != 0) 1606 return (EINVAL); 1607 1608 flags = 0; 1609 if ((args->flags & LINUX_O_NONBLOCK) != 0) 1610 flags |= O_NONBLOCK; 1611 if ((args->flags & LINUX_O_CLOEXEC) != 0) 1612 flags |= O_CLOEXEC; 1613 error = do_pipe(td, fildes, flags); 1614 if (error) 1615 return (error); 1616 1617 /* XXX: Close descriptors on error. */ 1618 return (copyout(fildes, args->pipefds, sizeof(fildes))); 1619 } 1620