Commit 37bcd711 authored by Max Kellermann's avatar Max Kellermann

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.
parent 231636b9
...@@ -83,16 +83,23 @@ GMainLoop *main_loop; ...@@ -83,16 +83,23 @@ GMainLoop *main_loop;
struct notify main_notify; 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); const char *path = config_get_path(CONF_DB_FILE);
bool ret;
if (!mapper_has_music_directory()) { if (!mapper_has_music_directory()) {
if (path != NULL) if (path != NULL)
g_message("Found " CONF_DB_FILE " setting without " g_message("Found " CONF_DB_FILE " setting without "
CONF_MUSIC_DIR " - disabling database"); CONF_MUSIC_DIR " - disabling database");
db_init(NULL); db_init(NULL);
return; return true;
} }
if (path == NULL) if (path == NULL)
...@@ -100,9 +107,12 @@ static void openDB(Options * options, char *argv0) ...@@ -100,9 +107,12 @@ static void openDB(Options * options, char *argv0)
db_init(path); db_init(path);
if (options->createDB > 0 || !db_load()) { if (options->createDB > 0)
unsigned job; /* don't attempt to load the old database */
return false;
ret = db_load();
if (!ret) {
if (options->createDB < 0) { if (options->createDB < 0) {
g_error("can't open db file and using " g_error("can't open db file and using "
"\"--no-create-db\" command line option; " "\"--no-create-db\" command line option; "
...@@ -114,10 +124,11 @@ static void openDB(Options * options, char *argv0) ...@@ -114,10 +124,11 @@ static void openDB(Options * options, char *argv0)
db_clear(); db_clear();
job = directory_update_init(NULL); /* run database update after daemonization */
if (job == 0) return false;
g_error("directory update failed");
} }
return true;
} }
/** /**
...@@ -186,6 +197,7 @@ int main(int argc, char *argv[]) ...@@ -186,6 +197,7 @@ int main(int argc, char *argv[])
{ {
Options options; Options options;
clock_t start; clock_t start;
bool create_db;
daemonize_close_stdin(); daemonize_close_stdin();
...@@ -238,7 +250,7 @@ int main(int argc, char *argv[]) ...@@ -238,7 +250,7 @@ int main(int argc, char *argv[])
decoder_plugin_init_all(); decoder_plugin_init_all();
update_global_init(); update_global_init();
openDB(&options, argv[0]); create_db = !openDB(&options, argv[0]);
#ifdef ENABLE_SQLITE #ifdef ENABLE_SQLITE
sticker_global_init(config_get_path(CONF_STICKER_FILE)); sticker_global_init(config_get_path(CONF_STICKER_FILE));
...@@ -264,6 +276,15 @@ int main(int argc, char *argv[]) ...@@ -264,6 +276,15 @@ int main(int argc, char *argv[])
player_create(); 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)); state_file_init(config_get_path(CONF_STATE_FILE));
/* run the main loop */ /* run the main loop */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment