]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/verbs.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "string-util.h"
33 /* Wraps running_in_chroot() which is used in various places,
34 * but also adds an environment variable check so external processes
35 * can reliably force this on.
37 bool running_in_chroot_or_offline(void) {
40 /* Added to support use cases like rpm-ostree, where from %post
41 * scripts we only want to execute "preset", but not "start"/"restart"
44 * See ENVIRONMENT.md for docs.
46 r
= getenv_bool("SYSTEMD_OFFLINE");
48 log_debug_errno(r
, "Parsing SYSTEMD_OFFLINE: %m");
54 /* We've had this condition check for a long time which basically
55 * checks for legacy chroot case like Fedora's
56 * "mock", which is used for package builds. We don't want
57 * to try to start systemd services there, since without --new-chroot
58 * we don't even have systemd running, and even if we did, adding
59 * a concept of background daemons to builds would be an enormous change,
60 * requiring considering things like how the journal output is handled, etc.
61 * And there's really not a use case today for a build talking to a service.
63 * Note this call itself also looks for a different variable SYSTEMD_IGNORE_CHROOT=1.
65 r
= running_in_chroot();
67 log_debug_errno(r
, "running_in_chroot(): %m");
74 int dispatch_verb(int argc
, char *argv
[], const Verb verbs
[], void *userdata
) {
81 assert(verbs
[0].dispatch
);
84 assert(argc
>= optind
);
92 /* At the end of the list? */
93 if (!verbs
[i
].dispatch
) {
95 log_error("Unknown operation %s.", name
);
97 log_error("Requires operation parameter.");
102 found
= streq(name
, verbs
[i
].verb
);
104 found
= !!(verbs
[i
].flags
& VERB_DEFAULT
);
117 if (verb
->min_args
!= VERB_ANY
&&
118 (unsigned) left
< verb
->min_args
) {
119 log_error("Too few arguments.");
123 if (verb
->max_args
!= VERB_ANY
&&
124 (unsigned) left
> verb
->max_args
) {
125 log_error("Too many arguments.");
129 if ((verb
->flags
& VERB_NOCHROOT
) && running_in_chroot_or_offline()) {
131 log_info("Running in chroot, ignoring request: %s", name
);
133 log_info("Running in chroot, ignoring request.");
137 if (verb
->flags
& VERB_MUSTBEROOT
) {
144 return verb
->dispatch(left
, argv
+ optind
, userdata
);
151 return verb
->dispatch(1, fake
, userdata
);