1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Wolfgang Bumiller <w.bumiller@proxmox.com>
3 Date: Tue, 31 Mar 2020 15:22:42 +0200
4 Subject: [PATCH] allow running lxc-monitord as a system daemon
6 lxc-monitord instances are spawned on demand and, if this
7 happens from a service, the daemon is considered part of
8 it by systemd, as it is running in the same cgroups. This
9 can be avoided by leaving it running permanently.
11 Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
14 config/init/systemd/Makefile.am | 10 +++---
17 src/lxc/cmd/lxc_monitord.c | 60 +++++++++++++++++++++++----------
18 5 files changed, 52 insertions(+), 21 deletions(-)
20 diff --git a/.gitignore b/.gitignore
21 index 3cff48d96..44345454f 100644
24 @@ -120,6 +120,7 @@ config/bash/lxc
25 config/init/common/lxc-containers
26 config/init/common/lxc-net
27 config/init/systemd/lxc-autostart-helper
28 +config/init/systemd/lxc-monitord.service
29 config/init/systemd/lxc-net.service
30 config/init/systemd/lxc.service
31 config/init/systemd/lxc@.service
32 diff --git a/config/init/systemd/Makefile.am b/config/init/systemd/Makefile.am
33 index c448850d1..4a4fde5e7 100644
34 --- a/config/init/systemd/Makefile.am
35 +++ b/config/init/systemd/Makefile.am
36 @@ -2,19 +2,21 @@ EXTRA_DIST = \
41 + lxc-net.service.in \
42 + lxc-monitord.service.in
44 if INIT_SCRIPT_SYSTEMD
45 -BUILT_SOURCES = lxc.service lxc@.service lxc-net.service
46 +BUILT_SOURCES = lxc.service lxc@.service lxc-net.service lxc-monitord.service
48 -install-systemd: lxc.service lxc@.service lxc-net.service lxc-apparmor-load
49 +install-systemd: lxc.service lxc@.service lxc-net.service lxc-monitord.service lxc-apparmor-load
50 $(MKDIR_P) $(DESTDIR)$(SYSTEMD_UNIT_DIR)
51 - $(INSTALL_DATA) lxc.service lxc@.service lxc-net.service $(DESTDIR)$(SYSTEMD_UNIT_DIR)/
52 + $(INSTALL_DATA) lxc.service lxc@.service lxc-net.service lxc-monitord.service $(DESTDIR)$(SYSTEMD_UNIT_DIR)/
55 rm -f $(DESTDIR)$(SYSTEMD_UNIT_DIR)/lxc.service
56 rm -f $(DESTDIR)$(SYSTEMD_UNIT_DIR)/lxc@.service
57 rm -f $(DESTDIR)$(SYSTEMD_UNIT_DIR)/lxc-net.service
58 + rm -f $(DESTDIR)$(SYSTEMD_UNIT_DIR)/lxc-monitord.service
59 rmdir $(DESTDIR)$(SYSTEMD_UNIT_DIR) || :
61 pkglibexec_SCRIPTS = lxc-apparmor-load
62 diff --git a/configure.ac b/configure.ac
63 index e30ea6f6e..16c5ab8c4 100644
66 @@ -913,6 +913,7 @@ AC_CONFIG_FILES([
67 config/init/systemd/lxc.service
68 config/init/systemd/lxc@.service
69 config/init/systemd/lxc-net.service
70 + config/init/systemd/lxc-monitord.service
71 config/init/sysvinit/Makefile
72 config/init/sysvinit/lxc-containers
73 config/init/sysvinit/lxc-net
74 diff --git a/lxc.spec.in b/lxc.spec.in
75 index ec6321c33..ea6789fb6 100644
78 @@ -251,6 +251,7 @@ fi
79 %{_unitdir}/lxc-net.service
80 %{_unitdir}/lxc.service
81 %{_unitdir}/lxc@.service
82 +%{_unitdir}/lxc-monitord.service
84 %{_sysconfdir}/rc.d/init.d/lxc
85 %{_sysconfdir}/rc.d/init.d/lxc-net
86 diff --git a/src/lxc/cmd/lxc_monitord.c b/src/lxc/cmd/lxc_monitord.c
87 index bcb289ca6..da7db2820 100644
88 --- a/src/lxc/cmd/lxc_monitord.c
89 +++ b/src/lxc/cmd/lxc_monitord.c
90 @@ -338,17 +338,44 @@ static void lxc_monitord_sig_handler(int sig)
92 int main(int argc, char *argv[])
95 + int ret, pipefd = -1;
96 char logpath[PATH_MAX];
98 - char *lxcpath = argv[1];
99 + const char *lxcpath = NULL;
100 bool mainloop_opened = false;
101 bool monitord_created = false;
102 + bool persistent = false;
106 + if (argc > 1 && !strcmp(argv[1], "--daemon")) {
117 + lxcpath = lxc_global_config_value("lxc.lxcpath");
119 + ERROR("Failed to get default lxcpath");
120 + exit(EXIT_FAILURE);
125 + if (lxc_safe_int(argv[1], &pipefd) < 0)
126 + exit(EXIT_FAILURE);
131 + if (argc != 1 || (persistent != (pipefd == -1))) {
133 - "Usage: lxc-monitord lxcpath sync-pipe-fd\n\n"
134 + "Usage: lxc-monitord lxcpath sync-pipe-fd\n"
135 + " lxc-monitord --daemon lxcpath\n\n"
136 "NOTE: lxc-monitord is intended for use by lxc internally\n"
137 " and does not need to be run by hand\n\n");
139 @@ -371,9 +398,6 @@ int main(int argc, char *argv[])
140 INFO("Failed to open log file %s, log will be lost", lxcpath);
141 lxc_log_options_no_override();
143 - if (lxc_safe_int(argv[2], &pipefd) < 0)
144 - exit(EXIT_FAILURE);
146 if (sigfillset(&mask) ||
147 sigdelset(&mask, SIGILL) ||
148 sigdelset(&mask, SIGSEGV) ||
149 @@ -406,15 +430,17 @@ int main(int argc, char *argv[])
151 monitord_created = true;
153 - /* sync with parent, we're ignoring the return from write
154 - * because regardless if it works or not, the following
155 - * close will sync us with the parent process. the
156 - * if-empty-statement construct is to quiet the
157 - * warn-unused-result warning.
159 - if (lxc_write_nointr(pipefd, "S", 1))
162 + if (pipefd != -1) {
163 + /* sync with parent, we're ignoring the return from write
164 + * because regardless if it works or not, the following
165 + * close will sync us with the parent process. the
166 + * if-empty-statement construct is to quiet the
167 + * warn-unused-result warning.
169 + if (lxc_write_nointr(pipefd, "S", 1))
174 if (lxc_monitord_mainloop_add(&monitor)) {
175 ERROR("Failed to add mainloop handlers");
176 @@ -425,7 +451,7 @@ int main(int argc, char *argv[])
177 lxc_raw_getpid(), monitor.lxcpath);
180 - ret = lxc_mainloop(&monitor.descr, 1000 * 30);
181 + ret = lxc_mainloop(&monitor.descr, persistent ? -1 : 1000 * 30);
183 ERROR("mainloop returned an error");