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