summaryrefslogtreecommitdiff
path: root/src/basic/verbs.c
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2017-12-13 16:04:41 -0500
committerSven Eden <yamakuzure@gmx.net>2018-05-30 07:49:37 +0200
commitdb80da22478c43afb2541427cf9f8052af4c3312 (patch)
tree3dd21b84597a60bc9b446388868aa5c2db0b0e94 /src/basic/verbs.c
parentb8735de11d27cae6d33a1814b99fcaeb839907e5 (diff)
systemctl,verbs: Introduce SYSTEMD_OFFLINE environment variable
A lot of code references the `running_in_chroot()` function; while I didn't dig I'm pretty certain this arose to deal with situations like RPM package builds in `mock` - there we don't want the `%post`s to `systemctl start` for example. And actually this exact same use case arises for [rpm-ostree](https://github.com/projectatomic/rpm-ostree/) where we implement offline upgrades by default; the `%post`s are always run in a new chroot using [bwrap](https://github.com/projectatomic/bubblewrap). And here's the problem: bwrap creates proper mount roots, so it passes `running_in_chroot()`, and then if a script tries to do `systemctl start` we get: `System has not been booted with systemd as init system (PID 1)` but that's an *error*, unlike the `running_in_chroot()` case where we ignore. Further complicating things is there are real world RPM packages like `glusterfs` which end up invoking `systemctl start`. A while ago, the `SYSTEMD_IGNORE_CHROOT` environment variable was added for the inverse case of running in a chroot, but still wanting to use systemd as PID 1 (presumably some broken initramfs setups?). Let's introduce a `SYSTEMD_OFFLINE` environment variable for cases like mock/rpm-ostree so we can force on the "ignore everything except preset" logic. This way we'll still not start services even if mock switches to use nspawn or bwrap or something else that isn't a chroot. We also cleanly supercede the `SYSTEMD_IGNORE_CHROOT=1` which is now spelled `SYSTEMD_OFFLINE=0`. (Suggested by @poettering) Also I made things slightly nicer here and we now print the ignored operation.
Diffstat (limited to 'src/basic/verbs.c')
-rw-r--r--src/basic/verbs.c49
1 files changed, 47 insertions, 2 deletions
diff --git a/src/basic/verbs.c b/src/basic/verbs.c
index cb42e6dd0..97bb59f5e 100644
--- a/src/basic/verbs.c
+++ b/src/basic/verbs.c
@@ -23,12 +23,54 @@
#include <stdbool.h>
#include <stddef.h>
+//#include "env-util.h"
#include "log.h"
#include "macro.h"
#include "string-util.h"
#include "verbs.h"
#include "virt.h"
+/* Wraps running_in_chroot() which is used in various places,
+ * but also adds an environment variable check so external processes
+ * can reliably force this on.
+ */
+bool running_in_chroot_or_offline(void) {
+ int r;
+
+ /* Added to support use cases like rpm-ostree, where from %post
+ * scripts we only want to execute "preset", but not "start"/"restart"
+ * for example.
+ *
+ * See ENVIRONMENT.md for docs.
+ */
+ r = getenv_bool("SYSTEMD_OFFLINE");
+ if (r < 0)
+ log_debug_errno(r, "Parsing SYSTEMD_OFFLINE: %m");
+ else if (r == 0)
+ return false;
+ else
+ return true;
+
+ /* We've had this condition check for a long time which basically
+ * checks for legacy chroot case like Fedora's
+ * "mock", which is used for package builds. We don't want
+ * to try to start systemd services there, since without --new-chroot
+ * we don't even have systemd running, and even if we did, adding
+ * a concept of background daemons to builds would be an enormous change,
+ * requiring considering things like how the journal output is handled, etc.
+ * And there's really not a use case today for a build talking to a service.
+ *
+ * Note this call itself also looks for a different variable SYSTEMD_IGNORE_CHROOT=1.
+ */
+ r = running_in_chroot();
+ if (r < 0)
+ log_debug_errno(r, "running_in_chroot(): %m");
+ else if (r > 0)
+ return true;
+
+ return false;
+}
+
int dispatch_verb(int argc, char *argv[], const Verb verbs[], void *userdata) {
const Verb *verb;
const char *name;
@@ -84,8 +126,11 @@ int dispatch_verb(int argc, char *argv[], const Verb verbs[], void *userdata) {
return -EINVAL;
}
- if ((verb->flags & VERB_NOCHROOT) && running_in_chroot() > 0) {
- log_info("Running in chroot, ignoring request.");
+ if ((verb->flags & VERB_NOCHROOT) && running_in_chroot_or_offline()) {
+ if (name)
+ log_info("Running in chroot, ignoring request: %s", name);
+ else
+ log_info("Running in chroot, ignoring request.");
return 0;
}