From c5b8a857f2535fe284426cefda2fbd1bbb3d5808 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Mon, 15 Apr 2019 11:32:50 +0200 Subject: Prevent shutdown while running dpkg As long as we are running dpkg, keep an inhibitor that blocks us from shutting down. LP: #1820886 (cherry picked from commit 96aef11a5cf400377a52f7e93e70944b17e249d1) (cherry picked from commit 8ea79afcadead5a5b7d94bf8623cb833859f6d80) --- apt-pkg/CMakeLists.txt | 2 ++ apt-pkg/contrib/fileutl.cc | 48 ++++++++++++++++++++++++++++++++++++++++++++++ apt-pkg/contrib/fileutl.h | 2 ++ apt-pkg/deb/dpkgpm.cc | 15 +++++++++++++++ 4 files changed, 67 insertions(+) (limited to 'apt-pkg') diff --git a/apt-pkg/CMakeLists.txt b/apt-pkg/CMakeLists.txt index 64709ce34..ce73c6a34 100644 --- a/apt-pkg/CMakeLists.txt +++ b/apt-pkg/CMakeLists.txt @@ -46,6 +46,7 @@ target_include_directories(apt-pkg ${LZ4_INCLUDE_DIRS} $<$:${ZSTD_INCLUDE_DIRS}> $<$:${UDEV_INCLUDE_DIRS}> + $<$:${SYSTEMD_INCLUDE_DIRS}> ${ICONV_INCLUDE_DIRS} ) @@ -58,6 +59,7 @@ target_link_libraries(apt-pkg ${LZ4_LIBRARIES} $<$:${ZSTD_LIBRARIES}> $<$:${UDEV_LIBRARIES}> + $<$:${SYSTEMD_LIBRARIES}> ${ICONV_LIBRARIES} ) set_target_properties(apt-pkg PROPERTIES VERSION ${MAJOR}.${MINOR}) diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index eab05de4f..b38186b3d 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -71,6 +71,9 @@ #ifdef HAVE_ZSTD #include #endif +#ifdef HAVE_SYSTEMD +#include +#endif #include #include @@ -3387,3 +3390,48 @@ bool OpenConfigurationFileFd(std::string const &File, FileFd &Fd) /*{{{*/ return true; } /*}}}*/ +int Inhibit(const char *what, const char *who, const char *why, const char *mode) /*{{{*/ +{ +#ifdef HAVE_SYSTEMD + sd_bus_error error = SD_BUS_ERROR_NULL; + sd_bus_message *m = NULL; + sd_bus *bus = NULL; + int fd; + int r; + + r = sd_bus_open_system(&bus); + if (r < 0) + goto out; + + r = sd_bus_call_method(bus, + "org.freedesktop.login1", + "/org/freedesktop/login1", + "org.freedesktop.login1.Manager", + "Inhibit", + &error, + &m, + "ssss", + what, + who, + why, + mode); + if (r < 0) + goto out; + + r = sd_bus_message_read(m, "h", &fd); + if (r < 0) + goto out; + + // We received a file descriptor, return it - systemd will close the read fd + // on free, so let's duplicate it here. + r = dup(fd); +out: + sd_bus_error_free(&error); + sd_bus_message_unref(m); + sd_bus_unref(bus); + return r; +#else + return -ENOTSUP; +#endif +} + /*}}}*/ diff --git a/apt-pkg/contrib/fileutl.h b/apt-pkg/contrib/fileutl.h index 6249b7c16..35cd8139a 100644 --- a/apt-pkg/contrib/fileutl.h +++ b/apt-pkg/contrib/fileutl.h @@ -294,4 +294,6 @@ bool Popen(const char* Args[], FileFd &Fd, pid_t &Child, FileFd::OpenMode Mode); APT_HIDDEN bool OpenConfigurationFileFd(std::string const &File, FileFd &Fd); +APT_HIDDEN int Inhibit(const char *what, const char *who, const char *why, const char *mode); + #endif diff --git a/apt-pkg/deb/dpkgpm.cc b/apt-pkg/deb/dpkgpm.cc index c8e55fe14..2700bef7b 100644 --- a/apt-pkg/deb/dpkgpm.cc +++ b/apt-pkg/deb/dpkgpm.cc @@ -1463,6 +1463,21 @@ bool pkgDPkgPM::ExpandPendingCalls(std::vector &List, pkgDepCache &Cache) } bool pkgDPkgPM::Go(APT::Progress::PackageManager *progress) { + struct Inhibitor + { + int Fd = -1; + Inhibitor() + { + if (_config->FindB("DPkg::Inhibit-Shutdown", true)) + Fd = Inhibit("shutdown", "APT", "APT is installing or removing packages", "block"); + } + ~Inhibitor() + { + if (Fd > 0) + close(Fd); + } + } inhibitor; + // explicitly remove&configure everything for hookscripts and progress building // we need them only temporarily through, so keep the length and erase afterwards decltype(List)::const_iterator::difference_type explicitIdx = -- cgit v1.2.3