X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=afs.c;h=95f6b311bb8597527f894d525f8cdd6c8eed5ca8;hp=455bee7d62fe46a85d26adea1b1106b01e5ada85;hb=53d503ce75eb1d9439cfb75053d3cff4cbca6ff9;hpb=c7c5668dddc48b860655c105b23cc50c45c39540 diff --git a/afs.c b/afs.c index 455bee7d..95f6b311 100644 --- a/afs.c +++ b/afs.c @@ -10,6 +10,7 @@ #include #include "server.cmdline.h" #include "para.h" +#include "string.h" #include "afh.h" #include "server.h" #include "error.h" @@ -19,7 +20,6 @@ #include "net.h" #include "afs.h" #include "ipc.h" -#include "string.h" #include "list.h" #include "sched.h" #include "signal.h" @@ -53,7 +53,15 @@ enum afs_table_num { NUM_AFS_TABLES }; -static struct table_info afs_tables[NUM_AFS_TABLES]; +static struct afs_table afs_tables[NUM_AFS_TABLES] = { + [TBLNUM_AUDIO_FILES] = {.init = aft_init}, + [TBLNUM_ATTRIBUTES] = {.init = attribute_init}, + [TBLNUM_SCORES] = {.init = score_init}, + [TBLNUM_MOODS] = {.init = moods_init}, + [TBLNUM_LYRICS] = {.init = lyrics_init}, + [TBLNUM_IMAGES] = {.init = images_init}, + [TBLNUM_PLAYLIST] = {.init = playlists_init}, +}; struct command_task { /** The file descriptor for the local socket. */ @@ -123,7 +131,7 @@ struct callback_result { }; /** - * Ask the parent process to call a given function. + * Ask the afs process to call a given function. * * \param f The function to be called. * \param query Pointer to arbitrary data for the callback. @@ -307,6 +315,14 @@ static int action_if_pattern_matches(struct osl_row *row, void *data) return 1; } +/** + * Execute the given function for each matching row. + * + * \param pmd Describes what to match and how. + * + * \return The return value of the underlying call to osl_rbtree_loop() + * or osl_rbtree_loop_reverse(). + */ int for_each_matching_row(struct pattern_match_data *pmd) { if (pmd->pm_flags & PM_REVERSE_LOOP) @@ -452,26 +468,29 @@ int close_audio_file(struct audio_file_data *afd) static enum play_mode init_admissible_files(void) { - int ret; - - if (conf.mood_given) { - ret = change_current_mood(conf.mood_arg); - if (ret >= 0) { - if (conf.playlist_given) - PARA_WARNING_LOG("ignoring playlist %s\n", - conf.playlist_arg); - return PLAY_MODE_MOOD; + int ret = 0; + char *arg = conf.afs_initial_mode_arg; + + if (conf.afs_initial_mode_given) { + if (!strncmp(arg, "p:", 2)) { + ret = playlist_open(arg + 2); + if (ret >= 0) + return PLAY_MODE_PLAYLIST; + goto dummy; } + if (!strncmp(arg, "m:", 2)) { + ret = change_current_mood(arg + 2); + if (ret >= 0) + return PLAY_MODE_MOOD; + goto dummy; + } + PARA_ERROR_LOG("bad afs initial mode arg: %s\n", arg); } - if (conf.playlist_given) { - ret = playlist_open(conf.playlist_arg); - if (ret >= 0) - return PLAY_MODE_PLAYLIST; - } - ret = change_current_mood(NULL); /* open first available mood */ - if (ret >= 0) - return PLAY_MODE_MOOD; - change_current_mood(""); /* open dummy mood, always successful */ +dummy: + if (ret < 0) + PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret)); + PARA_NOTICE_LOG("defaulting to dummy mood\n"); + change_current_mood(""); /* always successful */ return PLAY_MODE_MOOD; } @@ -497,6 +516,62 @@ static int setup_command_socket_or_die(void) return ret; } +static void close_afs_tables(void) +{ + int i; + PARA_NOTICE_LOG("closing afs_tables\n"); + for (i = 0; i < NUM_AFS_TABLES; i++) + afs_tables[i].close(); +} + +static char *database_dir; + +static void get_database_dir(void) +{ + 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); +} + +static int make_database_dir(void) +{ + int ret; + + get_database_dir(); + ret = para_mkdir(database_dir, 0777); + if (ret >= 0 || is_errno(-ret, EEXIST)) + return 1; + return ret; +} + +static int open_afs_tables(void) +{ + int i, ret; + + get_database_dir(); + for (i = 0; i < NUM_AFS_TABLES; i++) { + ret = afs_tables[i].open(database_dir); + if (ret >= 0) + continue; + PARA_ERROR_LOG("%s init: %s\n", afs_tables[i].name, + PARA_STRERROR(-ret)); + } + if (ret >= 0) + return ret; + do + afs_tables[i].close(); + while (i--); + return ret; +} + static int server_socket; static struct command_task command_task_struct; static struct signal_task signal_task_struct; @@ -507,20 +582,6 @@ static void unregister_tasks(void) unregister_task(&signal_task_struct.task); } -static void close_afs_tables(enum osl_close_flags flags) -{ - PARA_NOTICE_LOG("closing afs_tables\n"); - score_shutdown(flags); - attribute_shutdown(flags); - close_current_mood(); - playlist_close(); - moods_shutdown(flags); - playlists_shutdown(flags); - lyrics_shutdown(flags); - images_shutdown(flags); - aft_shutdown(flags); -} - static void signal_pre_select(struct sched *s, struct task *t) { struct signal_task *st = t->private_data; @@ -538,8 +599,13 @@ 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(); + t->ret = open_afs_tables(); + return; + } PARA_NOTICE_LOG("caught signal %d\n", st->signum); - t->ret = -E_SIGNAL_CAUGHT; + t->ret = -E_AFS_SIGNAL; unregister_tasks(); } @@ -551,6 +617,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; @@ -661,10 +728,11 @@ static void execute_afs_command(int fd, uint32_t expected_cookie) query_shmid); return; } - /* Ignore return value: Errors might be ok here. */ + /* Ignore return value: Errors might be OK here. */ call_callback(fd, query_shmid); } +/** Shutdown connection if query has not arrived until this many seconds. */ #define AFS_CLIENT_TIMEOUT 3 static void command_post_select(struct sched *s, struct task *t) @@ -677,7 +745,7 @@ static void command_post_select(struct sched *s, struct task *t) list_for_each_entry_safe(client, tmp, &afs_client_list, node) { if (FD_ISSET(client->fd, &s->rfds)) execute_afs_command(client->fd, ct->cookie); - else { /* prevent bogus connection flodding */ + else { /* prevent bogus connection flooding */ struct timeval diff; tv_diff(now, &client->connect_time, &diff); if (diff.tv_sec < AFS_CLIENT_TIMEOUT) @@ -717,88 +785,27 @@ static void register_command_task(uint32_t cookie) register_task(&ct->task); } -void register_tasks(uint32_t cookie) +static void register_tasks(uint32_t cookie) { register_signal_task(); 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) +/** + * Initialize the audio file selector process. + * + * \param cookie The value used for "authentication". + * \param socket_fd File descriptor used for communication with the server. + */ +__noreturn void afs_init(uint32_t cookie, int socket_fd) { enum play_mode current_play_mode; struct sched s; - int ret; + int i, ret; INIT_LIST_HEAD(&afs_client_list); + for (i = 0; i < NUM_AFS_TABLES; i++) + afs_tables[i].init(&afs_tables[i]); ret = open_afs_tables(); if (ret < 0) { @@ -818,7 +825,7 @@ __noreturn int afs_init(uint32_t cookie, int socket_fd) ret = sched(&s); if (ret < 0) PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret)); - close_afs_tables(OSL_MARK_CLEAN); + close_afs_tables(); exit(EXIT_FAILURE); } @@ -828,15 +835,15 @@ static int create_tables_callback(const struct osl_object *query, uint32_t table_mask = *(uint32_t *)query->data; int i, ret; - close_afs_tables(OSL_MARK_CLEAN); + close_afs_tables(); for (i = 0; i < NUM_AFS_TABLES; i++) { - struct table_info *ti = afs_tables + i; + struct afs_table *t = &afs_tables[i]; - if (ti->flags & TBLFLAG_SKIP_CREATE) - continue; if (!(table_mask & (1 << i))) continue; - ret = osl_create_table(ti->desc); + if (!t->create) + continue; + ret = t->create(database_dir); if (ret < 0) return ret; } @@ -851,15 +858,16 @@ int com_init(int fd, int argc, char * const * const argv) struct osl_object query = {.data = &table_mask, .size = sizeof(table_mask)}; + ret = make_database_dir(); + if (ret < 0) + return ret; if (argc != 1) { table_mask = 0; for (i = 1; i < argc; i++) { for (j = 0; j < NUM_AFS_TABLES; j++) { - struct table_info *ti = afs_tables + j; + struct afs_table *t = &afs_tables[j]; - if (ti->flags & TBLFLAG_SKIP_CREATE) - continue; - if (strcmp(argv[i], ti->desc->name)) + if (strcmp(argv[i], t->name)) continue; table_mask |= (1 << j); break; @@ -874,9 +882,17 @@ int com_init(int fd, int argc, char * const * const argv) return send_va_buffer(fd, "successfully created afs table(s)\n"); } +/** + * Flags for the check command. + * + * \sa com_check(). + */ enum com_check_flags { + /** Check the audio file table. */ CHECK_AFT = 1, + /** Check the mood table. */ CHECK_MOODS = 2, + /** Check the playlist table. */ CHECK_PLAYLISTS = 4 };