pthread_cancelstub.c revision 1.14 1 1.14 ad /* $NetBSD: pthread_cancelstub.c,v 1.14 2007/03/04 20:07:13 ad Exp $ */
2 1.2 thorpej
3 1.2 thorpej /*-
4 1.14 ad * Copyright (c) 2002, 2007 The NetBSD Foundation, Inc.
5 1.2 thorpej * All rights reserved.
6 1.2 thorpej *
7 1.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.14 ad * by Nathan J. Williams and Andrew Doran.
9 1.2 thorpej *
10 1.2 thorpej * Redistribution and use in source and binary forms, with or without
11 1.2 thorpej * modification, are permitted provided that the following conditions
12 1.2 thorpej * are met:
13 1.2 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.2 thorpej * notice, this list of conditions and the following disclaimer.
15 1.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.2 thorpej * documentation and/or other materials provided with the distribution.
18 1.2 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.2 thorpej * must display the following acknowledgement:
20 1.2 thorpej * This product includes software developed by the NetBSD
21 1.2 thorpej * Foundation, Inc. and its contributors.
22 1.2 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.2 thorpej * contributors may be used to endorse or promote products derived
24 1.2 thorpej * from this software without specific prior written permission.
25 1.2 thorpej *
26 1.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.2 thorpej */
38 1.5 lukem
39 1.5 lukem #include <sys/cdefs.h>
40 1.14 ad __RCSID("$NetBSD: pthread_cancelstub.c,v 1.14 2007/03/04 20:07:13 ad Exp $");
41 1.6 thorpej
42 1.6 thorpej /*
43 1.11 kleink * This is necessary because the names are always weak (they are not
44 1.11 kleink * POSIX functions).
45 1.6 thorpej */
46 1.6 thorpej #define fsync_range _fsync_range
47 1.11 kleink #define pollts _pollts
48 1.2 thorpej
49 1.10 kleink /*
50 1.10 kleink * XXX this is necessary to get the prototypes for the __sigsuspend14
51 1.10 kleink * XXX and __msync13 internal names, instead of the application-visible
52 1.10 kleink * XXX sigsuspend and msync names. It's kind of gross, but we're pretty
53 1.10 kleink * XXX intimate with libc already.
54 1.10 kleink */
55 1.10 kleink #define __LIBC12_SOURCE__
56 1.10 kleink
57 1.2 thorpej #include <sys/msg.h>
58 1.2 thorpej #include <sys/types.h>
59 1.2 thorpej #include <sys/uio.h>
60 1.2 thorpej #include <sys/wait.h>
61 1.2 thorpej #include <fcntl.h>
62 1.2 thorpej #include <poll.h>
63 1.2 thorpej #include <stdarg.h>
64 1.2 thorpej #include <unistd.h>
65 1.2 thorpej
66 1.2 thorpej #include <signal.h>
67 1.2 thorpej #include <sys/mman.h>
68 1.12 kleink #include <sys/select.h>
69 1.3 nathanw #include <sys/socket.h>
70 1.2 thorpej
71 1.13 christos #include <compat/sys/mman.h>
72 1.13 christos
73 1.2 thorpej #include "pthread.h"
74 1.2 thorpej #include "pthread_int.h"
75 1.2 thorpej
76 1.10 kleink int pthread__cancel_stub_binder;
77 1.10 kleink
78 1.3 nathanw int _sys_accept(int, struct sockaddr *, socklen_t *);
79 1.2 thorpej int _sys_close(int);
80 1.3 nathanw int _sys_connect(int, const struct sockaddr *, socklen_t);
81 1.2 thorpej int _sys_fcntl(int, int, ...);
82 1.9 kleink int _sys_fdatasync(int);
83 1.2 thorpej int _sys_fsync(int);
84 1.6 thorpej int _sys_fsync_range(int, int, off_t, off_t);
85 1.2 thorpej ssize_t _sys_msgrcv(int, void *, size_t, long, int);
86 1.2 thorpej int _sys_msgsnd(int, const void *, size_t, int);
87 1.2 thorpej int _sys___msync13(void *, size_t, int);
88 1.2 thorpej int _sys_open(const char *, int, ...);
89 1.2 thorpej int _sys_poll(struct pollfd *, nfds_t, int);
90 1.11 kleink int _sys_pollts(struct pollfd *, nfds_t, const struct timespec *,
91 1.11 kleink const sigset_t *);
92 1.2 thorpej ssize_t _sys_pread(int, void *, size_t, off_t);
93 1.11 kleink int _sys_pselect(int, fd_set *, fd_set *, fd_set *,
94 1.11 kleink const struct timespec *, const sigset_t *);
95 1.2 thorpej ssize_t _sys_pwrite(int, const void *, size_t, off_t);
96 1.2 thorpej ssize_t _sys_read(int, void *, size_t);
97 1.2 thorpej ssize_t _sys_readv(int, const struct iovec *, int);
98 1.2 thorpej int _sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
99 1.2 thorpej int _sys_wait4(pid_t, int *, int, struct rusage *);
100 1.2 thorpej ssize_t _sys_write(int, const void *, size_t);
101 1.2 thorpej ssize_t _sys_writev(int, const struct iovec *, int);
102 1.14 ad int _sys___sigsuspend14(const sigset_t *);
103 1.14 ad int _sigtimedwait(const sigset_t * __restrict, siginfo_t * __restrict,
104 1.14 ad const struct timespec * __restrict);
105 1.14 ad int __sigsuspend14(const sigset_t *);
106 1.2 thorpej
107 1.7 nathanw #define TESTCANCEL(id) do { \
108 1.7 nathanw if (__predict_false((id)->pt_cancel)) \
109 1.7 nathanw pthread_exit(PTHREAD_CANCELED); \
110 1.8 cl } while (/*CONSTCOND*/0)
111 1.7 nathanw
112 1.2 thorpej
113 1.2 thorpej int
114 1.3 nathanw accept(int s, struct sockaddr *addr, socklen_t *addrlen)
115 1.3 nathanw {
116 1.3 nathanw int retval;
117 1.3 nathanw pthread_t self;
118 1.3 nathanw
119 1.3 nathanw self = pthread__self();
120 1.7 nathanw TESTCANCEL(self);
121 1.3 nathanw retval = _sys_accept(s, addr, addrlen);
122 1.7 nathanw TESTCANCEL(self);
123 1.3 nathanw
124 1.3 nathanw return retval;
125 1.3 nathanw }
126 1.3 nathanw
127 1.3 nathanw int
128 1.2 thorpej close(int d)
129 1.2 thorpej {
130 1.2 thorpej int retval;
131 1.2 thorpej pthread_t self;
132 1.2 thorpej
133 1.2 thorpej self = pthread__self();
134 1.7 nathanw TESTCANCEL(self);
135 1.2 thorpej retval = _sys_close(d);
136 1.7 nathanw TESTCANCEL(self);
137 1.3 nathanw
138 1.3 nathanw return retval;
139 1.3 nathanw }
140 1.3 nathanw
141 1.3 nathanw int
142 1.3 nathanw connect(int s, const struct sockaddr *addr, socklen_t namelen)
143 1.3 nathanw {
144 1.3 nathanw int retval;
145 1.3 nathanw pthread_t self;
146 1.3 nathanw
147 1.3 nathanw self = pthread__self();
148 1.7 nathanw TESTCANCEL(self);
149 1.3 nathanw retval = _sys_connect(s, addr, namelen);
150 1.7 nathanw TESTCANCEL(self);
151 1.2 thorpej
152 1.2 thorpej return retval;
153 1.2 thorpej }
154 1.2 thorpej
155 1.2 thorpej int
156 1.2 thorpej fcntl(int fd, int cmd, ...)
157 1.2 thorpej {
158 1.2 thorpej int retval;
159 1.2 thorpej pthread_t self;
160 1.2 thorpej va_list ap;
161 1.2 thorpej
162 1.2 thorpej self = pthread__self();
163 1.7 nathanw TESTCANCEL(self);
164 1.2 thorpej va_start(ap, cmd);
165 1.2 thorpej retval = _sys_fcntl(fd, cmd, va_arg(ap, void *));
166 1.2 thorpej va_end(ap);
167 1.7 nathanw TESTCANCEL(self);
168 1.2 thorpej
169 1.2 thorpej return retval;
170 1.2 thorpej }
171 1.2 thorpej
172 1.2 thorpej int
173 1.9 kleink fdatasync(int d)
174 1.9 kleink {
175 1.9 kleink int retval;
176 1.9 kleink pthread_t self;
177 1.9 kleink
178 1.9 kleink self = pthread__self();
179 1.9 kleink TESTCANCEL(self);
180 1.9 kleink retval = _sys_fdatasync(d);
181 1.9 kleink TESTCANCEL(self);
182 1.9 kleink
183 1.9 kleink return retval;
184 1.9 kleink }
185 1.9 kleink
186 1.9 kleink int
187 1.2 thorpej fsync(int d)
188 1.2 thorpej {
189 1.2 thorpej int retval;
190 1.2 thorpej pthread_t self;
191 1.2 thorpej
192 1.2 thorpej self = pthread__self();
193 1.7 nathanw TESTCANCEL(self);
194 1.2 thorpej retval = _sys_fsync(d);
195 1.7 nathanw TESTCANCEL(self);
196 1.2 thorpej
197 1.2 thorpej return retval;
198 1.2 thorpej }
199 1.2 thorpej
200 1.6 thorpej int
201 1.6 thorpej fsync_range(int d, int f, off_t s, off_t e)
202 1.6 thorpej {
203 1.6 thorpej int retval;
204 1.6 thorpej pthread_t self;
205 1.6 thorpej
206 1.6 thorpej self = pthread__self();
207 1.7 nathanw TESTCANCEL(self);
208 1.6 thorpej retval = _sys_fsync_range(d, f, s, e);
209 1.7 nathanw TESTCANCEL(self);
210 1.6 thorpej
211 1.6 thorpej return retval;
212 1.6 thorpej }
213 1.6 thorpej
214 1.2 thorpej ssize_t
215 1.2 thorpej msgrcv(int msgid, void *msgp, size_t msgsz, long msgtyp, int msgflg)
216 1.2 thorpej {
217 1.2 thorpej ssize_t retval;
218 1.2 thorpej pthread_t self;
219 1.2 thorpej
220 1.2 thorpej self = pthread__self();
221 1.7 nathanw TESTCANCEL(self);
222 1.2 thorpej retval = _sys_msgrcv(msgid, msgp, msgsz, msgtyp, msgflg);
223 1.7 nathanw TESTCANCEL(self);
224 1.2 thorpej
225 1.2 thorpej return retval;
226 1.2 thorpej }
227 1.2 thorpej
228 1.2 thorpej int
229 1.2 thorpej msgsnd(int msgid, const void *msgp, size_t msgsz, int msgflg)
230 1.2 thorpej {
231 1.2 thorpej int retval;
232 1.2 thorpej pthread_t self;
233 1.2 thorpej
234 1.2 thorpej self = pthread__self();
235 1.7 nathanw TESTCANCEL(self);
236 1.2 thorpej retval = _sys_msgsnd(msgid, msgp, msgsz, msgflg);
237 1.7 nathanw TESTCANCEL(self);
238 1.2 thorpej
239 1.2 thorpej return retval;
240 1.2 thorpej }
241 1.2 thorpej
242 1.2 thorpej int
243 1.2 thorpej __msync13(void *addr, size_t len, int flags)
244 1.2 thorpej {
245 1.2 thorpej int retval;
246 1.2 thorpej pthread_t self;
247 1.2 thorpej
248 1.2 thorpej self = pthread__self();
249 1.7 nathanw TESTCANCEL(self);
250 1.2 thorpej retval = _sys___msync13(addr, len, flags);
251 1.7 nathanw TESTCANCEL(self);
252 1.2 thorpej
253 1.2 thorpej return retval;
254 1.2 thorpej }
255 1.2 thorpej
256 1.2 thorpej int
257 1.2 thorpej open(const char *path, int flags, ...)
258 1.2 thorpej {
259 1.2 thorpej int retval;
260 1.2 thorpej pthread_t self;
261 1.2 thorpej va_list ap;
262 1.2 thorpej
263 1.2 thorpej self = pthread__self();
264 1.7 nathanw TESTCANCEL(self);
265 1.2 thorpej va_start(ap, flags);
266 1.2 thorpej retval = _sys_open(path, flags, va_arg(ap, mode_t));
267 1.2 thorpej va_end(ap);
268 1.7 nathanw TESTCANCEL(self);
269 1.2 thorpej
270 1.2 thorpej return retval;
271 1.2 thorpej }
272 1.2 thorpej
273 1.2 thorpej int
274 1.2 thorpej poll(struct pollfd *fds, nfds_t nfds, int timeout)
275 1.2 thorpej {
276 1.2 thorpej int retval;
277 1.2 thorpej pthread_t self;
278 1.2 thorpej
279 1.2 thorpej self = pthread__self();
280 1.7 nathanw TESTCANCEL(self);
281 1.2 thorpej retval = _sys_poll(fds, nfds, timeout);
282 1.7 nathanw TESTCANCEL(self);
283 1.2 thorpej
284 1.2 thorpej return retval;
285 1.2 thorpej }
286 1.2 thorpej
287 1.11 kleink int
288 1.11 kleink pollts(struct pollfd *fds, nfds_t nfds, const struct timespec *ts,
289 1.11 kleink const sigset_t *sigmask)
290 1.11 kleink {
291 1.11 kleink int retval;
292 1.11 kleink pthread_t self;
293 1.11 kleink
294 1.11 kleink self = pthread__self();
295 1.11 kleink TESTCANCEL(self);
296 1.11 kleink retval = _sys_pollts(fds, nfds, ts, sigmask);
297 1.11 kleink TESTCANCEL(self);
298 1.11 kleink
299 1.11 kleink return retval;
300 1.11 kleink }
301 1.11 kleink
302 1.2 thorpej ssize_t
303 1.2 thorpej pread(int d, void *buf, size_t nbytes, off_t offset)
304 1.2 thorpej {
305 1.2 thorpej ssize_t retval;
306 1.2 thorpej pthread_t self;
307 1.2 thorpej
308 1.2 thorpej self = pthread__self();
309 1.7 nathanw TESTCANCEL(self);
310 1.2 thorpej retval = _sys_pread(d, buf, nbytes, offset);
311 1.7 nathanw TESTCANCEL(self);
312 1.2 thorpej
313 1.2 thorpej return retval;
314 1.2 thorpej }
315 1.2 thorpej
316 1.11 kleink int
317 1.11 kleink pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
318 1.11 kleink const struct timespec *timeout, const sigset_t *sigmask)
319 1.11 kleink {
320 1.11 kleink int retval;
321 1.11 kleink pthread_t self;
322 1.11 kleink
323 1.11 kleink self = pthread__self();
324 1.11 kleink TESTCANCEL(self);
325 1.11 kleink retval = _sys_pselect(nfds, readfds, writefds, exceptfds, timeout,
326 1.11 kleink sigmask);
327 1.11 kleink TESTCANCEL(self);
328 1.11 kleink
329 1.11 kleink return retval;
330 1.11 kleink }
331 1.11 kleink
332 1.2 thorpej ssize_t
333 1.2 thorpej pwrite(int d, const void *buf, size_t nbytes, off_t offset)
334 1.2 thorpej {
335 1.2 thorpej ssize_t retval;
336 1.2 thorpej pthread_t self;
337 1.2 thorpej
338 1.2 thorpej self = pthread__self();
339 1.7 nathanw TESTCANCEL(self);
340 1.2 thorpej retval = _sys_pwrite(d, buf, nbytes, offset);
341 1.7 nathanw TESTCANCEL(self);
342 1.2 thorpej
343 1.2 thorpej return retval;
344 1.2 thorpej }
345 1.2 thorpej
346 1.2 thorpej ssize_t
347 1.2 thorpej read(int d, void *buf, size_t nbytes)
348 1.2 thorpej {
349 1.2 thorpej ssize_t retval;
350 1.2 thorpej pthread_t self;
351 1.2 thorpej
352 1.2 thorpej self = pthread__self();
353 1.7 nathanw TESTCANCEL(self);
354 1.2 thorpej retval = _sys_read(d, buf, nbytes);
355 1.7 nathanw TESTCANCEL(self);
356 1.2 thorpej
357 1.2 thorpej return retval;
358 1.2 thorpej }
359 1.2 thorpej
360 1.2 thorpej ssize_t
361 1.2 thorpej readv(int d, const struct iovec *iov, int iovcnt)
362 1.2 thorpej {
363 1.2 thorpej ssize_t retval;
364 1.2 thorpej pthread_t self;
365 1.2 thorpej
366 1.2 thorpej self = pthread__self();
367 1.7 nathanw TESTCANCEL(self);
368 1.2 thorpej retval = _sys_readv(d, iov, iovcnt);
369 1.7 nathanw TESTCANCEL(self);
370 1.2 thorpej
371 1.2 thorpej return retval;
372 1.2 thorpej }
373 1.2 thorpej
374 1.2 thorpej int
375 1.2 thorpej select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
376 1.2 thorpej struct timeval *timeout)
377 1.2 thorpej {
378 1.2 thorpej int retval;
379 1.2 thorpej pthread_t self;
380 1.2 thorpej
381 1.2 thorpej self = pthread__self();
382 1.7 nathanw TESTCANCEL(self);
383 1.2 thorpej retval = _sys_select(nfds, readfds, writefds, exceptfds, timeout);
384 1.7 nathanw TESTCANCEL(self);
385 1.2 thorpej
386 1.2 thorpej return retval;
387 1.2 thorpej }
388 1.2 thorpej
389 1.2 thorpej pid_t
390 1.2 thorpej wait4(pid_t wpid, int *status, int options, struct rusage *rusage)
391 1.2 thorpej {
392 1.2 thorpej pid_t retval;
393 1.2 thorpej pthread_t self;
394 1.2 thorpej
395 1.2 thorpej self = pthread__self();
396 1.7 nathanw TESTCANCEL(self);
397 1.2 thorpej retval = _sys_wait4(wpid, status, options, rusage);
398 1.7 nathanw TESTCANCEL(self);
399 1.2 thorpej
400 1.2 thorpej return retval;
401 1.2 thorpej }
402 1.2 thorpej
403 1.2 thorpej ssize_t
404 1.2 thorpej write(int d, const void *buf, size_t nbytes)
405 1.2 thorpej {
406 1.2 thorpej ssize_t retval;
407 1.2 thorpej pthread_t self;
408 1.2 thorpej
409 1.2 thorpej self = pthread__self();
410 1.7 nathanw TESTCANCEL(self);
411 1.2 thorpej retval = _sys_write(d, buf, nbytes);
412 1.7 nathanw TESTCANCEL(self);
413 1.2 thorpej
414 1.2 thorpej return retval;
415 1.2 thorpej }
416 1.2 thorpej
417 1.2 thorpej ssize_t
418 1.2 thorpej writev(int d, const struct iovec *iov, int iovcnt)
419 1.2 thorpej {
420 1.2 thorpej ssize_t retval;
421 1.2 thorpej pthread_t self;
422 1.2 thorpej
423 1.2 thorpej self = pthread__self();
424 1.7 nathanw TESTCANCEL(self);
425 1.2 thorpej retval = _sys_writev(d, iov, iovcnt);
426 1.7 nathanw TESTCANCEL(self);
427 1.2 thorpej
428 1.2 thorpej return retval;
429 1.2 thorpej }
430 1.2 thorpej
431 1.14 ad int
432 1.14 ad __sigsuspend14(const sigset_t *sigmask)
433 1.14 ad {
434 1.14 ad pthread_t self;
435 1.14 ad int retval;
436 1.14 ad
437 1.14 ad self = pthread__self();
438 1.14 ad TESTCANCEL(self);
439 1.14 ad retval = _sys___sigsuspend14(sigmask);
440 1.14 ad TESTCANCEL(self);
441 1.14 ad
442 1.14 ad return retval;
443 1.14 ad }
444 1.14 ad
445 1.14 ad int
446 1.14 ad sigtimedwait(const sigset_t * __restrict set, siginfo_t * __restrict info,
447 1.14 ad const struct timespec * __restrict timeout)
448 1.14 ad {
449 1.14 ad pthread_t self;
450 1.14 ad int retval;
451 1.14 ad
452 1.14 ad self = pthread__self();
453 1.14 ad TESTCANCEL(self);
454 1.14 ad retval = _sigtimedwait(set, info, timeout);
455 1.14 ad TESTCANCEL(self);
456 1.14 ad
457 1.14 ad return retval;
458 1.14 ad }
459 1.2 thorpej
460 1.2 thorpej __strong_alias(_close, close)
461 1.2 thorpej __strong_alias(_fcntl, fcntl)
462 1.9 kleink __strong_alias(_fdatasync, fdatasync)
463 1.2 thorpej __strong_alias(_fsync, fsync)
464 1.6 thorpej __weak_alias(fsync_range, _fsync_range)
465 1.2 thorpej __strong_alias(_msgrcv, msgrcv)
466 1.2 thorpej __strong_alias(_msgsnd, msgsnd)
467 1.2 thorpej __strong_alias(___msync13, __msync13)
468 1.2 thorpej __strong_alias(_open, open)
469 1.2 thorpej __strong_alias(_poll, poll)
470 1.11 kleink __weak_alias(pollts, _pollts)
471 1.2 thorpej __strong_alias(_pread, pread)
472 1.11 kleink __strong_alias(_pselect, pselect)
473 1.2 thorpej __strong_alias(_pwrite, pwrite)
474 1.2 thorpej __strong_alias(_read, read)
475 1.2 thorpej __strong_alias(_readv, readv)
476 1.2 thorpej __strong_alias(_select, select)
477 1.2 thorpej __strong_alias(_wait4, wait4)
478 1.2 thorpej __strong_alias(_write, write)
479 1.2 thorpej __strong_alias(_writev, writev)
480