1 | #define _GNU_SOURCE |
2 | #include <fcntl.h> |
3 | #include <stdarg.h> |
4 | #include <errno.h> |
5 | #include <unistd.h> |
6 | #include <syscall.h> |
7 | #include "syscall.h" |
8 | |
9 | #include <features.h> |
10 | #if defined(__GLIBC__) && __GLIBC__ >= 2 |
11 | # define GLIBC_MINOR __GLIBC_MINOR__ |
12 | #elif defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ >= 2 |
13 | # define GLIBC_MINOR __GNU_LIBRARY_MINOR__ |
14 | #endif |
15 | |
16 | #if defined(GLIBC_MINOR) && GLIBC_MINOR >= 28 |
17 | |
18 | int fcntl64(int fd, int cmd, ...) |
19 | { |
20 | unsigned long arg; |
21 | va_list ap; |
22 | va_start(ap, cmd); |
23 | arg = va_arg(ap, unsigned long); |
24 | va_end(ap); |
25 | if (cmd == F_SETFL) arg |= O_LARGEFILE; |
26 | if (cmd == F_SETLKW) return syscall(SYS_fcntl, fd, cmd, (void *)arg); |
27 | if (cmd == F_GETOWN) { |
28 | struct f_owner_ex ex; |
29 | int ret = __syscall(SYS_fcntl, fd, F_GETOWN_EX, &ex); |
30 | if (ret == -EINVAL) return __syscall(SYS_fcntl, fd, cmd, (void *)arg); |
31 | if (ret) return __syscall_ret(ret); |
32 | return ex.type == F_OWNER_PGRP ? -ex.pid : ex.pid; |
33 | } |
34 | if (cmd == F_DUPFD_CLOEXEC) { |
35 | int ret = __syscall(SYS_fcntl, fd, F_DUPFD_CLOEXEC, arg); |
36 | if (ret != -EINVAL) { |
37 | if (ret >= 0) |
38 | __syscall(SYS_fcntl, ret, F_SETFD, FD_CLOEXEC); |
39 | return __syscall_ret(ret); |
40 | } |
41 | ret = __syscall(SYS_fcntl, fd, F_DUPFD_CLOEXEC, 0); |
42 | if (ret != -EINVAL) { |
43 | if (ret >= 0) __syscall(SYS_close, ret); |
44 | return __syscall_ret(-EINVAL); |
45 | } |
46 | ret = __syscall(SYS_fcntl, fd, F_DUPFD, arg); |
47 | if (ret >= 0) __syscall(SYS_fcntl, ret, F_SETFD, FD_CLOEXEC); |
48 | return __syscall_ret(ret); |
49 | } |
50 | switch (cmd) { |
51 | case F_SETLK: |
52 | case F_GETLK: |
53 | case F_GETOWN_EX: |
54 | case F_SETOWN_EX: |
55 | return syscall(SYS_fcntl, fd, cmd, (void *)arg); |
56 | default: |
57 | return syscall(SYS_fcntl, fd, cmd, arg); |
58 | } |
59 | } |
60 | |
61 | #endif |
62 | |