diff options
author | Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> | 2019-02-02 00:40:36 +0200 |
---|---|---|
committer | Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> | 2019-02-02 01:07:23 +0200 |
commit | 561e375c73a37934fe77a519762d81edf7a3325c (patch) | |
tree | f76b7e57e4d74ccbbbfbf0cd6a09f37adf4dab28 | |
parent | 745a2ace18ce857bc712d7e66c8bad7c082c07e2 (diff) |
Make PID file creating errors fatal
-rw-r--r-- | src/init.cpp | 21 | ||||
-rw-r--r-- | src/util/system.cpp | 10 | ||||
-rw-r--r-- | src/util/system.h | 1 | ||||
-rwxr-xr-x | test/lint/lint-locale-dependence.sh | 2 |
4 files changed, 21 insertions, 13 deletions
diff --git a/src/init.cpp b/src/init.cpp index a5cd4d3cd1..f8be487cb7 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -53,6 +53,8 @@ #include <stdio.h> #ifndef WIN32 +#include <attributes.h> +#include <cerrno> #include <signal.h> #include <sys/stat.h> #endif @@ -1192,12 +1194,29 @@ bool AppInitLockDataDirectory() 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) { 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())) { diff --git a/src/util/system.cpp b/src/util/system.cpp index 06317a3a90..8268456919 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -963,16 +963,6 @@ 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) diff --git a/src/util/system.h b/src/util/system.h index 5932e55793..44ba740e4e 100644 --- a/src/util/system.h +++ b/src/util/system.h @@ -86,7 +86,6 @@ 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); diff --git a/test/lint/lint-locale-dependence.sh b/test/lint/lint-locale-dependence.sh index 1534d5ef68..2b6c78c2c8 100755 --- a/test/lint/lint-locale-dependence.sh +++ b/test/lint/lint-locale-dependence.sh @@ -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" @@ -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/)" |