summaryrefslogtreecommitdiff
path: root/apt-pkg
diff options
context:
space:
mode:
authorJulian Andres Klode <julian.klode@canonical.com>2019-04-15 11:32:50 +0200
committerJulian Andres Klode <julian.klode@canonical.com>2019-05-03 16:37:50 +0200
commit60cc44d160af02c49614653c97bfa3e4e5c9601d (patch)
treebcb3a3031af6f0ead0156932453869822567f913 /apt-pkg
parente547a738922b434ae0bad32ead26a1a3c2095d1e (diff)
Prevent shutdown while running dpkg
As long as we are running dpkg, keep an inhibitor that blocks us from shutting down. LP: #1820886
Diffstat (limited to 'apt-pkg')
-rw-r--r--apt-pkg/CMakeLists.txt2
-rw-r--r--apt-pkg/contrib/fileutl.cc48
-rw-r--r--apt-pkg/contrib/fileutl.h2
-rw-r--r--apt-pkg/deb/dpkgpm.cc15
4 files changed, 67 insertions, 0 deletions
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 =