xref: /freebsd/lib/libthr/thread/thr_syscalls.c (revision 7aa383846770374466b1dcb2cefd71bde9acf463)
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. Neither the name of the author nor the names of any co-contributors
47  *    may be used to endorse or promote products derived from this software
48  *    without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  *
62  */
63 
64 #include "namespace.h"
65 #include <sys/types.h>
66 #include <sys/mman.h>
67 #include <sys/param.h>
68 #include <sys/select.h>
69 #include <sys/signalvar.h>
70 #include <sys/socket.h>
71 #include <sys/stat.h>
72 #include <sys/time.h>
73 #include <sys/uio.h>
74 #include <sys/wait.h>
75 #include <aio.h>
76 #include <dirent.h>
77 #include <errno.h>
78 #include <fcntl.h>
79 #include <poll.h>
80 #include <signal.h>
81 #include <stdarg.h>
82 #include <stdio.h>
83 #include <stdlib.h>
84 #include <string.h>
85 #include <termios.h>
86 #include <unistd.h>
87 #include <pthread.h>
88 #include "un-namespace.h"
89 
90 #include "thr_private.h"
91 
92 extern int	__creat(const char *, mode_t);
93 extern int	__pselect(int, fd_set *, fd_set *, fd_set *,
94 			const struct timespec *, const sigset_t *);
95 extern unsigned	__sleep(unsigned int);
96 extern int	__system(const char *);
97 extern int	__tcdrain(int);
98 extern int	__usleep(useconds_t);
99 extern pid_t	__wait(int *);
100 extern pid_t	__waitpid(pid_t, int *, int);
101 extern int	__sys_aio_suspend(const struct aiocb * const[], int,
102 			const struct timespec *);
103 extern int	__sys_accept(int, struct sockaddr *, socklen_t *);
104 extern int	__sys_connect(int, const struct sockaddr *, socklen_t);
105 extern int	__sys_fsync(int);
106 extern int	__sys_msync(void *, size_t, int);
107 extern int	__sys_pselect(int, fd_set *, fd_set *, fd_set *,
108 			const struct timespec *, const sigset_t *);
109 extern int	__sys_poll(struct pollfd *, unsigned, int);
110 extern ssize_t	__sys_recv(int, void *, size_t, int);
111 extern ssize_t	__sys_recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *);
112 extern ssize_t	__sys_recvmsg(int, struct msghdr *, int);
113 extern int	__sys_select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
114 extern int	__sys_sendfile(int, int, off_t, size_t, struct sf_hdtr *,
115 			off_t *, int);
116 extern ssize_t	__sys_sendmsg(int, const struct msghdr *, int);
117 extern ssize_t	__sys_sendto(int, const void *,size_t, int, const struct sockaddr *, socklen_t);
118 extern ssize_t	__sys_readv(int, const struct iovec *, int);
119 extern pid_t	__sys_wait4(pid_t, int *, int, struct rusage *);
120 extern ssize_t	__sys_writev(int, const struct iovec *, int);
121 
122 int	___creat(const char *, mode_t);
123 int	___pselect(int, fd_set *, fd_set *, fd_set *,
124 		const struct timespec *, const sigset_t *);
125 unsigned	___sleep(unsigned);
126 int	___system(const char *);
127 int	___tcdrain(int);
128 int	___usleep(useconds_t useconds);
129 pid_t	___wait(int *);
130 pid_t	___waitpid(pid_t, int *, int);
131 int	__accept(int, struct sockaddr *, socklen_t *);
132 int	__aio_suspend(const struct aiocb * const iocbs[], int,
133 		const struct timespec *);
134 int	__close(int);
135 int	__connect(int, const struct sockaddr *, socklen_t);
136 int	__fcntl(int, int,...);
137 #ifdef SYSCALL_COMPAT
138 extern int __fcntl_compat(int, int,...);
139 #endif
140 int	__fsync(int);
141 int	__msync(void *, size_t, int);
142 int	__nanosleep(const struct timespec *, struct timespec *);
143 int	__open(const char *, int,...);
144 int	__openat(int, const char *, int,...);
145 int	__poll(struct pollfd *, unsigned int, int);
146 ssize_t	__read(int, void *buf, size_t);
147 ssize_t	__readv(int, const struct iovec *, int);
148 ssize_t	__recvfrom(int, void *, size_t, int f, struct sockaddr *, socklen_t *);
149 ssize_t	__recvmsg(int, struct msghdr *, int);
150 int	__select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
151 ssize_t	__sendmsg(int, const struct msghdr *, int);
152 ssize_t	__sendto(int, const void *, size_t, int,
153 		const struct sockaddr *, socklen_t);
154 pid_t	__wait3(int *, int, struct rusage *);
155 pid_t	__wait4(pid_t, int *, int, struct rusage *);
156 ssize_t	__write(int, const void *, size_t);
157 ssize_t	__writev(int, const struct iovec *, int);
158 
159 __weak_reference(__accept, accept);
160 
161 int
162 __accept(int s, struct sockaddr *addr, socklen_t *addrlen)
163 {
164 	struct pthread *curthread;
165 	int ret;
166 
167 	curthread = _get_curthread();
168 	_thr_cancel_enter(curthread);
169 	ret = __sys_accept(s, addr, addrlen);
170 	_thr_cancel_leave(curthread);
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 ret;
183 
184 	_thr_cancel_enter(curthread);
185 	ret = __sys_aio_suspend(iocbs, niocb, timeout);
186 	_thr_cancel_leave(curthread);
187 
188 	return (ret);
189 }
190 
191 __weak_reference(__close, close);
192 
193 int
194 __close(int fd)
195 {
196 	struct pthread	*curthread = _get_curthread();
197 	int	ret;
198 
199 	_thr_cancel_enter(curthread);
200 	ret = __sys_close(fd);
201 	_thr_cancel_leave(curthread);
202 
203 	return (ret);
204 }
205 
206 __weak_reference(__connect, connect);
207 
208 int
209 __connect(int fd, const struct sockaddr *name, socklen_t namelen)
210 {
211 	struct pthread *curthread = _get_curthread();
212 	int ret;
213 
214 	_thr_cancel_enter(curthread);
215 	ret = __sys_connect(fd, name, namelen);
216 	_thr_cancel_leave(curthread);
217 
218  	return (ret);
219 }
220 
221 __weak_reference(___creat, creat);
222 
223 int
224 ___creat(const char *path, mode_t mode)
225 {
226 	struct pthread *curthread = _get_curthread();
227 	int ret;
228 
229 	_thr_cancel_enter(curthread);
230 	ret = __creat(path, mode);
231 	_thr_cancel_leave(curthread);
232 
233 	return ret;
234 }
235 
236 __weak_reference(__fcntl, fcntl);
237 
238 int
239 __fcntl(int fd, int cmd,...)
240 {
241 	struct pthread *curthread = _get_curthread();
242 	int	ret;
243 	va_list	ap;
244 
245 	_thr_cancel_enter(curthread);
246 
247 	va_start(ap, cmd);
248 	switch (cmd) {
249 	case F_DUPFD:
250 		ret = __sys_fcntl(fd, cmd, va_arg(ap, int));
251 		break;
252 	case F_SETFD:
253 	case F_SETFL:
254 		ret = __sys_fcntl(fd, cmd, va_arg(ap, int));
255 		break;
256 	case F_GETFD:
257 	case F_GETFL:
258 		ret = __sys_fcntl(fd, cmd);
259 		break;
260 	default:
261 #ifdef SYSCALL_COMPAT
262 		ret = __fcntl_compat(fd, cmd, va_arg(ap, void *));
263 #else
264 		ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
265 #endif
266 	}
267 	va_end(ap);
268 
269 	_thr_cancel_leave(curthread);
270 
271 	return (ret);
272 }
273 
274 __weak_reference(__fsync, fsync);
275 
276 int
277 __fsync(int fd)
278 {
279 	struct pthread *curthread = _get_curthread();
280 	int	ret;
281 
282 	_thr_cancel_enter(curthread);
283 	ret = __sys_fsync(fd);
284 	_thr_cancel_leave(curthread);
285 
286 	return (ret);
287 }
288 
289 __weak_reference(__msync, msync);
290 
291 int
292 __msync(void *addr, size_t len, int flags)
293 {
294 	struct pthread *curthread = _get_curthread();
295 	int	ret;
296 
297 	_thr_cancel_enter(curthread);
298 	ret = __sys_msync(addr, len, flags);
299 	_thr_cancel_leave(curthread);
300 
301 	return ret;
302 }
303 
304 __weak_reference(__nanosleep, nanosleep);
305 
306 int
307 __nanosleep(const struct timespec *time_to_sleep,
308     struct timespec *time_remaining)
309 {
310 	struct pthread *curthread = _get_curthread();
311 	int		ret;
312 
313 	_thr_cancel_enter(curthread);
314 	ret = __sys_nanosleep(time_to_sleep, time_remaining);
315 	_thr_cancel_leave(curthread);
316 
317 	return (ret);
318 }
319 
320 __weak_reference(__open, open);
321 
322 int
323 __open(const char *path, int flags,...)
324 {
325 	struct pthread *curthread = _get_curthread();
326 	int	ret;
327 	int	mode = 0;
328 	va_list	ap;
329 
330 	_thr_cancel_enter(curthread);
331 
332 	/* Check if the file is being created: */
333 	if (flags & O_CREAT) {
334 		/* Get the creation mode: */
335 		va_start(ap, flags);
336 		mode = va_arg(ap, int);
337 		va_end(ap);
338 	}
339 
340 	ret = __sys_open(path, flags, mode);
341 
342 	_thr_cancel_leave(curthread);
343 
344 	return ret;
345 }
346 
347 __weak_reference(__openat, openat);
348 
349 int
350 __openat(int fd, const char *path, int flags, ...)
351 {
352 	struct pthread *curthread = _get_curthread();
353 	int	ret;
354 	int	mode = 0;
355 	va_list	ap;
356 
357 	_thr_cancel_enter(curthread);
358 
359 	/* Check if the file is being created: */
360 	if (flags & O_CREAT) {
361 		/* Get the creation mode: */
362 		va_start(ap, flags);
363 		mode = va_arg(ap, int);
364 		va_end(ap);
365 	}
366 
367 	ret = __sys_openat(fd, path, flags, mode);
368 
369 	_thr_cancel_leave(curthread);
370 
371 	return ret;
372 }
373 
374 __weak_reference(__poll, poll);
375 
376 int
377 __poll(struct pollfd *fds, unsigned int nfds, int timeout)
378 {
379 	struct pthread *curthread = _get_curthread();
380 	int ret;
381 
382 	_thr_cancel_enter(curthread);
383 	ret = __sys_poll(fds, nfds, timeout);
384 	_thr_cancel_leave(curthread);
385 
386 	return ret;
387 }
388 
389 __weak_reference(___pselect, pselect);
390 
391 int
392 ___pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
393 	const struct timespec *timo, const sigset_t *mask)
394 {
395 	struct pthread *curthread = _get_curthread();
396 	int ret;
397 
398 	_thr_cancel_enter(curthread);
399 	ret = __sys_pselect(count, rfds, wfds, efds, timo, mask);
400 	_thr_cancel_leave(curthread);
401 
402 	return (ret);
403 }
404 
405 __weak_reference(__read, read);
406 
407 ssize_t
408 __read(int fd, void *buf, size_t nbytes)
409 {
410 	struct pthread *curthread = _get_curthread();
411 	ssize_t	ret;
412 
413 	_thr_cancel_enter(curthread);
414 	ret = __sys_read(fd, buf, nbytes);
415 	_thr_cancel_leave(curthread);
416 
417 	return ret;
418 }
419 
420 __weak_reference(__readv, readv);
421 
422 ssize_t
423 __readv(int fd, const struct iovec *iov, int iovcnt)
424 {
425 	struct pthread *curthread = _get_curthread();
426 	ssize_t ret;
427 
428 	_thr_cancel_enter(curthread);
429 	ret = __sys_readv(fd, iov, iovcnt);
430 	_thr_cancel_leave(curthread);
431 
432 	return ret;
433 }
434 
435 __weak_reference(__recvfrom, recvfrom);
436 
437 ssize_t
438 __recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from,
439     socklen_t *fl)
440 {
441 	struct pthread *curthread = _get_curthread();
442 	ssize_t ret;
443 
444 	_thr_cancel_enter(curthread);
445 	ret = __sys_recvfrom(s, b, l, f, from, fl);
446 	_thr_cancel_leave(curthread);
447 	return (ret);
448 }
449 
450 __weak_reference(__recvmsg, recvmsg);
451 
452 ssize_t
453 __recvmsg(int s, struct msghdr *m, int f)
454 {
455 	struct pthread *curthread = _get_curthread();
456 	ssize_t ret;
457 
458 	_thr_cancel_enter(curthread);
459 	ret = __sys_recvmsg(s, m, f);
460 	_thr_cancel_leave(curthread);
461 	return (ret);
462 }
463 
464 __weak_reference(__select, select);
465 
466 int
467 __select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
468 	struct timeval *timeout)
469 {
470 	struct pthread *curthread = _get_curthread();
471 	int ret;
472 
473 	_thr_cancel_enter(curthread);
474 	ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
475 	_thr_cancel_leave(curthread);
476 	return ret;
477 }
478 
479 __weak_reference(__sendmsg, sendmsg);
480 
481 ssize_t
482 __sendmsg(int s, const struct msghdr *m, int f)
483 {
484 	struct pthread *curthread = _get_curthread();
485 	ssize_t ret;
486 
487 	_thr_cancel_enter(curthread);
488 	ret = __sys_sendmsg(s, m, f);
489 	_thr_cancel_leave(curthread);
490 	return (ret);
491 }
492 
493 __weak_reference(__sendto, sendto);
494 
495 ssize_t
496 __sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t,
497     socklen_t tl)
498 {
499 	struct pthread *curthread = _get_curthread();
500 	ssize_t ret;
501 
502 	_thr_cancel_enter(curthread);
503 	ret = __sys_sendto(s, m, l, f, t, tl);
504 	_thr_cancel_leave(curthread);
505 	return (ret);
506 }
507 
508 __weak_reference(___sleep, sleep);
509 
510 unsigned int
511 ___sleep(unsigned int seconds)
512 {
513 	struct pthread *curthread = _get_curthread();
514 	unsigned int	ret;
515 
516 	_thr_cancel_enter(curthread);
517 	ret = __sleep(seconds);
518 	_thr_cancel_leave(curthread);
519 
520 	return (ret);
521 }
522 
523 __weak_reference(___system, system);
524 
525 int
526 ___system(const char *string)
527 {
528 	struct pthread *curthread = _get_curthread();
529 	int	ret;
530 
531 	_thr_cancel_enter(curthread);
532 	ret = __system(string);
533 	_thr_cancel_leave(curthread);
534 
535 	return ret;
536 }
537 
538 __weak_reference(___tcdrain, tcdrain);
539 
540 int
541 ___tcdrain(int fd)
542 {
543 	struct pthread *curthread = _get_curthread();
544 	int	ret;
545 
546 	_thr_cancel_enter(curthread);
547 	ret = __tcdrain(fd);
548 	_thr_cancel_leave(curthread);
549 
550 	return (ret);
551 }
552 
553 __weak_reference(___usleep, usleep);
554 
555 int
556 ___usleep(useconds_t useconds)
557 {
558 	struct pthread *curthread = _get_curthread();
559 	int		ret;
560 
561 	_thr_cancel_enter(curthread);
562 	ret = __usleep(useconds);
563 	_thr_cancel_leave(curthread);
564 
565 	return (ret);
566 }
567 
568 __weak_reference(___wait, wait);
569 
570 pid_t
571 ___wait(int *istat)
572 {
573 	struct pthread *curthread = _get_curthread();
574 	pid_t	ret;
575 
576 	_thr_cancel_enter(curthread);
577 	ret = __wait(istat);
578 	_thr_cancel_leave(curthread);
579 
580 	return ret;
581 }
582 
583 __weak_reference(__wait3, wait3);
584 
585 pid_t
586 __wait3(int *status, int options, struct rusage *rusage)
587 {
588 	struct pthread *curthread = _get_curthread();
589 	pid_t ret;
590 
591 	_thr_cancel_enter(curthread);
592 	ret = _wait4(WAIT_ANY, status, options, rusage);
593 	_thr_cancel_leave(curthread);
594 
595 	return (ret);
596 }
597 
598 __weak_reference(__wait4, wait4);
599 
600 pid_t
601 __wait4(pid_t pid, int *status, int options, struct rusage *rusage)
602 {
603 	struct pthread *curthread = _get_curthread();
604 	pid_t ret;
605 
606 	_thr_cancel_enter(curthread);
607 	ret = __sys_wait4(pid, status, options, rusage);
608 	_thr_cancel_leave(curthread);
609 
610 	return ret;
611 }
612 
613 __weak_reference(___waitpid, waitpid);
614 
615 pid_t
616 ___waitpid(pid_t wpid, int *status, int options)
617 {
618 	struct pthread *curthread = _get_curthread();
619 	pid_t	ret;
620 
621 	_thr_cancel_enter(curthread);
622 	ret = __waitpid(wpid, status, options);
623 	_thr_cancel_leave(curthread);
624 
625 	return ret;
626 }
627 
628 __weak_reference(__write, write);
629 
630 ssize_t
631 __write(int fd, const void *buf, size_t nbytes)
632 {
633 	struct pthread *curthread = _get_curthread();
634 	ssize_t	ret;
635 
636 	_thr_cancel_enter(curthread);
637 	ret = __sys_write(fd, buf, nbytes);
638 	_thr_cancel_leave(curthread);
639 
640 	return ret;
641 }
642 
643 __weak_reference(__writev, writev);
644 
645 ssize_t
646 __writev(int fd, const struct iovec *iov, int iovcnt)
647 {
648 	struct pthread *curthread = _get_curthread();
649 	ssize_t ret;
650 
651 	_thr_cancel_enter(curthread);
652 	ret = __sys_writev(fd, iov, iovcnt);
653 	_thr_cancel_leave(curthread);
654 
655 	return ret;
656 }
657