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 and datadir handling #2726

Merged
merged 5 commits into from
Jan 20, 2022
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
32 changes: 30 additions & 2 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
#include <memory>

#ifndef WIN32
#include <attributes.h>
#include <cerrno>
#include <signal.h>
#include <sys/stat.h>
#endif
Expand Down Expand Up @@ -114,6 +116,29 @@ static const char* DEFAULT_ASMAP_FILENAME="ip_asn.map";
static const char* FEE_ESTIMATES_FILENAME = "fee_estimates.dat";
CClientUIInterface uiInterface; // Declared but not defined in guiinterface.h

/**
* The PID file facilities.
*/
const char * const PIVX_PID_FILENAME = "pivx.pid";

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

NODISCARD static bool CreatePidFile()
{
FILE* file = fsbridge::fopen(GetPidFile(), "w");
if (file) {
fprintf(file, "%d\n", getpid());
fclose(file);
return true;
} else {
return UIError(strprintf(_("Unable to create the PID file '%s': %s"), GetPidFile().string(), std::strerror(errno)));
}
}

//////////////////////////////////////////////////////////////////////////////
//
// Shutdown
Expand Down Expand Up @@ -309,7 +334,7 @@ void Shutdown()
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

Expand Down Expand Up @@ -1168,7 +1193,10 @@ bool AppInitMain()
}

#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
2 changes: 1 addition & 1 deletion src/pivx-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static bool AppInitRPC(int argc, char* argv[])
fprintf(stdout, "%s", strUsage.c_str());
return false;
}
if (!fs::is_directory(GetDataDir(false))) {
if (!CheckDataDirOption()) {
fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "").c_str());
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/pivxd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ bool AppInit(int argc, char* argv[])
}

try {
if (!fs::is_directory(GetDataDir(false))) {
if (!CheckDataDirOption()) {
fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "").c_str());
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/qt/pivx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,9 +613,9 @@ int main(int argc, char* argv[])
if (!Intro::pickDataDirectory())
return 0;

/// 6. Determine availability of data and blocks directory and parse pivx.conf
/// 6. Determine availability of data directory and parse pivx.conf
/// - Do not call GetDataDir(true) before this step finishes
if (!fs::is_directory(GetDataDir(false))) {
if (!CheckDataDirOption()) {
QMessageBox::critical(nullptr, PACKAGE_NAME,
QObject::tr("Error: Specified data directory \"%1\" does not exist.").arg(QString::fromStdString(gArgs.GetArg("-datadir", ""))));
return 1;
Expand Down
34 changes: 11 additions & 23 deletions src/util/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
#endif

const char * const PIVX_CONF_FILENAME = "pivx.conf";
const char * const PIVX_PID_FILENAME = "pivx.pid";
const char * const PIVX_MASTERNODE_CONF_FILENAME = "masternode.conf";


Expand Down Expand Up @@ -730,11 +729,11 @@ const fs::path& GetDataDir(bool fNetSpecific)

// This can be called during exceptions by LogPrintf(), so we cache the
// value so we don't have to do memory allocations after that.
if (!path.empty())
return path;
if (!path.empty()) return path;

if (gArgs.IsArgSet("-datadir")) {
path = fs::system_complete(gArgs.GetArg("-datadir", ""));
std::string datadir = gArgs.GetArg("-datadir", "");
if (!datadir.empty()) {
path = fs::system_complete(datadir);
if (!fs::is_directory(path)) {
path = "";
return path;
Expand All @@ -753,6 +752,12 @@ const fs::path& GetDataDir(bool fNetSpecific)
return path;
}

bool CheckDataDirOption()
{
std::string datadir = gArgs.GetArg("-datadir", "");
return datadir.empty() || fs::is_directory(fs::system_complete(datadir));
}

void ClearDatadirCache()
{
LOCK(csPathCached);
Expand Down Expand Up @@ -840,7 +845,7 @@ void ArgsManager::ReadConfigFile(const std::string& confPath)

// If datadir is changed in .conf file:
ClearDatadirCache();
if (!fs::is_directory(GetDataDir(false))) {
if (!CheckDataDirOption()) {
throw std::runtime_error(strprintf("specified data directory \"%s\" does not exist.", gArgs.GetArg("-datadir", "").c_str()));
}
}
Expand All @@ -867,23 +872,6 @@ std::string ArgsManager::GetChainName() const
return CBaseChainParams::MAIN;
}

#ifndef WIN32
fs::path GetPidFile()
{
fs::path pathPidFile(gArgs.GetArg("-pid", PIVX_PID_FILENAME));
return AbsPathForConfigVal(pathPidFile);
}

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
6 changes: 2 additions & 4 deletions src/util/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,15 @@ fs::path GetDefaultDataDir();
// The blocks directory is always net specific.
const fs::path &GetBlocksDir();
const fs::path &GetDataDir(bool fNetSpecific = true);
// Return true if -datadir option points to a valid directory or is not specified.
bool CheckDataDirOption();
// Sapling network dir
const fs::path &ZC_GetParamsDir();
// Init sapling library
void initZKSNARKS();
void ClearDatadirCache();
fs::path GetConfigFile(const std::string& confPath);
fs::path GetMasternodeConfigFile();
#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