]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/verbs.c
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / basic / verbs.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
dca59f62 2/***
dca59f62 3 Copyright 2014 Lennart Poettering
dca59f62
LP
4***/
5
11c3a366
TA
6#include <errno.h>
7#include <getopt.h>
8#include <stdbool.h>
9#include <stddef.h>
dccca82b 10#include <string.h>
11c3a366 11
f38951a6 12#include "env-util.h"
11c3a366
TA
13#include "log.h"
14#include "macro.h"
dccca82b 15#include "process-util.h"
07630cea 16#include "string-util.h"
dca59f62 17#include "verbs.h"
a16f96cd 18#include "virt.h"
dca59f62 19
c944ca8e
LP
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.
f38951a6
CW
22 */
23bool running_in_chroot_or_offline(void) {
24 int r;
25
c944ca8e
LP
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.
f38951a6 28 *
f09eb768 29 * See doc/ENVIRONMENT.md for docs.
f38951a6
CW
30 */
31 r = getenv_bool("SYSTEMD_OFFLINE");
c944ca8e
LP
32 if (r < 0 && r != -ENXIO)
33 log_debug_errno(r, "Failed to parse $SYSTEMD_OFFLINE: %m");
34 else if (r >= 0)
35 return r > 0;
f38951a6 36
c944ca8e
LP
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.
f38951a6
CW
42 *
43 * Note this call itself also looks for a different variable SYSTEMD_IGNORE_CHROOT=1.
44 */
45 r = running_in_chroot();
46 if (r < 0)
47 log_debug_errno(r, "running_in_chroot(): %m");
f38951a6 48
c944ca8e 49 return r > 0;
f38951a6
CW
50}
51
dca59f62
LP
52int dispatch_verb(int argc, char *argv[], const Verb verbs[], void *userdata) {
53 const Verb *verb;
54 const char *name;
55 unsigned i;
0c63eb71 56 int left, r;
dca59f62
LP
57
58 assert(verbs);
59 assert(verbs[0].dispatch);
60 assert(argc >= 0);
61 assert(argv);
62 assert(argc >= optind);
63
64 left = argc - optind;
65 name = argv[optind];
66
67 for (i = 0;; i++) {
68 bool found;
69
70 /* At the end of the list? */
71 if (!verbs[i].dispatch) {
72 if (name)
73 log_error("Unknown operation %s.", name);
74 else
75 log_error("Requires operation parameter.");
76 return -EINVAL;
77 }
78
43343ee7 79 if (name)
dca59f62 80 found = streq(name, verbs[i].verb);
43343ee7 81 else
5d904a6a 82 found = verbs[i].flags & VERB_DEFAULT;
dca59f62
LP
83
84 if (found) {
85 verb = &verbs[i];
86 break;
87 }
88 }
89
43343ee7
LP
90 assert(verb);
91
92 if (!name)
93 left = 1;
94
dca59f62
LP
95 if (verb->min_args != VERB_ANY &&
96 (unsigned) left < verb->min_args) {
97 log_error("Too few arguments.");
98 return -EINVAL;
99 }
100
101 if (verb->max_args != VERB_ANY &&
102 (unsigned) left > verb->max_args) {
540d8581 103 log_error("Too many arguments.");
dca59f62
LP
104 return -EINVAL;
105 }
106
c56d1e2c 107 if ((verb->flags & VERB_ONLINE_ONLY) && running_in_chroot_or_offline()) {
f38951a6
CW
108 if (name)
109 log_info("Running in chroot, ignoring request: %s", name);
110 else
111 log_info("Running in chroot, ignoring request.");
a16f96cd
LP
112 return 0;
113 }
114
c56d1e2c 115 if (verb->flags & VERB_MUST_BE_ROOT) {
0c63eb71
LP
116 r = must_be_root();
117 if (r < 0)
118 return r;
119 }
120
43343ee7
LP
121 if (name)
122 return verb->dispatch(left, argv + optind, userdata);
123 else {
124 char* fake[2] = {
125 (char*) verb->verb,
126 NULL
127 };
128
129 return verb->dispatch(1, fake, userdata);
130 }
dca59f62 131}