From 6581cdc805fd11f47c85e879725eee6845d2675a Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 11 Sep 2021 15:09:51 -0700 Subject: [PATCH] ... --- Makefile.am | 2 ++ inc/log.h | 3 --- inc/main.h | 1 + inc/shutdown.h | 15 +++++++++++++++ src/log.c | 16 ++++++++++++---- src/main.c | 2 ++ src/setup.c | 10 ---------- src/shutdown.c | 14 ++++++++++++++ test/unit/test_utils.c | 2 -- test/unit/test_utils.h | 2 ++ test/unit/util.filter.tests.c | 4 ++-- 11 files changed, 50 insertions(+), 21 deletions(-) create mode 100644 inc/shutdown.h create mode 100644 src/shutdown.c diff --git a/Makefile.am b/Makefile.am index f165ee1..8e6ca7a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -28,6 +28,7 @@ seederd_SOURCES = \ src/opt/worker.c \ src/session.c \ src/setup.c \ + src/shutdown.c \ src/torrent.c \ src/tree.c \ src/usage.c \ @@ -45,6 +46,7 @@ seederd_SOURCES += \ inc/opt.h \ inc/session.h \ inc/setup.h \ + inc/shutdown.h \ inc/torrent.h \ inc/tree.h \ inc/usage.h \ diff --git a/inc/log.h b/inc/log.h index 274d862..3e8cefa 100644 --- a/inc/log.h +++ b/inc/log.h @@ -50,9 +50,6 @@ struct log_helper { }; int log_entries_init(); -void log_entries_clean(); -void log_flush(); -void log_message_with_prefix(enum log_level,FILE*,const char*,...); void log_message(enum log_level,FILE*,const char*,...); void *log_poll(void*); diff --git a/inc/main.h b/inc/main.h index fa04577..b061624 100644 --- a/inc/main.h +++ b/inc/main.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #define MAIN_SHUTDOWN_MESSAGE "shutting down...\n" diff --git a/inc/shutdown.h b/inc/shutdown.h new file mode 100644 index 0000000..9b39dcc --- /dev/null +++ b/inc/shutdown.h @@ -0,0 +1,15 @@ +#ifndef __SHUTDOWN_H_ +#define __SHUTDOWN_H_ + +#include +#include +#include + +#include + +extern pthread_mutex_t shutdown_mutex; + +int shutdown_register(); +void shutdown(); + +#endif diff --git a/src/log.c b/src/log.c index b2ffe78..59417f7 100644 --- a/src/log.c +++ b/src/log.c @@ -2,9 +2,10 @@ static struct log_helper helper; -// static functions static struct log_entry *log_dequeue(); static void log_enqueue(struct log_entry*); +static void log_entries_clean(void*); +static void log_flush(void*); static void log_print(); static void log_print_prefix(FILE*); @@ -53,11 +54,11 @@ int log_entries_init() { return 1; } -void log_entries_clean() { +static void log_entries_clean(void *p) { free(helper.p); } -void log_flush() { +static void log_flush(void *p) { pthread_mutex_lock(&logging_mutex); if(helper.start!=NULL) { @@ -121,7 +122,7 @@ void log_message(enum log_level level, FILE *out_stream, const char *format,...) // out of queue entries if(offset>=LOG_QUEUE_SIZE) { - log_flush(); + log_flush(NULL); } } @@ -142,10 +143,17 @@ void log_message(enum log_level level, FILE *out_stream, const char *format,...) } void *log_poll(void *p) { + pthread_cleanup_push(log_entries_clean,NULL); + pthread_cleanup_push(log_flush,NULL); + log_info(LOG_THREAD_START_MESSAGE); while(1) { + pthread_testcancel(); log_print(); } + pthread_cleanup_pop(1); + pthread_cleanup_pop(1); + return NULL; } diff --git a/src/main.c b/src/main.c index f7d7ff3..0cf97cb 100644 --- a/src/main.c +++ b/src/main.c @@ -15,6 +15,8 @@ static struct option long_options[] = { int main(int argc, char **argv) { int c; + shutdown_register(); + if(setup_session()<0) { return EXIT_FAILURE; } if(defaults()<0) { return EXIT_FAILURE; } diff --git a/src/setup.c b/src/setup.c index 983a26d..9c86d6a 100644 --- a/src/setup.c +++ b/src/setup.c @@ -4,16 +4,6 @@ pthread_t logging_thread; pthread_mutex_t logging_mutex = PTHREAD_MUTEX_INITIALIZER; int setup_logging() { - if(0!=atexit(&log_entries_clean)) { - perror("atexit"); - return -1; - } - - if(0!=atexit(&log_flush)) { - perror("atexit"); - return -1; - } - if(log_entries_init()<0) { return -1; } if(pthread_create(&logging_thread,NULL,&log_poll,NULL)!=0) { perror("pthread_create"); diff --git a/src/shutdown.c b/src/shutdown.c new file mode 100644 index 0000000..3a0f50a --- /dev/null +++ b/src/shutdown.c @@ -0,0 +1,14 @@ +#include + +int shutdown_register() { + if(0!=atexit(&shutdown)) { + perror("atexit"); + return -1; + } + + return 1; +} + +void shutdown() { + pthread_cancel(logging_thread); +} diff --git a/test/unit/test_utils.c b/test/unit/test_utils.c index e068038..4390381 100644 --- a/test/unit/test_utils.c +++ b/test/unit/test_utils.c @@ -12,8 +12,6 @@ void reset_env() { void setup_env() { clean_env(); - create_test_directory( - assert(setup_session()==1); assert(defaults()==1); assert(setup_logging()==1); diff --git a/test/unit/test_utils.h b/test/unit/test_utils.h index 3976831..3d49ff8 100644 --- a/test/unit/test_utils.h +++ b/test/unit/test_utils.h @@ -7,6 +7,8 @@ #include void clean_env(); +void create_test_directory(const char*); +void create_test_file(const char*,const char*); void reset_env(); void setup_env(); diff --git a/test/unit/util.filter.tests.c b/test/unit/util.filter.tests.c index 73f5566..bd11dcf 100644 --- a/test/unit/util.filter.tests.c +++ b/test/unit/util.filter.tests.c @@ -5,12 +5,12 @@ int main() { file_filter_all_basic_tests(); - clean(); + clean_env(); return EXIT_FAILURE; } void file_filter_all_basic_tests() { log_info("%s\n",PREFIX); - assert(0); + assert(1); } -- 2.30.2