xref: /freebsd/lib/libthr/thread/thr_syscalls.c (revision d9f0ce31900a48d1a2bfc1c8c86f79d1e831451a)
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 /*
231  * Cancellation behavior:
232  *   Thread may be canceled after system call.
233  */
234 static int
235 __thr_msync(void *addr, size_t len, int flags)
236 {
237 	struct pthread *curthread;
238 	int ret;
239 
240 	curthread = _get_curthread();
241 	_thr_cancel_enter2(curthread, 0);
242 	ret = __sys_msync(addr, len, flags);
243 	_thr_cancel_leave(curthread, 1);
244 
245 	return (ret);
246 }
247 
248 static int
249 __thr_nanosleep(const struct timespec *time_to_sleep,
250     struct timespec *time_remaining)
251 {
252 	struct pthread *curthread;
253 	int ret;
254 
255 	curthread = _get_curthread();
256 	_thr_cancel_enter(curthread);
257 	ret = __sys_nanosleep(time_to_sleep, time_remaining);
258 	_thr_cancel_leave(curthread, 1);
259 
260 	return (ret);
261 }
262 
263 /*
264  * Cancellation behavior:
265  *   If the thread is canceled, file is not opened.
266  */
267 static int
268 __thr_openat(int fd, const char *path, int flags, ...)
269 {
270 	struct pthread *curthread;
271 	int mode, ret;
272 	va_list	ap;
273 
274 
275 	/* Check if the file is being created: */
276 	if ((flags & O_CREAT) != 0) {
277 		/* Get the creation mode: */
278 		va_start(ap, flags);
279 		mode = va_arg(ap, int);
280 		va_end(ap);
281 	} else {
282 		mode = 0;
283 	}
284 
285 	curthread = _get_curthread();
286 	_thr_cancel_enter(curthread);
287 	ret = __sys_openat(fd, path, flags, mode);
288 	_thr_cancel_leave(curthread, ret == -1);
289 
290 	return (ret);
291 }
292 
293 /*
294  * Cancellation behavior:
295  *   Thread may be canceled at start, but if the system call returns something,
296  *   the thread is not canceled.
297  */
298 static int
299 __thr_poll(struct pollfd *fds, unsigned int nfds, int timeout)
300 {
301 	struct pthread *curthread;
302 	int ret;
303 
304 	curthread = _get_curthread();
305 	_thr_cancel_enter(curthread);
306 	ret = __sys_poll(fds, nfds, timeout);
307 	_thr_cancel_leave(curthread, ret == -1);
308 
309 	return (ret);
310 }
311 
312 /*
313  * Cancellation behavior:
314  *   Thread may be canceled at start, but if the system call returns something,
315  *   the thread is not canceled.
316  */
317 static int
318 __thr_ppoll(struct pollfd pfd[], nfds_t nfds, const struct timespec *
319     timeout, const sigset_t *newsigmask)
320 {
321 	struct pthread *curthread;
322 	int ret;
323 
324 	curthread = _get_curthread();
325 	_thr_cancel_enter(curthread);
326 	ret = __sys_ppoll(pfd, nfds, timeout, newsigmask);
327 	_thr_cancel_leave(curthread, ret == -1);
328 
329 	return (ret);
330 }
331 
332 /*
333  * Cancellation behavior:
334  *   Thread may be canceled at start, but if the system call returns something,
335  *   the thread is not canceled.
336  */
337 static int
338 __thr_pselect(int count, fd_set *rfds, fd_set *wfds, fd_set *efds,
339 	const struct timespec *timo, const sigset_t *mask)
340 {
341 	struct pthread *curthread;
342 	int ret;
343 
344 	curthread = _get_curthread();
345 	_thr_cancel_enter(curthread);
346 	ret = __sys_pselect(count, rfds, wfds, efds, timo, mask);
347 	_thr_cancel_leave(curthread, ret == -1);
348 
349 	return (ret);
350 }
351 
352 static int
353 __thr_kevent(int kq, const struct kevent *changelist, int nchanges,
354     struct kevent *eventlist, int nevents, const struct timespec *timeout)
355 {
356 	struct pthread *curthread;
357 	int ret;
358 
359 	if (nevents == 0) {
360 		/*
361 		 * No blocking, do not make the call cancellable.
362 		 */
363 		return (__sys_kevent(kq, changelist, nchanges, eventlist,
364 		    nevents, timeout));
365 	}
366 	curthread = _get_curthread();
367 	_thr_cancel_enter(curthread);
368 	ret = __sys_kevent(kq, changelist, nchanges, eventlist, nevents,
369 	    timeout);
370 	_thr_cancel_leave(curthread, ret == -1 && nchanges == 0);
371 
372 	return (ret);
373 }
374 
375 /*
376  * Cancellation behavior:
377  *   Thread may be canceled at start, but if the system call got some data,
378  *   the thread is not canceled.
379  */
380 static ssize_t
381 __thr_read(int fd, void *buf, size_t nbytes)
382 {
383 	struct pthread *curthread;
384 	ssize_t	ret;
385 
386 	curthread = _get_curthread();
387 	_thr_cancel_enter(curthread);
388 	ret = __sys_read(fd, buf, nbytes);
389 	_thr_cancel_leave(curthread, ret == -1);
390 
391 	return (ret);
392 }
393 
394 /*
395  * Cancellation behavior:
396  *   Thread may be canceled at start, but if the system call got some data,
397  *   the thread is not canceled.
398  */
399 static ssize_t
400 __thr_readv(int fd, const struct iovec *iov, int iovcnt)
401 {
402 	struct pthread *curthread;
403 	ssize_t ret;
404 
405 	curthread = _get_curthread();
406 	_thr_cancel_enter(curthread);
407 	ret = __sys_readv(fd, iov, iovcnt);
408 	_thr_cancel_leave(curthread, ret == -1);
409 	return (ret);
410 }
411 
412 /*
413  * Cancellation behavior:
414  *   Thread may be canceled at start, but if the system call got some data,
415  *   the thread is not canceled.
416  */
417 static ssize_t
418 __thr_recvfrom(int s, void *b, size_t l, int f, struct sockaddr *from,
419     socklen_t *fl)
420 {
421 	struct pthread *curthread;
422 	ssize_t ret;
423 
424 	curthread = _get_curthread();
425 	_thr_cancel_enter(curthread);
426 	ret = __sys_recvfrom(s, b, l, f, from, fl);
427 	_thr_cancel_leave(curthread, ret == -1);
428 	return (ret);
429 }
430 
431 /*
432  * Cancellation behavior:
433  *   Thread may be canceled at start, but if the system call got some data,
434  *   the thread is not canceled.
435  */
436 static ssize_t
437 __thr_recvmsg(int s, struct msghdr *m, int f)
438 {
439 	struct pthread *curthread;
440 	ssize_t ret;
441 
442 	curthread = _get_curthread();
443 	_thr_cancel_enter(curthread);
444 	ret = __sys_recvmsg(s, m, f);
445 	_thr_cancel_leave(curthread, ret == -1);
446 	return (ret);
447 }
448 
449 /*
450  * Cancellation behavior:
451  *   Thread may be canceled at start, but if the system call returns something,
452  *   the thread is not canceled.
453  */
454 static int
455 __thr_select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
456 	struct timeval *timeout)
457 {
458 	struct pthread *curthread;
459 	int ret;
460 
461 	curthread = _get_curthread();
462 	_thr_cancel_enter(curthread);
463 	ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
464 	_thr_cancel_leave(curthread, ret == -1);
465 	return (ret);
466 }
467 
468 /*
469  * Cancellation behavior:
470  *   Thread may be canceled at start, but if the system call sent
471  *   data, the thread is not canceled.
472  */
473 static ssize_t
474 __thr_sendmsg(int s, const struct msghdr *m, int f)
475 {
476 	struct pthread *curthread;
477 	ssize_t ret;
478 
479 	curthread = _get_curthread();
480 	_thr_cancel_enter(curthread);
481 	ret = __sys_sendmsg(s, m, f);
482 	_thr_cancel_leave(curthread, ret <= 0);
483 	return (ret);
484 }
485 
486 /*
487  * Cancellation behavior:
488  *   Thread may be canceled at start, but if the system call sent some
489  *   data, the thread is not canceled.
490  */
491 static ssize_t
492 __thr_sendto(int s, const void *m, size_t l, int f, const struct sockaddr *t,
493     socklen_t tl)
494 {
495 	struct pthread *curthread;
496 	ssize_t ret;
497 
498 	curthread = _get_curthread();
499 	_thr_cancel_enter(curthread);
500 	ret = __sys_sendto(s, m, l, f, t, tl);
501 	_thr_cancel_leave(curthread, ret <= 0);
502 	return (ret);
503 }
504 
505 static int
506 __thr_system(const char *string)
507 {
508 	struct pthread *curthread;
509 	int ret;
510 
511 	curthread = _get_curthread();
512 	_thr_cancel_enter(curthread);
513 	ret = __libc_system(string);
514 	_thr_cancel_leave(curthread, 1);
515 	return (ret);
516 }
517 
518 /*
519  * Cancellation behavior:
520  *   If thread is canceled, the system call is not completed,
521  *   this means not all bytes were drained.
522  */
523 static int
524 __thr_tcdrain(int fd)
525 {
526 	struct pthread *curthread;
527 	int ret;
528 
529 	curthread = _get_curthread();
530 	_thr_cancel_enter(curthread);
531 	ret = __libc_tcdrain(fd);
532 	_thr_cancel_leave(curthread, ret == -1);
533 	return (ret);
534 }
535 
536 /*
537  * Cancellation behavior:
538  *   Thread may be canceled at start, but if the system call returns
539  *   a child pid, the thread is not canceled.
540  */
541 static pid_t
542 __thr_wait4(pid_t pid, int *status, int options, struct rusage *rusage)
543 {
544 	struct pthread *curthread;
545 	pid_t ret;
546 
547 	curthread = _get_curthread();
548 	_thr_cancel_enter(curthread);
549 	ret = __sys_wait4(pid, status, options, rusage);
550 	_thr_cancel_leave(curthread, ret <= 0);
551 	return (ret);
552 }
553 
554 /*
555  * Cancellation behavior:
556  *   Thread may be canceled at start, but if the system call returns
557  *   a child pid, the thread is not canceled.
558  */
559 static pid_t
560 __thr_wait6(idtype_t idtype, id_t id, int *status, int options,
561     struct __wrusage *ru, siginfo_t *infop)
562 {
563 	struct pthread *curthread;
564 	pid_t ret;
565 
566 	curthread = _get_curthread();
567 	_thr_cancel_enter(curthread);
568 	ret = __sys_wait6(idtype, id, status, options, ru, infop);
569 	_thr_cancel_leave(curthread, ret <= 0);
570 	return (ret);
571 }
572 
573 /*
574  * Cancellation behavior:
575  *   Thread may be canceled at start, but if the thread wrote some data,
576  *   it is not canceled.
577  */
578 static ssize_t
579 __thr_write(int fd, const void *buf, size_t nbytes)
580 {
581 	struct pthread *curthread;
582 	ssize_t	ret;
583 
584 	curthread = _get_curthread();
585 	_thr_cancel_enter(curthread);
586 	ret = __sys_write(fd, buf, nbytes);
587 	_thr_cancel_leave(curthread, (ret <= 0));
588 	return (ret);
589 }
590 
591 /*
592  * Cancellation behavior:
593  *   Thread may be canceled at start, but if the thread wrote some data,
594  *   it is not canceled.
595  */
596 static ssize_t
597 __thr_writev(int fd, const struct iovec *iov, int iovcnt)
598 {
599 	struct pthread *curthread;
600 	ssize_t ret;
601 
602 	curthread = _get_curthread();
603 	_thr_cancel_enter(curthread);
604 	ret = __sys_writev(fd, iov, iovcnt);
605 	_thr_cancel_leave(curthread, (ret <= 0));
606 	return (ret);
607 }
608 
609 void
610 __thr_interpose_libc(void)
611 {
612 
613 	__set_error_selector(__error_threaded);
614 #define	SLOT(name)					\
615 	*(__libc_interposing_slot(INTERPOS_##name)) =	\
616 	    (interpos_func_t)__thr_##name;
617 	SLOT(accept);
618 	SLOT(accept4);
619 	SLOT(aio_suspend);
620 	SLOT(close);
621 	SLOT(connect);
622 	SLOT(fcntl);
623 	SLOT(fsync);
624 	SLOT(fork);
625 	SLOT(msync);
626 	SLOT(nanosleep);
627 	SLOT(openat);
628 	SLOT(poll);
629 	SLOT(pselect);
630 	SLOT(read);
631 	SLOT(readv);
632 	SLOT(recvfrom);
633 	SLOT(recvmsg);
634 	SLOT(select);
635 	SLOT(sendmsg);
636 	SLOT(sendto);
637 	SLOT(setcontext);
638 	SLOT(sigaction);
639 	SLOT(sigprocmask);
640 	SLOT(sigsuspend);
641 	SLOT(sigwait);
642 	SLOT(sigtimedwait);
643 	SLOT(sigwaitinfo);
644 	SLOT(swapcontext);
645 	SLOT(system);
646 	SLOT(tcdrain);
647 	SLOT(wait4);
648 	SLOT(write);
649 	SLOT(writev);
650 	SLOT(spinlock);
651 	SLOT(spinunlock);
652 	SLOT(kevent);
653 	SLOT(wait6);
654 	SLOT(ppoll);
655 	SLOT(map_stacks_exec);
656 #undef SLOT
657 	*(__libc_interposing_slot(
658 	    INTERPOS__pthread_mutex_init_calloc_cb)) =
659 	    (interpos_func_t)_pthread_mutex_init_calloc_cb;
660 }
661