Skip to content

Commit 6f74ae6

Browse files
author
Felipe cruz
committed
Make zctx_destroy restore original sighandler
1 parent 7bc4f32 commit 6f74ae6

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/zctx.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct _zctx_t {
6464
bool main; // True if we're the main thread
6565
int iothreads; // Number of IO threads, default 1
6666
int linger; // Linger timeout, default 0
67+
struct sigaction oacts[2]; // Current sigaction values
6768
};
6869

6970

@@ -108,8 +109,8 @@ zctx_new (void)
108109
action.sa_handler = s_signal_handler;
109110
action.sa_flags = 0;
110111
sigemptyset (&action.sa_mask);
111-
sigaction (SIGINT, &action, NULL);
112-
sigaction (SIGTERM, &action, NULL);
112+
sigaction (SIGINT, &action, &self->oacts[0]);
113+
sigaction (SIGTERM, &action, &self->oacts[1]);
113114
}
114115
#endif
115116
return self;
@@ -130,6 +131,10 @@ zctx_destroy (zctx_t **self_p)
130131
zlist_destroy (&self->sockets);
131132
if (self->main && self->context)
132133
zmq_term (self->context);
134+
#if defined (__UNIX__)
135+
sigaction (SIGINT, &self->oacts[0], NULL);
136+
sigaction (SIGTERM, &self->oacts[1], NULL);
137+
#endif
133138
free (self);
134139
*self_p = NULL;
135140
}
@@ -292,8 +297,33 @@ zctx_test (bool verbose)
292297
zsocket_connect (s6, "tcp://127.0.0.1:5555");
293298
assert (zctx_underlying (ctx));
294299

300+
#if defined (__UNIX__)
301+
struct sigaction action;
302+
sigaction (SIGINT, NULL, &action);
303+
assert (action.sa_handler == s_signal_handler);
304+
305+
sigaction (SIGTERM, NULL, &action);
306+
assert (action.sa_handler == s_signal_handler);
295307
// Everything should be cleanly closed now
296308
zctx_destroy (&ctx);
309+
310+
sigaction (SIGINT, NULL, &action);
311+
sigaction (SIGTERM, NULL, &action);
312+
assert (action.sa_handler != s_signal_handler);
313+
assert (action.sa_handler != s_signal_handler);
314+
315+
// Check if no signal handler is installed if zctx_interrupted
316+
zctx_interrupted = 1;
317+
bzero(&action, sizeof(struct sigaction));
318+
ctx = zctx_new ();
319+
320+
sigaction (SIGINT, NULL, &action);
321+
assert (action.sa_handler != s_signal_handler);
322+
323+
sigaction (SIGTERM, NULL, &action);
324+
assert (action.sa_handler != s_signal_handler);
325+
#endif
326+
zctx_destroy (&ctx);
297327
// @end
298328

299329
printf ("OK\n");

0 commit comments

Comments
 (0)