From 626242b5e2712deea57cf8210d5d6a61be12d0f4 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Sat, 20 Aug 2011 11:10:12 +0200 Subject: [PATCH] [lib-result] example: handle signals Handle signals in the sample program owlps-resultreader-udp. --- .../owlps-resultreader-udp.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/libowlps-resultreader-udp/owlps-resultreader-udp.c b/libowlps-resultreader-udp/owlps-resultreader-udp.c index a0cffc0..69b6e25 100644 --- a/libowlps-resultreader-udp/owlps-resultreader-udp.c +++ b/libowlps-resultreader-udp/owlps-resultreader-udp.c @@ -7,18 +7,31 @@ #include "owlps-resultreader-udp.h" +#include #include int main(void) { + struct sigaction action ; // Signal handler structure owl_result *result ; int sockfd ; + /* Set up signal handlers */ + action.sa_flags = 0 ; + sigemptyset(&action.sa_mask) ; + action.sa_handler = owl_sigint_handler ; + sigaction(SIGINT, &action, NULL) ; + action.sa_handler = owl_sigterm_handler ; + sigaction(SIGTERM, &action, NULL) ; + + /* Open the socket */ sockfd = owl_create_udp_listening_socket(MOBILE_DEFAULT_PORT) ; if (sockfd < 0) return 1 ; - while (1) + /* Read loop */ + owl_run = TRUE ; + while (owl_run) { if (! (result = owl_receive_position(sockfd))) return 1 ; @@ -27,6 +40,7 @@ int main(void) printf("--------------\n") ; } + /* Cleaning */ close(sockfd) ; return 0 ;