xref: /freebsd/lib/libthr/thread/thr_syscalls.c (revision 1f467eaaf82a9b863fc6264c7c8e6b40fffdb357)
1 /*
2  * Copyright (C) 2005 David Xu <davidxu@freebsd.org>.
3  * Copyright (c) 2003 Daniel Eischen <deischen@freebsd.org>.
4  * Copyright (C) 2000 Jason Evans <jasone@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/types.h>
68 #include <sys/mman.h>
69 #include <sys/param.h>
70 #include <sys/select.h>
71 #include <sys/signalvar.h>
72 #include <sys/socket.h>
73 #include <sys/stat.h>
74 #include <sys/time.h>
75 #include <sys/uio.h>
76 #include <sys/wait.h>
77 #include <aio.h>
78 #include <dirent.h>
79 #include <errno.h>
80 #include <fcntl.h>
81 #include <poll.h>
82 #include <signal.h>
83 #include <stdarg.h>
84 #include <stdio.h>
85 #include <stdlib.h>
86 #include <string.h>
87 #include <termios.h>
88 #include <unistd.h>
89 #include <pthread.h>
90 
91 #include "thr_private.h"
92 
93 extern int __creat(const char *, mode_t);
94 extern int __pause(void);
95 extern int __pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
96 		const struct timespec *timo, const sigset_t *mask);
97 extern unsigned int __sleep(unsigned int);
98 extern int __system(const char *);
99 extern int __tcdrain(int);
100 extern int __usleep(useconds_t);
101 extern pid_t __wait(int *);
102 extern pid_t __sys_wait4(pid_t, int *, int, struct rusage *);
103 extern pid_t __waitpid(pid_t, int *, int);
104 
105 __weak_reference(__accept, accept);
106 int
107 __accept(int s, struct sockaddr *addr, socklen_t *addrlen)
108 {
109 	struct pthread *curthread;
110 	int oldcancel;
111 	int ret;
112 
113 	curthread = _get_curthread();
114 	oldcancel = _thr_cancel_enter(curthread);
115 	ret = __sys_accept(s, addr, addrlen);
116 	_thr_cancel_leave(curthread, oldcancel);
117 
118  	return (ret);
119 }
120 
121 __weak_reference(_aio_suspend, aio_suspend);
122 
123 int
124 _aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct
125     timespec *timeout)
126 {
127 	struct pthread *curthread = _get_curthread();
128 	int oldcancel;
129 	int ret;
130 
131 	oldcancel = _thr_cancel_enter(curthread);
132 	ret = __sys_aio_suspend(iocbs, niocb, timeout);
133 	_thr_cancel_leave(curthread, oldcancel);
134 
135 	return (ret);
136 }
137 
138 __weak_reference(__close, close);
139 
140 int
141 __close(int fd)
142 {
143 	struct pthread	*curthread = _get_curthread();
144 	int	oldcancel;
145 	int	ret;
146 
147 	oldcancel = _thr_cancel_enter(curthread);
148 	ret = __sys_close(fd);
149 	_thr_cancel_leave(curthread, oldcancel);
150 
151 	return (ret);
152 }
153 
154 __weak_reference(__connect, connect);
155 
156 int
157 __connect(int fd, const struct sockaddr *name, socklen_t namelen)
158 {
159 	struct pthread *curthread = _get_curthread();
160 	int oldcancel;
161 	int ret;
162 
163 	curthread = _get_curthread();
164 	oldcancel = _thr_cancel_enter(curthread);
165 	ret = __sys_connect(fd, name, namelen);
166 	_thr_cancel_leave(curthread, oldcancel);
167 
168  	return (ret);
169 }
170 
171 __weak_reference(___creat, creat);
172 
173 int
174 ___creat(const char *path, mode_t mode)
175 {
176 	struct pthread *curthread = _get_curthread();
177 	int oldcancel;
178 	int ret;
179 
180 	oldcancel = _thr_cancel_enter(curthread);
181 	ret = __creat(path, mode);
182 	_thr_cancel_leave(curthread, oldcancel);
183 
184 	return ret;
185 }
186 
187 __weak_reference(__fcntl, fcntl);
188 
189 int
190 __fcntl(int fd, int cmd,...)
191 {
192 	struct pthread *curthread = _get_curthread();
193 	int	oldcancel;
194 	int	ret;
195 	va_list	ap;
196 
197 	oldcancel = _thr_cancel_enter(curthread);
198 
199 	va_start(ap, cmd);
200 	switch (cmd) {
201 	case F_DUPFD:
202 		ret = __sys_fcntl(fd, cmd, va_arg(ap, int));
203 		break;
204 	case F_SETFD:
205 	case F_SETFL:
206 		ret = __sys_fcntl(fd, cmd, va_arg(ap, int));
207 		break;
208 	case F_GETFD:
209 	case F_GETFL:
210 		ret = __sys_fcntl(fd, cmd);
211 		break;
212 	default:
213 		ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
214 	}
215 	va_end(ap);
216 
217 	_thr_cancel_leave(curthread, oldcancel);
218 
219 	return (ret);
220 }
221 
222 __weak_reference(__fsync, fsync);
223 
224 int
225 __fsync(int fd)
226 {
227 	struct pthread *curthread = _get_curthread();
228 	int	oldcancel;
229 	int	ret;
230 
231 	oldcancel = _thr_cancel_enter(curthread);
232 	ret = __sys_fsync(fd);
233 	_thr_cancel_leave(curthread, oldcancel);
234 
235 	return (ret);
236 }
237 
238 __weak_reference(__msync, msync);
239 
240 int
241 __msync(void *addr, size_t len, int flags)
242 {
243 	struct pthread *curthread = _get_curthread();
244 	int	oldcancel;
245 	int	ret;
246 
247 	oldcancel = _thr_cancel_enter(curthread);
248 	ret = __sys_msync(addr, len, flags);
249 	_thr_cancel_leave(curthread, oldcancel);
250 
251 	return ret;
252 }
253 
254 __weak_reference(__nanosleep, nanosleep);
255 
256 int
257 __nanosleep(const struct timespec *time_to_sleep,
258     struct timespec *time_remaining)
259 {
260 	struct pthread *curthread = _get_curthread();
261 	int		oldcancel;
262 	int		ret;
263 
264 	oldcancel = _thr_cancel_enter(curthread);
265 	ret = __sys_nanosleep(time_to_sleep, time_remaining);
266 	_thr_cancel_leave(curthread, oldcancel);
267 
268 	return (ret);
269 }
270 
271 __weak_reference(__open, open);
272 
273 int
274 __open(const char *path, int flags,...)
275 {
276 	struct pthread *curthread = _get_curthread();
277 	int	oldcancel;
278 	int	ret;
279 	int	mode = 0;
280 	va_list	ap;
281 
282 	oldcancel = _thr_cancel_enter(curthread);
283 
284 	/* Check if the file is being created: */
285 	if (flags & O_CREAT) {
286 		/* Get the creation mode: */
287 		va_start(ap, flags);
288 		mode = va_arg(ap, int);
289 		va_end(ap);
290 	}
291 
292 	ret = __sys_open(path, flags, mode);
293 
294 	_thr_cancel_leave(curthread, oldcancel);
295 
296 	return ret;
297 }
298 
299 __weak_reference(_pause, pause);
300 
301 int
302 _pause(void)
303 {
304 	struct pthread *curthread = _get_curthread();
305 	int	oldcancel;
306 	int	ret;
307 
308 	oldcancel = _thr_cancel_enter(curthread);
309 	ret = __pause();
310 	_thr_cancel_leave(curthread, oldcancel);
311 
312 	return ret;
313 }
314 
315 __weak_reference(__poll, poll);
316 
317 int
318 __poll(struct pollfd *fds, unsigned int nfds, int timeout)
319 {
320 	struct pthread *curthread = _get_curthread();
321 	int oldcancel;
322 	int ret;
323 
324 	oldcancel = _thr_cancel_enter(curthread);
325 	ret = __sys_poll(fds, nfds, timeout);
326 	_thr_cancel_leave(curthread, oldcancel);
327 
328 	return ret;
329 }
330 
331 __weak_reference(_pselect, pselect);
332 
333 int
334 _pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
335 	const struct timespec *timo, const sigset_t *mask)
336 {
337 	struct pthread *curthread = _get_curthread();
338 	int oldcancel;
339 	int ret;
340 
341 	oldcancel = _thr_cancel_enter(curthread);
342 	ret = __pselect(count, rfds, wfds, efds, timo, mask);
343 	_thr_cancel_leave(curthread, oldcancel);
344 
345 	return (ret);
346 }
347 
348 __weak_reference(_raise, raise);
349 
350 int
351 _raise(int sig)
352 {
353 	int ret;
354 
355 	if (!_thr_isthreaded())
356 		ret = kill(getpid(), sig);
357 	else
358 		ret = _thr_send_sig(_get_curthread(), sig);
359 	return (ret);
360 }
361 
362 __weak_reference(__read, read);
363 
364 ssize_t
365 __read(int fd, void *buf, size_t nbytes)
366 {
367 	struct pthread *curthread = _get_curthread();
368 	int oldcancel;
369 	ssize_t	ret;
370 
371 	oldcancel = _thr_cancel_enter(curthread);
372 	ret = __sys_read(fd, buf, nbytes);
373 	_thr_cancel_leave(curthread, oldcancel);
374 
375 	return ret;
376 }
377 
378 __weak_reference(__readv, readv);
379 
380 ssize_t
381 __readv(int fd, const struct iovec *iov, int iovcnt)
382 {
383 	struct pthread *curthread = _get_curthread();
384 	int oldcancel;
385 	ssize_t ret;
386 
387 	oldcancel = _thr_cancel_enter(curthread);
388 	ret = __sys_readv(fd, iov, iovcnt);
389 	_thr_cancel_leave(curthread, oldcancel);
390 
391 	return ret;
392 }
393 
394 __weak_reference(__recvfrom, recvfrom);
395 
396 ssize_t
397 __recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from,
398     socklen_t *fl)
399 {
400 	struct pthread *curthread = _get_curthread();
401 	int oldcancel;
402 	ssize_t ret;
403 
404 	oldcancel = _thr_cancel_enter(curthread);
405 	ret = __sys_recvfrom(s, b, l, f, from, fl);
406 	_thr_cancel_leave(curthread, oldcancel);
407 	return (ret);
408 }
409 
410 __weak_reference(__recvmsg, recvmsg);
411 
412 ssize_t
413 __recvmsg(int s, struct msghdr *m, int f)
414 {
415 	struct pthread *curthread = _get_curthread();
416 	ssize_t ret;
417 	int oldcancel;
418 
419 	oldcancel = _thr_cancel_enter(curthread);
420 	ret = __sys_recvmsg(s, m, f);
421 	_thr_cancel_leave(curthread, oldcancel);
422 	return (ret);
423 }
424 
425 __weak_reference(__select, select);
426 
427 int
428 __select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
429 	struct timeval *timeout)
430 {
431 	struct pthread *curthread = _get_curthread();
432 	int oldcancel;
433 	int ret;
434 
435 	oldcancel = _thr_cancel_enter(curthread);
436 	ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
437 	_thr_cancel_leave(curthread, oldcancel);
438 	return ret;
439 }
440 
441 __weak_reference(__sendmsg, sendmsg);
442 
443 ssize_t
444 __sendmsg(int s, const struct msghdr *m, int f)
445 {
446 	struct pthread *curthread = _get_curthread();
447 	ssize_t ret;
448 	int oldcancel;
449 
450 	oldcancel = _thr_cancel_enter(curthread);
451 	ret = __sys_sendmsg(s, m, f);
452 	_thr_cancel_leave(curthread, oldcancel);
453 	return (ret);
454 }
455 
456 __weak_reference(__sendto, sendto);
457 
458 ssize_t
459 __sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t,
460     socklen_t tl)
461 {
462 	struct pthread *curthread = _get_curthread();
463 	ssize_t ret;
464 	int oldcancel;
465 
466 	oldcancel = _thr_cancel_enter(curthread);
467 	ret = __sys_sendto(s, m, l, f, t, tl);
468 	_thr_cancel_leave(curthread, oldcancel);
469 	return (ret);
470 }
471 
472 __weak_reference(_sleep, sleep);
473 
474 unsigned int
475 _sleep(unsigned int seconds)
476 {
477 	struct pthread *curthread = _get_curthread();
478 	int		oldcancel;
479 	unsigned int	ret;
480 
481 	oldcancel = _thr_cancel_enter(curthread);
482 	ret = __sleep(seconds);
483 	_thr_cancel_leave(curthread, oldcancel);
484 
485 	return (ret);
486 }
487 
488 __weak_reference(_system, system);
489 
490 int
491 _system(const char *string)
492 {
493 	struct pthread *curthread = _get_curthread();
494 	int	oldcancel;
495 	int	ret;
496 
497 	oldcancel = _thr_cancel_enter(curthread);
498 	ret = __system(string);
499 	_thr_cancel_leave(curthread, oldcancel);
500 
501 	return ret;
502 }
503 
504 __weak_reference(_tcdrain, tcdrain);
505 
506 int
507 _tcdrain(int fd)
508 {
509 	struct pthread *curthread = _get_curthread();
510 	int	oldcancel;
511 	int	ret;
512 
513 	oldcancel = _thr_cancel_enter(curthread);
514 	ret = __tcdrain(fd);
515 	_thr_cancel_leave(curthread, oldcancel);
516 
517 	return (ret);
518 }
519 
520 __weak_reference(_usleep, usleep);
521 
522 int
523 _usleep(useconds_t useconds)
524 {
525 	struct pthread *curthread = _get_curthread();
526 	int		oldcancel;
527 	int		ret;
528 
529 	oldcancel = _thr_cancel_enter(curthread);
530 	ret = __usleep(useconds);
531 	_thr_cancel_leave(curthread, oldcancel);
532 
533 	return (ret);
534 }
535 
536 __weak_reference(_vfork, vfork);
537 
538 int
539 _vfork(void)
540 {
541 	return (fork());
542 }
543 
544 __weak_reference(_wait, wait);
545 
546 pid_t
547 _wait(int *istat)
548 {
549 	struct pthread *curthread = _get_curthread();
550 	int	oldcancel;
551 	pid_t	ret;
552 
553 	oldcancel = _thr_cancel_enter(curthread);
554 	ret = __wait(istat);
555 	_thr_cancel_leave(curthread, oldcancel);
556 
557 	return ret;
558 }
559 
560 __weak_reference(__wait4, wait4);
561 
562 pid_t
563 __wait4(pid_t pid, int *istat, int options, struct rusage *rusage)
564 {
565 	struct pthread *curthread = _get_curthread();
566 	int oldcancel;
567 	pid_t ret;
568 
569 	oldcancel = _thr_cancel_enter(curthread);
570 	ret = __sys_wait4(pid, istat, options, rusage);
571 	_thr_cancel_leave(curthread, oldcancel);
572 
573 	return ret;
574 }
575 
576 __weak_reference(_waitpid, waitpid);
577 
578 pid_t
579 _waitpid(pid_t wpid, int *status, int options)
580 {
581 	struct pthread *curthread = _get_curthread();
582 	int	oldcancel;
583 	pid_t	ret;
584 
585 	oldcancel = _thr_cancel_enter(curthread);
586 	ret = __waitpid(wpid, status, options);
587 	_thr_cancel_leave(curthread, oldcancel);
588 
589 	return ret;
590 }
591 
592 __weak_reference(__write, write);
593 
594 ssize_t
595 __write(int fd, const void *buf, size_t nbytes)
596 {
597 	struct pthread *curthread = _get_curthread();
598 	int	oldcancel;
599 	ssize_t	ret;
600 
601 	oldcancel = _thr_cancel_enter(curthread);
602 	ret = __sys_write(fd, buf, nbytes);
603 	_thr_cancel_leave(curthread, oldcancel);
604 
605 	return ret;
606 }
607 
608 __weak_reference(__writev, writev);
609 
610 ssize_t
611 __writev(int fd, const struct iovec *iov, int iovcnt)
612 {
613 	struct pthread *curthread = _get_curthread();
614 	int	oldcancel;
615 	ssize_t ret;
616 
617 	oldcancel = _thr_cancel_enter(curthread);
618 	ret = __sys_writev(fd, iov, iovcnt);
619 	_thr_cancel_leave(curthread, oldcancel);
620 
621 	return ret;
622 }
623