afs: Handle SIGHUP.
[paraslash.git] / afs.c
diff --git a/afs.c b/afs.c
index 455bee7..de713fa 100644 (file)
--- a/afs.c
+++ b/afs.c
@@ -497,16 +497,6 @@ static int setup_command_socket_or_die(void)
        return ret;
 }
 
-static int server_socket;
-static struct command_task command_task_struct;
-static struct signal_task signal_task_struct;
-
-static void unregister_tasks(void)
-{
-       unregister_task(&command_task_struct.task);
-       unregister_task(&signal_task_struct.task);
-}
-
 static void close_afs_tables(enum osl_close_flags flags)
 {
        PARA_NOTICE_LOG("closing afs_tables\n");
@@ -521,6 +511,86 @@ static void close_afs_tables(enum osl_close_flags flags)
        aft_shutdown(flags);
 }
 
+static char *database_dir;
+
+static int make_database_dir(void)
+{
+       int ret;
+
+       if (!database_dir) {
+               if (conf.afs_database_dir_given)
+                       database_dir = para_strdup(conf.afs_database_dir_arg);
+               else {
+                       char *home = para_homedir();
+                       database_dir = make_message(
+                               "%s/.paraslash/afs_database", home);
+                       free(home);
+               }
+       }
+       PARA_INFO_LOG("afs_database dir %s\n", database_dir);
+       ret = para_mkdir(database_dir, 0777);
+       if (ret >= 0 || ret == -E_EXIST)
+               return 1;
+       free(database_dir);
+       database_dir = NULL;
+       return ret;
+}
+
+
+static int open_afs_tables(void)
+{
+       int ret = make_database_dir();
+
+       if (ret < 0)
+               return ret;
+       ret = attribute_init(&afs_tables[TBLNUM_ATTRIBUTES], database_dir);
+       if (ret < 0)
+               return ret;
+       ret = moods_init(&afs_tables[TBLNUM_MOODS], database_dir);
+       if (ret < 0)
+               goto moods_init_error;
+       ret = playlists_init(&afs_tables[TBLNUM_PLAYLIST], database_dir);
+       if (ret < 0)
+               goto playlists_init_error;
+       ret = lyrics_init(&afs_tables[TBLNUM_LYRICS], database_dir);
+       if (ret < 0)
+               goto lyrics_init_error;
+       ret = images_init(&afs_tables[TBLNUM_IMAGES], database_dir);
+       if (ret < 0)
+               goto images_init_error;
+       ret = score_init(&afs_tables[TBLNUM_SCORES], database_dir);
+       if (ret < 0)
+               goto score_init_error;
+       ret = aft_init(&afs_tables[TBLNUM_AUDIO_FILES], database_dir);
+       if (ret < 0)
+               goto aft_init_error;
+       return 1;
+
+aft_init_error:
+       score_shutdown(OSL_MARK_CLEAN);
+score_init_error:
+       images_shutdown(OSL_MARK_CLEAN);
+images_init_error:
+       lyrics_shutdown(OSL_MARK_CLEAN);
+lyrics_init_error:
+       playlists_shutdown(OSL_MARK_CLEAN);
+playlists_init_error:
+       moods_shutdown(OSL_MARK_CLEAN);
+moods_init_error:
+       attribute_shutdown(OSL_MARK_CLEAN);
+       return ret;
+}
+
+static int server_socket;
+static struct command_task command_task_struct;
+static struct signal_task signal_task_struct;
+
+static void unregister_tasks(void)
+{
+       unregister_task(&command_task_struct.task);
+       unregister_task(&signal_task_struct.task);
+}
+
 static void signal_pre_select(struct sched *s, struct task *t)
 {
        struct signal_task *st = t->private_data;
@@ -538,6 +608,11 @@ static void signal_post_select(struct sched *s, struct task *t)
        t->ret = 1;
        if (st->signum == SIGUSR1)
                return; /* ignore SIGUSR1 */
+       if (st->signum == SIGHUP) {
+               close_afs_tables(OSL_MARK_CLEAN);
+               t->ret = open_afs_tables();
+               return;
+       }
        PARA_NOTICE_LOG("caught signal %d\n", st->signum);
        t->ret = -E_SIGNAL_CAUGHT;
        unregister_tasks();
@@ -551,6 +626,7 @@ static void register_signal_task(void)
        para_install_sighandler(SIGINT);
        para_install_sighandler(SIGTERM);
        para_install_sighandler(SIGPIPE);
+       para_install_sighandler(SIGHUP);
 
        st->task.pre_select = signal_pre_select;
        st->task.post_select = signal_post_select;
@@ -723,75 +799,6 @@ void register_tasks(uint32_t cookie)
        register_command_task(cookie);
 }
 
-static char *database_dir;
-
-static int make_database_dir(void)
-{
-       int ret;
-
-       if (!database_dir) {
-               if (conf.afs_database_dir_given)
-                       database_dir = para_strdup(conf.afs_database_dir_arg);
-               else {
-                       char *home = para_homedir();
-                       database_dir = make_message(
-                               "%s/.paraslash/afs_database", home);
-                       free(home);
-               }
-       }
-       PARA_INFO_LOG("afs_database dir %s\n", database_dir);
-       ret = para_mkdir(database_dir, 0777);
-       if (ret >= 0 || ret == -E_EXIST)
-               return 1;
-       free(database_dir);
-       database_dir = NULL;
-       return ret;
-}
-
-static int open_afs_tables(void)
-{
-       int ret = make_database_dir();
-
-       if (ret < 0)
-               return ret;
-       ret = attribute_init(&afs_tables[TBLNUM_ATTRIBUTES], database_dir);
-       if (ret < 0)
-               return ret;
-       ret = moods_init(&afs_tables[TBLNUM_MOODS], database_dir);
-       if (ret < 0)
-               goto moods_init_error;
-       ret = playlists_init(&afs_tables[TBLNUM_PLAYLIST], database_dir);
-       if (ret < 0)
-               goto playlists_init_error;
-       ret = lyrics_init(&afs_tables[TBLNUM_LYRICS], database_dir);
-       if (ret < 0)
-               goto lyrics_init_error;
-       ret = images_init(&afs_tables[TBLNUM_IMAGES], database_dir);
-       if (ret < 0)
-               goto images_init_error;
-       ret = score_init(&afs_tables[TBLNUM_SCORES], database_dir);
-       if (ret < 0)
-               goto score_init_error;
-       ret = aft_init(&afs_tables[TBLNUM_AUDIO_FILES], database_dir);
-       if (ret < 0)
-               goto aft_init_error;
-       return 1;
-
-aft_init_error:
-       score_shutdown(OSL_MARK_CLEAN);
-score_init_error:
-       images_shutdown(OSL_MARK_CLEAN);
-images_init_error:
-       lyrics_shutdown(OSL_MARK_CLEAN);
-lyrics_init_error:
-       playlists_shutdown(OSL_MARK_CLEAN);
-playlists_init_error:
-       moods_shutdown(OSL_MARK_CLEAN);
-moods_init_error:
-       attribute_shutdown(OSL_MARK_CLEAN);
-       return ret;
-}
-
 __noreturn int afs_init(uint32_t cookie, int socket_fd)
 {
        enum play_mode current_play_mode;