From 5f9de6e1dc5c9f5304842dece74fe935493117c5 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Thu, 24 Mar 2011 14:57:33 +0100 Subject: [PATCH] [Listener] Fix the "don't want to quit" bug In capture(), the pcap_loop() call was blocking, so the program did not quit if no packet came on rtap_iface. This is now fixed by a call to pcap_breakloop() in the signal handlers. --- TODO | 5 ----- owlps-listener/owlps-listener.h | 3 +++ owlps-listener/owlps-listenerd.c | 33 ++++++++++++++++++++++++-------- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/TODO b/TODO index 6b26fef..0c19b03 100644 --- a/TODO +++ b/TODO @@ -35,11 +35,6 @@ - read_packet(): use ieee80211_header_size for all implicit packets Currently the size is corrected only for data packets. -- Fix the "don't want to quit" bug - Sometimes owlps-listener refuses to quit on SIGTERM. This is - because of the blocking call of pcap_loop() in capture(): something - has to be read on rtap_iface before to quit. Maybe we should thread - capture(), to be able to pthread_cancel() it. - Fix segfault when rtap_iface is not in monitor mode (?) on Foneras. - Merge Makefile and Makefile_atheros? Use autohell? diff --git a/owlps-listener/owlps-listener.h b/owlps-listener/owlps-listener.h index bf85999..c6e31f4 100644 --- a/owlps-listener/owlps-listener.h +++ b/owlps-listener/owlps-listener.h @@ -97,6 +97,9 @@ void send_autocalibration_request(void) ; uint_fast16_t make_packet(uint8_t **packet) ; #endif // USE_PTHREAD +void sigint_handler(int num) ; +void sigterm_handler(int num) ; + void print_usage(void) ; void print_version(void) ; diff --git a/owlps-listener/owlps-listenerd.c b/owlps-listener/owlps-listenerd.c index 13c2b85..1f891d4 100644 --- a/owlps-listener/owlps-listenerd.c +++ b/owlps-listener/owlps-listenerd.c @@ -11,6 +11,7 @@ char *program_name = NULL ; uint8_t my_mac_bytes[6] ; // AP MAC address char my_ip[INET_ADDRSTRLEN] ; // AP IP address +pcap_t *capture_handler = NULL ; // Packet capture descriptor int aggregation_sockfd ; struct sockaddr_in aggregation_server ; int autocalibration_send_sockfd ; @@ -89,9 +90,9 @@ int main(int argc, char *argv[]) /* Set up signal handlers */ sigemptyset(&action.sa_mask) ; - action.sa_handler = owl_sigint_handler ; + action.sa_handler = sigint_handler ; sigaction(SIGINT, &action, NULL) ; - action.sa_handler = owl_sigterm_handler ; + action.sa_handler = sigterm_handler ; sigaction(SIGTERM, &action, NULL) ; get_mac_addr(GET_WIFI_IFACE(), my_mac_bytes) ; @@ -585,12 +586,12 @@ void* keep_mode_monitor(void *iface) */ int capture() { - pcap_t *handle ; // Packet capture descriptor char errbuf[PCAP_ERRBUF_SIZE] ; // Error message // Start capture: - handle = pcap_open_live(GET_RTAP_IFACE(), BUFSIZ, 1, 1000, errbuf) ; - if (handle == NULL) // Capture starting failed + capture_handler = + pcap_open_live(GET_RTAP_IFACE(), BUFSIZ, 1, 1000, errbuf) ; + if (capture_handler == NULL) // Capture starting failed { fprintf(stderr, "Cannot open capture interface %s\n", errbuf) ; return ERR_OPENING_IFACE ; @@ -602,11 +603,11 @@ int capture() GET_AGGREGATION_PORT(), &aggregation_server, NULL) ; - while(run) + while (run) // Capture one packet at time, and call read_packet() on it: - pcap_loop(handle, 1, read_packet, NULL) ; + pcap_loop(capture_handler, 1, read_packet, NULL) ; - pcap_close(handle) ; // Stop capture + pcap_close(capture_handler) ; // Stop capture close(aggregation_sockfd) ; // Close socket return 0 ; @@ -1176,6 +1177,22 @@ uint_fast16_t make_packet(uint8_t **packet) +void sigint_handler(int num) +{ + owl_sigint_handler(num) ; + pcap_breakloop(capture_handler) ; +} + + + +void sigterm_handler(int num) +{ + owl_sigterm_handler(num) ; + pcap_breakloop(capture_handler) ; +} + + + void print_usage() { printf("Usage :\n"