1 | /* Verify the interaction of kill and thread groups. |
2 | Copyright (C) 2019-2022 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
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 | /* 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). */ |
22 | |
23 | #include <errno.h> |
24 | #include <signal.h> |
25 | #include <support/check.h> |
26 | #include <support/xsignal.h> |
27 | #include <support/xthread.h> |
28 | #include <support/xunistd.h> |
29 | |
30 | /* Signal set containing SIGUSR1. */ |
31 | static sigset_t sigusr1_set; |
32 | |
33 | /* Used to synchronize the threads. */ |
34 | static pthread_barrier_t barrier; |
35 | |
36 | /* TID of the thread to which the signal is sent. */ |
37 | static pid_t target_tid; |
38 | |
39 | /* Thread which is expected to receive the SIGUSR1 signal. */ |
40 | static pthread_t signal_thread; |
41 | |
42 | /* Pipe used to block and terminate the signal thread. */ |
43 | static int pipe_signal[2]; |
44 | |
45 | static volatile sig_atomic_t handler_tid; |
46 | |
47 | static void |
48 | sigusr1_handler (int signo) |
49 | { |
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. */ |
57 | char ch = 'X'; |
58 | xwrite (pipe_signal[1], &ch, 1); |
59 | } |
60 | |
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. */ |
63 | static void * |
64 | thread_pause_noreturn (void *closure) |
65 | { |
66 | target_tid = gettid (); |
67 | TEST_VERIFY (target_tid > 0); |
68 | xpthread_barrier_wait (barrier: &barrier); |
69 | pause (); |
70 | FAIL_EXIT1 ("The pause function returned" ); |
71 | return NULL; |
72 | } |
73 | |
74 | /* Thread which is expected to receive the signal. */ |
75 | static void * |
76 | thread_read_signal (void *closure) |
77 | { |
78 | xpthread_sigmask (SIG_UNBLOCK, set: &sigusr1_set, NULL); |
79 | xpthread_barrier_wait (barrier: &barrier); |
80 | TEST_VERIFY (target_tid > 0); |
81 | TEST_VERIFY (gettid () != target_tid); |
82 | char ch; |
83 | ssize_t ret = read (pipe_signal[0], &ch, 1); |
84 | if (ret == 1) |
85 | /* The signal was delivered before we entered the read system |
86 | call. */ |
87 | TEST_COMPARE (ch, 'X'); |
88 | else |
89 | { |
90 | /* The signal was delivered while blocked in the read system |
91 | call. */ |
92 | TEST_COMPARE (ret, -1); |
93 | TEST_COMPARE (errno, EINTR); |
94 | } |
95 | TEST_COMPARE (handler_tid, gettid ()); |
96 | return NULL; |
97 | } |
98 | |
99 | static int |
100 | do_test (void) |
101 | { |
102 | /* Block the SIGUSR1 signal in all threads. */ |
103 | sigemptyset (&sigusr1_set); |
104 | sigaddset (&sigusr1_set, SIGUSR1); |
105 | xpthread_sigmask (SIG_BLOCK, set: &sigusr1_set, NULL); |
106 | |
107 | xsignal (SIGUSR1, handler: sigusr1_handler); |
108 | xpipe (pipe_signal); |
109 | |
110 | xpthread_barrier_init (barrier: &barrier, NULL, count: 3); |
111 | |
112 | pthread_t target_thread |
113 | = xpthread_create (NULL, thread_func: thread_pause_noreturn, NULL); |
114 | signal_thread = xpthread_create (NULL, thread_func: thread_read_signal, NULL); |
115 | xpthread_barrier_wait (barrier: &barrier); |
116 | |
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); |
120 | |
121 | xpthread_join (thr: signal_thread); |
122 | xpthread_cancel (thr: target_thread); |
123 | xpthread_join (thr: target_thread); |
124 | |
125 | xpthread_barrier_destroy (barrier: &barrier); |
126 | return 0; |
127 | } |
128 | |
129 | #include <support/test-driver.c> |
130 | |