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/capsicum.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 #ifdef COMPAT_LINUX32 59 #include <machine/../linux32/linux.h> 60 #include <machine/../linux32/linux32_proto.h> 61 #else 62 #include <machine/../linux/linux.h> 63 #include <machine/../linux/linux_proto.h> 64 #endif 65 #include <compat/linux/linux_misc.h> 66 #include <compat/linux/linux_util.h> 67 #include <compat/linux/linux_file.h> 68 69 int 70 linux_creat(struct thread *td, struct linux_creat_args *args) 71 { 72 char *path; 73 int error; 74 75 LCONVPATHEXIST(td, args->path, &path); 76 77 #ifdef DEBUG 78 if (ldebug(creat)) 79 printf(ARGS(creat, "%s, %d"), path, args->mode); 80 #endif 81 error = kern_openat(td, AT_FDCWD, path, UIO_SYSSPACE, 82 O_WRONLY | O_CREAT | O_TRUNC, args->mode); 83 LFREEPATH(path); 84 return (error); 85 } 86 87 88 static int 89 linux_common_open(struct thread *td, int dirfd, char *path, int l_flags, int mode) 90 { 91 cap_rights_t rights; 92 struct proc *p = td->td_proc; 93 struct file *fp; 94 int fd; 95 int bsd_flags, error; 96 97 bsd_flags = 0; 98 switch (l_flags & LINUX_O_ACCMODE) { 99 case LINUX_O_WRONLY: 100 bsd_flags |= O_WRONLY; 101 break; 102 case LINUX_O_RDWR: 103 bsd_flags |= O_RDWR; 104 break; 105 default: 106 bsd_flags |= O_RDONLY; 107 } 108 if (l_flags & LINUX_O_NDELAY) 109 bsd_flags |= O_NONBLOCK; 110 if (l_flags & LINUX_O_APPEND) 111 bsd_flags |= O_APPEND; 112 if (l_flags & LINUX_O_SYNC) 113 bsd_flags |= O_FSYNC; 114 if (l_flags & LINUX_O_NONBLOCK) 115 bsd_flags |= O_NONBLOCK; 116 if (l_flags & LINUX_FASYNC) 117 bsd_flags |= O_ASYNC; 118 if (l_flags & LINUX_O_CREAT) 119 bsd_flags |= O_CREAT; 120 if (l_flags & LINUX_O_TRUNC) 121 bsd_flags |= O_TRUNC; 122 if (l_flags & LINUX_O_EXCL) 123 bsd_flags |= O_EXCL; 124 if (l_flags & LINUX_O_NOCTTY) 125 bsd_flags |= O_NOCTTY; 126 if (l_flags & LINUX_O_DIRECT) 127 bsd_flags |= O_DIRECT; 128 if (l_flags & LINUX_O_NOFOLLOW) 129 bsd_flags |= O_NOFOLLOW; 130 if (l_flags & LINUX_O_DIRECTORY) 131 bsd_flags |= O_DIRECTORY; 132 /* XXX LINUX_O_NOATIME: unable to be easily implemented. */ 133 134 error = kern_openat(td, dirfd, path, UIO_SYSSPACE, bsd_flags, mode); 135 if (error != 0) 136 goto done; 137 138 if (bsd_flags & O_NOCTTY) 139 goto done; 140 141 /* 142 * XXX In between kern_open() and fget(), another process 143 * having the same filedesc could use that fd without 144 * checking below. 145 */ 146 fd = td->td_retval[0]; 147 if (fget(td, fd, cap_rights_init(&rights, CAP_IOCTL), &fp) == 0) { 148 if (fp->f_type != DTYPE_VNODE) { 149 fdrop(fp, td); 150 goto done; 151 } 152 sx_slock(&proctree_lock); 153 PROC_LOCK(p); 154 if (SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) { 155 PROC_UNLOCK(p); 156 sx_sunlock(&proctree_lock); 157 /* XXXPJD: Verify if TIOCSCTTY is allowed. */ 158 (void) fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, 159 td->td_ucred, td); 160 } else { 161 PROC_UNLOCK(p); 162 sx_sunlock(&proctree_lock); 163 } 164 fdrop(fp, td); 165 } 166 167 done: 168 #ifdef DEBUG 169 if (ldebug(open)) 170 printf(LMSG("open returns error %d"), error); 171 #endif 172 LFREEPATH(path); 173 return (error); 174 } 175 176 int 177 linux_openat(struct thread *td, struct linux_openat_args *args) 178 { 179 char *path; 180 int dfd; 181 182 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 183 if (args->flags & LINUX_O_CREAT) 184 LCONVPATH_AT(td, args->filename, &path, 1, dfd); 185 else 186 LCONVPATH_AT(td, args->filename, &path, 0, dfd); 187 #ifdef DEBUG 188 if (ldebug(openat)) 189 printf(ARGS(openat, "%i, %s, 0x%x, 0x%x"), args->dfd, 190 path, args->flags, args->mode); 191 #endif 192 return (linux_common_open(td, dfd, path, args->flags, args->mode)); 193 } 194 195 int 196 linux_open(struct thread *td, struct linux_open_args *args) 197 { 198 char *path; 199 200 if (args->flags & LINUX_O_CREAT) 201 LCONVPATHCREAT(td, args->path, &path); 202 else 203 LCONVPATHEXIST(td, args->path, &path); 204 205 #ifdef DEBUG 206 if (ldebug(open)) 207 printf(ARGS(open, "%s, 0x%x, 0x%x"), 208 path, args->flags, args->mode); 209 #endif 210 211 return (linux_common_open(td, AT_FDCWD, path, args->flags, args->mode)); 212 } 213 214 int 215 linux_lseek(struct thread *td, struct linux_lseek_args *args) 216 { 217 218 struct lseek_args /* { 219 int fd; 220 int pad; 221 off_t offset; 222 int whence; 223 } */ tmp_args; 224 int error; 225 226 #ifdef DEBUG 227 if (ldebug(lseek)) 228 printf(ARGS(lseek, "%d, %ld, %d"), 229 args->fdes, (long)args->off, args->whence); 230 #endif 231 tmp_args.fd = args->fdes; 232 tmp_args.offset = (off_t)args->off; 233 tmp_args.whence = args->whence; 234 error = sys_lseek(td, &tmp_args); 235 return error; 236 } 237 238 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 239 int 240 linux_llseek(struct thread *td, struct linux_llseek_args *args) 241 { 242 struct lseek_args bsd_args; 243 int error; 244 off_t off; 245 246 #ifdef DEBUG 247 if (ldebug(llseek)) 248 printf(ARGS(llseek, "%d, %d:%d, %d"), 249 args->fd, args->ohigh, args->olow, args->whence); 250 #endif 251 off = (args->olow) | (((off_t) args->ohigh) << 32); 252 253 bsd_args.fd = args->fd; 254 bsd_args.offset = off; 255 bsd_args.whence = args->whence; 256 257 if ((error = sys_lseek(td, &bsd_args))) 258 return error; 259 260 if ((error = copyout(td->td_retval, args->res, sizeof (off_t)))) 261 return error; 262 263 td->td_retval[0] = 0; 264 return 0; 265 } 266 267 int 268 linux_readdir(struct thread *td, struct linux_readdir_args *args) 269 { 270 struct linux_getdents_args lda; 271 272 lda.fd = args->fd; 273 lda.dent = args->dent; 274 lda.count = 1; 275 return linux_getdents(td, &lda); 276 } 277 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 278 279 /* 280 * Note that linux_getdents(2) and linux_getdents64(2) have the same 281 * arguments. They only differ in the definition of struct dirent they 282 * operate on. We use this to common the code, with the exception of 283 * accessing struct dirent. Note that linux_readdir(2) is implemented 284 * by means of linux_getdents(2). In this case we never operate on 285 * struct dirent64 and thus don't need to handle it... 286 */ 287 288 struct l_dirent { 289 l_ulong d_ino; 290 l_off_t d_off; 291 l_ushort d_reclen; 292 char d_name[LINUX_NAME_MAX + 1]; 293 }; 294 295 struct l_dirent64 { 296 uint64_t d_ino; 297 int64_t d_off; 298 l_ushort d_reclen; 299 u_char d_type; 300 char d_name[LINUX_NAME_MAX + 1]; 301 }; 302 303 /* 304 * Linux uses the last byte in the dirent buffer to store d_type, 305 * at least glibc-2.7 requires it. That is why l_dirent is padded with 2 bytes. 306 */ 307 #define LINUX_RECLEN(namlen) \ 308 roundup(offsetof(struct l_dirent, d_name) + (namlen) + 2, sizeof(l_ulong)) 309 310 #define LINUX_RECLEN64(namlen) \ 311 roundup(offsetof(struct l_dirent64, d_name) + (namlen) + 1, \ 312 sizeof(uint64_t)) 313 314 #define LINUX_MAXRECLEN max(LINUX_RECLEN(LINUX_NAME_MAX), \ 315 LINUX_RECLEN64(LINUX_NAME_MAX)) 316 #define LINUX_DIRBLKSIZ 512 317 318 static int 319 getdents_common(struct thread *td, struct linux_getdents64_args *args, 320 int is64bit) 321 { 322 struct dirent *bdp; 323 struct vnode *vp; 324 caddr_t inp, buf; /* BSD-format */ 325 int len, reclen; /* BSD-format */ 326 caddr_t outp; /* Linux-format */ 327 int resid, linuxreclen=0; /* Linux-format */ 328 caddr_t lbuf; /* Linux-format */ 329 cap_rights_t rights; 330 struct file *fp; 331 struct uio auio; 332 struct iovec aiov; 333 off_t off; 334 struct l_dirent *linux_dirent; 335 struct l_dirent64 *linux_dirent64; 336 int buflen, error, eofflag, nbytes, justone; 337 u_long *cookies = NULL, *cookiep; 338 int ncookies; 339 340 nbytes = args->count; 341 if (nbytes == 1) { 342 /* readdir(2) case. Always struct dirent. */ 343 if (is64bit) 344 return (EINVAL); 345 nbytes = sizeof(*linux_dirent); 346 justone = 1; 347 } else 348 justone = 0; 349 350 error = getvnode(td, args->fd, cap_rights_init(&rights, CAP_READ), &fp); 351 if (error != 0) 352 return (error); 353 354 if ((fp->f_flag & FREAD) == 0) { 355 fdrop(fp, td); 356 return (EBADF); 357 } 358 359 off = foffset_lock(fp, 0); 360 vp = fp->f_vnode; 361 if (vp->v_type != VDIR) { 362 foffset_unlock(fp, off, 0); 363 fdrop(fp, td); 364 return (EINVAL); 365 } 366 367 368 buflen = max(LINUX_DIRBLKSIZ, nbytes); 369 buflen = min(buflen, MAXBSIZE); 370 buf = malloc(buflen, M_LINUX, M_WAITOK); 371 lbuf = malloc(LINUX_MAXRECLEN, M_LINUX, M_WAITOK | M_ZERO); 372 vn_lock(vp, LK_SHARED | LK_RETRY); 373 374 aiov.iov_base = buf; 375 aiov.iov_len = buflen; 376 auio.uio_iov = &aiov; 377 auio.uio_iovcnt = 1; 378 auio.uio_rw = UIO_READ; 379 auio.uio_segflg = UIO_SYSSPACE; 380 auio.uio_td = td; 381 auio.uio_resid = buflen; 382 auio.uio_offset = off; 383 384 #ifdef MAC 385 /* 386 * Do directory search MAC check using non-cached credentials. 387 */ 388 if ((error = mac_vnode_check_readdir(td->td_ucred, vp))) 389 goto out; 390 #endif /* MAC */ 391 if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies, 392 &cookies))) 393 goto out; 394 395 inp = buf; 396 outp = (caddr_t)args->dirent; 397 resid = nbytes; 398 if ((len = buflen - auio.uio_resid) <= 0) 399 goto eof; 400 401 cookiep = cookies; 402 403 if (cookies) { 404 /* 405 * When using cookies, the vfs has the option of reading from 406 * a different offset than that supplied (UFS truncates the 407 * offset to a block boundary to make sure that it never reads 408 * partway through a directory entry, even if the directory 409 * has been compacted). 410 */ 411 while (len > 0 && ncookies > 0 && *cookiep <= off) { 412 bdp = (struct dirent *) inp; 413 len -= bdp->d_reclen; 414 inp += bdp->d_reclen; 415 cookiep++; 416 ncookies--; 417 } 418 } 419 420 while (len > 0) { 421 if (cookiep && ncookies == 0) 422 break; 423 bdp = (struct dirent *) inp; 424 reclen = bdp->d_reclen; 425 if (reclen & 3) { 426 error = EFAULT; 427 goto out; 428 } 429 430 if (bdp->d_fileno == 0) { 431 inp += reclen; 432 if (cookiep) { 433 off = *cookiep++; 434 ncookies--; 435 } else 436 off += reclen; 437 438 len -= reclen; 439 continue; 440 } 441 442 linuxreclen = (is64bit) 443 ? LINUX_RECLEN64(bdp->d_namlen) 444 : LINUX_RECLEN(bdp->d_namlen); 445 446 if (reclen > len || resid < linuxreclen) { 447 outp++; 448 break; 449 } 450 451 if (justone) { 452 /* readdir(2) case. */ 453 linux_dirent = (struct l_dirent*)lbuf; 454 linux_dirent->d_ino = bdp->d_fileno; 455 linux_dirent->d_off = (l_off_t)linuxreclen; 456 linux_dirent->d_reclen = (l_ushort)bdp->d_namlen; 457 strlcpy(linux_dirent->d_name, bdp->d_name, 458 linuxreclen - offsetof(struct l_dirent, d_name)); 459 error = copyout(linux_dirent, outp, linuxreclen); 460 } 461 if (is64bit) { 462 linux_dirent64 = (struct l_dirent64*)lbuf; 463 linux_dirent64->d_ino = bdp->d_fileno; 464 linux_dirent64->d_off = (cookiep) 465 ? (l_off_t)*cookiep 466 : (l_off_t)(off + reclen); 467 linux_dirent64->d_reclen = (l_ushort)linuxreclen; 468 linux_dirent64->d_type = bdp->d_type; 469 strlcpy(linux_dirent64->d_name, bdp->d_name, 470 linuxreclen - offsetof(struct l_dirent64, d_name)); 471 error = copyout(linux_dirent64, outp, linuxreclen); 472 } else if (!justone) { 473 linux_dirent = (struct l_dirent*)lbuf; 474 linux_dirent->d_ino = bdp->d_fileno; 475 linux_dirent->d_off = (cookiep) 476 ? (l_off_t)*cookiep 477 : (l_off_t)(off + reclen); 478 linux_dirent->d_reclen = (l_ushort)linuxreclen; 479 /* 480 * Copy d_type to last byte of l_dirent buffer 481 */ 482 lbuf[linuxreclen-1] = bdp->d_type; 483 strlcpy(linux_dirent->d_name, bdp->d_name, 484 linuxreclen - offsetof(struct l_dirent, d_name)-1); 485 error = copyout(linux_dirent, outp, linuxreclen); 486 } 487 488 if (error) 489 goto out; 490 491 inp += reclen; 492 if (cookiep) { 493 off = *cookiep++; 494 ncookies--; 495 } else 496 off += reclen; 497 498 outp += linuxreclen; 499 resid -= linuxreclen; 500 len -= reclen; 501 if (justone) 502 break; 503 } 504 505 if (outp == (caddr_t)args->dirent) { 506 nbytes = resid; 507 goto eof; 508 } 509 510 if (justone) 511 nbytes = resid + linuxreclen; 512 513 eof: 514 td->td_retval[0] = nbytes - resid; 515 516 out: 517 free(cookies, M_TEMP); 518 519 VOP_UNLOCK(vp, 0); 520 foffset_unlock(fp, off, 0); 521 fdrop(fp, td); 522 free(buf, M_LINUX); 523 free(lbuf, M_LINUX); 524 return (error); 525 } 526 527 int 528 linux_getdents(struct thread *td, struct linux_getdents_args *args) 529 { 530 531 #ifdef DEBUG 532 if (ldebug(getdents)) 533 printf(ARGS(getdents, "%d, *, %d"), args->fd, args->count); 534 #endif 535 536 return (getdents_common(td, (struct linux_getdents64_args*)args, 0)); 537 } 538 539 int 540 linux_getdents64(struct thread *td, struct linux_getdents64_args *args) 541 { 542 543 #ifdef DEBUG 544 if (ldebug(getdents64)) 545 printf(ARGS(getdents64, "%d, *, %d"), args->fd, args->count); 546 #endif 547 548 return (getdents_common(td, args, 1)); 549 } 550 551 /* 552 * These exist mainly for hooks for doing /compat/linux translation. 553 */ 554 555 int 556 linux_access(struct thread *td, struct linux_access_args *args) 557 { 558 char *path; 559 int error; 560 561 /* linux convention */ 562 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK)) 563 return (EINVAL); 564 565 LCONVPATHEXIST(td, args->path, &path); 566 567 #ifdef DEBUG 568 if (ldebug(access)) 569 printf(ARGS(access, "%s, %d"), path, args->amode); 570 #endif 571 error = kern_accessat(td, AT_FDCWD, path, UIO_SYSSPACE, 0, 572 args->amode); 573 LFREEPATH(path); 574 575 return (error); 576 } 577 578 int 579 linux_faccessat(struct thread *td, struct linux_faccessat_args *args) 580 { 581 char *path; 582 int error, dfd; 583 584 /* linux convention */ 585 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK)) 586 return (EINVAL); 587 588 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 589 LCONVPATHEXIST_AT(td, args->filename, &path, dfd); 590 591 #ifdef DEBUG 592 if (ldebug(access)) 593 printf(ARGS(access, "%s, %d"), path, args->amode); 594 #endif 595 596 error = kern_accessat(td, dfd, path, UIO_SYSSPACE, 0, args->amode); 597 LFREEPATH(path); 598 599 return (error); 600 } 601 602 int 603 linux_unlink(struct thread *td, struct linux_unlink_args *args) 604 { 605 char *path; 606 int error; 607 struct stat st; 608 609 LCONVPATHEXIST(td, args->path, &path); 610 611 #ifdef DEBUG 612 if (ldebug(unlink)) 613 printf(ARGS(unlink, "%s"), path); 614 #endif 615 616 error = kern_unlinkat(td, AT_FDCWD, path, UIO_SYSSPACE, 0); 617 if (error == EPERM) { 618 /* Introduce POSIX noncompliant behaviour of Linux */ 619 if (kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &st, 620 NULL) == 0) { 621 if (S_ISDIR(st.st_mode)) 622 error = EISDIR; 623 } 624 } 625 LFREEPATH(path); 626 return (error); 627 } 628 629 int 630 linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args) 631 { 632 char *path; 633 int error, dfd; 634 struct stat st; 635 636 if (args->flag & ~LINUX_AT_REMOVEDIR) 637 return (EINVAL); 638 639 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 640 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd); 641 642 #ifdef DEBUG 643 if (ldebug(unlinkat)) 644 printf(ARGS(unlinkat, "%s"), path); 645 #endif 646 647 if (args->flag & LINUX_AT_REMOVEDIR) 648 error = kern_rmdirat(td, dfd, path, UIO_SYSSPACE); 649 else 650 error = kern_unlinkat(td, dfd, path, UIO_SYSSPACE, 0); 651 if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) { 652 /* Introduce POSIX noncompliant behaviour of Linux */ 653 if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path, 654 UIO_SYSSPACE, &st, NULL) == 0 && S_ISDIR(st.st_mode)) 655 error = EISDIR; 656 } 657 LFREEPATH(path); 658 return (error); 659 } 660 int 661 linux_chdir(struct thread *td, struct linux_chdir_args *args) 662 { 663 char *path; 664 int error; 665 666 LCONVPATHEXIST(td, args->path, &path); 667 668 #ifdef DEBUG 669 if (ldebug(chdir)) 670 printf(ARGS(chdir, "%s"), path); 671 #endif 672 error = kern_chdir(td, path, UIO_SYSSPACE); 673 LFREEPATH(path); 674 return (error); 675 } 676 677 int 678 linux_chmod(struct thread *td, struct linux_chmod_args *args) 679 { 680 char *path; 681 int error; 682 683 LCONVPATHEXIST(td, args->path, &path); 684 685 #ifdef DEBUG 686 if (ldebug(chmod)) 687 printf(ARGS(chmod, "%s, %d"), path, args->mode); 688 #endif 689 error = kern_fchmodat(td, AT_FDCWD, path, UIO_SYSSPACE, 690 args->mode, 0); 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_mkdirat(td, AT_FDCWD, 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_rmdirat(td, AT_FDCWD, 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_renameat(td, AT_FDCWD, from, AT_FDCWD, 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_symlinkat(td, path, AT_FDCWD, 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_readlinkat(td, AT_FDCWD, name, UIO_SYSSPACE, 880 args->buf, UIO_USERSPACE, 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 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 925 int 926 linux_truncate64(struct thread *td, struct linux_truncate64_args *args) 927 { 928 char *path; 929 int error; 930 931 LCONVPATHEXIST(td, args->path, &path); 932 933 #ifdef DEBUG 934 if (ldebug(truncate64)) 935 printf(ARGS(truncate64, "%s, %jd"), path, args->length); 936 #endif 937 938 error = kern_truncate(td, path, UIO_SYSSPACE, args->length); 939 LFREEPATH(path); 940 return (error); 941 } 942 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 943 944 int 945 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args) 946 { 947 struct ftruncate_args /* { 948 int fd; 949 int pad; 950 off_t length; 951 } */ nuap; 952 953 nuap.fd = args->fd; 954 nuap.length = args->length; 955 return (sys_ftruncate(td, &nuap)); 956 } 957 958 int 959 linux_link(struct thread *td, struct linux_link_args *args) 960 { 961 char *path, *to; 962 int error; 963 964 LCONVPATHEXIST(td, args->path, &path); 965 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */ 966 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD); 967 if (to == NULL) { 968 LFREEPATH(path); 969 return (error); 970 } 971 972 #ifdef DEBUG 973 if (ldebug(link)) 974 printf(ARGS(link, "%s, %s"), path, to); 975 #endif 976 error = kern_linkat(td, AT_FDCWD, AT_FDCWD, path, to, UIO_SYSSPACE, 977 FOLLOW); 978 LFREEPATH(path); 979 LFREEPATH(to); 980 return (error); 981 } 982 983 int 984 linux_linkat(struct thread *td, struct linux_linkat_args *args) 985 { 986 char *path, *to; 987 int error, olddfd, newdfd, follow; 988 989 if (args->flag & ~LINUX_AT_SYMLINK_FOLLOW) 990 return (EINVAL); 991 992 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd; 993 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd; 994 LCONVPATHEXIST_AT(td, args->oldname, &path, olddfd); 995 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */ 996 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd); 997 if (to == NULL) { 998 LFREEPATH(path); 999 return (error); 1000 } 1001 1002 #ifdef DEBUG 1003 if (ldebug(linkat)) 1004 printf(ARGS(linkat, "%i, %s, %i, %s, %i"), args->olddfd, path, 1005 args->newdfd, to, args->flag); 1006 #endif 1007 1008 follow = (args->flag & LINUX_AT_SYMLINK_FOLLOW) == 0 ? NOFOLLOW : 1009 FOLLOW; 1010 error = kern_linkat(td, olddfd, newdfd, path, to, UIO_SYSSPACE, follow); 1011 LFREEPATH(path); 1012 LFREEPATH(to); 1013 return (error); 1014 } 1015 1016 int 1017 linux_fdatasync(td, uap) 1018 struct thread *td; 1019 struct linux_fdatasync_args *uap; 1020 { 1021 struct fsync_args bsd; 1022 1023 bsd.fd = uap->fd; 1024 return sys_fsync(td, &bsd); 1025 } 1026 1027 int 1028 linux_pread(td, uap) 1029 struct thread *td; 1030 struct linux_pread_args *uap; 1031 { 1032 struct pread_args bsd; 1033 cap_rights_t rights; 1034 struct vnode *vp; 1035 int error; 1036 1037 bsd.fd = uap->fd; 1038 bsd.buf = uap->buf; 1039 bsd.nbyte = uap->nbyte; 1040 bsd.offset = uap->offset; 1041 1042 error = sys_pread(td, &bsd); 1043 1044 if (error == 0) { 1045 /* This seems to violate POSIX but linux does it */ 1046 error = fgetvp(td, uap->fd, 1047 cap_rights_init(&rights, CAP_PREAD), &vp); 1048 if (error != 0) 1049 return (error); 1050 if (vp->v_type == VDIR) { 1051 vrele(vp); 1052 return (EISDIR); 1053 } 1054 vrele(vp); 1055 } 1056 1057 return (error); 1058 } 1059 1060 int 1061 linux_pwrite(td, uap) 1062 struct thread *td; 1063 struct linux_pwrite_args *uap; 1064 { 1065 struct pwrite_args bsd; 1066 1067 bsd.fd = uap->fd; 1068 bsd.buf = uap->buf; 1069 bsd.nbyte = uap->nbyte; 1070 bsd.offset = uap->offset; 1071 return sys_pwrite(td, &bsd); 1072 } 1073 1074 int 1075 linux_mount(struct thread *td, struct linux_mount_args *args) 1076 { 1077 char fstypename[MFSNAMELEN]; 1078 char mntonname[MNAMELEN], mntfromname[MNAMELEN]; 1079 int error; 1080 int fsflags; 1081 1082 error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1, 1083 NULL); 1084 if (error) 1085 return (error); 1086 error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL); 1087 if (error) 1088 return (error); 1089 error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL); 1090 if (error) 1091 return (error); 1092 1093 #ifdef DEBUG 1094 if (ldebug(mount)) 1095 printf(ARGS(mount, "%s, %s, %s"), 1096 fstypename, mntfromname, mntonname); 1097 #endif 1098 1099 if (strcmp(fstypename, "ext2") == 0) { 1100 strcpy(fstypename, "ext2fs"); 1101 } else if (strcmp(fstypename, "proc") == 0) { 1102 strcpy(fstypename, "linprocfs"); 1103 } else if (strcmp(fstypename, "vfat") == 0) { 1104 strcpy(fstypename, "msdosfs"); 1105 } 1106 1107 fsflags = 0; 1108 1109 if ((args->rwflag & 0xffff0000) == 0xc0ed0000) { 1110 /* 1111 * Linux SYNC flag is not included; the closest equivalent 1112 * FreeBSD has is !ASYNC, which is our default. 1113 */ 1114 if (args->rwflag & LINUX_MS_RDONLY) 1115 fsflags |= MNT_RDONLY; 1116 if (args->rwflag & LINUX_MS_NOSUID) 1117 fsflags |= MNT_NOSUID; 1118 if (args->rwflag & LINUX_MS_NOEXEC) 1119 fsflags |= MNT_NOEXEC; 1120 if (args->rwflag & LINUX_MS_REMOUNT) 1121 fsflags |= MNT_UPDATE; 1122 } 1123 1124 error = kernel_vmount(fsflags, 1125 "fstype", fstypename, 1126 "fspath", mntonname, 1127 "from", mntfromname, 1128 NULL); 1129 return (error); 1130 } 1131 1132 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1133 int 1134 linux_oldumount(struct thread *td, struct linux_oldumount_args *args) 1135 { 1136 struct linux_umount_args args2; 1137 1138 args2.path = args->path; 1139 args2.flags = 0; 1140 return (linux_umount(td, &args2)); 1141 } 1142 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1143 1144 int 1145 linux_umount(struct thread *td, struct linux_umount_args *args) 1146 { 1147 struct unmount_args bsd; 1148 1149 bsd.path = args->path; 1150 bsd.flags = args->flags; /* XXX correct? */ 1151 return (sys_unmount(td, &bsd)); 1152 } 1153 1154 /* 1155 * fcntl family of syscalls 1156 */ 1157 1158 struct l_flock { 1159 l_short l_type; 1160 l_short l_whence; 1161 l_off_t l_start; 1162 l_off_t l_len; 1163 l_pid_t l_pid; 1164 } 1165 #if defined(__amd64__) && defined(COMPAT_LINUX32) 1166 __packed 1167 #endif 1168 ; 1169 1170 static void 1171 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock) 1172 { 1173 switch (linux_flock->l_type) { 1174 case LINUX_F_RDLCK: 1175 bsd_flock->l_type = F_RDLCK; 1176 break; 1177 case LINUX_F_WRLCK: 1178 bsd_flock->l_type = F_WRLCK; 1179 break; 1180 case LINUX_F_UNLCK: 1181 bsd_flock->l_type = F_UNLCK; 1182 break; 1183 default: 1184 bsd_flock->l_type = -1; 1185 break; 1186 } 1187 bsd_flock->l_whence = linux_flock->l_whence; 1188 bsd_flock->l_start = (off_t)linux_flock->l_start; 1189 bsd_flock->l_len = (off_t)linux_flock->l_len; 1190 bsd_flock->l_pid = (pid_t)linux_flock->l_pid; 1191 bsd_flock->l_sysid = 0; 1192 } 1193 1194 static void 1195 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock) 1196 { 1197 switch (bsd_flock->l_type) { 1198 case F_RDLCK: 1199 linux_flock->l_type = LINUX_F_RDLCK; 1200 break; 1201 case F_WRLCK: 1202 linux_flock->l_type = LINUX_F_WRLCK; 1203 break; 1204 case F_UNLCK: 1205 linux_flock->l_type = LINUX_F_UNLCK; 1206 break; 1207 } 1208 linux_flock->l_whence = bsd_flock->l_whence; 1209 linux_flock->l_start = (l_off_t)bsd_flock->l_start; 1210 linux_flock->l_len = (l_off_t)bsd_flock->l_len; 1211 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid; 1212 } 1213 1214 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1215 struct l_flock64 { 1216 l_short l_type; 1217 l_short l_whence; 1218 l_loff_t l_start; 1219 l_loff_t l_len; 1220 l_pid_t l_pid; 1221 } 1222 #if defined(__amd64__) && defined(COMPAT_LINUX32) 1223 __packed 1224 #endif 1225 ; 1226 1227 static void 1228 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock) 1229 { 1230 switch (linux_flock->l_type) { 1231 case LINUX_F_RDLCK: 1232 bsd_flock->l_type = F_RDLCK; 1233 break; 1234 case LINUX_F_WRLCK: 1235 bsd_flock->l_type = F_WRLCK; 1236 break; 1237 case LINUX_F_UNLCK: 1238 bsd_flock->l_type = F_UNLCK; 1239 break; 1240 default: 1241 bsd_flock->l_type = -1; 1242 break; 1243 } 1244 bsd_flock->l_whence = linux_flock->l_whence; 1245 bsd_flock->l_start = (off_t)linux_flock->l_start; 1246 bsd_flock->l_len = (off_t)linux_flock->l_len; 1247 bsd_flock->l_pid = (pid_t)linux_flock->l_pid; 1248 bsd_flock->l_sysid = 0; 1249 } 1250 1251 static void 1252 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock) 1253 { 1254 switch (bsd_flock->l_type) { 1255 case F_RDLCK: 1256 linux_flock->l_type = LINUX_F_RDLCK; 1257 break; 1258 case F_WRLCK: 1259 linux_flock->l_type = LINUX_F_WRLCK; 1260 break; 1261 case F_UNLCK: 1262 linux_flock->l_type = LINUX_F_UNLCK; 1263 break; 1264 } 1265 linux_flock->l_whence = bsd_flock->l_whence; 1266 linux_flock->l_start = (l_loff_t)bsd_flock->l_start; 1267 linux_flock->l_len = (l_loff_t)bsd_flock->l_len; 1268 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid; 1269 } 1270 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1271 1272 static int 1273 fcntl_common(struct thread *td, struct linux_fcntl_args *args) 1274 { 1275 struct l_flock linux_flock; 1276 struct flock bsd_flock; 1277 cap_rights_t rights; 1278 struct file *fp; 1279 long arg; 1280 int error, result; 1281 1282 switch (args->cmd) { 1283 case LINUX_F_DUPFD: 1284 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg)); 1285 1286 case LINUX_F_GETFD: 1287 return (kern_fcntl(td, args->fd, F_GETFD, 0)); 1288 1289 case LINUX_F_SETFD: 1290 return (kern_fcntl(td, args->fd, F_SETFD, args->arg)); 1291 1292 case LINUX_F_GETFL: 1293 error = kern_fcntl(td, args->fd, F_GETFL, 0); 1294 result = td->td_retval[0]; 1295 td->td_retval[0] = 0; 1296 if (result & O_RDONLY) 1297 td->td_retval[0] |= LINUX_O_RDONLY; 1298 if (result & O_WRONLY) 1299 td->td_retval[0] |= LINUX_O_WRONLY; 1300 if (result & O_RDWR) 1301 td->td_retval[0] |= LINUX_O_RDWR; 1302 if (result & O_NDELAY) 1303 td->td_retval[0] |= LINUX_O_NONBLOCK; 1304 if (result & O_APPEND) 1305 td->td_retval[0] |= LINUX_O_APPEND; 1306 if (result & O_FSYNC) 1307 td->td_retval[0] |= LINUX_O_SYNC; 1308 if (result & O_ASYNC) 1309 td->td_retval[0] |= LINUX_FASYNC; 1310 #ifdef LINUX_O_NOFOLLOW 1311 if (result & O_NOFOLLOW) 1312 td->td_retval[0] |= LINUX_O_NOFOLLOW; 1313 #endif 1314 #ifdef LINUX_O_DIRECT 1315 if (result & O_DIRECT) 1316 td->td_retval[0] |= LINUX_O_DIRECT; 1317 #endif 1318 return (error); 1319 1320 case LINUX_F_SETFL: 1321 arg = 0; 1322 if (args->arg & LINUX_O_NDELAY) 1323 arg |= O_NONBLOCK; 1324 if (args->arg & LINUX_O_APPEND) 1325 arg |= O_APPEND; 1326 if (args->arg & LINUX_O_SYNC) 1327 arg |= O_FSYNC; 1328 if (args->arg & LINUX_FASYNC) 1329 arg |= O_ASYNC; 1330 #ifdef LINUX_O_NOFOLLOW 1331 if (args->arg & LINUX_O_NOFOLLOW) 1332 arg |= O_NOFOLLOW; 1333 #endif 1334 #ifdef LINUX_O_DIRECT 1335 if (args->arg & LINUX_O_DIRECT) 1336 arg |= O_DIRECT; 1337 #endif 1338 return (kern_fcntl(td, args->fd, F_SETFL, arg)); 1339 1340 case LINUX_F_GETLK: 1341 error = copyin((void *)args->arg, &linux_flock, 1342 sizeof(linux_flock)); 1343 if (error) 1344 return (error); 1345 linux_to_bsd_flock(&linux_flock, &bsd_flock); 1346 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock); 1347 if (error) 1348 return (error); 1349 bsd_to_linux_flock(&bsd_flock, &linux_flock); 1350 return (copyout(&linux_flock, (void *)args->arg, 1351 sizeof(linux_flock))); 1352 1353 case LINUX_F_SETLK: 1354 error = copyin((void *)args->arg, &linux_flock, 1355 sizeof(linux_flock)); 1356 if (error) 1357 return (error); 1358 linux_to_bsd_flock(&linux_flock, &bsd_flock); 1359 return (kern_fcntl(td, args->fd, F_SETLK, 1360 (intptr_t)&bsd_flock)); 1361 1362 case LINUX_F_SETLKW: 1363 error = copyin((void *)args->arg, &linux_flock, 1364 sizeof(linux_flock)); 1365 if (error) 1366 return (error); 1367 linux_to_bsd_flock(&linux_flock, &bsd_flock); 1368 return (kern_fcntl(td, args->fd, F_SETLKW, 1369 (intptr_t)&bsd_flock)); 1370 1371 case LINUX_F_GETOWN: 1372 return (kern_fcntl(td, args->fd, F_GETOWN, 0)); 1373 1374 case LINUX_F_SETOWN: 1375 /* 1376 * XXX some Linux applications depend on F_SETOWN having no 1377 * significant effect for pipes (SIGIO is not delivered for 1378 * pipes under Linux-2.2.35 at least). 1379 */ 1380 error = fget(td, args->fd, 1381 cap_rights_init(&rights, CAP_FCNTL), &fp); 1382 if (error) 1383 return (error); 1384 if (fp->f_type == DTYPE_PIPE) { 1385 fdrop(fp, td); 1386 return (EINVAL); 1387 } 1388 fdrop(fp, td); 1389 1390 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg)); 1391 1392 case LINUX_F_DUPFD_CLOEXEC: 1393 return (kern_fcntl(td, args->fd, F_DUPFD_CLOEXEC, args->arg)); 1394 } 1395 1396 return (EINVAL); 1397 } 1398 1399 int 1400 linux_fcntl(struct thread *td, struct linux_fcntl_args *args) 1401 { 1402 1403 #ifdef DEBUG 1404 if (ldebug(fcntl)) 1405 printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd); 1406 #endif 1407 1408 return (fcntl_common(td, args)); 1409 } 1410 1411 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1412 int 1413 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args) 1414 { 1415 struct l_flock64 linux_flock; 1416 struct flock bsd_flock; 1417 struct linux_fcntl_args fcntl_args; 1418 int error; 1419 1420 #ifdef DEBUG 1421 if (ldebug(fcntl64)) 1422 printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd); 1423 #endif 1424 1425 switch (args->cmd) { 1426 case LINUX_F_GETLK64: 1427 error = copyin((void *)args->arg, &linux_flock, 1428 sizeof(linux_flock)); 1429 if (error) 1430 return (error); 1431 linux_to_bsd_flock64(&linux_flock, &bsd_flock); 1432 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock); 1433 if (error) 1434 return (error); 1435 bsd_to_linux_flock64(&bsd_flock, &linux_flock); 1436 return (copyout(&linux_flock, (void *)args->arg, 1437 sizeof(linux_flock))); 1438 1439 case LINUX_F_SETLK64: 1440 error = copyin((void *)args->arg, &linux_flock, 1441 sizeof(linux_flock)); 1442 if (error) 1443 return (error); 1444 linux_to_bsd_flock64(&linux_flock, &bsd_flock); 1445 return (kern_fcntl(td, args->fd, F_SETLK, 1446 (intptr_t)&bsd_flock)); 1447 1448 case LINUX_F_SETLKW64: 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_SETLKW, 1455 (intptr_t)&bsd_flock)); 1456 } 1457 1458 fcntl_args.fd = args->fd; 1459 fcntl_args.cmd = args->cmd; 1460 fcntl_args.arg = args->arg; 1461 return (fcntl_common(td, &fcntl_args)); 1462 } 1463 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1464 1465 int 1466 linux_chown(struct thread *td, struct linux_chown_args *args) 1467 { 1468 char *path; 1469 int error; 1470 1471 LCONVPATHEXIST(td, args->path, &path); 1472 1473 #ifdef DEBUG 1474 if (ldebug(chown)) 1475 printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid); 1476 #endif 1477 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid, 1478 args->gid, 0); 1479 LFREEPATH(path); 1480 return (error); 1481 } 1482 1483 int 1484 linux_fchownat(struct thread *td, struct linux_fchownat_args *args) 1485 { 1486 char *path; 1487 int error, dfd, flag; 1488 1489 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW) 1490 return (EINVAL); 1491 1492 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 1493 LCONVPATHEXIST_AT(td, args->filename, &path, dfd); 1494 1495 #ifdef DEBUG 1496 if (ldebug(fchownat)) 1497 printf(ARGS(fchownat, "%s, %d, %d"), path, args->uid, args->gid); 1498 #endif 1499 1500 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 : 1501 AT_SYMLINK_NOFOLLOW; 1502 error = kern_fchownat(td, dfd, path, UIO_SYSSPACE, args->uid, args->gid, 1503 flag); 1504 LFREEPATH(path); 1505 return (error); 1506 } 1507 1508 int 1509 linux_lchown(struct thread *td, struct linux_lchown_args *args) 1510 { 1511 char *path; 1512 int error; 1513 1514 LCONVPATHEXIST(td, args->path, &path); 1515 1516 #ifdef DEBUG 1517 if (ldebug(lchown)) 1518 printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid); 1519 #endif 1520 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid, 1521 args->gid, AT_SYMLINK_NOFOLLOW); 1522 LFREEPATH(path); 1523 return (error); 1524 } 1525 1526 static int 1527 convert_fadvice(int advice) 1528 { 1529 switch (advice) { 1530 case LINUX_POSIX_FADV_NORMAL: 1531 return (POSIX_FADV_NORMAL); 1532 case LINUX_POSIX_FADV_RANDOM: 1533 return (POSIX_FADV_RANDOM); 1534 case LINUX_POSIX_FADV_SEQUENTIAL: 1535 return (POSIX_FADV_SEQUENTIAL); 1536 case LINUX_POSIX_FADV_WILLNEED: 1537 return (POSIX_FADV_WILLNEED); 1538 case LINUX_POSIX_FADV_DONTNEED: 1539 return (POSIX_FADV_DONTNEED); 1540 case LINUX_POSIX_FADV_NOREUSE: 1541 return (POSIX_FADV_NOREUSE); 1542 default: 1543 return (-1); 1544 } 1545 } 1546 1547 int 1548 linux_fadvise64(struct thread *td, struct linux_fadvise64_args *args) 1549 { 1550 int advice; 1551 1552 advice = convert_fadvice(args->advice); 1553 if (advice == -1) 1554 return (EINVAL); 1555 return (kern_posix_fadvise(td, args->fd, args->offset, args->len, 1556 advice)); 1557 } 1558 1559 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1560 int 1561 linux_fadvise64_64(struct thread *td, struct linux_fadvise64_64_args *args) 1562 { 1563 int advice; 1564 1565 advice = convert_fadvice(args->advice); 1566 if (advice == -1) 1567 return (EINVAL); 1568 return (kern_posix_fadvise(td, args->fd, args->offset, args->len, 1569 advice)); 1570 } 1571 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1572 1573 int 1574 linux_pipe(struct thread *td, struct linux_pipe_args *args) 1575 { 1576 int fildes[2]; 1577 int error; 1578 1579 #ifdef DEBUG 1580 if (ldebug(pipe)) 1581 printf(ARGS(pipe, "*")); 1582 #endif 1583 1584 error = kern_pipe(td, fildes, 0, NULL, NULL); 1585 if (error) 1586 return (error); 1587 1588 /* XXX: Close descriptors on error. */ 1589 return (copyout(fildes, args->pipefds, sizeof(fildes))); 1590 } 1591 1592 int 1593 linux_pipe2(struct thread *td, struct linux_pipe2_args *args) 1594 { 1595 int fildes[2]; 1596 int error, flags; 1597 1598 #ifdef DEBUG 1599 if (ldebug(pipe2)) 1600 printf(ARGS(pipe2, "*, %d"), args->flags); 1601 #endif 1602 1603 if ((args->flags & ~(LINUX_O_NONBLOCK | LINUX_O_CLOEXEC)) != 0) 1604 return (EINVAL); 1605 1606 flags = 0; 1607 if ((args->flags & LINUX_O_NONBLOCK) != 0) 1608 flags |= O_NONBLOCK; 1609 if ((args->flags & LINUX_O_CLOEXEC) != 0) 1610 flags |= O_CLOEXEC; 1611 error = kern_pipe(td, fildes, flags, NULL, NULL); 1612 if (error) 1613 return (error); 1614 1615 /* XXX: Close descriptors on error. */ 1616 return (copyout(fildes, args->pipefds, sizeof(fildes))); 1617 } 1618 1619 int 1620 linux_dup3(struct thread *td, struct linux_dup3_args *args) 1621 { 1622 int cmd; 1623 intptr_t newfd; 1624 1625 if (args->oldfd == args->newfd) 1626 return (EINVAL); 1627 if ((args->flags & ~LINUX_O_CLOEXEC) != 0) 1628 return (EINVAL); 1629 if (args->flags & LINUX_O_CLOEXEC) 1630 cmd = F_DUP2FD_CLOEXEC; 1631 else 1632 cmd = F_DUP2FD; 1633 1634 newfd = args->newfd; 1635 return (kern_fcntl(td, args->oldfd, cmd, newfd)); 1636 } 1637 1638 int 1639 linux_fallocate(struct thread *td, struct linux_fallocate_args *args) 1640 { 1641 1642 /* 1643 * We emulate only posix_fallocate system call for which 1644 * mode should be 0. 1645 */ 1646 if (args->mode != 0) 1647 return (ENOSYS); 1648 1649 return (kern_posix_fallocate(td, args->fd, args->offset, 1650 args->len)); 1651 } 1652