1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Lennart Poettering
17 #include "process-util.h"
18 #include "string-util.h"
22 /* Wraps running_in_chroot() which is used in various places, but also adds an environment variable check so external
23 * processes can reliably force this on.
25 bool running_in_chroot_or_offline(void) {
28 /* Added to support use cases like rpm-ostree, where from %post scripts we only want to execute "preset", but
29 * not "start"/"restart" for example.
31 * See doc/ENVIRONMENT.md for docs.
33 r = getenv_bool("SYSTEMD_OFFLINE");
34 if (r < 0 && r != -ENXIO)
35 log_debug_errno(r, "Failed to parse $SYSTEMD_OFFLINE: %m");
39 /* We've had this condition check for a long time which basically checks for legacy chroot case like Fedora's
40 * "mock", which is used for package builds. We don't want to try to start systemd services there, since
41 * without --new-chroot we don't even have systemd running, and even if we did, adding a concept of background
42 * daemons to builds would be an enormous change, requiring considering things like how the journal output is
43 * handled, etc. And there's really not a use case today for a build talking to a service.
45 * Note this call itself also looks for a different variable SYSTEMD_IGNORE_CHROOT=1.
47 r = running_in_chroot();
49 log_debug_errno(r, "running_in_chroot(): %m");
54 int dispatch_verb(int argc, char *argv[], const Verb verbs[], void *userdata) {
61 assert(verbs[0].dispatch);
64 assert(argc >= optind);
72 /* At the end of the list? */
73 if (!verbs[i].dispatch) {
75 log_error("Unknown operation %s.", name);
77 log_error("Requires operation parameter.");
82 found = streq(name, verbs[i].verb);
84 found = !!(verbs[i].flags & VERB_DEFAULT);
97 if (verb->min_args != VERB_ANY &&
98 (unsigned) left < verb->min_args) {
99 log_error("Too few arguments.");
103 if (verb->max_args != VERB_ANY &&
104 (unsigned) left > verb->max_args) {
105 log_error("Too many arguments.");
109 if ((verb->flags & VERB_ONLINE_ONLY) && running_in_chroot_or_offline()) {
111 log_info("Running in chroot, ignoring request: %s", name);
113 log_info("Running in chroot, ignoring request.");
117 if (verb->flags & VERB_MUST_BE_ROOT) {
124 return verb->dispatch(left, argv + optind, userdata);
131 return verb->dispatch(1, fake, userdata);