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