diff options
author | s_nakamoto <s_nakamoto@1a98c847-1fd6-4fd8-948a-caf3550aa51b> | 2010-10-20 16:12:23 +0000 |
---|---|---|
committer | s_nakamoto <s_nakamoto@1a98c847-1fd6-4fd8-948a-caf3550aa51b> | 2010-10-20 16:12:23 +0000 |
commit | d9c6b09ac42d21abd151a0feaff676b8a8a24bb9 (patch) | |
tree | c855864e24f765881448e581fca30945207ba34e /init.cpp | |
parent | 2ea5fa07105ef52ba94c43e6a70a14ef6008abb0 (diff) |
Gavin Andresen: multiple instance check, -testnet use port 18333
git-svn-id: https://bitcoin.svn.sourceforge.net/svnroot/bitcoin/trunk@170 1a98c847-1fd6-4fd8-948a-caf3550aa51b
Diffstat (limited to 'init.cpp')
-rw-r--r-- | init.cpp | 16 |
1 files changed, 12 insertions, 4 deletions
@@ -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)) { |