...
authoralex <[email protected]>
Sat, 11 Sep 2021 22:09:51 +0000 (15:09 -0700)
committeralex <[email protected]>
Sat, 11 Sep 2021 22:09:51 +0000 (15:09 -0700)
Makefile.am
inc/log.h
inc/main.h
inc/shutdown.h [new file with mode: 0644]
src/log.c
src/main.c
src/setup.c
src/shutdown.c [new file with mode: 0644]
test/unit/test_utils.c
test/unit/test_utils.h
test/unit/util.filter.tests.c

index f165ee12b6cb064df9e4d0409313b981932bebe8..8e6ca7afd40742ec43f30d8bf1271ad483720f7b 100644 (file)
@@ -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 \
index 274d862533ab21aaef9b03f19b762d16813440c9..3e8cefa268bd2a66c4928d7630a8c11374fc19e1 100644 (file)
--- 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*);
 
index fa0457706a56e9dded51e3af242058912b0d595c..b061624e614746ade5c8cd44c6cb10a29ebf44bc 100644 (file)
@@ -8,6 +8,7 @@
 #include<default.h>
 #include<log.h>
 #include<setup.h>
+#include<shutdown.h>
 #include<usage.h>
 
 #define MAIN_SHUTDOWN_MESSAGE "shutting down...\n"
diff --git a/inc/shutdown.h b/inc/shutdown.h
new file mode 100644 (file)
index 0000000..9b39dcc
--- /dev/null
@@ -0,0 +1,15 @@
+#ifndef __SHUTDOWN_H_
+#define __SHUTDOWN_H_
+
+#include<pthread.h>
+#include<stdio.h>
+#include<stdlib.h>
+
+#include<log.h>
+
+extern pthread_mutex_t shutdown_mutex;
+
+int shutdown_register();
+void shutdown();
+
+#endif
index b2ffe78dc24512f57387f4f6d2a42aad4af0f422..59417f71d9f2d315a9dd453cb1805fde5d0f58a0 100644 (file)
--- 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;
 }
index f7d7ff3bd6766ba1e883d35dc4434bf3ce6d7b3b..0cf97cbd8bf9c8294e4076613c8fff5bb2c5adcc 100644 (file)
@@ -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; }
index 983a26da159063f2869128fe687cedd919ea5c24..9c86d6a283f81c14be7e9d2e2de69ebeb0f29665 100644 (file)
@@ -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 (file)
index 0000000..3a0f50a
--- /dev/null
@@ -0,0 +1,14 @@
+#include<shutdown.h>
+
+int shutdown_register() {
+       if(0!=atexit(&shutdown)) {
+               perror("atexit");
+               return -1;
+       }
+
+       return 1;
+}
+
+void shutdown() {
+       pthread_cancel(logging_thread);
+}
index e068038d5cc46fcfb88da86d5b23d8aaaa283030..4390381b4702d93ece3d56eb21728cad7357b3a6 100644 (file)
@@ -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);
index 3976831a778aa9ece576f7c809ea0d3c266ed2b3..3d49ff88d457028d3e8481a2489da34a547dcb5d 100644 (file)
@@ -7,6 +7,8 @@
 #include<setup.h>
 
 void clean_env();
+void create_test_directory(const char*);
+void create_test_file(const char*,const char*);
 void reset_env();
 void setup_env();
 
index 73f5566c9647aa58af8ddd5dcde2cb2762840079..bd11dcf14bdc9e7705f96fb5cb4a1b75f812272e 100644 (file)
@@ -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);
 }