1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Wolfgang Bumiller <w.bumiller@proxmox.com>
3 Date: Mon, 20 Nov 2017 10:49:41 +0100
4 Subject: [PATCH] possibility to run lxc-monitord as a regular 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>
13 config/init/systemd/Makefile.am | 10 +++--
14 config/init/systemd/lxc-monitord.service.in | 12 ++++++
17 src/lxc/cmd/lxc_monitord.c | 60 +++++++++++++++++++++--------
18 5 files changed, 63 insertions(+), 21 deletions(-)
19 create mode 100644 config/init/systemd/lxc-monitord.service.in
21 diff --git a/config/init/systemd/Makefile.am b/config/init/systemd/Makefile.am
22 index c448850d..4a4fde5e 100644
23 --- a/config/init/systemd/Makefile.am
24 +++ b/config/init/systemd/Makefile.am
25 @@ -2,19 +2,21 @@ EXTRA_DIST = \
30 + lxc-net.service.in \
31 + lxc-monitord.service.in
33 if INIT_SCRIPT_SYSTEMD
34 -BUILT_SOURCES = lxc.service lxc@.service lxc-net.service
35 +BUILT_SOURCES = lxc.service lxc@.service lxc-net.service lxc-monitord.service
37 -install-systemd: lxc.service lxc@.service lxc-net.service lxc-apparmor-load
38 +install-systemd: lxc.service lxc@.service lxc-net.service lxc-monitord.service lxc-apparmor-load
39 $(MKDIR_P) $(DESTDIR)$(SYSTEMD_UNIT_DIR)
40 - $(INSTALL_DATA) lxc.service lxc@.service lxc-net.service $(DESTDIR)$(SYSTEMD_UNIT_DIR)/
41 + $(INSTALL_DATA) lxc.service lxc@.service lxc-net.service lxc-monitord.service $(DESTDIR)$(SYSTEMD_UNIT_DIR)/
44 rm -f $(DESTDIR)$(SYSTEMD_UNIT_DIR)/lxc.service
45 rm -f $(DESTDIR)$(SYSTEMD_UNIT_DIR)/lxc@.service
46 rm -f $(DESTDIR)$(SYSTEMD_UNIT_DIR)/lxc-net.service
47 + rm -f $(DESTDIR)$(SYSTEMD_UNIT_DIR)/lxc-monitord.service
48 rmdir $(DESTDIR)$(SYSTEMD_UNIT_DIR) || :
50 pkglibexec_SCRIPTS = lxc-apparmor-load
51 diff --git a/config/init/systemd/lxc-monitord.service.in b/config/init/systemd/lxc-monitord.service.in
53 index 00000000..40635168
55 +++ b/config/init/systemd/lxc-monitord.service.in
58 +Description=LXC Container Monitoring Daemon
59 +After=syslog.service network.target
63 +ExecStart=@LIBEXECDIR@/lxc/lxc-monitord --daemon
64 +StandardOutput=syslog
68 +WantedBy=multi-user.target
69 diff --git a/configure.ac b/configure.ac
70 index 50c99836..efe56991 100644
73 @@ -658,6 +658,7 @@ AC_CONFIG_FILES([
74 config/init/systemd/lxc.service
75 config/init/systemd/lxc@.service
76 config/init/systemd/lxc-net.service
77 + config/init/systemd/lxc-monitord.service
78 config/init/sysvinit/Makefile
79 config/init/sysvinit/lxc-containers
80 config/init/sysvinit/lxc-net
81 diff --git a/lxc.spec.in b/lxc.spec.in
82 index 004ced26..1adbb6bd 100644
85 @@ -235,6 +235,7 @@ fi
86 %{_unitdir}/lxc-net.service
87 %{_unitdir}/lxc.service
88 %{_unitdir}/lxc@.service
89 +%{_unitdir}/lxc-monitord.service
91 %{_sysconfdir}/rc.d/init.d/lxc
92 %{_sysconfdir}/rc.d/init.d/lxc-net
93 diff --git a/src/lxc/cmd/lxc_monitord.c b/src/lxc/cmd/lxc_monitord.c
94 index 99f2bdb8..2bc44dea 100644
95 --- a/src/lxc/cmd/lxc_monitord.c
96 +++ b/src/lxc/cmd/lxc_monitord.c
97 @@ -346,17 +346,44 @@ static void lxc_monitord_sig_handler(int sig)
99 int main(int argc, char *argv[])
102 + int ret, pipefd = -1;
103 char logpath[PATH_MAX];
105 - char *lxcpath = argv[1];
106 + const char *lxcpath = NULL;
107 bool mainloop_opened = false;
108 bool monitord_created = false;
109 + bool persistent = false;
113 + if (argc > 1 && !strcmp(argv[1], "--daemon")) {
124 + lxcpath = lxc_global_config_value("lxc.lxcpath");
126 + ERROR("Out of memory getting lxcpath");
127 + exit(EXIT_FAILURE);
132 + if (lxc_safe_int(argv[1], &pipefd) < 0)
133 + exit(EXIT_FAILURE);
138 + if (argc != 1 || (persistent != (pipefd == -1))) {
140 - "Usage: lxc-monitord lxcpath sync-pipe-fd\n\n"
141 + "Usage: lxc-monitord lxcpath sync-pipe-fd\n"
142 + " lxc-monitord --daemon lxcpath\n\n"
143 "NOTE: lxc-monitord is intended for use by lxc internally\n"
144 " and does not need to be run by hand\n\n");
146 @@ -378,9 +405,6 @@ int main(int argc, char *argv[])
147 INFO("Failed to open log file %s, log will be lost.", lxcpath);
148 lxc_log_options_no_override();
150 - if (lxc_safe_int(argv[2], &pipefd) < 0)
151 - exit(EXIT_FAILURE);
153 if (sigfillset(&mask) ||
154 sigdelset(&mask, SIGILL) ||
155 sigdelset(&mask, SIGSEGV) ||
156 @@ -412,15 +436,17 @@ int main(int argc, char *argv[])
158 monitord_created = true;
160 - /* sync with parent, we're ignoring the return from write
161 - * because regardless if it works or not, the following
162 - * close will sync us with the parent process. the
163 - * if-empty-statement construct is to quiet the
164 - * warn-unused-result warning.
166 - if (write(pipefd, "S", 1))
169 + if (pipefd != -1) {
170 + /* sync with parent, we're ignoring the return from write
171 + * because regardless if it works or not, the following
172 + * close will sync us with the parent process. the
173 + * if-empty-statement construct is to quiet the
174 + * warn-unused-result warning.
176 + if (write(pipefd, "S", 1))
181 if (lxc_monitord_mainloop_add(&mon)) {
182 ERROR("Failed to add mainloop handlers.");
183 @@ -430,7 +456,7 @@ int main(int argc, char *argv[])
184 NOTICE("lxc-monitord with pid %d is now monitoring lxcpath %s.",
185 lxc_raw_getpid(), mon.lxcpath);
187 - ret = lxc_mainloop(&mon.descr, 1000 * 30);
188 + ret = lxc_mainloop(&mon.descr, persistent ? -1 : 1000 * 30);
190 ERROR("mainloop returned an error");