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