]>
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/>.
30 #include "process-util.h"
31 #include "string-util.h"
35 /* Wraps running_in_chroot() which is used in various places, but also adds an environment variable check so external
36 * processes can reliably force this on.
38 bool running_in_chroot_or_offline(void) {
41 /* Added to support use cases like rpm-ostree, where from %post scripts we only want to execute "preset", but
42 * not "start"/"restart" for example.
44 * See ENVIRONMENT.md for docs.
46 r
= getenv_bool("SYSTEMD_OFFLINE");
47 if (r
< 0 && r
!= -ENXIO
)
48 log_debug_errno(r
, "Failed to parse $SYSTEMD_OFFLINE: %m");
52 /* We've had this condition check for a long time which basically checks for legacy chroot case like Fedora's
53 * "mock", which is used for package builds. We don't want to try to start systemd services there, since
54 * without --new-chroot we don't even have systemd running, and even if we did, adding a concept of background
55 * daemons to builds would be an enormous change, requiring considering things like how the journal output is
56 * handled, etc. And there's really not a use case today for a build talking to a service.
58 * Note this call itself also looks for a different variable SYSTEMD_IGNORE_CHROOT=1.
60 r
= running_in_chroot();
62 log_debug_errno(r
, "running_in_chroot(): %m");
67 int dispatch_verb(int argc
, char *argv
[], const Verb verbs
[], void *userdata
) {
74 assert(verbs
[0].dispatch
);
77 assert(argc
>= optind
);
85 /* At the end of the list? */
86 if (!verbs
[i
].dispatch
) {
88 log_error("Unknown operation %s.", name
);
90 log_error("Requires operation parameter.");
95 found
= streq(name
, verbs
[i
].verb
);
97 found
= !!(verbs
[i
].flags
& VERB_DEFAULT
);
110 if (verb
->min_args
!= VERB_ANY
&&
111 (unsigned) left
< verb
->min_args
) {
112 log_error("Too few arguments.");
116 if (verb
->max_args
!= VERB_ANY
&&
117 (unsigned) left
> verb
->max_args
) {
118 log_error("Too many arguments.");
122 if ((verb
->flags
& VERB_ONLINE_ONLY
) && running_in_chroot_or_offline()) {
124 log_info("Running in chroot, ignoring request: %s", name
);
126 log_info("Running in chroot, ignoring request.");
130 if (verb
->flags
& VERB_MUST_BE_ROOT
) {
137 return verb
->dispatch(left
, argv
+ optind
, userdata
);
144 return verb
->dispatch(1, fake
, userdata
);