]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/verbs.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "process-util.h"
12 #include "string-util.h"
16 /* Wraps running_in_chroot() which is used in various places, but also adds an environment variable check so external
17 * processes can reliably force this on.
19 bool running_in_chroot_or_offline(void) {
22 /* Added to support use cases like rpm-ostree, where from %post scripts we only want to execute "preset", but
23 * not "start"/"restart" for example.
25 * See docs/ENVIRONMENT.md for docs.
27 r
= getenv_bool("SYSTEMD_OFFLINE");
28 if (r
< 0 && r
!= -ENXIO
)
29 log_debug_errno(r
, "Failed to parse $SYSTEMD_OFFLINE: %m");
33 /* We've had this condition check for a long time which basically checks for legacy chroot case like Fedora's
34 * "mock", which is used for package builds. We don't want to try to start systemd services there, since
35 * without --new-chroot we don't even have systemd running, and even if we did, adding a concept of background
36 * daemons to builds would be an enormous change, requiring considering things like how the journal output is
37 * handled, etc. And there's really not a use case today for a build talking to a service.
39 * Note this call itself also looks for a different variable SYSTEMD_IGNORE_CHROOT=1.
41 r
= running_in_chroot();
43 log_debug_errno(r
, "running_in_chroot(): %m");
48 const Verb
* verbs_find_verb(const char *name
, const Verb verbs
[]) {
49 for (size_t i
= 0; verbs
[i
].dispatch
; i
++)
50 if (streq_ptr(name
, verbs
[i
].verb
) ||
51 (!name
&& FLAGS_SET(verbs
[i
].flags
, VERB_DEFAULT
)))
54 /* At the end of the list? */
58 int dispatch_verb(int argc
, char *argv
[], const Verb verbs
[], void *userdata
) {
64 assert(verbs
[0].dispatch
);
67 assert(argc
>= optind
);
74 verb
= verbs_find_verb(name
, verbs
);
77 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
78 "Unknown command verb %s.", name
);
80 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
81 "Command verb required.");
87 if (verb
->min_args
!= VERB_ANY
&&
88 (unsigned) left
< verb
->min_args
)
89 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
90 "Too few arguments.");
92 if (verb
->max_args
!= VERB_ANY
&&
93 (unsigned) left
> verb
->max_args
)
94 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
95 "Too many arguments.");
97 if ((verb
->flags
& VERB_ONLINE_ONLY
) && running_in_chroot_or_offline()) {
98 log_info("Running in chroot, ignoring command '%s'", name
?: verb
->verb
);
103 return verb
->dispatch(left
, argv
, userdata
);
110 return verb
->dispatch(1, fake
, userdata
);