From 37bcd711b57c1d668a9400be1a94f5d52d54f5a4 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 2 Feb 2009 16:57:24 +0100 Subject: main: create database after daemonization When the update thread is started before MPD has forked (for daemonization), it is killed, because threads do not survive a fork(). This induces an inconsistent state where MPD won't start any update thread at all, because it thinks the thread is already running. --- src/main.c | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index e872d3d91..fbc1da0f4 100644 --- a/src/main.c +++ b/src/main.c @@ -83,16 +83,23 @@ GMainLoop *main_loop; struct notify main_notify; -static void openDB(Options * options, char *argv0) +/** + * Returns the database. If this function returns false, this has not + * succeeded, and the caller should create the database after the + * process has been daemonized. + */ +static bool +openDB(Options * options, char *argv0) { const char *path = config_get_path(CONF_DB_FILE); + bool ret; if (!mapper_has_music_directory()) { if (path != NULL) g_message("Found " CONF_DB_FILE " setting without " CONF_MUSIC_DIR " - disabling database"); db_init(NULL); - return; + return true; } if (path == NULL) @@ -100,9 +107,12 @@ static void openDB(Options * options, char *argv0) db_init(path); - if (options->createDB > 0 || !db_load()) { - unsigned job; + if (options->createDB > 0) + /* don't attempt to load the old database */ + return false; + ret = db_load(); + if (!ret) { if (options->createDB < 0) { g_error("can't open db file and using " "\"--no-create-db\" command line option; " @@ -114,10 +124,11 @@ static void openDB(Options * options, char *argv0) db_clear(); - job = directory_update_init(NULL); - if (job == 0) - g_error("directory update failed"); + /* run database update after daemonization */ + return false; } + + return true; } /** @@ -186,6 +197,7 @@ int main(int argc, char *argv[]) { Options options; clock_t start; + bool create_db; daemonize_close_stdin(); @@ -238,7 +250,7 @@ int main(int argc, char *argv[]) decoder_plugin_init_all(); update_global_init(); - openDB(&options, argv[0]); + create_db = !openDB(&options, argv[0]); #ifdef ENABLE_SQLITE sticker_global_init(config_get_path(CONF_STICKER_FILE)); @@ -264,6 +276,15 @@ int main(int argc, char *argv[]) player_create(); + if (create_db) { + /* the database failed to load, or MPD was started + with --create-db: recreate a new database */ + unsigned job = directory_update_init(NULL); + if (job == 0) + g_error("directory update failed"); + } + + state_file_init(config_get_path(CONF_STATE_FILE)); /* run the main loop */ -- cgit v1.2.3