Skip to content

Commit ffdda4e

Browse files
committed
Catch errors on datadir lock and pidfile delete
Prevents bad permissions (or other fs related problems) from resulting in hard crashes with cryptic messages on startup and shutdown.
1 parent 2cc1372 commit ffdda4e

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/init.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,11 @@ void Shutdown()
187187
pwalletMain->Flush(true);
188188
#endif
189189
#ifndef WIN32
190-
boost::filesystem::remove(GetPidFile());
190+
try {
191+
boost::filesystem::remove(GetPidFile());
192+
} catch (const boost::filesystem::filesystem_error& e) {
193+
LogPrintf("%s: Unable to remove pidfile: %s\n", __func__, e.what());
194+
}
191195
#endif
192196
UnregisterAllValidationInterfaces();
193197
#ifdef ENABLE_WALLET
@@ -862,9 +866,15 @@ bool AppInit2(boost::thread_group& threadGroup)
862866
boost::filesystem::path pathLockFile = GetDataDir() / ".lock";
863867
FILE* file = fopen(pathLockFile.string().c_str(), "a"); // empty lock file; created if it doesn't exist.
864868
if (file) fclose(file);
865-
static boost::interprocess::file_lock lock(pathLockFile.string().c_str());
866-
if (!lock.try_lock())
867-
return InitError(strprintf(_("Cannot obtain a lock on data directory %s. Bitcoin Core is probably already running."), strDataDir));
869+
870+
try {
871+
static boost::interprocess::file_lock lock(pathLockFile.string().c_str());
872+
if (!lock.try_lock())
873+
return InitError(strprintf(_("Cannot obtain a lock on data directory %s. Bitcoin Core is probably already running."), strDataDir));
874+
} catch(const boost::interprocess::interprocess_exception& e) {
875+
return InitError(strprintf(_("Cannot obtain a lock on data directory %s. Bitcoin Core is probably already running.") + " %s.", strDataDir, e.what()));
876+
}
877+
868878
#ifndef WIN32
869879
CreatePidFile(GetPidFile(), getpid());
870880
#endif

0 commit comments

Comments
 (0)