@@ -311,6 +311,24 @@ int main(int argc, char *argv[])
311311 .arg (QString::fromStdString (GetArg (" -datadir" , " " ))));
312312 return EXIT_FAILURE;
313313 }
314+
315+ // This check must be done before logging is initialized or the config file is read. We do not want another
316+ // instance writing into an already running Gridcoin instance's logs. This is checked in init too,
317+ // but that is too late.
318+ fs::path dataDir = GetDataDir ();
319+
320+ if (!LockDirectory (dataDir, " .lock" , false )) {
321+ std::string str = strprintf (_ (" Cannot obtain a lock on data directory %s. %s is probably already running "
322+ " and using that directory." ),
323+ dataDir, PACKAGE_NAME);
324+ ThreadSafeMessageBox (str, _ (" Gridcoin" ), CClientUIInterface::OK | CClientUIInterface::MODAL);
325+ QMessageBox::critical (nullptr , PACKAGE_NAME,
326+ QObject::tr (" Error: Cannot obtain a lock on the specified data directory. "
327+ " An instance is probably already using that directory." ));
328+
329+ return EXIT_FAILURE;
330+ }
331+
314332 if (!ReadConfigFile (mapArgs, mapMultiArgs)) {
315333 ThreadSafeMessageBox (strprintf (" Error reading configuration file.\n " ),
316334 " " , CClientUIInterface::ICON_ERROR | CClientUIInterface::OK | CClientUIInterface::MODAL);
@@ -327,34 +345,23 @@ int main(int argc, char *argv[])
327345 // Do this early as we don't want to bother initializing if we are just calling IPC
328346 ipcScanRelay (argc, argv);
329347
330- // Here we do it if it was started with the snapshot argument and we not TestNet
348+ // Run snapshot main if Gridcoin was started with the snapshot argument and we are not TestNet
331349 if (mapArgs.count (" -snapshotdownload" ) && !mapArgs.count (" -testnet" ))
332350 {
333351 GRC::Upgrade snapshot;
334352
335- // Let's check make sure gridcoin is not already running in the data directory.
336- if (!LockDirectory (GetDataDir (), " .lock" , false ))
353+ try
337354 {
338- fprintf (stderr, " Cannot obtain a lock on data directory %s. Gridcoin is probably already running." , GetDataDir ().string ().c_str ());
339-
340- exit (1 );
355+ snapshot.SnapshotMain ();
341356 }
342- else
343- {
344- try
345- {
346- snapshot.SnapshotMain ();
347- }
348-
349- catch (std::runtime_error& e)
350- {
351- LogPrintf (" Snapshot Downloader: Runtime exception occurred in SnapshotMain() (%s)" , e.what ());
352357
353- snapshot.DeleteSnapshot ();
358+ catch (std::runtime_error& e)
359+ {
360+ LogPrintf (" Snapshot Downloader: Runtime exception occurred in SnapshotMain() (%s)" , e.what ());
354361
355- exit (1 );
356- }
362+ snapshot.DeleteSnapshot ();
357363
364+ return EXIT_FAILURE;
358365 }
359366
360367 // Delete snapshot regardless of result.
@@ -405,7 +412,7 @@ int main(int argc, char *argv[])
405412 Snapshot.DeleteSnapshot ();
406413 }
407414
408- return 0 ;
415+ return EXIT_SUCCESS ;
409416}
410417
411418int StartGridcoinQt (int argc, char *argv[], QApplication& app)
@@ -444,7 +451,7 @@ int StartGridcoinQt(int argc, char *argv[], QApplication& app)
444451 {
445452 GUIUtil::HelpMessageBox help;
446453 help.showOrPrint ();
447- return 1 ;
454+ return EXIT_FAILURE ;
448455 }
449456
450457 QSplashScreen splash (QPixmap (" :/images/splash" ), 0 );
@@ -469,7 +476,7 @@ int StartGridcoinQt(int argc, char *argv[], QApplication& app)
469476 if (!threads->createThread (ThreadAppInit2,threads," AppInit2 Thread" ))
470477 {
471478 LogPrintf (" Error; NewThread(ThreadAppInit2) failed" );
472- return 1 ;
479+ return EXIT_FAILURE ;
473480 }
474481 else
475482 {
@@ -549,7 +556,7 @@ int StartGridcoinQt(int argc, char *argv[], QApplication& app)
549556 threads->removeAll ();
550557 threads.reset ();
551558
552- return 0 ;
559+ return EXIT_SUCCESS ;
553560}
554561
555562#endif // BITCOIN_QT_TEST
0 commit comments