1 /* Copyright (C) 2000,2002-2004,2006,2009,2010 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/>. */
20 #include <sysdep-cancel.h> /* Must come before <fcntl.h>. */
24 #include <sys/syscall.h>
25 #include <kernel-features.h>
27 #ifndef __ASSUME_FCNTL64
28 /* This variable is shared with all files that check for fcntl64. */
29 int __have_no_fcntl64
;
32 #ifdef __ASSUME_F_GETOWN_EX
33 # define miss_F_GETOWN_EX 0
34 #elif !defined __ASSUME_FCNTL64
35 static int miss_F_GETOWN_EX
;
39 #if defined NO_CANCELLATION && !defined __ASSUME_FCNTL64
40 # define __fcntl_nocancel __libc_fcntl
43 #if !defined NO_CANCELLATION || !defined __ASSUME_FCNTL64
45 __fcntl_nocancel (int fd
, int cmd
, ...)
51 arg
= va_arg (ap
, void *);
54 #ifndef __ASSUME_FCNTL64
56 if (! __have_no_fcntl64
)
58 int result
= INLINE_SYSCALL (fcntl64
, 3, fd
, cmd
, arg
);
59 if (result
>= 0 || errno
!= ENOSYS
)
62 __have_no_fcntl64
= 1;
68 /* Convert arg from flock64 to flock and back. */
71 struct flock64
*fl64
= arg
;
74 fl
.l_start
= (off_t
)fl64
->l_start
;
75 /* Check if we can represent the values with the smaller type. */
76 if ((off64_t
) fl
.l_start
!= fl64
->l_start
)
79 __set_errno (EOVERFLOW
);
82 fl
.l_len
= (off_t
) fl64
->l_len
;
83 /* Check if we can represent the values with the smaller type. */
84 if ((off64_t
) fl
.l_len
!= fl64
->l_len
)
87 fl
.l_type
= fl64
->l_type
;
88 fl
.l_whence
= fl64
->l_whence
;
89 fl
.l_pid
= fl64
->l_pid
;
91 res
= INLINE_SYSCALL (fcntl
, 3, fd
, F_GETLK
, &fl
);
94 /* Everything ok, convert back. */
95 fl64
->l_type
= fl
.l_type
;
96 fl64
->l_whence
= fl
.l_whence
;
97 fl64
->l_start
= fl
.l_start
;
98 fl64
->l_len
= fl
.l_len
;
99 fl64
->l_pid
= fl
.l_pid
;
105 /* Try to convert arg from flock64 to flock. */
108 struct flock64
*fl64
= arg
;
110 fl
.l_start
= (off_t
) fl64
->l_start
;
111 /* Check if we can represent the values with the smaller type. */
112 if ((off64_t
) fl
.l_start
!= fl64
->l_start
)
115 fl
.l_len
= (off_t
)fl64
->l_len
;
116 /* Check if we can represent the values with the smaller type. */
117 if ((off64_t
) fl
.l_len
!= fl64
->l_len
)
119 __set_errno (EOVERFLOW
);
122 fl
.l_type
= fl64
->l_type
;
123 fl
.l_whence
= fl64
->l_whence
;
124 fl
.l_pid
= fl64
->l_pid
;
125 assert (F_SETLK
- F_SETLKW
== F_SETLK64
- F_SETLKW64
);
126 return INLINE_SYSCALL (fcntl
, 3, fd
, cmd
+ F_SETLK
- F_SETLK64
, &fl
);
129 if (! miss_F_GETOWN_EX
)
131 INTERNAL_SYSCALL_DECL (err
);
132 struct f_owner_ex fex
;
133 int res
= INTERNAL_SYSCALL (fcntl
, err
, 3, fd
, F_GETOWN_EX
, &fex
);
134 if (!INTERNAL_SYSCALL_ERROR_P (res
, err
))
135 return fex
.type
== F_OWNER_GID
? -fex
.pid
: fex
.pid
;
137 # ifndef __ASSUME_F_GETOWN_EX
138 if (INTERNAL_SYSCALL_ERRNO (res
, err
) == EINVAL
)
139 miss_F_GETOWN_EX
= 1;
143 __set_errno (INTERNAL_SYSCALL_ERRNO (res
, err
));
149 return INLINE_SYSCALL (fcntl
, 3, fd
, cmd
, arg
);
153 return INLINE_SYSCALL (fcntl64
, 3, fd
, cmd
, arg
);
154 #endif /* !__ASSUME_FCNTL64 */
156 #endif /* NO_CANCELLATION || !__ASSUME_FCNTL64 */
159 #ifndef __fcntl_nocancel
161 __libc_fcntl (int fd
, int cmd
, ...)
167 arg
= va_arg (ap
, void *);
170 # ifdef __ASSUME_FCNTL64
171 if (SINGLE_THREAD_P
|| (cmd
!= F_SETLKW
&& cmd
!= F_SETLKW64
))
172 return INLINE_SYSCALL (fcntl64
, 3, fd
, cmd
, arg
);
174 int oldtype
= LIBC_CANCEL_ASYNC ();
176 int result
= INLINE_SYSCALL (fcntl64
, 3, fd
, cmd
, arg
);
178 if (SINGLE_THREAD_P
|| (cmd
!= F_SETLKW
&& cmd
!= F_SETLKW64
))
179 return __fcntl_nocancel (fd
, cmd
, arg
);
181 int oldtype
= LIBC_CANCEL_ASYNC ();
183 int result
= __fcntl_nocancel (fd
, cmd
, arg
);
186 LIBC_CANCEL_RESET (oldtype
);
191 libc_hidden_def (__libc_fcntl
)
193 weak_alias (__libc_fcntl
, __fcntl
)
194 libc_hidden_weak (__fcntl
)
195 weak_alias (__libc_fcntl
, fcntl
)