Consolidate Linux fcntl implementation
[glibc.git] / sysdeps / unix / sysv / linux / fcntl.c
blob76e846c6accd5a38436bc2ec7fb7f722c0ca26a2
1 /* Copyright (C) 2000-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <fcntl.h>
19 #include <stdarg.h>
20 #include <errno.h>
21 #include <sysdep-cancel.h>
23 #ifndef __NR_fcntl64
24 # define __NR_fcntl64 __NR_fcntl
25 #endif
27 #ifndef FCNTL_ADJUST_CMD
28 # define FCNTL_ADJUST_CMD(__cmd) __cmd
29 #endif
31 static int
32 fcntl_common (int fd, int cmd, void *arg)
34 if (cmd == F_GETOWN)
36 INTERNAL_SYSCALL_DECL (err);
37 struct f_owner_ex fex;
38 int res = INTERNAL_SYSCALL_CALL (fcntl64, err, fd, F_GETOWN_EX, &fex);
39 if (!INTERNAL_SYSCALL_ERROR_P (res, err))
40 return fex.type == F_OWNER_GID ? -fex.pid : fex.pid;
42 return INLINE_SYSCALL_ERROR_RETURN_VALUE (INTERNAL_SYSCALL_ERRNO (res,
43 err));
46 return INLINE_SYSCALL_CALL (fcntl64, fd, cmd, (void *) arg);
49 #ifndef NO_CANCELLATION
50 int
51 __fcntl_nocancel (int fd, int cmd, ...)
53 va_list ap;
54 void *arg;
56 va_start (ap, cmd);
57 arg = va_arg (ap, void *);
58 va_end (ap);
60 return fcntl_common (fd, cmd, arg);
62 #endif
64 int
65 __libc_fcntl (int fd, int cmd, ...)
67 va_list ap;
68 void *arg;
70 va_start (ap, cmd);
71 arg = va_arg (ap, void *);
72 va_end (ap);
74 cmd = FCNTL_ADJUST_CMD (cmd);
76 if (cmd == F_SETLKW || cmd == F_SETLKW64)
77 return SYSCALL_CANCEL (fcntl64, fd, cmd, (void *) arg);
79 return fcntl_common (fd, cmd, arg);
81 libc_hidden_def (__libc_fcntl)
83 weak_alias (__libc_fcntl, __fcntl)
84 libc_hidden_weak (__fcntl)
85 weak_alias (__libc_fcntl, fcntl)