Skip to content

Commit b4fc525

Browse files
committed
Merge #15456: Enable PID file creation on WIN
3f5ad62 Enable PID file creation on Windows - Add available WIN PID function - Consider WIN32 in each relevant case - Add new preprocessor definitions to suppress warning - Update error message for generic OS (riordant) Pull request description: # Introduction As discussed with @laanwj on IRC: - PID file creation was never enabled for Windows, as the `pid_t` filetype is not available for it. However, the WIN32 API contains the header [`Processthreadsapi.h`](https://github.com/CodeShark/x86_64-w64-mingw32/blob/master/include/processthreadsapi.h) which in turn contains the function [`GetCurrentProcessId()`](https://docs.microsoft.com/en-gb/windows/desktop/api/processthreadsapi/nf-processthreadsapi-getcurrentprocessid). ~~This function is called at a higher level by [`_getpid()`](https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getpid?view=vs-2017)~~ EDIT: `_getpid()` is not available to the MSVC compiler used in the AppVeyor build. As a result, I have changed the function call to`GetCurrentProcessId()`, which performs the same function and is available to both MinGW & MSVC. This allows one to capture the PID in Windows, without any additional includes - the above function is already available. - Within this PR, I have added a separate line that calls `GetCurrentProcessId()` in the case of a WIN compilation, and the usual `getpid()` otherwise. All code blocks processing PID file logic that avoid WIN32 have been changed to consider it. I have also updated the preprocessor definitions in `libbitcoin_server.vcxproj.in` to suppress a warning related to `std::strerror` for the MSVC build, that was causing the AppVeyor build to fail (see @fanquake comment below). # Rationale - Consistency between OS's running Bitcoin - Applications which build off of `bitcoind`, such as novel front-end clients, often need access to the PID in order to control the daemon. Instead of designing some alternate way of doing this for one system, it should be consistent between all of them. In collaboration with @joernroeder Tree-SHA512: 22fcbf866e99115d12ed29716e68d200d4c118ae2f7b188b7705dc0cf5f0cd0ce5fb18f772744c6238eecd9e6d0922c615e2f0e12a7fe7c810062a79d97aa6a2
2 parents 1a8a5ed + 3f5ad62 commit b4fc525

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
lines changed

build_msvc/libbitcoin_server/libbitcoin_server.vcxproj.in

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
<PrecompiledHeader>NotUsing</PrecompiledHeader>
9090
<WarningLevel>Level3</WarningLevel>
9191
<Optimization>Disabled</Optimization>
92-
<PreprocessorDefinitions>WIN32;HAVE_CONFIG_H;_SCL_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
92+
<PreprocessorDefinitions>WIN32;HAVE_CONFIG_H;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
9393
<SDLCheck>true</SDLCheck>
9494
<AdditionalIncludeDirectories>..\..\src;..\..\src\univalue\include;..\..\src\leveldb\include;..\..\src\leveldb\helpers\memenv;</AdditionalIncludeDirectories>
9595
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
@@ -104,7 +104,7 @@
104104
<PrecompiledHeader>NotUsing</PrecompiledHeader>
105105
<WarningLevel>Level3</WarningLevel>
106106
<Optimization>Disabled</Optimization>
107-
<PreprocessorDefinitions>WIN32;HAVE_CONFIG_H;_SCL_SECURE_NO_WARNINGS;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
107+
<PreprocessorDefinitions>WIN32;HAVE_CONFIG_H;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
108108
<SDLCheck>true</SDLCheck>
109109
<AdditionalIncludeDirectories>..\..\src;..\..\src\univalue\include;..\..\src\leveldb\include;..\..\src\leveldb\helpers\memenv;</AdditionalIncludeDirectories>
110110
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
@@ -122,7 +122,7 @@
122122
<Optimization>MaxSpeed</Optimization>
123123
<FunctionLevelLinking>true</FunctionLevelLinking>
124124
<IntrinsicFunctions>true</IntrinsicFunctions>
125-
<PreprocessorDefinitions>WIN32;HAVE_CONFIG_H;_SCL_SECURE_NO_WARNINGS;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
125+
<PreprocessorDefinitions>WIN32;HAVE_CONFIG_H;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
126126
<SDLCheck>true</SDLCheck>
127127
<AdditionalIncludeDirectories>..\..\src;..\..\src\univalue\include;..\..\src\leveldb\include;..\..\src\leveldb\helpers\memenv;</AdditionalIncludeDirectories>
128128
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
@@ -141,7 +141,7 @@
141141
<Optimization>MaxSpeed</Optimization>
142142
<FunctionLevelLinking>true</FunctionLevelLinking>
143143
<IntrinsicFunctions>true</IntrinsicFunctions>
144-
<PreprocessorDefinitions>WIN32;HAVE_CONFIG_H;_SCL_SECURE_NO_WARNINGS;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
144+
<PreprocessorDefinitions>WIN32;HAVE_CONFIG_H;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
145145
<SDLCheck>true</SDLCheck>
146146
<AdditionalIncludeDirectories>..\..\src;..\..\src\univalue\include;..\..\src\leveldb\include;..\..\src\leveldb\helpers\memenv;</AdditionalIncludeDirectories>
147147
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>

src/init.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ static const char* FEE_ESTIMATES_FILENAME="fee_estimates.dat";
9797
/**
9898
* The PID file facilities.
9999
*/
100-
#ifndef WIN32
101100
static const char* BITCOIN_PID_FILENAME = "bitcoind.pid";
102101

103102
static fs::path GetPidFile()
@@ -109,14 +108,17 @@ NODISCARD static bool CreatePidFile()
109108
{
110109
FILE* file = fsbridge::fopen(GetPidFile(), "w");
111110
if (file) {
111+
#ifdef WIN32
112+
fprintf(file, "%d\n", GetCurrentProcessId());
113+
#else
112114
fprintf(file, "%d\n", getpid());
115+
#endif
113116
fclose(file);
114117
return true;
115118
} else {
116119
return InitError(strprintf(_("Unable to create the PID file '%s': %s"), GetPidFile().string(), std::strerror(errno)));
117120
}
118121
}
119-
#endif
120122

121123
//////////////////////////////////////////////////////////////////////////////
122124
//
@@ -286,15 +288,13 @@ void Shutdown(InitInterfaces& interfaces)
286288
}
287289
#endif
288290

289-
#ifndef WIN32
290291
try {
291292
if (!fs::remove(GetPidFile())) {
292293
LogPrintf("%s: Unable to remove PID file: File does not exist\n", __func__);
293294
}
294295
} catch (const fs::filesystem_error& e) {
295-
LogPrintf("%s: Unable to remove PID file: %s\n", __func__, e.what());
296+
LogPrintf("%s: Unable to remove PID file: %s\n", __func__, fsbridge::get_filesystem_error_message(e));
296297
}
297-
#endif
298298
interfaces.chain_clients.clear();
299299
UnregisterAllValidationInterfaces();
300300
GetMainSignals().UnregisterBackgroundSignalScheduler();
@@ -392,11 +392,7 @@ void SetupServerArgs()
392392
gArgs.AddArg("-par=<n>", strprintf("Set the number of script verification threads (%u to %d, 0 = auto, <0 = leave that many cores free, default: %d)",
393393
-GetNumCores(), MAX_SCRIPTCHECK_THREADS, DEFAULT_SCRIPTCHECK_THREADS), false, OptionsCategory::OPTIONS);
394394
gArgs.AddArg("-persistmempool", strprintf("Whether to save the mempool on shutdown and load on restart (default: %u)", DEFAULT_PERSIST_MEMPOOL), false, OptionsCategory::OPTIONS);
395-
#ifndef WIN32
396395
gArgs.AddArg("-pid=<file>", strprintf("Specify pid file. Relative paths will be prefixed by a net-specific datadir location. (default: %s)", BITCOIN_PID_FILENAME), false, OptionsCategory::OPTIONS);
397-
#else
398-
hidden_args.emplace_back("-pid");
399-
#endif
400396
gArgs.AddArg("-prune=<n>", strprintf("Reduce storage requirements by enabling pruning (deleting) of old blocks. This allows the pruneblockchain RPC to be called to delete specific blocks, and enables automatic pruning of old blocks if a target size in MiB is provided. This mode is incompatible with -txindex and -rescan. "
401397
"Warning: Reverting this setting requires re-downloading the entire blockchain. "
402398
"(default: 0 = disable pruning blocks, 1 = allow manual pruning via RPC, >=%u = automatically prune block files to stay under the specified target size in MiB)", MIN_DISK_SPACE_FOR_BLOCK_FILES / 1024 / 1024), false, OptionsCategory::OPTIONS);
@@ -1228,12 +1224,10 @@ bool AppInitMain(InitInterfaces& interfaces)
12281224
{
12291225
const CChainParams& chainparams = Params();
12301226
// ********************************************************* Step 4a: application initialization
1231-
#ifndef WIN32
12321227
if (!CreatePidFile()) {
12331228
// Detailed error printed inside CreatePidFile().
12341229
return false;
12351230
}
1236-
#endif
12371231
if (LogInstance().m_print_to_file) {
12381232
if (gArgs.GetBoolArg("-shrinkdebugfile", LogInstance().DefaultShrinkDebugFile())) {
12391233
// Do this first since it both loads a bunch of debug.log into memory,

0 commit comments

Comments
 (0)