| 1 | /* Copyright (C) 2002-2021 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <signal.h> |
| 21 | #include <stdlib.h> |
| 22 | #include "pthreadP.h" |
| 23 | #include <atomic.h> |
| 24 | #include <sysdep.h> |
| 25 | #include <unistd.h> |
| 26 | #include <unwind-link.h> |
| 27 | #include <stdio.h> |
| 28 | #include <gnu/lib-names.h> |
| 29 | #include <sys/single_threaded.h> |
| 30 | |
| 31 | /* For asynchronous cancellation we use a signal. */ |
| 32 | static void |
| 33 | sigcancel_handler (int sig, siginfo_t *si, void *ctx) |
| 34 | { |
| 35 | /* Safety check. It would be possible to call this function for |
| 36 | other signals and send a signal from another process. This is not |
| 37 | correct and might even be a security problem. Try to catch as |
| 38 | many incorrect invocations as possible. */ |
| 39 | if (sig != SIGCANCEL |
| 40 | || si->si_pid != __getpid() |
| 41 | || si->si_code != SI_TKILL) |
| 42 | return; |
| 43 | |
| 44 | struct pthread *self = THREAD_SELF; |
| 45 | |
| 46 | int ch = atomic_load_relaxed (&self->cancelhandling); |
| 47 | /* Cancelation not enabled, not cancelled, or already exitting. */ |
| 48 | if (self->cancelstate == PTHREAD_CANCEL_DISABLE |
| 49 | || (ch & CANCELED_BITMASK) == 0 |
| 50 | || (ch & EXITING_BITMASK) != 0) |
| 51 | return; |
| 52 | |
| 53 | /* Set the return value. */ |
| 54 | THREAD_SETMEM (self, result, PTHREAD_CANCELED); |
| 55 | /* Make sure asynchronous cancellation is still enabled. */ |
| 56 | if (self->canceltype == PTHREAD_CANCEL_ASYNCHRONOUS) |
| 57 | __do_cancel (); |
| 58 | } |
| 59 | |
| 60 | int |
| 61 | __pthread_cancel (pthread_t th) |
| 62 | { |
| 63 | volatile struct pthread *pd = (volatile struct pthread *) th; |
| 64 | |
| 65 | /* Make sure the descriptor is valid. */ |
| 66 | if (INVALID_TD_P (pd)) |
| 67 | /* Not a valid thread handle. */ |
| 68 | return ESRCH; |
| 69 | |
| 70 | static int init_sigcancel = 0; |
| 71 | if (atomic_load_relaxed (&init_sigcancel) == 0) |
| 72 | { |
| 73 | struct sigaction sa; |
| 74 | sa.sa_sigaction = sigcancel_handler; |
| 75 | /* The signal handle should be non-interruptible to avoid the risk of |
| 76 | spurious EINTR caused by SIGCANCEL sent to process or if |
| 77 | pthread_cancel() is called while cancellation is disabled in the |
| 78 | target thread. */ |
| 79 | sa.sa_flags = SA_SIGINFO | SA_RESTART; |
| 80 | __sigemptyset (&sa.sa_mask); |
| 81 | __libc_sigaction (SIGCANCEL, &sa, NULL); |
| 82 | atomic_store_relaxed (&init_sigcancel, 1); |
| 83 | } |
| 84 | |
| 85 | #ifdef SHARED |
| 86 | /* Trigger an error if libgcc_s cannot be loaded. */ |
| 87 | { |
| 88 | struct unwind_link *unwind_link = __libc_unwind_link_get (); |
| 89 | if (unwind_link == NULL) |
| 90 | __libc_fatal (LIBGCC_S_SO |
| 91 | " must be installed for pthread_cancel to work\n" ); |
| 92 | } |
| 93 | #endif |
| 94 | |
| 95 | int oldch = atomic_fetch_or_acquire (&pd->cancelhandling, CANCELED_BITMASK); |
| 96 | if ((oldch & CANCELED_BITMASK) != 0) |
| 97 | return 0; |
| 98 | |
| 99 | if (pd == THREAD_SELF) |
| 100 | { |
| 101 | /* A single-threaded process should be able to kill itself, since there |
| 102 | is nothing in the POSIX specification that says that it cannot. So |
| 103 | we set multiple_threads to true so that cancellation points get |
| 104 | executed. */ |
| 105 | THREAD_SETMEM (THREAD_SELF, header.multiple_threads, 1); |
| 106 | #ifndef TLS_MULTIPLE_THREADS_IN_TCB |
| 107 | __libc_multiple_threads = 1; |
| 108 | #endif |
| 109 | |
| 110 | THREAD_SETMEM (pd, result, PTHREAD_CANCELED); |
| 111 | if (pd->cancelstate == PTHREAD_CANCEL_ENABLE |
| 112 | && pd->canceltype == PTHREAD_CANCEL_ASYNCHRONOUS) |
| 113 | __do_cancel (); |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | return __pthread_kill_internal (th, SIGCANCEL); |
| 118 | } |
| 119 | versioned_symbol (libc, __pthread_cancel, pthread_cancel, GLIBC_2_34); |
| 120 | |
| 121 | #if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_0, GLIBC_2_34) |
| 122 | compat_symbol (libpthread, __pthread_cancel, pthread_cancel, GLIBC_2_0); |
| 123 | #endif |
| 124 | |
| 125 | /* Ensure that the unwinder is always linked in (the __pthread_unwind |
| 126 | reference from __do_cancel is weak). Use ___pthread_unwind_next |
| 127 | (three underscores) to produce a strong reference to the same |
| 128 | file. */ |
| 129 | PTHREAD_STATIC_FN_REQUIRE (___pthread_unwind_next) |
| 130 | |