From 1bbccc2bb07109007fecaf8007a6552d056edb66 Mon Sep 17 00:00:00 2001 From: Bui Nguyen Quoc Thanh Date: Wed, 23 Sep 2020 10:30:07 +0700 Subject: libdlt: Use SIGUSR1 for thread on Android Since bionic does not support pthread_cancel(), try to terminate housekeeper thread via SIGUSR1. Signed-off-by: Bui Nguyen Quoc Thanh --- src/lib/dlt_user.c | 35 +++++++++++++++++++++++++++++++++-- src/lib/dlt_user_cfg.h | 6 +++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/lib/dlt_user.c b/src/lib/dlt_user.c index f1c1864..795bfbd 100644 --- a/src/lib/dlt_user.c +++ b/src/lib/dlt_user.c @@ -3621,13 +3621,31 @@ static void dlt_user_cleanup_handler(void *arg) void dlt_user_housekeeperthread_function(__attribute__((unused)) void *ptr) { struct timespec ts; + bool in_loop = true; + +#ifdef __ANDROID_API__ + sigset_t set; + sigset_t pset; + /* + * bionic is not supporting pthread_cancel so + * use SIGUSR1 to kill thread properly. + */ + sigemptyset(&set); + sigaddset(&set, SIGUSR1); + if (pthread_sigmask(SIG_BLOCK, &set, NULL) != 0) { + dlt_vlog(LOG_ERR, "Failed to block signal with error [%s]\n", + strerror(errno)); + in_loop = false; + } +#endif + #ifdef linux prctl(PR_SET_NAME, "dlt_housekeeper", 0, 0, 0); #endif pthread_cleanup_push(dlt_user_cleanup_handler, NULL); - while (1) { + while (in_loop) { /* Check for new messages from DLT daemon */ if (!dlt_user.disable_injection_msg) if (dlt_user_log_check_user_message() < DLT_RETURN_OK) @@ -3640,6 +3658,19 @@ void dlt_user_housekeeperthread_function(__attribute__((unused)) void *ptr) /* flush buffer to DLT daemon if possible */ if (dlt_user.dlt_log_handle != DLT_FD_INIT) dlt_user_log_resend_buffer(); + +#ifdef __ANDROID_API__ + if (sigpending(&pset)) { + dlt_vlog(LOG_ERR, "sigpending failed with error [%s]!\n", strerror(errno)); + break; + } + + if (sigismember(&pset, SIGUSR1)) { + dlt_log(LOG_NOTICE, "Received SIGUSR1! Stop thread\n"); + break; + } +#endif + /* delay */ ts.tv_sec = 0; ts.tv_nsec = DLT_USER_RECEIVE_NDELAY; @@ -4796,7 +4827,7 @@ void dlt_stop_threads() #ifdef DLT_NETWORK_TRACE_ENABLE dlt_lock_mutex(&mq_mutex); #endif /* DLT_NETWORK_TRACE_ENABLE */ - dlt_housekeeperthread_result = pthread_kill(dlt_housekeeperthread_handle, SIGKILL); + dlt_housekeeperthread_result = pthread_kill(dlt_housekeeperthread_handle, SIGUSR1); dlt_user_cleanup_handler(NULL); #endif diff --git a/src/lib/dlt_user_cfg.h b/src/lib/dlt_user_cfg.h index 93d3206..d394daa 100644 --- a/src/lib/dlt_user_cfg.h +++ b/src/lib/dlt_user_cfg.h @@ -123,12 +123,12 @@ /* default message id for non-verbose mode, if no message id was provided */ #define DLT_USER_DEFAULT_MSGID 0xffff -/* delay for housekeeper thread (nsec) while receiving messages*/ -#define DLT_USER_RECEIVE_NDELAY (500 * 1000 * 1000) - /* timeout for poll operations in milliseconds*/ #define DLT_USER_RECEIVE_MDELAY (500) +/* delay for housekeeper thread (nsec) while receiving messages*/ +#define DLT_USER_RECEIVE_NDELAY (DLT_USER_RECEIVE_MDELAY * 1000 * 1000) + /* Name of environment variable for local print mode */ #define DLT_USER_ENV_LOCAL_PRINT_MODE "DLT_LOCAL_PRINT_MODE" -- cgit v1.2.1