]> git.proxmox.com Git - lxc.git/blob - debian/patches/pve/0001-allow-running-lxc-monitord-as-a-system-daemon.patch
813b29103c1195e0ee912dd9382ab947f2bb4333
[lxc.git] / debian / patches / pve / 0001-allow-running-lxc-monitord-as-a-system-daemon.patch
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
5
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.
10
11 Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
12 ---
13 .gitignore | 1 +
14 config/init/systemd/Makefile.am | 10 +++---
15 configure.ac | 1 +
16 lxc.spec.in | 1 +
17 src/lxc/cmd/lxc_monitord.c | 60 +++++++++++++++++++++++----------
18 5 files changed, 52 insertions(+), 21 deletions(-)
19
20 diff --git a/.gitignore b/.gitignore
21 index 3cff48d96..44345454f 100644
22 --- a/.gitignore
23 +++ b/.gitignore
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 = \
37 lxc-apparmor-load \
38 lxc.service.in \
39 lxc@.service.in \
40 - lxc-net.service.in
41 + lxc-net.service.in \
42 + lxc-monitord.service.in
43
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
47
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)/
53
54 uninstall-systemd:
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) || :
60
61 pkglibexec_SCRIPTS = lxc-apparmor-load
62 diff --git a/configure.ac b/configure.ac
63 index e30ea6f6e..16c5ab8c4 100644
64 --- a/configure.ac
65 +++ b/configure.ac
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
76 --- a/lxc.spec.in
77 +++ b/lxc.spec.in
78 @@ -251,6 +251,7 @@ fi
79 %{_unitdir}/lxc-net.service
80 %{_unitdir}/lxc.service
81 %{_unitdir}/lxc@.service
82 +%{_unitdir}/lxc-monitord.service
83 %else
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)
91
92 int main(int argc, char *argv[])
93 {
94 - int ret, pipefd;
95 + int ret, pipefd = -1;
96 char logpath[PATH_MAX];
97 sigset_t mask;
98 - char *lxcpath = argv[1];
99 + const char *lxcpath = NULL;
100 bool mainloop_opened = false;
101 bool monitord_created = false;
102 + bool persistent = false;
103 struct lxc_log log;
104
105 - if (argc != 3) {
106 + if (argc > 1 && !strcmp(argv[1], "--daemon")) {
107 + persistent = true;
108 + --argc;
109 + ++argv;
110 + }
111 +
112 + if (argc > 1) {
113 + lxcpath = argv[1];
114 + --argc;
115 + ++argv;
116 + } else {
117 + lxcpath = lxc_global_config_value("lxc.lxcpath");
118 + if (!lxcpath) {
119 + ERROR("Failed to get default lxcpath");
120 + exit(EXIT_FAILURE);
121 + }
122 + }
123 +
124 + if (argc > 1) {
125 + if (lxc_safe_int(argv[1], &pipefd) < 0)
126 + exit(EXIT_FAILURE);
127 + --argc;
128 + ++argv;
129 + }
130 +
131 + if (argc != 1 || (persistent != (pipefd == -1))) {
132 fprintf(stderr,
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");
138 exit(EXIT_FAILURE);
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();
142
143 - if (lxc_safe_int(argv[2], &pipefd) < 0)
144 - exit(EXIT_FAILURE);
145 -
146 if (sigfillset(&mask) ||
147 sigdelset(&mask, SIGILL) ||
148 sigdelset(&mask, SIGSEGV) ||
149 @@ -406,15 +430,17 @@ int main(int argc, char *argv[])
150 goto on_error;
151 monitord_created = true;
152
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.
158 - */
159 - if (lxc_write_nointr(pipefd, "S", 1))
160 - ;
161 - close(pipefd);
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.
168 + */
169 + if (lxc_write_nointr(pipefd, "S", 1))
170 + ;
171 + close(pipefd);
172 + }
173
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);
178
179 for (;;) {
180 - ret = lxc_mainloop(&monitor.descr, 1000 * 30);
181 + ret = lxc_mainloop(&monitor.descr, persistent ? -1 : 1000 * 30);
182 if (ret) {
183 ERROR("mainloop returned an error");
184 break;