]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/verbs.c
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / basic / verbs.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2014 Lennart Poettering
4 ***/
5
6 #include <errno.h>
7 #include <getopt.h>
8 #include <stdbool.h>
9 #include <stddef.h>
10 #include <string.h>
11
12 #include "env-util.h"
13 #include "log.h"
14 #include "macro.h"
15 #include "process-util.h"
16 #include "string-util.h"
17 #include "verbs.h"
18 #include "virt.h"
19
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.
22 */
23 bool running_in_chroot_or_offline(void) {
24 int r;
25
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.
28 *
29 * See doc/ENVIRONMENT.md for docs.
30 */
31 r = getenv_bool("SYSTEMD_OFFLINE");
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;
36
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.
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");
48
49 return r > 0;
50 }
51
52 int dispatch_verb(int argc, char *argv[], const Verb verbs[], void *userdata) {
53 const Verb *verb;
54 const char *name;
55 unsigned i;
56 int left, r;
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
79 if (name)
80 found = streq(name, verbs[i].verb);
81 else
82 found = verbs[i].flags & VERB_DEFAULT;
83
84 if (found) {
85 verb = &verbs[i];
86 break;
87 }
88 }
89
90 assert(verb);
91
92 if (!name)
93 left = 1;
94
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) {
103 log_error("Too many arguments.");
104 return -EINVAL;
105 }
106
107 if ((verb->flags & VERB_ONLINE_ONLY) && running_in_chroot_or_offline()) {
108 if (name)
109 log_info("Running in chroot, ignoring request: %s", name);
110 else
111 log_info("Running in chroot, ignoring request.");
112 return 0;
113 }
114
115 if (verb->flags & VERB_MUST_BE_ROOT) {
116 r = must_be_root();
117 if (r < 0)
118 return r;
119 }
120
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 }
131 }