Skip to content

Commit 04d0c77

Browse files
committed
Correct datadir lock detection in bitcoin.cpp
This does an early check for the ability to lock the data directory and corrects a segfault condition if the lock is not obtainable.
1 parent 018b1a8 commit 04d0c77

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

src/qt/bitcoin.cpp

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,24 @@ int main(int argc, char *argv[])
315315
.arg(QString::fromStdString(GetArg("-datadir", ""))));
316316
return EXIT_FAILURE;
317317
}
318+
319+
// This check must be done before logging is initialized or the config file is read. We do not want another
320+
// instance writing into an already running Gridcoin instance's logs. This is checked in init too,
321+
// but that is too late.
322+
fs::path dataDir = GetDataDir();
323+
324+
if (!LockDirectory(dataDir, ".lock", false)) {
325+
std::string str = strprintf(_("Cannot obtain a lock on data directory %s. %s is probably already running "
326+
"and using that directory."),
327+
dataDir, PACKAGE_NAME);
328+
ThreadSafeMessageBox(str, _("Gridcoin"), CClientUIInterface::OK | CClientUIInterface::MODAL);
329+
QMessageBox::critical(nullptr, PACKAGE_NAME,
330+
QObject::tr("Error: Cannot obtain a lock on the specified data directory. "
331+
"An instance is probably already using that directory."));
332+
333+
return EXIT_FAILURE;
334+
}
335+
318336
if (!ReadConfigFile(mapArgs, mapMultiArgs)) {
319337
ThreadSafeMessageBox(strprintf("Error reading configuration file.\n"),
320338
"", CClientUIInterface::ICON_ERROR | CClientUIInterface::OK | CClientUIInterface::MODAL);
@@ -331,34 +349,23 @@ int main(int argc, char *argv[])
331349
// Do this early as we don't want to bother initializing if we are just calling IPC
332350
ipcScanRelay(argc, argv);
333351

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

339-
// Let's check make sure gridcoin is not already running in the data directory.
340-
if (!LockDirectory(GetDataDir(), ".lock", false))
357+
try
341358
{
342-
fprintf(stderr, "Cannot obtain a lock on data directory %s. Gridcoin is probably already running.", GetDataDir().string().c_str());
343-
344-
exit(1);
359+
snapshot.SnapshotMain();
345360
}
346-
else
347-
{
348-
try
349-
{
350-
snapshot.SnapshotMain();
351-
}
352-
353-
catch (std::runtime_error& e)
354-
{
355-
LogPrintf("Snapshot Downloader: Runtime exception occurred in SnapshotMain() (%s)", e.what());
356361

357-
snapshot.DeleteSnapshot();
362+
catch (std::runtime_error& e)
363+
{
364+
LogPrintf("Snapshot Downloader: Runtime exception occurred in SnapshotMain() (%s)", e.what());
358365

359-
exit(1);
360-
}
366+
snapshot.DeleteSnapshot();
361367

368+
return EXIT_FAILURE;
362369
}
363370

364371
// Delete snapshot regardless of result.
@@ -409,7 +416,7 @@ int main(int argc, char *argv[])
409416
Snapshot.DeleteSnapshot();
410417
}
411418

412-
return 0;
419+
return EXIT_SUCCESS;
413420
}
414421

415422
int StartGridcoinQt(int argc, char *argv[], QApplication& app, OptionsModel& optionsModel)
@@ -445,7 +452,7 @@ int StartGridcoinQt(int argc, char *argv[], QApplication& app, OptionsModel& opt
445452
{
446453
GUIUtil::HelpMessageBox help;
447454
help.showOrPrint();
448-
return 1;
455+
return EXIT_FAILURE;
449456
}
450457

451458
QSplashScreen splash(QPixmap(":/images/splash"), 0);
@@ -470,7 +477,7 @@ int StartGridcoinQt(int argc, char *argv[], QApplication& app, OptionsModel& opt
470477
if (!threads->createThread(ThreadAppInit2,threads,"AppInit2 Thread"))
471478
{
472479
LogPrintf("Error; NewThread(ThreadAppInit2) failed");
473-
return 1;
480+
return EXIT_FAILURE;
474481
}
475482
else
476483
{
@@ -550,7 +557,7 @@ int StartGridcoinQt(int argc, char *argv[], QApplication& app, OptionsModel& opt
550557
threads->removeAll();
551558
threads.reset();
552559

553-
return 0;
560+
return EXIT_SUCCESS;
554561
}
555562

556563
#endif // BITCOIN_QT_TEST

0 commit comments

Comments
 (0)