Home | History | Annotate | Line # | Download | only in libpthread
pthread_cancelstub.c revision 1.4
      1 /*	$NetBSD: pthread_cancelstub.c,v 1.4 2003/02/15 22:15:50 nathanw 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 #include <sys/socket.h>
     60 
     61 #include "pthread.h"
     62 #include "pthread_int.h"
     63 
     64 int	_sys_accept(int, struct sockaddr *, socklen_t *);
     65 int	_sys_close(int);
     66 int	_sys_connect(int, const struct sockaddr *, socklen_t);
     67 int	_sys_fcntl(int, int, ...);
     68 int	_sys_fsync(int);
     69 ssize_t	_sys_msgrcv(int, void *, size_t, long, int);
     70 int	_sys_msgsnd(int, const void *, size_t, int);
     71 int	_sys___msync13(void *, size_t, int);
     72 int	_sys_open(const char *, int, ...);
     73 int	_sys_poll(struct pollfd *, nfds_t, int);
     74 ssize_t	_sys_pread(int, void *, size_t, off_t);
     75 ssize_t	_sys_pwrite(int, const void *, size_t, off_t);
     76 ssize_t	_sys_read(int, void *, size_t);
     77 ssize_t	_sys_readv(int, const struct iovec *, int);
     78 int	_sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
     79 int	_sys_wait4(pid_t, int *, int, struct rusage *);
     80 ssize_t	_sys_write(int, const void *, size_t);
     81 ssize_t	_sys_writev(int, const struct iovec *, int);
     82 
     83 
     84 int
     85 accept(int s, struct sockaddr *addr, socklen_t *addrlen)
     86 {
     87 	int retval;
     88 	pthread_t self;
     89 
     90 	self = pthread__self();
     91 	pthread__testcancel(self);
     92 	retval = _sys_accept(s, addr, addrlen);
     93 	pthread__testcancel(self);
     94 
     95 	return retval;
     96 }
     97 
     98 int
     99 close(int d)
    100 {
    101 	int retval;
    102 	pthread_t self;
    103 
    104 	self = pthread__self();
    105 	pthread__testcancel(self);
    106 	retval = _sys_close(d);
    107 	pthread__testcancel(self);
    108 
    109 	return retval;
    110 }
    111 
    112 int
    113 connect(int s, const struct sockaddr *addr, socklen_t namelen)
    114 {
    115 	int retval;
    116 	pthread_t self;
    117 
    118 	self = pthread__self();
    119 	pthread__testcancel(self);
    120 	retval = _sys_connect(s, addr, namelen);
    121 	pthread__testcancel(self);
    122 
    123 	return retval;
    124 }
    125 
    126 int
    127 fcntl(int fd, int cmd, ...)
    128 {
    129 	int retval;
    130 	pthread_t self;
    131 	va_list ap;
    132 
    133 	self = pthread__self();
    134 	pthread__testcancel(self);
    135 	va_start(ap, cmd);
    136 	retval = _sys_fcntl(fd, cmd, va_arg(ap, void *));
    137 	va_end(ap);
    138 	pthread__testcancel(self);
    139 
    140 	return retval;
    141 }
    142 
    143 int
    144 fsync(int d)
    145 {
    146 	int retval;
    147 	pthread_t self;
    148 
    149 	self = pthread__self();
    150 	pthread__testcancel(self);
    151 	retval = _sys_fsync(d);
    152 	pthread__testcancel(self);
    153 
    154 	return retval;
    155 }
    156 
    157 ssize_t
    158 msgrcv(int msgid, void *msgp, size_t msgsz, long msgtyp, int msgflg)
    159 {
    160 	ssize_t retval;
    161 	pthread_t self;
    162 
    163 	self = pthread__self();
    164 	pthread__testcancel(self);
    165 	retval = _sys_msgrcv(msgid, msgp, msgsz, msgtyp, msgflg);
    166 	pthread__testcancel(self);
    167 
    168 	return retval;
    169 }
    170 
    171 int
    172 msgsnd(int msgid, const void *msgp, size_t msgsz, int msgflg)
    173 {
    174 	int retval;
    175 	pthread_t self;
    176 
    177 	self = pthread__self();
    178 	pthread__testcancel(self);
    179 	retval = _sys_msgsnd(msgid, msgp, msgsz, msgflg);
    180 	pthread__testcancel(self);
    181 
    182 	return retval;
    183 }
    184 
    185 int
    186 __msync13(void *addr, size_t len, int flags)
    187 {
    188 	int retval;
    189 	pthread_t self;
    190 
    191 	self = pthread__self();
    192 	pthread__testcancel(self);
    193 	retval = _sys___msync13(addr, len, flags);
    194 	pthread__testcancel(self);
    195 
    196 	return retval;
    197 }
    198 
    199 int
    200 open(const char *path, int flags, ...)
    201 {
    202 	int retval;
    203 	pthread_t self;
    204 	va_list ap;
    205 
    206 	self = pthread__self();
    207 	pthread__testcancel(self);
    208 	va_start(ap, flags);
    209 	retval = _sys_open(path, flags, va_arg(ap, mode_t));
    210 	va_end(ap);
    211 	pthread__testcancel(self);
    212 
    213 	return retval;
    214 }
    215 
    216 int
    217 poll(struct pollfd *fds, nfds_t nfds, int timeout)
    218 {
    219 	int retval;
    220 	pthread_t self;
    221 
    222 	self = pthread__self();
    223 	pthread__testcancel(self);
    224 	retval = _sys_poll(fds, nfds, timeout);
    225 	pthread__testcancel(self);
    226 
    227 	return retval;
    228 }
    229 
    230 ssize_t
    231 pread(int d, void *buf, size_t nbytes, off_t offset)
    232 {
    233 	ssize_t retval;
    234 	pthread_t self;
    235 
    236 	self = pthread__self();
    237 	pthread__testcancel(self);
    238 	retval = _sys_pread(d, buf, nbytes, offset);
    239 	pthread__testcancel(self);
    240 
    241 	return retval;
    242 }
    243 
    244 ssize_t
    245 pwrite(int d, const void *buf, size_t nbytes, off_t offset)
    246 {
    247 	ssize_t retval;
    248 	pthread_t self;
    249 
    250 	self = pthread__self();
    251 	pthread__testcancel(self);
    252 	retval = _sys_pwrite(d, buf, nbytes, offset);
    253 	pthread__testcancel(self);
    254 
    255 	return retval;
    256 }
    257 
    258 ssize_t
    259 read(int d, void *buf, size_t nbytes)
    260 {
    261 	ssize_t retval;
    262 	pthread_t self;
    263 
    264 	self = pthread__self();
    265 	pthread__testcancel(self);
    266 	retval = _sys_read(d, buf, nbytes);
    267 	pthread__testcancel(self);
    268 
    269 	return retval;
    270 }
    271 
    272 ssize_t
    273 readv(int d, const struct iovec *iov, int iovcnt)
    274 {
    275 	ssize_t retval;
    276 	pthread_t self;
    277 
    278 	self = pthread__self();
    279 	pthread__testcancel(self);
    280 	retval = _sys_readv(d, iov, iovcnt);
    281 	pthread__testcancel(self);
    282 
    283 	return retval;
    284 }
    285 
    286 int
    287 select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
    288     struct timeval *timeout)
    289 {
    290 	int retval;
    291 	pthread_t self;
    292 
    293 	self = pthread__self();
    294 	pthread__testcancel(self);
    295 	retval = _sys_select(nfds, readfds, writefds, exceptfds, timeout);
    296 	pthread__testcancel(self);
    297 
    298 	return retval;
    299 }
    300 
    301 pid_t
    302 wait4(pid_t wpid, int *status, int options, struct rusage *rusage)
    303 {
    304 	pid_t retval;
    305 	pthread_t self;
    306 
    307 	self = pthread__self();
    308 	pthread__testcancel(self);
    309 	retval = _sys_wait4(wpid, status, options, rusage);
    310 	pthread__testcancel(self);
    311 
    312 	return retval;
    313 }
    314 
    315 ssize_t
    316 write(int d, const void *buf, size_t nbytes)
    317 {
    318 	ssize_t retval;
    319 	pthread_t self;
    320 
    321 	self = pthread__self();
    322 	pthread__testcancel(self);
    323 	retval = _sys_write(d, buf, nbytes);
    324 	pthread__testcancel(self);
    325 
    326 	return retval;
    327 }
    328 
    329 ssize_t
    330 writev(int d, const struct iovec *iov, int iovcnt)
    331 {
    332 	ssize_t retval;
    333 	pthread_t self;
    334 
    335 	self = pthread__self();
    336 	pthread__testcancel(self);
    337 	retval = _sys_writev(d, iov, iovcnt);
    338 	pthread__testcancel(self);
    339 
    340 	return retval;
    341 }
    342 
    343 
    344 __strong_alias(_close, close)
    345 __strong_alias(_fcntl, fcntl)
    346 __strong_alias(_fsync, fsync)
    347 __strong_alias(_msgrcv, msgrcv)
    348 __strong_alias(_msgsnd, msgsnd)
    349 __strong_alias(___msync13, __msync13)
    350 __strong_alias(_open, open)
    351 __strong_alias(_poll, poll)
    352 __strong_alias(_pread, pread)
    353 __strong_alias(_pwrite, pwrite)
    354 __strong_alias(_read, read)
    355 __strong_alias(_readv, readv)
    356 __strong_alias(_select, select)
    357 __strong_alias(_wait4, wait4)
    358 __strong_alias(_write, write)
    359 __strong_alias(_writev, writev)
    360