1 /*- 2 * Copyright (c) 1994-1995 Søren Schmidt 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_compat.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/capability.h> 37 #include <sys/conf.h> 38 #include <sys/dirent.h> 39 #include <sys/fcntl.h> 40 #include <sys/file.h> 41 #include <sys/filedesc.h> 42 #include <sys/lock.h> 43 #include <sys/malloc.h> 44 #include <sys/mount.h> 45 #include <sys/mutex.h> 46 #include <sys/namei.h> 47 #include <sys/proc.h> 48 #include <sys/stat.h> 49 #include <sys/sx.h> 50 #include <sys/syscallsubr.h> 51 #include <sys/sysproto.h> 52 #include <sys/tty.h> 53 #include <sys/unistd.h> 54 #include <sys/vnode.h> 55 56 #include <security/mac/mac_framework.h> 57 58 #include <ufs/ufs/extattr.h> 59 #include <ufs/ufs/quota.h> 60 #include <ufs/ufs/ufsmount.h> 61 62 #ifdef COMPAT_LINUX32 63 #include <machine/../linux32/linux.h> 64 #include <machine/../linux32/linux32_proto.h> 65 #else 66 #include <machine/../linux/linux.h> 67 #include <machine/../linux/linux_proto.h> 68 #endif 69 #include <compat/linux/linux_misc.h> 70 #include <compat/linux/linux_util.h> 71 #include <compat/linux/linux_file.h> 72 73 int 74 linux_creat(struct thread *td, struct linux_creat_args *args) 75 { 76 char *path; 77 int error; 78 79 LCONVPATHEXIST(td, args->path, &path); 80 81 #ifdef DEBUG 82 if (ldebug(creat)) 83 printf(ARGS(creat, "%s, %d"), path, args->mode); 84 #endif 85 error = kern_open(td, path, UIO_SYSSPACE, O_WRONLY | O_CREAT | O_TRUNC, 86 args->mode); 87 LFREEPATH(path); 88 return (error); 89 } 90 91 92 static int 93 linux_common_open(struct thread *td, int dirfd, char *path, int l_flags, int mode) 94 { 95 struct proc *p = td->td_proc; 96 struct file *fp; 97 int fd; 98 int bsd_flags, error; 99 100 bsd_flags = 0; 101 switch (l_flags & LINUX_O_ACCMODE) { 102 case LINUX_O_WRONLY: 103 bsd_flags |= O_WRONLY; 104 break; 105 case LINUX_O_RDWR: 106 bsd_flags |= O_RDWR; 107 break; 108 default: 109 bsd_flags |= O_RDONLY; 110 } 111 if (l_flags & LINUX_O_NDELAY) 112 bsd_flags |= O_NONBLOCK; 113 if (l_flags & LINUX_O_APPEND) 114 bsd_flags |= O_APPEND; 115 if (l_flags & LINUX_O_SYNC) 116 bsd_flags |= O_FSYNC; 117 if (l_flags & LINUX_O_NONBLOCK) 118 bsd_flags |= O_NONBLOCK; 119 if (l_flags & LINUX_FASYNC) 120 bsd_flags |= O_ASYNC; 121 if (l_flags & LINUX_O_CREAT) 122 bsd_flags |= O_CREAT; 123 if (l_flags & LINUX_O_TRUNC) 124 bsd_flags |= O_TRUNC; 125 if (l_flags & LINUX_O_EXCL) 126 bsd_flags |= O_EXCL; 127 if (l_flags & LINUX_O_NOCTTY) 128 bsd_flags |= O_NOCTTY; 129 if (l_flags & LINUX_O_DIRECT) 130 bsd_flags |= O_DIRECT; 131 if (l_flags & LINUX_O_NOFOLLOW) 132 bsd_flags |= O_NOFOLLOW; 133 if (l_flags & LINUX_O_DIRECTORY) 134 bsd_flags |= O_DIRECTORY; 135 /* XXX LINUX_O_NOATIME: unable to be easily implemented. */ 136 137 error = kern_openat(td, dirfd, path, UIO_SYSSPACE, bsd_flags, mode); 138 139 if (!error) { 140 fd = td->td_retval[0]; 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 error = fget(td, fd, CAP_IOCTL, &fp); 147 if (!error) { 148 sx_slock(&proctree_lock); 149 PROC_LOCK(p); 150 if (!(bsd_flags & O_NOCTTY) && 151 SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) { 152 PROC_UNLOCK(p); 153 sx_unlock(&proctree_lock); 154 /* XXXPJD: Verify if TIOCSCTTY is allowed. */ 155 if (fp->f_type == DTYPE_VNODE) 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 * XXX as above, fdrop()/kern_close() pair is racy. 165 */ 166 if (error) 167 kern_close(td, fd); 168 } 169 } 170 171 #ifdef DEBUG 172 if (ldebug(open)) 173 printf(LMSG("open returns error %d"), error); 174 #endif 175 LFREEPATH(path); 176 return (error); 177 } 178 179 int 180 linux_openat(struct thread *td, struct linux_openat_args *args) 181 { 182 char *path; 183 int dfd; 184 185 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 186 if (args->flags & LINUX_O_CREAT) 187 LCONVPATH_AT(td, args->filename, &path, 1, dfd); 188 else 189 LCONVPATH_AT(td, args->filename, &path, 0, dfd); 190 #ifdef DEBUG 191 if (ldebug(openat)) 192 printf(ARGS(openat, "%i, %s, 0x%x, 0x%x"), args->dfd, 193 path, args->flags, args->mode); 194 #endif 195 return (linux_common_open(td, dfd, path, args->flags, args->mode)); 196 } 197 198 int 199 linux_open(struct thread *td, struct linux_open_args *args) 200 { 201 char *path; 202 203 if (args->flags & LINUX_O_CREAT) 204 LCONVPATHCREAT(td, args->path, &path); 205 else 206 LCONVPATHEXIST(td, args->path, &path); 207 208 #ifdef DEBUG 209 if (ldebug(open)) 210 printf(ARGS(open, "%s, 0x%x, 0x%x"), 211 path, args->flags, args->mode); 212 #endif 213 214 return (linux_common_open(td, AT_FDCWD, path, args->flags, args->mode)); 215 } 216 217 int 218 linux_lseek(struct thread *td, struct linux_lseek_args *args) 219 { 220 221 struct lseek_args /* { 222 int fd; 223 int pad; 224 off_t offset; 225 int whence; 226 } */ tmp_args; 227 int error; 228 229 #ifdef DEBUG 230 if (ldebug(lseek)) 231 printf(ARGS(lseek, "%d, %ld, %d"), 232 args->fdes, (long)args->off, args->whence); 233 #endif 234 tmp_args.fd = args->fdes; 235 tmp_args.offset = (off_t)args->off; 236 tmp_args.whence = args->whence; 237 error = sys_lseek(td, &tmp_args); 238 return error; 239 } 240 241 int 242 linux_llseek(struct thread *td, struct linux_llseek_args *args) 243 { 244 struct lseek_args bsd_args; 245 int error; 246 off_t off; 247 248 #ifdef DEBUG 249 if (ldebug(llseek)) 250 printf(ARGS(llseek, "%d, %d:%d, %d"), 251 args->fd, args->ohigh, args->olow, args->whence); 252 #endif 253 off = (args->olow) | (((off_t) args->ohigh) << 32); 254 255 bsd_args.fd = args->fd; 256 bsd_args.offset = off; 257 bsd_args.whence = args->whence; 258 259 if ((error = sys_lseek(td, &bsd_args))) 260 return error; 261 262 if ((error = copyout(td->td_retval, args->res, sizeof (off_t)))) 263 return error; 264 265 td->td_retval[0] = 0; 266 return 0; 267 } 268 269 int 270 linux_readdir(struct thread *td, struct linux_readdir_args *args) 271 { 272 struct linux_getdents_args lda; 273 274 lda.fd = args->fd; 275 lda.dent = args->dent; 276 lda.count = 1; 277 return linux_getdents(td, &lda); 278 } 279 280 /* 281 * Note that linux_getdents(2) and linux_getdents64(2) have the same 282 * arguments. They only differ in the definition of struct dirent they 283 * operate on. We use this to common the code, with the exception of 284 * accessing struct dirent. Note that linux_readdir(2) is implemented 285 * by means of linux_getdents(2). In this case we never operate on 286 * struct dirent64 and thus don't need to handle it... 287 */ 288 289 struct l_dirent { 290 l_ulong d_ino; 291 l_off_t d_off; 292 l_ushort d_reclen; 293 char d_name[LINUX_NAME_MAX + 1]; 294 }; 295 296 struct l_dirent64 { 297 uint64_t d_ino; 298 int64_t d_off; 299 l_ushort d_reclen; 300 u_char d_type; 301 char d_name[LINUX_NAME_MAX + 1]; 302 }; 303 304 /* 305 * Linux uses the last byte in the dirent buffer to store d_type, 306 * at least glibc-2.7 requires it. That is why l_dirent is padded with 2 bytes. 307 */ 308 #define LINUX_RECLEN(namlen) \ 309 roundup((offsetof(struct l_dirent, d_name) + (namlen) + 2), \ 310 sizeof(l_ulong)) 311 312 #define LINUX_RECLEN64(namlen) \ 313 roundup((offsetof(struct l_dirent64, d_name) + (namlen) + 1), \ 314 sizeof(uint64_t)) 315 316 #define LINUX_MAXRECLEN max(LINUX_RECLEN(LINUX_NAME_MAX), \ 317 LINUX_RECLEN64(LINUX_NAME_MAX)) 318 #define LINUX_DIRBLKSIZ 512 319 320 static int 321 getdents_common(struct thread *td, struct linux_getdents64_args *args, 322 int is64bit) 323 { 324 struct dirent *bdp; 325 struct vnode *vp; 326 caddr_t inp, buf; /* BSD-format */ 327 int len, reclen; /* BSD-format */ 328 caddr_t outp; /* Linux-format */ 329 int resid, linuxreclen=0; /* Linux-format */ 330 caddr_t lbuf; /* Linux-format */ 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 if ((error = getvnode(td->td_proc->p_fd, args->fd, CAP_READ, &fp)) != 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_TEMP, M_WAITOK); 371 lbuf = malloc(LINUX_MAXRECLEN, M_TEMP, 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_TEMP); 523 free(lbuf, M_TEMP); 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_access(td, path, UIO_SYSSPACE, args->amode); 572 LFREEPATH(path); 573 574 return (error); 575 } 576 577 int 578 linux_faccessat(struct thread *td, struct linux_faccessat_args *args) 579 { 580 char *path; 581 int error, dfd, flag; 582 583 if (args->flag & ~LINUX_AT_EACCESS) 584 return (EINVAL); 585 /* linux convention */ 586 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK)) 587 return (EINVAL); 588 589 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 590 LCONVPATHEXIST_AT(td, args->filename, &path, dfd); 591 592 #ifdef DEBUG 593 if (ldebug(access)) 594 printf(ARGS(access, "%s, %d"), path, args->amode); 595 #endif 596 597 flag = (args->flag & LINUX_AT_EACCESS) == 0 ? 0 : AT_EACCESS; 598 error = kern_accessat(td, dfd, path, UIO_SYSSPACE, flag, 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_unlink(td, path, UIO_SYSSPACE); 619 if (error == EPERM) 620 /* Introduce POSIX noncompliant behaviour of Linux */ 621 if (kern_stat(td, path, UIO_SYSSPACE, &st) == 0) 622 if (S_ISDIR(st.st_mode)) 623 error = EISDIR; 624 LFREEPATH(path); 625 return (error); 626 } 627 628 int 629 linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args) 630 { 631 char *path; 632 int error, dfd; 633 struct stat st; 634 635 if (args->flag & ~LINUX_AT_REMOVEDIR) 636 return (EINVAL); 637 638 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 639 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd); 640 641 #ifdef DEBUG 642 if (ldebug(unlinkat)) 643 printf(ARGS(unlinkat, "%s"), path); 644 #endif 645 646 if (args->flag & LINUX_AT_REMOVEDIR) 647 error = kern_rmdirat(td, dfd, path, UIO_SYSSPACE); 648 else 649 error = kern_unlinkat(td, dfd, path, UIO_SYSSPACE, 0); 650 if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) { 651 /* Introduce POSIX noncompliant behaviour of Linux */ 652 if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path, 653 UIO_SYSSPACE, &st) == 0 && S_ISDIR(st.st_mode)) 654 error = EISDIR; 655 } 656 LFREEPATH(path); 657 return (error); 658 } 659 int 660 linux_chdir(struct thread *td, struct linux_chdir_args *args) 661 { 662 char *path; 663 int error; 664 665 LCONVPATHEXIST(td, args->path, &path); 666 667 #ifdef DEBUG 668 if (ldebug(chdir)) 669 printf(ARGS(chdir, "%s"), path); 670 #endif 671 error = kern_chdir(td, path, UIO_SYSSPACE); 672 LFREEPATH(path); 673 return (error); 674 } 675 676 int 677 linux_chmod(struct thread *td, struct linux_chmod_args *args) 678 { 679 char *path; 680 int error; 681 682 LCONVPATHEXIST(td, args->path, &path); 683 684 #ifdef DEBUG 685 if (ldebug(chmod)) 686 printf(ARGS(chmod, "%s, %d"), path, args->mode); 687 #endif 688 error = kern_chmod(td, path, UIO_SYSSPACE, args->mode); 689 LFREEPATH(path); 690 return (error); 691 } 692 693 int 694 linux_fchmodat(struct thread *td, struct linux_fchmodat_args *args) 695 { 696 char *path; 697 int error, dfd; 698 699 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 700 LCONVPATHEXIST_AT(td, args->filename, &path, dfd); 701 702 #ifdef DEBUG 703 if (ldebug(fchmodat)) 704 printf(ARGS(fchmodat, "%s, %d"), path, args->mode); 705 #endif 706 707 error = kern_fchmodat(td, dfd, path, UIO_SYSSPACE, args->mode, 0); 708 LFREEPATH(path); 709 return (error); 710 } 711 712 int 713 linux_mkdir(struct thread *td, struct linux_mkdir_args *args) 714 { 715 char *path; 716 int error; 717 718 LCONVPATHCREAT(td, args->path, &path); 719 720 #ifdef DEBUG 721 if (ldebug(mkdir)) 722 printf(ARGS(mkdir, "%s, %d"), path, args->mode); 723 #endif 724 error = kern_mkdir(td, path, UIO_SYSSPACE, args->mode); 725 LFREEPATH(path); 726 return (error); 727 } 728 729 int 730 linux_mkdirat(struct thread *td, struct linux_mkdirat_args *args) 731 { 732 char *path; 733 int error, dfd; 734 735 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 736 LCONVPATHCREAT_AT(td, args->pathname, &path, dfd); 737 738 #ifdef DEBUG 739 if (ldebug(mkdirat)) 740 printf(ARGS(mkdirat, "%s, %d"), path, args->mode); 741 #endif 742 error = kern_mkdirat(td, dfd, path, UIO_SYSSPACE, args->mode); 743 LFREEPATH(path); 744 return (error); 745 } 746 747 int 748 linux_rmdir(struct thread *td, struct linux_rmdir_args *args) 749 { 750 char *path; 751 int error; 752 753 LCONVPATHEXIST(td, args->path, &path); 754 755 #ifdef DEBUG 756 if (ldebug(rmdir)) 757 printf(ARGS(rmdir, "%s"), path); 758 #endif 759 error = kern_rmdir(td, path, UIO_SYSSPACE); 760 LFREEPATH(path); 761 return (error); 762 } 763 764 int 765 linux_rename(struct thread *td, struct linux_rename_args *args) 766 { 767 char *from, *to; 768 int error; 769 770 LCONVPATHEXIST(td, args->from, &from); 771 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */ 772 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD); 773 if (to == NULL) { 774 LFREEPATH(from); 775 return (error); 776 } 777 778 #ifdef DEBUG 779 if (ldebug(rename)) 780 printf(ARGS(rename, "%s, %s"), from, to); 781 #endif 782 error = kern_rename(td, from, to, UIO_SYSSPACE); 783 LFREEPATH(from); 784 LFREEPATH(to); 785 return (error); 786 } 787 788 int 789 linux_renameat(struct thread *td, struct linux_renameat_args *args) 790 { 791 char *from, *to; 792 int error, olddfd, newdfd; 793 794 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd; 795 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd; 796 LCONVPATHEXIST_AT(td, args->oldname, &from, olddfd); 797 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */ 798 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd); 799 if (to == NULL) { 800 LFREEPATH(from); 801 return (error); 802 } 803 804 #ifdef DEBUG 805 if (ldebug(renameat)) 806 printf(ARGS(renameat, "%s, %s"), from, to); 807 #endif 808 error = kern_renameat(td, olddfd, from, newdfd, to, UIO_SYSSPACE); 809 LFREEPATH(from); 810 LFREEPATH(to); 811 return (error); 812 } 813 814 int 815 linux_symlink(struct thread *td, struct linux_symlink_args *args) 816 { 817 char *path, *to; 818 int error; 819 820 LCONVPATHEXIST(td, args->path, &path); 821 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */ 822 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD); 823 if (to == NULL) { 824 LFREEPATH(path); 825 return (error); 826 } 827 828 #ifdef DEBUG 829 if (ldebug(symlink)) 830 printf(ARGS(symlink, "%s, %s"), path, to); 831 #endif 832 error = kern_symlink(td, path, to, UIO_SYSSPACE); 833 LFREEPATH(path); 834 LFREEPATH(to); 835 return (error); 836 } 837 838 int 839 linux_symlinkat(struct thread *td, struct linux_symlinkat_args *args) 840 { 841 char *path, *to; 842 int error, dfd; 843 844 dfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd; 845 LCONVPATHEXIST_AT(td, args->oldname, &path, dfd); 846 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */ 847 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, dfd); 848 if (to == NULL) { 849 LFREEPATH(path); 850 return (error); 851 } 852 853 #ifdef DEBUG 854 if (ldebug(symlinkat)) 855 printf(ARGS(symlinkat, "%s, %s"), path, to); 856 #endif 857 858 error = kern_symlinkat(td, path, dfd, to, UIO_SYSSPACE); 859 LFREEPATH(path); 860 LFREEPATH(to); 861 return (error); 862 } 863 864 int 865 linux_readlink(struct thread *td, struct linux_readlink_args *args) 866 { 867 char *name; 868 int error; 869 870 LCONVPATHEXIST(td, args->name, &name); 871 872 #ifdef DEBUG 873 if (ldebug(readlink)) 874 printf(ARGS(readlink, "%s, %p, %d"), name, (void *)args->buf, 875 args->count); 876 #endif 877 error = kern_readlink(td, name, UIO_SYSSPACE, args->buf, UIO_USERSPACE, 878 args->count); 879 LFREEPATH(name); 880 return (error); 881 } 882 883 int 884 linux_readlinkat(struct thread *td, struct linux_readlinkat_args *args) 885 { 886 char *name; 887 int error, dfd; 888 889 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 890 LCONVPATHEXIST_AT(td, args->path, &name, dfd); 891 892 #ifdef DEBUG 893 if (ldebug(readlinkat)) 894 printf(ARGS(readlinkat, "%s, %p, %d"), name, (void *)args->buf, 895 args->bufsiz); 896 #endif 897 898 error = kern_readlinkat(td, dfd, name, UIO_SYSSPACE, args->buf, 899 UIO_USERSPACE, args->bufsiz); 900 LFREEPATH(name); 901 return (error); 902 } 903 904 int 905 linux_truncate(struct thread *td, struct linux_truncate_args *args) 906 { 907 char *path; 908 int error; 909 910 LCONVPATHEXIST(td, args->path, &path); 911 912 #ifdef DEBUG 913 if (ldebug(truncate)) 914 printf(ARGS(truncate, "%s, %ld"), path, (long)args->length); 915 #endif 916 917 error = kern_truncate(td, path, UIO_SYSSPACE, args->length); 918 LFREEPATH(path); 919 return (error); 920 } 921 922 int 923 linux_truncate64(struct thread *td, struct linux_truncate64_args *args) 924 { 925 char *path; 926 int error; 927 928 LCONVPATHEXIST(td, args->path, &path); 929 930 #ifdef DEBUG 931 if (ldebug(truncate64)) 932 printf(ARGS(truncate64, "%s, %jd"), path, args->length); 933 #endif 934 935 error = kern_truncate(td, path, UIO_SYSSPACE, args->length); 936 LFREEPATH(path); 937 return (error); 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_link(td, path, to, UIO_SYSSPACE); 972 LFREEPATH(path); 973 LFREEPATH(to); 974 return (error); 975 } 976 977 int 978 linux_linkat(struct thread *td, struct linux_linkat_args *args) 979 { 980 char *path, *to; 981 int error, olddfd, newdfd, follow; 982 983 if (args->flag & ~LINUX_AT_SYMLINK_FOLLOW) 984 return (EINVAL); 985 986 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd; 987 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd; 988 LCONVPATHEXIST_AT(td, args->oldname, &path, olddfd); 989 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */ 990 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd); 991 if (to == NULL) { 992 LFREEPATH(path); 993 return (error); 994 } 995 996 #ifdef DEBUG 997 if (ldebug(linkat)) 998 printf(ARGS(linkat, "%i, %s, %i, %s, %i"), args->olddfd, path, 999 args->newdfd, to, args->flag); 1000 #endif 1001 1002 follow = (args->flag & LINUX_AT_SYMLINK_FOLLOW) == 0 ? NOFOLLOW : 1003 FOLLOW; 1004 error = kern_linkat(td, olddfd, newdfd, path, to, UIO_SYSSPACE, follow); 1005 LFREEPATH(path); 1006 LFREEPATH(to); 1007 return (error); 1008 } 1009 1010 int 1011 linux_fdatasync(td, uap) 1012 struct thread *td; 1013 struct linux_fdatasync_args *uap; 1014 { 1015 struct fsync_args bsd; 1016 1017 bsd.fd = uap->fd; 1018 return sys_fsync(td, &bsd); 1019 } 1020 1021 int 1022 linux_pread(td, uap) 1023 struct thread *td; 1024 struct linux_pread_args *uap; 1025 { 1026 struct pread_args bsd; 1027 struct vnode *vp; 1028 int error; 1029 1030 bsd.fd = uap->fd; 1031 bsd.buf = uap->buf; 1032 bsd.nbyte = uap->nbyte; 1033 bsd.offset = uap->offset; 1034 1035 error = sys_pread(td, &bsd); 1036 1037 if (error == 0) { 1038 /* This seems to violate POSIX but linux does it */ 1039 if ((error = fgetvp(td, uap->fd, CAP_PREAD, &vp)) != 0) 1040 return (error); 1041 if (vp->v_type == VDIR) { 1042 vrele(vp); 1043 return (EISDIR); 1044 } 1045 vrele(vp); 1046 } 1047 1048 return (error); 1049 } 1050 1051 int 1052 linux_pwrite(td, uap) 1053 struct thread *td; 1054 struct linux_pwrite_args *uap; 1055 { 1056 struct pwrite_args bsd; 1057 1058 bsd.fd = uap->fd; 1059 bsd.buf = uap->buf; 1060 bsd.nbyte = uap->nbyte; 1061 bsd.offset = uap->offset; 1062 return sys_pwrite(td, &bsd); 1063 } 1064 1065 int 1066 linux_mount(struct thread *td, struct linux_mount_args *args) 1067 { 1068 struct ufs_args ufs; 1069 char fstypename[MFSNAMELEN]; 1070 char mntonname[MNAMELEN], mntfromname[MNAMELEN]; 1071 int error; 1072 int fsflags; 1073 void *fsdata; 1074 1075 error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1, 1076 NULL); 1077 if (error) 1078 return (error); 1079 error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL); 1080 if (error) 1081 return (error); 1082 error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL); 1083 if (error) 1084 return (error); 1085 1086 #ifdef DEBUG 1087 if (ldebug(mount)) 1088 printf(ARGS(mount, "%s, %s, %s"), 1089 fstypename, mntfromname, mntonname); 1090 #endif 1091 1092 if (strcmp(fstypename, "ext2") == 0) { 1093 strcpy(fstypename, "ext2fs"); 1094 fsdata = &ufs; 1095 ufs.fspec = mntfromname; 1096 #define DEFAULT_ROOTID -2 1097 ufs.export.ex_root = DEFAULT_ROOTID; 1098 ufs.export.ex_flags = 1099 args->rwflag & LINUX_MS_RDONLY ? MNT_EXRDONLY : 0; 1100 } else if (strcmp(fstypename, "proc") == 0) { 1101 strcpy(fstypename, "linprocfs"); 1102 fsdata = NULL; 1103 } else if (strcmp(fstypename, "vfat") == 0) { 1104 strcpy(fstypename, "msdosfs"); 1105 fsdata = NULL; 1106 } else { 1107 return (ENODEV); 1108 } 1109 1110 fsflags = 0; 1111 1112 if ((args->rwflag & 0xffff0000) == 0xc0ed0000) { 1113 /* 1114 * Linux SYNC flag is not included; the closest equivalent 1115 * FreeBSD has is !ASYNC, which is our default. 1116 */ 1117 if (args->rwflag & LINUX_MS_RDONLY) 1118 fsflags |= MNT_RDONLY; 1119 if (args->rwflag & LINUX_MS_NOSUID) 1120 fsflags |= MNT_NOSUID; 1121 if (args->rwflag & LINUX_MS_NOEXEC) 1122 fsflags |= MNT_NOEXEC; 1123 if (args->rwflag & LINUX_MS_REMOUNT) 1124 fsflags |= MNT_UPDATE; 1125 } 1126 1127 if (strcmp(fstypename, "linprocfs") == 0) { 1128 error = kernel_vmount(fsflags, 1129 "fstype", fstypename, 1130 "fspath", mntonname, 1131 NULL); 1132 } else if (strcmp(fstypename, "msdosfs") == 0) { 1133 error = kernel_vmount(fsflags, 1134 "fstype", fstypename, 1135 "fspath", mntonname, 1136 "from", mntfromname, 1137 NULL); 1138 } else 1139 error = EOPNOTSUPP; 1140 return (error); 1141 } 1142 1143 int 1144 linux_oldumount(struct thread *td, struct linux_oldumount_args *args) 1145 { 1146 struct linux_umount_args args2; 1147 1148 args2.path = args->path; 1149 args2.flags = 0; 1150 return (linux_umount(td, &args2)); 1151 } 1152 1153 int 1154 linux_umount(struct thread *td, struct linux_umount_args *args) 1155 { 1156 struct unmount_args bsd; 1157 1158 bsd.path = args->path; 1159 bsd.flags = args->flags; /* XXX correct? */ 1160 return (sys_unmount(td, &bsd)); 1161 } 1162 1163 /* 1164 * fcntl family of syscalls 1165 */ 1166 1167 struct l_flock { 1168 l_short l_type; 1169 l_short l_whence; 1170 l_off_t l_start; 1171 l_off_t l_len; 1172 l_pid_t l_pid; 1173 } 1174 #if defined(__amd64__) && defined(COMPAT_LINUX32) 1175 __packed 1176 #endif 1177 ; 1178 1179 static void 1180 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock) 1181 { 1182 switch (linux_flock->l_type) { 1183 case LINUX_F_RDLCK: 1184 bsd_flock->l_type = F_RDLCK; 1185 break; 1186 case LINUX_F_WRLCK: 1187 bsd_flock->l_type = F_WRLCK; 1188 break; 1189 case LINUX_F_UNLCK: 1190 bsd_flock->l_type = F_UNLCK; 1191 break; 1192 default: 1193 bsd_flock->l_type = -1; 1194 break; 1195 } 1196 bsd_flock->l_whence = linux_flock->l_whence; 1197 bsd_flock->l_start = (off_t)linux_flock->l_start; 1198 bsd_flock->l_len = (off_t)linux_flock->l_len; 1199 bsd_flock->l_pid = (pid_t)linux_flock->l_pid; 1200 bsd_flock->l_sysid = 0; 1201 } 1202 1203 static void 1204 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock) 1205 { 1206 switch (bsd_flock->l_type) { 1207 case F_RDLCK: 1208 linux_flock->l_type = LINUX_F_RDLCK; 1209 break; 1210 case F_WRLCK: 1211 linux_flock->l_type = LINUX_F_WRLCK; 1212 break; 1213 case F_UNLCK: 1214 linux_flock->l_type = LINUX_F_UNLCK; 1215 break; 1216 } 1217 linux_flock->l_whence = bsd_flock->l_whence; 1218 linux_flock->l_start = (l_off_t)bsd_flock->l_start; 1219 linux_flock->l_len = (l_off_t)bsd_flock->l_len; 1220 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid; 1221 } 1222 1223 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1224 struct l_flock64 { 1225 l_short l_type; 1226 l_short l_whence; 1227 l_loff_t l_start; 1228 l_loff_t l_len; 1229 l_pid_t l_pid; 1230 } 1231 #if defined(__amd64__) && defined(COMPAT_LINUX32) 1232 __packed 1233 #endif 1234 ; 1235 1236 static void 1237 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock) 1238 { 1239 switch (linux_flock->l_type) { 1240 case LINUX_F_RDLCK: 1241 bsd_flock->l_type = F_RDLCK; 1242 break; 1243 case LINUX_F_WRLCK: 1244 bsd_flock->l_type = F_WRLCK; 1245 break; 1246 case LINUX_F_UNLCK: 1247 bsd_flock->l_type = F_UNLCK; 1248 break; 1249 default: 1250 bsd_flock->l_type = -1; 1251 break; 1252 } 1253 bsd_flock->l_whence = linux_flock->l_whence; 1254 bsd_flock->l_start = (off_t)linux_flock->l_start; 1255 bsd_flock->l_len = (off_t)linux_flock->l_len; 1256 bsd_flock->l_pid = (pid_t)linux_flock->l_pid; 1257 bsd_flock->l_sysid = 0; 1258 } 1259 1260 static void 1261 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock) 1262 { 1263 switch (bsd_flock->l_type) { 1264 case F_RDLCK: 1265 linux_flock->l_type = LINUX_F_RDLCK; 1266 break; 1267 case F_WRLCK: 1268 linux_flock->l_type = LINUX_F_WRLCK; 1269 break; 1270 case F_UNLCK: 1271 linux_flock->l_type = LINUX_F_UNLCK; 1272 break; 1273 } 1274 linux_flock->l_whence = bsd_flock->l_whence; 1275 linux_flock->l_start = (l_loff_t)bsd_flock->l_start; 1276 linux_flock->l_len = (l_loff_t)bsd_flock->l_len; 1277 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid; 1278 } 1279 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1280 1281 static int 1282 fcntl_common(struct thread *td, struct linux_fcntl64_args *args) 1283 { 1284 struct l_flock linux_flock; 1285 struct flock bsd_flock; 1286 struct file *fp; 1287 long arg; 1288 int error, result; 1289 1290 switch (args->cmd) { 1291 case LINUX_F_DUPFD: 1292 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg)); 1293 1294 case LINUX_F_GETFD: 1295 return (kern_fcntl(td, args->fd, F_GETFD, 0)); 1296 1297 case LINUX_F_SETFD: 1298 return (kern_fcntl(td, args->fd, F_SETFD, args->arg)); 1299 1300 case LINUX_F_GETFL: 1301 error = kern_fcntl(td, args->fd, F_GETFL, 0); 1302 result = td->td_retval[0]; 1303 td->td_retval[0] = 0; 1304 if (result & O_RDONLY) 1305 td->td_retval[0] |= LINUX_O_RDONLY; 1306 if (result & O_WRONLY) 1307 td->td_retval[0] |= LINUX_O_WRONLY; 1308 if (result & O_RDWR) 1309 td->td_retval[0] |= LINUX_O_RDWR; 1310 if (result & O_NDELAY) 1311 td->td_retval[0] |= LINUX_O_NONBLOCK; 1312 if (result & O_APPEND) 1313 td->td_retval[0] |= LINUX_O_APPEND; 1314 if (result & O_FSYNC) 1315 td->td_retval[0] |= LINUX_O_SYNC; 1316 if (result & O_ASYNC) 1317 td->td_retval[0] |= LINUX_FASYNC; 1318 #ifdef LINUX_O_NOFOLLOW 1319 if (result & O_NOFOLLOW) 1320 td->td_retval[0] |= LINUX_O_NOFOLLOW; 1321 #endif 1322 #ifdef LINUX_O_DIRECT 1323 if (result & O_DIRECT) 1324 td->td_retval[0] |= LINUX_O_DIRECT; 1325 #endif 1326 return (error); 1327 1328 case LINUX_F_SETFL: 1329 arg = 0; 1330 if (args->arg & LINUX_O_NDELAY) 1331 arg |= O_NONBLOCK; 1332 if (args->arg & LINUX_O_APPEND) 1333 arg |= O_APPEND; 1334 if (args->arg & LINUX_O_SYNC) 1335 arg |= O_FSYNC; 1336 if (args->arg & LINUX_FASYNC) 1337 arg |= O_ASYNC; 1338 #ifdef LINUX_O_NOFOLLOW 1339 if (args->arg & LINUX_O_NOFOLLOW) 1340 arg |= O_NOFOLLOW; 1341 #endif 1342 #ifdef LINUX_O_DIRECT 1343 if (args->arg & LINUX_O_DIRECT) 1344 arg |= O_DIRECT; 1345 #endif 1346 return (kern_fcntl(td, args->fd, F_SETFL, arg)); 1347 1348 case LINUX_F_GETLK: 1349 error = copyin((void *)args->arg, &linux_flock, 1350 sizeof(linux_flock)); 1351 if (error) 1352 return (error); 1353 linux_to_bsd_flock(&linux_flock, &bsd_flock); 1354 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock); 1355 if (error) 1356 return (error); 1357 bsd_to_linux_flock(&bsd_flock, &linux_flock); 1358 return (copyout(&linux_flock, (void *)args->arg, 1359 sizeof(linux_flock))); 1360 1361 case LINUX_F_SETLK: 1362 error = copyin((void *)args->arg, &linux_flock, 1363 sizeof(linux_flock)); 1364 if (error) 1365 return (error); 1366 linux_to_bsd_flock(&linux_flock, &bsd_flock); 1367 return (kern_fcntl(td, args->fd, F_SETLK, 1368 (intptr_t)&bsd_flock)); 1369 1370 case LINUX_F_SETLKW: 1371 error = copyin((void *)args->arg, &linux_flock, 1372 sizeof(linux_flock)); 1373 if (error) 1374 return (error); 1375 linux_to_bsd_flock(&linux_flock, &bsd_flock); 1376 return (kern_fcntl(td, args->fd, F_SETLKW, 1377 (intptr_t)&bsd_flock)); 1378 1379 case LINUX_F_GETOWN: 1380 return (kern_fcntl(td, args->fd, F_GETOWN, 0)); 1381 1382 case LINUX_F_SETOWN: 1383 /* 1384 * XXX some Linux applications depend on F_SETOWN having no 1385 * significant effect for pipes (SIGIO is not delivered for 1386 * pipes under Linux-2.2.35 at least). 1387 */ 1388 error = fget(td, args->fd, CAP_FCNTL, &fp); 1389 if (error) 1390 return (error); 1391 if (fp->f_type == DTYPE_PIPE) { 1392 fdrop(fp, td); 1393 return (EINVAL); 1394 } 1395 fdrop(fp, td); 1396 1397 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg)); 1398 } 1399 1400 return (EINVAL); 1401 } 1402 1403 int 1404 linux_fcntl(struct thread *td, struct linux_fcntl_args *args) 1405 { 1406 struct linux_fcntl64_args args64; 1407 1408 #ifdef DEBUG 1409 if (ldebug(fcntl)) 1410 printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd); 1411 #endif 1412 1413 args64.fd = args->fd; 1414 args64.cmd = args->cmd; 1415 args64.arg = args->arg; 1416 return (fcntl_common(td, &args64)); 1417 } 1418 1419 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1420 int 1421 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args) 1422 { 1423 struct l_flock64 linux_flock; 1424 struct flock bsd_flock; 1425 int error; 1426 1427 #ifdef DEBUG 1428 if (ldebug(fcntl64)) 1429 printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd); 1430 #endif 1431 1432 switch (args->cmd) { 1433 case LINUX_F_GETLK64: 1434 error = copyin((void *)args->arg, &linux_flock, 1435 sizeof(linux_flock)); 1436 if (error) 1437 return (error); 1438 linux_to_bsd_flock64(&linux_flock, &bsd_flock); 1439 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock); 1440 if (error) 1441 return (error); 1442 bsd_to_linux_flock64(&bsd_flock, &linux_flock); 1443 return (copyout(&linux_flock, (void *)args->arg, 1444 sizeof(linux_flock))); 1445 1446 case LINUX_F_SETLK64: 1447 error = copyin((void *)args->arg, &linux_flock, 1448 sizeof(linux_flock)); 1449 if (error) 1450 return (error); 1451 linux_to_bsd_flock64(&linux_flock, &bsd_flock); 1452 return (kern_fcntl(td, args->fd, F_SETLK, 1453 (intptr_t)&bsd_flock)); 1454 1455 case LINUX_F_SETLKW64: 1456 error = copyin((void *)args->arg, &linux_flock, 1457 sizeof(linux_flock)); 1458 if (error) 1459 return (error); 1460 linux_to_bsd_flock64(&linux_flock, &bsd_flock); 1461 return (kern_fcntl(td, args->fd, F_SETLKW, 1462 (intptr_t)&bsd_flock)); 1463 } 1464 1465 return (fcntl_common(td, args)); 1466 } 1467 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1468 1469 int 1470 linux_chown(struct thread *td, struct linux_chown_args *args) 1471 { 1472 char *path; 1473 int error; 1474 1475 LCONVPATHEXIST(td, args->path, &path); 1476 1477 #ifdef DEBUG 1478 if (ldebug(chown)) 1479 printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid); 1480 #endif 1481 error = kern_chown(td, path, UIO_SYSSPACE, args->uid, args->gid); 1482 LFREEPATH(path); 1483 return (error); 1484 } 1485 1486 int 1487 linux_fchownat(struct thread *td, struct linux_fchownat_args *args) 1488 { 1489 char *path; 1490 int error, dfd, flag; 1491 1492 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW) 1493 return (EINVAL); 1494 1495 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 1496 LCONVPATHEXIST_AT(td, args->filename, &path, dfd); 1497 1498 #ifdef DEBUG 1499 if (ldebug(fchownat)) 1500 printf(ARGS(fchownat, "%s, %d, %d"), path, args->uid, args->gid); 1501 #endif 1502 1503 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 : 1504 AT_SYMLINK_NOFOLLOW; 1505 error = kern_fchownat(td, dfd, path, UIO_SYSSPACE, args->uid, args->gid, 1506 flag); 1507 LFREEPATH(path); 1508 return (error); 1509 } 1510 1511 int 1512 linux_lchown(struct thread *td, struct linux_lchown_args *args) 1513 { 1514 char *path; 1515 int error; 1516 1517 LCONVPATHEXIST(td, args->path, &path); 1518 1519 #ifdef DEBUG 1520 if (ldebug(lchown)) 1521 printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid); 1522 #endif 1523 error = kern_lchown(td, path, UIO_SYSSPACE, args->uid, args->gid); 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 int 1562 linux_fadvise64_64(struct thread *td, struct linux_fadvise64_64_args *args) 1563 { 1564 int advice; 1565 1566 advice = convert_fadvice(args->advice); 1567 if (advice == -1) 1568 return (EINVAL); 1569 return (kern_posix_fadvise(td, args->fd, args->offset, args->len, 1570 advice)); 1571 } 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_pipe2(td, fildes, 0); 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_pipe2(td, fildes, flags); 1612 if (error) 1613 return (error); 1614 1615 /* XXX: Close descriptors on error. */ 1616 return (copyout(fildes, args->pipefds, sizeof(fildes))); 1617 } 1618