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), \ 309 sizeof(l_ulong)) 310 311 #define LINUX_RECLEN64(namlen) \ 312 roundup((offsetof(struct l_dirent64, d_name) + (namlen) + 1), \ 313 sizeof(uint64_t)) 314 315 #define LINUX_MAXRECLEN max(LINUX_RECLEN(LINUX_NAME_MAX), \ 316 LINUX_RECLEN64(LINUX_NAME_MAX)) 317 #define LINUX_DIRBLKSIZ 512 318 319 static int 320 getdents_common(struct thread *td, struct linux_getdents64_args *args, 321 int is64bit) 322 { 323 struct dirent *bdp; 324 struct vnode *vp; 325 caddr_t inp, buf; /* BSD-format */ 326 int len, reclen; /* BSD-format */ 327 caddr_t outp; /* Linux-format */ 328 int resid, linuxreclen=0; /* Linux-format */ 329 caddr_t lbuf; /* Linux-format */ 330 cap_rights_t rights; 331 struct file *fp; 332 struct uio auio; 333 struct iovec aiov; 334 off_t off; 335 struct l_dirent *linux_dirent; 336 struct l_dirent64 *linux_dirent64; 337 int buflen, error, eofflag, nbytes, justone; 338 u_long *cookies = NULL, *cookiep; 339 int ncookies; 340 341 nbytes = args->count; 342 if (nbytes == 1) { 343 /* readdir(2) case. Always struct dirent. */ 344 if (is64bit) 345 return (EINVAL); 346 nbytes = sizeof(*linux_dirent); 347 justone = 1; 348 } else 349 justone = 0; 350 351 error = getvnode(td->td_proc->p_fd, args->fd, 352 cap_rights_init(&rights, CAP_READ), &fp); 353 if (error != 0) 354 return (error); 355 356 if ((fp->f_flag & FREAD) == 0) { 357 fdrop(fp, td); 358 return (EBADF); 359 } 360 361 off = foffset_lock(fp, 0); 362 vp = fp->f_vnode; 363 if (vp->v_type != VDIR) { 364 foffset_unlock(fp, off, 0); 365 fdrop(fp, td); 366 return (EINVAL); 367 } 368 369 370 buflen = max(LINUX_DIRBLKSIZ, nbytes); 371 buflen = min(buflen, MAXBSIZE); 372 buf = malloc(buflen, M_LINUX, M_WAITOK); 373 lbuf = malloc(LINUX_MAXRECLEN, M_LINUX, M_WAITOK | M_ZERO); 374 vn_lock(vp, LK_SHARED | LK_RETRY); 375 376 aiov.iov_base = buf; 377 aiov.iov_len = buflen; 378 auio.uio_iov = &aiov; 379 auio.uio_iovcnt = 1; 380 auio.uio_rw = UIO_READ; 381 auio.uio_segflg = UIO_SYSSPACE; 382 auio.uio_td = td; 383 auio.uio_resid = buflen; 384 auio.uio_offset = off; 385 386 #ifdef MAC 387 /* 388 * Do directory search MAC check using non-cached credentials. 389 */ 390 if ((error = mac_vnode_check_readdir(td->td_ucred, vp))) 391 goto out; 392 #endif /* MAC */ 393 if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies, 394 &cookies))) 395 goto out; 396 397 inp = buf; 398 outp = (caddr_t)args->dirent; 399 resid = nbytes; 400 if ((len = buflen - auio.uio_resid) <= 0) 401 goto eof; 402 403 cookiep = cookies; 404 405 if (cookies) { 406 /* 407 * When using cookies, the vfs has the option of reading from 408 * a different offset than that supplied (UFS truncates the 409 * offset to a block boundary to make sure that it never reads 410 * partway through a directory entry, even if the directory 411 * has been compacted). 412 */ 413 while (len > 0 && ncookies > 0 && *cookiep <= off) { 414 bdp = (struct dirent *) inp; 415 len -= bdp->d_reclen; 416 inp += bdp->d_reclen; 417 cookiep++; 418 ncookies--; 419 } 420 } 421 422 while (len > 0) { 423 if (cookiep && ncookies == 0) 424 break; 425 bdp = (struct dirent *) inp; 426 reclen = bdp->d_reclen; 427 if (reclen & 3) { 428 error = EFAULT; 429 goto out; 430 } 431 432 if (bdp->d_fileno == 0) { 433 inp += reclen; 434 if (cookiep) { 435 off = *cookiep++; 436 ncookies--; 437 } else 438 off += reclen; 439 440 len -= reclen; 441 continue; 442 } 443 444 linuxreclen = (is64bit) 445 ? LINUX_RECLEN64(bdp->d_namlen) 446 : LINUX_RECLEN(bdp->d_namlen); 447 448 if (reclen > len || resid < linuxreclen) { 449 outp++; 450 break; 451 } 452 453 if (justone) { 454 /* readdir(2) case. */ 455 linux_dirent = (struct l_dirent*)lbuf; 456 linux_dirent->d_ino = bdp->d_fileno; 457 linux_dirent->d_off = (l_off_t)linuxreclen; 458 linux_dirent->d_reclen = (l_ushort)bdp->d_namlen; 459 strlcpy(linux_dirent->d_name, bdp->d_name, 460 linuxreclen - offsetof(struct l_dirent, d_name)); 461 error = copyout(linux_dirent, outp, linuxreclen); 462 } 463 if (is64bit) { 464 linux_dirent64 = (struct l_dirent64*)lbuf; 465 linux_dirent64->d_ino = bdp->d_fileno; 466 linux_dirent64->d_off = (cookiep) 467 ? (l_off_t)*cookiep 468 : (l_off_t)(off + reclen); 469 linux_dirent64->d_reclen = (l_ushort)linuxreclen; 470 linux_dirent64->d_type = bdp->d_type; 471 strlcpy(linux_dirent64->d_name, bdp->d_name, 472 linuxreclen - offsetof(struct l_dirent64, d_name)); 473 error = copyout(linux_dirent64, outp, linuxreclen); 474 } else if (!justone) { 475 linux_dirent = (struct l_dirent*)lbuf; 476 linux_dirent->d_ino = bdp->d_fileno; 477 linux_dirent->d_off = (cookiep) 478 ? (l_off_t)*cookiep 479 : (l_off_t)(off + reclen); 480 linux_dirent->d_reclen = (l_ushort)linuxreclen; 481 /* 482 * Copy d_type to last byte of l_dirent buffer 483 */ 484 lbuf[linuxreclen-1] = bdp->d_type; 485 strlcpy(linux_dirent->d_name, bdp->d_name, 486 linuxreclen - offsetof(struct l_dirent, d_name)-1); 487 error = copyout(linux_dirent, outp, linuxreclen); 488 } 489 490 if (error) 491 goto out; 492 493 inp += reclen; 494 if (cookiep) { 495 off = *cookiep++; 496 ncookies--; 497 } else 498 off += reclen; 499 500 outp += linuxreclen; 501 resid -= linuxreclen; 502 len -= reclen; 503 if (justone) 504 break; 505 } 506 507 if (outp == (caddr_t)args->dirent) { 508 nbytes = resid; 509 goto eof; 510 } 511 512 if (justone) 513 nbytes = resid + linuxreclen; 514 515 eof: 516 td->td_retval[0] = nbytes - resid; 517 518 out: 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_LINUX); 525 free(lbuf, M_LINUX); 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_accessat(td, AT_FDCWD, path, UIO_SYSSPACE, 0, 574 args->amode); 575 LFREEPATH(path); 576 577 return (error); 578 } 579 580 int 581 linux_faccessat(struct thread *td, struct linux_faccessat_args *args) 582 { 583 char *path; 584 int error, dfd; 585 586 /* linux convention */ 587 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK)) 588 return (EINVAL); 589 590 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 591 LCONVPATHEXIST_AT(td, args->filename, &path, dfd); 592 593 #ifdef DEBUG 594 if (ldebug(access)) 595 printf(ARGS(access, "%s, %d"), path, args->amode); 596 #endif 597 598 error = kern_accessat(td, dfd, path, UIO_SYSSPACE, 0, args->amode); 599 LFREEPATH(path); 600 601 return (error); 602 } 603 604 int 605 linux_unlink(struct thread *td, struct linux_unlink_args *args) 606 { 607 char *path; 608 int error; 609 struct stat st; 610 611 LCONVPATHEXIST(td, args->path, &path); 612 613 #ifdef DEBUG 614 if (ldebug(unlink)) 615 printf(ARGS(unlink, "%s"), path); 616 #endif 617 618 error = kern_unlinkat(td, AT_FDCWD, path, UIO_SYSSPACE, 0); 619 if (error == EPERM) { 620 /* Introduce POSIX noncompliant behaviour of Linux */ 621 if (kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &st, 622 NULL) == 0) { 623 if (S_ISDIR(st.st_mode)) 624 error = EISDIR; 625 } 626 } 627 LFREEPATH(path); 628 return (error); 629 } 630 631 int 632 linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args) 633 { 634 char *path; 635 int error, dfd; 636 struct stat st; 637 638 if (args->flag & ~LINUX_AT_REMOVEDIR) 639 return (EINVAL); 640 641 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 642 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd); 643 644 #ifdef DEBUG 645 if (ldebug(unlinkat)) 646 printf(ARGS(unlinkat, "%s"), path); 647 #endif 648 649 if (args->flag & LINUX_AT_REMOVEDIR) 650 error = kern_rmdirat(td, dfd, path, UIO_SYSSPACE); 651 else 652 error = kern_unlinkat(td, dfd, path, UIO_SYSSPACE, 0); 653 if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) { 654 /* Introduce POSIX noncompliant behaviour of Linux */ 655 if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path, 656 UIO_SYSSPACE, &st, NULL) == 0 && S_ISDIR(st.st_mode)) 657 error = EISDIR; 658 } 659 LFREEPATH(path); 660 return (error); 661 } 662 int 663 linux_chdir(struct thread *td, struct linux_chdir_args *args) 664 { 665 char *path; 666 int error; 667 668 LCONVPATHEXIST(td, args->path, &path); 669 670 #ifdef DEBUG 671 if (ldebug(chdir)) 672 printf(ARGS(chdir, "%s"), path); 673 #endif 674 error = kern_chdir(td, path, UIO_SYSSPACE); 675 LFREEPATH(path); 676 return (error); 677 } 678 679 int 680 linux_chmod(struct thread *td, struct linux_chmod_args *args) 681 { 682 char *path; 683 int error; 684 685 LCONVPATHEXIST(td, args->path, &path); 686 687 #ifdef DEBUG 688 if (ldebug(chmod)) 689 printf(ARGS(chmod, "%s, %d"), path, args->mode); 690 #endif 691 error = kern_fchmodat(td, AT_FDCWD, path, UIO_SYSSPACE, 692 args->mode, 0); 693 LFREEPATH(path); 694 return (error); 695 } 696 697 int 698 linux_fchmodat(struct thread *td, struct linux_fchmodat_args *args) 699 { 700 char *path; 701 int error, dfd; 702 703 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 704 LCONVPATHEXIST_AT(td, args->filename, &path, dfd); 705 706 #ifdef DEBUG 707 if (ldebug(fchmodat)) 708 printf(ARGS(fchmodat, "%s, %d"), path, args->mode); 709 #endif 710 711 error = kern_fchmodat(td, dfd, path, UIO_SYSSPACE, args->mode, 0); 712 LFREEPATH(path); 713 return (error); 714 } 715 716 int 717 linux_mkdir(struct thread *td, struct linux_mkdir_args *args) 718 { 719 char *path; 720 int error; 721 722 LCONVPATHCREAT(td, args->path, &path); 723 724 #ifdef DEBUG 725 if (ldebug(mkdir)) 726 printf(ARGS(mkdir, "%s, %d"), path, args->mode); 727 #endif 728 error = kern_mkdirat(td, AT_FDCWD, path, UIO_SYSSPACE, args->mode); 729 LFREEPATH(path); 730 return (error); 731 } 732 733 int 734 linux_mkdirat(struct thread *td, struct linux_mkdirat_args *args) 735 { 736 char *path; 737 int error, dfd; 738 739 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 740 LCONVPATHCREAT_AT(td, args->pathname, &path, dfd); 741 742 #ifdef DEBUG 743 if (ldebug(mkdirat)) 744 printf(ARGS(mkdirat, "%s, %d"), path, args->mode); 745 #endif 746 error = kern_mkdirat(td, dfd, path, UIO_SYSSPACE, args->mode); 747 LFREEPATH(path); 748 return (error); 749 } 750 751 int 752 linux_rmdir(struct thread *td, struct linux_rmdir_args *args) 753 { 754 char *path; 755 int error; 756 757 LCONVPATHEXIST(td, args->path, &path); 758 759 #ifdef DEBUG 760 if (ldebug(rmdir)) 761 printf(ARGS(rmdir, "%s"), path); 762 #endif 763 error = kern_rmdirat(td, AT_FDCWD, path, UIO_SYSSPACE); 764 LFREEPATH(path); 765 return (error); 766 } 767 768 int 769 linux_rename(struct thread *td, struct linux_rename_args *args) 770 { 771 char *from, *to; 772 int error; 773 774 LCONVPATHEXIST(td, args->from, &from); 775 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */ 776 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD); 777 if (to == NULL) { 778 LFREEPATH(from); 779 return (error); 780 } 781 782 #ifdef DEBUG 783 if (ldebug(rename)) 784 printf(ARGS(rename, "%s, %s"), from, to); 785 #endif 786 error = kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, UIO_SYSSPACE); 787 LFREEPATH(from); 788 LFREEPATH(to); 789 return (error); 790 } 791 792 int 793 linux_renameat(struct thread *td, struct linux_renameat_args *args) 794 { 795 char *from, *to; 796 int error, olddfd, newdfd; 797 798 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd; 799 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd; 800 LCONVPATHEXIST_AT(td, args->oldname, &from, olddfd); 801 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */ 802 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd); 803 if (to == NULL) { 804 LFREEPATH(from); 805 return (error); 806 } 807 808 #ifdef DEBUG 809 if (ldebug(renameat)) 810 printf(ARGS(renameat, "%s, %s"), from, to); 811 #endif 812 error = kern_renameat(td, olddfd, from, newdfd, to, UIO_SYSSPACE); 813 LFREEPATH(from); 814 LFREEPATH(to); 815 return (error); 816 } 817 818 int 819 linux_symlink(struct thread *td, struct linux_symlink_args *args) 820 { 821 char *path, *to; 822 int error; 823 824 LCONVPATHEXIST(td, args->path, &path); 825 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */ 826 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD); 827 if (to == NULL) { 828 LFREEPATH(path); 829 return (error); 830 } 831 832 #ifdef DEBUG 833 if (ldebug(symlink)) 834 printf(ARGS(symlink, "%s, %s"), path, to); 835 #endif 836 error = kern_symlinkat(td, path, AT_FDCWD, to, UIO_SYSSPACE); 837 LFREEPATH(path); 838 LFREEPATH(to); 839 return (error); 840 } 841 842 int 843 linux_symlinkat(struct thread *td, struct linux_symlinkat_args *args) 844 { 845 char *path, *to; 846 int error, dfd; 847 848 dfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd; 849 LCONVPATHEXIST_AT(td, args->oldname, &path, dfd); 850 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */ 851 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, dfd); 852 if (to == NULL) { 853 LFREEPATH(path); 854 return (error); 855 } 856 857 #ifdef DEBUG 858 if (ldebug(symlinkat)) 859 printf(ARGS(symlinkat, "%s, %s"), path, to); 860 #endif 861 862 error = kern_symlinkat(td, path, dfd, to, UIO_SYSSPACE); 863 LFREEPATH(path); 864 LFREEPATH(to); 865 return (error); 866 } 867 868 int 869 linux_readlink(struct thread *td, struct linux_readlink_args *args) 870 { 871 char *name; 872 int error; 873 874 LCONVPATHEXIST(td, args->name, &name); 875 876 #ifdef DEBUG 877 if (ldebug(readlink)) 878 printf(ARGS(readlink, "%s, %p, %d"), name, (void *)args->buf, 879 args->count); 880 #endif 881 error = kern_readlinkat(td, AT_FDCWD, name, UIO_SYSSPACE, 882 args->buf, UIO_USERSPACE, args->count); 883 LFREEPATH(name); 884 return (error); 885 } 886 887 int 888 linux_readlinkat(struct thread *td, struct linux_readlinkat_args *args) 889 { 890 char *name; 891 int error, dfd; 892 893 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 894 LCONVPATHEXIST_AT(td, args->path, &name, dfd); 895 896 #ifdef DEBUG 897 if (ldebug(readlinkat)) 898 printf(ARGS(readlinkat, "%s, %p, %d"), name, (void *)args->buf, 899 args->bufsiz); 900 #endif 901 902 error = kern_readlinkat(td, dfd, name, UIO_SYSSPACE, args->buf, 903 UIO_USERSPACE, args->bufsiz); 904 LFREEPATH(name); 905 return (error); 906 } 907 908 int 909 linux_truncate(struct thread *td, struct linux_truncate_args *args) 910 { 911 char *path; 912 int error; 913 914 LCONVPATHEXIST(td, args->path, &path); 915 916 #ifdef DEBUG 917 if (ldebug(truncate)) 918 printf(ARGS(truncate, "%s, %ld"), path, (long)args->length); 919 #endif 920 921 error = kern_truncate(td, path, UIO_SYSSPACE, args->length); 922 LFREEPATH(path); 923 return (error); 924 } 925 926 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 927 int 928 linux_truncate64(struct thread *td, struct linux_truncate64_args *args) 929 { 930 char *path; 931 int error; 932 933 LCONVPATHEXIST(td, args->path, &path); 934 935 #ifdef DEBUG 936 if (ldebug(truncate64)) 937 printf(ARGS(truncate64, "%s, %jd"), path, args->length); 938 #endif 939 940 error = kern_truncate(td, path, UIO_SYSSPACE, args->length); 941 LFREEPATH(path); 942 return (error); 943 } 944 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 945 946 int 947 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args) 948 { 949 struct ftruncate_args /* { 950 int fd; 951 int pad; 952 off_t length; 953 } */ nuap; 954 955 nuap.fd = args->fd; 956 nuap.length = args->length; 957 return (sys_ftruncate(td, &nuap)); 958 } 959 960 int 961 linux_link(struct thread *td, struct linux_link_args *args) 962 { 963 char *path, *to; 964 int error; 965 966 LCONVPATHEXIST(td, args->path, &path); 967 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */ 968 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD); 969 if (to == NULL) { 970 LFREEPATH(path); 971 return (error); 972 } 973 974 #ifdef DEBUG 975 if (ldebug(link)) 976 printf(ARGS(link, "%s, %s"), path, to); 977 #endif 978 error = kern_linkat(td, AT_FDCWD, AT_FDCWD, path, to, UIO_SYSSPACE, 979 FOLLOW); 980 LFREEPATH(path); 981 LFREEPATH(to); 982 return (error); 983 } 984 985 int 986 linux_linkat(struct thread *td, struct linux_linkat_args *args) 987 { 988 char *path, *to; 989 int error, olddfd, newdfd, follow; 990 991 if (args->flag & ~LINUX_AT_SYMLINK_FOLLOW) 992 return (EINVAL); 993 994 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd; 995 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd; 996 LCONVPATHEXIST_AT(td, args->oldname, &path, olddfd); 997 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */ 998 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd); 999 if (to == NULL) { 1000 LFREEPATH(path); 1001 return (error); 1002 } 1003 1004 #ifdef DEBUG 1005 if (ldebug(linkat)) 1006 printf(ARGS(linkat, "%i, %s, %i, %s, %i"), args->olddfd, path, 1007 args->newdfd, to, args->flag); 1008 #endif 1009 1010 follow = (args->flag & LINUX_AT_SYMLINK_FOLLOW) == 0 ? NOFOLLOW : 1011 FOLLOW; 1012 error = kern_linkat(td, olddfd, newdfd, path, to, UIO_SYSSPACE, follow); 1013 LFREEPATH(path); 1014 LFREEPATH(to); 1015 return (error); 1016 } 1017 1018 int 1019 linux_fdatasync(td, uap) 1020 struct thread *td; 1021 struct linux_fdatasync_args *uap; 1022 { 1023 struct fsync_args bsd; 1024 1025 bsd.fd = uap->fd; 1026 return sys_fsync(td, &bsd); 1027 } 1028 1029 int 1030 linux_pread(td, uap) 1031 struct thread *td; 1032 struct linux_pread_args *uap; 1033 { 1034 struct pread_args bsd; 1035 cap_rights_t rights; 1036 struct vnode *vp; 1037 int error; 1038 1039 bsd.fd = uap->fd; 1040 bsd.buf = uap->buf; 1041 bsd.nbyte = uap->nbyte; 1042 bsd.offset = uap->offset; 1043 1044 error = sys_pread(td, &bsd); 1045 1046 if (error == 0) { 1047 /* This seems to violate POSIX but linux does it */ 1048 error = fgetvp(td, uap->fd, 1049 cap_rights_init(&rights, CAP_PREAD), &vp); 1050 if (error != 0) 1051 return (error); 1052 if (vp->v_type == VDIR) { 1053 vrele(vp); 1054 return (EISDIR); 1055 } 1056 vrele(vp); 1057 } 1058 1059 return (error); 1060 } 1061 1062 int 1063 linux_pwrite(td, uap) 1064 struct thread *td; 1065 struct linux_pwrite_args *uap; 1066 { 1067 struct pwrite_args bsd; 1068 1069 bsd.fd = uap->fd; 1070 bsd.buf = uap->buf; 1071 bsd.nbyte = uap->nbyte; 1072 bsd.offset = uap->offset; 1073 return sys_pwrite(td, &bsd); 1074 } 1075 1076 int 1077 linux_mount(struct thread *td, struct linux_mount_args *args) 1078 { 1079 char fstypename[MFSNAMELEN]; 1080 char mntonname[MNAMELEN], mntfromname[MNAMELEN]; 1081 int error; 1082 int fsflags; 1083 1084 error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1, 1085 NULL); 1086 if (error) 1087 return (error); 1088 error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL); 1089 if (error) 1090 return (error); 1091 error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL); 1092 if (error) 1093 return (error); 1094 1095 #ifdef DEBUG 1096 if (ldebug(mount)) 1097 printf(ARGS(mount, "%s, %s, %s"), 1098 fstypename, mntfromname, mntonname); 1099 #endif 1100 1101 if (strcmp(fstypename, "ext2") == 0) { 1102 strcpy(fstypename, "ext2fs"); 1103 } else if (strcmp(fstypename, "proc") == 0) { 1104 strcpy(fstypename, "linprocfs"); 1105 } else if (strcmp(fstypename, "vfat") == 0) { 1106 strcpy(fstypename, "msdosfs"); 1107 } 1108 1109 fsflags = 0; 1110 1111 if ((args->rwflag & 0xffff0000) == 0xc0ed0000) { 1112 /* 1113 * Linux SYNC flag is not included; the closest equivalent 1114 * FreeBSD has is !ASYNC, which is our default. 1115 */ 1116 if (args->rwflag & LINUX_MS_RDONLY) 1117 fsflags |= MNT_RDONLY; 1118 if (args->rwflag & LINUX_MS_NOSUID) 1119 fsflags |= MNT_NOSUID; 1120 if (args->rwflag & LINUX_MS_NOEXEC) 1121 fsflags |= MNT_NOEXEC; 1122 if (args->rwflag & LINUX_MS_REMOUNT) 1123 fsflags |= MNT_UPDATE; 1124 } 1125 1126 error = kernel_vmount(fsflags, 1127 "fstype", fstypename, 1128 "fspath", mntonname, 1129 "from", mntfromname, 1130 NULL); 1131 return (error); 1132 } 1133 1134 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1135 int 1136 linux_oldumount(struct thread *td, struct linux_oldumount_args *args) 1137 { 1138 struct linux_umount_args args2; 1139 1140 args2.path = args->path; 1141 args2.flags = 0; 1142 return (linux_umount(td, &args2)); 1143 } 1144 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1145 1146 int 1147 linux_umount(struct thread *td, struct linux_umount_args *args) 1148 { 1149 struct unmount_args bsd; 1150 1151 bsd.path = args->path; 1152 bsd.flags = args->flags; /* XXX correct? */ 1153 return (sys_unmount(td, &bsd)); 1154 } 1155 1156 /* 1157 * fcntl family of syscalls 1158 */ 1159 1160 struct l_flock { 1161 l_short l_type; 1162 l_short l_whence; 1163 l_off_t l_start; 1164 l_off_t l_len; 1165 l_pid_t l_pid; 1166 } 1167 #if defined(__amd64__) && defined(COMPAT_LINUX32) 1168 __packed 1169 #endif 1170 ; 1171 1172 static void 1173 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock) 1174 { 1175 switch (linux_flock->l_type) { 1176 case LINUX_F_RDLCK: 1177 bsd_flock->l_type = F_RDLCK; 1178 break; 1179 case LINUX_F_WRLCK: 1180 bsd_flock->l_type = F_WRLCK; 1181 break; 1182 case LINUX_F_UNLCK: 1183 bsd_flock->l_type = F_UNLCK; 1184 break; 1185 default: 1186 bsd_flock->l_type = -1; 1187 break; 1188 } 1189 bsd_flock->l_whence = linux_flock->l_whence; 1190 bsd_flock->l_start = (off_t)linux_flock->l_start; 1191 bsd_flock->l_len = (off_t)linux_flock->l_len; 1192 bsd_flock->l_pid = (pid_t)linux_flock->l_pid; 1193 bsd_flock->l_sysid = 0; 1194 } 1195 1196 static void 1197 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock) 1198 { 1199 switch (bsd_flock->l_type) { 1200 case F_RDLCK: 1201 linux_flock->l_type = LINUX_F_RDLCK; 1202 break; 1203 case F_WRLCK: 1204 linux_flock->l_type = LINUX_F_WRLCK; 1205 break; 1206 case F_UNLCK: 1207 linux_flock->l_type = LINUX_F_UNLCK; 1208 break; 1209 } 1210 linux_flock->l_whence = bsd_flock->l_whence; 1211 linux_flock->l_start = (l_off_t)bsd_flock->l_start; 1212 linux_flock->l_len = (l_off_t)bsd_flock->l_len; 1213 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid; 1214 } 1215 1216 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1217 struct l_flock64 { 1218 l_short l_type; 1219 l_short l_whence; 1220 l_loff_t l_start; 1221 l_loff_t l_len; 1222 l_pid_t l_pid; 1223 } 1224 #if defined(__amd64__) && defined(COMPAT_LINUX32) 1225 __packed 1226 #endif 1227 ; 1228 1229 static void 1230 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock) 1231 { 1232 switch (linux_flock->l_type) { 1233 case LINUX_F_RDLCK: 1234 bsd_flock->l_type = F_RDLCK; 1235 break; 1236 case LINUX_F_WRLCK: 1237 bsd_flock->l_type = F_WRLCK; 1238 break; 1239 case LINUX_F_UNLCK: 1240 bsd_flock->l_type = F_UNLCK; 1241 break; 1242 default: 1243 bsd_flock->l_type = -1; 1244 break; 1245 } 1246 bsd_flock->l_whence = linux_flock->l_whence; 1247 bsd_flock->l_start = (off_t)linux_flock->l_start; 1248 bsd_flock->l_len = (off_t)linux_flock->l_len; 1249 bsd_flock->l_pid = (pid_t)linux_flock->l_pid; 1250 bsd_flock->l_sysid = 0; 1251 } 1252 1253 static void 1254 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock) 1255 { 1256 switch (bsd_flock->l_type) { 1257 case F_RDLCK: 1258 linux_flock->l_type = LINUX_F_RDLCK; 1259 break; 1260 case F_WRLCK: 1261 linux_flock->l_type = LINUX_F_WRLCK; 1262 break; 1263 case F_UNLCK: 1264 linux_flock->l_type = LINUX_F_UNLCK; 1265 break; 1266 } 1267 linux_flock->l_whence = bsd_flock->l_whence; 1268 linux_flock->l_start = (l_loff_t)bsd_flock->l_start; 1269 linux_flock->l_len = (l_loff_t)bsd_flock->l_len; 1270 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid; 1271 } 1272 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1273 1274 static int 1275 fcntl_common(struct thread *td, struct linux_fcntl_args *args) 1276 { 1277 struct l_flock linux_flock; 1278 struct flock bsd_flock; 1279 cap_rights_t rights; 1280 struct file *fp; 1281 long arg; 1282 int error, result; 1283 1284 switch (args->cmd) { 1285 case LINUX_F_DUPFD: 1286 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg)); 1287 1288 case LINUX_F_GETFD: 1289 return (kern_fcntl(td, args->fd, F_GETFD, 0)); 1290 1291 case LINUX_F_SETFD: 1292 return (kern_fcntl(td, args->fd, F_SETFD, args->arg)); 1293 1294 case LINUX_F_GETFL: 1295 error = kern_fcntl(td, args->fd, F_GETFL, 0); 1296 result = td->td_retval[0]; 1297 td->td_retval[0] = 0; 1298 if (result & O_RDONLY) 1299 td->td_retval[0] |= LINUX_O_RDONLY; 1300 if (result & O_WRONLY) 1301 td->td_retval[0] |= LINUX_O_WRONLY; 1302 if (result & O_RDWR) 1303 td->td_retval[0] |= LINUX_O_RDWR; 1304 if (result & O_NDELAY) 1305 td->td_retval[0] |= LINUX_O_NONBLOCK; 1306 if (result & O_APPEND) 1307 td->td_retval[0] |= LINUX_O_APPEND; 1308 if (result & O_FSYNC) 1309 td->td_retval[0] |= LINUX_O_SYNC; 1310 if (result & O_ASYNC) 1311 td->td_retval[0] |= LINUX_FASYNC; 1312 #ifdef LINUX_O_NOFOLLOW 1313 if (result & O_NOFOLLOW) 1314 td->td_retval[0] |= LINUX_O_NOFOLLOW; 1315 #endif 1316 #ifdef LINUX_O_DIRECT 1317 if (result & O_DIRECT) 1318 td->td_retval[0] |= LINUX_O_DIRECT; 1319 #endif 1320 return (error); 1321 1322 case LINUX_F_SETFL: 1323 arg = 0; 1324 if (args->arg & LINUX_O_NDELAY) 1325 arg |= O_NONBLOCK; 1326 if (args->arg & LINUX_O_APPEND) 1327 arg |= O_APPEND; 1328 if (args->arg & LINUX_O_SYNC) 1329 arg |= O_FSYNC; 1330 if (args->arg & LINUX_FASYNC) 1331 arg |= O_ASYNC; 1332 #ifdef LINUX_O_NOFOLLOW 1333 if (args->arg & LINUX_O_NOFOLLOW) 1334 arg |= O_NOFOLLOW; 1335 #endif 1336 #ifdef LINUX_O_DIRECT 1337 if (args->arg & LINUX_O_DIRECT) 1338 arg |= O_DIRECT; 1339 #endif 1340 return (kern_fcntl(td, args->fd, F_SETFL, arg)); 1341 1342 case LINUX_F_GETLK: 1343 error = copyin((void *)args->arg, &linux_flock, 1344 sizeof(linux_flock)); 1345 if (error) 1346 return (error); 1347 linux_to_bsd_flock(&linux_flock, &bsd_flock); 1348 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock); 1349 if (error) 1350 return (error); 1351 bsd_to_linux_flock(&bsd_flock, &linux_flock); 1352 return (copyout(&linux_flock, (void *)args->arg, 1353 sizeof(linux_flock))); 1354 1355 case LINUX_F_SETLK: 1356 error = copyin((void *)args->arg, &linux_flock, 1357 sizeof(linux_flock)); 1358 if (error) 1359 return (error); 1360 linux_to_bsd_flock(&linux_flock, &bsd_flock); 1361 return (kern_fcntl(td, args->fd, F_SETLK, 1362 (intptr_t)&bsd_flock)); 1363 1364 case LINUX_F_SETLKW: 1365 error = copyin((void *)args->arg, &linux_flock, 1366 sizeof(linux_flock)); 1367 if (error) 1368 return (error); 1369 linux_to_bsd_flock(&linux_flock, &bsd_flock); 1370 return (kern_fcntl(td, args->fd, F_SETLKW, 1371 (intptr_t)&bsd_flock)); 1372 1373 case LINUX_F_GETOWN: 1374 return (kern_fcntl(td, args->fd, F_GETOWN, 0)); 1375 1376 case LINUX_F_SETOWN: 1377 /* 1378 * XXX some Linux applications depend on F_SETOWN having no 1379 * significant effect for pipes (SIGIO is not delivered for 1380 * pipes under Linux-2.2.35 at least). 1381 */ 1382 error = fget(td, args->fd, 1383 cap_rights_init(&rights, CAP_FCNTL), &fp); 1384 if (error) 1385 return (error); 1386 if (fp->f_type == DTYPE_PIPE) { 1387 fdrop(fp, td); 1388 return (EINVAL); 1389 } 1390 fdrop(fp, td); 1391 1392 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg)); 1393 1394 case LINUX_F_DUPFD_CLOEXEC: 1395 return (kern_fcntl(td, args->fd, F_DUPFD_CLOEXEC, args->arg)); 1396 } 1397 1398 return (EINVAL); 1399 } 1400 1401 int 1402 linux_fcntl(struct thread *td, struct linux_fcntl_args *args) 1403 { 1404 1405 #ifdef DEBUG 1406 if (ldebug(fcntl)) 1407 printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd); 1408 #endif 1409 1410 return (fcntl_common(td, args)); 1411 } 1412 1413 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1414 int 1415 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args) 1416 { 1417 struct l_flock64 linux_flock; 1418 struct flock bsd_flock; 1419 struct linux_fcntl_args fcntl_args; 1420 int error; 1421 1422 #ifdef DEBUG 1423 if (ldebug(fcntl64)) 1424 printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd); 1425 #endif 1426 1427 switch (args->cmd) { 1428 case LINUX_F_GETLK64: 1429 error = copyin((void *)args->arg, &linux_flock, 1430 sizeof(linux_flock)); 1431 if (error) 1432 return (error); 1433 linux_to_bsd_flock64(&linux_flock, &bsd_flock); 1434 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock); 1435 if (error) 1436 return (error); 1437 bsd_to_linux_flock64(&bsd_flock, &linux_flock); 1438 return (copyout(&linux_flock, (void *)args->arg, 1439 sizeof(linux_flock))); 1440 1441 case LINUX_F_SETLK64: 1442 error = copyin((void *)args->arg, &linux_flock, 1443 sizeof(linux_flock)); 1444 if (error) 1445 return (error); 1446 linux_to_bsd_flock64(&linux_flock, &bsd_flock); 1447 return (kern_fcntl(td, args->fd, F_SETLK, 1448 (intptr_t)&bsd_flock)); 1449 1450 case LINUX_F_SETLKW64: 1451 error = copyin((void *)args->arg, &linux_flock, 1452 sizeof(linux_flock)); 1453 if (error) 1454 return (error); 1455 linux_to_bsd_flock64(&linux_flock, &bsd_flock); 1456 return (kern_fcntl(td, args->fd, F_SETLKW, 1457 (intptr_t)&bsd_flock)); 1458 } 1459 1460 fcntl_args.fd = args->fd; 1461 fcntl_args.cmd = args->cmd; 1462 fcntl_args.arg = args->arg; 1463 return (fcntl_common(td, &fcntl_args)); 1464 } 1465 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1466 1467 int 1468 linux_chown(struct thread *td, struct linux_chown_args *args) 1469 { 1470 char *path; 1471 int error; 1472 1473 LCONVPATHEXIST(td, args->path, &path); 1474 1475 #ifdef DEBUG 1476 if (ldebug(chown)) 1477 printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid); 1478 #endif 1479 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid, 1480 args->gid, 0); 1481 LFREEPATH(path); 1482 return (error); 1483 } 1484 1485 int 1486 linux_fchownat(struct thread *td, struct linux_fchownat_args *args) 1487 { 1488 char *path; 1489 int error, dfd, flag; 1490 1491 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW) 1492 return (EINVAL); 1493 1494 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 1495 LCONVPATHEXIST_AT(td, args->filename, &path, dfd); 1496 1497 #ifdef DEBUG 1498 if (ldebug(fchownat)) 1499 printf(ARGS(fchownat, "%s, %d, %d"), path, args->uid, args->gid); 1500 #endif 1501 1502 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 : 1503 AT_SYMLINK_NOFOLLOW; 1504 error = kern_fchownat(td, dfd, path, UIO_SYSSPACE, args->uid, args->gid, 1505 flag); 1506 LFREEPATH(path); 1507 return (error); 1508 } 1509 1510 int 1511 linux_lchown(struct thread *td, struct linux_lchown_args *args) 1512 { 1513 char *path; 1514 int error; 1515 1516 LCONVPATHEXIST(td, args->path, &path); 1517 1518 #ifdef DEBUG 1519 if (ldebug(lchown)) 1520 printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid); 1521 #endif 1522 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid, 1523 args->gid, AT_SYMLINK_NOFOLLOW); 1524 LFREEPATH(path); 1525 return (error); 1526 } 1527 1528 static int 1529 convert_fadvice(int advice) 1530 { 1531 switch (advice) { 1532 case LINUX_POSIX_FADV_NORMAL: 1533 return (POSIX_FADV_NORMAL); 1534 case LINUX_POSIX_FADV_RANDOM: 1535 return (POSIX_FADV_RANDOM); 1536 case LINUX_POSIX_FADV_SEQUENTIAL: 1537 return (POSIX_FADV_SEQUENTIAL); 1538 case LINUX_POSIX_FADV_WILLNEED: 1539 return (POSIX_FADV_WILLNEED); 1540 case LINUX_POSIX_FADV_DONTNEED: 1541 return (POSIX_FADV_DONTNEED); 1542 case LINUX_POSIX_FADV_NOREUSE: 1543 return (POSIX_FADV_NOREUSE); 1544 default: 1545 return (-1); 1546 } 1547 } 1548 1549 int 1550 linux_fadvise64(struct thread *td, struct linux_fadvise64_args *args) 1551 { 1552 int advice; 1553 1554 advice = convert_fadvice(args->advice); 1555 if (advice == -1) 1556 return (EINVAL); 1557 return (kern_posix_fadvise(td, args->fd, args->offset, args->len, 1558 advice)); 1559 } 1560 1561 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1562 int 1563 linux_fadvise64_64(struct thread *td, struct linux_fadvise64_64_args *args) 1564 { 1565 int advice; 1566 1567 advice = convert_fadvice(args->advice); 1568 if (advice == -1) 1569 return (EINVAL); 1570 return (kern_posix_fadvise(td, args->fd, args->offset, args->len, 1571 advice)); 1572 } 1573 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 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 = kern_pipe2(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 = kern_pipe2(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 1621 int 1622 linux_dup3(struct thread *td, struct linux_dup3_args *args) 1623 { 1624 int cmd; 1625 intptr_t newfd; 1626 1627 if (args->oldfd == args->newfd) 1628 return (EINVAL); 1629 if ((args->flags & ~LINUX_O_CLOEXEC) != 0) 1630 return (EINVAL); 1631 if (args->flags & LINUX_O_CLOEXEC) 1632 cmd = F_DUP2FD_CLOEXEC; 1633 else 1634 cmd = F_DUP2FD; 1635 1636 newfd = args->newfd; 1637 return (kern_fcntl(td, args->oldfd, cmd, newfd)); 1638 } 1639 1640 int 1641 linux_fallocate(struct thread *td, struct linux_fallocate_args *args) 1642 { 1643 1644 /* 1645 * We emulate only posix_fallocate system call for which 1646 * mode should be 0. 1647 */ 1648 if (args->mode != 0) 1649 return (ENOSYS); 1650 1651 return (kern_posix_fallocate(td, args->fd, args->offset, 1652 args->len)); 1653 } 1654