2 * Copyright (c) 1994-1995 Søren Schmidt
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_compat.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/capsicum.h>
38 #include <sys/dirent.h>
39 #include <sys/fcntl.h>
41 #include <sys/filedesc.h>
43 #include <sys/malloc.h>
44 #include <sys/mount.h>
45 #include <sys/mutex.h>
46 #include <sys/namei.h>
50 #include <sys/syscallsubr.h>
51 #include <sys/sysproto.h>
53 #include <sys/unistd.h>
54 #include <sys/vnode.h>
56 #include <security/mac/mac_framework.h>
59 #include <machine/../linux32/linux.h>
60 #include <machine/../linux32/linux32_proto.h>
62 #include <machine/../linux/linux.h>
63 #include <machine/../linux/linux_proto.h>
65 #include <compat/linux/linux_misc.h>
66 #include <compat/linux/linux_util.h>
67 #include <compat/linux/linux_file.h>
70 linux_creat(struct thread *td, struct linux_creat_args *args)
75 LCONVPATHEXIST(td, args->path, &path);
79 printf(ARGS(creat, "%s, %d"), path, args->mode);
81 error = kern_openat(td, AT_FDCWD, path, UIO_SYSSPACE,
82 O_WRONLY | O_CREAT | O_TRUNC, args->mode);
89 linux_common_open(struct thread *td, int dirfd, char *path, int l_flags, int mode)
92 struct proc *p = td->td_proc;
98 switch (l_flags & LINUX_O_ACCMODE) {
100 bsd_flags |= O_WRONLY;
106 bsd_flags |= O_RDONLY;
108 if (l_flags & LINUX_O_NDELAY)
109 bsd_flags |= O_NONBLOCK;
110 if (l_flags & LINUX_O_APPEND)
111 bsd_flags |= O_APPEND;
112 if (l_flags & LINUX_O_SYNC)
113 bsd_flags |= O_FSYNC;
114 if (l_flags & LINUX_O_NONBLOCK)
115 bsd_flags |= O_NONBLOCK;
116 if (l_flags & LINUX_FASYNC)
117 bsd_flags |= O_ASYNC;
118 if (l_flags & LINUX_O_CREAT)
119 bsd_flags |= O_CREAT;
120 if (l_flags & LINUX_O_TRUNC)
121 bsd_flags |= O_TRUNC;
122 if (l_flags & LINUX_O_EXCL)
124 if (l_flags & LINUX_O_NOCTTY)
125 bsd_flags |= O_NOCTTY;
126 if (l_flags & LINUX_O_DIRECT)
127 bsd_flags |= O_DIRECT;
128 if (l_flags & LINUX_O_NOFOLLOW)
129 bsd_flags |= O_NOFOLLOW;
130 if (l_flags & LINUX_O_DIRECTORY)
131 bsd_flags |= O_DIRECTORY;
132 /* XXX LINUX_O_NOATIME: unable to be easily implemented. */
134 error = kern_openat(td, dirfd, path, UIO_SYSSPACE, bsd_flags, mode);
138 if (bsd_flags & O_NOCTTY)
142 * XXX In between kern_open() and fget(), another process
143 * having the same filedesc could use that fd without
146 fd = td->td_retval[0];
147 if (fget(td, fd, cap_rights_init(&rights, CAP_IOCTL), &fp) == 0) {
148 if (fp->f_type != DTYPE_VNODE) {
152 sx_slock(&proctree_lock);
154 if (SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
156 sx_sunlock(&proctree_lock);
157 /* XXXPJD: Verify if TIOCSCTTY is allowed. */
158 (void) fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0,
162 sx_sunlock(&proctree_lock);
170 printf(LMSG("open returns error %d"), error);
177 linux_openat(struct thread *td, struct linux_openat_args *args)
182 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
183 if (args->flags & LINUX_O_CREAT)
184 LCONVPATH_AT(td, args->filename, &path, 1, dfd);
186 LCONVPATH_AT(td, args->filename, &path, 0, dfd);
189 printf(ARGS(openat, "%i, %s, 0x%x, 0x%x"), args->dfd,
190 path, args->flags, args->mode);
192 return (linux_common_open(td, dfd, path, args->flags, args->mode));
196 linux_open(struct thread *td, struct linux_open_args *args)
200 if (args->flags & LINUX_O_CREAT)
201 LCONVPATHCREAT(td, args->path, &path);
203 LCONVPATHEXIST(td, args->path, &path);
207 printf(ARGS(open, "%s, 0x%x, 0x%x"),
208 path, args->flags, args->mode);
211 return (linux_common_open(td, AT_FDCWD, path, args->flags, args->mode));
215 linux_lseek(struct thread *td, struct linux_lseek_args *args)
218 struct lseek_args /* {
228 printf(ARGS(lseek, "%d, %ld, %d"),
229 args->fdes, (long)args->off, args->whence);
231 tmp_args.fd = args->fdes;
232 tmp_args.offset = (off_t)args->off;
233 tmp_args.whence = args->whence;
234 error = sys_lseek(td, &tmp_args);
239 linux_llseek(struct thread *td, struct linux_llseek_args *args)
241 struct lseek_args bsd_args;
247 printf(ARGS(llseek, "%d, %d:%d, %d"),
248 args->fd, args->ohigh, args->olow, args->whence);
250 off = (args->olow) | (((off_t) args->ohigh) << 32);
252 bsd_args.fd = args->fd;
253 bsd_args.offset = off;
254 bsd_args.whence = args->whence;
256 if ((error = sys_lseek(td, &bsd_args)))
259 if ((error = copyout(td->td_retval, args->res, sizeof (off_t))))
262 td->td_retval[0] = 0;
267 linux_readdir(struct thread *td, struct linux_readdir_args *args)
269 struct linux_getdents_args lda;
272 lda.dent = args->dent;
274 return linux_getdents(td, &lda);
278 * Note that linux_getdents(2) and linux_getdents64(2) have the same
279 * arguments. They only differ in the definition of struct dirent they
280 * operate on. We use this to common the code, with the exception of
281 * accessing struct dirent. Note that linux_readdir(2) is implemented
282 * by means of linux_getdents(2). In this case we never operate on
283 * struct dirent64 and thus don't need to handle it...
290 char d_name[LINUX_NAME_MAX + 1];
298 char d_name[LINUX_NAME_MAX + 1];
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.
305 #define LINUX_RECLEN(namlen) \
306 roundup((offsetof(struct l_dirent, d_name) + (namlen) + 2), \
309 #define LINUX_RECLEN64(namlen) \
310 roundup((offsetof(struct l_dirent64, d_name) + (namlen) + 1), \
313 #define LINUX_MAXRECLEN max(LINUX_RECLEN(LINUX_NAME_MAX), \
314 LINUX_RECLEN64(LINUX_NAME_MAX))
315 #define LINUX_DIRBLKSIZ 512
318 getdents_common(struct thread *td, struct linux_getdents64_args *args,
323 caddr_t inp, buf; /* BSD-format */
324 int len, reclen; /* BSD-format */
325 caddr_t outp; /* Linux-format */
326 int resid, linuxreclen=0; /* Linux-format */
327 caddr_t lbuf; /* Linux-format */
333 struct l_dirent *linux_dirent;
334 struct l_dirent64 *linux_dirent64;
335 int buflen, error, eofflag, nbytes, justone;
336 u_long *cookies = NULL, *cookiep;
339 nbytes = args->count;
341 /* readdir(2) case. Always struct dirent. */
344 nbytes = sizeof(*linux_dirent);
349 error = getvnode(td->td_proc->p_fd, args->fd,
350 cap_rights_init(&rights, CAP_READ), &fp);
354 if ((fp->f_flag & FREAD) == 0) {
359 off = foffset_lock(fp, 0);
361 if (vp->v_type != VDIR) {
362 foffset_unlock(fp, off, 0);
368 buflen = max(LINUX_DIRBLKSIZ, nbytes);
369 buflen = min(buflen, MAXBSIZE);
370 buf = malloc(buflen, M_TEMP, M_WAITOK);
371 lbuf = malloc(LINUX_MAXRECLEN, M_TEMP, M_WAITOK | M_ZERO);
372 vn_lock(vp, LK_SHARED | LK_RETRY);
375 aiov.iov_len = buflen;
376 auio.uio_iov = &aiov;
378 auio.uio_rw = UIO_READ;
379 auio.uio_segflg = UIO_SYSSPACE;
381 auio.uio_resid = buflen;
382 auio.uio_offset = off;
386 * Do directory search MAC check using non-cached credentials.
388 if ((error = mac_vnode_check_readdir(td->td_ucred, vp)))
391 if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
396 outp = (caddr_t)args->dirent;
398 if ((len = buflen - auio.uio_resid) <= 0)
405 * When using cookies, the vfs has the option of reading from
406 * a different offset than that supplied (UFS truncates the
407 * offset to a block boundary to make sure that it never reads
408 * partway through a directory entry, even if the directory
409 * has been compacted).
411 while (len > 0 && ncookies > 0 && *cookiep <= off) {
412 bdp = (struct dirent *) inp;
413 len -= bdp->d_reclen;
414 inp += bdp->d_reclen;
421 if (cookiep && ncookies == 0)
423 bdp = (struct dirent *) inp;
424 reclen = bdp->d_reclen;
430 if (bdp->d_fileno == 0) {
442 linuxreclen = (is64bit)
443 ? LINUX_RECLEN64(bdp->d_namlen)
444 : LINUX_RECLEN(bdp->d_namlen);
446 if (reclen > len || resid < linuxreclen) {
452 /* readdir(2) case. */
453 linux_dirent = (struct l_dirent*)lbuf;
454 linux_dirent->d_ino = bdp->d_fileno;
455 linux_dirent->d_off = (l_off_t)linuxreclen;
456 linux_dirent->d_reclen = (l_ushort)bdp->d_namlen;
457 strlcpy(linux_dirent->d_name, bdp->d_name,
458 linuxreclen - offsetof(struct l_dirent, d_name));
459 error = copyout(linux_dirent, outp, linuxreclen);
462 linux_dirent64 = (struct l_dirent64*)lbuf;
463 linux_dirent64->d_ino = bdp->d_fileno;
464 linux_dirent64->d_off = (cookiep)
466 : (l_off_t)(off + reclen);
467 linux_dirent64->d_reclen = (l_ushort)linuxreclen;
468 linux_dirent64->d_type = bdp->d_type;
469 strlcpy(linux_dirent64->d_name, bdp->d_name,
470 linuxreclen - offsetof(struct l_dirent64, d_name));
471 error = copyout(linux_dirent64, outp, linuxreclen);
472 } else if (!justone) {
473 linux_dirent = (struct l_dirent*)lbuf;
474 linux_dirent->d_ino = bdp->d_fileno;
475 linux_dirent->d_off = (cookiep)
477 : (l_off_t)(off + reclen);
478 linux_dirent->d_reclen = (l_ushort)linuxreclen;
480 * Copy d_type to last byte of l_dirent buffer
482 lbuf[linuxreclen-1] = bdp->d_type;
483 strlcpy(linux_dirent->d_name, bdp->d_name,
484 linuxreclen - offsetof(struct l_dirent, d_name)-1);
485 error = copyout(linux_dirent, outp, linuxreclen);
499 resid -= linuxreclen;
505 if (outp == (caddr_t)args->dirent) {
511 nbytes = resid + linuxreclen;
514 td->td_retval[0] = nbytes - resid;
517 free(cookies, M_TEMP);
520 foffset_unlock(fp, off, 0);
528 linux_getdents(struct thread *td, struct linux_getdents_args *args)
532 if (ldebug(getdents))
533 printf(ARGS(getdents, "%d, *, %d"), args->fd, args->count);
536 return (getdents_common(td, (struct linux_getdents64_args*)args, 0));
540 linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
544 if (ldebug(getdents64))
545 printf(ARGS(getdents64, "%d, *, %d"), args->fd, args->count);
548 return (getdents_common(td, args, 1));
552 * These exist mainly for hooks for doing /compat/linux translation.
556 linux_access(struct thread *td, struct linux_access_args *args)
561 /* linux convention */
562 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
565 LCONVPATHEXIST(td, args->path, &path);
569 printf(ARGS(access, "%s, %d"), path, args->amode);
571 error = kern_accessat(td, AT_FDCWD, path, UIO_SYSSPACE, 0,
579 linux_faccessat(struct thread *td, struct linux_faccessat_args *args)
582 int error, dfd, flag;
584 if (args->flag & ~LINUX_AT_EACCESS)
586 /* linux convention */
587 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
590 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
591 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
595 printf(ARGS(access, "%s, %d"), path, args->amode);
598 flag = (args->flag & LINUX_AT_EACCESS) == 0 ? 0 : AT_EACCESS;
599 error = kern_accessat(td, dfd, path, UIO_SYSSPACE, flag, args->amode);
606 linux_unlink(struct thread *td, struct linux_unlink_args *args)
612 LCONVPATHEXIST(td, args->path, &path);
616 printf(ARGS(unlink, "%s"), path);
619 error = kern_unlinkat(td, AT_FDCWD, path, UIO_SYSSPACE, 0);
620 if (error == EPERM) {
621 /* Introduce POSIX noncompliant behaviour of Linux */
622 if (kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &st,
624 if (S_ISDIR(st.st_mode))
633 linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args)
639 if (args->flag & ~LINUX_AT_REMOVEDIR)
642 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
643 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
646 if (ldebug(unlinkat))
647 printf(ARGS(unlinkat, "%s"), path);
650 if (args->flag & LINUX_AT_REMOVEDIR)
651 error = kern_rmdirat(td, dfd, path, UIO_SYSSPACE);
653 error = kern_unlinkat(td, dfd, path, UIO_SYSSPACE, 0);
654 if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) {
655 /* Introduce POSIX noncompliant behaviour of Linux */
656 if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path,
657 UIO_SYSSPACE, &st, NULL) == 0 && S_ISDIR(st.st_mode))
664 linux_chdir(struct thread *td, struct linux_chdir_args *args)
669 LCONVPATHEXIST(td, args->path, &path);
673 printf(ARGS(chdir, "%s"), path);
675 error = kern_chdir(td, path, UIO_SYSSPACE);
681 linux_chmod(struct thread *td, struct linux_chmod_args *args)
686 LCONVPATHEXIST(td, args->path, &path);
690 printf(ARGS(chmod, "%s, %d"), path, args->mode);
692 error = kern_fchmodat(td, AT_FDCWD, path, UIO_SYSSPACE,
699 linux_fchmodat(struct thread *td, struct linux_fchmodat_args *args)
704 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
705 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
708 if (ldebug(fchmodat))
709 printf(ARGS(fchmodat, "%s, %d"), path, args->mode);
712 error = kern_fchmodat(td, dfd, path, UIO_SYSSPACE, args->mode, 0);
718 linux_mkdir(struct thread *td, struct linux_mkdir_args *args)
723 LCONVPATHCREAT(td, args->path, &path);
727 printf(ARGS(mkdir, "%s, %d"), path, args->mode);
729 error = kern_mkdirat(td, AT_FDCWD, path, UIO_SYSSPACE, args->mode);
735 linux_mkdirat(struct thread *td, struct linux_mkdirat_args *args)
740 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
741 LCONVPATHCREAT_AT(td, args->pathname, &path, dfd);
745 printf(ARGS(mkdirat, "%s, %d"), path, args->mode);
747 error = kern_mkdirat(td, dfd, path, UIO_SYSSPACE, args->mode);
753 linux_rmdir(struct thread *td, struct linux_rmdir_args *args)
758 LCONVPATHEXIST(td, args->path, &path);
762 printf(ARGS(rmdir, "%s"), path);
764 error = kern_rmdirat(td, AT_FDCWD, path, UIO_SYSSPACE);
770 linux_rename(struct thread *td, struct linux_rename_args *args)
775 LCONVPATHEXIST(td, args->from, &from);
776 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */
777 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
785 printf(ARGS(rename, "%s, %s"), from, to);
787 error = kern_renameat(td, AT_FDCWD, from, AT_FDCWD, to, UIO_SYSSPACE);
794 linux_renameat(struct thread *td, struct linux_renameat_args *args)
797 int error, olddfd, newdfd;
799 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
800 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
801 LCONVPATHEXIST_AT(td, args->oldname, &from, olddfd);
802 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */
803 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd);
810 if (ldebug(renameat))
811 printf(ARGS(renameat, "%s, %s"), from, to);
813 error = kern_renameat(td, olddfd, from, newdfd, to, UIO_SYSSPACE);
820 linux_symlink(struct thread *td, struct linux_symlink_args *args)
825 LCONVPATHEXIST(td, args->path, &path);
826 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
827 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
835 printf(ARGS(symlink, "%s, %s"), path, to);
837 error = kern_symlinkat(td, path, AT_FDCWD, to, UIO_SYSSPACE);
844 linux_symlinkat(struct thread *td, struct linux_symlinkat_args *args)
849 dfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
850 LCONVPATHEXIST_AT(td, args->oldname, &path, dfd);
851 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
852 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, dfd);
859 if (ldebug(symlinkat))
860 printf(ARGS(symlinkat, "%s, %s"), path, to);
863 error = kern_symlinkat(td, path, dfd, to, UIO_SYSSPACE);
870 linux_readlink(struct thread *td, struct linux_readlink_args *args)
875 LCONVPATHEXIST(td, args->name, &name);
878 if (ldebug(readlink))
879 printf(ARGS(readlink, "%s, %p, %d"), name, (void *)args->buf,
882 error = kern_readlinkat(td, AT_FDCWD, name, UIO_SYSSPACE,
883 args->buf, UIO_USERSPACE, args->count);
889 linux_readlinkat(struct thread *td, struct linux_readlinkat_args *args)
894 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
895 LCONVPATHEXIST_AT(td, args->path, &name, dfd);
898 if (ldebug(readlinkat))
899 printf(ARGS(readlinkat, "%s, %p, %d"), name, (void *)args->buf,
903 error = kern_readlinkat(td, dfd, name, UIO_SYSSPACE, args->buf,
904 UIO_USERSPACE, args->bufsiz);
910 linux_truncate(struct thread *td, struct linux_truncate_args *args)
915 LCONVPATHEXIST(td, args->path, &path);
918 if (ldebug(truncate))
919 printf(ARGS(truncate, "%s, %ld"), path, (long)args->length);
922 error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
928 linux_truncate64(struct thread *td, struct linux_truncate64_args *args)
933 LCONVPATHEXIST(td, args->path, &path);
936 if (ldebug(truncate64))
937 printf(ARGS(truncate64, "%s, %jd"), path, args->length);
940 error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
945 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args)
947 struct ftruncate_args /* {
954 nuap.length = args->length;
955 return (sys_ftruncate(td, &nuap));
959 linux_link(struct thread *td, struct linux_link_args *args)
964 LCONVPATHEXIST(td, args->path, &path);
965 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
966 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
974 printf(ARGS(link, "%s, %s"), path, to);
976 error = kern_linkat(td, AT_FDCWD, AT_FDCWD, path, to, UIO_SYSSPACE,
984 linux_linkat(struct thread *td, struct linux_linkat_args *args)
987 int error, olddfd, newdfd, follow;
989 if (args->flag & ~LINUX_AT_SYMLINK_FOLLOW)
992 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
993 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
994 LCONVPATHEXIST_AT(td, args->oldname, &path, olddfd);
995 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
996 error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd);
1004 printf(ARGS(linkat, "%i, %s, %i, %s, %i"), args->olddfd, path,
1005 args->newdfd, to, args->flag);
1008 follow = (args->flag & LINUX_AT_SYMLINK_FOLLOW) == 0 ? NOFOLLOW :
1010 error = kern_linkat(td, olddfd, newdfd, path, to, UIO_SYSSPACE, follow);
1017 linux_fdatasync(td, uap)
1019 struct linux_fdatasync_args *uap;
1021 struct fsync_args bsd;
1024 return sys_fsync(td, &bsd);
1028 linux_pread(td, uap)
1030 struct linux_pread_args *uap;
1032 struct pread_args bsd;
1033 cap_rights_t rights;
1039 bsd.nbyte = uap->nbyte;
1040 bsd.offset = uap->offset;
1042 error = sys_pread(td, &bsd);
1045 /* This seems to violate POSIX but linux does it */
1046 error = fgetvp(td, uap->fd,
1047 cap_rights_init(&rights, CAP_PREAD), &vp);
1050 if (vp->v_type == VDIR) {
1061 linux_pwrite(td, uap)
1063 struct linux_pwrite_args *uap;
1065 struct pwrite_args bsd;
1069 bsd.nbyte = uap->nbyte;
1070 bsd.offset = uap->offset;
1071 return sys_pwrite(td, &bsd);
1075 linux_mount(struct thread *td, struct linux_mount_args *args)
1077 char fstypename[MFSNAMELEN];
1078 char mntonname[MNAMELEN], mntfromname[MNAMELEN];
1082 error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1,
1086 error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
1089 error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
1095 printf(ARGS(mount, "%s, %s, %s"),
1096 fstypename, mntfromname, mntonname);
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");
1109 if ((args->rwflag & 0xffff0000) == 0xc0ed0000) {
1111 * Linux SYNC flag is not included; the closest equivalent
1112 * FreeBSD has is !ASYNC, which is our default.
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;
1124 error = kernel_vmount(fsflags,
1125 "fstype", fstypename,
1126 "fspath", mntonname,
1127 "from", mntfromname,
1133 linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
1135 struct linux_umount_args args2;
1137 args2.path = args->path;
1139 return (linux_umount(td, &args2));
1143 linux_umount(struct thread *td, struct linux_umount_args *args)
1145 struct unmount_args bsd;
1147 bsd.path = args->path;
1148 bsd.flags = args->flags; /* XXX correct? */
1149 return (sys_unmount(td, &bsd));
1153 * fcntl family of syscalls
1163 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1169 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
1171 switch (linux_flock->l_type) {
1173 bsd_flock->l_type = F_RDLCK;
1176 bsd_flock->l_type = F_WRLCK;
1179 bsd_flock->l_type = F_UNLCK;
1182 bsd_flock->l_type = -1;
1185 bsd_flock->l_whence = linux_flock->l_whence;
1186 bsd_flock->l_start = (off_t)linux_flock->l_start;
1187 bsd_flock->l_len = (off_t)linux_flock->l_len;
1188 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1189 bsd_flock->l_sysid = 0;
1193 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
1195 switch (bsd_flock->l_type) {
1197 linux_flock->l_type = LINUX_F_RDLCK;
1200 linux_flock->l_type = LINUX_F_WRLCK;
1203 linux_flock->l_type = LINUX_F_UNLCK;
1206 linux_flock->l_whence = bsd_flock->l_whence;
1207 linux_flock->l_start = (l_off_t)bsd_flock->l_start;
1208 linux_flock->l_len = (l_off_t)bsd_flock->l_len;
1209 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1212 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1220 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1226 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
1228 switch (linux_flock->l_type) {
1230 bsd_flock->l_type = F_RDLCK;
1233 bsd_flock->l_type = F_WRLCK;
1236 bsd_flock->l_type = F_UNLCK;
1239 bsd_flock->l_type = -1;
1242 bsd_flock->l_whence = linux_flock->l_whence;
1243 bsd_flock->l_start = (off_t)linux_flock->l_start;
1244 bsd_flock->l_len = (off_t)linux_flock->l_len;
1245 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1246 bsd_flock->l_sysid = 0;
1250 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
1252 switch (bsd_flock->l_type) {
1254 linux_flock->l_type = LINUX_F_RDLCK;
1257 linux_flock->l_type = LINUX_F_WRLCK;
1260 linux_flock->l_type = LINUX_F_UNLCK;
1263 linux_flock->l_whence = bsd_flock->l_whence;
1264 linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
1265 linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
1266 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1268 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1271 fcntl_common(struct thread *td, struct linux_fcntl64_args *args)
1273 struct l_flock linux_flock;
1274 struct flock bsd_flock;
1275 cap_rights_t rights;
1280 switch (args->cmd) {
1282 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
1285 return (kern_fcntl(td, args->fd, F_GETFD, 0));
1288 return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
1291 error = kern_fcntl(td, args->fd, F_GETFL, 0);
1292 result = td->td_retval[0];
1293 td->td_retval[0] = 0;
1294 if (result & O_RDONLY)
1295 td->td_retval[0] |= LINUX_O_RDONLY;
1296 if (result & O_WRONLY)
1297 td->td_retval[0] |= LINUX_O_WRONLY;
1298 if (result & O_RDWR)
1299 td->td_retval[0] |= LINUX_O_RDWR;
1300 if (result & O_NDELAY)
1301 td->td_retval[0] |= LINUX_O_NONBLOCK;
1302 if (result & O_APPEND)
1303 td->td_retval[0] |= LINUX_O_APPEND;
1304 if (result & O_FSYNC)
1305 td->td_retval[0] |= LINUX_O_SYNC;
1306 if (result & O_ASYNC)
1307 td->td_retval[0] |= LINUX_FASYNC;
1308 #ifdef LINUX_O_NOFOLLOW
1309 if (result & O_NOFOLLOW)
1310 td->td_retval[0] |= LINUX_O_NOFOLLOW;
1312 #ifdef LINUX_O_DIRECT
1313 if (result & O_DIRECT)
1314 td->td_retval[0] |= LINUX_O_DIRECT;
1320 if (args->arg & LINUX_O_NDELAY)
1322 if (args->arg & LINUX_O_APPEND)
1324 if (args->arg & LINUX_O_SYNC)
1326 if (args->arg & LINUX_FASYNC)
1328 #ifdef LINUX_O_NOFOLLOW
1329 if (args->arg & LINUX_O_NOFOLLOW)
1332 #ifdef LINUX_O_DIRECT
1333 if (args->arg & LINUX_O_DIRECT)
1336 return (kern_fcntl(td, args->fd, F_SETFL, arg));
1339 error = copyin((void *)args->arg, &linux_flock,
1340 sizeof(linux_flock));
1343 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1344 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1347 bsd_to_linux_flock(&bsd_flock, &linux_flock);
1348 return (copyout(&linux_flock, (void *)args->arg,
1349 sizeof(linux_flock)));
1352 error = copyin((void *)args->arg, &linux_flock,
1353 sizeof(linux_flock));
1356 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1357 return (kern_fcntl(td, args->fd, F_SETLK,
1358 (intptr_t)&bsd_flock));
1360 case LINUX_F_SETLKW:
1361 error = copyin((void *)args->arg, &linux_flock,
1362 sizeof(linux_flock));
1365 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1366 return (kern_fcntl(td, args->fd, F_SETLKW,
1367 (intptr_t)&bsd_flock));
1369 case LINUX_F_GETOWN:
1370 return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1372 case LINUX_F_SETOWN:
1374 * XXX some Linux applications depend on F_SETOWN having no
1375 * significant effect for pipes (SIGIO is not delivered for
1376 * pipes under Linux-2.2.35 at least).
1378 error = fget(td, args->fd,
1379 cap_rights_init(&rights, CAP_FCNTL), &fp);
1382 if (fp->f_type == DTYPE_PIPE) {
1388 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1395 linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1397 struct linux_fcntl64_args args64;
1401 printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd);
1404 args64.fd = args->fd;
1405 args64.cmd = args->cmd;
1406 args64.arg = args->arg;
1407 return (fcntl_common(td, &args64));
1410 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1412 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1414 struct l_flock64 linux_flock;
1415 struct flock bsd_flock;
1419 if (ldebug(fcntl64))
1420 printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd);
1423 switch (args->cmd) {
1424 case LINUX_F_GETLK64:
1425 error = copyin((void *)args->arg, &linux_flock,
1426 sizeof(linux_flock));
1429 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1430 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1433 bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1434 return (copyout(&linux_flock, (void *)args->arg,
1435 sizeof(linux_flock)));
1437 case LINUX_F_SETLK64:
1438 error = copyin((void *)args->arg, &linux_flock,
1439 sizeof(linux_flock));
1442 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1443 return (kern_fcntl(td, args->fd, F_SETLK,
1444 (intptr_t)&bsd_flock));
1446 case LINUX_F_SETLKW64:
1447 error = copyin((void *)args->arg, &linux_flock,
1448 sizeof(linux_flock));
1451 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1452 return (kern_fcntl(td, args->fd, F_SETLKW,
1453 (intptr_t)&bsd_flock));
1456 return (fcntl_common(td, args));
1458 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1461 linux_chown(struct thread *td, struct linux_chown_args *args)
1466 LCONVPATHEXIST(td, args->path, &path);
1470 printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid);
1472 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid,
1479 linux_fchownat(struct thread *td, struct linux_fchownat_args *args)
1482 int error, dfd, flag;
1484 if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
1487 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1488 LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
1491 if (ldebug(fchownat))
1492 printf(ARGS(fchownat, "%s, %d, %d"), path, args->uid, args->gid);
1495 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 :
1496 AT_SYMLINK_NOFOLLOW;
1497 error = kern_fchownat(td, dfd, path, UIO_SYSSPACE, args->uid, args->gid,
1504 linux_lchown(struct thread *td, struct linux_lchown_args *args)
1509 LCONVPATHEXIST(td, args->path, &path);
1513 printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid);
1515 error = kern_fchownat(td, AT_FDCWD, path, UIO_SYSSPACE, args->uid,
1516 args->gid, AT_SYMLINK_NOFOLLOW);
1522 convert_fadvice(int advice)
1525 case LINUX_POSIX_FADV_NORMAL:
1526 return (POSIX_FADV_NORMAL);
1527 case LINUX_POSIX_FADV_RANDOM:
1528 return (POSIX_FADV_RANDOM);
1529 case LINUX_POSIX_FADV_SEQUENTIAL:
1530 return (POSIX_FADV_SEQUENTIAL);
1531 case LINUX_POSIX_FADV_WILLNEED:
1532 return (POSIX_FADV_WILLNEED);
1533 case LINUX_POSIX_FADV_DONTNEED:
1534 return (POSIX_FADV_DONTNEED);
1535 case LINUX_POSIX_FADV_NOREUSE:
1536 return (POSIX_FADV_NOREUSE);
1543 linux_fadvise64(struct thread *td, struct linux_fadvise64_args *args)
1547 advice = convert_fadvice(args->advice);
1550 return (kern_posix_fadvise(td, args->fd, args->offset, args->len,
1555 linux_fadvise64_64(struct thread *td, struct linux_fadvise64_64_args *args)
1559 advice = convert_fadvice(args->advice);
1562 return (kern_posix_fadvise(td, args->fd, args->offset, args->len,
1567 linux_pipe(struct thread *td, struct linux_pipe_args *args)
1574 printf(ARGS(pipe, "*"));
1577 error = kern_pipe2(td, fildes, 0);
1581 /* XXX: Close descriptors on error. */
1582 return (copyout(fildes, args->pipefds, sizeof(fildes)));
1586 linux_pipe2(struct thread *td, struct linux_pipe2_args *args)
1593 printf(ARGS(pipe2, "*, %d"), args->flags);
1596 if ((args->flags & ~(LINUX_O_NONBLOCK | LINUX_O_CLOEXEC)) != 0)
1600 if ((args->flags & LINUX_O_NONBLOCK) != 0)
1601 flags |= O_NONBLOCK;
1602 if ((args->flags & LINUX_O_CLOEXEC) != 0)
1604 error = kern_pipe2(td, fildes, flags);
1608 /* XXX: Close descriptors on error. */
1609 return (copyout(fildes, args->pipefds, sizeof(fildes)));