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  * $DragonFly: src/lib/libthread_xu/thread/thr_syscalls.c,v 1.8 2008/01/10 22:30:27 nth Exp $
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 
78 #include <machine/tls.h>
79 
80 #include <aio.h>
81 #include <dirent.h>
82 #include <errno.h>
83 #include <fcntl.h>
84 #include <poll.h>
85 #include <signal.h>
86 #include <stdarg.h>
87 #include <stdio.h>
88 #include <stdlib.h>
89 #include <string.h>
90 #include <termios.h>
91 #include <unistd.h>
92 #include <pthread.h>
93 
94 #include "thr_private.h"
95 
96 extern int	__creat(const char *, mode_t);
97 extern int	__pause(void);
98 extern int	__sys_pselect(int, fd_set *, fd_set *, fd_set *,
99 			const struct timespec *, const sigset_t *);
100 extern unsigned	__sleep(unsigned int);
101 extern int	__system(const char *);
102 extern int	__tcdrain(int);
103 extern int	__usleep(unsigned);
104 extern pid_t	__wait(int *);
105 extern pid_t	__waitpid(pid_t, int *, int);
106 extern int	__sys_aio_suspend(const struct aiocb * const[], int,
107 			const struct timespec *);
108 extern int	__sys_accept(int, struct sockaddr *, socklen_t *);
109 extern int	__sys_connect(int, const struct sockaddr *, socklen_t);
110 extern int	__sys_fsync(int);
111 extern int	__sys_msync(void *, size_t, int);
112 extern int	__sys_poll(struct pollfd *, unsigned, int);
113 extern ssize_t	__sys_recv(int, void *, size_t, int);
114 extern ssize_t	__sys_recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *);
115 extern ssize_t	__sys_recvmsg(int, struct msghdr *, int);
116 extern int	__sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
117 extern int	__sys_sendfile(int, int, off_t, size_t, struct sf_hdtr *,
118 			off_t *, int);
119 extern ssize_t	__sys_sendmsg(int, const struct msghdr *, int);
120 extern ssize_t	__sys_sendto(int, const void *,size_t, int, const struct sockaddr *, socklen_t);
121 extern ssize_t	__sys_readv(int, const struct iovec *, int);
122 extern pid_t	__sys_wait4(pid_t, int *, int, struct rusage *);
123 extern ssize_t	__sys_writev(int, const struct iovec *, int);
124 
125 int	___creat(const char *, mode_t);
126 int	___usleep(unsigned);
127 int	__accept(int, struct sockaddr *, socklen_t *);
128 int	__close(int);
129 int	__connect(int, const struct sockaddr *, socklen_t);
130 int	__fcntl(int, int,...);
131 int	__fsync(int);
132 int	__msync(void *, size_t, int);
133 int	__nanosleep(const struct timespec *, struct timespec *);
134 int	__open(const char *, int,...);
135 int	__poll(struct pollfd *, unsigned int, int);
136 ssize_t	__read(int, void *buf, size_t);
137 ssize_t	__readv(int, const struct iovec *, int);
138 ssize_t	__recvfrom(int, void *, size_t, int f, struct sockaddr *, socklen_t *);
139 ssize_t	__recvmsg(int, struct msghdr *, int);
140 int	__select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
141 ssize_t	__sendmsg(int, const struct msghdr *, int);
142 ssize_t	__sendto(int, const void *, size_t, int,
143 		const struct sockaddr *, socklen_t);
144 pid_t	__wait4(pid_t, int *, int, struct rusage *);
145 ssize_t	__write(int, const void *, size_t);
146 ssize_t	__writev(int, const struct iovec *, int);
147 int	_aio_suspend(const struct aiocb * const iocbs[], int,
148 		const struct timespec *);
149 int	_pause(void);
150 int	_pselect(int, fd_set *, fd_set *, fd_set *,
151 		const struct timespec *, const sigset_t *);
152 int	_raise(int);
153 unsigned	_sleep(unsigned);
154 int	_system(const char *);
155 int	_tcdrain(int);
156 int	_vfork(void);
157 pid_t	_wait(int *);
158 pid_t	_waitpid(pid_t wpid, int *status, int options);
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 = tls_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 __strong_reference(__accept, accept);
176 
177 int
178 _aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct
179     timespec *timeout)
180 {
181 	struct pthread *curthread = tls_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 __strong_reference(_aio_suspend, aio_suspend);
193 
194 int
195 __close(int fd)
196 {
197 	struct pthread	*curthread = tls_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 __strong_reference(__close, close);
209 
210 int
211 __connect(int fd, const struct sockaddr *name, socklen_t namelen)
212 {
213 	struct pthread *curthread = tls_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 __strong_reference(__connect, connect);
225 
226 int
227 ___creat(const char *path, mode_t mode)
228 {
229 	struct pthread *curthread = tls_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 __strong_reference(___creat, creat);
241 
242 int
243 __fcntl(int fd, int cmd,...)
244 {
245 	struct pthread *curthread = tls_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 __strong_reference(__fcntl, fcntl);
276 
277 int
278 __fsync(int fd)
279 {
280 	struct pthread *curthread = tls_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 __strong_reference(__fsync, fsync);
292 
293 int
294 __msync(void *addr, size_t len, int flags)
295 {
296 	struct pthread *curthread = tls_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 __strong_reference(__msync, msync);
308 
309 int
310 __nanosleep(const struct timespec *time_to_sleep,
311     struct timespec *time_remaining)
312 {
313 	struct pthread *curthread = tls_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 __strong_reference(__nanosleep, nanosleep);
325 
326 int
327 __open(const char *path, int flags,...)
328 {
329 	struct pthread *curthread = tls_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 __strong_reference(__open, open);
353 
354 int
355 _pause(void)
356 {
357 	struct pthread *curthread = tls_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 __strong_reference(_pause, pause);
369 
370 int
371 __poll(struct pollfd *fds, unsigned int nfds, int timeout)
372 {
373 	struct pthread *curthread = tls_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 __strong_reference(__poll, poll);
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 = tls_get_curthread();
391 	int oldcancel;
392 	int ret;
393 
394 	oldcancel = _thr_cancel_enter(curthread);
395 	ret = __sys_pselect(count, rfds, wfds, efds, timo, mask);
396 	_thr_cancel_leave(curthread, oldcancel);
397 
398 	return (ret);
399 }
400 __strong_reference(__pselect, pselect);
401 
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(tls_get_curthread(), sig);
412 	return (ret);
413 }
414 
415 __strong_reference(_raise, raise);
416 
417 ssize_t
418 __read(int fd, void *buf, size_t nbytes)
419 {
420 	struct pthread *curthread = tls_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 __strong_reference(__read, read);
432 
433 ssize_t
434 __readv(int fd, const struct iovec *iov, int iovcnt)
435 {
436 	struct pthread *curthread = tls_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 __strong_reference(__readv, readv);
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 = tls_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 __strong_reference(__recvfrom, recvfrom);
464 
465 ssize_t
466 __recvmsg(int s, struct msghdr *m, int f)
467 {
468 	struct pthread *curthread = tls_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 __strong_reference(__recvmsg, recvmsg);
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 = tls_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 __strong_reference(__select, select);
495 
496 ssize_t
497 __sendmsg(int s, const struct msghdr *m, int f)
498 {
499 	struct pthread *curthread = tls_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 __strong_reference(__sendmsg, sendmsg);
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 = tls_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 __strong_reference(__sendto, sendto);
526 
527 unsigned int
528 _sleep(unsigned int seconds)
529 {
530 	struct pthread *curthread = tls_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 int
542 _system(const char *string)
543 {
544 	struct pthread *curthread = tls_get_curthread();
545 	int	oldcancel;
546 	int	ret;
547 
548 	oldcancel = _thr_cancel_enter(curthread);
549 	ret = __system(string);
550 	_thr_cancel_leave(curthread, oldcancel);
551 
552 	return ret;
553 }
554 
555 __strong_reference(_system, system);
556 
557 int
558 _tcdrain(int fd)
559 {
560 	struct pthread *curthread = tls_get_curthread();
561 	int	oldcancel;
562 	int	ret;
563 
564 	oldcancel = _thr_cancel_enter(curthread);
565 	ret = __tcdrain(fd);
566 	_thr_cancel_leave(curthread, oldcancel);
567 
568 	return (ret);
569 }
570 
571 __strong_reference(_tcdrain, tcdrain);
572 
573 int
574 ___usleep(unsigned int useconds)
575 {
576 	struct pthread *curthread = tls_get_curthread();
577 	int		oldcancel;
578 	int		ret;
579 
580 	oldcancel = _thr_cancel_enter(curthread);
581 	ret = __usleep(useconds);
582 	_thr_cancel_leave(curthread, oldcancel);
583 
584 	return (ret);
585 }
586 
587 __strong_reference(___usleep, usleep);
588 
589 int
590 _vfork(void)
591 {
592 	return (fork());
593 }
594 
595 __strong_reference(_vfork, vfork);
596 
597 pid_t
598 _wait(int *istat)
599 {
600 	struct pthread *curthread = tls_get_curthread();
601 	int	oldcancel;
602 	pid_t	ret;
603 
604 	oldcancel = _thr_cancel_enter(curthread);
605 	ret = __wait(istat);
606 	_thr_cancel_leave(curthread, oldcancel);
607 
608 	return ret;
609 }
610 
611 __strong_reference(_wait, wait);
612 
613 pid_t
614 __wait4(pid_t pid, int *istat, int options, struct rusage *rusage)
615 {
616 	struct pthread *curthread = tls_get_curthread();
617 	int oldcancel;
618 	pid_t ret;
619 
620 	oldcancel = _thr_cancel_enter(curthread);
621 	ret = __sys_wait4(pid, istat, options, rusage);
622 	_thr_cancel_leave(curthread, oldcancel);
623 
624 	return ret;
625 }
626 
627 __strong_reference(__wait4, wait4);
628 
629 pid_t
630 _waitpid(pid_t wpid, int *status, int options)
631 {
632 	struct pthread *curthread = tls_get_curthread();
633 	int	oldcancel;
634 	pid_t	ret;
635 
636 	oldcancel = _thr_cancel_enter(curthread);
637 	ret = __waitpid(wpid, status, options);
638 	_thr_cancel_leave(curthread, oldcancel);
639 
640 	return ret;
641 }
642 
643 __strong_reference(_waitpid, waitpid);
644 
645 ssize_t
646 __write(int fd, const void *buf, size_t nbytes)
647 {
648 	struct pthread *curthread = tls_get_curthread();
649 	int	oldcancel;
650 	ssize_t	ret;
651 
652 	oldcancel = _thr_cancel_enter(curthread);
653 	ret = __sys_write(fd, buf, nbytes);
654 	_thr_cancel_leave(curthread, oldcancel);
655 
656 	return ret;
657 }
658 
659 __strong_reference(__write, write);
660 
661 ssize_t
662 __writev(int fd, const struct iovec *iov, int iovcnt)
663 {
664 	struct pthread *curthread = tls_get_curthread();
665 	int	oldcancel;
666 	ssize_t ret;
667 
668 	oldcancel = _thr_cancel_enter(curthread);
669 	ret = __sys_writev(fd, iov, iovcnt);
670 	_thr_cancel_leave(curthread, oldcancel);
671 
672 	return ret;
673 }
674 
675 __strong_reference(__writev, writev);
676 
677