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