| 1 |
/* Test of pthread_sigmask in a multi-threaded program. |
| 2 |
Copyright (C) 2011-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation, either version 3 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
/* Written by Bruno Haible <[email protected]>, 2011. */ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
#include <signal.h> |
| 22 |
|
| 23 |
#include <errno.h> |
| 24 |
#include <pthread.h> |
| 25 |
#include <stdio.h> |
| 26 |
#include <unistd.h> |
| 27 |
|
| 28 |
#include "virtualbox.h" |
| 29 |
#include "macros.h" |
| 30 |
|
| 31 |
#if USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS |
| 32 |
|
| 33 |
static pthread_t main_thread; |
| 34 |
static pthread_t killer_thread; |
| 35 |
|
| 36 |
static void * |
| 37 |
killer_thread_func (_GL_UNUSED void *arg) |
| 38 |
{ |
| 39 |
sleep (1); |
| 40 |
pthread_kill (main_thread, SIGINT); |
| 41 |
return NULL; |
| 42 |
} |
| 43 |
|
| 44 |
static volatile int sigint_occurred; |
| 45 |
|
| 46 |
static void |
| 47 |
sigint_handler (_GL_UNUSED int sig) |
| 48 |
{ |
| 49 |
sigint_occurred++; |
| 50 |
} |
| 51 |
|
| 52 |
int |
| 53 |
main () |
| 54 |
{ |
| 55 |
/* This test occasionally fails on Linux (glibc or musl libc), in a |
| 56 |
VirtualBox VM with paravirtualization = Default or KVM, with ≥ 2 CPUs. |
| 57 |
Skip the test in this situation. */ |
| 58 |
if (is_running_under_virtualbox_kvm () && num_cpus () > 1) |
| 59 |
{ |
| 60 |
fputs ("Skipping test: avoiding VirtualBox bug with KVM paravirtualization\n", |
| 61 |
stderr); |
| 62 |
return 77; |
| 63 |
} |
| 64 |
|
| 65 |
sigset_t set; |
| 66 |
|
| 67 |
signal (SIGINT, sigint_handler); |
| 68 |
|
| 69 |
sigemptyset (&set); |
| 70 |
sigaddset (&set, SIGINT); |
| 71 |
|
| 72 |
/* Check error handling. */ |
| 73 |
/* This call returns 0 on NetBSD 8.0. */ |
| 74 |
#if !defined __NetBSD__ |
| 75 |
ASSERT (pthread_sigmask (1729, &set, NULL) == EINVAL); |
| 76 |
#endif |
| 77 |
|
| 78 |
/* Block SIGINT. */ |
| 79 |
ASSERT (pthread_sigmask (SIG_BLOCK, &set, NULL) == 0); |
| 80 |
|
| 81 |
/* Request a SIGINT signal from another thread. */ |
| 82 |
main_thread = pthread_self (); |
| 83 |
ASSERT (pthread_create (&killer_thread, NULL, killer_thread_func, NULL) == 0); |
| 84 |
|
| 85 |
/* Wait. */ |
| 86 |
sleep (2); |
| 87 |
|
| 88 |
/* The signal should not have arrived yet, because it is blocked. */ |
| 89 |
ASSERT (sigint_occurred == 0); |
| 90 |
|
| 91 |
/* Unblock SIGINT. */ |
| 92 |
ASSERT (pthread_sigmask (SIG_UNBLOCK, &set, NULL) == 0); |
| 93 |
|
| 94 |
/* The signal should have arrived now, because POSIX says |
| 95 |
"If there are any pending unblocked signals after the call to |
| 96 |
pthread_sigmask(), at least one of those signals shall be delivered |
| 97 |
before the call to pthread_sigmask() returns." */ |
| 98 |
ASSERT (sigint_occurred == 1); |
| 99 |
|
| 100 |
/* Clean up the thread. This avoid a "ThreadSanitizer: thread leak" warning |
| 101 |
from "gcc -fsanitize=thread". */ |
| 102 |
ASSERT (pthread_join (killer_thread, NULL) == 0); |
| 103 |
|
| 104 |
return test_exit_status; |
| 105 |
} |
| 106 |
|
| 107 |
#else |
| 108 |
|
| 109 |
int |
| 110 |
main () |
| 111 |
{ |
| 112 |
fputs ("Skipping test: POSIX threads not enabled\n", stderr); |
| 113 |
return 77; |
| 114 |
} |
| 115 |
|
| 116 |
#endif |