/src/systemd/src/shared/verbs.c
Line | Count | Source (jump to first uncovered line) |
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 | 0 | bool running_in_chroot_or_offline(void) { |
21 | 0 | int r; |
22 | 0 |
|
23 | 0 | /* Added to support use cases like rpm-ostree, where from %post scripts we only want to execute "preset", but |
24 | 0 | * not "start"/"restart" for example. |
25 | 0 | * |
26 | 0 | * See docs/ENVIRONMENT.md for docs. |
27 | 0 | */ |
28 | 0 | r = getenv_bool("SYSTEMD_OFFLINE"); |
29 | 0 | if (r < 0 && r != -ENXIO) |
30 | 0 | log_debug_errno(r, "Failed to parse $SYSTEMD_OFFLINE: %m"); |
31 | 0 | else if (r >= 0) |
32 | 0 | return r > 0; |
33 | 0 | |
34 | 0 | /* We've had this condition check for a long time which basically checks for legacy chroot case like Fedora's |
35 | 0 | * "mock", which is used for package builds. We don't want to try to start systemd services there, since |
36 | 0 | * without --new-chroot we don't even have systemd running, and even if we did, adding a concept of background |
37 | 0 | * daemons to builds would be an enormous change, requiring considering things like how the journal output is |
38 | 0 | * handled, etc. And there's really not a use case today for a build talking to a service. |
39 | 0 | * |
40 | 0 | * Note this call itself also looks for a different variable SYSTEMD_IGNORE_CHROOT=1. |
41 | 0 | */ |
42 | 0 | r = running_in_chroot(); |
43 | 0 | if (r < 0) |
44 | 0 | log_debug_errno(r, "running_in_chroot(): %m"); |
45 | 0 |
|
46 | 0 | return r > 0; |
47 | 0 | } |
48 | | |
49 | 0 | int dispatch_verb(int argc, char *argv[], const Verb verbs[], void *userdata) { |
50 | 0 | const Verb *verb; |
51 | 0 | const char *name; |
52 | 0 | unsigned i; |
53 | 0 | int left; |
54 | 0 |
|
55 | 0 | assert(verbs); |
56 | 0 | assert(verbs[0].dispatch); |
57 | 0 | assert(argc >= 0); |
58 | 0 | assert(argv); |
59 | 0 | assert(argc >= optind); |
60 | 0 |
|
61 | 0 | left = argc - optind; |
62 | 0 | argv += optind; |
63 | 0 | optind = 0; |
64 | 0 | name = argv[0]; |
65 | 0 |
|
66 | 0 | for (i = 0;; i++) { |
67 | 0 | bool found; |
68 | 0 |
|
69 | 0 | /* At the end of the list? */ |
70 | 0 | if (!verbs[i].dispatch) { |
71 | 0 | if (name) |
72 | 0 | log_error("Unknown operation %s.", name); |
73 | 0 | else |
74 | 0 | log_error("Requires operation parameter."); |
75 | 0 | return -EINVAL; |
76 | 0 | } |
77 | 0 |
|
78 | 0 | if (name) |
79 | 0 | found = streq(name, verbs[i].verb); |
80 | 0 | else |
81 | 0 | found = verbs[i].flags & VERB_DEFAULT; |
82 | 0 |
|
83 | 0 | if (found) { |
84 | 0 | verb = &verbs[i]; |
85 | 0 | break; |
86 | 0 | } |
87 | 0 | } |
88 | 0 |
|
89 | 0 | assert(verb); |
90 | 0 |
|
91 | 0 | if (!name) |
92 | 0 | left = 1; |
93 | 0 |
|
94 | 0 | if (verb->min_args != VERB_ANY && |
95 | 0 | (unsigned) left < verb->min_args) |
96 | 0 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
97 | 0 | "Too few arguments."); |
98 | 0 | |
99 | 0 | if (verb->max_args != VERB_ANY && |
100 | 0 | (unsigned) left > verb->max_args) |
101 | 0 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
102 | 0 | "Too many arguments."); |
103 | 0 | |
104 | 0 | if ((verb->flags & VERB_ONLINE_ONLY) && running_in_chroot_or_offline()) { |
105 | 0 | if (name) |
106 | 0 | log_info("Running in chroot, ignoring request: %s", name); |
107 | 0 | else |
108 | 0 | log_info("Running in chroot, ignoring request."); |
109 | 0 | return 0; |
110 | 0 | } |
111 | 0 |
|
112 | 0 | if (name) |
113 | 0 | return verb->dispatch(left, argv, userdata); |
114 | 0 | else { |
115 | 0 | char* fake[2] = { |
116 | 0 | (char*) verb->verb, |
117 | 0 | NULL |
118 | 0 | }; |
119 | 0 |
|
120 | 0 | return verb->dispatch(1, fake, userdata); |
121 | 0 | } |
122 | 0 | } |