From 5cbc0ad3c7043ea120bb5e9d116a927d3d7567a7 Mon Sep 17 00:00:00 2001 From: Jan Steemann Date: Wed, 27 Jun 2012 11:38:43 +0200 Subject: [PATCH] added some error messages when using --pid-file --- lib/Basics/FileUtils.cpp | 11 +++++++++++ lib/Basics/FileUtils.h | 8 ++++++++ lib/Rest/AnyServer.cpp | 7 +++++-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/Basics/FileUtils.cpp b/lib/Basics/FileUtils.cpp index 93d59bf857..c5116a7618 100644 --- a/lib/Basics/FileUtils.cpp +++ b/lib/Basics/FileUtils.cpp @@ -357,6 +357,17 @@ namespace triagens { return res == 0; } + + + off_t size (string const& path) { + struct stat stbuf; + int res = stat(path.c_str(), &stbuf); + if (res != 0) { + return 0; + } + + return stbuf.st_size; + } diff --git a/lib/Basics/FileUtils.h b/lib/Basics/FileUtils.h index 7fec55ed97..7ebc5fb990 100644 --- a/lib/Basics/FileUtils.h +++ b/lib/Basics/FileUtils.h @@ -130,6 +130,14 @@ namespace triagens { bool exists (string const& path); +//////////////////////////////////////////////////////////////////////////////// +/// @brief returns the size of a file. will return 0 for non-existing files +/// +/// the caller should check first if the file exists via the exists() method +//////////////////////////////////////////////////////////////////////////////// + + off_t size (string const& path); + //////////////////////////////////////////////////////////////////////////////// /// @brief strip extension //////////////////////////////////////////////////////////////////////////////// diff --git a/lib/Rest/AnyServer.cpp b/lib/Rest/AnyServer.cpp index 7d920d823c..c8eea29870 100644 --- a/lib/Rest/AnyServer.cpp +++ b/lib/Rest/AnyServer.cpp @@ -68,7 +68,7 @@ static void CheckPidFile (string const& pidFile) { TRI_FlushLogging(); exit(EXIT_FAILURE); } - else if (FileUtils::exists(pidFile)) { + else if (FileUtils::exists(pidFile) && FileUtils::size(pidFile) > 0) { LOGGER_INFO << "pid-file '" << pidFile << "' already exists, verifying pid"; ifstream f(pidFile.c_str()); @@ -137,7 +137,7 @@ static void WritePidFile (string const& pidFile, int pid) { ofstream out(pidFile.c_str(), ios::trunc); if (! out) { - cerr << "cannot write pid\n"; + cerr << "cannot write pid-file \"" << pidFile << "\"\n"; exit(EXIT_FAILURE); } @@ -159,6 +159,9 @@ static int forkProcess (string const& pidFile, string const& workingDirectory, s exit(EXIT_FAILURE); } + // Upon successful completion, fork() shall return 0 to the child process and + // shall return the process ID of the child process to the parent process. + // if we got a good PID, then we can exit the parent process if (pid > 0) { LOGGER_DEBUG << "started child process with pid " << pid;