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