From: alex Date: Sat, 30 Apr 2022 01:25:07 +0000 (-0700) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=296b25024f13c71eb155880758112bc542a38fe5;p=seeder ... --- diff --git a/inc/add.h b/inc/add.h index f7a179c..ceefb32 100644 --- a/inc/add.h +++ b/inc/add.h @@ -21,6 +21,7 @@ struct add_queue_entry { #define ADD_MESSAGE_ADDING_TORRENT "adding all files in %s to torrent named %s\n" #define ADD_MESSAGE_ADDED_FILE "added file: %s\n" #define ADD_MESSAGE_HASH_FILE_FAILED "failed to hash file: %s\n" +#define ADD_MESSAGE_SHUTDOWN_FAILED "adding threads shutdown failed\n" #define ADD_QUEUE_INITIAL_SIZE 8 diff --git a/inc/hashmap.h b/inc/hashmap.h index d30d22f..c0217db 100644 --- a/inc/hashmap.h +++ b/inc/hashmap.h @@ -10,7 +10,7 @@ struct hash_map { }; void hashmap_clear(struct hash_map*); -void *hashmap_find(struct hash_map*,const void*,size_t); +void *hashmap_find(const struct hash_map*,const void*,size_t); void hashmap_free(struct hash_map*); int hashmap_init(struct hash_map**,size_t); int hashmap_insert(struct hash_map*,const void*,size_t,void*); diff --git a/inc/watch.h b/inc/watch.h index 18b2aab..7d636b4 100644 --- a/inc/watch.h +++ b/inc/watch.h @@ -10,6 +10,7 @@ #define WATCH_MESSAGE_FAILED "failed to start watching\n" #define WATCH_MESSAGE_START "watching started: %s\n" #define WATCH_MESSAGE_START_FAILED "failed to start watching %s\n" +#define WATCH_MESSAGE_SHUTDOWN_FAILED "failed to shutdown watching thread\n" int watch(); diff --git a/src/add.c b/src/add.c index 3afaacb..d0b5909 100644 --- a/src/add.c +++ b/src/add.c @@ -1,6 +1,7 @@ #include static pthread_mutex_t adding_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_t *adding_threads; static struct hash_map *add_queue; static size_t add_queue_index; @@ -12,6 +13,7 @@ static int add_queue_entry_add_torrent(struct add_queue_entry*,struct torrent*); static void add_queue_entry_free(struct add_queue_entry*); static int add_queue_entry_init(struct add_queue_entry**); static int add_queue_resize(); +static void add_shutdown(); static int add_to_queue(struct add_queue_entry*); static struct add_queue_entry *get_next(); static int ftw_helper(const char*,const struct stat*,int); @@ -19,6 +21,13 @@ static int ftw_helper(const char*,const struct stat*,int); int add() { struct torrent *p; + adding_threads = NULL; + + if(0!=atexit(&add_shutdown)) { + perror("atexit"); + return -1; + } + if(hashmap_init(&add_queue,ADD_QUEUE_INITIAL_SIZE)<0) { return -1; } for(size_t i=0;isize;i++) { @@ -30,7 +39,7 @@ int add() { add_queue_index = 0; - pthread_t *adding_threads = calloc(sizeof(pthread_t),global_options.worker_threads); + adding_threads = calloc(sizeof(pthread_t),global_options.worker_threads); for(size_t i=0;iepoll_fd); close(info->tcp_socket); close(info->udp_socket); @@ -51,6 +52,7 @@ int net_start() { } static void net_stop() { + log_info("net thread stopping\n"); for(size_t i=0;isize;i++) { p = session.torrents.paths->map[i]; if(p!=NULL) { torrent_free(p); } @@ -20,6 +22,8 @@ static void session_clean() { hashmap_clear(session.torrents.paths); hashmap_free(session.torrents.paths); + + log_info("session cleared\n"); } struct torrent *session_find_torrent(uint8_t *infohash, size_t size) { @@ -43,6 +47,8 @@ int session_setup() { return -1; } + log_info("session setup\n"); + return 1; } diff --git a/src/sighandler.c b/src/sighandler.c index a46f902..793fa11 100644 --- a/src/sighandler.c +++ b/src/sighandler.c @@ -9,7 +9,7 @@ static void handle_interrupt() { * call exit explicitly will call functions registered * with atexit and do appropriate cleanup. */ - exit(EXIT_SUCCESS); + exit(EXIT_FAILURE); } static void shutdown_success() { diff --git a/src/watch.c b/src/watch.c index 142f7ec..71e0027 100644 --- a/src/watch.c +++ b/src/watch.c @@ -1,12 +1,19 @@ #include +static int watch_fd; static pthread_t watching_thread; +static void watch_shutdown(); static void *watch_spawn(void*); -static int watch_spawn_all(int,const char*,struct tree*); -static void watch_poll(int); +static int watch_spawn_all(const char*,struct tree*); +static void watch_poll(); int watch() { + if(0!=atexit(&watch_shutdown)) { + perror("atexit"); + return -1; + } + if(pthread_create(&watching_thread,NULL,&watch_spawn,NULL)!=0) { perror("pthread_create"); return -1; @@ -17,10 +24,9 @@ int watch() { static void *watch_spawn(void *unused) { struct torrent *p; - int fd; - fd = inotify_init(); - if(fd<0) { + watch_fd = inotify_init(); + if(watch_fd<0) { log_err(WATCH_MESSAGE_FAILED); perror("inotify_init"); return NULL; @@ -29,27 +35,26 @@ static void *watch_spawn(void *unused) { for(size_t i=0;isize;i++) { p = session.torrents.infohashes->map[i]; if(p!=NULL) { - if(watch_spawn_all(fd,p->root,p->tree)<0) { return NULL; } + if(watch_spawn_all(p->root,p->tree)<0) { return NULL; } } } - watch_poll(fd); + watch_poll(); return NULL; } #define WATCH_FLAGS IN_CLOSE_WRITE | IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_TO -static int watch_spawn_all(int fd, const char *root, struct tree *tree) { +static int watch_spawn_all(const char *root, struct tree *tree) { struct tree_entry *p; - if(fd<0) { return -1; } if(NULL==root) { return -1; } if(strlen(root)<=0) { return -1; } if(NULL==tree) { return -1; } - tree->watch_fd = inotify_add_watch(fd,root,WATCH_FLAGS); + tree->watch_fd = inotify_add_watch(watch_fd,root,WATCH_FLAGS); if(tree->watch_fd<0) { perror("inotify_add_watch"); log_err(WATCH_MESSAGE_START_FAILED,root); @@ -62,7 +67,7 @@ static int watch_spawn_all(int fd, const char *root, struct tree *tree) { while(p!=NULL) { if(p->children!=NULL) { char *newroot = concat(root,p->name); - if(watch_spawn_all(fd,newroot,p->children)<0) { return -1; } + if(watch_spawn_all(newroot,p->children)<0) { return -1; } free(newroot); } p = p->next; @@ -71,14 +76,16 @@ static int watch_spawn_all(int fd, const char *root, struct tree *tree) { return 1; } -static void watch_poll(int fd) { +static void watch_poll() { /* see `man inotify` for explanation of alignment modifiers */ char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event)))); const struct inotify_event *event; ssize_t len; for(;;) { - len = read(fd,buf,sizeof(buf)); + pthread_testcancel(); + + len = read(watch_fd,buf,sizeof(buf)); if(len<=0) { perror("read"); break; @@ -92,3 +99,12 @@ static void watch_poll(int fd) { } } } + +static void watch_shutdown() { + log_info("watch thread stopping\n"); + if(0!=pthread_cancel(watching_thread)) { + log_err(WATCH_MESSAGE_SHUTDOWN_FAILED); + } + close(watch_fd); + log_info("watch thread stopped\n"); +} diff --git a/test/integration/test_utils.c b/test/integration/test_utils.c index ed3cda2..373304d 100644 --- a/test/integration/test_utils.c +++ b/test/integration/test_utils.c @@ -30,20 +30,27 @@ pid_t run(char *const opts[]) { void run_and_exit_successfully(char *const opts[]) { int status; + printf("running\n"); pid_t child_pid = run(opts); // wait 5 seconds for startup + printf("sleeping\n"); sleep(5); // verify still running + printf("verify process running\n"); assert(0==waitpid(child_pid,&status,WNOHANG)); // SIGINT to shutdown + printf("send SIGINT\n"); assert(0==kill(child_pid,SIGINT)); // verify child process exited successfully + printf("waiting for child process to exit\n"); assert(child_pid==waitpid(child_pid,&status,0)); + printf("checking exit status\n"); assert(WIFEXITED(status)); + printf("success!\n"); } void setup_env() {