chiark / gitweb /
tree-wide: remove Lennart's copyright lines
[elogind.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 doc/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         name = argv[optind];
63
64         for (i = 0;; i++) {
65                 bool found;
66
67                 /* At the end of the list? */
68                 if (!verbs[i].dispatch) {
69                         if (name)
70                                 log_error("Unknown operation %s.", name);
71                         else
72                                 log_error("Requires operation parameter.");
73                         return -EINVAL;
74                 }
75
76                 if (name)
77                         found = streq(name, verbs[i].verb);
78                 else
79                         found = verbs[i].flags & VERB_DEFAULT;
80
81                 if (found) {
82                         verb = &verbs[i];
83                         break;
84                 }
85         }
86
87         assert(verb);
88
89         if (!name)
90                 left = 1;
91
92         if (verb->min_args != VERB_ANY &&
93             (unsigned) left < verb->min_args) {
94                 log_error("Too few arguments.");
95                 return -EINVAL;
96         }
97
98         if (verb->max_args != VERB_ANY &&
99             (unsigned) left > verb->max_args) {
100                 log_error("Too many arguments.");
101                 return -EINVAL;
102         }
103
104         if ((verb->flags & VERB_ONLINE_ONLY) && running_in_chroot_or_offline()) {
105                 if (name)
106                         log_info("Running in chroot, ignoring request: %s", name);
107                 else
108                         log_info("Running in chroot, ignoring request.");
109                 return 0;
110         }
111
112         if (verb->flags & VERB_MUST_BE_ROOT) {
113                 r = must_be_root();
114                 if (r < 0)
115                         return r;
116         }
117
118         if (name)
119                 return verb->dispatch(left, argv + optind, userdata);
120         else {
121                 char* fake[2] = {
122                         (char*) verb->verb,
123                         NULL
124                 };
125
126                 return verb->dispatch(1, fake, userdata);
127         }
128 }