Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve PID file error handling #15278

Merged
merged 3 commits into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
#include <stdio.h>

#ifndef WIN32
#include <attributes.h>
#include <cerrno>
#include <signal.h>
#include <sys/stat.h>
#endif
Expand Down Expand Up @@ -92,6 +94,30 @@ std::unique_ptr<BanMan> g_banman;

static const char* FEE_ESTIMATES_FILENAME="fee_estimates.dat";

/**
* The PID file facilities.
*/
#ifndef WIN32
static const char* BITCOIN_PID_FILENAME = "bitcoind.pid";

static fs::path GetPidFile()
{
return AbsPathForConfigVal(fs::path(gArgs.GetArg("-pid", BITCOIN_PID_FILENAME)));
}

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

//////////////////////////////////////////////////////////////////////////////
//
// Shutdown
Expand Down Expand Up @@ -262,9 +288,11 @@ void Shutdown(InitInterfaces& interfaces)

#ifndef WIN32
try {
fs::remove(GetPidFile());
if (!fs::remove(GetPidFile())) {
LogPrintf("%s: Unable to remove PID file: File does not exist\n", __func__);
}
} catch (const fs::filesystem_error& e) {
LogPrintf("%s: Unable to remove pidfile: %s\n", __func__, e.what());
LogPrintf("%s: Unable to remove PID file: %s\n", __func__, e.what());
}
#endif
interfaces.chain_clients.clear();
Expand Down Expand Up @@ -1195,7 +1223,10 @@ bool AppInitMain(InitInterfaces& interfaces)
const CChainParams& chainparams = Params();
// ********************************************************* Step 4a: application initialization
#ifndef WIN32
CreatePidFile(GetPidFile(), getpid());
if (!CreatePidFile()) {
// Detailed error printed inside CreatePidFile().
return false;
}
#endif
if (g_logger->m_print_to_file) {
if (gArgs.GetBoolArg("-shrinkdebugfile", g_logger->DefaultShrinkDebugFile())) {
Expand Down
18 changes: 0 additions & 18 deletions src/util/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
const int64_t nStartupTime = GetTime();

const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
const char * const BITCOIN_PID_FILENAME = "bitcoind.pid";

ArgsManager gArgs;

Expand Down Expand Up @@ -958,23 +957,6 @@ std::string ArgsManager::GetChainName() const
return CBaseChainParams::MAIN;
}

#ifndef WIN32
fs::path GetPidFile()
{
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

bool RenameOver(fs::path src, fs::path dest)
{
#ifdef WIN32
Expand Down
5 changes: 0 additions & 5 deletions src/util/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
int64_t GetStartupTime();

extern const char * const BITCOIN_CONF_FILENAME;
extern const char * const BITCOIN_PID_FILENAME;

/** Translate a message to the native language of the user. */
const extern std::function<std::string(const char*)> G_TRANSLATION_FUN;
Expand Down Expand Up @@ -84,10 +83,6 @@ const fs::path &GetBlocksDir();
const fs::path &GetDataDir(bool fNetSpecific = true);
void ClearDatadirCache();
fs::path GetConfigFile(const std::string& confPath);
#ifndef WIN32
fs::path GetPidFile();
void CreatePidFile(const fs::path &path, pid_t pid);
#endif
#ifdef WIN32
fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/lint/lint-locale-dependence.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ KNOWN_VIOLATIONS=(
"src/dbwrapper.cpp:.*vsnprintf"
"src/httprpc.cpp.*trim"
"src/init.cpp:.*atoi"
"src/init.cpp:.*fprintf"
"src/qt/rpcconsole.cpp:.*atoi"
"src/rest.cpp:.*strtol"
"src/test/dbwrapper_tests.cpp:.*snprintf"
Expand All @@ -18,7 +19,6 @@ KNOWN_VIOLATIONS=(
"src/util/strencodings.cpp:.*strtoul"
"src/util/strencodings.h:.*atoi"
"src/util/system.cpp:.*atoi"
"src/util/system.cpp:.*fprintf"
)

REGEXP_IGNORE_EXTERNAL_DEPENDENCIES="^src/(crypto/ctaes/|leveldb/|secp256k1/|tinyformat.h|univalue/)"
Expand Down