From d9c6b09ac42d21abd151a0feaff676b8a8a24bb9 Mon Sep 17 00:00:00 2001 From: s_nakamoto Date: Wed, 20 Oct 2010 16:12:23 +0000 Subject: 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 --- init.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'init.cpp') diff --git a/init.cpp b/init.cpp index 98410d188..b10e22a32 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)) { -- cgit v1.2.3