]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/i386/fcntl.c
Replace FSF snail mail address with URLs.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / i386 / fcntl.c
1 /* Copyright (C) 2000,2002-2004,2006,2009,2010 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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/>. */
17
18 #include <assert.h>
19 #include <errno.h>
20 #include <sysdep-cancel.h> /* Must come before <fcntl.h>. */
21 #include <fcntl.h>
22 #include <stdarg.h>
23
24 #include <sys/syscall.h>
25 #include <kernel-features.h>
26
27 #ifndef __ASSUME_FCNTL64
28 /* This variable is shared with all files that check for fcntl64. */
29 int __have_no_fcntl64;
30 #endif
31
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;
36 #endif
37
38
39 #if defined NO_CANCELLATION && !defined __ASSUME_FCNTL64
40 # define __fcntl_nocancel __libc_fcntl
41 #endif
42
43 #if !defined NO_CANCELLATION || !defined __ASSUME_FCNTL64
44 int
45 __fcntl_nocancel (int fd, int cmd, ...)
46 {
47 va_list ap;
48 void *arg;
49
50 va_start (ap, cmd);
51 arg = va_arg (ap, void *);
52 va_end (ap);
53
54 #ifndef __ASSUME_FCNTL64
55 # ifdef __NR_fcntl64
56 if (! __have_no_fcntl64)
57 {
58 int result = INLINE_SYSCALL (fcntl64, 3, fd, cmd, arg);
59 if (result >= 0 || errno != ENOSYS)
60 return result;
61
62 __have_no_fcntl64 = 1;
63 }
64 # endif
65 switch (cmd)
66 {
67 case F_GETLK64:
68 /* Convert arg from flock64 to flock and back. */
69 {
70 struct flock fl;
71 struct flock64 *fl64 = arg;
72 int res;
73
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)
77 {
78 eoverflow:
79 __set_errno (EOVERFLOW);
80 return -1;
81 }
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)
85 goto eoverflow;
86
87 fl.l_type = fl64->l_type;
88 fl.l_whence = fl64->l_whence;
89 fl.l_pid = fl64->l_pid;
90
91 res = INLINE_SYSCALL (fcntl, 3, fd, F_GETLK, &fl);
92 if (res != 0)
93 return res;
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;
100
101 return 0;
102 }
103 case F_SETLK64:
104 case F_SETLKW64:
105 /* Try to convert arg from flock64 to flock. */
106 {
107 struct flock fl;
108 struct flock64 *fl64 = arg;
109
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)
113 goto eoverflow;
114
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)
118 {
119 __set_errno (EOVERFLOW);
120 return -1;
121 }
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);
127 }
128 case F_GETOWN:
129 if (! miss_F_GETOWN_EX)
130 {
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;
136
137 # ifndef __ASSUME_F_GETOWN_EX
138 if (INTERNAL_SYSCALL_ERRNO (res, err) == EINVAL)
139 miss_F_GETOWN_EX = 1;
140 else
141 # endif
142 {
143 __set_errno (INTERNAL_SYSCALL_ERRNO (res, err));
144 return -1;
145 }
146 }
147 /* FALLTHROUGH */
148 default:
149 return INLINE_SYSCALL (fcntl, 3, fd, cmd, arg);
150 }
151 return -1;
152 #else
153 return INLINE_SYSCALL (fcntl64, 3, fd, cmd, arg);
154 #endif /* !__ASSUME_FCNTL64 */
155 }
156 #endif /* NO_CANCELLATION || !__ASSUME_FCNTL64 */
157
158
159 #ifndef __fcntl_nocancel
160 int
161 __libc_fcntl (int fd, int cmd, ...)
162 {
163 va_list ap;
164 void *arg;
165
166 va_start (ap, cmd);
167 arg = va_arg (ap, void *);
168 va_end (ap);
169
170 # ifdef __ASSUME_FCNTL64
171 if (SINGLE_THREAD_P || (cmd != F_SETLKW && cmd != F_SETLKW64))
172 return INLINE_SYSCALL (fcntl64, 3, fd, cmd, arg);
173
174 int oldtype = LIBC_CANCEL_ASYNC ();
175
176 int result = INLINE_SYSCALL (fcntl64, 3, fd, cmd, arg);
177 # else
178 if (SINGLE_THREAD_P || (cmd != F_SETLKW && cmd != F_SETLKW64))
179 return __fcntl_nocancel (fd, cmd, arg);
180
181 int oldtype = LIBC_CANCEL_ASYNC ();
182
183 int result = __fcntl_nocancel (fd, cmd, arg);
184 # endif
185
186 LIBC_CANCEL_RESET (oldtype);
187
188 return result;
189 }
190 #endif
191 libc_hidden_def (__libc_fcntl)
192
193 weak_alias (__libc_fcntl, __fcntl)
194 libc_hidden_weak (__fcntl)
195 weak_alias (__libc_fcntl, fcntl)