...
authoralex <[email protected]>
Thu, 28 Apr 2022 00:34:18 +0000 (17:34 -0700)
committeralex <[email protected]>
Thu, 28 Apr 2022 00:34:18 +0000 (17:34 -0700)
Makefile.am
inc/init.h
inc/log.h
inc/session.h
inc/setup.h [deleted file]
src/init.c
src/log.c
src/session.c
src/setup.c [deleted file]
test/integration/basic.test [deleted file]

index 978cc666a301c41c3b9942d37bcbae03d5fcbbfa..cb77b55f2a432657d966e4cec64870fcecdd66c4 100644 (file)
@@ -81,7 +81,6 @@ seederd_SOURCES = \
        src/rss/info.c \
        src/rss/init.c \
        src/session.c \
-       src/setup.c \
        src/torrent/add.c \
        src/torrent/file.c \
        src/torrent/free.c \
@@ -116,7 +115,6 @@ seederd_SOURCES += \
        inc/pqueue.h \
        inc/rss.h \
        inc/session.h \
-       inc/setup.h \
        inc/torrent.h \
        inc/tree.h \
        inc/usage.h \
index 59e1f910d7ce69239cb3010d5e46a68964466c71..eb3007e39f3da65b771ad4d1aab307117849eb69 100644 (file)
@@ -2,10 +2,11 @@
 #define __INIT_H_
 
 #include<getopt.h>
+#include<signal.h>
 
 #include<default.h>
-#include<setup.h>
-#include<shutdown.h>
+#include<log.h>
+#include<session.h>
 #include<usage.h>
 
 extern struct option long_options[];
index db677eaf0a1ae207b5159945499b6f804ffe2ef7..acc2b308ad1043e43e33552a87aed69cc6757784 100644 (file)
--- a/inc/log.h
+++ b/inc/log.h
@@ -51,9 +51,8 @@ struct log_helper {
        size_t next;
 };
 
-int log_entries_init();
 void log_message(enum log_level,FILE*,const char*,...);
 void *log_poll(void*);
-void log_shutdown();
+int logging_setup();
 
 #endif
index 70109da0ce388e626dee3c9b3836803f6ad40a36..2b3b4049bbed4992cddf8e089d75c71a3b62586d 100644 (file)
@@ -19,9 +19,9 @@ struct session {
 
 extern struct session session;
 
-void session_clean();
 struct torrent *session_find_torrent(uint8_t*,size_t);
 int session_init();
+int session_setup();
 int session_torrent_add(struct torrent*);
 
 #endif
diff --git a/inc/setup.h b/inc/setup.h
deleted file mode 100644 (file)
index 950da59..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-#ifndef __SETUP_H_
-#define __SETUP_H_
-
-#include<log.h>
-#include<session.h>
-
-int setup_logging();
-int setup_session();
-
-#endif
index ab4514c12de8b52418502872643ea2894c9e1fdd..7b03d6ca3f2ec95f6ee45f8f8e52af07b9f6d795 100644 (file)
@@ -19,15 +19,19 @@ struct option long_options[] = {
        {0,0,0,0}
 };
 
+static int signal_handler();
+
 int init(int argc, char **argv) {
        int c;
+
+       if(signal_handler()<0) { return -1; }
        
-       if(setup_session()<0) { return -1; }
+       if(session_setup()<0) { return -1; }
 
        if(defaults()<0) { return -1; }
        if(opt_load_from_env()<0) { return -1; }
 
-       if(setup_logging()<0) { return -1; }
+       if(logging_setup()<0) { return -1; }
 
        while(1) {
                int option_index = 0;
@@ -77,3 +81,24 @@ int init(int argc, char **argv) {
 
        return 1;
 }
+
+static void handle_interrupt() {
+       /*
+        * call exit explicitly will call functions registered
+        * with atexit and do appropriate cleanup.
+        */
+       exit(EXIT_FAILURE);
+}
+
+static int signal_handler() {
+       struct sigaction action;
+
+       action.sa_handler = &handle_interrupt;
+       action.sa_flags = SA_RESETHAND;
+       if(sigaction(SIGINT,&action,NULL)!=0) {
+               perror("sigaction");
+               return -1;
+       }
+
+       return 1;
+}
index 211e2a66fbce240de86f0c94630ba9c8515e64eb..4572150d0f8464299a01f089b8a388ecc73b745f 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -1,13 +1,18 @@
 #include<log.h>
 
+pthread_t logging_thread;
+pthread_mutex_t logging_mutex = PTHREAD_MUTEX_INITIALIZER;
+
 static struct log_helper helper;
 
 static struct log_entry *log_dequeue();
 static void log_enqueue(struct log_entry*);
 static void log_entries_clean(void*);
+static int log_entries_init();
 static void log_flush(void*);
 static void log_print();
 static void log_print_prefix(FILE*);
+static void log_shutdown();
 
 static struct log_entry *log_dequeue() {
        // requires logging_mutex to be locked
@@ -37,7 +42,11 @@ static void log_enqueue(struct log_entry *p) {
        pthread_mutex_unlock(&logging_mutex);
 }
 
-int log_entries_init() {
+static void log_entries_clean(void *p) {
+       free(helper.p);
+}
+
+static int log_entries_init() {
        helper.start = NULL;
        helper.end = NULL;
        helper.next = 0;
@@ -51,10 +60,6 @@ int log_entries_init() {
        return 1;
 }
 
-static void log_entries_clean(void *p) {
-       free(helper.p);
-}
-
 static void log_flush(void *p) {
        pthread_mutex_lock(&logging_mutex);
 
@@ -158,7 +163,7 @@ void *log_poll(void *p) {
        return NULL;
 }
 
-void log_shutdown() {
+static void log_shutdown() {
        int ret_cancel, ret_join;
        void *res;
                
@@ -172,3 +177,19 @@ void log_shutdown() {
                log_msg(LOG_MESSAGE_SHUTDOWN_SUCCESS);
        }
 }
+
+int logging_setup() {
+       if(log_entries_init()<0) { return -1; }
+
+       if(pthread_create(&logging_thread,NULL,&log_poll,NULL)!=0) {
+               perror("pthread_create");
+               return -1;
+       }
+
+       if(0!=atexit(&log_shutdown)) {
+               perror("atexit");
+               return -1;
+       }
+
+       return 1;
+}
index fa843d1b273b945023e24d9a82ad447e46678b45..a9659b6f1d9f760fde7f4ac14ce730b5d15c44b6 100644 (file)
@@ -2,11 +2,12 @@
 
 struct session session;
 
+static void session_clean();
 static int session_torrent_add_by_infohash(struct torrent*);
 static int session_torrent_add_by_path(struct torrent*);
 static int session_torrent_resize(struct hash_map**,size_t);
 
-void session_clean() {
+static void session_clean() {
        struct torrent *p;
 
        for(size_t i=0;i<session.torrents.paths->size;i++) {
@@ -34,6 +35,17 @@ int session_init() {
        return 1;
 }
 
+int session_setup() {
+       if(session_init(&session)<0) { return -1; }
+
+       if(0!=atexit(&session_clean)) {
+               perror("atexit");
+               return -1;
+       }
+
+       return 1;
+}
+
 int session_torrent_add(struct torrent *p) {
        int i;
 
diff --git a/src/setup.c b/src/setup.c
deleted file mode 100644 (file)
index 6eccddd..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-#include<setup.h>
-
-pthread_t logging_thread;
-pthread_mutex_t logging_mutex = PTHREAD_MUTEX_INITIALIZER;
-
-int setup_logging() {
-       if(log_entries_init()<0) { return -1; }
-       if(pthread_create(&logging_thread,NULL,&log_poll,NULL)!=0) {
-               perror("pthread_create");
-               return -1;
-       }
-
-       if(0!=atexit(&log_shutdown)) {
-               perror("atexit");
-               return -1;
-       }
-
-       return 1;
-}
-
-int setup_session() {
-       if(session_init(&session)<0) { return -1; }
-
-       if(0!=atexit(&session_clean)) {
-               perror("atexit");
-               return -1;
-       }
-
-       return 1;
-}
diff --git a/test/integration/basic.test b/test/integration/basic.test
deleted file mode 100755 (executable)
index 508464c..0000000
Binary files a/test/integration/basic.test and /dev/null differ