diff options
author | Julian Andres Klode <julian.klode@canonical.com> | 2019-04-15 11:32:50 +0200 |
---|---|---|
committer | Julian Andres Klode <julian.klode@canonical.com> | 2019-05-03 16:37:50 +0200 |
commit | 60cc44d160af02c49614653c97bfa3e4e5c9601d (patch) | |
tree | bcb3a3031af6f0ead0156932453869822567f913 | |
parent | e547a738922b434ae0bad32ead26a1a3c2095d1e (diff) | |
download | apt-60cc44d160af02c49614653c97bfa3e4e5c9601d.tar.gz |
Prevent shutdown while running dpkg
As long as we are running dpkg, keep an inhibitor that
blocks us from shutting down.
LP: #1820886
-rw-r--r-- | CMake/FindSystemd.cmake | 24 | ||||
-rw-r--r-- | CMake/config.h.in | 3 | ||||
-rw-r--r-- | CMakeLists.txt | 5 | ||||
-rw-r--r-- | apt-pkg/CMakeLists.txt | 2 | ||||
-rw-r--r-- | apt-pkg/contrib/fileutl.cc | 48 | ||||
-rw-r--r-- | apt-pkg/contrib/fileutl.h | 2 | ||||
-rw-r--r-- | apt-pkg/deb/dpkgpm.cc | 15 | ||||
-rw-r--r-- | debian/control | 1 | ||||
-rw-r--r-- | doc/examples/configure-index | 3 |
9 files changed, 103 insertions, 0 deletions
diff --git a/CMake/FindSystemd.cmake b/CMake/FindSystemd.cmake new file mode 100644 index 000000000..1c7a7debf --- /dev/null +++ b/CMake/FindSystemd.cmake @@ -0,0 +1,24 @@ +# - Try to find SYSTEMD +# Once done, this will define +# +# SYSTEMD_FOUND - system has SYSTEMD +# SYSTEMD_INCLUDE_DIRS - the SYSTEMD include directories +# SYSTEMD_LIBRARIES - the SYSTEMD library +find_package(PkgConfig) + +pkg_check_modules(SYSTEMD_PKGCONF libsystemd) + +find_path(SYSTEMD_INCLUDE_DIRS + NAMES systemd/sd-bus.h + PATHS ${SYSTEMD_PKGCONF_INCLUDE_DIRS} +) + +find_library(SYSTEMD_LIBRARIES + NAMES systemd + PATHS ${SYSTEMD_PKGCONF_LIBRARY_DIRS} +) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(Systemd DEFAULT_MSG SYSTEMD_INCLUDE_DIRS SYSTEMD_LIBRARIES) + +mark_as_advanced(SYSTEMD_INCLUDE_DIRS SYSTEMD_LIBRARIES) diff --git a/CMake/config.h.in b/CMake/config.h.in index 74d78fdb2..a9528ccfa 100644 --- a/CMake/config.h.in +++ b/CMake/config.h.in @@ -20,6 +20,9 @@ /* Define if we have the zstd library for zst */ #cmakedefine HAVE_ZSTD +/* Define if we have the systemd library */ +#cmakedefine HAVE_SYSTEMD + /* Define if we have the udev library */ #cmakedefine HAVE_UDEV diff --git a/CMakeLists.txt b/CMakeLists.txt index 29cd30bc6..02bad9377 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -117,6 +117,11 @@ if (UDEV_FOUND) set(HAVE_UDEV 1) endif() +find_package(Systemd) +if (SYSTEMD_FOUND) + set(HAVE_SYSTEMD 1) +endif() + find_package(Seccomp) if (SECCOMP_FOUND) set(HAVE_SECCOMP 1) diff --git a/apt-pkg/CMakeLists.txt b/apt-pkg/CMakeLists.txt index 57e422a14..efa680f6b 100644 --- a/apt-pkg/CMakeLists.txt +++ b/apt-pkg/CMakeLists.txt @@ -48,6 +48,7 @@ target_include_directories(apt-pkg ${LZ4_INCLUDE_DIRS} $<$<BOOL:${ZSTD_FOUND}>:${ZSTD_INCLUDE_DIRS}> $<$<BOOL:${UDEV_FOUND}>:${UDEV_INCLUDE_DIRS}> + $<$<BOOL:${SYSTEMD_FOUND}>:${SYSTEMD_INCLUDE_DIRS}> ${ICONV_INCLUDE_DIRS} ) @@ -60,6 +61,7 @@ target_link_libraries(apt-pkg ${LZ4_LIBRARIES} $<$<BOOL:${ZSTD_FOUND}>:${ZSTD_LIBRARIES}> $<$<BOOL:${UDEV_FOUND}>:${UDEV_LIBRARIES}> + $<$<BOOL:${SYSTEMD_FOUND}>:${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 8d6cb3146..798636647 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -70,6 +70,9 @@ #ifdef HAVE_ZSTD #include <zstd.h> #endif +#ifdef HAVE_SYSTEMD +#include <systemd/sd-bus.h> +#endif #include <endian.h> #include <stdint.h> @@ -3369,3 +3372,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 fff49a757..cc0191192 100644 --- a/apt-pkg/contrib/fileutl.h +++ b/apt-pkg/contrib/fileutl.h @@ -283,4 +283,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 101571782..ad5a9007e 100644 --- a/apt-pkg/deb/dpkgpm.cc +++ b/apt-pkg/deb/dpkgpm.cc @@ -1448,6 +1448,21 @@ bool pkgDPkgPM::ExpandPendingCalls(std::vector<Item> &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 = diff --git a/debian/control b/debian/control index 8c6ebb499..4aca00b95 100644 --- a/debian/control +++ b/debian/control @@ -21,6 +21,7 @@ Build-Depends: cmake (>= 3.4), liblz4-dev (>= 0.0~r126), liblzma-dev, libseccomp-dev [amd64 arm64 armel armhf i386 mips mips64el mipsel ppc64el s390x hppa powerpc powerpcspe ppc64 x32], + libsystemd-dev [linux-any], libudev-dev [linux-any], libzstd-dev (>= 1.0), ninja-build, diff --git a/doc/examples/configure-index b/doc/examples/configure-index index 743e676de..5e317d7e0 100644 --- a/doc/examples/configure-index +++ b/doc/examples/configure-index @@ -503,6 +503,9 @@ DPkg progress-bg "<STRING>"; progress-bar "<BOOL>"; }; + + // Set a shutdown block inhibitor on systemd systems while running dpkg + Inhibit-Shutdown "<BOOL>"; } /* Options you can set to see some debugging text They correspond to names |