From 0de84364e374591b71826e56302e5228bc0d2dab 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) (cherry picked from commit c5b8a857f2535fe284426cefda2fbd1bbb3d5808) Also fixup prepare-release for CI to handle [linux-any] build-dep, and do the whole autoconf / makefile stuff. --- apt-pkg/contrib/fileutl.cc | 48 ++++++++++++++++++++++++++++++++++++++++++++ apt-pkg/contrib/fileutl.h | 2 ++ apt-pkg/deb/dpkgpm.cc | 16 +++++++++++++++ apt-pkg/makefile | 5 ++++- buildlib/config.h.in | 3 +++ buildlib/environment.mak.in | 3 +++ configure.ac | 6 ++++++ debian/control | 3 ++- doc/examples/configure-index | 3 +++ prepare-release | 2 +- 10 files changed, 88 insertions(+), 3 deletions(-) diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 5d283bdf5..f2642e5dc 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -67,6 +67,9 @@ #ifdef HAVE_LZ4 #include #endif +#ifdef HAVE_SYSTEMD +#include +#endif #include #include @@ -3029,3 +3032,48 @@ bool DropPrivileges() /*{{{*/ 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 7ee302d7b..32e5612bf 100644 --- a/apt-pkg/contrib/fileutl.h +++ b/apt-pkg/contrib/fileutl.h @@ -253,4 +253,6 @@ bool Popen(const char* Args[], FileFd &Fd, pid_t &Child, FileFd::OpenMode Mode, bool Popen(const char* Args[], FileFd &Fd, pid_t &Child, FileFd::OpenMode Mode); +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 7a3d1d1f4..05bee9286 100644 --- a/apt-pkg/deb/dpkgpm.cc +++ b/apt-pkg/deb/dpkgpm.cc @@ -1232,6 +1232,22 @@ bool pkgDPkgPM::Go(APT::Progress::PackageManager *progress) return (I.Pkg->Flags & pkgCache::Flag::Essential) != 0; }; + 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; + + pkgPackageManager::SigINTStop = false; d->progress = progress; diff --git a/apt-pkg/makefile b/apt-pkg/makefile index 789d3f2fd..b66e9b272 100644 --- a/apt-pkg/makefile +++ b/apt-pkg/makefile @@ -12,7 +12,7 @@ include ../buildlib/defaults.mak # The library name and version (indirectly used from init.h) include ../buildlib/libversion.mak -CPPFLAGS+=-DAPT_PKG_EXPOSE_STRING_VIEW +CPPFLAGS+=-DAPT_PKG_EXPOSE_STRING_VIEW $(SYSTEMD_CFLAGS) LIBRARY=apt-pkg MAJOR=$(LIBAPTPKG_MAJOR) MINOR=$(LIBAPTPKG_RELEASE) @@ -29,6 +29,9 @@ endif ifeq ($(HAVE_LZ4),yes) SLIBS+= -llz4 endif +ifeq ($(HAVE_SYSTEMD),yes) +SLIBS+= $(SYSTEMD_LIBS) +endif APT_DOMAIN:=libapt-pkg$(LIBAPTPKG_MAJOR) SOURCE = $(sort $(wildcard *.cc */*.cc)) diff --git a/buildlib/config.h.in b/buildlib/config.h.in index fb21b387f..3179beb69 100644 --- a/buildlib/config.h.in +++ b/buildlib/config.h.in @@ -17,6 +17,9 @@ /* Define if we have the lz4 library for lz4 */ #undef HAVE_LZ4 +/* Define if we have the systemd library */ +#undef HAVE_SYSTEMD + /* These two are used by the statvfs shim for glibc2.0 and bsd */ /* Define if we have sys/vfs.h */ #undef HAVE_VFS_H diff --git a/buildlib/environment.mak.in b/buildlib/environment.mak.in index 9620722de..2df4786f9 100644 --- a/buildlib/environment.mak.in +++ b/buildlib/environment.mak.in @@ -71,6 +71,9 @@ HAVE_BZ2 = @HAVE_BZ2@ HAVE_LZMA = @HAVE_LZMA@ HAVE_LZ4 = @HAVE_LZ4@ NEED_SOCKLEN_T_DEFINE = @NEED_SOCKLEN_T_DEFINE@ +HAVE_SYSTEMD = @HAVE_SYSTEMD@ +SYSTEMD_CFLAGS = @SYSTEMD_CFLAGS@ +SYSTEMD_LIBS = @SYSTEMD_LIBS@ # Shared library things HOST_OS = @host_os@ diff --git a/configure.ac b/configure.ac index 82b7e73a6..edf98fbeb 100644 --- a/configure.ac +++ b/configure.ac @@ -129,6 +129,12 @@ if test "x$HAVE_LZMA" = "xyes"; then AC_DEFINE(HAVE_LZMA) fi +HAVE_SYSTEMD=no +AC_SUBST(HAVE_SYSTEMD) +PKG_CHECK_MODULES([SYSTEMD], [libsystemd >= 221], + [HAVE_SYSTEMD=yes + AC_DEFINE(HAVE_SYSTEMD)]) + dnl Converts the ARCH to be something singular for this general CPU family dnl This is often the dpkg architecture string. dnl First check against the full canonical canoncial-system-type in $target diff --git a/debian/control b/debian/control index 319aea118..bb8e92520 100644 --- a/debian/control +++ b/debian/control @@ -9,7 +9,8 @@ Build-Depends: dpkg-dev (>= 1.17.14), debhelper (>= 9.20141010), libdb-dev, gettext (>= 0.12), libcurl4-gnutls-dev (>= 7.19.4~), zlib1g-dev, libbz2-dev, liblzma-dev, liblz4-dev (>= 0.0~r126), xsltproc, docbook-xsl, docbook-xml, po4a (>= 0.34-2), - autotools-dev, autoconf, automake, libgtest-dev , dh-systemd + autotools-dev, autoconf, automake, libgtest-dev , dh-systemd, + libsystemd-dev [linux-any], pkg-config Build-Depends-Indep: doxygen, w3m, graphviz Build-Conflicts: autoconf2.13, automake1.4 Vcs-Git: git://anonscm.debian.org/apt/apt.git diff --git a/doc/examples/configure-index b/doc/examples/configure-index index 8e8110d94..24c5581b4 100644 --- a/doc/examples/configure-index +++ b/doc/examples/configure-index @@ -418,6 +418,9 @@ DPkg // controls if apt will apport on the first dpkg error or if it // tries to install as many packages as possible StopOnError "true"; + + // Set a shutdown block inhibitor on systemd systems while running dpkg + Inhibit-Shutdown ""; } /* Options you can set to see some debugging text They correspond to names diff --git a/prepare-release b/prepare-release index 731eb1096..8810dd500 100755 --- a/prepare-release +++ b/prepare-release @@ -147,7 +147,7 @@ elif [ "$1" = 'buildlog' ]; then shift done elif [ "$1" = 'travis-ci' ]; then - apt-get install -qy --no-install-recommends $(sed -n -e '/^Build-Depends: /,/^Build-Depends-Indep: / {p}' debian/control | sed -e 's#([^)]*)##g' -e 's#^Build-Depends\(-Indep\)\?: ##' -e 's#<.*>##g' | tr -d ',') + apt-get install -qy --no-install-recommends $(sed -n -e '/^Build-Depends: /,/^Build-Depends-Indep: / {p}' debian/control | sed -e 's#([^)]*)##g' -e 's#\[[^]]*\]##g' -e 's#^Build-Depends\(-Indep\)\?: ##' -e 's#<.*>##g' | tr -d ',') apt-get install -qy --no-install-recommends $(sed -n 's#^Depends: .*@, \(.*\)$#\1#p' debian/tests/control | tr -d ',') elif [ "$1" = 'coverage' ]; then DIR="${2:-./coverage}" -- cgit v1.2.3