xref: /freebsd/lib/libthr/thread/thr_syscalls.c (revision 77b7cdf1999ee965ad494fddd184b18f532ac91a)
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 __weak_reference(_aio_suspend, aio_suspend);
94 
95 int
96 _aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct
97     timespec *timeout)
98 {
99 	int	ret;
100 
101 	_thread_enter_cancellation_point();
102 	ret = __sys_aio_suspend(iocbs, niocb, timeout);
103 	_thread_leave_cancellation_point();
104 
105 	return ret;
106 }
107 
108 __weak_reference(__close, close);
109 
110 int
111 __close(int fd)
112 {
113 	int	ret;
114 
115 	_thread_enter_cancellation_point();
116 	ret = __sys_close(fd);
117 	_thread_leave_cancellation_point();
118 
119 	return ret;
120 }
121 __weak_reference(___creat, creat);
122 
123 int
124 ___creat(const char *path, mode_t mode)
125 {
126 	int	ret;
127 
128 	_thread_enter_cancellation_point();
129 	ret = __creat(path, mode);
130 	_thread_leave_cancellation_point();
131 
132 	return ret;
133 }
134 
135 __weak_reference(__fcntl, fcntl);
136 
137 int
138 __fcntl(int fd, int cmd,...)
139 {
140 	int	ret;
141 	va_list	ap;
142 
143 	_thread_enter_cancellation_point();
144 
145 	va_start(ap, cmd);
146 	switch (cmd) {
147 		case F_DUPFD:
148 		case F_SETFD:
149 		case F_SETFL:
150 			ret = __sys_fcntl(fd, cmd, va_arg(ap, int));
151 			break;
152 		case F_GETFD:
153 		case F_GETFL:
154 			ret = __sys_fcntl(fd, cmd);
155 			break;
156 		default:
157 			ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
158 	}
159 	va_end(ap);
160 
161 	_thread_leave_cancellation_point();
162 
163 	return ret;
164 }
165 
166 __weak_reference(__fsync, fsync);
167 
168 int
169 __fsync(int fd)
170 {
171 	int	ret;
172 
173 	_thread_enter_cancellation_point();
174 	ret = __sys_fsync(fd);
175 	_thread_leave_cancellation_point();
176 
177 	return ret;
178 }
179 
180 __weak_reference(__msync, msync);
181 
182 int
183 __msync(void *addr, size_t len, int flags)
184 {
185 	int	ret;
186 
187 	_thread_enter_cancellation_point();
188 	ret = __sys_msync(addr, len, flags);
189 	_thread_leave_cancellation_point();
190 
191 	return ret;
192 }
193 
194 __weak_reference(_nanosleep, nanosleep);
195 
196 int
197 _nanosleep(const struct timespec * time_to_sleep, struct timespec *
198     time_remaining)
199 {
200 	int	ret;
201 
202 	_thread_enter_cancellation_point();
203 	ret = __sys_nanosleep(time_to_sleep, time_remaining);
204 	_thread_leave_cancellation_point();
205 
206 	return ret;
207 }
208 
209 __weak_reference(__open, open);
210 
211 int
212 __open(const char *path, int flags,...)
213 {
214 	int	ret;
215 	int	mode = 0;
216 	va_list	ap;
217 
218 	_thread_enter_cancellation_point();
219 
220 	/* Check if the file is being created: */
221 	if (flags & O_CREAT) {
222 		/* Get the creation mode: */
223 		va_start(ap, flags);
224 		mode = va_arg(ap, int);
225 		va_end(ap);
226 	}
227 
228 	ret = __sys_open(path, flags, mode);
229 	_thread_leave_cancellation_point();
230 
231 	return ret;
232 }
233 
234 __weak_reference(__poll, poll);
235 
236 int
237 __poll(struct pollfd *fds, unsigned int nfds, int timeout)
238 {
239 	int ret;
240 
241 	_thread_enter_cancellation_point();
242 	ret = __sys_poll(fds, nfds, timeout);
243 	_thread_leave_cancellation_point();
244 
245 	return ret;
246 }
247 
248 extern int __pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
249 		const struct timespec *timo, const sigset_t *mask);
250 
251 int
252 pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
253 	const struct timespec *timo, const sigset_t *mask)
254 {
255 	int ret;
256 
257 	_thread_enter_cancellation_point();
258 	ret = __pselect(count, rfds, wfds, efds, timo, mask);
259 	_thread_leave_cancellation_point();
260 
261 	return (ret);
262 }
263 
264 __weak_reference(__read, read);
265 
266 ssize_t
267 __read(int fd, void *buf, size_t nbytes)
268 {
269 	ssize_t	ret;
270 
271 	_thread_enter_cancellation_point();
272 	ret = __sys_read(fd, buf, nbytes);
273 	_thread_leave_cancellation_point();
274 
275 	return ret;
276 }
277 
278 __weak_reference(__readv, readv);
279 
280 ssize_t
281 __readv(int fd, const struct iovec *iov, int iovcnt)
282 {
283 	ssize_t ret;
284 
285 	_thread_enter_cancellation_point();
286 	ret = __sys_readv(fd, iov, iovcnt);
287 	_thread_leave_cancellation_point();
288 
289 	return ret;
290 }
291 
292 __weak_reference(__select, select);
293 
294 int
295 __select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
296 	struct timeval *timeout)
297 {
298 	int ret;
299 
300 	_thread_enter_cancellation_point();
301 	ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
302 	_thread_leave_cancellation_point();
303 
304 	return ret;
305 }
306 
307 __weak_reference(_sleep, sleep);
308 
309 unsigned int
310 _sleep(unsigned int seconds)
311 {
312 	unsigned int	ret;
313 
314 	_thread_enter_cancellation_point();
315 	ret = __sleep(seconds);
316 	_thread_leave_cancellation_point();
317 
318 	return ret;
319 }
320 
321 __weak_reference(_system, system);
322 
323 int
324 _system(const char *string)
325 {
326 	int	ret;
327 
328 	_thread_enter_cancellation_point();
329 	ret = __system(string);
330 	_thread_leave_cancellation_point();
331 
332 	return ret;
333 }
334 
335 
336 __weak_reference(_tcdrain, tcdrain);
337 
338 int
339 _tcdrain(int fd)
340 {
341 	int	ret;
342 
343 	_thread_enter_cancellation_point();
344 	ret = __tcdrain(fd);
345 	_thread_leave_cancellation_point();
346 
347 	return ret;
348 }
349 
350 __weak_reference(_wait, wait);
351 
352 pid_t
353 _wait(int *istat)
354 {
355 	pid_t	ret;
356 
357 	_thread_enter_cancellation_point();
358 	ret = __wait(istat);
359 	_thread_leave_cancellation_point();
360 
361 	return ret;
362 }
363 
364 __weak_reference(__wait4, wait4);
365 
366 pid_t
367 __wait4(pid_t pid, int *istat, int options, struct rusage *rusage)
368 {
369 	pid_t ret;
370 
371 	_thread_enter_cancellation_point();
372 	ret = _wait4(pid, istat, options, rusage);
373 	_thread_leave_cancellation_point();
374 
375 	return ret;
376 }
377 
378 __weak_reference(_waitpid, waitpid);
379 
380 pid_t
381 _waitpid(pid_t wpid, int *status, int options)
382 {
383 	pid_t	ret;
384 
385 	_thread_enter_cancellation_point();
386 	ret = __waitpid(wpid, status, options);
387 	_thread_leave_cancellation_point();
388 
389 	return ret;
390 }
391 
392 __weak_reference(__write, write);
393 
394 ssize_t
395 __write(int fd, const void *buf, size_t nbytes)
396 {
397 	ssize_t	ret;
398 
399 	_thread_enter_cancellation_point();
400 	ret = __sys_write(fd, buf, nbytes);
401 	_thread_leave_cancellation_point();
402 
403 	return ret;
404 }
405 
406 __weak_reference(__writev, writev);
407 
408 ssize_t
409 __writev(int fd, const struct iovec *iov, int iovcnt)
410 {
411 	ssize_t ret;
412 
413 	_thread_enter_cancellation_point();
414 	ret = __sys_writev(fd, iov, iovcnt);
415 	_thread_leave_cancellation_point();
416 
417 	return ret;
418 }
419