xref: /netbsd-src/lib/libpthread/pthread_cancelstub.c (revision c62a74e6d5cfb2fb5034e98b983da7e2fc87709d)
1 /*	$NetBSD: pthread_cancelstub.c,v 1.2 2003/01/18 10:34:15 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Nathan J. Williams.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/msg.h>
40 #include <sys/types.h>
41 #include <sys/uio.h>
42 #include <sys/wait.h>
43 #include <fcntl.h>
44 #include <poll.h>
45 #include <stdarg.h>
46 #include <unistd.h>
47 
48 int	pthread__cancel_stub_binder;
49 
50 /*
51  * XXX this is necessary to get the prototypes for the __sigsuspend14
52  * XXX and __msync13 internal names, instead of the application-visible
53  * XXX sigsuspend and msync names. It's kind of gross, but we're pretty
54  * XXX intimate with libc already.
55  */
56 #define __LIBC12_SOURCE__
57 #include <signal.h>
58 #include <sys/mman.h>
59 
60 #include "pthread.h"
61 #include "pthread_int.h"
62 
63 int	_sys_close(int);
64 int	_sys_fcntl(int, int, ...);
65 int	_sys_fsync(int);
66 ssize_t	_sys_msgrcv(int, void *, size_t, long, int);
67 int	_sys_msgsnd(int, const void *, size_t, int);
68 int	_sys___msync13(void *, size_t, int);
69 int	_sys_nanosleep(const struct timespec *, struct timespec *);
70 int	_sys_open(const char *, int, ...);
71 int	_sys_poll(struct pollfd *, nfds_t, int);
72 ssize_t	_sys_pread(int, void *, size_t, off_t);
73 ssize_t	_sys_pwrite(int, const void *, size_t, off_t);
74 ssize_t	_sys_read(int, void *, size_t);
75 ssize_t	_sys_readv(int, const struct iovec *, int);
76 int	_sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
77 int	_sys_wait4(pid_t, int *, int, struct rusage *);
78 ssize_t	_sys_write(int, const void *, size_t);
79 ssize_t	_sys_writev(int, const struct iovec *, int);
80 
81 
82 int
83 close(int d)
84 {
85 	int retval;
86 	pthread_t self;
87 
88 	self = pthread__self();
89 	pthread__testcancel(self);
90 	retval = _sys_close(d);
91 	pthread__testcancel(self);
92 
93 	return retval;
94 }
95 
96 int
97 fcntl(int fd, int cmd, ...)
98 {
99 	int retval;
100 	pthread_t self;
101 	va_list ap;
102 
103 	self = pthread__self();
104 	pthread__testcancel(self);
105 	va_start(ap, cmd);
106 	retval = _sys_fcntl(fd, cmd, va_arg(ap, void *));
107 	va_end(ap);
108 	pthread__testcancel(self);
109 
110 	return retval;
111 }
112 
113 int
114 fsync(int d)
115 {
116 	int retval;
117 	pthread_t self;
118 
119 	self = pthread__self();
120 	pthread__testcancel(self);
121 	retval = _sys_fsync(d);
122 	pthread__testcancel(self);
123 
124 	return retval;
125 }
126 
127 ssize_t
128 msgrcv(int msgid, void *msgp, size_t msgsz, long msgtyp, int msgflg)
129 {
130 	ssize_t retval;
131 	pthread_t self;
132 
133 	self = pthread__self();
134 	pthread__testcancel(self);
135 	retval = _sys_msgrcv(msgid, msgp, msgsz, msgtyp, msgflg);
136 	pthread__testcancel(self);
137 
138 	return retval;
139 }
140 
141 int
142 msgsnd(int msgid, const void *msgp, size_t msgsz, int msgflg)
143 {
144 	int retval;
145 	pthread_t self;
146 
147 	self = pthread__self();
148 	pthread__testcancel(self);
149 	retval = _sys_msgsnd(msgid, msgp, msgsz, msgflg);
150 	pthread__testcancel(self);
151 
152 	return retval;
153 }
154 
155 int
156 __msync13(void *addr, size_t len, int flags)
157 {
158 	int retval;
159 	pthread_t self;
160 
161 	self = pthread__self();
162 	pthread__testcancel(self);
163 	retval = _sys___msync13(addr, len, flags);
164 	pthread__testcancel(self);
165 
166 	return retval;
167 }
168 
169 int
170 nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
171 {
172 	int retval;
173 	pthread_t self;
174 
175 	self = pthread__self();
176 	pthread__testcancel(self);
177 	retval = _sys_nanosleep(rqtp, rmtp);
178 	pthread__testcancel(self);
179 
180 	return retval;
181 }
182 
183 int
184 open(const char *path, int flags, ...)
185 {
186 	int retval;
187 	pthread_t self;
188 	va_list ap;
189 
190 	self = pthread__self();
191 	pthread__testcancel(self);
192 	va_start(ap, flags);
193 	retval = _sys_open(path, flags, va_arg(ap, mode_t));
194 	va_end(ap);
195 	pthread__testcancel(self);
196 
197 	return retval;
198 }
199 
200 int
201 poll(struct pollfd *fds, nfds_t nfds, int timeout)
202 {
203 	int retval;
204 	pthread_t self;
205 
206 	self = pthread__self();
207 	pthread__testcancel(self);
208 	retval = _sys_poll(fds, nfds, timeout);
209 	pthread__testcancel(self);
210 
211 	return retval;
212 }
213 
214 ssize_t
215 pread(int d, void *buf, size_t nbytes, off_t offset)
216 {
217 	ssize_t retval;
218 	pthread_t self;
219 
220 	self = pthread__self();
221 	pthread__testcancel(self);
222 	retval = _sys_pread(d, buf, nbytes, offset);
223 	pthread__testcancel(self);
224 
225 	return retval;
226 }
227 
228 ssize_t
229 pwrite(int d, const void *buf, size_t nbytes, off_t offset)
230 {
231 	ssize_t retval;
232 	pthread_t self;
233 
234 	self = pthread__self();
235 	pthread__testcancel(self);
236 	retval = _sys_pwrite(d, buf, nbytes, offset);
237 	pthread__testcancel(self);
238 
239 	return retval;
240 }
241 
242 ssize_t
243 read(int d, void *buf, size_t nbytes)
244 {
245 	ssize_t retval;
246 	pthread_t self;
247 
248 	self = pthread__self();
249 	pthread__testcancel(self);
250 	retval = _sys_read(d, buf, nbytes);
251 	pthread__testcancel(self);
252 
253 	return retval;
254 }
255 
256 ssize_t
257 readv(int d, const struct iovec *iov, int iovcnt)
258 {
259 	ssize_t retval;
260 	pthread_t self;
261 
262 	self = pthread__self();
263 	pthread__testcancel(self);
264 	retval = _sys_readv(d, iov, iovcnt);
265 	pthread__testcancel(self);
266 
267 	return retval;
268 }
269 
270 int
271 select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
272     struct timeval *timeout)
273 {
274 	int retval;
275 	pthread_t self;
276 
277 	self = pthread__self();
278 	pthread__testcancel(self);
279 	retval = _sys_select(nfds, readfds, writefds, exceptfds, timeout);
280 	pthread__testcancel(self);
281 
282 	return retval;
283 }
284 
285 pid_t
286 wait4(pid_t wpid, int *status, int options, struct rusage *rusage)
287 {
288 	pid_t retval;
289 	pthread_t self;
290 
291 	self = pthread__self();
292 	pthread__testcancel(self);
293 	retval = _sys_wait4(wpid, status, options, rusage);
294 	pthread__testcancel(self);
295 
296 	return retval;
297 }
298 
299 ssize_t
300 write(int d, const void *buf, size_t nbytes)
301 {
302 	ssize_t retval;
303 	pthread_t self;
304 
305 	self = pthread__self();
306 	pthread__testcancel(self);
307 	retval = _sys_write(d, buf, nbytes);
308 	pthread__testcancel(self);
309 
310 	return retval;
311 }
312 
313 ssize_t
314 writev(int d, const struct iovec *iov, int iovcnt)
315 {
316 	ssize_t retval;
317 	pthread_t self;
318 
319 	self = pthread__self();
320 	pthread__testcancel(self);
321 	retval = _sys_writev(d, iov, iovcnt);
322 	pthread__testcancel(self);
323 
324 	return retval;
325 }
326 
327 
328 __strong_alias(_close, close)
329 __strong_alias(_fcntl, fcntl)
330 __strong_alias(_fsync, fsync)
331 __strong_alias(_msgrcv, msgrcv)
332 __strong_alias(_msgsnd, msgsnd)
333 __strong_alias(___msync13, __msync13)
334 __strong_alias(_nanosleep, nanosleep)
335 __strong_alias(_open, open)
336 __strong_alias(_poll, poll)
337 __strong_alias(_pread, pread)
338 __strong_alias(_pwrite, pwrite)
339 __strong_alias(_read, read)
340 __strong_alias(_readv, readv)
341 __strong_alias(_select, select)
342 __strong_alias(_wait4, wait4)
343 __strong_alias(_write, write)
344 __strong_alias(_writev, writev)
345