Skip to content

Commit

Permalink
Correct datadir lock detection in bitcoin.cpp
Browse files Browse the repository at this point in the history
This does an early check for the ability to lock the data directory
and corrects a segfault condition if the lock is not obtainable.
  • Loading branch information
jamescowens committed Dec 5, 2020
1 parent 32f7491 commit d7c0573
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions src/qt/bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,24 @@ int main(int argc, char *argv[])
.arg(QString::fromStdString(GetArg("-datadir", ""))));
return EXIT_FAILURE;
}

// This check must be done before logging is initialized or the config file is read. We do not want another
// instance writing into an already running Gridcoin instance's logs. This is checked in init too,
// but that is too late.
fs::path dataDir = GetDataDir();

if (!LockDirectory(dataDir, ".lock", false)) {
std::string str = strprintf(_("Cannot obtain a lock on data directory %s. %s is probably already running "
"and using that directory."),
dataDir, PACKAGE_NAME);
ThreadSafeMessageBox(str, _("Gridcoin"), CClientUIInterface::OK | CClientUIInterface::MODAL);
QMessageBox::critical(nullptr, PACKAGE_NAME,
QObject::tr("Error: Cannot obtain a lock on the specified data directory. "
"An instance is probably already using that directory."));

return EXIT_FAILURE;
}

if (!ReadConfigFile(mapArgs, mapMultiArgs)) {
ThreadSafeMessageBox(strprintf("Error reading configuration file.\n"),
"", CClientUIInterface::ICON_ERROR | CClientUIInterface::OK | CClientUIInterface::MODAL);
Expand All @@ -327,34 +345,23 @@ int main(int argc, char *argv[])
// Do this early as we don't want to bother initializing if we are just calling IPC
ipcScanRelay(argc, argv);

// Here we do it if it was started with the snapshot argument and we not TestNet
// Run snapshot main if Gridcoin was started with the snapshot argument and we are not TestNet
if (mapArgs.count("-snapshotdownload") && !mapArgs.count("-testnet"))
{
GRC::Upgrade snapshot;

// Let's check make sure gridcoin is not already running in the data directory.
if (!LockDirectory(GetDataDir(), ".lock", false))
try
{
fprintf(stderr, "Cannot obtain a lock on data directory %s. Gridcoin is probably already running.", GetDataDir().string().c_str());

exit(1);
snapshot.SnapshotMain();
}
else
{
try
{
snapshot.SnapshotMain();
}

catch (std::runtime_error& e)
{
LogPrintf("Snapshot Downloader: Runtime exception occurred in SnapshotMain() (%s)", e.what());

snapshot.DeleteSnapshot();
catch (std::runtime_error& e)
{
LogPrintf("Snapshot Downloader: Runtime exception occurred in SnapshotMain() (%s)", e.what());

exit(1);
}
snapshot.DeleteSnapshot();

return EXIT_FAILURE;
}

// Delete snapshot regardless of result.
Expand Down Expand Up @@ -405,7 +412,7 @@ int main(int argc, char *argv[])
Snapshot.DeleteSnapshot();
}

return 0;
return EXIT_SUCCESS;
}

int StartGridcoinQt(int argc, char *argv[], QApplication& app)
Expand Down Expand Up @@ -444,7 +451,7 @@ int StartGridcoinQt(int argc, char *argv[], QApplication& app)
{
GUIUtil::HelpMessageBox help;
help.showOrPrint();
return 1;
return EXIT_FAILURE;
}

QSplashScreen splash(QPixmap(":/images/splash"), 0);
Expand All @@ -469,7 +476,7 @@ int StartGridcoinQt(int argc, char *argv[], QApplication& app)
if (!threads->createThread(ThreadAppInit2,threads,"AppInit2 Thread"))
{
LogPrintf("Error; NewThread(ThreadAppInit2) failed");
return 1;
return EXIT_FAILURE;
}
else
{
Expand Down Expand Up @@ -549,7 +556,7 @@ int StartGridcoinQt(int argc, char *argv[], QApplication& app)
threads->removeAll();
threads.reset();

return 0;
return EXIT_SUCCESS;
}

#endif // BITCOIN_QT_TEST

0 comments on commit d7c0573

Please sign in to comment.