1 /* Wrapper around system calls to provide cancellation points.
2    Copyright (C) 1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5 
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10 
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15 
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    see <http://www.gnu.org/licenses/>.  */
19 
20 #include <fcntl.h>
21 #include <sys/mman.h>
22 #include <pthread.h>
23 #include <unistd.h>
24 #include <stdarg.h>
25 #include <stddef.h>
26 #include <stdlib.h>
27 #include <termios.h>
28 #include <sys/epoll.h>
29 #include <sys/resource.h>
30 #include <sys/wait.h>
31 #include <sys/socket.h>
32 #include <sys/syscall.h>
33 
34 
35 #ifndef __PIC__
36 /* We need a hook to force this file to be linked in when static
37    libpthread is used.  */
38 const char __pthread_provide_wrappers = 0;
39 #endif
40 
41 /* Using private interface to libc (__libc_foo) to implement
42  * cancellable versions of some libc functions */
43 #define CANCELABLE_SYSCALL(res_type, name, param_list, params)			\
44 res_type __libc_##name param_list;						\
45 res_type									\
46 __attribute__ ((weak))								\
47 name param_list									\
48 {										\
49   res_type result;								\
50   int oldtype;									\
51   pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);		\
52   result = __libc_##name params;						\
53   pthread_setcanceltype (oldtype, NULL);					\
54   return result;								\
55 }
56 
57 #define CANCELABLE_SYSCALL_VA(res_type, name, param_list, params, last_arg)	\
58 res_type __libc_##name param_list;						\
59 res_type									\
60 __attribute__ ((weak))								\
61 name param_list									\
62 {										\
63   res_type result;								\
64   int oldtype;									\
65   va_list ap;									\
66   pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);		\
67   va_start (ap, last_arg);							\
68   result = __libc_##name params;						\
69   va_end (ap);									\
70   pthread_setcanceltype (oldtype, NULL);					\
71   return result;								\
72 }
73 
74 
75 /* close(2).  */
76 CANCELABLE_SYSCALL (int, close, (int fd), (fd))
77 
78 
79 /* fcntl(2).  */
80 CANCELABLE_SYSCALL_VA (int, fcntl, (int fd, int cmd, ...),
81 		       (fd, cmd, va_arg (ap, long int)), cmd)
82 
83 #if __WORDSIZE == 32
84 /* fcntl64(2).  */
85 CANCELABLE_SYSCALL_VA (int, fcntl64, (int fd, int cmd, ...),
86 		       (fd, cmd, va_arg (ap, long int)), cmd)
87 #endif
88 
89 /* fsync(2).  */
90 CANCELABLE_SYSCALL (int, fsync, (int fd), (fd))
91 
92 
93 /* lseek(2).  */
94 CANCELABLE_SYSCALL (off_t, lseek, (int fd, off_t offset, int whence),
95 		    (fd, offset, whence))
96 
97 /* lseek64(2).  */
98 CANCELABLE_SYSCALL (off64_t, lseek64, (int fd, off64_t offset, int whence),
99 		    (fd, offset, whence))
100 
101 #if defined(__NR_msync) && defined(__ARCH_USE_MMU__)
102 
103 /* msync(2).  */
104 CANCELABLE_SYSCALL (int, msync, (void *addr, size_t length, int flags),
105 		    (addr, length, flags))
106 #endif
107 
108 
109 /* nanosleep(2).  */
110 libpthread_hidden_proto(nanosleep)
111 CANCELABLE_SYSCALL (int, nanosleep, (const struct timespec *requested_time,
112 				     struct timespec *remaining),
113 		    (requested_time, remaining))
114 libpthread_hidden_def(nanosleep)
115 
116 
117 /* open(2).  */
118 CANCELABLE_SYSCALL_VA (int, open, (const char *pathname, int flags, ...),
119 		       (pathname, flags, va_arg (ap, mode_t)), flags)
120 
121 
122 /* open64(3).  */
123 CANCELABLE_SYSCALL_VA (int, open64, (const char *pathname, int flags, ...),
124 		       (pathname, flags, va_arg (ap, mode_t)), flags)
125 
126 /* pause(2).  */
127 CANCELABLE_SYSCALL (int, pause, (void), ())
128 
129 
130 /* Enable this if enabling these in syscalls.c */
131 /* pread(3).  */
132 CANCELABLE_SYSCALL (ssize_t, pread, (int fd, void *buf, size_t count,
133 				     off_t offset),
134 		    (fd, buf, count, offset))
135 
136 
137 #if defined __NR_pread64
138 /* pread64(3).  */
139 CANCELABLE_SYSCALL (ssize_t, pread64, (int fd, void *buf, size_t count,
140 				       off64_t offset),
141 		    (fd, buf, count, offset))
142 #endif
143 
144 /* pwrite(3).  */
145 CANCELABLE_SYSCALL (ssize_t, pwrite, (int fd, const void *buf, size_t n,
146 				      off_t offset),
147 		    (fd, buf, n, offset))
148 
149 
150 #if defined __NR_pwrited64
151 /* pwrite64(3).  */
152 CANCELABLE_SYSCALL (ssize_t, pwrite64, (int fd, const void *buf, size_t n,
153 					off64_t offset),
154 		    (fd, buf, n, offset))
155 #endif
156 
157 /* read(2).  */
158 CANCELABLE_SYSCALL (ssize_t, read, (int fd, void *buf, size_t count),
159 		    (fd, buf, count))
160 
161 
162 /* system(3).  */
163 CANCELABLE_SYSCALL (int, system, (const char *line), (line))
164 
165 
166 /* tcdrain(2).  */
167 CANCELABLE_SYSCALL (int, tcdrain, (int fd), (fd))
168 
169 
170 /* wait(2).  */
171 CANCELABLE_SYSCALL (__pid_t, wait, (__WAIT_STATUS_DEFN stat_loc), (stat_loc))
172 
173 
174 /* waitpid(2).  */
175 libpthread_hidden_proto(waitpid)
176 CANCELABLE_SYSCALL (__pid_t, waitpid, (__pid_t pid, int *stat_loc,
177 				       int options),
178 		    (pid, stat_loc, options))
179 libpthread_hidden_def(waitpid)
180 
181 
182 /* write(2).  */
183 CANCELABLE_SYSCALL (ssize_t, write, (int fd, const void *buf, size_t n),
184 		    (fd, buf, n))
185 
186 #if defined __UCLIBC_HAS_SOCKET__
187 /* The following system calls are thread cancellation points specified
188    in XNS.  */
189 
190 /* accept(2).  */
191 CANCELABLE_SYSCALL (int, accept, (int fd, __SOCKADDR_ARG addr,
192 				  socklen_t *addr_len),
193 		    (fd, addr, addr_len))
194 
195 #if defined __UCLIBC_LINUX_SPECIFIC__
196 /* accept4(2).  */
197 CANCELABLE_SYSCALL (int, accept4, (int fd, __SOCKADDR_ARG addr,
198 				  socklen_t *addr_len, int flags),
199 		    (fd, addr, addr_len, flags))
200 #endif
201 
202 /* connect(2).  */
203 CANCELABLE_SYSCALL (int, connect, (int fd, __CONST_SOCKADDR_ARG addr,
204 				     socklen_t len),
205 		    (fd, addr, len))
206 
207 /* recv(2).  */
208 CANCELABLE_SYSCALL (ssize_t, recv, (int fd, __ptr_t buf, size_t n, int flags),
209 		    (fd, buf, n, flags))
210 
211 /* recvfrom(2).  */
212 CANCELABLE_SYSCALL (ssize_t, recvfrom, (int fd, __ptr_t buf, size_t n, int flags,
213 					__SOCKADDR_ARG addr, socklen_t *addr_len),
214 		    (fd, buf, n, flags, addr, addr_len))
215 
216 /* recvmsg(2).  */
217 CANCELABLE_SYSCALL (ssize_t, recvmsg, (int fd, struct msghdr *message, int flags),
218 		    (fd, message, flags))
219 
220 /* send(2).  */
221 CANCELABLE_SYSCALL (ssize_t, send, (int fd, const __ptr_t buf, size_t n,
222 				    int flags),
223 		    (fd, buf, n, flags))
224 
225 /* sendmsg(2).  */
226 CANCELABLE_SYSCALL (ssize_t, sendmsg, (int fd, const struct msghdr *message,
227 				       int flags),
228 		    (fd, message, flags))
229 
230 /* sendto(2).  */
231 CANCELABLE_SYSCALL (ssize_t, sendto, (int fd, const __ptr_t buf, size_t n,
232 				      int flags, __CONST_SOCKADDR_ARG addr,
233 				      socklen_t addr_len),
234 		    (fd, buf, n, flags, addr, addr_len))
235 #endif /* __UCLIBC_HAS_SOCKET__ */
236 
237 #ifdef  __UCLIBC_HAS_EPOLL__
238 # include <sys/epoll.h>
239 # ifdef __NR_epoll_wait
240 CANCELABLE_SYSCALL (int, epoll_wait, (int epfd, struct epoll_event *events, int maxevents, int timeout),
241 		    (epfd, events, maxevents, timeout))
242 # endif
243 # ifdef __NR_epoll_pwait
244 CANCELABLE_SYSCALL (int, epoll_pwait, (int epfd, struct epoll_event *events, int maxevents, int timeout,
245 				       const sigset_t *set),
246 		    (epfd, events, maxevents, timeout, set))
247 # endif
248 #endif
249