From: alex Date: Thu, 20 Jan 2022 08:16:38 +0000 (-0800) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=40c763fa5feb8c579cc0a9a0a1d9aa15b9248945;p=seeder ... --- diff --git a/Makefile.am b/Makefile.am index 49ef6d5..7fd0a10 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/inc/log.h b/inc/log.h index 3e8cefa..db677ea 100644 --- 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 diff --git a/inc/net.h b/inc/net.h index b90aea5..9d19b32 100644 --- a/inc/net.h +++ b/inc/net.h @@ -1,6 +1,8 @@ #ifndef __NET_H_ #define __NET_H_ +#include + #include #include #include diff --git a/inc/opt.h b/inc/opt.h index 8c60900..b7d968f 100644 --- 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 diff --git a/src/default.c b/src/default.c index 9ac4b16..fd8bbae 100644 --- a/src/default.c +++ b/src/default.c @@ -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]; diff --git a/src/init.c b/src/init.c index 908bdb5..ab4514c 100644 --- a/src/init.c +++ b/src/init.c @@ -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; diff --git a/src/log.c b/src/log.c index c6232f7..211e2a6 100644 --- 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); + } +} diff --git a/src/main.c b/src/main.c index 4f88989..81272df 100644 --- a/src/main.c +++ b/src/main.c @@ -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; } diff --git a/src/net/start.c b/src/net/start.c index a4c7dc7..ccf5c7b 100644 --- a/src/net/start.c +++ b/src/net/start.c @@ -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; } diff --git a/src/opt/env.c b/src/opt/env.c index 5e33b89..f3fcbf6 100644 --- a/src/opt/env.c +++ b/src/opt/env.c @@ -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 index 0000000..a1f4195 --- /dev/null +++ b/src/opt/port.c @@ -0,0 +1,24 @@ +#include + +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; +} diff --git a/src/opt/set.c b/src/opt/set.c index 3a5108b..35fe0cb 100644 --- a/src/opt/set.c +++ b/src/opt/set.c @@ -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} diff --git a/src/setup.c b/src/setup.c index 9c86d6a..6eccddd 100644 --- a/src/setup.c +++ b/src/setup.c @@ -10,6 +10,11 @@ int setup_logging() { return -1; } + if(0!=atexit(&log_shutdown)) { + perror("atexit"); + return -1; + } + return 1; } diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index dd21f44..b0db911 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -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 \ diff --git a/test/unit/opt.tests.c b/test/unit/opt.tests.c index 70a95b3..8de1d24 100644 --- a/test/unit/opt.tests.c +++ b/test/unit/opt.tests.c @@ -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"));