diff --git a/examples/client.c b/examples/client.c index 6f648417..9cc2c0a9 100644 --- a/examples/client.c +++ b/examples/client.c @@ -1569,6 +1569,9 @@ main(int argc, char **argv) { sa.sa_flags = 0; sigaction (SIGINT, &sa, NULL); sigaction (SIGTERM, &sa, NULL); + /* So we do not exit on a SIGPIPE */ + sa.sa_handler = SIG_IGN; + sigaction (SIGPIPE, &sa, NULL); #endif while (!quit && !(ready && !doing_getting_block && coap_can_exit(ctx)) ) { diff --git a/examples/coap-rd.c b/examples/coap-rd.c index 53e3ad1d..7c7a8332 100644 --- a/examples/coap-rd.c +++ b/examples/coap-rd.c @@ -85,9 +85,14 @@ rd_delete(rd_t *rd) { } } -/* temporary storage for dynamic resource representations */ static int quit = 0; +/* SIGINT handler: set quit to 1 for graceful termination */ +static void +handle_sigint(int signum UNUSED_PARAM) { + quit = 1; +} + static void hnd_get_resource(coap_context_t *ctx UNUSED_PARAM, struct coap_resource_t *resource, @@ -620,6 +625,9 @@ main(int argc, char **argv) { char *group = NULL; int opt; coap_log_t log_level = LOG_WARNING; +#ifndef _WIN32 + struct sigaction sa; +#endif while ((opt = getopt(argc, argv, "A:g:p:v:")) != -1) { switch (opt) { @@ -656,6 +664,20 @@ main(int argc, char **argv) { init_resources(ctx); +#ifdef _WIN32 + signal(SIGINT, handle_sigint); +#else + memset (&sa, 0, sizeof(sa)); + sigemptyset(&sa.sa_mask); + sa.sa_handler = handle_sigint; + sa.sa_flags = 0; + sigaction (SIGINT, &sa, NULL); + sigaction (SIGTERM, &sa, NULL); + /* So we do not exit on a SIGPIPE */ + sa.sa_handler = SIG_IGN; + sigaction (SIGPIPE, &sa, NULL); +#endif + while ( !quit ) { result = coap_run_once( ctx, COAP_RESOURCE_CHECK_TIME * 1000 ); if ( result >= 0 ) { diff --git a/examples/coap-server.c b/examples/coap-server.c index 4723d2bc..1f9c4001 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -983,6 +983,9 @@ main(int argc, char **argv) { sa.sa_flags = 0; sigaction (SIGINT, &sa, NULL); sigaction (SIGTERM, &sa, NULL); + /* So we do not exit on a SIGPIPE */ + sa.sa_handler = SIG_IGN; + sigaction (SIGPIPE, &sa, NULL); #endif wait_ms = COAP_RESOURCE_CHECK_TIME * 1000; diff --git a/examples/etsi_iot_01.c b/examples/etsi_iot_01.c index 457926d5..8af8a19c 100644 --- a/examples/etsi_iot_01.c +++ b/examples/etsi_iot_01.c @@ -662,6 +662,9 @@ main(int argc, char **argv) { sa.sa_flags = 0; sigaction (SIGINT, &sa, NULL); sigaction (SIGTERM, &sa, NULL); + /* So we do not exit on a SIGPIPE */ + sa.sa_handler = SIG_IGN; + sigaction (SIGPIPE, &sa, NULL); while ( !quit ) { result = coap_run_once( ctx, COAP_RESOURCE_CHECK_TIME * 1000 ); diff --git a/examples/tiny.c b/examples/tiny.c index f9609795..984be66c 100644 --- a/examples/tiny.c +++ b/examples/tiny.c @@ -148,6 +148,9 @@ main(int argc, char **argv) { sa.sa_flags = 0; sigaction (SIGINT, &sa, NULL); sigaction (SIGTERM, &sa, NULL); + /* So we do not exit on a SIGPIPE */ + sa.sa_handler = SIG_IGN; + sigaction (SIGPIPE, &sa, NULL); while ( !quit ) {