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