aboutsummaryrefslogtreecommitdiff
path: root/init.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'init.cpp')
-rw-r--r--init.cpp16
1 files changed, 12 insertions, 4 deletions
diff --git a/init.cpp b/init.cpp
index 98410d1889..b10e22a329 100644
--- a/init.cpp
+++ b/init.cpp
@@ -244,8 +244,7 @@ bool AppInit2(int argc, char* argv[])
// Required to protect the database files if we're going to keep deleting log.*
//
#if defined(__WXMSW__) && defined(GUI)
- // todo: wxSingleInstanceChecker wasn't working on Linux, never deleted its lock file
- // maybe should go by whether successfully bind port 8333 instead
+ // wxSingleInstanceChecker doesn't work on Linux
wxString strMutexName = wxString("bitcoin_running.") + getenv("HOMEPATH");
for (int i = 0; i < strMutexName.size(); i++)
if (!isalnum(strMutexName[i]))
@@ -257,7 +256,6 @@ bool AppInit2(int argc, char* argv[])
unsigned int nStart = GetTime();
loop
{
- // TODO: find out how to do this in Linux, or replace with wxWidgets commands
// Show the previous instance and exit
HWND hwndPrev = FindWindowA("wxWindowClassNR", "Bitcoin");
if (hwndPrev)
@@ -281,8 +279,18 @@ bool AppInit2(int argc, char* argv[])
}
#endif
+ // Make sure only a single bitcoin process is using the data directory.
+ string strLockFile = GetDataDir() + "/.lock";
+ FILE* file = fopen(strLockFile.c_str(), "a"); // empty lock file; created if it doesn't exist.
+ fclose(file);
+ static boost::interprocess::file_lock lock(strLockFile.c_str());
+ if (!lock.try_lock())
+ {
+ wxMessageBox(strprintf(_("Cannot obtain a lock on data directory %s. Bitcoin is probably already running."), GetDataDir().c_str()), "Bitcoin");
+ return false;
+ }
+
// Bind to the port early so we can tell if another instance is already running.
- // This is a backup to wxSingleInstanceChecker, which doesn't work on Linux.
string strErrors;
if (!BindListenPort(strErrors))
{