2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / i386 / fcntl.c
blobb27373d24bc09e86a9839b5a359918d87bd02970
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
17 02111-1307 USA. */
19 #include <assert.h>
20 #include <errno.h>
21 #include <sysdep-cancel.h> /* Must come before <fcntl.h>. */
22 #include <fcntl.h>
23 #include <stdarg.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;
31 #endif
33 #if defined NO_CANCELLATION && __ASSUME_FCNTL64 == 0
34 # define __fcntl_nocancel __libc_fcntl
35 #endif
37 #if !defined NO_CANCELLATION || __ASSUME_FCNTL64 == 0
38 int
39 __fcntl_nocancel (int fd, int cmd, ...)
41 va_list ap;
42 void *arg;
44 va_start (ap, cmd);
45 arg = va_arg (ap, void *);
46 va_end (ap);
48 #if __ASSUME_FCNTL64 == 0
49 # ifdef __NR_fcntl64
50 if (! __have_no_fcntl64)
52 int result = INLINE_SYSCALL (fcntl64, 3, fd, cmd, arg);
53 if (result >= 0 || errno != ENOSYS)
54 return result;
56 __have_no_fcntl64 = 1;
58 # endif
59 switch (cmd)
61 case F_GETLK64:
62 /* Convert arg from flock64 to flock and back. */
64 struct flock fl;
65 struct flock64 *fl64 = arg;
66 int res;
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)
72 eoverflow:
73 __set_errno (EOVERFLOW);
74 return -1;
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)
79 goto eoverflow;
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);
86 if (res != 0)
87 return res;
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;
95 return 0;
97 case F_SETLK64:
98 case F_SETLKW64:
99 /* Try to convert arg from flock64 to flock. */
101 struct flock fl;
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)
107 goto eoverflow;
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);
114 return -1;
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);
122 default:
123 return INLINE_SYSCALL (fcntl, 3, fd, cmd, arg);
125 return -1;
126 #else
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, ...)
137 va_list ap;
138 void *arg;
140 va_start (ap, cmd);
141 arg = va_arg (ap, void *);
142 va_end (ap);
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);
151 #else
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);
158 #endif
160 LIBC_CANCEL_RESET (oldtype);
162 return result;
164 #endif
165 libc_hidden_def (__libc_fcntl)
167 weak_alias (__libc_fcntl, __fcntl)
168 libc_hidden_weak (__fcntl)
169 weak_alias (__libc_fcntl, fcntl)