xref: /freebsd/lib/libthr/thread/thr_syscalls.c (revision 193d9e768ba63fcfb187cfd17f461f7d41345048)
1 /*
2  * Copyright (c) 2014 The FreeBSD Foundation.
3  * Copyright (C) 2005 David Xu <davidxu@freebsd.org>.
4  * Copyright (c) 2003 Daniel Eischen <deischen@freebsd.org>.
5  * Copyright (C) 2000 Jason Evans <jasone@freebsd.org>.
6  * All rights reserved.
7  *
8  * Portions of this software were developed by Konstantin Belousov
9  * under sponsorship from the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice(s), this list of conditions and the following disclaimer as
16  *    the first lines of this file unmodified other than the possible
17  *    addition of one or more copyright notices.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice(s), this list of conditions and the following disclaimer in
20  *    the documentation and/or other materials provided with the
21  *    distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
24  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
33  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /*
37  * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
38  * All rights reserved.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. Neither the name of the author nor the names of any co-contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  *
64  */
65 
66 #include <sys/cdefs.h>
67 __FBSDID("$FreeBSD$");
68 
69 #include "namespace.h"
70 #include <sys/types.h>
71 #include <sys/mman.h>
72 #include <sys/param.h>
73 #include <sys/select.h>
74 #include <sys/signalvar.h>
75 #include <sys/socket.h>
76 #include <sys/stat.h>
77 #include <sys/time.h>
78 #include <sys/uio.h>
79 #include <sys/wait.h>
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 #include "un-namespace.h"
94 
95 #include "libc_private.h"
96 #include "thr_private.h"
97 
98 static int
99 __thr_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
100 {
101 	struct pthread *curthread;
102 	int ret;
103 
104 	curthread = _get_curthread();
105 	_thr_cancel_enter(curthread);
106 	ret = __sys_accept(s, addr, addrlen);
107 	_thr_cancel_leave(curthread, ret == -1);
108 
109  	return (ret);
110 }
111 
112 /*
113  * Cancellation behavior:
114  *   If thread is canceled, no socket is created.
115  */
116 static int
117 __thr_accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags)
118 {
119 	struct pthread *curthread;
120 	int ret;
121 
122 	curthread = _get_curthread();
123 	_thr_cancel_enter(curthread);
124 	ret = __sys_accept4(s, addr, addrlen, flags);
125 	_thr_cancel_leave(curthread, ret == -1);
126 
127  	return (ret);
128 }
129 
130 static int
131 __thr_aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct
132     timespec *timeout)
133 {
134 	struct pthread *curthread;
135 	int ret;
136 
137 	curthread = _get_curthread();
138 	_thr_cancel_enter(curthread);
139 	ret = __sys_aio_suspend(iocbs, niocb, timeout);
140 	_thr_cancel_leave(curthread, 1);
141 
142 	return (ret);
143 }
144 
145 /*
146  * Cancellation behavior:
147  *   According to manual of close(), the file descriptor is always deleted.
148  *   Here, thread is only canceled after the system call, so the file
149  *   descriptor is always deleted despite whether the thread is canceled
150  *   or not.
151  */
152 static int
153 __thr_close(int fd)
154 {
155 	struct pthread *curthread;
156 	int ret;
157 
158 	curthread = _get_curthread();
159 	_thr_cancel_enter2(curthread, 0);
160 	ret = __sys_close(fd);
161 	_thr_cancel_leave(curthread, 1);
162 
163 	return (ret);
164 }
165 
166 /*
167  * Cancellation behavior:
168  *   If the thread is canceled, connection is not made.
169  */
170 static int
171 __thr_connect(int fd, const struct sockaddr *name, socklen_t namelen)
172 {
173 	struct pthread *curthread;
174 	int ret;
175 
176 	curthread = _get_curthread();
177 	_thr_cancel_enter(curthread);
178 	ret = __sys_connect(fd, name, namelen);
179 	_thr_cancel_leave(curthread, ret == -1);
180 
181  	return (ret);
182 }
183 
184 /*
185  * Cancellation behavior:
186  *   According to specification, only F_SETLKW is a cancellation point.
187  *   Thread is only canceled at start, or canceled if the system call
188  *   is failure, this means the function does not generate side effect
189  *   if it is canceled.
190  */
191 static int
192 __thr_fcntl(int fd, int cmd, ...)
193 {
194 	struct pthread *curthread;
195 	int ret;
196 	va_list	ap;
197 
198 	curthread = _get_curthread();
199 	va_start(ap, cmd);
200 	if (cmd == F_OSETLKW || cmd == F_SETLKW) {
201 		_thr_cancel_enter(curthread);
202 		ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
203 		_thr_cancel_leave(curthread, ret == -1);
204 	} else {
205 		ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
206 	}
207 	va_end(ap);
208 
209 	return (ret);
210 }
211 
212 /*
213  * Cancellation behavior:
214  *   Thread may be canceled after system call.
215  */
216 static int
217 __thr_fsync(int fd)
218 {
219 	struct pthread *curthread;
220 	int ret;
221 
222 	curthread = _get_curthread();
223 	_thr_cancel_enter2(curthread, 0);
224 	ret = __sys_fsync(fd);
225 	_thr_cancel_leave(curthread, 1);
226 
227 	return (ret);
228 }
229 
230 static int
231 __thr_fdatasync(int fd)
232 {
233 	struct pthread *curthread;
234 	int ret;
235 
236 	curthread = _get_curthread();
237 	_thr_cancel_enter2(curthread, 0);
238 	ret = __sys_fdatasync(fd);
239 	_thr_cancel_leave(curthread, 1);
240 
241 	return (ret);
242 }
243 
244 /*
245  * Cancellation behavior:
246  *   Thread may be canceled after system call.
247  */
248 static int
249 __thr_msync(void *addr, size_t len, int flags)
250 {
251 	struct pthread *curthread;
252 	int ret;
253 
254 	curthread = _get_curthread();
255 	_thr_cancel_enter2(curthread, 0);
256 	ret = __sys_msync(addr, len, flags);
257 	_thr_cancel_leave(curthread, 1);
258 
259 	return (ret);
260 }
261 
262 static int
263 __thr_nanosleep(const struct timespec *time_to_sleep,
264     struct timespec *time_remaining)
265 {
266 	struct pthread *curthread;
267 	int ret;
268 
269 	curthread = _get_curthread();
270 	_thr_cancel_enter(curthread);
271 	ret = __sys_nanosleep(time_to_sleep, time_remaining);
272 	_thr_cancel_leave(curthread, 1);
273 
274 	return (ret);
275 }
276 
277 /*
278  * Cancellation behavior:
279  *   If the thread is canceled, file is not opened.
280  */
281 static int
282 __thr_openat(int fd, const char *path, int flags, ...)
283 {
284 	struct pthread *curthread;
285 	int mode, ret;
286 	va_list	ap;
287 
288 
289 	/* Check if the file is being created: */
290 	if ((flags & O_CREAT) != 0) {
291 		/* Get the creation mode: */
292 		va_start(ap, flags);
293 		mode = va_arg(ap, int);
294 		va_end(ap);
295 	} else {
296 		mode = 0;
297 	}
298 
299 	curthread = _get_curthread();
300 	_thr_cancel_enter(curthread);
301 	ret = __sys_openat(fd, path, flags, mode);
302 	_thr_cancel_leave(curthread, ret == -1);
303 
304 	return (ret);
305 }
306 
307 /*
308  * Cancellation behavior:
309  *   Thread may be canceled at start, but if the system call returns something,
310  *   the thread is not canceled.
311  */
312 static int
313 __thr_poll(struct pollfd *fds, unsigned int nfds, int timeout)
314 {
315 	struct pthread *curthread;
316 	int ret;
317 
318 	curthread = _get_curthread();
319 	_thr_cancel_enter(curthread);
320 	ret = __sys_poll(fds, nfds, timeout);
321 	_thr_cancel_leave(curthread, ret == -1);
322 
323 	return (ret);
324 }
325 
326 /*
327  * Cancellation behavior:
328  *   Thread may be canceled at start, but if the system call returns something,
329  *   the thread is not canceled.
330  */
331 static int
332 __thr_ppoll(struct pollfd pfd[], nfds_t nfds, const struct timespec *
333     timeout, const sigset_t *newsigmask)
334 {
335 	struct pthread *curthread;
336 	int ret;
337 
338 	curthread = _get_curthread();
339 	_thr_cancel_enter(curthread);
340 	ret = __sys_ppoll(pfd, nfds, timeout, newsigmask);
341 	_thr_cancel_leave(curthread, ret == -1);
342 
343 	return (ret);
344 }
345 
346 /*
347  * Cancellation behavior:
348  *   Thread may be canceled at start, but if the system call returns something,
349  *   the thread is not canceled.
350  */
351 static int
352 __thr_pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
353 	const struct timespec *timo, const sigset_t *mask)
354 {
355 	struct pthread *curthread;
356 	int ret;
357 
358 	curthread = _get_curthread();
359 	_thr_cancel_enter(curthread);
360 	ret = __sys_pselect(count, rfds, wfds, efds, timo, mask);
361 	_thr_cancel_leave(curthread, ret == -1);
362 
363 	return (ret);
364 }
365 
366 static int
367 __thr_kevent(int kq, const struct kevent *changelist, int nchanges,
368     struct kevent *eventlist, int nevents, const struct timespec *timeout)
369 {
370 	struct pthread *curthread;
371 	int ret;
372 
373 	if (nevents == 0) {
374 		/*
375 		 * No blocking, do not make the call cancellable.
376 		 */
377 		return (__sys_kevent(kq, changelist, nchanges, eventlist,
378 		    nevents, timeout));
379 	}
380 	curthread = _get_curthread();
381 	_thr_cancel_enter(curthread);
382 	ret = __sys_kevent(kq, changelist, nchanges, eventlist, nevents,
383 	    timeout);
384 	_thr_cancel_leave(curthread, ret == -1 && nchanges == 0);
385 
386 	return (ret);
387 }
388 
389 /*
390  * Cancellation behavior:
391  *   Thread may be canceled at start, but if the system call got some data,
392  *   the thread is not canceled.
393  */
394 static ssize_t
395 __thr_read(int fd, void *buf, size_t nbytes)
396 {
397 	struct pthread *curthread;
398 	ssize_t	ret;
399 
400 	curthread = _get_curthread();
401 	_thr_cancel_enter(curthread);
402 	ret = __sys_read(fd, buf, nbytes);
403 	_thr_cancel_leave(curthread, ret == -1);
404 
405 	return (ret);
406 }
407 
408 /*
409  * Cancellation behavior:
410  *   Thread may be canceled at start, but if the system call got some data,
411  *   the thread is not canceled.
412  */
413 static ssize_t
414 __thr_readv(int fd, const struct iovec *iov, int iovcnt)
415 {
416 	struct pthread *curthread;
417 	ssize_t ret;
418 
419 	curthread = _get_curthread();
420 	_thr_cancel_enter(curthread);
421 	ret = __sys_readv(fd, iov, iovcnt);
422 	_thr_cancel_leave(curthread, ret == -1);
423 	return (ret);
424 }
425 
426 /*
427  * Cancellation behavior:
428  *   Thread may be canceled at start, but if the system call got some data,
429  *   the thread is not canceled.
430  */
431 static ssize_t
432 __thr_recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from,
433     socklen_t *fl)
434 {
435 	struct pthread *curthread;
436 	ssize_t ret;
437 
438 	curthread = _get_curthread();
439 	_thr_cancel_enter(curthread);
440 	ret = __sys_recvfrom(s, b, l, f, from, fl);
441 	_thr_cancel_leave(curthread, ret == -1);
442 	return (ret);
443 }
444 
445 /*
446  * Cancellation behavior:
447  *   Thread may be canceled at start, but if the system call got some data,
448  *   the thread is not canceled.
449  */
450 static ssize_t
451 __thr_recvmsg(int s, struct msghdr *m, int f)
452 {
453 	struct pthread *curthread;
454 	ssize_t ret;
455 
456 	curthread = _get_curthread();
457 	_thr_cancel_enter(curthread);
458 	ret = __sys_recvmsg(s, m, f);
459 	_thr_cancel_leave(curthread, ret == -1);
460 	return (ret);
461 }
462 
463 /*
464  * Cancellation behavior:
465  *   Thread may be canceled at start, but if the system call returns something,
466  *   the thread is not canceled.
467  */
468 static int
469 __thr_select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
470 	struct timeval *timeout)
471 {
472 	struct pthread *curthread;
473 	int ret;
474 
475 	curthread = _get_curthread();
476 	_thr_cancel_enter(curthread);
477 	ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
478 	_thr_cancel_leave(curthread, ret == -1);
479 	return (ret);
480 }
481 
482 /*
483  * Cancellation behavior:
484  *   Thread may be canceled at start, but if the system call sent
485  *   data, the thread is not canceled.
486  */
487 static ssize_t
488 __thr_sendmsg(int s, const struct msghdr *m, int f)
489 {
490 	struct pthread *curthread;
491 	ssize_t ret;
492 
493 	curthread = _get_curthread();
494 	_thr_cancel_enter(curthread);
495 	ret = __sys_sendmsg(s, m, f);
496 	_thr_cancel_leave(curthread, ret <= 0);
497 	return (ret);
498 }
499 
500 /*
501  * Cancellation behavior:
502  *   Thread may be canceled at start, but if the system call sent some
503  *   data, the thread is not canceled.
504  */
505 static ssize_t
506 __thr_sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t,
507     socklen_t tl)
508 {
509 	struct pthread *curthread;
510 	ssize_t ret;
511 
512 	curthread = _get_curthread();
513 	_thr_cancel_enter(curthread);
514 	ret = __sys_sendto(s, m, l, f, t, tl);
515 	_thr_cancel_leave(curthread, ret <= 0);
516 	return (ret);
517 }
518 
519 static int
520 __thr_system(const char *string)
521 {
522 	struct pthread *curthread;
523 	int ret;
524 
525 	curthread = _get_curthread();
526 	_thr_cancel_enter(curthread);
527 	ret = __libc_system(string);
528 	_thr_cancel_leave(curthread, 1);
529 	return (ret);
530 }
531 
532 /*
533  * Cancellation behavior:
534  *   If thread is canceled, the system call is not completed,
535  *   this means not all bytes were drained.
536  */
537 static int
538 __thr_tcdrain(int fd)
539 {
540 	struct pthread *curthread;
541 	int ret;
542 
543 	curthread = _get_curthread();
544 	_thr_cancel_enter(curthread);
545 	ret = __libc_tcdrain(fd);
546 	_thr_cancel_leave(curthread, ret == -1);
547 	return (ret);
548 }
549 
550 /*
551  * Cancellation behavior:
552  *   Thread may be canceled at start, but if the system call returns
553  *   a child pid, the thread is not canceled.
554  */
555 static pid_t
556 __thr_wait4(pid_t pid, int *status, int options, struct rusage *rusage)
557 {
558 	struct pthread *curthread;
559 	pid_t ret;
560 
561 	curthread = _get_curthread();
562 	_thr_cancel_enter(curthread);
563 	ret = __sys_wait4(pid, status, options, rusage);
564 	_thr_cancel_leave(curthread, ret <= 0);
565 	return (ret);
566 }
567 
568 /*
569  * Cancellation behavior:
570  *   Thread may be canceled at start, but if the system call returns
571  *   a child pid, the thread is not canceled.
572  */
573 static pid_t
574 __thr_wait6(idtype_t idtype, id_t id, int *status, int options,
575     struct __wrusage *ru, siginfo_t *infop)
576 {
577 	struct pthread *curthread;
578 	pid_t ret;
579 
580 	curthread = _get_curthread();
581 	_thr_cancel_enter(curthread);
582 	ret = __sys_wait6(idtype, id, status, options, ru, infop);
583 	_thr_cancel_leave(curthread, ret <= 0);
584 	return (ret);
585 }
586 
587 /*
588  * Cancellation behavior:
589  *   Thread may be canceled at start, but if the thread wrote some data,
590  *   it is not canceled.
591  */
592 static ssize_t
593 __thr_write(int fd, const void *buf, size_t nbytes)
594 {
595 	struct pthread *curthread;
596 	ssize_t	ret;
597 
598 	curthread = _get_curthread();
599 	_thr_cancel_enter(curthread);
600 	ret = __sys_write(fd, buf, nbytes);
601 	_thr_cancel_leave(curthread, (ret <= 0));
602 	return (ret);
603 }
604 
605 /*
606  * Cancellation behavior:
607  *   Thread may be canceled at start, but if the thread wrote some data,
608  *   it is not canceled.
609  */
610 static ssize_t
611 __thr_writev(int fd, const struct iovec *iov, int iovcnt)
612 {
613 	struct pthread *curthread;
614 	ssize_t ret;
615 
616 	curthread = _get_curthread();
617 	_thr_cancel_enter(curthread);
618 	ret = __sys_writev(fd, iov, iovcnt);
619 	_thr_cancel_leave(curthread, (ret <= 0));
620 	return (ret);
621 }
622 
623 void
624 __thr_interpose_libc(void)
625 {
626 
627 	__set_error_selector(__error_threaded);
628 #define	SLOT(name)					\
629 	*(__libc_interposing_slot(INTERPOS_##name)) =	\
630 	    (interpos_func_t)__thr_##name;
631 	SLOT(accept);
632 	SLOT(accept4);
633 	SLOT(aio_suspend);
634 	SLOT(close);
635 	SLOT(connect);
636 	SLOT(fcntl);
637 	SLOT(fsync);
638 	SLOT(fork);
639 	SLOT(msync);
640 	SLOT(nanosleep);
641 	SLOT(openat);
642 	SLOT(poll);
643 	SLOT(pselect);
644 	SLOT(read);
645 	SLOT(readv);
646 	SLOT(recvfrom);
647 	SLOT(recvmsg);
648 	SLOT(select);
649 	SLOT(sendmsg);
650 	SLOT(sendto);
651 	SLOT(setcontext);
652 	SLOT(sigaction);
653 	SLOT(sigprocmask);
654 	SLOT(sigsuspend);
655 	SLOT(sigwait);
656 	SLOT(sigtimedwait);
657 	SLOT(sigwaitinfo);
658 	SLOT(swapcontext);
659 	SLOT(system);
660 	SLOT(tcdrain);
661 	SLOT(wait4);
662 	SLOT(write);
663 	SLOT(writev);
664 	SLOT(spinlock);
665 	SLOT(spinunlock);
666 	SLOT(kevent);
667 	SLOT(wait6);
668 	SLOT(ppoll);
669 	SLOT(map_stacks_exec);
670 	SLOT(fdatasync);
671 #undef SLOT
672 	*(__libc_interposing_slot(
673 	    INTERPOS__pthread_mutex_init_calloc_cb)) =
674 	    (interpos_func_t)_pthread_mutex_init_calloc_cb;
675 }
676