xref: /freebsd/lib/libthr/thread/thr_syscalls.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
1 /*
2  * Copyright (c) 2000 Jason Evans <jasone@freebsd.org>.
3  * Copyright (c) 2002 Daniel M. Eischen <deischen@freebsd.org>
4  * Copyright (c) 2003 Jeff Roberson <jeff@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice(s), this list of conditions and the following disclaimer as
12  *    the first lines of this file unmodified other than the possible
13  *    addition of one or more copyright notices.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice(s), this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
26  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
28  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * $FreeBSD$
32  */
33 
34 /*
35  * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *	This product includes software developed by John Birrell.
49  * 4. Neither the name of the author nor the names of any co-contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  */
66 
67 #include <sys/cdefs.h>
68 #include <sys/fcntl.h>
69 #include <sys/mman.h>
70 #include <sys/param.h>
71 #include <sys/select.h>
72 #include <sys/time.h>
73 #include <sys/types.h>
74 #include <sys/uio.h>
75 #include <sys/wait.h>
76 
77 #include <aio.h>
78 #include <dirent.h>
79 #include <errno.h>
80 #include <fcntl.h>
81 #include <poll.h>
82 #include <pthread.h>
83 #include <signal.h>
84 #include <stdarg.h>
85 #include <stdio.h>
86 #include <stdlib.h>
87 #include <string.h>
88 #include <termios.h>
89 #include <unistd.h>
90 
91 #include "thr_private.h"
92 
93 extern int __creat(const char *, mode_t);
94 extern int __sleep(unsigned int);
95 extern int __sys_nanosleep(const struct timespec *, struct timespec *);
96 extern int __sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
97 extern int __system(const char *);
98 extern int __tcdrain(int);
99 extern pid_t __wait(int *);
100 extern pid_t _wait4(pid_t, int *, int, struct rusage *);
101 extern pid_t __waitpid(pid_t, int *, int);
102 
103 __weak_reference(_aio_suspend, aio_suspend);
104 
105 int
106 _aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct
107     timespec *timeout)
108 {
109 	int	ret;
110 
111 	_thread_enter_cancellation_point();
112 	ret = __sys_aio_suspend(iocbs, niocb, timeout);
113 	_thread_leave_cancellation_point();
114 
115 	return ret;
116 }
117 
118 __weak_reference(__close, close);
119 
120 int
121 __close(int fd)
122 {
123 	int	ret;
124 
125 	_thread_enter_cancellation_point();
126 	ret = __sys_close(fd);
127 	_thread_leave_cancellation_point();
128 
129 	return ret;
130 }
131 __weak_reference(___creat, creat);
132 
133 int
134 ___creat(const char *path, mode_t mode)
135 {
136 	int	ret;
137 
138 	_thread_enter_cancellation_point();
139 	ret = __creat(path, mode);
140 	_thread_leave_cancellation_point();
141 
142 	return ret;
143 }
144 
145 __weak_reference(__fcntl, fcntl);
146 
147 int
148 __fcntl(int fd, int cmd,...)
149 {
150 	int	ret;
151 	va_list	ap;
152 
153 	_thread_enter_cancellation_point();
154 
155 	va_start(ap, cmd);
156 	switch (cmd) {
157 		case F_DUPFD:
158 		case F_SETFD:
159 		case F_SETFL:
160 			ret = __sys_fcntl(fd, cmd, va_arg(ap, int));
161 			break;
162 		case F_GETFD:
163 		case F_GETFL:
164 			ret = __sys_fcntl(fd, cmd);
165 			break;
166 		default:
167 			ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
168 	}
169 	va_end(ap);
170 
171 	_thread_leave_cancellation_point();
172 
173 	return ret;
174 }
175 
176 __weak_reference(__fsync, fsync);
177 
178 int
179 __fsync(int fd)
180 {
181 	int	ret;
182 
183 	_thread_enter_cancellation_point();
184 	ret = __sys_fsync(fd);
185 	_thread_leave_cancellation_point();
186 
187 	return ret;
188 }
189 
190 __weak_reference(__msync, msync);
191 
192 int
193 __msync(void *addr, size_t len, int flags)
194 {
195 	int	ret;
196 
197 	_thread_enter_cancellation_point();
198 	ret = __sys_msync(addr, len, flags);
199 	_thread_leave_cancellation_point();
200 
201 	return ret;
202 }
203 
204 __weak_reference(_nanosleep, nanosleep);
205 
206 int
207 _nanosleep(const struct timespec * time_to_sleep, struct timespec *
208     time_remaining)
209 {
210 	int	ret;
211 
212 	_thread_enter_cancellation_point();
213 	ret = __sys_nanosleep(time_to_sleep, time_remaining);
214 	_thread_leave_cancellation_point();
215 
216 	return ret;
217 }
218 
219 __weak_reference(__open, open);
220 
221 int
222 __open(const char *path, int flags,...)
223 {
224 	int	ret;
225 	int	mode = 0;
226 	va_list	ap;
227 
228 	_thread_enter_cancellation_point();
229 
230 	/* Check if the file is being created: */
231 	if (flags & O_CREAT) {
232 		/* Get the creation mode: */
233 		va_start(ap, flags);
234 		mode = va_arg(ap, int);
235 		va_end(ap);
236 	}
237 
238 	ret = __sys_open(path, flags, mode);
239 	_thread_leave_cancellation_point();
240 
241 	return ret;
242 }
243 
244 __weak_reference(__poll, poll);
245 
246 int
247 __poll(struct pollfd *fds, unsigned int nfds, int timeout)
248 {
249 	int ret;
250 
251 	_thread_enter_cancellation_point();
252 	ret = __sys_poll(fds, nfds, timeout);
253 	_thread_leave_cancellation_point();
254 
255 	return ret;
256 }
257 
258 extern int __pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
259 		const struct timespec *timo, const sigset_t *mask);
260 
261 int
262 pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
263 	const struct timespec *timo, const sigset_t *mask)
264 {
265 	int ret;
266 
267 	_thread_enter_cancellation_point();
268 	ret = __pselect(count, rfds, wfds, efds, timo, mask);
269 	_thread_leave_cancellation_point();
270 
271 	return (ret);
272 }
273 
274 __weak_reference(__read, read);
275 
276 ssize_t
277 __read(int fd, void *buf, size_t nbytes)
278 {
279 	ssize_t	ret;
280 
281 	_thread_enter_cancellation_point();
282 	ret = __sys_read(fd, buf, nbytes);
283 	_thread_leave_cancellation_point();
284 
285 	return ret;
286 }
287 
288 __weak_reference(__readv, readv);
289 
290 ssize_t
291 __readv(int fd, const struct iovec *iov, int iovcnt)
292 {
293 	ssize_t ret;
294 
295 	_thread_enter_cancellation_point();
296 	ret = __sys_readv(fd, iov, iovcnt);
297 	_thread_leave_cancellation_point();
298 
299 	return ret;
300 }
301 
302 __weak_reference(__select, select);
303 
304 int
305 __select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
306 	struct timeval *timeout)
307 {
308 	int ret;
309 
310 	_thread_enter_cancellation_point();
311 	ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
312 	_thread_leave_cancellation_point();
313 
314 	return ret;
315 }
316 
317 __weak_reference(_sleep, sleep);
318 
319 unsigned int
320 _sleep(unsigned int seconds)
321 {
322 	unsigned int	ret;
323 
324 	_thread_enter_cancellation_point();
325 	ret = __sleep(seconds);
326 	_thread_leave_cancellation_point();
327 
328 	return ret;
329 }
330 
331 __weak_reference(_system, system);
332 
333 int
334 _system(const char *string)
335 {
336 	int	ret;
337 
338 	_thread_enter_cancellation_point();
339 	ret = __system(string);
340 	_thread_leave_cancellation_point();
341 
342 	return ret;
343 }
344 
345 
346 __weak_reference(_tcdrain, tcdrain);
347 
348 int
349 _tcdrain(int fd)
350 {
351 	int	ret;
352 
353 	_thread_enter_cancellation_point();
354 	ret = __tcdrain(fd);
355 	_thread_leave_cancellation_point();
356 
357 	return ret;
358 }
359 
360 __weak_reference(_wait, wait);
361 
362 pid_t
363 _wait(int *istat)
364 {
365 	pid_t	ret;
366 
367 	_thread_enter_cancellation_point();
368 	ret = __wait(istat);
369 	_thread_leave_cancellation_point();
370 
371 	return ret;
372 }
373 
374 __weak_reference(__wait4, wait4);
375 
376 pid_t
377 __wait4(pid_t pid, int *istat, int options, struct rusage *rusage)
378 {
379 	pid_t ret;
380 
381 	_thread_enter_cancellation_point();
382 	ret = _wait4(pid, istat, options, rusage);
383 	_thread_leave_cancellation_point();
384 
385 	return ret;
386 }
387 
388 __weak_reference(_waitpid, waitpid);
389 
390 pid_t
391 _waitpid(pid_t wpid, int *status, int options)
392 {
393 	pid_t	ret;
394 
395 	_thread_enter_cancellation_point();
396 	ret = __waitpid(wpid, status, options);
397 	_thread_leave_cancellation_point();
398 
399 	return ret;
400 }
401 
402 __weak_reference(__write, write);
403 
404 ssize_t
405 __write(int fd, const void *buf, size_t nbytes)
406 {
407 	ssize_t	ret;
408 
409 	_thread_enter_cancellation_point();
410 	ret = __sys_write(fd, buf, nbytes);
411 	_thread_leave_cancellation_point();
412 
413 	return ret;
414 }
415 
416 __weak_reference(__writev, writev);
417 
418 ssize_t
419 __writev(int fd, const struct iovec *iov, int iovcnt)
420 {
421 	ssize_t ret;
422 
423 	_thread_enter_cancellation_point();
424 	ret = __sys_writev(fd, iov, iovcnt);
425 	_thread_leave_cancellation_point();
426 
427 	return ret;
428 }
429