Skip to content

Commit 3f5ad62

Browse files
riordantjoernroeder
andcommitted
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 Co-authored-by: Jörn Röder <[email protected]>
1 parent b853746 commit 3f5ad62

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)