]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/verbs.c
docs: Update links to updated docs
[thirdparty/systemd.git] / src / basic / verbs.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <getopt.h>
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include <string.h>
8
9 #include "env-util.h"
10 #include "log.h"
11 #include "macro.h"
12 #include "process-util.h"
13 #include "string-util.h"
14 #include "verbs.h"
15 #include "virt.h"
16
17 /* Wraps running_in_chroot() which is used in various places, but also adds an environment variable check so external
18 * processes can reliably force this on.
19 */
20 bool running_in_chroot_or_offline(void) {
21 int r;
22
23 /* Added to support use cases like rpm-ostree, where from %post scripts we only want to execute "preset", but
24 * not "start"/"restart" for example.
25 *
26 * See docs/ENVIRONMENT.md for docs.
27 */
28 r = getenv_bool("SYSTEMD_OFFLINE");
29 if (r < 0 && r != -ENXIO)
30 log_debug_errno(r, "Failed to parse $SYSTEMD_OFFLINE: %m");
31 else if (r >= 0)
32 return r > 0;
33
34 /* We've had this condition check for a long time which basically checks for legacy chroot case like Fedora's
35 * "mock", which is used for package builds. We don't want to try to start systemd services there, since
36 * without --new-chroot we don't even have systemd running, and even if we did, adding a concept of background
37 * daemons to builds would be an enormous change, requiring considering things like how the journal output is
38 * handled, etc. And there's really not a use case today for a build talking to a service.
39 *
40 * Note this call itself also looks for a different variable SYSTEMD_IGNORE_CHROOT=1.
41 */
42 r = running_in_chroot();
43 if (r < 0)
44 log_debug_errno(r, "running_in_chroot(): %m");
45
46 return r > 0;
47 }
48
49 int dispatch_verb(int argc, char *argv[], const Verb verbs[], void *userdata) {
50 const Verb *verb;
51 const char *name;
52 unsigned i;
53 int left, r;
54
55 assert(verbs);
56 assert(verbs[0].dispatch);
57 assert(argc >= 0);
58 assert(argv);
59 assert(argc >= optind);
60
61 left = argc - optind;
62 argv += optind;
63 optind = 0;
64 name = argv[0];
65
66 for (i = 0;; i++) {
67 bool found;
68
69 /* At the end of the list? */
70 if (!verbs[i].dispatch) {
71 if (name)
72 log_error("Unknown operation %s.", name);
73 else
74 log_error("Requires operation parameter.");
75 return -EINVAL;
76 }
77
78 if (name)
79 found = streq(name, verbs[i].verb);
80 else
81 found = verbs[i].flags & VERB_DEFAULT;
82
83 if (found) {
84 verb = &verbs[i];
85 break;
86 }
87 }
88
89 assert(verb);
90
91 if (!name)
92 left = 1;
93
94 if (verb->min_args != VERB_ANY &&
95 (unsigned) left < verb->min_args) {
96 log_error("Too few arguments.");
97 return -EINVAL;
98 }
99
100 if (verb->max_args != VERB_ANY &&
101 (unsigned) left > verb->max_args) {
102 log_error("Too many arguments.");
103 return -EINVAL;
104 }
105
106 if ((verb->flags & VERB_ONLINE_ONLY) && running_in_chroot_or_offline()) {
107 if (name)
108 log_info("Running in chroot, ignoring request: %s", name);
109 else
110 log_info("Running in chroot, ignoring request.");
111 return 0;
112 }
113
114 if (verb->flags & VERB_MUST_BE_ROOT) {
115 r = must_be_root();
116 if (r < 0)
117 return r;
118 }
119
120 if (name)
121 return verb->dispatch(left, argv, userdata);
122 else {
123 char* fake[2] = {
124 (char*) verb->verb,
125 NULL
126 };
127
128 return verb->dispatch(1, fake, userdata);
129 }
130 }