summaryrefslogtreecommitdiff
path: root/apt-pkg/contrib/fileutl.cc
diff options
context:
space:
mode:
authorJulian Andres Klode <jak@debian.org>2019-05-03 14:51:40 +0000
committerJulian Andres Klode <jak@debian.org>2019-05-03 14:51:40 +0000
commit9ada8cb08b9d54291993118120712992302b6641 (patch)
treebcb3a3031af6f0ead0156932453869822567f913 /apt-pkg/contrib/fileutl.cc
parente547a738922b434ae0bad32ead26a1a3c2095d1e (diff)
parent60cc44d160af02c49614653c97bfa3e4e5c9601d (diff)
Merge branch 'pu/systemd-inhibitor' into 'master'
Prevent shutdown while running dpkg See merge request apt-team/apt!60
Diffstat (limited to 'apt-pkg/contrib/fileutl.cc')
-rw-r--r--apt-pkg/contrib/fileutl.cc48
1 files changed, 48 insertions, 0 deletions
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
+}
+ /*}}}*/