Skip to content

Commit 3e1ca13

Browse files
committed
Merge #15278: Improve PID file error handling
3782075 Move all PID file stuff to init.cpp (Hennadii Stepanov) 561e375 Make PID file creating errors fatal (Hennadii Stepanov) 745a2ac Improve PID file removing errors logging (Hennadii Stepanov) Pull request description: Digging into #15240 the lack of the proper logging has been discovered. Fixed by this PR. UPDATE (inspired by @laanwj's [comment](bitcoin/bitcoin#15278 (comment))): Not being able to create the PID file is fatal now. Output of `bitcoind`: ``` $ src/bitcoind -pid=/run/bitcoind/bitcoind.pid 2019-02-01T23:20:10Z Bitcoin Core version v0.17.99.0-561e375c7 (release build) 2019-02-01T23:20:10Z Assuming ancestors of block 0000000000000037a8cd3e06cd5edbfe9dd1dbcc5dacab279376ef7cfc2b4c75 have valid signatures. 2019-02-01T23:20:10Z Setting nMinimumChainWork=00000000000000000000000000000000000000000000007dbe94253893cbd463 2019-02-01T23:20:10Z Using the 'sse4(1way),sse41(4way),avx2(8way)' SHA256 implementation 2019-02-01T23:20:10Z Using RdRand as an additional entropy source 2019-02-01T23:20:11Z Error: Unable to create the PID file '/run/bitcoind/bitcoind.pid': No such file or directory Error: Unable to create the PID file '/run/bitcoind/bitcoind.pid': No such file or directory 2019-02-01T23:20:11Z Shutdown: In progress... 2019-02-01T23:20:11Z Shutdown: Unable to remove PID file: File does not exist 2019-02-01T23:20:11Z Shutdown: done ``` Output of `bitcoin-qt`: ![screenshot from 2019-02-02 01-19-05](https://user-images.githubusercontent.com/32963518/52154886-9349b600-2688-11e9-8128-470f16790305.png) **Notes for reviewers** 1. `CreatePidFile()` has been moved from `util/system.cpp` to `init.cpp` for the following reasons: - to get the ability to use `InitError()` - now `init.cpp` contains code of both creating PID file and removing it 2. Regarding 0.18 release process: this PR modifies 1 string and introduces 2 new ones. Tree-SHA512: ac07d0f800e61ec759e427d0afc0ca43d67f232e977662253963afdd0a220d34b871050f58149fc9fabd427bfc8e0d3f6a6032f2a38f30ad366fc0d074b0f2b3
2 parents f9775a8 + 3782075 commit 3e1ca13

File tree

4 files changed

+35
-27
lines changed

4 files changed

+35
-27
lines changed

src/init.cpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
#include <stdio.h>
5454

5555
#ifndef WIN32
56+
#include <attributes.h>
57+
#include <cerrno>
5658
#include <signal.h>
5759
#include <sys/stat.h>
5860
#endif
@@ -92,6 +94,30 @@ std::unique_ptr<BanMan> g_banman;
9294

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

97+
/**
98+
* The PID file facilities.
99+
*/
100+
#ifndef WIN32
101+
static const char* BITCOIN_PID_FILENAME = "bitcoind.pid";
102+
103+
static fs::path GetPidFile()
104+
{
105+
return AbsPathForConfigVal(fs::path(gArgs.GetArg("-pid", BITCOIN_PID_FILENAME)));
106+
}
107+
108+
NODISCARD static bool CreatePidFile()
109+
{
110+
FILE* file = fsbridge::fopen(GetPidFile(), "w");
111+
if (file) {
112+
fprintf(file, "%d\n", getpid());
113+
fclose(file);
114+
return true;
115+
} else {
116+
return InitError(strprintf(_("Unable to create the PID file '%s': %s"), GetPidFile().string(), std::strerror(errno)));
117+
}
118+
}
119+
#endif
120+
95121
//////////////////////////////////////////////////////////////////////////////
96122
//
97123
// Shutdown
@@ -262,9 +288,11 @@ void Shutdown(InitInterfaces& interfaces)
262288

263289
#ifndef WIN32
264290
try {
265-
fs::remove(GetPidFile());
291+
if (!fs::remove(GetPidFile())) {
292+
LogPrintf("%s: Unable to remove PID file: File does not exist\n", __func__);
293+
}
266294
} catch (const fs::filesystem_error& e) {
267-
LogPrintf("%s: Unable to remove pidfile: %s\n", __func__, e.what());
295+
LogPrintf("%s: Unable to remove PID file: %s\n", __func__, e.what());
268296
}
269297
#endif
270298
interfaces.chain_clients.clear();
@@ -1201,7 +1229,10 @@ bool AppInitMain(InitInterfaces& interfaces)
12011229
const CChainParams& chainparams = Params();
12021230
// ********************************************************* Step 4a: application initialization
12031231
#ifndef WIN32
1204-
CreatePidFile(GetPidFile(), getpid());
1232+
if (!CreatePidFile()) {
1233+
// Detailed error printed inside CreatePidFile().
1234+
return false;
1235+
}
12051236
#endif
12061237
if (LogInstance().m_print_to_file) {
12071238
if (gArgs.GetBoolArg("-shrinkdebugfile", LogInstance().DefaultShrinkDebugFile())) {

src/util/system.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
const int64_t nStartupTime = GetTime();
7575

7676
const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
77-
const char * const BITCOIN_PID_FILENAME = "bitcoind.pid";
7877

7978
ArgsManager gArgs;
8079

@@ -965,23 +964,6 @@ std::string ArgsManager::GetChainName() const
965964
return CBaseChainParams::MAIN;
966965
}
967966

968-
#ifndef WIN32
969-
fs::path GetPidFile()
970-
{
971-
return AbsPathForConfigVal(fs::path(gArgs.GetArg("-pid", BITCOIN_PID_FILENAME)));
972-
}
973-
974-
void CreatePidFile(const fs::path &path, pid_t pid)
975-
{
976-
FILE* file = fsbridge::fopen(path, "w");
977-
if (file)
978-
{
979-
fprintf(file, "%d\n", pid);
980-
fclose(file);
981-
}
982-
}
983-
#endif
984-
985967
bool RenameOver(fs::path src, fs::path dest)
986968
{
987969
#ifdef WIN32

src/util/system.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
int64_t GetStartupTime();
4141

4242
extern const char * const BITCOIN_CONF_FILENAME;
43-
extern const char * const BITCOIN_PID_FILENAME;
4443

4544
/** Translate a message to the native language of the user. */
4645
const extern std::function<std::string(const char*)> G_TRANSLATION_FUN;
@@ -86,10 +85,6 @@ const fs::path &GetBlocksDir();
8685
const fs::path &GetDataDir(bool fNetSpecific = true);
8786
void ClearDatadirCache();
8887
fs::path GetConfigFile(const std::string& confPath);
89-
#ifndef WIN32
90-
fs::path GetPidFile();
91-
void CreatePidFile(const fs::path &path, pid_t pid);
92-
#endif
9388
#ifdef WIN32
9489
fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
9590
#endif

test/lint/lint-locale-dependence.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ KNOWN_VIOLATIONS=(
88
"src/dbwrapper.cpp:.*vsnprintf"
99
"src/httprpc.cpp.*trim"
1010
"src/init.cpp:.*atoi"
11+
"src/init.cpp:.*fprintf"
1112
"src/qt/rpcconsole.cpp:.*atoi"
1213
"src/rest.cpp:.*strtol"
1314
"src/test/dbwrapper_tests.cpp:.*snprintf"
@@ -18,7 +19,6 @@ KNOWN_VIOLATIONS=(
1819
"src/util/strencodings.cpp:.*strtoul"
1920
"src/util/strencodings.h:.*atoi"
2021
"src/util/system.cpp:.*atoi"
21-
"src/util/system.cpp:.*fprintf"
2222
)
2323

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

0 commit comments

Comments
 (0)