From: alex <alex@infiniteadaptability.org>
Date: Thu, 28 Apr 2022 00:34:18 +0000 (-0700)
Subject: ...
X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=f44333dcb46bc347195ebd0297b4fca2172e334a;p=seeder

...
---

diff --git a/Makefile.am b/Makefile.am
index 978cc66..cb77b55 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -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 \
diff --git a/inc/init.h b/inc/init.h
index 59e1f91..eb3007e 100644
--- a/inc/init.h
+++ b/inc/init.h
@@ -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[];
diff --git a/inc/log.h b/inc/log.h
index db677ea..acc2b30 100644
--- 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
diff --git a/inc/session.h b/inc/session.h
index 70109da..2b3b404 100644
--- a/inc/session.h
+++ b/inc/session.h
@@ -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
index 950da59..0000000
--- a/inc/setup.h
+++ /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
diff --git a/src/init.c b/src/init.c
index ab4514c..7b03d6c 100644
--- a/src/init.c
+++ b/src/init.c
@@ -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;
+}
diff --git a/src/log.c b/src/log.c
index 211e2a6..4572150 100644
--- 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;
+}
diff --git a/src/session.c b/src/session.c
index fa843d1..a9659b6 100644
--- a/src/session.c
+++ b/src/session.c
@@ -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
index 6eccddd..0000000
--- a/src/setup.c
+++ /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
index 508464c..0000000
Binary files a/test/integration/basic.test and /dev/null differ