1 /*-
2 * SPDX-License-Identifier: BSD-2-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 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/dirent.h>
32 #include <sys/fcntl.h>
33 #include <sys/file.h>
34 #include <sys/filedesc.h>
35 #include <sys/inotify.h>
36 #include <sys/lock.h>
37 #include <sys/mman.h>
38 #include <sys/selinfo.h>
39 #include <sys/pipe.h>
40 #include <sys/proc.h>
41 #include <sys/specialfd.h>
42 #include <sys/stat.h>
43 #include <sys/sx.h>
44 #include <sys/syscallsubr.h>
45 #include <sys/sysproto.h>
46 #include <sys/tty.h>
47 #include <sys/unistd.h>
48 #include <sys/vnode.h>
49
50 #ifdef COMPAT_LINUX32
51 #include <compat/freebsd32/freebsd32_misc.h>
52 #include <compat/freebsd32/freebsd32_util.h>
53 #include <machine/../linux32/linux.h>
54 #include <machine/../linux32/linux32_proto.h>
55 #else
56 #include <machine/../linux/linux.h>
57 #include <machine/../linux/linux_proto.h>
58 #endif
59 #include <compat/linux/linux_misc.h>
60 #include <compat/linux/linux_util.h>
61 #include <compat/linux/linux_file.h>
62
63 static int linux_common_open(struct thread *, int, const char *, int, int,
64 enum uio_seg);
65 static int linux_do_accessat(struct thread *t, int, const char *, int, int);
66 static int linux_getdents_error(struct thread *, int, int);
67
68 static struct bsd_to_linux_bitmap seal_bitmap[] = {
69 BITMAP_1t1_LINUX(F_SEAL_SEAL),
70 BITMAP_1t1_LINUX(F_SEAL_SHRINK),
71 BITMAP_1t1_LINUX(F_SEAL_GROW),
72 BITMAP_1t1_LINUX(F_SEAL_WRITE),
73 };
74
75 #define MFD_HUGETLB_ENTRY(_size) \
76 { \
77 .bsd_value = MFD_HUGE_##_size, \
78 .linux_value = LINUX_HUGETLB_FLAG_ENCODE_##_size \
79 }
80 static struct bsd_to_linux_bitmap mfd_bitmap[] = {
81 BITMAP_1t1_LINUX(MFD_CLOEXEC),
82 BITMAP_1t1_LINUX(MFD_ALLOW_SEALING),
83 BITMAP_1t1_LINUX(MFD_HUGETLB),
84 MFD_HUGETLB_ENTRY(64KB),
85 MFD_HUGETLB_ENTRY(512KB),
86 MFD_HUGETLB_ENTRY(1MB),
87 MFD_HUGETLB_ENTRY(2MB),
88 MFD_HUGETLB_ENTRY(8MB),
89 MFD_HUGETLB_ENTRY(16MB),
90 MFD_HUGETLB_ENTRY(32MB),
91 MFD_HUGETLB_ENTRY(256MB),
92 MFD_HUGETLB_ENTRY(512MB),
93 MFD_HUGETLB_ENTRY(1GB),
94 MFD_HUGETLB_ENTRY(2GB),
95 MFD_HUGETLB_ENTRY(16GB),
96 };
97 #undef MFD_HUGETLB_ENTRY
98
99 #ifdef LINUX_LEGACY_SYSCALLS
100 int
linux_creat(struct thread * td,struct linux_creat_args * args)101 linux_creat(struct thread *td, struct linux_creat_args *args)
102 {
103
104 return (kern_openat(td, AT_FDCWD, args->path, UIO_USERSPACE,
105 O_WRONLY | O_CREAT | O_TRUNC, args->mode));
106 }
107 #endif
108
109 int
linux_common_openflags(int l_flags)110 linux_common_openflags(int l_flags)
111 {
112 int bsd_flags;
113
114 bsd_flags = 0;
115 switch (l_flags & LINUX_O_ACCMODE) {
116 case LINUX_O_WRONLY:
117 bsd_flags |= O_WRONLY;
118 break;
119 case LINUX_O_RDWR:
120 bsd_flags |= O_RDWR;
121 break;
122 default:
123 bsd_flags |= O_RDONLY;
124 }
125 if (l_flags & LINUX_O_NDELAY)
126 bsd_flags |= O_NONBLOCK;
127 if (l_flags & LINUX_O_APPEND)
128 bsd_flags |= O_APPEND;
129 if (l_flags & LINUX_O_SYNC)
130 bsd_flags |= O_FSYNC;
131 if (l_flags & LINUX_O_CLOEXEC)
132 bsd_flags |= O_CLOEXEC;
133 if (l_flags & LINUX_O_NONBLOCK)
134 bsd_flags |= O_NONBLOCK;
135 if (l_flags & LINUX_O_ASYNC)
136 bsd_flags |= O_ASYNC;
137 if (l_flags & LINUX_O_CREAT)
138 bsd_flags |= O_CREAT;
139 if (l_flags & LINUX_O_TRUNC)
140 bsd_flags |= O_TRUNC;
141 if (l_flags & LINUX_O_EXCL)
142 bsd_flags |= O_EXCL;
143 if (l_flags & LINUX_O_NOCTTY)
144 bsd_flags |= O_NOCTTY;
145 if (l_flags & LINUX_O_DIRECT)
146 bsd_flags |= O_DIRECT;
147 if (l_flags & LINUX_O_NOFOLLOW)
148 bsd_flags |= O_NOFOLLOW;
149 if (l_flags & LINUX_O_DIRECTORY)
150 bsd_flags |= O_DIRECTORY;
151 if (l_flags & LINUX_O_PATH)
152 bsd_flags |= O_PATH;
153 /* XXX LINUX_O_NOATIME: unable to be easily implemented. */
154 return (bsd_flags);
155 }
156
157 static int
linux_common_open(struct thread * td,int dirfd,const char * path,int l_flags,int mode,enum uio_seg seg)158 linux_common_open(struct thread *td, int dirfd, const char *path, int l_flags,
159 int mode, enum uio_seg seg)
160 {
161 struct proc *p = td->td_proc;
162 struct file *fp;
163 int fd;
164 int bsd_flags, error;
165
166 bsd_flags = linux_common_openflags(l_flags);
167 error = kern_openat(td, dirfd, path, seg, bsd_flags, mode);
168 if (error != 0) {
169 if (error == EMLINK)
170 error = ELOOP;
171 goto done;
172 }
173 if (p->p_flag & P_CONTROLT)
174 goto done;
175 if (bsd_flags & O_NOCTTY)
176 goto done;
177
178 /*
179 * XXX In between kern_openat() and fget(), another process
180 * having the same filedesc could use that fd without
181 * checking below.
182 */
183 fd = td->td_retval[0];
184 if (fget(td, fd, &cap_ioctl_rights, &fp) == 0) {
185 if (fp->f_type != DTYPE_VNODE) {
186 fdrop(fp, td);
187 goto done;
188 }
189 sx_slock(&proctree_lock);
190 PROC_LOCK(p);
191 if (SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
192 PROC_UNLOCK(p);
193 sx_sunlock(&proctree_lock);
194 /* XXXPJD: Verify if TIOCSCTTY is allowed. */
195 (void) fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0,
196 td->td_ucred, td);
197 } else {
198 PROC_UNLOCK(p);
199 sx_sunlock(&proctree_lock);
200 }
201 fdrop(fp, td);
202 }
203
204 done:
205 return (error);
206 }
207
208 int
linux_openat(struct thread * td,struct linux_openat_args * args)209 linux_openat(struct thread *td, struct linux_openat_args *args)
210 {
211 int dfd;
212
213 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
214 return (linux_common_open(td, dfd, args->filename, args->flags,
215 args->mode, UIO_USERSPACE));
216 }
217
218 #ifdef LINUX_LEGACY_SYSCALLS
219 int
linux_open(struct thread * td,struct linux_open_args * args)220 linux_open(struct thread *td, struct linux_open_args *args)
221 {
222
223 return (linux_common_open(td, AT_FDCWD, args->path, args->flags,
224 args->mode, UIO_USERSPACE));
225 }
226 #endif
227
228 int
linux_name_to_handle_at(struct thread * td,struct linux_name_to_handle_at_args * args)229 linux_name_to_handle_at(struct thread *td,
230 struct linux_name_to_handle_at_args *args)
231 {
232 static const l_int valid_flags = (LINUX_AT_SYMLINK_FOLLOW |
233 LINUX_AT_EMPTY_PATH);
234 static const l_uint fh_size = sizeof(fhandle_t);
235
236 fhandle_t fh;
237 l_uint fh_bytes;
238 l_int mount_id;
239 int error, fd, bsd_flags;
240
241 if (args->flags & ~valid_flags)
242 return (EINVAL);
243
244 fd = args->dirfd;
245 if (fd == LINUX_AT_FDCWD)
246 fd = AT_FDCWD;
247
248 bsd_flags = 0;
249 if (!(args->flags & LINUX_AT_SYMLINK_FOLLOW))
250 bsd_flags |= AT_SYMLINK_NOFOLLOW;
251 if ((args->flags & LINUX_AT_EMPTY_PATH) != 0)
252 bsd_flags |= AT_EMPTY_PATH;
253
254 error = kern_getfhat(td, bsd_flags, fd, args->name,
255 UIO_USERSPACE, &fh, UIO_SYSSPACE);
256 if (error != 0)
257 return (error);
258
259 /* Emit mount_id -- required before EOVERFLOW case. */
260 mount_id = (fh.fh_fsid.val[0] ^ fh.fh_fsid.val[1]);
261 error = copyout(&mount_id, args->mnt_id, sizeof(mount_id));
262 if (error != 0)
263 return (error);
264
265 /* Check if there is room for handle. */
266 error = copyin(&args->handle->handle_bytes, &fh_bytes,
267 sizeof(fh_bytes));
268 if (error != 0)
269 return (error);
270
271 if (fh_bytes < fh_size) {
272 error = copyout(&fh_size, &args->handle->handle_bytes,
273 sizeof(fh_size));
274 if (error == 0)
275 error = EOVERFLOW;
276 return (error);
277 }
278
279 /* Emit handle. */
280 mount_id = 0;
281 /*
282 * We don't use handle_type for anything yet, but initialize a known
283 * value.
284 */
285 error = copyout(&mount_id, &args->handle->handle_type,
286 sizeof(mount_id));
287 if (error != 0)
288 return (error);
289
290 error = copyout(&fh, &args->handle->f_handle,
291 sizeof(fh));
292 return (error);
293 }
294
295 int
linux_open_by_handle_at(struct thread * td,struct linux_open_by_handle_at_args * args)296 linux_open_by_handle_at(struct thread *td,
297 struct linux_open_by_handle_at_args *args)
298 {
299 l_uint fh_bytes;
300 int bsd_flags, error;
301
302 error = copyin(&args->handle->handle_bytes, &fh_bytes,
303 sizeof(fh_bytes));
304 if (error != 0)
305 return (error);
306
307 if (fh_bytes < sizeof(fhandle_t))
308 return (EINVAL);
309
310 bsd_flags = linux_common_openflags(args->flags);
311 return (kern_fhopen(td, (void *)&args->handle->f_handle, bsd_flags));
312 }
313
314 int
linux_lseek(struct thread * td,struct linux_lseek_args * args)315 linux_lseek(struct thread *td, struct linux_lseek_args *args)
316 {
317
318 return (kern_lseek(td, args->fdes, args->off, args->whence));
319 }
320
321 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
322 int
linux_llseek(struct thread * td,struct linux_llseek_args * args)323 linux_llseek(struct thread *td, struct linux_llseek_args *args)
324 {
325 int error;
326 off_t off;
327
328 off = (args->olow) | (((off_t) args->ohigh) << 32);
329
330 error = kern_lseek(td, args->fd, off, args->whence);
331 if (error != 0)
332 return (error);
333
334 error = copyout(td->td_retval, args->res, sizeof(off_t));
335 if (error != 0)
336 return (error);
337
338 td->td_retval[0] = 0;
339 return (0);
340 }
341 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
342
343 /*
344 * Note that linux_getdents(2) and linux_getdents64(2) have the same
345 * arguments. They only differ in the definition of struct dirent they
346 * operate on.
347 * Note that linux_readdir(2) is a special case of linux_getdents(2)
348 * where count is always equals 1, meaning that the buffer is one
349 * dirent-structure in size and that the code can't handle more anyway.
350 * Note that linux_readdir(2) can't be implemented by means of linux_getdents(2)
351 * as in case when the *dent buffer size is equal to 1 linux_getdents(2) will
352 * trash user stack.
353 */
354
355 static int
linux_getdents_error(struct thread * td,int fd,int err)356 linux_getdents_error(struct thread *td, int fd, int err)
357 {
358 struct vnode *vp;
359 struct file *fp;
360 int error;
361
362 /* Linux return ENOTDIR in case when fd is not a directory. */
363 error = getvnode(td, fd, &cap_read_rights, &fp);
364 if (error != 0)
365 return (error);
366 vp = fp->f_vnode;
367 if (vp->v_type != VDIR) {
368 fdrop(fp, td);
369 return (ENOTDIR);
370 }
371 fdrop(fp, td);
372 return (err);
373 }
374
375 struct l_dirent {
376 l_ulong d_ino;
377 l_off_t d_off;
378 l_ushort d_reclen;
379 char d_name[LINUX_NAME_MAX + 1];
380 };
381
382 struct l_dirent64 {
383 uint64_t d_ino;
384 int64_t d_off;
385 l_ushort d_reclen;
386 u_char d_type;
387 char d_name[LINUX_NAME_MAX + 1];
388 };
389
390 /*
391 * Linux uses the last byte in the dirent buffer to store d_type,
392 * at least glibc-2.7 requires it. That is why l_dirent is padded with 2 bytes.
393 */
394 #define LINUX_RECLEN(namlen) \
395 roundup(offsetof(struct l_dirent, d_name) + (namlen) + 2, sizeof(l_ulong))
396
397 #define LINUX_RECLEN64(namlen) \
398 roundup(offsetof(struct l_dirent64, d_name) + (namlen) + 1, \
399 sizeof(uint64_t))
400
401 #ifdef LINUX_LEGACY_SYSCALLS
402 int
linux_getdents(struct thread * td,struct linux_getdents_args * args)403 linux_getdents(struct thread *td, struct linux_getdents_args *args)
404 {
405 struct dirent *bdp;
406 caddr_t inp, buf; /* BSD-format */
407 int len, reclen; /* BSD-format */
408 caddr_t outp; /* Linux-format */
409 int resid, linuxreclen; /* Linux-format */
410 caddr_t lbuf; /* Linux-format */
411 off_t base;
412 struct l_dirent *linux_dirent;
413 int buflen, error;
414 size_t retval;
415
416 buflen = min(args->count, MAXBSIZE);
417 buf = malloc(buflen, M_LINUX, M_WAITOK);
418
419 error = kern_getdirentries(td, args->fd, buf, buflen,
420 &base, NULL, UIO_SYSSPACE);
421 if (error != 0) {
422 error = linux_getdents_error(td, args->fd, error);
423 goto out1;
424 }
425
426 lbuf = malloc(LINUX_RECLEN(LINUX_NAME_MAX), M_LINUX, M_WAITOK | M_ZERO);
427
428 len = td->td_retval[0];
429 inp = buf;
430 outp = (caddr_t)args->dent;
431 resid = args->count;
432 retval = 0;
433
434 while (len > 0) {
435 bdp = (struct dirent *) inp;
436 reclen = bdp->d_reclen;
437 linuxreclen = LINUX_RECLEN(bdp->d_namlen);
438 /*
439 * No more space in the user supplied dirent buffer.
440 * Return EINVAL.
441 */
442 if (resid < linuxreclen) {
443 error = EINVAL;
444 goto out;
445 }
446
447 linux_dirent = (struct l_dirent*)lbuf;
448 linux_dirent->d_ino = bdp->d_fileno;
449 linux_dirent->d_off = bdp->d_off;
450 linux_dirent->d_reclen = linuxreclen;
451 /*
452 * Copy d_type to last byte of l_dirent buffer
453 */
454 lbuf[linuxreclen - 1] = bdp->d_type;
455 strlcpy(linux_dirent->d_name, bdp->d_name,
456 linuxreclen - offsetof(struct l_dirent, d_name)-1);
457 error = copyout(linux_dirent, outp, linuxreclen);
458 if (error != 0)
459 goto out;
460
461 inp += reclen;
462 base += reclen;
463 len -= reclen;
464
465 retval += linuxreclen;
466 outp += linuxreclen;
467 resid -= linuxreclen;
468 }
469 td->td_retval[0] = retval;
470
471 out:
472 free(lbuf, M_LINUX);
473 out1:
474 free(buf, M_LINUX);
475 return (error);
476 }
477 #endif
478
479 int
linux_getdents64(struct thread * td,struct linux_getdents64_args * args)480 linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
481 {
482 struct dirent *bdp;
483 caddr_t inp, buf; /* BSD-format */
484 int len, reclen; /* BSD-format */
485 caddr_t outp; /* Linux-format */
486 int resid, linuxreclen; /* Linux-format */
487 off_t base;
488 struct l_dirent64 *linux_dirent64;
489 int buflen, error;
490 size_t retval;
491
492 buflen = min(args->count, MAXBSIZE);
493 buf = malloc(buflen, M_LINUX, M_WAITOK);
494
495 error = kern_getdirentries(td, args->fd, buf, buflen,
496 &base, NULL, UIO_SYSSPACE);
497 if (error != 0) {
498 error = linux_getdents_error(td, args->fd, error);
499 goto out1;
500 }
501
502 linux_dirent64 = malloc(LINUX_RECLEN64(LINUX_NAME_MAX), M_LINUX,
503 M_WAITOK | M_ZERO);
504
505 len = td->td_retval[0];
506 inp = buf;
507 outp = (caddr_t)args->dirent;
508 resid = args->count;
509 retval = 0;
510
511 while (len > 0) {
512 bdp = (struct dirent *) inp;
513 reclen = bdp->d_reclen;
514 linuxreclen = LINUX_RECLEN64(bdp->d_namlen);
515 /*
516 * No more space in the user supplied dirent buffer.
517 * Return EINVAL.
518 */
519 if (resid < linuxreclen) {
520 error = EINVAL;
521 goto out;
522 }
523
524 linux_dirent64->d_ino = bdp->d_fileno;
525 linux_dirent64->d_off = bdp->d_off;
526 linux_dirent64->d_reclen = linuxreclen;
527 linux_dirent64->d_type = bdp->d_type;
528 strlcpy(linux_dirent64->d_name, bdp->d_name,
529 linuxreclen - offsetof(struct l_dirent64, d_name));
530 error = copyout(linux_dirent64, outp, linuxreclen);
531 if (error != 0)
532 goto out;
533
534 inp += reclen;
535 base += reclen;
536 len -= reclen;
537
538 retval += linuxreclen;
539 outp += linuxreclen;
540 resid -= linuxreclen;
541 }
542 td->td_retval[0] = retval;
543
544 out:
545 free(linux_dirent64, M_LINUX);
546 out1:
547 free(buf, M_LINUX);
548 return (error);
549 }
550
551 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
552 int
linux_readdir(struct thread * td,struct linux_readdir_args * args)553 linux_readdir(struct thread *td, struct linux_readdir_args *args)
554 {
555 struct dirent *bdp;
556 caddr_t buf; /* BSD-format */
557 int linuxreclen; /* Linux-format */
558 off_t base;
559 struct l_dirent *linux_dirent; /* Linux-format */
560 int buflen, error;
561
562 buflen = sizeof(*bdp);
563 buf = malloc(buflen, M_LINUX, M_WAITOK);
564
565 error = kern_getdirentries(td, args->fd, buf, buflen,
566 &base, NULL, UIO_SYSSPACE);
567 if (error != 0) {
568 error = linux_getdents_error(td, args->fd, error);
569 goto out;
570 }
571 if (td->td_retval[0] == 0)
572 goto out;
573
574 linux_dirent = malloc(LINUX_RECLEN(LINUX_NAME_MAX), M_LINUX,
575 M_WAITOK | M_ZERO);
576
577 bdp = (struct dirent *) buf;
578 linuxreclen = LINUX_RECLEN(bdp->d_namlen);
579
580 linux_dirent->d_ino = bdp->d_fileno;
581 linux_dirent->d_off = bdp->d_off;
582 linux_dirent->d_reclen = bdp->d_namlen;
583 strlcpy(linux_dirent->d_name, bdp->d_name,
584 linuxreclen - offsetof(struct l_dirent, d_name));
585 error = copyout(linux_dirent, args->dent, linuxreclen);
586 if (error == 0)
587 td->td_retval[0] = linuxreclen;
588
589 free(linux_dirent, M_LINUX);
590 out:
591 free(buf, M_LINUX);
592 return (error);
593 }
594 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
595
596 /*
597 * These exist mainly for hooks for doing /compat/linux translation.
598 */
599
600 #ifdef LINUX_LEGACY_SYSCALLS
601 int
linux_access(struct thread * td,struct linux_access_args * args)602 linux_access(struct thread *td, struct linux_access_args *args)
603 {
604
605 /* Linux convention. */
606 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
607 return (EINVAL);
608
609 return (kern_accessat(td, AT_FDCWD, args->path, UIO_USERSPACE, 0,
610 args->amode));
611 }
612 #endif
613
614 static int
linux_do_accessat(struct thread * td,int ldfd,const char * filename,int amode,int flags)615 linux_do_accessat(struct thread *td, int ldfd, const char *filename,
616 int amode, int flags)
617 {
618 int dfd;
619
620 /* Linux convention. */
621 if (amode & ~(F_OK | X_OK | W_OK | R_OK))
622 return (EINVAL);
623
624 dfd = (ldfd == LINUX_AT_FDCWD) ? AT_FDCWD : ldfd;
625 return (kern_accessat(td, dfd, filename, UIO_USERSPACE, flags, amode));
626 }
627
628 int
linux_faccessat(struct thread * td,struct linux_faccessat_args * args)629 linux_faccessat(struct thread *td, struct linux_faccessat_args *args)
630 {
631
632 return (linux_do_accessat(td, args->dfd, args->filename, args->amode,
633 0));
634 }
635
636 int
linux_faccessat2(struct thread * td,struct linux_faccessat2_args * args)637 linux_faccessat2(struct thread *td, struct linux_faccessat2_args *args)
638 {
639 int flags, unsupported;
640
641 unsupported = args->flags & ~(LINUX_AT_EACCESS | LINUX_AT_EMPTY_PATH |
642 LINUX_AT_SYMLINK_NOFOLLOW);
643 if (unsupported != 0) {
644 linux_msg(td, "faccessat2 unsupported flag 0x%x", unsupported);
645 return (EINVAL);
646 }
647
648 flags = (args->flags & LINUX_AT_EACCESS) == 0 ? 0 :
649 AT_EACCESS;
650 flags |= (args->flags & LINUX_AT_EMPTY_PATH) == 0 ? 0 :
651 AT_EMPTY_PATH;
652 flags |= (args->flags & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 :
653 AT_SYMLINK_NOFOLLOW;
654 return (linux_do_accessat(td, args->dfd, args->filename, args->amode,
655 flags));
656 }
657
658
659 #ifdef LINUX_LEGACY_SYSCALLS
660 int
linux_unlink(struct thread * td,struct linux_unlink_args * args)661 linux_unlink(struct thread *td, struct linux_unlink_args *args)
662 {
663 int error;
664 struct stat st;
665
666 error = kern_funlinkat(td, AT_FDCWD, args->path, FD_NONE,
667 UIO_USERSPACE, 0, 0);
668 if (error == EPERM) {
669 /* Introduce POSIX noncompliant behaviour of Linux */
670 if (kern_statat(td, 0, AT_FDCWD, args->path,
671 UIO_USERSPACE, &st) == 0) {
672 if (S_ISDIR(st.st_mode))
673 error = EISDIR;
674 }
675 }
676
677 return (error);
678 }
679 #endif
680
681 static int
linux_unlinkat_impl(struct thread * td,enum uio_seg pathseg,const char * path,int dfd,struct linux_unlinkat_args * args)682 linux_unlinkat_impl(struct thread *td, enum uio_seg pathseg, const char *path,
683 int dfd, struct linux_unlinkat_args *args)
684 {
685 struct stat st;
686 int error;
687
688 if (args->flag & LINUX_AT_REMOVEDIR)
689 error = kern_frmdirat(td, dfd, path, FD_NONE, pathseg, 0);
690 else
691 error = kern_funlinkat(td, dfd, path, FD_NONE, pathseg, 0, 0);
692 if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) {
693 /* Introduce POSIX noncompliant behaviour of Linux */
694 if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path,
695 pathseg, &st) == 0 && S_ISDIR(st.st_mode))
696 error = EISDIR;
697 }
698 return (error);
699 }
700
701 int
linux_unlinkat(struct thread * td,struct linux_unlinkat_args * args)702 linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args)
703 {
704 int dfd;
705
706 if (args->flag & ~LINUX_AT_REMOVEDIR)
707 return (EINVAL);
708 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
709 return (linux_unlinkat_impl(td, UIO_USERSPACE, args->pathname,
710 dfd, args));
711 }
712
713 int
linux_chdir(struct thread * td,struct linux_chdir_args * args)714 linux_chdir(struct thread *td, struct linux_chdir_args *args)
715 {
716
717 return (kern_chdir(td, args->path, UIO_USERSPACE));
718 }
719
720 #ifdef LINUX_LEGACY_SYSCALLS
721 int
linux_chmod(struct thread * td,struct linux_chmod_args * args)722 linux_chmod(struct thread *td, struct linux_chmod_args *args)
723 {
724
725 return (kern_fchmodat(td, AT_FDCWD, args->path, UIO_USERSPACE,
726 args->mode, 0));
727 }
728 #endif
729
730 int
linux_fchmodat(struct thread * td,struct linux_fchmodat_args * args)731 linux_fchmodat(struct thread *td, struct linux_fchmodat_args *args)
732 {
733 int dfd;
734
735 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
736 return (kern_fchmodat(td, dfd, args->filename, UIO_USERSPACE,
737 args->mode, 0));
738 }
739
740 #ifdef LINUX_LEGACY_SYSCALLS
741 int
linux_mkdir(struct thread * td,struct linux_mkdir_args * args)742 linux_mkdir(struct thread *td, struct linux_mkdir_args *args)
743 {
744
745 return (kern_mkdirat(td, AT_FDCWD, args->path, UIO_USERSPACE, args->mode));
746 }
747 #endif
748
749 int
linux_mkdirat(struct thread * td,struct linux_mkdirat_args * args)750 linux_mkdirat(struct thread *td, struct linux_mkdirat_args *args)
751 {
752 int dfd;
753
754 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
755 return (kern_mkdirat(td, dfd, args->pathname, UIO_USERSPACE, args->mode));
756 }
757
758 #ifdef LINUX_LEGACY_SYSCALLS
759 int
linux_rmdir(struct thread * td,struct linux_rmdir_args * args)760 linux_rmdir(struct thread *td, struct linux_rmdir_args *args)
761 {
762
763 return (kern_frmdirat(td, AT_FDCWD, args->path, FD_NONE,
764 UIO_USERSPACE, 0));
765 }
766
767 int
linux_rename(struct thread * td,struct linux_rename_args * args)768 linux_rename(struct thread *td, struct linux_rename_args *args)
769 {
770
771 return (kern_renameat(td, AT_FDCWD, args->from, AT_FDCWD,
772 args->to, UIO_USERSPACE));
773 }
774 #endif
775
776 int
linux_renameat(struct thread * td,struct linux_renameat_args * args)777 linux_renameat(struct thread *td, struct linux_renameat_args *args)
778 {
779 struct linux_renameat2_args renameat2_args = {
780 .olddfd = args->olddfd,
781 .oldname = args->oldname,
782 .newdfd = args->newdfd,
783 .newname = args->newname,
784 .flags = 0
785 };
786
787 return (linux_renameat2(td, &renameat2_args));
788 }
789
790 int
linux_renameat2(struct thread * td,struct linux_renameat2_args * args)791 linux_renameat2(struct thread *td, struct linux_renameat2_args *args)
792 {
793 int olddfd, newdfd;
794
795 if (args->flags != 0) {
796 if (args->flags & ~(LINUX_RENAME_EXCHANGE |
797 LINUX_RENAME_NOREPLACE | LINUX_RENAME_WHITEOUT))
798 return (EINVAL);
799 if (args->flags & LINUX_RENAME_EXCHANGE &&
800 args->flags & (LINUX_RENAME_NOREPLACE |
801 LINUX_RENAME_WHITEOUT))
802 return (EINVAL);
803 #if 0
804 /*
805 * This spams the console on Ubuntu Focal.
806 *
807 * What's needed here is a general mechanism to let users know
808 * about missing features without hogging the system.
809 */
810 linux_msg(td, "renameat2 unsupported flags 0x%x",
811 args->flags);
812 #endif
813 return (EINVAL);
814 }
815
816 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
817 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
818 return (kern_renameat(td, olddfd, args->oldname, newdfd,
819 args->newname, UIO_USERSPACE));
820 }
821
822 #ifdef LINUX_LEGACY_SYSCALLS
823 int
linux_symlink(struct thread * td,struct linux_symlink_args * args)824 linux_symlink(struct thread *td, struct linux_symlink_args *args)
825 {
826
827 return (kern_symlinkat(td, args->path, AT_FDCWD, args->to,
828 UIO_USERSPACE));
829 }
830 #endif
831
832 int
linux_symlinkat(struct thread * td,struct linux_symlinkat_args * args)833 linux_symlinkat(struct thread *td, struct linux_symlinkat_args *args)
834 {
835 int dfd;
836
837 dfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
838 return (kern_symlinkat(td, args->oldname, dfd, args->newname,
839 UIO_USERSPACE));
840 }
841
842 #ifdef LINUX_LEGACY_SYSCALLS
843 int
linux_readlink(struct thread * td,struct linux_readlink_args * args)844 linux_readlink(struct thread *td, struct linux_readlink_args *args)
845 {
846
847 if (args->count <= 0)
848 return (EINVAL);
849
850 return (kern_readlinkat(td, AT_FDCWD, args->name, UIO_USERSPACE,
851 args->buf, UIO_USERSPACE, args->count));
852 }
853 #endif
854
855 int
linux_readlinkat(struct thread * td,struct linux_readlinkat_args * args)856 linux_readlinkat(struct thread *td, struct linux_readlinkat_args *args)
857 {
858 int dfd;
859
860 if (args->bufsiz <= 0)
861 return (EINVAL);
862
863 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
864 return (kern_readlinkat(td, dfd, args->path, UIO_USERSPACE,
865 args->buf, UIO_USERSPACE, args->bufsiz));
866 }
867
868 int
linux_truncate(struct thread * td,struct linux_truncate_args * args)869 linux_truncate(struct thread *td, struct linux_truncate_args *args)
870 {
871
872 return (kern_truncate(td, args->path, UIO_USERSPACE, args->length));
873 }
874
875 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
876 int
linux_truncate64(struct thread * td,struct linux_truncate64_args * args)877 linux_truncate64(struct thread *td, struct linux_truncate64_args *args)
878 {
879 off_t length;
880
881 #if defined(__amd64__) && defined(COMPAT_LINUX32)
882 length = PAIR32TO64(off_t, args->length);
883 #else
884 length = args->length;
885 #endif
886
887 return (kern_truncate(td, args->path, UIO_USERSPACE, length));
888 }
889 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
890
891 int
linux_ftruncate(struct thread * td,struct linux_ftruncate_args * args)892 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args)
893 {
894
895 return (kern_ftruncate(td, args->fd, args->length));
896 }
897
898 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
899 int
linux_ftruncate64(struct thread * td,struct linux_ftruncate64_args * args)900 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
901 {
902 off_t length;
903
904 #if defined(__amd64__) && defined(COMPAT_LINUX32)
905 length = PAIR32TO64(off_t, args->length);
906 #else
907 length = args->length;
908 #endif
909
910 return (kern_ftruncate(td, args->fd, length));
911 }
912 #endif
913
914 #ifdef LINUX_LEGACY_SYSCALLS
915 int
linux_link(struct thread * td,struct linux_link_args * args)916 linux_link(struct thread *td, struct linux_link_args *args)
917 {
918
919 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, args->path, args->to,
920 UIO_USERSPACE, AT_SYMLINK_FOLLOW));
921 }
922 #endif
923
924 int
linux_linkat(struct thread * td,struct linux_linkat_args * args)925 linux_linkat(struct thread *td, struct linux_linkat_args *args)
926 {
927 int olddfd, newdfd, flag;
928
929 if (args->flag & ~(LINUX_AT_SYMLINK_FOLLOW | LINUX_AT_EMPTY_PATH))
930 return (EINVAL);
931
932 flag = (args->flag & LINUX_AT_SYMLINK_FOLLOW) != 0 ? AT_SYMLINK_FOLLOW :
933 0;
934 flag |= (args->flag & LINUX_AT_EMPTY_PATH) != 0 ? AT_EMPTY_PATH : 0;
935
936 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
937 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
938 return (kern_linkat(td, olddfd, newdfd, args->oldname,
939 args->newname, UIO_USERSPACE, flag));
940 }
941
942 int
linux_fdatasync(struct thread * td,struct linux_fdatasync_args * uap)943 linux_fdatasync(struct thread *td, struct linux_fdatasync_args *uap)
944 {
945
946 return (kern_fsync(td, uap->fd, false));
947 }
948
949 int
linux_sync_file_range(struct thread * td,struct linux_sync_file_range_args * uap)950 linux_sync_file_range(struct thread *td, struct linux_sync_file_range_args *uap)
951 {
952 off_t nbytes, offset;
953
954 #if defined(__amd64__) && defined(COMPAT_LINUX32)
955 nbytes = PAIR32TO64(off_t, uap->nbytes);
956 offset = PAIR32TO64(off_t, uap->offset);
957 #else
958 nbytes = uap->nbytes;
959 offset = uap->offset;
960 #endif
961
962 if (offset < 0 || nbytes < 0 ||
963 (uap->flags & ~(LINUX_SYNC_FILE_RANGE_WAIT_BEFORE |
964 LINUX_SYNC_FILE_RANGE_WRITE |
965 LINUX_SYNC_FILE_RANGE_WAIT_AFTER)) != 0) {
966 return (EINVAL);
967 }
968
969 return (kern_fsync(td, uap->fd, false));
970 }
971
972 int
linux_pread(struct thread * td,struct linux_pread_args * uap)973 linux_pread(struct thread *td, struct linux_pread_args *uap)
974 {
975 struct vnode *vp;
976 off_t offset;
977 int error;
978
979 #if defined(__amd64__) && defined(COMPAT_LINUX32)
980 offset = PAIR32TO64(off_t, uap->offset);
981 #else
982 offset = uap->offset;
983 #endif
984
985 error = kern_pread(td, uap->fd, uap->buf, uap->nbyte, offset);
986 if (error == 0) {
987 /* This seems to violate POSIX but Linux does it. */
988 error = fgetvp(td, uap->fd, &cap_pread_rights, &vp);
989 if (error != 0)
990 return (error);
991 if (vp->v_type == VDIR)
992 error = EISDIR;
993 vrele(vp);
994 }
995 return (error);
996 }
997
998 int
linux_pwrite(struct thread * td,struct linux_pwrite_args * uap)999 linux_pwrite(struct thread *td, struct linux_pwrite_args *uap)
1000 {
1001 off_t offset;
1002
1003 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1004 offset = PAIR32TO64(off_t, uap->offset);
1005 #else
1006 offset = uap->offset;
1007 #endif
1008
1009 return (linux_enobufs2eagain(td, uap->fd,
1010 kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, offset)));
1011 }
1012
1013 #define HALF_LONG_BITS ((sizeof(l_long) * NBBY / 2))
1014
1015 static inline off_t
pos_from_hilo(unsigned long high,unsigned long low)1016 pos_from_hilo(unsigned long high, unsigned long low)
1017 {
1018
1019 return (((off_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
1020 }
1021
1022 int
linux_preadv(struct thread * td,struct linux_preadv_args * uap)1023 linux_preadv(struct thread *td, struct linux_preadv_args *uap)
1024 {
1025 struct uio *auio;
1026 int error;
1027 off_t offset;
1028
1029 /*
1030 * According http://man7.org/linux/man-pages/man2/preadv.2.html#NOTES
1031 * pos_l and pos_h, respectively, contain the
1032 * low order and high order 32 bits of offset.
1033 */
1034 offset = pos_from_hilo(uap->pos_h, uap->pos_l);
1035 if (offset < 0)
1036 return (EINVAL);
1037 #ifdef COMPAT_LINUX32
1038 error = freebsd32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio);
1039 #else
1040 error = copyinuio(uap->vec, uap->vlen, &auio);
1041 #endif
1042 if (error != 0)
1043 return (error);
1044 error = kern_preadv(td, uap->fd, auio, offset);
1045 freeuio(auio);
1046 return (error);
1047 }
1048
1049 int
linux_pwritev(struct thread * td,struct linux_pwritev_args * uap)1050 linux_pwritev(struct thread *td, struct linux_pwritev_args *uap)
1051 {
1052 struct uio *auio;
1053 int error;
1054 off_t offset;
1055
1056 /*
1057 * According http://man7.org/linux/man-pages/man2/pwritev.2.html#NOTES
1058 * pos_l and pos_h, respectively, contain the
1059 * low order and high order 32 bits of offset.
1060 */
1061 offset = pos_from_hilo(uap->pos_h, uap->pos_l);
1062 if (offset < 0)
1063 return (EINVAL);
1064 #ifdef COMPAT_LINUX32
1065 error = freebsd32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio);
1066 #else
1067 error = copyinuio(uap->vec, uap->vlen, &auio);
1068 #endif
1069 if (error != 0)
1070 return (error);
1071 error = kern_pwritev(td, uap->fd, auio, offset);
1072 freeuio(auio);
1073 return (linux_enobufs2eagain(td, uap->fd, error));
1074 }
1075
1076 int
linux_mount(struct thread * td,struct linux_mount_args * args)1077 linux_mount(struct thread *td, struct linux_mount_args *args)
1078 {
1079 struct mntarg *ma = NULL;
1080 char *fstypename, *mntonname, *mntfromname, *data;
1081 int error, fsflags;
1082
1083 fstypename = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1084 mntonname = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1085 mntfromname = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1086 data = NULL;
1087 error = copyinstr(args->filesystemtype, fstypename, MNAMELEN - 1,
1088 NULL);
1089 if (error != 0)
1090 goto out;
1091 if (args->specialfile != NULL) {
1092 error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
1093 if (error != 0)
1094 goto out;
1095 } else {
1096 mntfromname[0] = '\0';
1097 }
1098 error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
1099 if (error != 0)
1100 goto out;
1101
1102 if (strcmp(fstypename, "ext2") == 0) {
1103 strcpy(fstypename, "ext2fs");
1104 } else if (strcmp(fstypename, "proc") == 0) {
1105 strcpy(fstypename, "linprocfs");
1106 } else if (strcmp(fstypename, "vfat") == 0) {
1107 strcpy(fstypename, "msdosfs");
1108 } else if (strcmp(fstypename, "fuse") == 0 ||
1109 strncmp(fstypename, "fuse.", 5) == 0) {
1110 char *fuse_options, *fuse_option, *fuse_name;
1111
1112 strcpy(mntfromname, "/dev/fuse");
1113 strcpy(fstypename, "fusefs");
1114 data = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1115 error = copyinstr(args->data, data, MNAMELEN - 1, NULL);
1116 if (error != 0)
1117 goto out;
1118
1119 fuse_options = data;
1120 while ((fuse_option = strsep(&fuse_options, ",")) != NULL) {
1121 fuse_name = strsep(&fuse_option, "=");
1122 if (fuse_name == NULL || fuse_option == NULL)
1123 goto out;
1124 ma = mount_arg(ma, fuse_name, fuse_option, -1);
1125 }
1126
1127 /*
1128 * The FUSE server uses Linux errno values instead of FreeBSD
1129 * ones; add a flag to tell fuse(4) to do errno translation.
1130 */
1131 ma = mount_arg(ma, "linux_errnos", "1", -1);
1132 }
1133
1134 fsflags = 0;
1135
1136 /*
1137 * Linux SYNC flag is not included; the closest equivalent
1138 * FreeBSD has is !ASYNC, which is our default.
1139 */
1140 if (args->rwflag & LINUX_MS_RDONLY)
1141 fsflags |= MNT_RDONLY;
1142 if (args->rwflag & LINUX_MS_NOSUID)
1143 fsflags |= MNT_NOSUID;
1144 if (args->rwflag & LINUX_MS_NOEXEC)
1145 fsflags |= MNT_NOEXEC;
1146 if (args->rwflag & LINUX_MS_REMOUNT)
1147 fsflags |= MNT_UPDATE;
1148
1149 ma = mount_arg(ma, "fstype", fstypename, -1);
1150 ma = mount_arg(ma, "fspath", mntonname, -1);
1151 ma = mount_arg(ma, "from", mntfromname, -1);
1152 error = kernel_mount(ma, fsflags);
1153 out:
1154 free(fstypename, M_TEMP);
1155 free(mntonname, M_TEMP);
1156 free(mntfromname, M_TEMP);
1157 free(data, M_TEMP);
1158 return (error);
1159 }
1160
1161 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1162 int
linux_oldumount(struct thread * td,struct linux_oldumount_args * args)1163 linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
1164 {
1165
1166 return (kern_unmount(td, args->path, 0));
1167 }
1168 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1169
1170 #ifdef LINUX_LEGACY_SYSCALLS
1171 int
linux_umount(struct thread * td,struct linux_umount_args * args)1172 linux_umount(struct thread *td, struct linux_umount_args *args)
1173 {
1174 int flags;
1175
1176 flags = 0;
1177 if ((args->flags & LINUX_MNT_FORCE) != 0) {
1178 args->flags &= ~LINUX_MNT_FORCE;
1179 flags |= MNT_FORCE;
1180 }
1181 if (args->flags != 0) {
1182 linux_msg(td, "unsupported umount2 flags %#x", args->flags);
1183 return (EINVAL);
1184 }
1185
1186 return (kern_unmount(td, args->path, flags));
1187 }
1188 #endif
1189
1190 /*
1191 * fcntl family of syscalls
1192 */
1193
1194 struct l_flock {
1195 l_short l_type;
1196 l_short l_whence;
1197 l_off_t l_start;
1198 l_off_t l_len;
1199 l_pid_t l_pid;
1200 }
1201 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1202 __packed
1203 #endif
1204 ;
1205
1206 static void
linux_to_bsd_flock(struct l_flock * linux_flock,struct flock * bsd_flock)1207 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
1208 {
1209 switch (linux_flock->l_type) {
1210 case LINUX_F_RDLCK:
1211 bsd_flock->l_type = F_RDLCK;
1212 break;
1213 case LINUX_F_WRLCK:
1214 bsd_flock->l_type = F_WRLCK;
1215 break;
1216 case LINUX_F_UNLCK:
1217 bsd_flock->l_type = F_UNLCK;
1218 break;
1219 default:
1220 bsd_flock->l_type = -1;
1221 break;
1222 }
1223 bsd_flock->l_whence = linux_flock->l_whence;
1224 bsd_flock->l_start = (off_t)linux_flock->l_start;
1225 bsd_flock->l_len = (off_t)linux_flock->l_len;
1226 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1227 bsd_flock->l_sysid = 0;
1228 }
1229
1230 static void
bsd_to_linux_flock(struct flock * bsd_flock,struct l_flock * linux_flock)1231 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
1232 {
1233 switch (bsd_flock->l_type) {
1234 case F_RDLCK:
1235 linux_flock->l_type = LINUX_F_RDLCK;
1236 break;
1237 case F_WRLCK:
1238 linux_flock->l_type = LINUX_F_WRLCK;
1239 break;
1240 case F_UNLCK:
1241 linux_flock->l_type = LINUX_F_UNLCK;
1242 break;
1243 }
1244 linux_flock->l_whence = bsd_flock->l_whence;
1245 linux_flock->l_start = (l_off_t)bsd_flock->l_start;
1246 linux_flock->l_len = (l_off_t)bsd_flock->l_len;
1247 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1248 }
1249
1250 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1251 struct l_flock64 {
1252 l_short l_type;
1253 l_short l_whence;
1254 l_loff_t l_start;
1255 l_loff_t l_len;
1256 l_pid_t l_pid;
1257 }
1258 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1259 __packed
1260 #endif
1261 ;
1262
1263 static void
linux_to_bsd_flock64(struct l_flock64 * linux_flock,struct flock * bsd_flock)1264 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
1265 {
1266 switch (linux_flock->l_type) {
1267 case LINUX_F_RDLCK:
1268 bsd_flock->l_type = F_RDLCK;
1269 break;
1270 case LINUX_F_WRLCK:
1271 bsd_flock->l_type = F_WRLCK;
1272 break;
1273 case LINUX_F_UNLCK:
1274 bsd_flock->l_type = F_UNLCK;
1275 break;
1276 default:
1277 bsd_flock->l_type = -1;
1278 break;
1279 }
1280 bsd_flock->l_whence = linux_flock->l_whence;
1281 bsd_flock->l_start = (off_t)linux_flock->l_start;
1282 bsd_flock->l_len = (off_t)linux_flock->l_len;
1283 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1284 bsd_flock->l_sysid = 0;
1285 }
1286
1287 static void
bsd_to_linux_flock64(struct flock * bsd_flock,struct l_flock64 * linux_flock)1288 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
1289 {
1290 switch (bsd_flock->l_type) {
1291 case F_RDLCK:
1292 linux_flock->l_type = LINUX_F_RDLCK;
1293 break;
1294 case F_WRLCK:
1295 linux_flock->l_type = LINUX_F_WRLCK;
1296 break;
1297 case F_UNLCK:
1298 linux_flock->l_type = LINUX_F_UNLCK;
1299 break;
1300 }
1301 linux_flock->l_whence = bsd_flock->l_whence;
1302 linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
1303 linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
1304 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1305 }
1306 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1307
1308 static int
fcntl_common(struct thread * td,struct linux_fcntl_args * args)1309 fcntl_common(struct thread *td, struct linux_fcntl_args *args)
1310 {
1311 struct l_flock linux_flock;
1312 struct flock bsd_flock;
1313 struct pipe *fpipe;
1314 struct file *fp;
1315 long arg;
1316 int error, result;
1317
1318 switch (args->cmd) {
1319 case LINUX_F_DUPFD:
1320 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
1321
1322 case LINUX_F_GETFD:
1323 return (kern_fcntl(td, args->fd, F_GETFD, 0));
1324
1325 case LINUX_F_SETFD:
1326 return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
1327
1328 case LINUX_F_GETFL:
1329 error = kern_fcntl(td, args->fd, F_GETFL, 0);
1330 result = td->td_retval[0];
1331 td->td_retval[0] = 0;
1332 if (result & O_RDONLY)
1333 td->td_retval[0] |= LINUX_O_RDONLY;
1334 if (result & O_WRONLY)
1335 td->td_retval[0] |= LINUX_O_WRONLY;
1336 if (result & O_RDWR)
1337 td->td_retval[0] |= LINUX_O_RDWR;
1338 if (result & O_NDELAY)
1339 td->td_retval[0] |= LINUX_O_NONBLOCK;
1340 if (result & O_APPEND)
1341 td->td_retval[0] |= LINUX_O_APPEND;
1342 if (result & O_FSYNC)
1343 td->td_retval[0] |= LINUX_O_SYNC;
1344 if (result & O_ASYNC)
1345 td->td_retval[0] |= LINUX_O_ASYNC;
1346 #ifdef LINUX_O_NOFOLLOW
1347 if (result & O_NOFOLLOW)
1348 td->td_retval[0] |= LINUX_O_NOFOLLOW;
1349 #endif
1350 #ifdef LINUX_O_DIRECT
1351 if (result & O_DIRECT)
1352 td->td_retval[0] |= LINUX_O_DIRECT;
1353 #endif
1354 return (error);
1355
1356 case LINUX_F_SETFL:
1357 arg = 0;
1358 if (args->arg & LINUX_O_NDELAY)
1359 arg |= O_NONBLOCK;
1360 if (args->arg & LINUX_O_APPEND)
1361 arg |= O_APPEND;
1362 if (args->arg & LINUX_O_SYNC)
1363 arg |= O_FSYNC;
1364 if (args->arg & LINUX_O_ASYNC)
1365 arg |= O_ASYNC;
1366 #ifdef LINUX_O_NOFOLLOW
1367 if (args->arg & LINUX_O_NOFOLLOW)
1368 arg |= O_NOFOLLOW;
1369 #endif
1370 #ifdef LINUX_O_DIRECT
1371 if (args->arg & LINUX_O_DIRECT)
1372 arg |= O_DIRECT;
1373 #endif
1374 return (kern_fcntl(td, args->fd, F_SETFL, arg));
1375
1376 case LINUX_F_GETLK:
1377 error = copyin((void *)args->arg, &linux_flock,
1378 sizeof(linux_flock));
1379 if (error)
1380 return (error);
1381 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1382 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1383 if (error)
1384 return (error);
1385 bsd_to_linux_flock(&bsd_flock, &linux_flock);
1386 return (copyout(&linux_flock, (void *)args->arg,
1387 sizeof(linux_flock)));
1388
1389 case LINUX_F_SETLK:
1390 error = copyin((void *)args->arg, &linux_flock,
1391 sizeof(linux_flock));
1392 if (error)
1393 return (error);
1394 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1395 return (kern_fcntl(td, args->fd, F_SETLK,
1396 (intptr_t)&bsd_flock));
1397
1398 case LINUX_F_SETLKW:
1399 error = copyin((void *)args->arg, &linux_flock,
1400 sizeof(linux_flock));
1401 if (error)
1402 return (error);
1403 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1404 return (kern_fcntl(td, args->fd, F_SETLKW,
1405 (intptr_t)&bsd_flock));
1406
1407 case LINUX_F_GETOWN:
1408 return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1409
1410 case LINUX_F_SETOWN:
1411 /*
1412 * XXX some Linux applications depend on F_SETOWN having no
1413 * significant effect for pipes (SIGIO is not delivered for
1414 * pipes under Linux-2.2.35 at least).
1415 */
1416 error = fget(td, args->fd,
1417 &cap_fcntl_rights, &fp);
1418 if (error)
1419 return (error);
1420 if (fp->f_type == DTYPE_PIPE) {
1421 fdrop(fp, td);
1422 return (EINVAL);
1423 }
1424 fdrop(fp, td);
1425
1426 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1427
1428 case LINUX_F_DUPFD_CLOEXEC:
1429 return (kern_fcntl(td, args->fd, F_DUPFD_CLOEXEC, args->arg));
1430 /*
1431 * Our F_SEAL_* values match Linux one for maximum compatibility. So we
1432 * only needed to account for different values for fcntl(2) commands.
1433 */
1434 case LINUX_F_GET_SEALS:
1435 error = kern_fcntl(td, args->fd, F_GET_SEALS, 0);
1436 if (error != 0)
1437 return (error);
1438 td->td_retval[0] = bsd_to_linux_bits(td->td_retval[0],
1439 seal_bitmap, 0);
1440 return (0);
1441
1442 case LINUX_F_ADD_SEALS:
1443 return (kern_fcntl(td, args->fd, F_ADD_SEALS,
1444 linux_to_bsd_bits(args->arg, seal_bitmap, 0)));
1445
1446 case LINUX_F_GETPIPE_SZ:
1447 error = fget(td, args->fd,
1448 &cap_fcntl_rights, &fp);
1449 if (error != 0)
1450 return (error);
1451 if (fp->f_type != DTYPE_PIPE) {
1452 fdrop(fp, td);
1453 return (EINVAL);
1454 }
1455 fpipe = fp->f_data;
1456 td->td_retval[0] = fpipe->pipe_buffer.size;
1457 fdrop(fp, td);
1458 return (0);
1459
1460 case LINUX_F_DUPFD_QUERY:
1461 error = kern_kcmp(td, td->td_proc->p_pid, td->td_proc->p_pid,
1462 KCMP_FILE, args->fd, args->arg);
1463 if (error != 0)
1464 return (error);
1465 td->td_retval[0] = (td->td_retval[0] == 0) ? 1 : 0;
1466 return (0);
1467
1468 default:
1469 linux_msg(td, "unsupported fcntl cmd %d", args->cmd);
1470 return (EINVAL);
1471 }
1472 }
1473
1474 int
linux_fcntl(struct thread * td,struct linux_fcntl_args * args)1475 linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1476 {
1477
1478 return (fcntl_common(td, args));
1479 }
1480
1481 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1482 int
linux_fcntl64(struct thread * td,struct linux_fcntl64_args * args)1483 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1484 {
1485 struct l_flock64 linux_flock;
1486 struct flock bsd_flock;
1487 struct linux_fcntl_args fcntl_args;
1488 int error;
1489
1490 switch (args->cmd) {
1491 case LINUX_F_GETLK64:
1492 error = copyin((void *)args->arg, &linux_flock,
1493 sizeof(linux_flock));
1494 if (error)
1495 return (error);
1496 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1497 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1498 if (error)
1499 return (error);
1500 bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1501 return (copyout(&linux_flock, (void *)args->arg,
1502 sizeof(linux_flock)));
1503
1504 case LINUX_F_SETLK64:
1505 error = copyin((void *)args->arg, &linux_flock,
1506 sizeof(linux_flock));
1507 if (error)
1508 return (error);
1509 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1510 return (kern_fcntl(td, args->fd, F_SETLK,
1511 (intptr_t)&bsd_flock));
1512
1513 case LINUX_F_SETLKW64:
1514 error = copyin((void *)args->arg, &linux_flock,
1515 sizeof(linux_flock));
1516 if (error)
1517 return (error);
1518 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1519 return (kern_fcntl(td, args->fd, F_SETLKW,
1520 (intptr_t)&bsd_flock));
1521 }
1522
1523 fcntl_args.fd = args->fd;
1524 fcntl_args.cmd = args->cmd;
1525 fcntl_args.arg = args->arg;
1526 return (fcntl_common(td, &fcntl_args));
1527 }
1528 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1529
1530 #ifdef LINUX_LEGACY_SYSCALLS
1531 int
linux_chown(struct thread * td,struct linux_chown_args * args)1532 linux_chown(struct thread *td, struct linux_chown_args *args)
1533 {
1534
1535 return (kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE,
1536 args->uid, args->gid, 0));
1537 }
1538 #endif
1539
1540 int
linux_fchownat(struct thread * td,struct linux_fchownat_args * args)1541 linux_fchownat(struct thread *td, struct linux_fchownat_args *args)
1542 {
1543 int dfd, flag, unsupported;
1544
1545 unsupported = args->flag & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH);
1546 if (unsupported != 0) {
1547 linux_msg(td, "fchownat unsupported flag 0x%x", unsupported);
1548 return (EINVAL);
1549 }
1550
1551 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 :
1552 AT_SYMLINK_NOFOLLOW;
1553 flag |= (args->flag & LINUX_AT_EMPTY_PATH) == 0 ? 0 :
1554 AT_EMPTY_PATH;
1555
1556 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1557 return (kern_fchownat(td, dfd, args->filename, UIO_USERSPACE,
1558 args->uid, args->gid, flag));
1559 }
1560
1561 #ifdef LINUX_LEGACY_SYSCALLS
1562 int
linux_lchown(struct thread * td,struct linux_lchown_args * args)1563 linux_lchown(struct thread *td, struct linux_lchown_args *args)
1564 {
1565
1566 return (kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE, args->uid,
1567 args->gid, AT_SYMLINK_NOFOLLOW));
1568 }
1569 #endif
1570
1571 static int
convert_fadvice(int advice)1572 convert_fadvice(int advice)
1573 {
1574 switch (advice) {
1575 case LINUX_POSIX_FADV_NORMAL:
1576 return (POSIX_FADV_NORMAL);
1577 case LINUX_POSIX_FADV_RANDOM:
1578 return (POSIX_FADV_RANDOM);
1579 case LINUX_POSIX_FADV_SEQUENTIAL:
1580 return (POSIX_FADV_SEQUENTIAL);
1581 case LINUX_POSIX_FADV_WILLNEED:
1582 return (POSIX_FADV_WILLNEED);
1583 case LINUX_POSIX_FADV_DONTNEED:
1584 return (POSIX_FADV_DONTNEED);
1585 case LINUX_POSIX_FADV_NOREUSE:
1586 return (POSIX_FADV_NOREUSE);
1587 default:
1588 return (-1);
1589 }
1590 }
1591
1592 int
linux_fadvise64(struct thread * td,struct linux_fadvise64_args * args)1593 linux_fadvise64(struct thread *td, struct linux_fadvise64_args *args)
1594 {
1595 off_t offset;
1596 int advice;
1597
1598 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1599 offset = PAIR32TO64(off_t, args->offset);
1600 #else
1601 offset = args->offset;
1602 #endif
1603
1604 advice = convert_fadvice(args->advice);
1605 if (advice == -1)
1606 return (EINVAL);
1607 return (kern_posix_fadvise(td, args->fd, offset, args->len, advice));
1608 }
1609
1610 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1611 int
linux_fadvise64_64(struct thread * td,struct linux_fadvise64_64_args * args)1612 linux_fadvise64_64(struct thread *td, struct linux_fadvise64_64_args *args)
1613 {
1614 off_t len, offset;
1615 int advice;
1616
1617 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1618 len = PAIR32TO64(off_t, args->len);
1619 offset = PAIR32TO64(off_t, args->offset);
1620 #else
1621 len = args->len;
1622 offset = args->offset;
1623 #endif
1624
1625 advice = convert_fadvice(args->advice);
1626 if (advice == -1)
1627 return (EINVAL);
1628 return (kern_posix_fadvise(td, args->fd, offset, len, advice));
1629 }
1630 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1631
1632 #ifdef LINUX_LEGACY_SYSCALLS
1633 int
linux_pipe(struct thread * td,struct linux_pipe_args * args)1634 linux_pipe(struct thread *td, struct linux_pipe_args *args)
1635 {
1636 int fildes[2];
1637 int error;
1638
1639 error = kern_pipe(td, fildes, 0, NULL, NULL);
1640 if (error != 0)
1641 return (error);
1642
1643 error = copyout(fildes, args->pipefds, sizeof(fildes));
1644 if (error != 0) {
1645 (void)kern_close(td, fildes[0]);
1646 (void)kern_close(td, fildes[1]);
1647 }
1648
1649 return (error);
1650 }
1651 #endif
1652
1653 int
linux_pipe2(struct thread * td,struct linux_pipe2_args * args)1654 linux_pipe2(struct thread *td, struct linux_pipe2_args *args)
1655 {
1656 int fildes[2];
1657 int error, flags;
1658
1659 if ((args->flags & ~(LINUX_O_NONBLOCK | LINUX_O_CLOEXEC)) != 0)
1660 return (EINVAL);
1661
1662 flags = 0;
1663 if ((args->flags & LINUX_O_NONBLOCK) != 0)
1664 flags |= O_NONBLOCK;
1665 if ((args->flags & LINUX_O_CLOEXEC) != 0)
1666 flags |= O_CLOEXEC;
1667 error = kern_pipe(td, fildes, flags, NULL, NULL);
1668 if (error != 0)
1669 return (error);
1670
1671 error = copyout(fildes, args->pipefds, sizeof(fildes));
1672 if (error != 0) {
1673 (void)kern_close(td, fildes[0]);
1674 (void)kern_close(td, fildes[1]);
1675 }
1676
1677 return (error);
1678 }
1679
1680 int
linux_dup3(struct thread * td,struct linux_dup3_args * args)1681 linux_dup3(struct thread *td, struct linux_dup3_args *args)
1682 {
1683 int cmd;
1684 intptr_t newfd;
1685
1686 if (args->oldfd == args->newfd)
1687 return (EINVAL);
1688 if ((args->flags & ~LINUX_O_CLOEXEC) != 0)
1689 return (EINVAL);
1690 if (args->flags & LINUX_O_CLOEXEC)
1691 cmd = F_DUP2FD_CLOEXEC;
1692 else
1693 cmd = F_DUP2FD;
1694
1695 newfd = args->newfd;
1696 return (kern_fcntl(td, args->oldfd, cmd, newfd));
1697 }
1698
1699 int
linux_fallocate(struct thread * td,struct linux_fallocate_args * args)1700 linux_fallocate(struct thread *td, struct linux_fallocate_args *args)
1701 {
1702 off_t len, offset;
1703
1704 /*
1705 * We emulate only posix_fallocate system call for which
1706 * mode should be 0.
1707 */
1708 if (args->mode != 0)
1709 return (EOPNOTSUPP);
1710
1711 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1712 len = PAIR32TO64(off_t, args->len);
1713 offset = PAIR32TO64(off_t, args->offset);
1714 #else
1715 len = args->len;
1716 offset = args->offset;
1717 #endif
1718
1719 return (kern_posix_fallocate(td, args->fd, offset, len));
1720 }
1721
1722 int
linux_copy_file_range(struct thread * td,struct linux_copy_file_range_args * args)1723 linux_copy_file_range(struct thread *td, struct linux_copy_file_range_args
1724 *args)
1725 {
1726 l_loff_t inoff, outoff, *inoffp, *outoffp;
1727 int error, flags;
1728
1729 /*
1730 * copy_file_range(2) on Linux doesn't define any flags (yet), so is
1731 * the native implementation. Enforce it.
1732 */
1733 if (args->flags != 0) {
1734 linux_msg(td, "copy_file_range unsupported flags 0x%x",
1735 args->flags);
1736 return (EINVAL);
1737 }
1738 flags = 0;
1739 inoffp = outoffp = NULL;
1740 if (args->off_in != NULL) {
1741 error = copyin(args->off_in, &inoff, sizeof(l_loff_t));
1742 if (error != 0)
1743 return (error);
1744 inoffp = &inoff;
1745 }
1746 if (args->off_out != NULL) {
1747 error = copyin(args->off_out, &outoff, sizeof(l_loff_t));
1748 if (error != 0)
1749 return (error);
1750 outoffp = &outoff;
1751 }
1752
1753 error = kern_copy_file_range(td, args->fd_in, inoffp, args->fd_out,
1754 outoffp, args->len, flags);
1755 if (error == 0 && args->off_in != NULL)
1756 error = copyout(inoffp, args->off_in, sizeof(l_loff_t));
1757 if (error == 0 && args->off_out != NULL)
1758 error = copyout(outoffp, args->off_out, sizeof(l_loff_t));
1759 return (error);
1760 }
1761
1762 #define LINUX_MEMFD_PREFIX "memfd:"
1763
1764 int
linux_memfd_create(struct thread * td,struct linux_memfd_create_args * args)1765 linux_memfd_create(struct thread *td, struct linux_memfd_create_args *args)
1766 {
1767 char memfd_name[LINUX_NAME_MAX + 1];
1768 int error, flags, shmflags, oflags;
1769
1770 /*
1771 * This is our clever trick to avoid the heap allocation to copy in the
1772 * uname. We don't really need to go this far out of our way, but it
1773 * does keep the rest of this function fairly clean as they don't have
1774 * to worry about cleanup on the way out.
1775 */
1776 error = copyinstr(args->uname_ptr,
1777 memfd_name + sizeof(LINUX_MEMFD_PREFIX) - 1,
1778 LINUX_NAME_MAX - sizeof(LINUX_MEMFD_PREFIX) - 1, NULL);
1779 if (error != 0) {
1780 if (error == ENAMETOOLONG)
1781 error = EINVAL;
1782 return (error);
1783 }
1784
1785 memcpy(memfd_name, LINUX_MEMFD_PREFIX, sizeof(LINUX_MEMFD_PREFIX) - 1);
1786 flags = linux_to_bsd_bits(args->flags, mfd_bitmap, 0);
1787 if ((flags & ~(MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB |
1788 MFD_HUGE_MASK)) != 0)
1789 return (EINVAL);
1790 /* Size specified but no HUGETLB. */
1791 if ((flags & MFD_HUGE_MASK) != 0 && (flags & MFD_HUGETLB) == 0)
1792 return (EINVAL);
1793 /* We don't actually support HUGETLB. */
1794 if ((flags & MFD_HUGETLB) != 0)
1795 return (ENOSYS);
1796 oflags = O_RDWR;
1797 shmflags = SHM_GROW_ON_WRITE;
1798 if ((flags & MFD_CLOEXEC) != 0)
1799 oflags |= O_CLOEXEC;
1800 if ((flags & MFD_ALLOW_SEALING) != 0)
1801 shmflags |= SHM_ALLOW_SEALING;
1802 return (kern_shm_open2(td, SHM_ANON, oflags, 0, shmflags, NULL,
1803 memfd_name, NULL));
1804 }
1805
1806 int
linux_splice(struct thread * td,struct linux_splice_args * args)1807 linux_splice(struct thread *td, struct linux_splice_args *args)
1808 {
1809
1810 linux_msg(td, "syscall splice not really implemented");
1811
1812 /*
1813 * splice(2) is documented to return EINVAL in various circumstances;
1814 * returning it instead of ENOSYS should hint the caller to use fallback
1815 * instead.
1816 */
1817 return (EINVAL);
1818 }
1819
1820 int
linux_close_range(struct thread * td,struct linux_close_range_args * args)1821 linux_close_range(struct thread *td, struct linux_close_range_args *args)
1822 {
1823 u_int flags = 0;
1824
1825 /*
1826 * Implementing close_range(CLOSE_RANGE_UNSHARE) allows Linux to
1827 * unshare filedesc table of the calling thread from others threads
1828 * in a thread group (i.e., process in the FreeBSD) or others processes,
1829 * which shares the same table, before closing the files. FreeBSD does
1830 * not have compatible unsharing mechanism due to the fact that sharing
1831 * process resources, including filedesc table, is at thread level in the
1832 * Linux, while in the FreeBSD it is at the process level.
1833 * Return EINVAL for now if the CLOSE_RANGE_UNSHARE flag is specified
1834 * until this new Linux API stabilizes.
1835 */
1836
1837 if ((args->flags & ~(LINUX_CLOSE_RANGE_CLOEXEC)) != 0)
1838 return (EINVAL);
1839 if (args->first > args->last)
1840 return (EINVAL);
1841 if ((args->flags & LINUX_CLOSE_RANGE_CLOEXEC) != 0)
1842 flags |= CLOSE_RANGE_CLOEXEC;
1843 return (kern_close_range(td, flags, args->first, args->last));
1844 }
1845
1846 int
linux_enobufs2eagain(struct thread * td,int fd,int error)1847 linux_enobufs2eagain(struct thread *td, int fd, int error)
1848 {
1849 struct file *fp;
1850
1851 if (error != ENOBUFS)
1852 return (error);
1853 if (fget(td, fd, &cap_no_rights, &fp) != 0)
1854 return (error);
1855 if (fp->f_type == DTYPE_SOCKET && (fp->f_flag & FNONBLOCK) != 0)
1856 error = EAGAIN;
1857 fdrop(fp, td);
1858 return (error);
1859 }
1860
1861 int
linux_write(struct thread * td,struct linux_write_args * args)1862 linux_write(struct thread *td, struct linux_write_args *args)
1863 {
1864 struct write_args bargs = {
1865 .fd = args->fd,
1866 .buf = args->buf,
1867 .nbyte = args->nbyte,
1868 };
1869
1870 return (linux_enobufs2eagain(td, args->fd, sys_write(td, &bargs)));
1871 }
1872
1873 int
linux_writev(struct thread * td,struct linux_writev_args * args)1874 linux_writev(struct thread *td, struct linux_writev_args *args)
1875 {
1876 struct uio *auio;
1877 int error;
1878
1879 #ifdef COMPAT_LINUX32
1880 error = freebsd32_copyinuio(PTRIN(args->iovp), args->iovcnt, &auio);
1881 #else
1882 error = copyinuio(args->iovp, args->iovcnt, &auio);
1883 #endif
1884 if (error != 0)
1885 return (error);
1886 error = kern_writev(td, args->fd, auio);
1887 freeuio(auio);
1888 return (linux_enobufs2eagain(td, args->fd, error));
1889 }
1890
1891 static int
linux_inotify_init_flags(int l_flags)1892 linux_inotify_init_flags(int l_flags)
1893 {
1894 int bsd_flags;
1895
1896 if ((l_flags & ~(LINUX_IN_CLOEXEC | LINUX_IN_NONBLOCK)) != 0)
1897 linux_msg(NULL, "inotify_init1 unsupported flags 0x%x",
1898 l_flags);
1899
1900 bsd_flags = 0;
1901 if ((l_flags & LINUX_IN_CLOEXEC) != 0)
1902 bsd_flags |= O_CLOEXEC;
1903 if ((l_flags & LINUX_IN_NONBLOCK) != 0)
1904 bsd_flags |= O_NONBLOCK;
1905 return (bsd_flags);
1906 }
1907
1908 static int
inotify_init_common(struct thread * td,int flags)1909 inotify_init_common(struct thread *td, int flags)
1910 {
1911 struct specialfd_inotify si;
1912
1913 si.flags = linux_inotify_init_flags(flags);
1914 return (kern_specialfd(td, SPECIALFD_INOTIFY, &si));
1915 }
1916
1917 #if defined(__i386__) || defined(__amd64__)
1918 int
linux_inotify_init(struct thread * td,struct linux_inotify_init_args * args)1919 linux_inotify_init(struct thread *td, struct linux_inotify_init_args *args)
1920 {
1921 return (inotify_init_common(td, 0));
1922 }
1923 #endif
1924
1925 int
linux_inotify_init1(struct thread * td,struct linux_inotify_init1_args * args)1926 linux_inotify_init1(struct thread *td, struct linux_inotify_init1_args *args)
1927 {
1928 return (inotify_init_common(td, args->flags));
1929 }
1930
1931 /*
1932 * The native implementation uses the same values for inotify events as
1933 * libinotify, which gives us binary compatibility with Linux. This simplifies
1934 * the shim implementation a lot, as otherwise we would have to handle read(2)
1935 * calls on inotify descriptors and translate events to Linux's ABI.
1936 */
1937 _Static_assert(LINUX_IN_ACCESS == IN_ACCESS,
1938 "IN_ACCESS mismatch");
1939 _Static_assert(LINUX_IN_MODIFY == IN_MODIFY,
1940 "IN_MODIFY mismatch");
1941 _Static_assert(LINUX_IN_ATTRIB == IN_ATTRIB,
1942 "IN_ATTRIB mismatch");
1943 _Static_assert(LINUX_IN_CLOSE_WRITE == IN_CLOSE_WRITE,
1944 "IN_CLOSE_WRITE mismatch");
1945 _Static_assert(LINUX_IN_CLOSE_NOWRITE == IN_CLOSE_NOWRITE,
1946 "IN_CLOSE_NOWRITE mismatch");
1947 _Static_assert(LINUX_IN_OPEN == IN_OPEN,
1948 "IN_OPEN mismatch");
1949 _Static_assert(LINUX_IN_MOVED_FROM == IN_MOVED_FROM,
1950 "IN_MOVED_FROM mismatch");
1951 _Static_assert(LINUX_IN_MOVED_TO == IN_MOVED_TO,
1952 "IN_MOVED_TO mismatch");
1953 _Static_assert(LINUX_IN_CREATE == IN_CREATE,
1954 "IN_CREATE mismatch");
1955 _Static_assert(LINUX_IN_DELETE == IN_DELETE,
1956 "IN_DELETE mismatch");
1957 _Static_assert(LINUX_IN_DELETE_SELF == IN_DELETE_SELF,
1958 "IN_DELETE_SELF mismatch");
1959 _Static_assert(LINUX_IN_MOVE_SELF == IN_MOVE_SELF,
1960 "IN_MOVE_SELF mismatch");
1961
1962 _Static_assert(LINUX_IN_UNMOUNT == IN_UNMOUNT,
1963 "IN_UNMOUNT mismatch");
1964 _Static_assert(LINUX_IN_Q_OVERFLOW == IN_Q_OVERFLOW,
1965 "IN_Q_OVERFLOW mismatch");
1966 _Static_assert(LINUX_IN_IGNORED == IN_IGNORED,
1967 "IN_IGNORED mismatch");
1968
1969 _Static_assert(LINUX_IN_ISDIR == IN_ISDIR,
1970 "IN_ISDIR mismatch");
1971 _Static_assert(LINUX_IN_ONLYDIR == IN_ONLYDIR,
1972 "IN_ONLYDIR mismatch");
1973 _Static_assert(LINUX_IN_DONT_FOLLOW == IN_DONT_FOLLOW,
1974 "IN_DONT_FOLLOW mismatch");
1975 _Static_assert(LINUX_IN_MASK_CREATE == IN_MASK_CREATE,
1976 "IN_MASK_CREATE mismatch");
1977 _Static_assert(LINUX_IN_MASK_ADD == IN_MASK_ADD,
1978 "IN_MASK_ADD mismatch");
1979 _Static_assert(LINUX_IN_ONESHOT == IN_ONESHOT,
1980 "IN_ONESHOT mismatch");
1981 _Static_assert(LINUX_IN_EXCL_UNLINK == IN_EXCL_UNLINK,
1982 "IN_EXCL_UNLINK mismatch");
1983
1984 static int
linux_inotify_watch_flags(int l_flags)1985 linux_inotify_watch_flags(int l_flags)
1986 {
1987 if ((l_flags & ~(LINUX_IN_ALL_EVENTS | LINUX_IN_ALL_FLAGS)) != 0) {
1988 linux_msg(NULL, "inotify_add_watch unsupported flags 0x%x",
1989 l_flags);
1990 }
1991
1992 return (l_flags);
1993 }
1994
1995 int
linux_inotify_add_watch(struct thread * td,struct linux_inotify_add_watch_args * args)1996 linux_inotify_add_watch(struct thread *td,
1997 struct linux_inotify_add_watch_args *args)
1998 {
1999 return (kern_inotify_add_watch(args->fd, AT_FDCWD, args->pathname,
2000 linux_inotify_watch_flags(args->mask), td));
2001 }
2002
2003 int
linux_inotify_rm_watch(struct thread * td,struct linux_inotify_rm_watch_args * args)2004 linux_inotify_rm_watch(struct thread *td,
2005 struct linux_inotify_rm_watch_args *args)
2006 {
2007 return (kern_inotify_rm_watch(args->fd, args->wd, td));
2008 }
2009