aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2009-02-02 16:57:24 +0100
committerMax Kellermann <max@duempel.org>2009-02-02 16:57:24 +0100
commit37bcd711b57c1d668a9400be1a94f5d52d54f5a4 (patch)
treeae26fdeb7da924450b490965b30ee2143ac5c8b1 /src/main.c
parent231636b9eb2ecd7d8669d309a74c3a51cd5ddfae (diff)
downloadmpd-37bcd711b57c1d668a9400be1a94f5d52d54f5a4.tar.gz
mpd-37bcd711b57c1d668a9400be1a94f5d52d54f5a4.tar.xz
mpd-37bcd711b57c1d668a9400be1a94f5d52d54f5a4.zip
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.
Diffstat (limited to '')
-rw-r--r--src/main.c37
1 files changed, 29 insertions, 8 deletions
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 */