message: init: Try to aquire datadir lock before and after daemonization
diff: @@ -1128,33 +1128,25 @@ bool AppInitParameterInteraction()
  return true;
  }
  -bool AppInitSanityChecks()
  +static bool LockDataDirectory(bool probeOnly)
  {
  - // ********************************************************* Step 4: sanity checks
  -
  - // Initialize elliptic curve code
  - RandomInit();
  - ECC_Start();
  - globalVerifyHandle.reset(new ECCVerifyHandle());
  -
  - // Sanity check
  - if (!InitSanityCheck())
  - return UIError(_("Initialization sanity check failed. Rapids Core is shutting down."));
  -
  std::string strDataDir = GetDataDir().string();
  // Make sure only a single RPD process is using the data directory.
  fs::path pathLockFile = GetDataDir() / ".lock";
  FILE* file = fsbridge::fopen(pathLockFile, "a"); // empty lock file; created if it doesn't exist.
  if (file) fclose(file);
  - static boost::interprocess::file_lock lock(pathLockFile.string().c_str());
  try {
  + static boost::interprocess::file_lock lock(pathLockFile.string().c_str());
  // Wait maximum 10 seconds if an old wallet is still running. Avoids lockup during restart
  if (!lock.timed_lock(boost::get_system_time() + boost::posix_time::seconds(10))) {
  return UIError(strprintf(_("Cannot obtain a lock on data directory %s. %s is probably already running."),
  strDataDir, _(PACKAGE_NAME)));
  }
  + if (probeOnly) {
  + lock.unlock();
  + }
  } catch (const boost::interprocess::interprocess_exception& e) {
  return UIError(
  strprintf(_("Cannot obtain a lock on data directory %s. %s is probably already running.") + " %s.",
  @@ -1163,9 +1155,33 @@ bool AppInitSanityChecks()
  return true;
  }
  +bool AppInitSanityChecks()
  +{
  + // ********************************************************* Step 4: sanity checks
  +
  + // Initialize elliptic curve code
  + RandomInit();
  + ECC_Start();
  + globalVerifyHandle.reset(new ECCVerifyHandle());
  +
  + // Sanity check
  + if (!InitSanityCheck())
  + return UIError(strprintf(_("Initialization sanity check failed. %s is shutting down."), _(PACKAGE_NAME)));
  +
  + // Probe the data directory lock to give an early error message, if possible
  + return LockDataDirectory(true);
  +}
  +
  bool AppInitMain()
  {
  // ********************************************************* Step 4a: application initialization
  + // After daemonization get the data directory lock again and hold on to it until exit
  + // This creates a slight window for a race condition to happen, however this condition is harmless: it
  + // will at most make us exit without printing a message to console.
  + if (!LockDataDirectory(false)) {
  + // Detailed error printed inside LockDataDirectory
  + return false;
  + }
  #ifndef WIN32
  CreatePidFile(GetPidFile(), getpid());
  