]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Thu, 20 Jan 2022 08:16:38 +0000 (00:16 -0800)
committeralex <[email protected]>
Thu, 20 Jan 2022 08:16:38 +0000 (00:16 -0800)
15 files changed:
Makefile.am
inc/log.h
inc/net.h
inc/opt.h
src/default.c
src/init.c
src/log.c
src/main.c
src/net/start.c
src/opt/env.c
src/opt/port.c [new file with mode: 0644]
src/opt/set.c
src/setup.c
test/unit/Makefile.am
test/unit/opt.tests.c

index 49ef6d55b768fc250a25715011867dc07c9be2d0..7fd0a108195b4268b7bea4b229e58492a55e8445 100644 (file)
@@ -41,6 +41,7 @@ seederd_SOURCES = \
        src/opt/loglevel.c \
        src/opt/out.c \
        src/opt/piecel.c \
+       src/opt/port.c \
        src/opt/set.c \
        src/opt/watch.c \
        src/opt/worker.c \
@@ -52,7 +53,6 @@ seederd_SOURCES = \
        src/rss/init.c \
        src/session.c \
        src/setup.c \
-       src/shutdown.c \
        src/torrent/add.c \
        src/torrent/file.c \
        src/torrent/free.c \
@@ -85,7 +85,6 @@ seederd_SOURCES += \
        inc/rss.h \
        inc/session.h \
        inc/setup.h \
-       inc/shutdown.h \
        inc/torrent.h \
        inc/tree.h \
        inc/usage.h \
index 3e8cefa268bd2a66c4928d7630a8c11374fc19e1..db677eaf0a1ae207b5159945499b6f804ffe2ef7 100644 (file)
--- a/inc/log.h
+++ b/inc/log.h
@@ -29,8 +29,10 @@ enum log_level {
 #define log_info(...) log_message(LOG_LEVEL_VERBOSE,stdout,__VA_ARGS__)
 #define log_msg(...) log_message(LOG_LEVEL_DEFAULT,stdout,__VA_ARGS__)
 
-#define LOG_FLUSH_MESSAGE "flushing log queue...\n"
-#define LOG_THREAD_START_MESSAGE "logging thread start\n"
+#define LOG_MESSAGE_FLUSH "flushing log queue...\n"
+#define LOG_MESSAGE_SHUTDOWN_FAILED "failed to shutdown logging thread successfully\nlog entries might be missing\n"
+#define LOG_MESSAGE_SHUTDOWN_SUCCESS "shutdown logging thread\n"
+#define LOG_MESSAGE_THREAD_START "logging thread start\n"
 #define LOG_ENTRY_MAX_LENGTH 100
 #define LOG_QUEUE_SIZE 100
 #define TIMESTAMP_BUF_LENGTH 20
@@ -52,5 +54,6 @@ struct log_helper {
 int log_entries_init();
 void log_message(enum log_level,FILE*,const char*,...);
 void *log_poll(void*);
+void log_shutdown();
 
 #endif
index b90aea552accac4801a0af8f529bf20e208c19e9..9d19b32045112214d0297b797dfc6307f06cdafe 100644 (file)
--- a/inc/net.h
+++ b/inc/net.h
@@ -1,6 +1,8 @@
 #ifndef __NET_H_
 #define __NET_H_
 
+#include<opt.h>
+
 #include<netdb.h>
 #include<stdio.h>
 #include<stdlib.h>
index 8c609002d33973b7219df51a3d7a79b1a6005fbd..b7d968f0c7df338e97ece980965310b93b03c564 100644 (file)
--- a/inc/opt.h
+++ b/inc/opt.h
@@ -19,6 +19,7 @@ struct options {
        char *feed_url;
        enum file_filters file_filter;
        unsigned long long piece_length;
+       char *port;
        int verbose_flag;
        unsigned int worker_threads;
 };
@@ -30,10 +31,12 @@ extern struct options global_options;
 #define OPT_MESSAGE_ENV_LOADING_END "finished loading settings from environment\n"
 #define OPT_MESSAGE_FILE_FILTER_SET "set file filter to %s\n"
 #define OPT_MESSAGE_FILE_FILTER_UNKNOWN "unknown file filter: %s\n"
+#define OPT_MESSAGE_INVALID_PORT "invalid port: %s\n"
 #define OPT_MESSAGE_LOADING_CONFIG_FILE "loading config file %s\n"
 #define OPT_MESSAGE_OUT_REDIRECT "redirecting output to %s\n"
 #define OPT_MESSAGE_PIECE_LENGTH_INVALID "invalid piece length of %llu\npiece length must be >16384 and a power of 2\n"
 #define OPT_MESSAGE_PIECE_LENGTH_SET "piece length set to %llu\n"
+#define OPT_MESSAGE_PORT_SET "port set to %lu\n"
 #define OPT_MESSAGE_UNABLE_OPEN_FILE "unable to open file %s\n"
 #define OPT_MESSAGE_UNKNOWN_OPTION "unknown option %s\n"
 #define OPT_MESSAGE_WATCH_ADD_SUCCESS "adding all files in %s to torrent with name %s\n"
@@ -50,6 +53,7 @@ int opt_set_file_filter(const char*);
 void opt_set_log_level(enum log_level);
 int opt_set_out_stream(const char*);
 int opt_set_piece_length(const char*);
+int opt_set_port(const char*);
 int opt_set_worker_threads(const char*);
 
 #endif
index 9ac4b16fdc307b7dc389995b8d60d1908eea247a..fd8bbae243e5be201d11bd87b32037c069ebc357 100644 (file)
@@ -18,6 +18,7 @@ int defaults() {
        if(opt_set_feed_url("")<0) { return -1; }
        if(opt_set_file_filter("default")<0) { return -1; }
        if(opt_set_piece_length("16384")<0) { return -1; }
+       if(opt_set_port("5150")<0) { return -1; }
        
        {
                char buf[10];
index 908bdb5be4f22beafaad4bbe12148149502ca19e..ab4514c12de8b52418502872643ea2894c9e1fdd 100644 (file)
@@ -11,6 +11,7 @@ struct option long_options[] = {
        {"file-filter", required_argument, 0, 'f'},
        {"help", no_argument, 0, 'h'},
        {"log-file", required_argument, 0, 'l'},
+       {"port", required_argument, 0, 'p'},
        {"quiet", no_argument, 0, 'q'},
        {"verbose", no_argument, 0, 'v'},
        {"watch-directory", required_argument, 0, 'w'},
@@ -21,17 +22,17 @@ struct option long_options[] = {
 int init(int argc, char **argv) {
        int c;
        
-       shutdown_register();
-
        if(setup_session()<0) { return -1; }
 
        if(defaults()<0) { return -1; }
        if(opt_load_from_env()<0) { return -1; }
 
+       if(setup_logging()<0) { return -1; }
+
        while(1) {
                int option_index = 0;
 
-               if((c = getopt_long(argc,argv,"c:df:hl:qu:vw:",long_options,&option_index))==-1) { break; }
+               if((c = getopt_long(argc,argv,"c:df:hl:p:qu:vw:",long_options,&option_index))==-1) { break; }
 
                switch(c) {
                        case 1:
@@ -53,6 +54,9 @@ int init(int argc, char **argv) {
                        case 'l':
                                if(opt_set_out_stream(optarg)<0) { return -1; }
                                break;
+                       case 'p':
+                               if(opt_set_port(optarg)<0) { return -1; }
+                               break;
                        case 'q':
                                opt_set_log_level(LOG_LEVEL_SILENT);
                                break;
index c6232f76ebef1c4af8ab72680e357bb6c672bde5..211e2a66fbce240de86f0c94630ba9c8515e64eb 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -59,7 +59,7 @@ static void log_flush(void *p) {
        pthread_mutex_lock(&logging_mutex);
 
        if(helper.start!=NULL) {        
-               fputs(LOG_FLUSH_MESSAGE,stderr);
+               fputs(LOG_MESSAGE_FLUSH,stderr);
        }
 
        while(helper.start!=NULL) {
@@ -143,7 +143,7 @@ void *log_poll(void *p) {
        pthread_cleanup_push(log_entries_clean,NULL);
        pthread_cleanup_push(log_flush,NULL);
 
-       log_info(LOG_THREAD_START_MESSAGE);
+       log_info(LOG_MESSAGE_THREAD_START);
        while(1) {
                pthread_testcancel();
                
@@ -157,3 +157,18 @@ void *log_poll(void *p) {
 
        return NULL;
 }
+
+void log_shutdown() {
+       int ret_cancel, ret_join;
+       void *res;
+               
+       ret_cancel = pthread_cancel(logging_thread);
+       ret_join = pthread_join(logging_thread,&res);
+
+       logging_thread = pthread_self();
+       if((ret_cancel!=0)||(ret_join!=0)||(res!=PTHREAD_CANCELED)) {
+               log_err(LOG_MESSAGE_SHUTDOWN_FAILED);
+       } else {
+               log_msg(LOG_MESSAGE_SHUTDOWN_SUCCESS);
+       }
+}
index 4f889897baf05a53e95535b4cb934a8fac8468a4..81272dfb5152cfe4db270eb35a407e6a20090ed3 100644 (file)
@@ -3,8 +3,6 @@
 int main(int argc, char **argv) {
        if(init(argc,argv)<0) { return EXIT_FAILURE; }
 
-       if(setup_logging()<0) { return EXIT_FAILURE; }
-
        if(add()<0) { return EXIT_FAILURE; }
        if(feeds()<0) { return EXIT_FAILURE; }
        if(watch()<0) { return EXIT_FAILURE; }
index a4c7dc7cf494247f468879251904ca4cf7fa9212..ccf5c7b690d814c8ce9b93c1c64841365b1a6bdf 100644 (file)
@@ -26,6 +26,11 @@ static int tcp() {
        sock_fd = listen_sock(&hints);
        if(sock_fd<0) { return -1; }
 
+       if(listen(sock_fd,MAX_BACKLOG)!=0) {
+               perror("listen");
+               return -1;
+       }
+
        return 1;
 }
 
@@ -52,7 +57,7 @@ static int listen_sock(struct addrinfo *hints) {
        struct addrinfo *p, *res;
        int sock_fd;
 
-       if(getaddrinfo(NULL,"5150",hints,&res)!=0) {
+       if(getaddrinfo(NULL,global_options.port,hints,&res)!=0) {
                perror("getaddrinfo");
                return -1;
        }
@@ -70,10 +75,5 @@ static int listen_sock(struct addrinfo *hints) {
 
        if(p==NULL) { return -1; }
 
-       if(listen(sock_fd,MAX_BACKLOG)!=0) {
-               perror("listen");
-               return -1;
-       }
-
        return sock_fd;
 }
index 5e33b89cb5c7f15e67d47cfde5b02dbf488f7385..f3fcbf631c0662674b59518c17a450792617d399 100644 (file)
@@ -19,6 +19,7 @@ int opt_load_from_env() {
        CHECK_ENV("FILE_FILTER",opt_set_file_filter);
        CHECK_ENV("LOG_FILE",opt_set_out_stream);
        CHECK_ENV("PIECE_LENGTH",opt_set_piece_length);
+       CHECK_ENV("PORT",opt_set_port);
        CHECK_ENV("WORKER_THREADS",opt_set_worker_threads);
 
        log_info(OPT_MESSAGE_ENV_LOADING_END);
diff --git a/src/opt/port.c b/src/opt/port.c
new file mode 100644 (file)
index 0000000..a1f4195
--- /dev/null
@@ -0,0 +1,24 @@
+#include<opt.h>
+
+int opt_set_port(const char *port_string) {
+       char *end;
+       unsigned long i;
+
+       if(NULL==port_string) { return -1; }
+
+       i = strtoul(port_string,&end,10);
+
+       if((i>65535)||((NULL!=end)&&(*end!='\0'))) {
+               log_err(OPT_MESSAGE_INVALID_PORT,port_string);
+               return -1;
+       }
+
+       if(global_options.port!=NULL) { free(global_options.port); }
+
+       global_options.port = strdup(port_string);
+       if(NULL==global_options.port) { return -1; }
+
+       log_info(OPT_MESSAGE_PORT_SET,i);
+
+       return 1;
+}
index 3a5108b509eaa083b0e540a99826b4c605fdbfd6..35fe0cb2306771cb0f09c82e986596ebb576b067 100644 (file)
@@ -12,6 +12,7 @@ struct option_lookup_table_entry option_lookup_table[] = {
        {"feed_url",&opt_set_feed_url},
        {"file_filter",&opt_set_file_filter},
        {"piece_length",&opt_set_piece_length},
+       {"port",&opt_set_port},
        {"watch_directory",&opt_add_watch},
        {"worker_threads",&opt_set_worker_threads},
        {NULL,NULL}
index 9c86d6a283f81c14be7e9d2e2de69ebeb0f29665..6eccdddbb321851b33387dc13c35be058120a747 100644 (file)
@@ -10,6 +10,11 @@ int setup_logging() {
                return -1;
        }
 
+       if(0!=atexit(&log_shutdown)) {
+               perror("atexit");
+               return -1;
+       }
+
        return 1;
 }
 
index dd21f44616b39600aec15c80583ca98477e742b6..b0db9117c88e998acc8eaac1b00fdc190d2b397c 100644 (file)
@@ -128,6 +128,7 @@ opt_tests_SOURCES = \
        $(top_srcdir)/src/opt/loglevel.c \
        $(top_srcdir)/src/opt/out.c \
        $(top_srcdir)/src/opt/piecel.c \
+       $(top_srcdir)/src/opt/port.c \
        $(top_srcdir)/src/opt/set.c \
        $(top_srcdir)/src/opt/watch.c \
        $(top_srcdir)/src/opt/worker.c \
index 70a95b373298336c3c241af1da79037c8dab9967..8de1d2441a951d4e467a0563865b81980000daaa 100644 (file)
@@ -14,6 +14,7 @@ static void opt_add_watch_basic_test();
 static void opt_set_feed_url_basic_test();
 static void opt_set_file_filter_basic_test();
 static void opt_set_piece_length_basic_test();
+static void opt_set_port_basic_test();
 static void opt_set_worker_threads_basic_test();
 
 int main() {
@@ -28,6 +29,7 @@ int main() {
        opt_set_feed_url_basic_test();
        opt_set_file_filter_basic_test();
        opt_set_piece_length_basic_test();
+       opt_set_port_basic_test();
        opt_set_worker_threads_basic_test();
 
        clean_env();
@@ -128,6 +130,19 @@ static void opt_set_piece_length_basic_test() {
        assert(1==defaults());
 }
 
+static void opt_set_port_basic_test() {
+       assert(-1==opt_set_port(NULL));
+       assert(-1==opt_set_port("lkndsaoifjasdo"));
+
+       assert(-1==opt_set_port("999999"));
+
+       assert(1==opt_set_port("0111"));
+       assert(1==opt_set_port("5150"));
+
+       assert(1==defaults());
+}
+
+
 static void opt_set_worker_threads_basic_test() {
        assert(-1==opt_set_worker_threads(NULL));
        assert(-1==opt_set_worker_threads("notanumber"));