From 8a9a2147bc4346c7e3da8a08719b8f2686d65887 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Sun, 29 Jan 2017 13:05:18 +0100 Subject: Add support for dpkg frontend lock The dpkg frontend lock is a lock dpkg tries to acquire except if the frontend already acquires it. This fixes a race condition in the install command where the dpkg lock is not held for a short period of time between different dpkg invocations. For this reason we also define an environment variable DPKG_FRONTEND_LOCKED for dpkg invocations so dpkg knows not to try to acquire the frontend lock because it's held by a parent process. We can set DPKG_FRONTEND_LOCKED only if the frontend lock really is held; that is, if our lock count is greater than 0 - otherwise an apt client not using the LockInner family of functions would run dpkg without the frontend lock set, but with DPKG_FRONTEND_LOCKED set. Such a process has a weaker guarantee: Because dpkg would not lock the frontend lock either, the process is prone to the existing races, and, more importantly, so is a new style process. Closes: #869546 [fixups: fix error messages, add public IsLocked() method, and make {Un,}LockInner return an error on !debSystem] (cherry picked from commit c2c8b4787b0882234ba2772ec7513afbf97b563a) LP: #1781169 (cherry picked from commit 6c0c94ed32b8e679b14b0f89b51c1c336dc0ab9c) --- apt-pkg/deb/debsystem.cc | 55 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 8 deletions(-) (limited to 'apt-pkg/deb/debsystem.cc') diff --git a/apt-pkg/deb/debsystem.cc b/apt-pkg/deb/debsystem.cc index 7118b534b..c087d6914 100644 --- a/apt-pkg/deb/debsystem.cc +++ b/apt-pkg/deb/debsystem.cc @@ -46,10 +46,11 @@ debSystem debSys; class APT_HIDDEN debSystemPrivate { public: - debSystemPrivate() : LockFD(-1), LockCount(0), StatusFile(0) + debSystemPrivate() : FrontendLockFD(-1), LockFD(-1), LockCount(0), StatusFile(0) { } // For locking support + int FrontendLockFD; int LockFD; unsigned LockCount; @@ -86,22 +87,30 @@ bool debSystem::Lock() } // Create the lockfile - string AdminDir = flNotFile(_config->Find("Dir::State::status")); - d->LockFD = GetLock(AdminDir + "lock"); - if (d->LockFD == -1) + string AdminDir = flNotFile(_config->FindFile("Dir::State::status")); + string FrontendLockFile = AdminDir + "lock-frontend"; + d->FrontendLockFD = GetLock(FrontendLockFile); + if (d->FrontendLockFD == -1) { if (errno == EACCES || errno == EAGAIN) - return _error->Error(_("Unable to lock the administration directory (%s), " - "is another process using it?"),AdminDir.c_str()); + return _error->Error(_("Unable to acquire the dpkg frontend lock (%s), " + "is another process using it?"),FrontendLockFile.c_str()); else - return _error->Error(_("Unable to lock the administration directory (%s), " - "are you root?"),AdminDir.c_str()); + return _error->Error(_("Unable to acquire the dpkg frontend lock (%s), " + "are you root?"),FrontendLockFile.c_str()); + } + if (LockInner() == false) + { + close(d->FrontendLockFD); + return false; } // See if we need to abort with a dirty journal if (CheckUpdates() == true) { close(d->LockFD); + close(d->FrontendLockFD); + d->FrontendLockFD = -1; d->LockFD = -1; const char *cmd; if (getenv("SUDO_USER") != NULL) @@ -118,6 +127,21 @@ bool debSystem::Lock() return true; } + +bool debSystem::LockInner() { + string AdminDir = flNotFile(_config->FindFile("Dir::State::status")); + d->LockFD = GetLock(AdminDir + "lock"); + if (d->LockFD == -1) + { + if (errno == EACCES || errno == EAGAIN) + return _error->Error(_("Unable to lock the administration directory (%s), " + "is another process using it?"),AdminDir.c_str()); + else + return _error->Error(_("Unable to lock the administration directory (%s), " + "are you root?"),AdminDir.c_str()); + } + return true; +} /*}}}*/ // System::UnLock - Drop a lock /*{{{*/ // --------------------------------------------------------------------- @@ -131,11 +155,26 @@ bool debSystem::UnLock(bool NoErrors) return _error->Error(_("Not locked")); if (--d->LockCount == 0) { + close(d->FrontendLockFD); close(d->LockFD); d->LockCount = 0; } return true; +} +bool debSystem::UnLockInner(bool NoErrors) { + (void) NoErrors; + close(d->LockFD); + return true; +} + /*}}}*/ +// System::IsLocked - Check if system is locked /*{{{*/ +// --------------------------------------------------------------------- +/* This checks if the frontend lock is hold. The inner lock might be + * released. */ +bool debSystem::IsLocked() +{ + return d->LockCount > 0; } /*}}}*/ // System::CheckUpdates - Check if the updates dir is dirty /*{{{*/ -- cgit v1.2.3