diff --git a/owlps-aggregator/owlps-aggregator.h b/owlps-aggregator/owlps-aggregator.h index bc5825e..4e1cb22 100644 --- a/owlps-aggregator/owlps-aggregator.h +++ b/owlps-aggregator/owlps-aggregator.h @@ -32,6 +32,7 @@ #define ERR_BAD_USAGE 3 // Bad program call #define ERR_PARSING_CONFIG_FILE 4 // Error reading the configuration file #define ERR_SENDING_INFO 5 // Error sending a message on a socket +#define ERR_CREATING_THREAD 6 // Error creating a thread /* Linked list storing data of each request */ @@ -89,14 +90,14 @@ void check_configuration(void) ; int read_loop(int sockfd) ; void got_request(owl_captured_request request) ; -void* monitor_requests(void) ; +void* monitor_requests(void *NULL_value) ; void free_request_list(void) ; #ifdef DEBUG void print_request_list(void) ; void print_request_info(request_info_list *info) ; #endif // DEBUG -void listen_for_aps(void) ; +void* listen_for_aps(void *NULL_value) ; void update_ap(uint8_t mac_addr_bytes[6], char ip_addr[16]) ; ap_list* find_ap(uint8_t mac_addr_bytes[6]) ; ap_list* add_ap_front(uint8_t mac_addr_bytes[6]) ; @@ -104,7 +105,7 @@ void update_ap_ip_addr(ap_list *ap, char ip_addr[16]) ; void update_ap_seen(ap_list *ap) ; void push_ap(ap_list *ap) ; -void monitor_aps(void) ; +void* monitor_aps(void *NULL_value) ; void delete_old_aps(void) ; void delete_ap(ap_list *ap) ; void unlink_ap(ap_list *ap) ; diff --git a/owlps-aggregator/owlps-aggregatord.c b/owlps-aggregator/owlps-aggregatord.c index 565f4e8..a7eac50 100644 --- a/owlps-aggregator/owlps-aggregatord.c +++ b/owlps-aggregator/owlps-aggregatord.c @@ -51,19 +51,70 @@ int main(int argc, char **argv) } /* Set up threads */ - pthread_create(&monitor_thread, NULL, - (void *) &monitor_requests, NULL) ; + ret = pthread_create(&monitor_thread, NULL, &monitor_requests, NULL) ; + if (ret != 0) + { + perror("Cannot create monitor thread") ; + ret = ERR_CREATING_THREAD ; + goto exit ; + } if (cfg_getbool(cfg, "autocalibration")) { - pthread_create(&autocalibration_hello_thread, NULL, - (void *) &listen_for_aps, NULL) ; - pthread_create(&monitor_aps_thread, NULL, - (void *) &monitor_aps, NULL) ; + ret = pthread_create(&autocalibration_hello_thread, NULL, + &listen_for_aps, NULL) ; + if (ret != 0) + { + perror("Cannot create autocalibration hello thread") ; + ret = ERR_CREATING_THREAD ; + goto exit ; + } + ret = pthread_create(&monitor_aps_thread, NULL, + &monitor_aps, NULL) ; + if (ret != 0) + { + perror("Cannot create monitor APs thread") ; + ret = ERR_CREATING_THREAD ; + goto exit ; + } } run = TRUE ; ret = read_loop(sockfd) ; + /* Wait for the threads to terminate */ + + fprintf(stderr, "Waiting for the monitor thread... ") ; + if (pthread_join(monitor_thread, NULL) != 0) + perror("Cannot join monitor thread") ; + else + fprintf(stderr, "OK.\n") ; + + if (cfg_getbool(cfg, "autocalibration")) + { + // We must cancel this thread because it can be blocked on the + // recvfrom() call: + fprintf(stderr, + "Cancelling the autocalibration hello thread... ") ; + if (pthread_cancel(autocalibration_hello_thread) != 0) + perror("Cannot cancel autocalibration hello thread") ; + else + fprintf(stderr, "OK.\n") ; + + fprintf(stderr, + "Waiting for the autocalibration hello thread... ") ; + if (pthread_join(autocalibration_hello_thread, NULL) != 0) + perror("Cannot join autocalibration hello thread") ; + else + fprintf(stderr, "OK.\n") ; + + fprintf(stderr, "Waiting for the monitor APs thread... ") ; + if (pthread_join(monitor_aps_thread, NULL) != 0) + perror("Cannot join monitor APs thread") ; + else + fprintf(stderr, "OK.\n") ; + } + + /* Last cleaning tasks */ exit: (void) close(sockfd) ; // Close socket free_request_list() ; @@ -419,7 +470,7 @@ int read_loop(int sockfd) * Thread function. Monitors the list and sends information to the * localisation server when the timeout is reached. */ -void* monitor_requests() +void* monitor_requests(void *NULL_value) { request_list *request_ptr, *request_prev ; request_info_list *request_info_ptr ; @@ -605,7 +656,7 @@ void* monitor_requests() perror("Error closing output file") ; (void) close(sockfd) ; - return NULL ; + pthread_exit(NULL_value) ; } @@ -757,7 +808,7 @@ void free_request_list() /* * Thread function. Listens for hello messages from APs. */ -void listen_for_aps(void) +void* listen_for_aps(void *NULL_value) { int listen_sockfd ; int nread ; // recvfrom return value @@ -803,6 +854,8 @@ void listen_for_aps(void) } (void) close(listen_sockfd) ; + + pthread_exit(NULL_value) ; } @@ -919,7 +972,7 @@ void push_ap(ap_list *ap) * Monitors the AP list: sends orders to APs following their order in * the list, and deletes old APs. */ -void monitor_aps() +void* monitor_aps(void *NULL_value) { #ifdef DEBUG fprintf(stderr, "Monitor AP thread launched.\n") ; @@ -937,6 +990,8 @@ void monitor_aps() usleep(cfg_getint(cfg, "ap_check_interval") * 1000) ; } + + pthread_exit(NULL_value) ; }