]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/verbs.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2014 Lennart Poettering
15 #include "process-util.h"
16 #include "string-util.h"
20 /* Wraps running_in_chroot() which is used in various places, but also adds an environment variable check so external
21 * processes can reliably force this on.
23 bool running_in_chroot_or_offline(void) {
26 /* Added to support use cases like rpm-ostree, where from %post scripts we only want to execute "preset", but
27 * not "start"/"restart" for example.
29 * See doc/ENVIRONMENT.md for docs.
31 r
= getenv_bool("SYSTEMD_OFFLINE");
32 if (r
< 0 && r
!= -ENXIO
)
33 log_debug_errno(r
, "Failed to parse $SYSTEMD_OFFLINE: %m");
37 /* We've had this condition check for a long time which basically checks for legacy chroot case like Fedora's
38 * "mock", which is used for package builds. We don't want to try to start systemd services there, since
39 * without --new-chroot we don't even have systemd running, and even if we did, adding a concept of background
40 * daemons to builds would be an enormous change, requiring considering things like how the journal output is
41 * handled, etc. And there's really not a use case today for a build talking to a service.
43 * Note this call itself also looks for a different variable SYSTEMD_IGNORE_CHROOT=1.
45 r
= running_in_chroot();
47 log_debug_errno(r
, "running_in_chroot(): %m");
52 int dispatch_verb(int argc
, char *argv
[], const Verb verbs
[], void *userdata
) {
59 assert(verbs
[0].dispatch
);
62 assert(argc
>= optind
);
70 /* At the end of the list? */
71 if (!verbs
[i
].dispatch
) {
73 log_error("Unknown operation %s.", name
);
75 log_error("Requires operation parameter.");
80 found
= streq(name
, verbs
[i
].verb
);
82 found
= verbs
[i
].flags
& VERB_DEFAULT
;
95 if (verb
->min_args
!= VERB_ANY
&&
96 (unsigned) left
< verb
->min_args
) {
97 log_error("Too few arguments.");
101 if (verb
->max_args
!= VERB_ANY
&&
102 (unsigned) left
> verb
->max_args
) {
103 log_error("Too many arguments.");
107 if ((verb
->flags
& VERB_ONLINE_ONLY
) && running_in_chroot_or_offline()) {
109 log_info("Running in chroot, ignoring request: %s", name
);
111 log_info("Running in chroot, ignoring request.");
115 if (verb
->flags
& VERB_MUST_BE_ROOT
) {
122 return verb
->dispatch(left
, argv
+ optind
, userdata
);
129 return verb
->dispatch(1, fake
, userdata
);