Make PID file creating errors fatal

This commit is contained in:
Hennadii Stepanov 2019-02-02 00:40:36 +02:00
parent 745a2ace18
commit 561e375c73
No known key found for this signature in database
GPG key ID: 410108112E7EA81F
4 changed files with 21 additions and 13 deletions

View file

@ -53,6 +53,8 @@
#include <stdio.h> #include <stdio.h>
#ifndef WIN32 #ifndef WIN32
#include <attributes.h>
#include <cerrno>
#include <signal.h> #include <signal.h>
#include <sys/stat.h> #include <sys/stat.h>
#endif #endif
@ -1192,12 +1194,29 @@ bool AppInitLockDataDirectory()
return true; return true;
} }
#ifndef WIN32
NODISCARD static bool CreatePidFile()
{
FILE* file = fsbridge::fopen(GetPidFile(), "w");
if (file) {
fprintf(file, "%d\n", getpid());
fclose(file);
return true;
} else {
return InitError(strprintf(_("Unable to create the PID file '%s': %s"), GetPidFile().string(), std::strerror(errno)));
}
}
#endif
bool AppInitMain(InitInterfaces& interfaces) bool AppInitMain(InitInterfaces& interfaces)
{ {
const CChainParams& chainparams = Params(); const CChainParams& chainparams = Params();
// ********************************************************* Step 4a: application initialization // ********************************************************* Step 4a: application initialization
#ifndef WIN32 #ifndef WIN32
CreatePidFile(GetPidFile(), getpid()); if (!CreatePidFile()) {
// Detailed error printed inside CreatePidFile().
return false;
}
#endif #endif
if (g_logger->m_print_to_file) { if (g_logger->m_print_to_file) {
if (gArgs.GetBoolArg("-shrinkdebugfile", g_logger->DefaultShrinkDebugFile())) { if (gArgs.GetBoolArg("-shrinkdebugfile", g_logger->DefaultShrinkDebugFile())) {

View file

@ -963,16 +963,6 @@ fs::path GetPidFile()
{ {
return AbsPathForConfigVal(fs::path(gArgs.GetArg("-pid", BITCOIN_PID_FILENAME))); return AbsPathForConfigVal(fs::path(gArgs.GetArg("-pid", BITCOIN_PID_FILENAME)));
} }
void CreatePidFile(const fs::path &path, pid_t pid)
{
FILE* file = fsbridge::fopen(path, "w");
if (file)
{
fprintf(file, "%d\n", pid);
fclose(file);
}
}
#endif #endif
bool RenameOver(fs::path src, fs::path dest) bool RenameOver(fs::path src, fs::path dest)

View file

@ -86,7 +86,6 @@ void ClearDatadirCache();
fs::path GetConfigFile(const std::string& confPath); fs::path GetConfigFile(const std::string& confPath);
#ifndef WIN32 #ifndef WIN32
fs::path GetPidFile(); fs::path GetPidFile();
void CreatePidFile(const fs::path &path, pid_t pid);
#endif #endif
#ifdef WIN32 #ifdef WIN32
fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true); fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true);

View file

@ -8,6 +8,7 @@ KNOWN_VIOLATIONS=(
"src/dbwrapper.cpp:.*vsnprintf" "src/dbwrapper.cpp:.*vsnprintf"
"src/httprpc.cpp.*trim" "src/httprpc.cpp.*trim"
"src/init.cpp:.*atoi" "src/init.cpp:.*atoi"
"src/init.cpp:.*fprintf"
"src/qt/rpcconsole.cpp:.*atoi" "src/qt/rpcconsole.cpp:.*atoi"
"src/rest.cpp:.*strtol" "src/rest.cpp:.*strtol"
"src/test/dbwrapper_tests.cpp:.*snprintf" "src/test/dbwrapper_tests.cpp:.*snprintf"
@ -18,7 +19,6 @@ KNOWN_VIOLATIONS=(
"src/util/strencodings.cpp:.*strtoul" "src/util/strencodings.cpp:.*strtoul"
"src/util/strencodings.h:.*atoi" "src/util/strencodings.h:.*atoi"
"src/util/system.cpp:.*atoi" "src/util/system.cpp:.*atoi"
"src/util/system.cpp:.*fprintf"
) )
REGEXP_IGNORE_EXTERNAL_DEPENDENCIES="^src/(crypto/ctaes/|leveldb/|secp256k1/|tinyformat.h|univalue/)" REGEXP_IGNORE_EXTERNAL_DEPENDENCIES="^src/(crypto/ctaes/|leveldb/|secp256k1/|tinyformat.h|univalue/)"