From aab0046101fe3df6fc3b553d15325d5b23c29fa9 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Mon, 2 Aug 2010 18:36:25 +0200 Subject: [PATCH] handle SIGINT to enable profiling --- main.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index 88bfec6e..db5360b9 100644 --- a/main.c +++ b/main.c @@ -18,6 +18,7 @@ #include #include #include +#include #include "subscribe.h" #include "coap.h" @@ -39,6 +40,13 @@ /* temporary storage for dynamic resource representations */ static char resource_buf[20000]; +static int quit = 0; + +/* SIGINT handler: set quit to 1 for graceful termination */ +void +handle_sigint(int signum) { + quit = 1; +} coap_pdu_t * new_ack( coap_context_t *ctx, coap_queue_t *node ) { @@ -997,7 +1005,10 @@ main(int argc, char **argv) { coap_register_message_handler( ctx, message_handler ); init_resources(ctx); - while ( 1 ) { + + signal(SIGINT, handle_sigint); + + while ( !quit ) { FD_ZERO(&readfds); FD_SET( ctx->sockfd, &readfds ); @@ -1022,7 +1033,8 @@ main(int argc, char **argv) { result = select( FD_SETSIZE, &readfds, 0, 0, timeout ); if ( result < 0 ) { /* error */ - perror("select"); + if (errno != EINTR) + perror("select"); } else if ( result > 0 ) { /* read from socket */ if ( FD_ISSET( ctx->sockfd, &readfds ) ) { coap_read( ctx ); /* read received data */