1 /* Verify the interaction of kill and thread groups.
2 Copyright (C) 2019-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 /* This test demonstrates that a signal which is sent to a specified
20 thread ID using the kill function is delivered to the entire thread
21 group (as if it had been sent to the process group). */
25 #include <support/check.h>
26 #include <support/xsignal.h>
27 #include <support/xthread.h>
28 #include <support/xunistd.h>
30 /* Signal set containing SIGUSR1. */
31 static sigset_t sigusr1_set
;
33 /* Used to synchronize the threads. */
34 static pthread_barrier_t barrier
;
36 /* TID of the thread to which the signal is sent. */
37 static pid_t target_tid
;
39 /* Thread which is expected to receive the SIGUSR1 signal. */
40 static pthread_t signal_thread
;
42 /* Pipe used to block and terminate the signal thread. */
43 static int pipe_signal
[2];
45 static volatile sig_atomic_t handler_tid
;
48 sigusr1_handler (int signo
)
50 TEST_COMPARE (signo
, SIGUSR1
);
51 TEST_VERIFY (pthread_self () == signal_thread
);
52 TEST_COMPARE (handler_tid
, 0);
53 handler_tid
= gettid ();
54 TEST_VERIFY (handler_tid
> 0);
55 /* Ensure that the read system call in thread_read exits if the
56 signal is delivered before the system call is invoked. */
58 xwrite (pipe_signal
[1], &ch
, 1);
61 /* Thread which calls pause without expecting it to return. The TID
62 of this thread is used as the target in the kill function call. */
64 thread_pause_noreturn (void *closure
)
66 target_tid
= gettid ();
67 TEST_VERIFY (target_tid
> 0);
68 xpthread_barrier_wait (&barrier
);
70 FAIL_EXIT1 ("The pause function returned");
74 /* Thread which is expected to receive the signal. */
76 thread_read_signal (void *closure
)
78 xpthread_sigmask (SIG_UNBLOCK
, &sigusr1_set
, NULL
);
79 xpthread_barrier_wait (&barrier
);
80 TEST_VERIFY (target_tid
> 0);
81 TEST_VERIFY (gettid () != target_tid
);
83 ssize_t ret
= read (pipe_signal
[0], &ch
, 1);
85 /* The signal was delivered before we entered the read system
87 TEST_COMPARE (ch
, 'X');
90 /* The signal was delivered while blocked in the read system
92 TEST_COMPARE (ret
, -1);
93 TEST_COMPARE (errno
, EINTR
);
95 TEST_COMPARE (handler_tid
, gettid ());
102 /* Block the SIGUSR1 signal in all threads. */
103 sigemptyset (&sigusr1_set
);
104 sigaddset (&sigusr1_set
, SIGUSR1
);
105 xpthread_sigmask (SIG_BLOCK
, &sigusr1_set
, NULL
);
107 xsignal (SIGUSR1
, sigusr1_handler
);
110 xpthread_barrier_init (&barrier
, NULL
, 3);
112 pthread_t target_thread
113 = xpthread_create (NULL
, thread_pause_noreturn
, NULL
);
114 signal_thread
= xpthread_create (NULL
, thread_read_signal
, NULL
);
115 xpthread_barrier_wait (&barrier
);
117 /* Send the SIGUSR1 signal to the thread which has it blocked, and
118 expect it to be delivered to the other thread. */
119 TEST_COMPARE (kill (target_tid
, SIGUSR1
), 0);
121 xpthread_join (signal_thread
);
122 xpthread_cancel (target_thread
);
123 xpthread_join (target_thread
);
125 xpthread_barrier_destroy (&barrier
);
129 #include <support/test-driver.c>