1 /* Copyright (C) 2000,2002,2003,2004,2006 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 #include <sysdep-cancel.h> /* Must come before <fcntl.h>. */
25 #include <sys/syscall.h>
26 #include <kernel-features.h>
28 #if __ASSUME_FCNTL64 == 0
29 /* This variable is shared with all files that check for fcntl64. */
30 int __have_no_fcntl64
;
33 #if defined NO_CANCELLATION && __ASSUME_FCNTL64 == 0
34 # define __fcntl_nocancel __libc_fcntl
37 #if !defined NO_CANCELLATION || __ASSUME_FCNTL64 == 0
39 __fcntl_nocancel (int fd
, int cmd
, ...)
45 arg
= va_arg (ap
, void *);
48 #if __ASSUME_FCNTL64 == 0
50 if (! __have_no_fcntl64
)
52 int result
= INLINE_SYSCALL (fcntl64
, 3, fd
, cmd
, arg
);
53 if (result
>= 0 || errno
!= ENOSYS
)
56 __have_no_fcntl64
= 1;
62 /* Convert arg from flock64 to flock and back. */
65 struct flock64
*fl64
= arg
;
68 fl
.l_start
= (off_t
)fl64
->l_start
;
69 /* Check if we can represent the values with the smaller type. */
70 if ((off64_t
) fl
.l_start
!= fl64
->l_start
)
73 __set_errno (EOVERFLOW
);
76 fl
.l_len
= (off_t
) fl64
->l_len
;
77 /* Check if we can represent the values with the smaller type. */
78 if ((off64_t
) fl
.l_len
!= fl64
->l_len
)
81 fl
.l_type
= fl64
->l_type
;
82 fl
.l_whence
= fl64
->l_whence
;
83 fl
.l_pid
= fl64
->l_pid
;
85 res
= INLINE_SYSCALL (fcntl
, 3, fd
, F_GETLK
, &fl
);
88 /* Everything ok, convert back. */
89 fl64
->l_type
= fl
.l_type
;
90 fl64
->l_whence
= fl
.l_whence
;
91 fl64
->l_start
= fl
.l_start
;
92 fl64
->l_len
= fl
.l_len
;
93 fl64
->l_pid
= fl
.l_pid
;
99 /* Try to convert arg from flock64 to flock. */
102 struct flock64
*fl64
= arg
;
104 fl
.l_start
= (off_t
) fl64
->l_start
;
105 /* Check if we can represent the values with the smaller type. */
106 if ((off64_t
) fl
.l_start
!= fl64
->l_start
)
109 fl
.l_len
= (off_t
)fl64
->l_len
;
110 /* Check if we can represent the values with the smaller type. */
111 if ((off64_t
) fl
.l_len
!= fl64
->l_len
)
113 __set_errno (EOVERFLOW
);
116 fl
.l_type
= fl64
->l_type
;
117 fl
.l_whence
= fl64
->l_whence
;
118 fl
.l_pid
= fl64
->l_pid
;
119 assert (F_SETLK
- F_SETLKW
== F_SETLK64
- F_SETLKW64
);
120 return INLINE_SYSCALL (fcntl
, 3, fd
, cmd
+ F_SETLK
- F_SETLK64
, &fl
);
123 return INLINE_SYSCALL (fcntl
, 3, fd
, cmd
, arg
);
127 return INLINE_SYSCALL (fcntl64
, 3, fd
, cmd
, arg
);
128 #endif /* !__ASSUME_FCNTL64 */
130 #endif /* NO_CANCELLATION || !__ASSUME_FCNTL64 */
133 #ifndef __fcntl_nocancel
135 __libc_fcntl (int fd
, int cmd
, ...)
141 arg
= va_arg (ap
, void *);
144 #if __ASSUME_FCNTL64 > 0
145 if (SINGLE_THREAD_P
|| (cmd
!= F_SETLKW
&& cmd
!= F_SETLKW64
))
146 return INLINE_SYSCALL (fcntl64
, 3, fd
, cmd
, arg
);
148 int oldtype
= LIBC_CANCEL_ASYNC ();
150 int result
= INLINE_SYSCALL (fcntl64
, 3, fd
, cmd
, arg
);
152 if (SINGLE_THREAD_P
|| (cmd
!= F_SETLKW
&& cmd
!= F_SETLKW64
))
153 return __fcntl_nocancel (fd
, cmd
, arg
);
155 int oldtype
= LIBC_CANCEL_ASYNC ();
157 int result
= __fcntl_nocancel (fd
, cmd
, arg
);
160 LIBC_CANCEL_RESET (oldtype
);
165 libc_hidden_def (__libc_fcntl
)
167 weak_alias (__libc_fcntl
, __fcntl
)
168 libc_hidden_weak (__fcntl
)
169 weak_alias (__libc_fcntl
, fcntl
)