From fe6c689250efc576e8c8ae4b4c9a8037abbe7a64 Mon Sep 17 00:00:00 2001 From: Jussi Lind Date: Thu, 7 Apr 2011 10:44:06 +0300 Subject: [PATCH] Changes: Fixed file locking when --daemon is used. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit RevBy: Pertti Kellomäki --- src/launcherlib/daemon.cpp | 6 ++++++ src/launcherlib/main.cpp | 16 +++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/launcherlib/daemon.cpp b/src/launcherlib/daemon.cpp index fde19be..ffbac5f 100644 --- a/src/launcherlib/daemon.cpp +++ b/src/launcherlib/daemon.cpp @@ -625,6 +625,12 @@ void Daemon::daemonize() exit(EXIT_SUCCESS); } + // Check the lock + if(!Daemon::lock()) + { + Logger::logErrorAndDie(EXIT_FAILURE, "%s is already running \n", PROG_NAME_LAUNCHER); + } + // Change the file mode mask umask(0); diff --git a/src/launcherlib/main.cpp b/src/launcherlib/main.cpp index 7c86c32..e0d99b1 100644 --- a/src/launcherlib/main.cpp +++ b/src/launcherlib/main.cpp @@ -21,6 +21,9 @@ #include "logger.h" #include +#include +#include +#include #include #include #include @@ -67,10 +70,17 @@ DECL_EXPORT int main(int argc, char * argv[]) Logger::openLog(PROG_NAME_LAUNCHER); Logger::logDebug("%s starting..", PROG_NAME_LAUNCHER); - // Check that an instance of launcher is not already running - if(!Daemon::lock()) + // Check that an instance of launcher is not already running already + // here if we are not going to fork. In that case the lock should be + // checked after fork() is done in Daemon::daemonize + std::vector args(argv, argv + argc); + if (find(args.begin(), args.end(), "-d") == args.end() && + find(args.begin(), args.end(), "--daemon") == args.end()) { - Logger::logErrorAndDie(EXIT_FAILURE, "%s is already running \n", PROG_NAME_LAUNCHER); + if(!Daemon::lock()) + { + Logger::logErrorAndDie(EXIT_FAILURE, "%s is already running \n", PROG_NAME_LAUNCHER); + } } // Create main daemon instance