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