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 defined __UCLIBC_HAS_LFS__ && __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 
90 /* fsync(2).  */
91 CANCELABLE_SYSCALL (int, fsync, (int fd), (fd))
92 
93 
94 /* lseek(2).  */
95 CANCELABLE_SYSCALL (off_t, lseek, (int fd, off_t offset, int whence),
96 		    (fd, offset, whence))
97 
98 #ifdef __UCLIBC_HAS_LFS__
99 /* lseek64(2).  */
100 CANCELABLE_SYSCALL (off64_t, lseek64, (int fd, off64_t offset, int whence),
101 		    (fd, offset, whence))
102 #endif
103 
104 #if defined(__NR_msync) && defined(__ARCH_USE_MMU__)
105 
106 /* msync(2).  */
107 CANCELABLE_SYSCALL (int, msync, (void *addr, size_t length, int flags),
108 		    (addr, length, flags))
109 #endif
110 
111 
112 /* nanosleep(2).  */
113 libpthread_hidden_proto(nanosleep)
114 CANCELABLE_SYSCALL (int, nanosleep, (const struct timespec *requested_time,
115 				     struct timespec *remaining),
116 		    (requested_time, remaining))
117 libpthread_hidden_def(nanosleep)
118 
119 
120 /* open(2).  */
121 CANCELABLE_SYSCALL_VA (int, open, (const char *pathname, int flags, ...),
122 		       (pathname, flags, va_arg (ap, mode_t)), flags)
123 
124 
125 #ifdef __UCLIBC_HAS_LFS__
126 /* open64(3).  */
127 CANCELABLE_SYSCALL_VA (int, open64, (const char *pathname, int flags, ...),
128 		       (pathname, flags, va_arg (ap, mode_t)), flags)
129 #endif
130 
131 /* pause(2).  */
132 CANCELABLE_SYSCALL (int, pause, (void), ())
133 
134 
135 /* Enable this if enabling these in syscalls.c */
136 /* pread(3).  */
137 CANCELABLE_SYSCALL (ssize_t, pread, (int fd, void *buf, size_t count,
138 				     off_t offset),
139 		    (fd, buf, count, offset))
140 
141 
142 #if defined __UCLIBC_HAS_LFS__ && defined __NR_pread64
143 /* pread64(3).  */
144 CANCELABLE_SYSCALL (ssize_t, pread64, (int fd, void *buf, size_t count,
145 				       off64_t offset),
146 		    (fd, buf, count, offset))
147 #endif
148 
149 /* pwrite(3).  */
150 CANCELABLE_SYSCALL (ssize_t, pwrite, (int fd, const void *buf, size_t n,
151 				      off_t offset),
152 		    (fd, buf, n, offset))
153 
154 
155 #if defined __UCLIBC_HAS_LFS__ && defined __NR_pwrited64
156 /* pwrite64(3).  */
157 CANCELABLE_SYSCALL (ssize_t, pwrite64, (int fd, const void *buf, size_t n,
158 					off64_t offset),
159 		    (fd, buf, n, offset))
160 #endif
161 
162 /* read(2).  */
163 CANCELABLE_SYSCALL (ssize_t, read, (int fd, void *buf, size_t count),
164 		    (fd, buf, count))
165 
166 
167 /* system(3).  */
168 CANCELABLE_SYSCALL (int, system, (const char *line), (line))
169 
170 
171 /* tcdrain(2).  */
172 CANCELABLE_SYSCALL (int, tcdrain, (int fd), (fd))
173 
174 
175 /* wait(2).  */
176 CANCELABLE_SYSCALL (__pid_t, wait, (__WAIT_STATUS_DEFN stat_loc), (stat_loc))
177 
178 
179 /* waitpid(2).  */
180 libpthread_hidden_proto(waitpid)
181 CANCELABLE_SYSCALL (__pid_t, waitpid, (__pid_t pid, int *stat_loc,
182 				       int options),
183 		    (pid, stat_loc, options))
184 libpthread_hidden_def(waitpid)
185 
186 
187 /* write(2).  */
188 CANCELABLE_SYSCALL (ssize_t, write, (int fd, const void *buf, size_t n),
189 		    (fd, buf, n))
190 
191 #if defined __UCLIBC_HAS_SOCKET__
192 /* The following system calls are thread cancellation points specified
193    in XNS.  */
194 
195 /* accept(2).  */
196 CANCELABLE_SYSCALL (int, accept, (int fd, __SOCKADDR_ARG addr,
197 				  socklen_t *addr_len),
198 		    (fd, addr, addr_len))
199 
200 /* connect(2).  */
201 CANCELABLE_SYSCALL (int, connect, (int fd, __CONST_SOCKADDR_ARG addr,
202 				     socklen_t len),
203 		    (fd, addr, len))
204 
205 /* recv(2).  */
206 CANCELABLE_SYSCALL (ssize_t, recv, (int fd, __ptr_t buf, size_t n, int flags),
207 		    (fd, buf, n, flags))
208 
209 /* recvfrom(2).  */
210 CANCELABLE_SYSCALL (ssize_t, recvfrom, (int fd, __ptr_t buf, size_t n, int flags,
211 					__SOCKADDR_ARG addr, socklen_t *addr_len),
212 		    (fd, buf, n, flags, addr, addr_len))
213 
214 /* recvmsg(2).  */
215 CANCELABLE_SYSCALL (ssize_t, recvmsg, (int fd, struct msghdr *message, int flags),
216 		    (fd, message, flags))
217 
218 /* send(2).  */
219 CANCELABLE_SYSCALL (ssize_t, send, (int fd, const __ptr_t buf, size_t n,
220 				    int flags),
221 		    (fd, buf, n, flags))
222 
223 /* sendmsg(2).  */
224 CANCELABLE_SYSCALL (ssize_t, sendmsg, (int fd, const struct msghdr *message,
225 				       int flags),
226 		    (fd, message, flags))
227 
228 /* sendto(2).  */
229 CANCELABLE_SYSCALL (ssize_t, sendto, (int fd, const __ptr_t buf, size_t n,
230 				      int flags, __CONST_SOCKADDR_ARG addr,
231 				      socklen_t addr_len),
232 		    (fd, buf, n, flags, addr, addr_len))
233 #endif /* __UCLIBC_HAS_SOCKET__ */
234 
235 #ifdef  __UCLIBC_HAS_EPOLL__
236 # include <sys/epoll.h>
237 # ifdef __NR_epoll_wait
238 CANCELABLE_SYSCALL (int, epoll_wait, (int epfd, struct epoll_event *events, int maxevents, int timeout),
239 		    (epfd, events, maxevents, timeout))
240 # endif
241 # ifdef __NR_epoll_pwait
242 CANCELABLE_SYSCALL (int, epoll_pwait, (int epfd, struct epoll_event *events, int maxevents, int timeout,
243 				       const sigset_t *set),
244 		    (epfd, events, maxevents, timeout, set))
245 # endif
246 #endif
247