xref: /freebsd/lib/libthr/thread/thr_syscalls.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
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  * SPDX-License-Identifier: BSD-3-Clause
38  *
39  * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
40  * All rights reserved.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. Neither the name of the author nor the names of any co-contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  */
67 
68 #include "namespace.h"
69 #include <sys/types.h>
70 #include <sys/mman.h>
71 #include <sys/param.h>
72 #include <sys/select.h>
73 #include <sys/signalvar.h>
74 #include <sys/socket.h>
75 #include <sys/stat.h>
76 #include <sys/time.h>
77 #include <sys/uio.h>
78 #include <sys/wait.h>
79 #include <aio.h>
80 #include <dirent.h>
81 #include <errno.h>
82 #include <fcntl.h>
83 #include <poll.h>
84 #include <signal.h>
85 #include <stdarg.h>
86 #include <stdio.h>
87 #include <stdlib.h>
88 #include <string.h>
89 #include <termios.h>
90 #include <unistd.h>
91 #include <pthread.h>
92 #include "un-namespace.h"
93 
94 #include "libc_private.h"
95 #include "thr_private.h"
96 
97 static int
98 __thr_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
99 {
100 	struct pthread *curthread;
101 	int ret;
102 
103 	curthread = _get_curthread();
104 	_thr_cancel_enter(curthread);
105 	ret = __sys_accept(s, addr, addrlen);
106 	_thr_cancel_leave(curthread, ret == -1);
107 
108  	return (ret);
109 }
110 
111 /*
112  * Cancellation behavior:
113  *   If thread is canceled, no socket is created.
114  */
115 static int
116 __thr_accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags)
117 {
118 	struct pthread *curthread;
119 	int ret;
120 
121 	curthread = _get_curthread();
122 	_thr_cancel_enter(curthread);
123 	ret = __sys_accept4(s, addr, addrlen, flags);
124 	_thr_cancel_leave(curthread, ret == -1);
125 
126  	return (ret);
127 }
128 
129 static int
130 __thr_aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct
131     timespec *timeout)
132 {
133 	struct pthread *curthread;
134 	int ret;
135 
136 	curthread = _get_curthread();
137 	_thr_cancel_enter(curthread);
138 	ret = __sys_aio_suspend(iocbs, niocb, timeout);
139 	_thr_cancel_leave(curthread, 1);
140 
141 	return (ret);
142 }
143 
144 /*
145  * Cancellation behavior:
146  *   According to manual of close(), the file descriptor is always deleted.
147  *   Here, thread is only canceled after the system call, so the file
148  *   descriptor is always deleted despite whether the thread is canceled
149  *   or not.
150  */
151 static int
152 __thr_close(int fd)
153 {
154 	struct pthread *curthread;
155 	int ret;
156 
157 	curthread = _get_curthread();
158 	_thr_cancel_enter2(curthread, 0);
159 	ret = __sys_close(fd);
160 	_thr_cancel_leave(curthread, 1);
161 
162 	return (ret);
163 }
164 
165 /*
166  * Cancellation behavior:
167  *   If the thread is canceled, connection is not made.
168  */
169 static int
170 __thr_connect(int fd, const struct sockaddr *name, socklen_t namelen)
171 {
172 	struct pthread *curthread;
173 	int ret;
174 
175 	curthread = _get_curthread();
176 	_thr_cancel_enter(curthread);
177 	ret = __sys_connect(fd, name, namelen);
178 	_thr_cancel_leave(curthread, ret == -1);
179 
180  	return (ret);
181 }
182 
183 /*
184  * Cancellation behavior:
185  *   According to specification, only F_SETLKW is a cancellation point.
186  *   Thread is only canceled at start, or canceled if the system call
187  *   is failure, this means the function does not generate side effect
188  *   if it is canceled.
189  */
190 static int
191 __thr_fcntl(int fd, int cmd, __intptr_t arg)
192 {
193 	struct pthread *curthread;
194 	int ret;
195 
196 	curthread = _get_curthread();
197 	if (cmd == F_OSETLKW || cmd == F_SETLKW) {
198 		_thr_cancel_enter(curthread);
199 		ret = __sys_fcntl(fd, cmd, arg);
200 		_thr_cancel_leave(curthread, ret == -1);
201 	} else {
202 		ret = __sys_fcntl(fd, cmd, arg);
203 	}
204 
205 	return (ret);
206 }
207 
208 /*
209  * Cancellation behavior:
210  *   Thread may be canceled after system call.
211  */
212 static int
213 __thr_fsync(int fd)
214 {
215 	struct pthread *curthread;
216 	int ret;
217 
218 	curthread = _get_curthread();
219 	_thr_cancel_enter2(curthread, 0);
220 	ret = __sys_fsync(fd);
221 	_thr_cancel_leave(curthread, 1);
222 
223 	return (ret);
224 }
225 
226 static int
227 __thr_fdatasync(int fd)
228 {
229 	struct pthread *curthread;
230 	int ret;
231 
232 	curthread = _get_curthread();
233 	_thr_cancel_enter2(curthread, 0);
234 	ret = __sys_fdatasync(fd);
235 	_thr_cancel_leave(curthread, 1);
236 
237 	return (ret);
238 }
239 
240 /*
241  * Cancellation behavior:
242  *   Thread may be canceled after system call.
243  */
244 static int
245 __thr_msync(void *addr, size_t len, int flags)
246 {
247 	struct pthread *curthread;
248 	int ret;
249 
250 	curthread = _get_curthread();
251 	_thr_cancel_enter2(curthread, 0);
252 	ret = __sys_msync(addr, len, flags);
253 	_thr_cancel_leave(curthread, 1);
254 
255 	return (ret);
256 }
257 
258 static int
259 __thr_clock_nanosleep(clockid_t clock_id, int flags,
260     const struct timespec *time_to_sleep, struct timespec *time_remaining)
261 {
262 	struct pthread *curthread;
263 	int ret;
264 
265 	curthread = _get_curthread();
266 	_thr_cancel_enter(curthread);
267 	ret = __sys_clock_nanosleep(clock_id, flags, time_to_sleep,
268 	    time_remaining);
269 	_thr_cancel_leave(curthread, 1);
270 
271 	return (ret);
272 }
273 
274 static int
275 __thr_nanosleep(const struct timespec *time_to_sleep,
276     struct timespec *time_remaining)
277 {
278 	struct pthread *curthread;
279 	int ret;
280 
281 	curthread = _get_curthread();
282 	_thr_cancel_enter(curthread);
283 	ret = __sys_nanosleep(time_to_sleep, time_remaining);
284 	_thr_cancel_leave(curthread, 1);
285 
286 	return (ret);
287 }
288 
289 /*
290  * Cancellation behavior:
291  *   If the thread is canceled, file is not opened.
292  */
293 static int
294 __thr_openat(int fd, const char *path, int flags, int mode)
295 {
296 	struct pthread *curthread;
297 	int ret;
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(system);
632 	SLOT(tcdrain);
633 	SLOT(spinlock);
634 	SLOT(spinunlock);
635 	SLOT(map_stacks_exec);
636 #undef SLOT
637 
638 #define	SLOT(name)					\
639 	*(__libc_interposing_slot(INTERPOS_##name)) =	\
640 	    (interpos_func_t)__thr_##name;
641 	SLOT(accept);
642 	SLOT(accept4);
643 	SLOT(aio_suspend);
644 	SLOT(close);
645 	SLOT(connect);
646 	SLOT(fcntl);
647 	SLOT(fsync);
648 	SLOT(fork);
649 	SLOT(msync);
650 	SLOT(nanosleep);
651 	SLOT(openat);
652 	SLOT(poll);
653 	SLOT(pselect);
654 	SLOT(read);
655 	SLOT(readv);
656 	SLOT(recvfrom);
657 	SLOT(recvmsg);
658 	SLOT(select);
659 	SLOT(sendmsg);
660 	SLOT(sendto);
661 	SLOT(setcontext);
662 	SLOT(sigaction);
663 	SLOT(sigprocmask);
664 	SLOT(sigsuspend);
665 	SLOT(sigwait);
666 	SLOT(sigtimedwait);
667 	SLOT(sigwaitinfo);
668 	SLOT(swapcontext);
669 	SLOT(wait4);
670 	SLOT(write);
671 	SLOT(writev);
672 	SLOT(kevent);
673 	SLOT(wait6);
674 	SLOT(ppoll);
675 	SLOT(fdatasync);
676 	SLOT(clock_nanosleep);
677 	SLOT(pdfork);
678 #undef SLOT
679 	*(__libc_interposing_slot(
680 	    INTERPOS__pthread_mutex_init_calloc_cb)) =
681 	    (interpos_func_t)_pthread_mutex_init_calloc_cb;
682 }
683