test-signal.c
Go to the documentation of this file.
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 #include "uv.h"
23 #include "task.h"
24 
25 #ifndef _WIN32
26 #include <unistd.h>
27 #endif
28 
29 TEST_IMPL(kill_invalid_signum) {
30  uv_pid_t pid;
31 
32  pid = uv_os_getpid();
33 
34  ASSERT(uv_kill(pid, -1) == UV_EINVAL);
35 #ifdef _WIN32
36  /* NSIG is not available on all platforms. */
37  ASSERT(uv_kill(pid, NSIG) == UV_EINVAL);
38 #endif
39  ASSERT(uv_kill(pid, 4096) == UV_EINVAL);
40 
42  return 0;
43 }
44 
45 /* For Windows we test only signum handling */
46 #ifdef _WIN32
47 static void signum_test_cb(uv_signal_t* handle, int signum) {
48  FATAL("signum_test_cb should not be called");
49 }
50 
51 TEST_IMPL(win32_signum_number) {
53  uv_loop_t* loop;
54 
57 
58  ASSERT(uv_signal_start(&signal, signum_test_cb, 0) == UV_EINVAL);
59  ASSERT(uv_signal_start(&signal, signum_test_cb, SIGINT) == 0);
60  ASSERT(uv_signal_start(&signal, signum_test_cb, SIGBREAK) == 0);
61  ASSERT(uv_signal_start(&signal, signum_test_cb, SIGHUP) == 0);
62  ASSERT(uv_signal_start(&signal, signum_test_cb, SIGWINCH) == 0);
63  ASSERT(uv_signal_start(&signal, signum_test_cb, SIGILL) == 0);
64  ASSERT(uv_signal_start(&signal, signum_test_cb, SIGABRT_COMPAT) == 0);
65  ASSERT(uv_signal_start(&signal, signum_test_cb, SIGFPE) == 0);
66  ASSERT(uv_signal_start(&signal, signum_test_cb, SIGSEGV) == 0);
67  ASSERT(uv_signal_start(&signal, signum_test_cb, SIGTERM) == 0);
68  ASSERT(uv_signal_start(&signal, signum_test_cb, SIGABRT) == 0);
69  ASSERT(uv_signal_start(&signal, signum_test_cb, -1) == UV_EINVAL);
70  ASSERT(uv_signal_start(&signal, signum_test_cb, NSIG) == UV_EINVAL);
71  ASSERT(uv_signal_start(&signal, signum_test_cb, 1024) == UV_EINVAL);
73  return 0;
74 }
75 #else
76 #include <errno.h>
77 #include <signal.h>
78 #include <stdarg.h>
79 #include <stdio.h>
80 #include <stdlib.h>
81 #include <string.h>
82 #include <unistd.h>
83 
84 #define NSIGNALS 10
85 
86 struct timer_ctx {
87  unsigned int ncalls;
89  int signum;
90 };
91 
92 struct signal_ctx {
94  unsigned int ncalls;
96  int signum;
97  int one_shot;
98 };
99 
100 
101 static void signal_cb(uv_signal_t* handle, int signum) {
102  struct signal_ctx* ctx = container_of(handle, struct signal_ctx, handle);
103  ASSERT(signum == ctx->signum);
104  if (++ctx->ncalls == NSIGNALS) {
105  if (ctx->stop_or_close == STOP)
107  else if (ctx->stop_or_close == CLOSE)
108  uv_close((uv_handle_t*)handle, NULL);
109  else
110  ASSERT(0);
111  }
112 }
113 
115  struct signal_ctx* ctx = container_of(handle, struct signal_ctx, handle);
116  ASSERT(signum == ctx->signum);
117  ASSERT(++ctx->ncalls == 1);
118  if (ctx->stop_or_close == CLOSE)
119  uv_close((uv_handle_t*)handle, NULL);
120 }
121 
122 
123 static void timer_cb(uv_timer_t* handle) {
124  struct timer_ctx* ctx = container_of(handle, struct timer_ctx, handle);
125 
126  raise(ctx->signum);
127 
128  if (++ctx->ncalls == NSIGNALS)
129  uv_close((uv_handle_t*)handle, NULL);
130 }
131 
132 
134  int signum,
135  struct signal_ctx* ctx,
136  int one_shot) {
137  ctx->ncalls = 0;
138  ctx->signum = signum;
139  ctx->stop_or_close = CLOSE;
140  ctx->one_shot = one_shot;
141  ASSERT(0 == uv_signal_init(loop, &ctx->handle));
142  if (one_shot)
144  else
145  ASSERT(0 == uv_signal_start(&ctx->handle, signal_cb, signum));
146 }
147 
148 static void start_timer(uv_loop_t* loop, int signum, struct timer_ctx* ctx) {
149  ctx->ncalls = 0;
150  ctx->signum = signum;
151  ASSERT(0 == uv_timer_init(loop, &ctx->handle));
152  ASSERT(0 == uv_timer_start(&ctx->handle, timer_cb, 5, 5));
153 }
154 
155 
156 TEST_IMPL(we_get_signal) {
157  struct signal_ctx sc;
158  struct timer_ctx tc;
159  uv_loop_t* loop;
160 
161  loop = uv_default_loop();
162  start_timer(loop, SIGCHLD, &tc);
163  start_watcher(loop, SIGCHLD, &sc, 0);
164  sc.stop_or_close = STOP; /* stop, don't close the signal handle */
166  ASSERT(tc.ncalls == NSIGNALS);
167  ASSERT(sc.ncalls == NSIGNALS);
168 
169  start_timer(loop, SIGCHLD, &tc);
171  ASSERT(tc.ncalls == NSIGNALS);
172  ASSERT(sc.ncalls == NSIGNALS);
173 
174  sc.ncalls = 0;
175  sc.stop_or_close = CLOSE; /* now close it when it's done */
176  uv_signal_start(&sc.handle, signal_cb, SIGCHLD);
177 
178  start_timer(loop, SIGCHLD, &tc);
180  ASSERT(tc.ncalls == NSIGNALS);
181  ASSERT(sc.ncalls == NSIGNALS);
182 
184  return 0;
185 }
186 
187 
188 TEST_IMPL(we_get_signals) {
189  struct signal_ctx sc[4];
190  struct timer_ctx tc[2];
191  uv_loop_t* loop;
192  unsigned int i;
193 
194  loop = uv_default_loop();
195  start_watcher(loop, SIGUSR1, sc + 0, 0);
196  start_watcher(loop, SIGUSR1, sc + 1, 0);
197  start_watcher(loop, SIGUSR2, sc + 2, 0);
198  start_watcher(loop, SIGUSR2, sc + 3, 0);
199  start_timer(loop, SIGUSR1, tc + 0);
200  start_timer(loop, SIGUSR2, tc + 1);
202 
203  for (i = 0; i < ARRAY_SIZE(sc); i++)
204  ASSERT(sc[i].ncalls == NSIGNALS);
205 
206  for (i = 0; i < ARRAY_SIZE(tc); i++)
207  ASSERT(tc[i].ncalls == NSIGNALS);
208 
210  return 0;
211 }
212 
213 TEST_IMPL(we_get_signal_one_shot) {
214  struct signal_ctx sc;
215  struct timer_ctx tc;
216  uv_loop_t* loop;
217 
218  loop = uv_default_loop();
219  start_timer(loop, SIGCHLD, &tc);
220  start_watcher(loop, SIGCHLD, &sc, 1);
221  sc.stop_or_close = NOOP;
223  ASSERT(tc.ncalls == NSIGNALS);
224  ASSERT(sc.ncalls == 1);
225 
226  start_timer(loop, SIGCHLD, &tc);
228  ASSERT(sc.ncalls == 1);
229 
230  sc.ncalls = 0;
231  sc.stop_or_close = CLOSE; /* now close it when it's done */
233  start_timer(loop, SIGCHLD, &tc);
235  ASSERT(tc.ncalls == NSIGNALS);
236  ASSERT(sc.ncalls == 1);
237 
239  return 0;
240 }
241 
242 TEST_IMPL(we_get_signals_mixed) {
243  struct signal_ctx sc[4];
244  struct timer_ctx tc;
245  uv_loop_t* loop;
246 
247  loop = uv_default_loop();
248 
249  /* 2 one-shot */
250  start_timer(loop, SIGCHLD, &tc);
251  start_watcher(loop, SIGCHLD, sc + 0, 1);
252  start_watcher(loop, SIGCHLD, sc + 1, 1);
253  sc[0].stop_or_close = CLOSE;
254  sc[1].stop_or_close = CLOSE;
256  ASSERT(tc.ncalls == NSIGNALS);
257  ASSERT(sc[0].ncalls == 1);
258  ASSERT(sc[1].ncalls == 1);
259 
260  /* 2 one-shot, 1 normal then remove normal */
261  start_timer(loop, SIGCHLD, &tc);
262  start_watcher(loop, SIGCHLD, sc + 0, 1);
263  start_watcher(loop, SIGCHLD, sc + 1, 1);
264  sc[0].stop_or_close = CLOSE;
265  sc[1].stop_or_close = CLOSE;
266  start_watcher(loop, SIGCHLD, sc + 2, 0);
267  uv_close((uv_handle_t*)&(sc[2]).handle, NULL);
269  ASSERT(tc.ncalls == NSIGNALS);
270  ASSERT(sc[0].ncalls == 1);
271  ASSERT(sc[1].ncalls == 1);
272  ASSERT(sc[2].ncalls == 0);
273 
274  /* 2 normal, 1 one-shot then remove one-shot */
275  start_timer(loop, SIGCHLD, &tc);
276  start_watcher(loop, SIGCHLD, sc + 0, 0);
277  start_watcher(loop, SIGCHLD, sc + 1, 0);
278  sc[0].stop_or_close = CLOSE;
279  sc[1].stop_or_close = CLOSE;
280  start_watcher(loop, SIGCHLD, sc + 2, 1);
281  uv_close((uv_handle_t*)&(sc[2]).handle, NULL);
283  ASSERT(tc.ncalls == NSIGNALS);
284  ASSERT(sc[0].ncalls == NSIGNALS);
285  ASSERT(sc[1].ncalls == NSIGNALS);
286  ASSERT(sc[2].ncalls == 0);
287 
288  /* 2 normal, 2 one-shot then remove 2 normal */
289  start_timer(loop, SIGCHLD, &tc);
290  start_watcher(loop, SIGCHLD, sc + 0, 0);
291  start_watcher(loop, SIGCHLD, sc + 1, 0);
292  start_watcher(loop, SIGCHLD, sc + 2, 1);
293  start_watcher(loop, SIGCHLD, sc + 3, 1);
294  sc[2].stop_or_close = CLOSE;
295  sc[3].stop_or_close = CLOSE;
296  uv_close((uv_handle_t*)&(sc[0]).handle, NULL);
297  uv_close((uv_handle_t*)&(sc[1]).handle, NULL);
299  ASSERT(tc.ncalls == NSIGNALS);
300  ASSERT(sc[0].ncalls == 0);
301  ASSERT(sc[1].ncalls == 0);
302  ASSERT(sc[2].ncalls == 1);
303  ASSERT(sc[2].ncalls == 1);
304 
305  /* 1 normal, 1 one-shot, 2 normal then remove 1st normal, 2nd normal */
306  start_timer(loop, SIGCHLD, &tc);
307  start_watcher(loop, SIGCHLD, sc + 0, 0);
308  start_watcher(loop, SIGCHLD, sc + 1, 1);
309  start_watcher(loop, SIGCHLD, sc + 2, 0);
310  start_watcher(loop, SIGCHLD, sc + 3, 0);
311  sc[3].stop_or_close = CLOSE;
312  uv_close((uv_handle_t*)&(sc[0]).handle, NULL);
313  uv_close((uv_handle_t*)&(sc[2]).handle, NULL);
315  ASSERT(tc.ncalls == NSIGNALS);
316  ASSERT(sc[0].ncalls == 0);
317  ASSERT(sc[1].ncalls == 1);
318  ASSERT(sc[2].ncalls == 0);
319  ASSERT(sc[3].ncalls == NSIGNALS);
320 
322  return 0;
323 }
324 
325 #endif /* _WIN32 */
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
ARRAY_SIZE
#define ARRAY_SIZE(array)
Definition: bloaty.cc:101
task.h
ctx
Definition: benchmark-async.c:30
signal_ctx::signum
int signum
Definition: test-signal.c:96
uv_kill
UV_EXTERN int uv_kill(int pid, int signum)
Definition: unix/process.c:582
uv_os_getpid
UV_EXTERN uv_pid_t uv_os_getpid(void)
Definition: unix/core.c:1392
uv_signal_init
UV_EXTERN int uv_signal_init(uv_loop_t *loop, uv_signal_t *handle)
Definition: unix/signal.c:317
string.h
SIGHUP
#define SIGHUP
Definition: win.h:86
uv_pid_t
pid_t uv_pid_t
Definition: unix.h:129
TEST_IMPL
TEST_IMPL(kill_invalid_signum)
Definition: test-signal.c:29
ASSERT
#define ASSERT(expr)
Definition: task.h:102
signal_ctx::CLOSE
@ CLOSE
Definition: test-signal.c:93
uv_signal_start_oneshot
UV_EXTERN int uv_signal_start_oneshot(uv_signal_t *handle, uv_signal_cb signal_cb, int signum)
Definition: unix/signal.c:343
signal_ctx
Definition: test-signal.c:92
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
container_of
#define container_of(ptr, type, member)
Definition: uv-common.h:57
signal_ctx::one_shot
int one_shot
Definition: test-signal.c:97
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
signal_cb
static void signal_cb(uv_signal_t *handle, int signum)
Definition: test-signal.c:101
SIGABRT_COMPAT
#define SIGABRT_COMPAT
Definition: win.h:101
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
signal
static void signal(notification *n)
Definition: alts_tsi_handshaker_test.cc:107
timer_ctx
Definition: test-signal.c:86
signal_cb_one_shot
static void signal_cb_one_shot(uv_signal_t *handle, int signum)
Definition: test-signal.c:114
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
timer_ctx::handle
uv_timer_t handle
Definition: test-signal.c:88
timer_ctx::signum
int signum
Definition: test-signal.c:89
uv_timer_s
Definition: uv.h:850
uv_signal_stop
UV_EXTERN int uv_signal_stop(uv_signal_t *handle)
Definition: unix/signal.c:511
signal_ctx::NOOP
@ NOOP
Definition: test-signal.c:93
uv.h
start_timer
static void start_timer(uv_loop_t *loop, int signum, struct timer_ctx *ctx)
Definition: test-signal.c:148
timer_ctx::ncalls
unsigned int ncalls
Definition: test-signal.c:87
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
FATAL
#define FATAL(msg)
Definition: task.h:88
uv_signal_s
Definition: uv.h:1561
timer_cb
static void timer_cb(uv_timer_t *handle)
Definition: test-signal.c:123
signal_ctx::handle
uv_signal_t handle
Definition: test-signal.c:95
signal_ctx::stop_or_close
enum signal_ctx::@412 stop_or_close
NSIG
#define NSIG
Definition: win.h:95
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
uv_timer_start
UV_EXTERN int uv_timer_start(uv_timer_t *handle, uv_timer_cb cb, uint64_t timeout, uint64_t repeat)
Definition: timer.c:66
uv_loop_s
Definition: uv.h:1767
start_watcher
static void start_watcher(uv_loop_t *loop, int signum, struct signal_ctx *ctx, int one_shot)
Definition: test-signal.c:133
uv_timer_init
UV_EXTERN int uv_timer_init(uv_loop_t *, uv_timer_t *handle)
Definition: timer.c:58
signal_ctx::STOP
@ STOP
Definition: test-signal.c:93
signal_ctx::ncalls
unsigned int ncalls
Definition: test-signal.c:94
errno.h
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
NSIGNALS
#define NSIGNALS
Definition: test-signal.c:84
uv_signal_start
UV_EXTERN int uv_signal_start(uv_signal_t *handle, uv_signal_cb signal_cb, int signum)
Definition: unix/signal.c:338
SIGWINCH
#define SIGWINCH
Definition: win.h:88


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:30