feat(pacman): close the manager holding the database lock instead of waiting
A locked database ended in "Un autre gestionnaire de paquets utilise déjà la base. Attendez la fin de l'autre opération puis réessayez": the app knew something else was running but could not say what, leaving the user to find and close it before retrying by hand. The lock is now inspected: the process holding /var/lib/pacman/db.lck is identified through /proc (with a pgrep fallback for the known managers, whose fd table is unreadable when they run as root), the dialog names it, and one click stops it — SIGTERM first, so pacman aborts its transaction and releases the lock itself, SIGKILL only when it does not react — then the pending operation resumes on its own. A lock whose owner is gone is dropped the same way, and one that disappears while the action is unwinding is retried without a dialog at all. Generated with Codebuff 🤖 Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
1 parent
d8bc17b2aa
commit
fb5c11e153
7 files changed
+517
-28
No files matched your search
@@ -1,5 +1,8 @@
|
||||
#include "pacman-manager.h"
|
||||
#include <array>
|
||||
#include <cctype>
|
||||
#include <chrono>
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
@@ -7,9 +10,11 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <set>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <unistd.h>
|
||||
|
||||
// Helper to run shell commands and capture output
|
||||
static std::string exec(const char* cmd) {
|
||||
@@ -112,6 +117,23 @@ static int percentFromLine(const std::string& line) {
|
||||
}
|
||||
}
|
||||
|
||||
// pacman's database lock file. Every manager that goes through libalpm (pacman,
|
||||
// pamac, octopi, the AUR helpers) takes it for the duration of a transaction.
|
||||
static const char* kLockFilePath = "/var/lib/pacman/db.lck";
|
||||
|
||||
// Processes that take that lock, used as a fallback when /proc cannot tell us
|
||||
// who does (the fd table of a root-owned process is not readable by a normal
|
||||
// user, which is exactly the "sudo pacman in another terminal" case).
|
||||
static const char* kKnownLockUsers[] = {
|
||||
"pacman", "pamac", "pamac-manager", "yay", "paru", "pikaur",
|
||||
"octopi", "octopi-helper", "discover",
|
||||
};
|
||||
|
||||
// How long the recovery waits for a SIGTERM-ed manager to release the lock
|
||||
// before forcing it, then for the forced stop to take effect.
|
||||
static constexpr int kTerminateGraceMs = 8000;
|
||||
static constexpr int kKillGraceMs = 3000;
|
||||
|
||||
// Read-only pacman output is reused by several views within a few seconds, so
|
||||
// it is cached for this long. Short enough that a package installed outside the
|
||||
// app still appears almost immediately, and every mutating operation drops the
|
||||
@@ -228,6 +250,7 @@ bool PacmanManager::install(const std::string& packageName,
|
||||
return false;
|
||||
}
|
||||
if (dbLocked(output)) {
|
||||
m_lastErrorWasLock = true;
|
||||
m_lastError = dbLockHint();
|
||||
return false;
|
||||
}
|
||||
@@ -244,12 +267,14 @@ bool PacmanManager::remove(const std::string& packageName,
|
||||
execPrivileged("pacman -R --noconfirm " + packageName, m_password, progressCallback);
|
||||
|
||||
m_lastError.clear();
|
||||
m_lastErrorWasLock = false;
|
||||
if (authFailed(output)) {
|
||||
clearPassword();
|
||||
m_lastError = "Échec d'authentification sudo.";
|
||||
return false;
|
||||
}
|
||||
if (dbLocked(output)) {
|
||||
m_lastErrorWasLock = true;
|
||||
m_lastError = dbLockHint();
|
||||
return false;
|
||||
}
|
||||
@@ -271,12 +296,14 @@ bool PacmanManager::update(const std::string& packageName,
|
||||
}
|
||||
|
||||
m_lastError.clear();
|
||||
m_lastErrorWasLock = false;
|
||||
if (authFailed(output)) {
|
||||
clearPassword();
|
||||
m_lastError = "Échec d'authentification sudo.";
|
||||
return false;
|
||||
}
|
||||
if (dbLocked(output)) {
|
||||
m_lastErrorWasLock = true;
|
||||
m_lastError = dbLockHint();
|
||||
return false;
|
||||
}
|
||||
@@ -365,12 +392,14 @@ bool PacmanManager::removePackages(const std::vector<std::string>& packages,
|
||||
std::string output = execPrivileged(command, m_password, progressCallback);
|
||||
|
||||
m_lastError.clear();
|
||||
m_lastErrorWasLock = false;
|
||||
if (authFailed(output)) {
|
||||
clearPassword();
|
||||
m_lastError = "Échec d'authentification sudo.";
|
||||
return false;
|
||||
}
|
||||
if (dbLocked(output)) {
|
||||
m_lastErrorWasLock = true;
|
||||
m_lastError = dbLockHint();
|
||||
return false;
|
||||
}
|
||||
@@ -395,12 +424,14 @@ bool PacmanManager::cleanCache(ProgressCallback progressCallback) {
|
||||
execPrivileged(cleanCacheCommand(paccache), m_password, progressCallback);
|
||||
|
||||
m_lastError.clear();
|
||||
m_lastErrorWasLock = false;
|
||||
if (authFailed(output)) {
|
||||
clearPassword();
|
||||
m_lastError = "Échec d'authentification sudo.";
|
||||
return false;
|
||||
}
|
||||
if (dbLocked(output)) {
|
||||
m_lastErrorWasLock = true;
|
||||
m_lastError = dbLockHint();
|
||||
return false;
|
||||
}
|
||||
@@ -422,12 +453,14 @@ bool PacmanManager::refresh(ProgressCallback progressCallback) {
|
||||
execPrivileged("pacman -Sy --noconfirm", m_password, progressCallback);
|
||||
|
||||
m_lastError.clear();
|
||||
m_lastErrorWasLock = false;
|
||||
if (authFailed(output)) {
|
||||
clearPassword();
|
||||
m_lastError = "Échec d'authentification sudo.";
|
||||
return false;
|
||||
}
|
||||
if (dbLocked(output)) {
|
||||
m_lastErrorWasLock = true;
|
||||
m_lastError = dbLockHint();
|
||||
return false;
|
||||
}
|
||||
@@ -613,6 +646,193 @@ bool PacmanManager::authFailed(const std::string& output) {
|
||||
// True when pacman refused to run because its database is locked
|
||||
// (/var/lib/pacman/db.lck): another pacman/pamac/yay run is in progress, or
|
||||
// a previous run crashed without releasing the lock.
|
||||
// Name of a process as the user recognizes it (/proc/<pid>/comm).
|
||||
static std::string processName(int pid) {
|
||||
std::ifstream comm("/proc/" + std::to_string(pid) + "/comm");
|
||||
std::string name;
|
||||
std::getline(comm, name);
|
||||
while (!name.empty() && (name.back() == '\n' || name.back() == ' ')) {
|
||||
name.pop_back();
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
// Parent of a process (/proc/<pid>/stat, 4th field); 0 when unknown.
|
||||
static int parentPid(int pid) {
|
||||
std::ifstream stat("/proc/" + std::to_string(pid) + "/stat");
|
||||
std::string line;
|
||||
if (!std::getline(stat, line)) return 0;
|
||||
|
||||
// The command name is parenthesised and may itself contain spaces, so the
|
||||
// fields after it are read from the last closing parenthesis.
|
||||
const size_t close = line.rfind(')');
|
||||
if (close == std::string::npos) return 0;
|
||||
std::istringstream rest(line.substr(close + 1));
|
||||
std::string state;
|
||||
int ppid = 0;
|
||||
rest >> state >> ppid;
|
||||
return ppid;
|
||||
}
|
||||
|
||||
// True when this process is keeping `path` open. Nothing takes a lock without
|
||||
// opening it, so the fd table answers the question directly — and it names the
|
||||
// exact process, even one we do not know about (a wrapper script, `sh -c`).
|
||||
static bool processHoldsFile(int pid, const std::string& path) {
|
||||
std::error_code listingError;
|
||||
auto it = std::filesystem::directory_iterator(
|
||||
"/proc/" + std::to_string(pid) + "/fd", listingError);
|
||||
const auto end = std::filesystem::directory_iterator();
|
||||
for (; it != end; it.increment(listingError)) {
|
||||
std::error_code linkError;
|
||||
const std::filesystem::path target =
|
||||
std::filesystem::read_symlink(it->path(), linkError);
|
||||
if (!linkError && target == path) return true;
|
||||
if (listingError) break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// First pid running `name`, excluding this process and its direct children: a
|
||||
// pacman we launched ourselves is not "another manager".
|
||||
static int pgrepPid(const std::string& name) {
|
||||
const std::string output =
|
||||
exec(("pgrep -x " + shellQuote(name) + " 2>/dev/null").c_str());
|
||||
const int self = static_cast<int>(getpid());
|
||||
|
||||
std::istringstream iss(output);
|
||||
std::string line;
|
||||
while (std::getline(iss, line)) {
|
||||
if (line.empty()) continue;
|
||||
const int pid = std::atoi(line.c_str());
|
||||
if (pid <= 0 || pid == self) continue;
|
||||
if (parentPid(pid) == self) continue;
|
||||
return pid;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int PacmanManager::lockHolderPid(const std::string& path) {
|
||||
std::error_code error;
|
||||
for (const auto& entry : std::filesystem::directory_iterator("/proc", error)) {
|
||||
const std::string name = entry.path().filename().string();
|
||||
if (name.empty() || !std::isdigit(static_cast<unsigned char>(name[0]))) continue;
|
||||
const int pid = std::atoi(name.c_str());
|
||||
if (pid <= 0 || pid == static_cast<int>(getpid())) continue;
|
||||
if (processHoldsFile(pid, path)) return pid;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
PacmanManager::LockHolder PacmanManager::detectLockHolder() {
|
||||
LockHolder holder;
|
||||
std::error_code error;
|
||||
holder.lockFilePresent = std::filesystem::exists(kLockFilePath, error);
|
||||
if (!holder.lockFilePresent) return holder;
|
||||
|
||||
holder.pid = lockHolderPid(kLockFilePath);
|
||||
if (holder.pid > 0) {
|
||||
holder.name = processName(holder.pid);
|
||||
return holder;
|
||||
}
|
||||
|
||||
for (const char* tool : kKnownLockUsers) {
|
||||
const int pid = pgrepPid(tool);
|
||||
if (pid <= 0) continue;
|
||||
holder.pid = pid;
|
||||
holder.name = tool;
|
||||
return holder;
|
||||
}
|
||||
|
||||
// Nobody holds it: the file is left over from a run that never finished.
|
||||
holder.stale = true;
|
||||
return holder;
|
||||
}
|
||||
|
||||
std::string PacmanManager::describeLockHolder(const LockHolder& holder) {
|
||||
if (holder.pid > 0) {
|
||||
const std::string name = holder.name.empty() ? "processus inconnu" : holder.name;
|
||||
return name + " (PID " + std::to_string(holder.pid) + ")";
|
||||
}
|
||||
if (holder.stale) {
|
||||
return "un verrou résiduel laissé par un gestionnaire interrompu";
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
bool PacmanManager::databaseIsFree() {
|
||||
std::error_code error;
|
||||
return !std::filesystem::exists(kLockFilePath, error);
|
||||
}
|
||||
|
||||
bool PacmanManager::waitForDatabase(int timeoutMs) {
|
||||
constexpr int kStepMs = 250;
|
||||
for (int waited = 0; waited <= timeoutMs; waited += kStepMs) {
|
||||
if (databaseIsFree()) return true;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(kStepMs));
|
||||
}
|
||||
return databaseIsFree();
|
||||
}
|
||||
|
||||
int PacmanManager::runCommand(const std::string& command, bool withSudo) const {
|
||||
std::string full = command;
|
||||
if (withSudo) {
|
||||
full = "printf '%s\\n' " + shellQuote(m_password) + " | sudo -S " + command;
|
||||
}
|
||||
return system((full + " > /dev/null 2>&1").c_str());
|
||||
}
|
||||
|
||||
bool PacmanManager::terminateProcess(int pid, const char* signal) const {
|
||||
const std::string command =
|
||||
"kill -" + std::string(signal) + " " + std::to_string(pid);
|
||||
if (runCommand(command, /*withSudo=*/false) == 0) return true;
|
||||
// The holder belongs to another user (a manager started with sudo in a
|
||||
// terminal): only the session password can reach it.
|
||||
return hasPassword() && runCommand(command, /*withSudo=*/true) == 0;
|
||||
}
|
||||
|
||||
bool PacmanManager::removeLockFile() const {
|
||||
const std::string command = std::string("rm -f ") + kLockFilePath;
|
||||
if (runCommand(command, /*withSudo=*/false) == 0 && databaseIsFree()) return true;
|
||||
return hasPassword() && runCommand(command, /*withSudo=*/true) == 0
|
||||
&& databaseIsFree();
|
||||
}
|
||||
|
||||
bool PacmanManager::releaseDatabaseLock(const LockHolder& holder,
|
||||
ProgressCallback progressCallback) {
|
||||
m_lastError.clear();
|
||||
m_lastErrorWasLock = false;
|
||||
|
||||
if (holder.pid > 0) {
|
||||
const std::string who = describeLockHolder(holder);
|
||||
if (progressCallback) progressCallback("Arrêt de " + who + "...", -1);
|
||||
|
||||
if (!terminateProcess(holder.pid, "TERM")) {
|
||||
m_lastError = "Impossible d'arrêter " + who
|
||||
+ " : droits insuffisants pour le faire depuis "
|
||||
"PacManager.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!waitForDatabase(kTerminateGraceMs)) {
|
||||
if (progressCallback) {
|
||||
progressCallback(who + " ne répond pas, arrêt forcé...", -1);
|
||||
}
|
||||
terminateProcess(holder.pid, "KILL");
|
||||
waitForDatabase(kKillGraceMs);
|
||||
}
|
||||
if (databaseIsFree()) return true;
|
||||
}
|
||||
|
||||
// No process left to stop (or it would not die): the lock file itself is
|
||||
// what keeps the database closed.
|
||||
if (progressCallback) progressCallback("Suppression du verrou résiduel...", -1);
|
||||
if (removeLockFile()) return true;
|
||||
|
||||
m_lastError = "La base pacman est toujours verrouillée : droits insuffisants "
|
||||
"pour supprimer le verrou.";
|
||||
return false;
|
||||
}
|
||||
|
||||
bool PacmanManager::dbLocked(const std::string& output) {
|
||||
return output.find("could not lock database") != std::string::npos
|
||||
|| output.find("unable to lock database") != std::string::npos
|
||||
|
||||
Reference in new issue
Block a user