Home | History | Annotate | Line # | Download | only in libpthread
pthread_cancelstub.c revision 1.11
      1 /*	$NetBSD: pthread_cancelstub.c,v 1.11 2005/03/18 11:23:44 kleink 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/cdefs.h>
     40 __RCSID("$NetBSD: pthread_cancelstub.c,v 1.11 2005/03/18 11:23:44 kleink Exp $");
     41 
     42 /*
     43  * This is necessary because the names are always weak (they are not
     44  * POSIX functions).
     45  */
     46 #define	fsync_range	_fsync_range
     47 #define	pollts		_pollts
     48 
     49 /*
     50  * XXX this is necessary to get the prototypes for the __sigsuspend14
     51  * XXX and __msync13 internal names, instead of the application-visible
     52  * XXX sigsuspend and msync names. It's kind of gross, but we're pretty
     53  * XXX intimate with libc already.
     54  */
     55 #define __LIBC12_SOURCE__
     56 
     57 #include <sys/msg.h>
     58 #include <sys/types.h>
     59 #include <sys/uio.h>
     60 #include <sys/wait.h>
     61 #include <fcntl.h>
     62 #include <poll.h>
     63 #include <stdarg.h>
     64 #include <unistd.h>
     65 
     66 #include <signal.h>
     67 #include <sys/mman.h>
     68 #include <sys/socket.h>
     69 
     70 #include "pthread.h"
     71 #include "pthread_int.h"
     72 
     73 int	pthread__cancel_stub_binder;
     74 
     75 int	_sys_accept(int, struct sockaddr *, socklen_t *);
     76 int	_sys_close(int);
     77 int	_sys_connect(int, const struct sockaddr *, socklen_t);
     78 int	_sys_fcntl(int, int, ...);
     79 int	_sys_fdatasync(int);
     80 int	_sys_fsync(int);
     81 int	_sys_fsync_range(int, int, off_t, off_t);
     82 ssize_t	_sys_msgrcv(int, void *, size_t, long, int);
     83 int	_sys_msgsnd(int, const void *, size_t, int);
     84 int	_sys___msync13(void *, size_t, int);
     85 int	_sys_open(const char *, int, ...);
     86 int	_sys_poll(struct pollfd *, nfds_t, int);
     87 int	_sys_pollts(struct pollfd *, nfds_t, const struct timespec *,
     88 	    const sigset_t *);
     89 ssize_t	_sys_pread(int, void *, size_t, off_t);
     90 int	_sys_pselect(int, fd_set *, fd_set *, fd_set *,
     91 	    const struct timespec *, const sigset_t *);
     92 ssize_t	_sys_pwrite(int, const void *, size_t, off_t);
     93 ssize_t	_sys_read(int, void *, size_t);
     94 ssize_t	_sys_readv(int, const struct iovec *, int);
     95 int	_sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
     96 int	_sys_wait4(pid_t, int *, int, struct rusage *);
     97 ssize_t	_sys_write(int, const void *, size_t);
     98 ssize_t	_sys_writev(int, const struct iovec *, int);
     99 
    100 #define TESTCANCEL(id) 	do {						\
    101 	if (__predict_false((id)->pt_cancel))				\
    102 		pthread_exit(PTHREAD_CANCELED);				\
    103 	} while (/*CONSTCOND*/0)
    104 
    105 
    106 int
    107 accept(int s, struct sockaddr *addr, socklen_t *addrlen)
    108 {
    109 	int retval;
    110 	pthread_t self;
    111 
    112 	self = pthread__self();
    113 	TESTCANCEL(self);
    114 	retval = _sys_accept(s, addr, addrlen);
    115 	TESTCANCEL(self);
    116 
    117 	return retval;
    118 }
    119 
    120 int
    121 close(int d)
    122 {
    123 	int retval;
    124 	pthread_t self;
    125 
    126 	self = pthread__self();
    127 	TESTCANCEL(self);
    128 	retval = _sys_close(d);
    129 	TESTCANCEL(self);
    130 
    131 	return retval;
    132 }
    133 
    134 int
    135 connect(int s, const struct sockaddr *addr, socklen_t namelen)
    136 {
    137 	int retval;
    138 	pthread_t self;
    139 
    140 	self = pthread__self();
    141 	TESTCANCEL(self);
    142 	retval = _sys_connect(s, addr, namelen);
    143 	TESTCANCEL(self);
    144 
    145 	return retval;
    146 }
    147 
    148 int
    149 fcntl(int fd, int cmd, ...)
    150 {
    151 	int retval;
    152 	pthread_t self;
    153 	va_list ap;
    154 
    155 	self = pthread__self();
    156 	TESTCANCEL(self);
    157 	va_start(ap, cmd);
    158 	retval = _sys_fcntl(fd, cmd, va_arg(ap, void *));
    159 	va_end(ap);
    160 	TESTCANCEL(self);
    161 
    162 	return retval;
    163 }
    164 
    165 int
    166 fdatasync(int d)
    167 {
    168 	int retval;
    169 	pthread_t self;
    170 
    171 	self = pthread__self();
    172 	TESTCANCEL(self);
    173 	retval = _sys_fdatasync(d);
    174 	TESTCANCEL(self);
    175 
    176 	return retval;
    177 }
    178 
    179 int
    180 fsync(int d)
    181 {
    182 	int retval;
    183 	pthread_t self;
    184 
    185 	self = pthread__self();
    186 	TESTCANCEL(self);
    187 	retval = _sys_fsync(d);
    188 	TESTCANCEL(self);
    189 
    190 	return retval;
    191 }
    192 
    193 int
    194 fsync_range(int d, int f, off_t s, off_t e)
    195 {
    196 	int retval;
    197 	pthread_t self;
    198 
    199 	self = pthread__self();
    200 	TESTCANCEL(self);
    201 	retval = _sys_fsync_range(d, f, s, e);
    202 	TESTCANCEL(self);
    203 
    204 	return retval;
    205 }
    206 
    207 ssize_t
    208 msgrcv(int msgid, void *msgp, size_t msgsz, long msgtyp, int msgflg)
    209 {
    210 	ssize_t retval;
    211 	pthread_t self;
    212 
    213 	self = pthread__self();
    214 	TESTCANCEL(self);
    215 	retval = _sys_msgrcv(msgid, msgp, msgsz, msgtyp, msgflg);
    216 	TESTCANCEL(self);
    217 
    218 	return retval;
    219 }
    220 
    221 int
    222 msgsnd(int msgid, const void *msgp, size_t msgsz, int msgflg)
    223 {
    224 	int retval;
    225 	pthread_t self;
    226 
    227 	self = pthread__self();
    228 	TESTCANCEL(self);
    229 	retval = _sys_msgsnd(msgid, msgp, msgsz, msgflg);
    230 	TESTCANCEL(self);
    231 
    232 	return retval;
    233 }
    234 
    235 int
    236 __msync13(void *addr, size_t len, int flags)
    237 {
    238 	int retval;
    239 	pthread_t self;
    240 
    241 	self = pthread__self();
    242 	TESTCANCEL(self);
    243 	retval = _sys___msync13(addr, len, flags);
    244 	TESTCANCEL(self);
    245 
    246 	return retval;
    247 }
    248 
    249 int
    250 open(const char *path, int flags, ...)
    251 {
    252 	int retval;
    253 	pthread_t self;
    254 	va_list ap;
    255 
    256 	self = pthread__self();
    257 	TESTCANCEL(self);
    258 	va_start(ap, flags);
    259 	retval = _sys_open(path, flags, va_arg(ap, mode_t));
    260 	va_end(ap);
    261 	TESTCANCEL(self);
    262 
    263 	return retval;
    264 }
    265 
    266 int
    267 poll(struct pollfd *fds, nfds_t nfds, int timeout)
    268 {
    269 	int retval;
    270 	pthread_t self;
    271 
    272 	self = pthread__self();
    273 	TESTCANCEL(self);
    274 	retval = _sys_poll(fds, nfds, timeout);
    275 	TESTCANCEL(self);
    276 
    277 	return retval;
    278 }
    279 
    280 int
    281 pollts(struct pollfd *fds, nfds_t nfds, const struct timespec *ts,
    282     const sigset_t *sigmask)
    283 {
    284 	int retval;
    285 	pthread_t self;
    286 
    287 	self = pthread__self();
    288 	TESTCANCEL(self);
    289 	retval = _sys_pollts(fds, nfds, ts, sigmask);
    290 	TESTCANCEL(self);
    291 
    292 	return retval;
    293 }
    294 
    295 ssize_t
    296 pread(int d, void *buf, size_t nbytes, off_t offset)
    297 {
    298 	ssize_t retval;
    299 	pthread_t self;
    300 
    301 	self = pthread__self();
    302 	TESTCANCEL(self);
    303 	retval = _sys_pread(d, buf, nbytes, offset);
    304 	TESTCANCEL(self);
    305 
    306 	return retval;
    307 }
    308 
    309 int
    310 pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
    311     const struct timespec *timeout, const sigset_t *sigmask)
    312 {
    313 	int retval;
    314 	pthread_t self;
    315 
    316 	self = pthread__self();
    317 	TESTCANCEL(self);
    318 	retval = _sys_pselect(nfds, readfds, writefds, exceptfds, timeout,
    319 	    sigmask);
    320 	TESTCANCEL(self);
    321 
    322 	return retval;
    323 }
    324 
    325 ssize_t
    326 pwrite(int d, const void *buf, size_t nbytes, off_t offset)
    327 {
    328 	ssize_t retval;
    329 	pthread_t self;
    330 
    331 	self = pthread__self();
    332 	TESTCANCEL(self);
    333 	retval = _sys_pwrite(d, buf, nbytes, offset);
    334 	TESTCANCEL(self);
    335 
    336 	return retval;
    337 }
    338 
    339 ssize_t
    340 read(int d, void *buf, size_t nbytes)
    341 {
    342 	ssize_t retval;
    343 	pthread_t self;
    344 
    345 	self = pthread__self();
    346 	TESTCANCEL(self);
    347 	retval = _sys_read(d, buf, nbytes);
    348 	TESTCANCEL(self);
    349 
    350 	return retval;
    351 }
    352 
    353 ssize_t
    354 readv(int d, const struct iovec *iov, int iovcnt)
    355 {
    356 	ssize_t retval;
    357 	pthread_t self;
    358 
    359 	self = pthread__self();
    360 	TESTCANCEL(self);
    361 	retval = _sys_readv(d, iov, iovcnt);
    362 	TESTCANCEL(self);
    363 
    364 	return retval;
    365 }
    366 
    367 int
    368 select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
    369     struct timeval *timeout)
    370 {
    371 	int retval;
    372 	pthread_t self;
    373 
    374 	self = pthread__self();
    375 	TESTCANCEL(self);
    376 	retval = _sys_select(nfds, readfds, writefds, exceptfds, timeout);
    377 	TESTCANCEL(self);
    378 
    379 	return retval;
    380 }
    381 
    382 pid_t
    383 wait4(pid_t wpid, int *status, int options, struct rusage *rusage)
    384 {
    385 	pid_t retval;
    386 	pthread_t self;
    387 
    388 	self = pthread__self();
    389 	TESTCANCEL(self);
    390 	retval = _sys_wait4(wpid, status, options, rusage);
    391 	TESTCANCEL(self);
    392 
    393 	return retval;
    394 }
    395 
    396 ssize_t
    397 write(int d, const void *buf, size_t nbytes)
    398 {
    399 	ssize_t retval;
    400 	pthread_t self;
    401 
    402 	self = pthread__self();
    403 	TESTCANCEL(self);
    404 	retval = _sys_write(d, buf, nbytes);
    405 	TESTCANCEL(self);
    406 
    407 	return retval;
    408 }
    409 
    410 ssize_t
    411 writev(int d, const struct iovec *iov, int iovcnt)
    412 {
    413 	ssize_t retval;
    414 	pthread_t self;
    415 
    416 	self = pthread__self();
    417 	TESTCANCEL(self);
    418 	retval = _sys_writev(d, iov, iovcnt);
    419 	TESTCANCEL(self);
    420 
    421 	return retval;
    422 }
    423 
    424 
    425 __strong_alias(_close, close)
    426 __strong_alias(_fcntl, fcntl)
    427 __strong_alias(_fdatasync, fdatasync)
    428 __strong_alias(_fsync, fsync)
    429 __weak_alias(fsync_range, _fsync_range)
    430 __strong_alias(_msgrcv, msgrcv)
    431 __strong_alias(_msgsnd, msgsnd)
    432 __strong_alias(___msync13, __msync13)
    433 __strong_alias(_open, open)
    434 __strong_alias(_poll, poll)
    435 __weak_alias(pollts, _pollts)
    436 __strong_alias(_pread, pread)
    437 __strong_alias(_pselect, pselect)
    438 __strong_alias(_pwrite, pwrite)
    439 __strong_alias(_read, read)
    440 __strong_alias(_readv, readv)
    441 __strong_alias(_select, select)
    442 __strong_alias(_wait4, wait4)
    443 __strong_alias(_write, write)
    444 __strong_alias(_writev, writev)
    445