xref: /freebsd/lib/libthr/thread/thr_syscalls.c (revision faf25f48d601ae39f5752602f3020e2e92605625)
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 <sys/cdefs.h>
69 __FBSDID("$FreeBSD$");
70 
71 #include "namespace.h"
72 #include <sys/types.h>
73 #include <sys/mman.h>
74 #include <sys/param.h>
75 #include <sys/select.h>
76 #include <sys/signalvar.h>
77 #include <sys/socket.h>
78 #include <sys/stat.h>
79 #include <sys/time.h>
80 #include <sys/uio.h>
81 #include <sys/wait.h>
82 #include <aio.h>
83 #include <dirent.h>
84 #include <errno.h>
85 #include <fcntl.h>
86 #include <poll.h>
87 #include <signal.h>
88 #include <stdarg.h>
89 #include <stdio.h>
90 #include <stdlib.h>
91 #include <string.h>
92 #include <termios.h>
93 #include <unistd.h>
94 #include <pthread.h>
95 #include "un-namespace.h"
96 
97 #include "libc_private.h"
98 #include "thr_private.h"
99 
100 static int
101 __thr_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
102 {
103 	struct pthread *curthread;
104 	int ret;
105 
106 	curthread = _get_curthread();
107 	_thr_cancel_enter(curthread);
108 	ret = __sys_accept(s, addr, addrlen);
109 	_thr_cancel_leave(curthread, ret == -1);
110 
111  	return (ret);
112 }
113 
114 /*
115  * Cancellation behavior:
116  *   If thread is canceled, no socket is created.
117  */
118 static int
119 __thr_accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags)
120 {
121 	struct pthread *curthread;
122 	int ret;
123 
124 	curthread = _get_curthread();
125 	_thr_cancel_enter(curthread);
126 	ret = __sys_accept4(s, addr, addrlen, flags);
127 	_thr_cancel_leave(curthread, ret == -1);
128 
129  	return (ret);
130 }
131 
132 static int
133 __thr_aio_suspend(const struct aiocb * const iocbs[], int niocb, const struct
134     timespec *timeout)
135 {
136 	struct pthread *curthread;
137 	int ret;
138 
139 	curthread = _get_curthread();
140 	_thr_cancel_enter(curthread);
141 	ret = __sys_aio_suspend(iocbs, niocb, timeout);
142 	_thr_cancel_leave(curthread, 1);
143 
144 	return (ret);
145 }
146 
147 /*
148  * Cancellation behavior:
149  *   According to manual of close(), the file descriptor is always deleted.
150  *   Here, thread is only canceled after the system call, so the file
151  *   descriptor is always deleted despite whether the thread is canceled
152  *   or not.
153  */
154 static int
155 __thr_close(int fd)
156 {
157 	struct pthread *curthread;
158 	int ret;
159 
160 	curthread = _get_curthread();
161 	_thr_cancel_enter2(curthread, 0);
162 	ret = __sys_close(fd);
163 	_thr_cancel_leave(curthread, 1);
164 
165 	return (ret);
166 }
167 
168 /*
169  * Cancellation behavior:
170  *   If the thread is canceled, connection is not made.
171  */
172 static int
173 __thr_connect(int fd, const struct sockaddr *name, socklen_t namelen)
174 {
175 	struct pthread *curthread;
176 	int ret;
177 
178 	curthread = _get_curthread();
179 	_thr_cancel_enter(curthread);
180 	ret = __sys_connect(fd, name, namelen);
181 	_thr_cancel_leave(curthread, ret == -1);
182 
183  	return (ret);
184 }
185 
186 /*
187  * Cancellation behavior:
188  *   According to specification, only F_SETLKW is a cancellation point.
189  *   Thread is only canceled at start, or canceled if the system call
190  *   is failure, this means the function does not generate side effect
191  *   if it is canceled.
192  */
193 static int
194 __thr_fcntl(int fd, int cmd, ...)
195 {
196 	struct pthread *curthread;
197 	int ret;
198 	va_list	ap;
199 
200 	curthread = _get_curthread();
201 	va_start(ap, cmd);
202 	if (cmd == F_OSETLKW || cmd == F_SETLKW) {
203 		_thr_cancel_enter(curthread);
204 		ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
205 		_thr_cancel_leave(curthread, ret == -1);
206 	} else {
207 		ret = __sys_fcntl(fd, cmd, va_arg(ap, void *));
208 	}
209 	va_end(ap);
210 
211 	return (ret);
212 }
213 
214 /*
215  * Cancellation behavior:
216  *   Thread may be canceled after system call.
217  */
218 static int
219 __thr_fsync(int fd)
220 {
221 	struct pthread *curthread;
222 	int ret;
223 
224 	curthread = _get_curthread();
225 	_thr_cancel_enter2(curthread, 0);
226 	ret = __sys_fsync(fd);
227 	_thr_cancel_leave(curthread, 1);
228 
229 	return (ret);
230 }
231 
232 static int
233 __thr_fdatasync(int fd)
234 {
235 	struct pthread *curthread;
236 	int ret;
237 
238 	curthread = _get_curthread();
239 	_thr_cancel_enter2(curthread, 0);
240 	ret = __sys_fdatasync(fd);
241 	_thr_cancel_leave(curthread, 1);
242 
243 	return (ret);
244 }
245 
246 /*
247  * Cancellation behavior:
248  *   Thread may be canceled after system call.
249  */
250 static int
251 __thr_msync(void *addr, size_t len, int flags)
252 {
253 	struct pthread *curthread;
254 	int ret;
255 
256 	curthread = _get_curthread();
257 	_thr_cancel_enter2(curthread, 0);
258 	ret = __sys_msync(addr, len, flags);
259 	_thr_cancel_leave(curthread, 1);
260 
261 	return (ret);
262 }
263 
264 static int
265 __thr_clock_nanosleep(clockid_t clock_id, int flags,
266     const struct timespec *time_to_sleep, struct timespec *time_remaining)
267 {
268 	struct pthread *curthread;
269 	int ret;
270 
271 	curthread = _get_curthread();
272 	_thr_cancel_enter(curthread);
273 	ret = __sys_clock_nanosleep(clock_id, flags, time_to_sleep,
274 	    time_remaining);
275 	_thr_cancel_leave(curthread, 1);
276 
277 	return (ret);
278 }
279 
280 static int
281 __thr_nanosleep(const struct timespec *time_to_sleep,
282     struct timespec *time_remaining)
283 {
284 	struct pthread *curthread;
285 	int ret;
286 
287 	curthread = _get_curthread();
288 	_thr_cancel_enter(curthread);
289 	ret = __sys_nanosleep(time_to_sleep, time_remaining);
290 	_thr_cancel_leave(curthread, 1);
291 
292 	return (ret);
293 }
294 
295 /*
296  * Cancellation behavior:
297  *   If the thread is canceled, file is not opened.
298  */
299 static int
300 __thr_openat(int fd, const char *path, int flags, ...)
301 {
302 	struct pthread *curthread;
303 	int mode, ret;
304 	va_list	ap;
305 
306 
307 	/* Check if the file is being created: */
308 	if ((flags & O_CREAT) != 0) {
309 		/* Get the creation mode: */
310 		va_start(ap, flags);
311 		mode = va_arg(ap, int);
312 		va_end(ap);
313 	} else {
314 		mode = 0;
315 	}
316 
317 	curthread = _get_curthread();
318 	_thr_cancel_enter(curthread);
319 	ret = __sys_openat(fd, path, flags, mode);
320 	_thr_cancel_leave(curthread, ret == -1);
321 
322 	return (ret);
323 }
324 
325 /*
326  * Cancellation behavior:
327  *   Thread may be canceled at start, but if the system call returns something,
328  *   the thread is not canceled.
329  */
330 static int
331 __thr_poll(struct pollfd *fds, unsigned int nfds, int timeout)
332 {
333 	struct pthread *curthread;
334 	int ret;
335 
336 	curthread = _get_curthread();
337 	_thr_cancel_enter(curthread);
338 	ret = __sys_poll(fds, nfds, timeout);
339 	_thr_cancel_leave(curthread, ret == -1);
340 
341 	return (ret);
342 }
343 
344 /*
345  * Cancellation behavior:
346  *   Thread may be canceled at start, but if the system call returns something,
347  *   the thread is not canceled.
348  */
349 static int
350 __thr_ppoll(struct pollfd pfd[], nfds_t nfds, const struct timespec *
351     timeout, const sigset_t *newsigmask)
352 {
353 	struct pthread *curthread;
354 	int ret;
355 
356 	curthread = _get_curthread();
357 	_thr_cancel_enter(curthread);
358 	ret = __sys_ppoll(pfd, nfds, timeout, newsigmask);
359 	_thr_cancel_leave(curthread, ret == -1);
360 
361 	return (ret);
362 }
363 
364 /*
365  * Cancellation behavior:
366  *   Thread may be canceled at start, but if the system call returns something,
367  *   the thread is not canceled.
368  */
369 static int
370 __thr_pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
371 	const struct timespec *timo, const sigset_t *mask)
372 {
373 	struct pthread *curthread;
374 	int ret;
375 
376 	curthread = _get_curthread();
377 	_thr_cancel_enter(curthread);
378 	ret = __sys_pselect(count, rfds, wfds, efds, timo, mask);
379 	_thr_cancel_leave(curthread, ret == -1);
380 
381 	return (ret);
382 }
383 
384 static int
385 __thr_kevent(int kq, const struct kevent *changelist, int nchanges,
386     struct kevent *eventlist, int nevents, const struct timespec *timeout)
387 {
388 	struct pthread *curthread;
389 	int ret;
390 
391 	if (nevents == 0) {
392 		/*
393 		 * No blocking, do not make the call cancellable.
394 		 */
395 		return (__sys_kevent(kq, changelist, nchanges, eventlist,
396 		    nevents, timeout));
397 	}
398 	curthread = _get_curthread();
399 	_thr_cancel_enter(curthread);
400 	ret = __sys_kevent(kq, changelist, nchanges, eventlist, nevents,
401 	    timeout);
402 	_thr_cancel_leave(curthread, ret == -1 && nchanges == 0);
403 
404 	return (ret);
405 }
406 
407 /*
408  * Cancellation behavior:
409  *   Thread may be canceled at start, but if the system call got some data,
410  *   the thread is not canceled.
411  */
412 static ssize_t
413 __thr_read(int fd, void *buf, size_t nbytes)
414 {
415 	struct pthread *curthread;
416 	ssize_t	ret;
417 
418 	curthread = _get_curthread();
419 	_thr_cancel_enter(curthread);
420 	ret = __sys_read(fd, buf, nbytes);
421 	_thr_cancel_leave(curthread, ret == -1);
422 
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_readv(int fd, const struct iovec *iov, int iovcnt)
433 {
434 	struct pthread *curthread;
435 	ssize_t ret;
436 
437 	curthread = _get_curthread();
438 	_thr_cancel_enter(curthread);
439 	ret = __sys_readv(fd, iov, iovcnt);
440 	_thr_cancel_leave(curthread, ret == -1);
441 	return (ret);
442 }
443 
444 /*
445  * Cancellation behavior:
446  *   Thread may be canceled at start, but if the system call got some data,
447  *   the thread is not canceled.
448  */
449 static ssize_t
450 __thr_recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from,
451     socklen_t *fl)
452 {
453 	struct pthread *curthread;
454 	ssize_t ret;
455 
456 	curthread = _get_curthread();
457 	_thr_cancel_enter(curthread);
458 	ret = __sys_recvfrom(s, b, l, f, from, fl);
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 got some data,
466  *   the thread is not canceled.
467  */
468 static ssize_t
469 __thr_recvmsg(int s, struct msghdr *m, int f)
470 {
471 	struct pthread *curthread;
472 	ssize_t ret;
473 
474 	curthread = _get_curthread();
475 	_thr_cancel_enter(curthread);
476 	ret = __sys_recvmsg(s, m, f);
477 	_thr_cancel_leave(curthread, ret == -1);
478 	return (ret);
479 }
480 
481 /*
482  * Cancellation behavior:
483  *   Thread may be canceled at start, but if the system call returns something,
484  *   the thread is not canceled.
485  */
486 static int
487 __thr_select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
488 	struct timeval *timeout)
489 {
490 	struct pthread *curthread;
491 	int ret;
492 
493 	curthread = _get_curthread();
494 	_thr_cancel_enter(curthread);
495 	ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
496 	_thr_cancel_leave(curthread, ret == -1);
497 	return (ret);
498 }
499 
500 /*
501  * Cancellation behavior:
502  *   Thread may be canceled at start, but if the system call sent
503  *   data, the thread is not canceled.
504  */
505 static ssize_t
506 __thr_sendmsg(int s, const struct msghdr *m, int f)
507 {
508 	struct pthread *curthread;
509 	ssize_t ret;
510 
511 	curthread = _get_curthread();
512 	_thr_cancel_enter(curthread);
513 	ret = __sys_sendmsg(s, m, f);
514 	_thr_cancel_leave(curthread, ret <= 0);
515 	return (ret);
516 }
517 
518 /*
519  * Cancellation behavior:
520  *   Thread may be canceled at start, but if the system call sent some
521  *   data, the thread is not canceled.
522  */
523 static ssize_t
524 __thr_sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t,
525     socklen_t tl)
526 {
527 	struct pthread *curthread;
528 	ssize_t ret;
529 
530 	curthread = _get_curthread();
531 	_thr_cancel_enter(curthread);
532 	ret = __sys_sendto(s, m, l, f, t, tl);
533 	_thr_cancel_leave(curthread, ret <= 0);
534 	return (ret);
535 }
536 
537 static int
538 __thr_system(const char *string)
539 {
540 	struct pthread *curthread;
541 	int ret;
542 
543 	curthread = _get_curthread();
544 	_thr_cancel_enter(curthread);
545 	ret = __libc_system(string);
546 	_thr_cancel_leave(curthread, 1);
547 	return (ret);
548 }
549 
550 /*
551  * Cancellation behavior:
552  *   If thread is canceled, the system call is not completed,
553  *   this means not all bytes were drained.
554  */
555 static int
556 __thr_tcdrain(int fd)
557 {
558 	struct pthread *curthread;
559 	int ret;
560 
561 	curthread = _get_curthread();
562 	_thr_cancel_enter(curthread);
563 	ret = __libc_tcdrain(fd);
564 	_thr_cancel_leave(curthread, ret == -1);
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_wait4(pid_t pid, int *status, int options, struct rusage *rusage)
575 {
576 	struct pthread *curthread;
577 	pid_t ret;
578 
579 	curthread = _get_curthread();
580 	_thr_cancel_enter(curthread);
581 	ret = __sys_wait4(pid, status, options, rusage);
582 	_thr_cancel_leave(curthread, ret <= 0);
583 	return (ret);
584 }
585 
586 /*
587  * Cancellation behavior:
588  *   Thread may be canceled at start, but if the system call returns
589  *   a child pid, the thread is not canceled.
590  */
591 static pid_t
592 __thr_wait6(idtype_t idtype, id_t id, int *status, int options,
593     struct __wrusage *ru, siginfo_t *infop)
594 {
595 	struct pthread *curthread;
596 	pid_t ret;
597 
598 	curthread = _get_curthread();
599 	_thr_cancel_enter(curthread);
600 	ret = __sys_wait6(idtype, id, status, options, ru, infop);
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_write(int fd, const void *buf, size_t nbytes)
612 {
613 	struct pthread *curthread;
614 	ssize_t	ret;
615 
616 	curthread = _get_curthread();
617 	_thr_cancel_enter(curthread);
618 	ret = __sys_write(fd, buf, nbytes);
619 	_thr_cancel_leave(curthread, (ret <= 0));
620 	return (ret);
621 }
622 
623 /*
624  * Cancellation behavior:
625  *   Thread may be canceled at start, but if the thread wrote some data,
626  *   it is not canceled.
627  */
628 static ssize_t
629 __thr_writev(int fd, const struct iovec *iov, int iovcnt)
630 {
631 	struct pthread *curthread;
632 	ssize_t ret;
633 
634 	curthread = _get_curthread();
635 	_thr_cancel_enter(curthread);
636 	ret = __sys_writev(fd, iov, iovcnt);
637 	_thr_cancel_leave(curthread, (ret <= 0));
638 	return (ret);
639 }
640 
641 void
642 __thr_interpose_libc(void)
643 {
644 
645 	__set_error_selector(__error_threaded);
646 #define	SLOT(name)					\
647 	*(__libc_interposing_slot(INTERPOS_##name)) =	\
648 	    (interpos_func_t)__thr_##name;
649 	SLOT(accept);
650 	SLOT(accept4);
651 	SLOT(aio_suspend);
652 	SLOT(close);
653 	SLOT(connect);
654 	SLOT(fcntl);
655 	SLOT(fsync);
656 	SLOT(fork);
657 	SLOT(msync);
658 	SLOT(nanosleep);
659 	SLOT(openat);
660 	SLOT(poll);
661 	SLOT(pselect);
662 	SLOT(read);
663 	SLOT(readv);
664 	SLOT(recvfrom);
665 	SLOT(recvmsg);
666 	SLOT(select);
667 	SLOT(sendmsg);
668 	SLOT(sendto);
669 	SLOT(setcontext);
670 	SLOT(sigaction);
671 	SLOT(sigprocmask);
672 	SLOT(sigsuspend);
673 	SLOT(sigwait);
674 	SLOT(sigtimedwait);
675 	SLOT(sigwaitinfo);
676 	SLOT(swapcontext);
677 	SLOT(system);
678 	SLOT(tcdrain);
679 	SLOT(wait4);
680 	SLOT(write);
681 	SLOT(writev);
682 	SLOT(spinlock);
683 	SLOT(spinunlock);
684 	SLOT(kevent);
685 	SLOT(wait6);
686 	SLOT(ppoll);
687 	SLOT(map_stacks_exec);
688 	SLOT(fdatasync);
689 	SLOT(clock_nanosleep);
690 	SLOT(pdfork);
691 #undef SLOT
692 	*(__libc_interposing_slot(
693 	    INTERPOS__pthread_mutex_init_calloc_cb)) =
694 	    (interpos_func_t)_pthread_mutex_init_calloc_cb;
695 }
696