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