• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21 
22 
23 /* This test does not pretend to be cross-platform. */
24 #ifndef _WIN32
25 
26 #include "uv.h"
27 #include "task.h"
28 
29 #include <errno.h>
30 #include <signal.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 
37 /* The value of NUM_SIGNAL_HANDLING_THREADS is not arbitrary; it needs to be a
38  * multiple of three for reasons that will become clear when you scroll down.
39  * We're basically creating three different thread groups.  The total needs
40  * to be divisible by three in order for the numbers in the final check to
41  * match up.
42  */
43 #define NUM_SIGNAL_HANDLING_THREADS 24
44 #define NUM_LOOP_CREATING_THREADS 10
45 
46 enum signal_action {
47   ONLY_SIGUSR1,
48   ONLY_SIGUSR2,
49   SIGUSR1_AND_SIGUSR2
50 };
51 
52 static uv_sem_t sem;
53 static uv_mutex_t counter_lock;
54 static volatile int stop = 0;
55 
56 static volatile int signal1_cb_counter = 0;
57 static volatile int signal2_cb_counter = 0;
58 static volatile int loop_creation_counter = 0;
59 
60 
increment_counter(volatile int * counter)61 static void increment_counter(volatile int* counter) {
62   uv_mutex_lock(&counter_lock);
63   ++(*counter);
64   uv_mutex_unlock(&counter_lock);
65 }
66 
67 
signal1_cb(uv_signal_t * handle,int signum)68 static void signal1_cb(uv_signal_t* handle, int signum) {
69   ASSERT(signum == SIGUSR1);
70   increment_counter(&signal1_cb_counter);
71   uv_signal_stop(handle);
72 }
73 
74 
signal2_cb(uv_signal_t * handle,int signum)75 static void signal2_cb(uv_signal_t* handle, int signum) {
76   ASSERT(signum == SIGUSR2);
77   increment_counter(&signal2_cb_counter);
78   uv_signal_stop(handle);
79 }
80 
81 
signal_handling_worker(void * context)82 static void signal_handling_worker(void* context) {
83   enum signal_action action;
84   uv_signal_t signal1a;
85   uv_signal_t signal1b;
86   uv_signal_t signal2;
87   uv_loop_t loop;
88   int r;
89 
90   action = (enum signal_action) (uintptr_t) context;
91 
92   ASSERT(0 == uv_loop_init(&loop));
93 
94   /* Setup the signal watchers and start them. */
95   if (action == ONLY_SIGUSR1 || action == SIGUSR1_AND_SIGUSR2) {
96     r = uv_signal_init(&loop, &signal1a);
97     ASSERT(r == 0);
98     r = uv_signal_start(&signal1a, signal1_cb, SIGUSR1);
99     ASSERT(r == 0);
100     r = uv_signal_init(&loop, &signal1b);
101     ASSERT(r == 0);
102     r = uv_signal_start(&signal1b, signal1_cb, SIGUSR1);
103     ASSERT(r == 0);
104   }
105 
106   if (action == ONLY_SIGUSR2 || action == SIGUSR1_AND_SIGUSR2) {
107     r = uv_signal_init(&loop, &signal2);
108     ASSERT(r == 0);
109     r = uv_signal_start(&signal2, signal2_cb, SIGUSR2);
110     ASSERT(r == 0);
111   }
112 
113   /* Signal watchers are now set up. */
114   uv_sem_post(&sem);
115 
116   /* Wait for all signals. The signal callbacks stop the watcher, so uv_run
117    * will return when all signal watchers caught a signal.
118    */
119   r = uv_run(&loop, UV_RUN_DEFAULT);
120   ASSERT(r == 0);
121 
122   /* Restart the signal watchers. */
123   if (action == ONLY_SIGUSR1 || action == SIGUSR1_AND_SIGUSR2) {
124     r = uv_signal_start(&signal1a, signal1_cb, SIGUSR1);
125     ASSERT(r == 0);
126     r = uv_signal_start(&signal1b, signal1_cb, SIGUSR1);
127     ASSERT(r == 0);
128   }
129 
130   if (action == ONLY_SIGUSR2 || action == SIGUSR1_AND_SIGUSR2) {
131     r = uv_signal_start(&signal2, signal2_cb, SIGUSR2);
132     ASSERT(r == 0);
133   }
134 
135   /* Wait for signals once more. */
136   uv_sem_post(&sem);
137 
138   r = uv_run(&loop, UV_RUN_DEFAULT);
139   ASSERT(r == 0);
140 
141   /* Close the watchers. */
142   if (action == ONLY_SIGUSR1 || action == SIGUSR1_AND_SIGUSR2) {
143     uv_close((uv_handle_t*) &signal1a, NULL);
144     uv_close((uv_handle_t*) &signal1b, NULL);
145   }
146 
147   if (action == ONLY_SIGUSR2 || action == SIGUSR1_AND_SIGUSR2) {
148     uv_close((uv_handle_t*) &signal2, NULL);
149   }
150 
151   /* Wait for the signal watchers to close. */
152   r = uv_run(&loop, UV_RUN_DEFAULT);
153   ASSERT(r == 0);
154 
155   uv_loop_close(&loop);
156 }
157 
158 
signal_unexpected_cb(uv_signal_t * handle,int signum)159 static void signal_unexpected_cb(uv_signal_t* handle, int signum) {
160   ASSERT(0 && "signal_unexpected_cb should never be called");
161 }
162 
163 
loop_creating_worker(void * context)164 static void loop_creating_worker(void* context) {
165   (void) context;
166 
167   do {
168     uv_loop_t *loop;
169     uv_signal_t signal;
170     int r;
171 
172     loop = malloc(sizeof(*loop));
173     ASSERT_NOT_NULL(loop);
174     ASSERT(0 == uv_loop_init(loop));
175 
176     r = uv_signal_init(loop, &signal);
177     ASSERT(r == 0);
178 
179     r = uv_signal_start(&signal, signal_unexpected_cb, SIGTERM);
180     ASSERT(r == 0);
181 
182     uv_close((uv_handle_t*) &signal, NULL);
183 
184     r = uv_run(loop, UV_RUN_DEFAULT);
185     ASSERT(r == 0);
186 
187     uv_loop_close(loop);
188     free(loop);
189 
190     increment_counter(&loop_creation_counter);
191   } while (!stop);
192 }
193 
194 
TEST_IMPL(signal_multiple_loops)195 TEST_IMPL(signal_multiple_loops) {
196 #if defined(__CYGWIN__) || defined(__MSYS__)
197   /* FIXME: This test needs more investigation.  Somehow the `read` in
198      uv__signal_lock fails spuriously with EACCES or even EAGAIN even
199      though it is supposed to be blocking.  Also the test hangs during
200      thread setup occasionally.  */
201   RETURN_SKIP("FIXME: This test needs more investigation on Cygwin");
202 #endif
203 /* TODO(gengjiawen): Fix test on QEMU. */
204 #if defined(__QEMU__)
205   // See https://github.com/libuv/libuv/issues/2859
206   RETURN_SKIP("QEMU's signal emulation code is notoriously tricky");
207 #endif
208   uv_thread_t loop_creating_threads[NUM_LOOP_CREATING_THREADS];
209   uv_thread_t signal_handling_threads[NUM_SIGNAL_HANDLING_THREADS];
210   enum signal_action action;
211   sigset_t sigset;
212   int i;
213   int r;
214 
215   r = uv_sem_init(&sem, 0);
216   ASSERT(r == 0);
217 
218   r = uv_mutex_init(&counter_lock);
219   ASSERT(r == 0);
220 
221   /* Create a couple of threads that create a destroy loops continuously. */
222   for (i = 0; i < NUM_LOOP_CREATING_THREADS; i++) {
223     r = uv_thread_create(&loop_creating_threads[i],
224                          loop_creating_worker,
225                          NULL);
226     ASSERT(r == 0);
227   }
228 
229   /* Create a couple of threads that actually handle signals. */
230   for (i = 0; i < NUM_SIGNAL_HANDLING_THREADS; i++) {
231     switch (i % 3) {
232       case 0: action = ONLY_SIGUSR1; break;
233       case 1: action = ONLY_SIGUSR2; break;
234       case 2: action = SIGUSR1_AND_SIGUSR2; break;
235     }
236 
237     r = uv_thread_create(&signal_handling_threads[i],
238                          signal_handling_worker,
239                          (void*) (uintptr_t) action);
240     ASSERT(r == 0);
241   }
242 
243   /* Wait until all threads have started and set up their signal watchers. */
244   for (i = 0; i < NUM_SIGNAL_HANDLING_THREADS; i++)
245     uv_sem_wait(&sem);
246 
247   r = kill(getpid(), SIGUSR1);
248   ASSERT(r == 0);
249   r = kill(getpid(), SIGUSR2);
250   ASSERT(r == 0);
251 
252   /* Wait for all threads to handle these signals. */
253   for (i = 0; i < NUM_SIGNAL_HANDLING_THREADS; i++)
254     uv_sem_wait(&sem);
255 
256   /* Block all signals to this thread, so we are sure that from here the signal
257    * handler runs in another thread. This is more likely to catch thread and
258    * signal safety issues if there are any.
259    */
260   sigfillset(&sigset);
261   pthread_sigmask(SIG_SETMASK, &sigset, NULL);
262 
263   r = kill(getpid(), SIGUSR1);
264   ASSERT(r == 0);
265   r = kill(getpid(), SIGUSR2);
266   ASSERT(r == 0);
267 
268   /* Wait for all signal handling threads to exit. */
269   for (i = 0; i < NUM_SIGNAL_HANDLING_THREADS; i++) {
270     r = uv_thread_join(&signal_handling_threads[i]);
271     ASSERT(r == 0);
272   }
273 
274   /* Tell all loop creating threads to stop. */
275   stop = 1;
276 
277   /* Wait for all loop creating threads to exit. */
278   for (i = 0; i < NUM_LOOP_CREATING_THREADS; i++) {
279     r = uv_thread_join(&loop_creating_threads[i]);
280     ASSERT(r == 0);
281   }
282 
283   uv_sem_destroy(&sem);
284   printf("signal1_cb calls: %d\n", signal1_cb_counter);
285   printf("signal2_cb calls: %d\n", signal2_cb_counter);
286   printf("loops created and destroyed: %d\n", loop_creation_counter);
287 
288   /* The division by three reflects the fact that we spawn three different
289    * thread groups of (NUM_SIGNAL_HANDLING_THREADS / 3) threads each.
290    */
291   ASSERT(signal1_cb_counter == 8 * (NUM_SIGNAL_HANDLING_THREADS / 3));
292   ASSERT(signal2_cb_counter == 4 * (NUM_SIGNAL_HANDLING_THREADS / 3));
293 
294   /* We don't know exactly how much loops will be created and destroyed, but at
295    * least there should be 1 for every loop creating thread.
296    */
297   ASSERT(loop_creation_counter >= NUM_LOOP_CREATING_THREADS);
298 
299   MAKE_VALGRIND_HAPPY();
300   return 0;
301 }
302 
303 #else
304 
305 typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */
306 
307 #endif /* !_WIN32 */
308