@@ -315,6 +315,24 @@ int main(int argc, char *argv[])
315
315
.arg (QString::fromStdString (GetArg (" -datadir" , " " ))));
316
316
return EXIT_FAILURE;
317
317
}
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
+
318
336
if (!ReadConfigFile (mapArgs, mapMultiArgs)) {
319
337
ThreadSafeMessageBox (strprintf (" Error reading configuration file.\n " ),
320
338
" " , CClientUIInterface::ICON_ERROR | CClientUIInterface::OK | CClientUIInterface::MODAL);
@@ -331,34 +349,23 @@ int main(int argc, char *argv[])
331
349
// Do this early as we don't want to bother initializing if we are just calling IPC
332
350
ipcScanRelay (argc, argv);
333
351
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
335
353
if (mapArgs.count (" -snapshotdownload" ) && !mapArgs.count (" -testnet" ))
336
354
{
337
355
GRC::Upgrade snapshot;
338
356
339
- // Let's check make sure gridcoin is not already running in the data directory.
340
- if (!LockDirectory (GetDataDir (), " .lock" , false ))
357
+ try
341
358
{
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 ();
345
360
}
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 ());
356
361
357
- snapshot.DeleteSnapshot ();
362
+ catch (std::runtime_error& e)
363
+ {
364
+ LogPrintf (" Snapshot Downloader: Runtime exception occurred in SnapshotMain() (%s)" , e.what ());
358
365
359
- exit (1 );
360
- }
366
+ snapshot.DeleteSnapshot ();
361
367
368
+ return EXIT_FAILURE;
362
369
}
363
370
364
371
// Delete snapshot regardless of result.
@@ -409,7 +416,7 @@ int main(int argc, char *argv[])
409
416
Snapshot.DeleteSnapshot ();
410
417
}
411
418
412
- return 0 ;
419
+ return EXIT_SUCCESS ;
413
420
}
414
421
415
422
int StartGridcoinQt (int argc, char *argv[], QApplication& app, OptionsModel& optionsModel)
@@ -445,7 +452,7 @@ int StartGridcoinQt(int argc, char *argv[], QApplication& app, OptionsModel& opt
445
452
{
446
453
GUIUtil::HelpMessageBox help;
447
454
help.showOrPrint ();
448
- return 1 ;
455
+ return EXIT_FAILURE ;
449
456
}
450
457
451
458
QSplashScreen splash (QPixmap (" :/images/splash" ), 0 );
@@ -470,7 +477,7 @@ int StartGridcoinQt(int argc, char *argv[], QApplication& app, OptionsModel& opt
470
477
if (!threads->createThread (ThreadAppInit2,threads," AppInit2 Thread" ))
471
478
{
472
479
LogPrintf (" Error; NewThread(ThreadAppInit2) failed" );
473
- return 1 ;
480
+ return EXIT_FAILURE ;
474
481
}
475
482
else
476
483
{
@@ -550,7 +557,7 @@ int StartGridcoinQt(int argc, char *argv[], QApplication& app, OptionsModel& opt
550
557
threads->removeAll ();
551
558
threads.reset ();
552
559
553
- return 0 ;
560
+ return EXIT_SUCCESS ;
554
561
}
555
562
556
563
#endif // BITCOIN_QT_TEST
0 commit comments