1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <errno.h>
4 #include <stdio.h>
5 
6 #include "bus-util.h"
7 #include "manager.h"
8 #include "manager-dump.h"
9 #include "rm-rf.h"
10 #include "service.h"
11 #include "slice.h"
12 #include "special.h"
13 #include "strv.h"
14 #include "tests.h"
15 #include "unit-serialize.h"
16 
verify_dependency_atoms(void)17 static void verify_dependency_atoms(void) {
18         UnitDependencyAtom combined = 0, multi_use_atoms = 0;
19 
20         /* Let's guarantee that our dependency type/atom translation tables are fully correct */
21 
22         for (UnitDependency d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
23                 UnitDependencyAtom a;
24                 UnitDependency reverse;
25                 bool has_superset = false;
26 
27                 assert_se((a = unit_dependency_to_atom(d)) >= 0);
28 
29                 for (UnitDependency t = 0; t < _UNIT_DEPENDENCY_MAX; t++) {
30                         UnitDependencyAtom b;
31 
32                         if (t == d)
33                                 continue;
34 
35                         assert_se((b = unit_dependency_to_atom(t)) >= 0);
36 
37                         if ((a & b) == a) {
38                                 has_superset = true;
39                                 break;
40                         }
41                 }
42 
43                 reverse = unit_dependency_from_unique_atom(a);
44                 assert_se(reverse == _UNIT_DEPENDENCY_INVALID || reverse >= 0);
45 
46                 assert_se((reverse < 0) == has_superset); /* If one dependency type is a superset of another,
47                                                            * then the reverse mapping is not unique, verify
48                                                            * that. */
49 
50                 log_info("Verified dependency type: %s", unit_dependency_to_string(d));
51 
52                 multi_use_atoms |= combined & a;
53                 combined |= a;
54         }
55 
56         /* Make sure all atoms are used, i.e. there's at least one dependency type that references it. */
57         assert_se(combined == _UNIT_DEPENDENCY_ATOM_MAX);
58 
59         for (UnitDependencyAtom a = 1; a <= _UNIT_DEPENDENCY_ATOM_MAX; a <<= 1) {
60 
61                 if (multi_use_atoms & a) {
62                         /* If an atom is used by multiple dep types, then mapping the atom to a dependency is
63                          * not unique and *must* fail */
64                         assert_se(unit_dependency_from_unique_atom(a) == _UNIT_DEPENDENCY_INVALID);
65                         continue;
66                 }
67 
68                 /* If only a single dep type uses specific atom, let's guarantee our mapping table is
69                 complete, and thus the atom can be mapped to the single dep type that is used. */
70                 assert_se(unit_dependency_from_unique_atom(a) >= 0);
71         }
72 }
73 
main(int argc,char * argv[])74 int main(int argc, char *argv[]) {
75         _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
76         _cleanup_(sd_bus_error_free) sd_bus_error err = SD_BUS_ERROR_NULL;
77         _cleanup_(manager_freep) Manager *m = NULL;
78         Unit *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL, *g = NULL,
79                 *h = NULL, *i = NULL, *a_conj = NULL, *unit_with_multiple_dashes = NULL, *stub = NULL,
80                 *tomato = NULL, *sauce = NULL, *fruit = NULL, *zupa = NULL;
81         Job *j;
82         int r;
83 
84         test_setup_logging(LOG_DEBUG);
85 
86         r = enter_cgroup_subroot(NULL);
87         if (r == -ENOMEDIUM)
88                 return log_tests_skipped("cgroupfs not available");
89 
90         /* prepare the test */
91         _cleanup_free_ char *unit_dir = NULL;
92         assert_se(get_testdata_dir("units", &unit_dir) >= 0);
93         assert_se(set_unit_path(unit_dir) >= 0);
94         assert_se(runtime_dir = setup_fake_runtime_dir());
95 
96         r = manager_new(LOOKUP_SCOPE_USER, MANAGER_TEST_RUN_BASIC, &m);
97         if (manager_errno_skip_test(r))
98                 return log_tests_skipped_errno(r, "manager_new");
99         assert_se(r >= 0);
100         assert_se(manager_startup(m, NULL, NULL, NULL) >= 0);
101 
102         printf("Load1:\n");
103         assert_se(manager_load_startable_unit_or_warn(m, "a.service", NULL, &a) >= 0);
104         assert_se(manager_load_startable_unit_or_warn(m, "b.service", NULL, &b) >= 0);
105         assert_se(manager_load_startable_unit_or_warn(m, "c.service", NULL, &c) >= 0);
106         manager_dump_units(m, stdout, "\t");
107 
108         printf("Test1: (Trivial)\n");
109         r = manager_add_job(m, JOB_START, c, JOB_REPLACE, NULL, &err, &j);
110         if (sd_bus_error_is_set(&err))
111                 log_error("error: %s: %s", err.name, err.message);
112         assert_se(r == 0);
113         manager_dump_jobs(m, stdout, "\t");
114 
115         printf("Load2:\n");
116         manager_clear_jobs(m);
117         assert_se(manager_load_startable_unit_or_warn(m, "d.service", NULL, &d) >= 0);
118         assert_se(manager_load_startable_unit_or_warn(m, "e.service", NULL, &e) >= 0);
119         manager_dump_units(m, stdout, "\t");
120 
121         printf("Test2: (Cyclic Order, Unfixable)\n");
122         assert_se(manager_add_job(m, JOB_START, d, JOB_REPLACE, NULL, NULL, &j) == -EDEADLK);
123         manager_dump_jobs(m, stdout, "\t");
124 
125         printf("Test3: (Cyclic Order, Fixable, Garbage Collector)\n");
126         assert_se(manager_add_job(m, JOB_START, e, JOB_REPLACE, NULL, NULL, &j) == 0);
127         manager_dump_jobs(m, stdout, "\t");
128 
129         printf("Test4: (Identical transaction)\n");
130         assert_se(manager_add_job(m, JOB_START, e, JOB_FAIL, NULL, NULL, &j) == 0);
131         manager_dump_jobs(m, stdout, "\t");
132 
133         printf("Load3:\n");
134         assert_se(manager_load_startable_unit_or_warn(m, "g.service", NULL, &g) >= 0);
135         manager_dump_units(m, stdout, "\t");
136 
137         printf("Test5: (Colliding transaction, fail)\n");
138         assert_se(manager_add_job(m, JOB_START, g, JOB_FAIL, NULL, NULL, &j) == -EDEADLK);
139 
140         printf("Test6: (Colliding transaction, replace)\n");
141         assert_se(manager_add_job(m, JOB_START, g, JOB_REPLACE, NULL, NULL, &j) == 0);
142         manager_dump_jobs(m, stdout, "\t");
143 
144         printf("Test7: (Unmergeable job type, fail)\n");
145         assert_se(manager_add_job(m, JOB_STOP, g, JOB_FAIL, NULL, NULL, &j) == -EDEADLK);
146 
147         printf("Test8: (Mergeable job type, fail)\n");
148         assert_se(manager_add_job(m, JOB_RESTART, g, JOB_FAIL, NULL, NULL, &j) == 0);
149         manager_dump_jobs(m, stdout, "\t");
150 
151         printf("Test9: (Unmergeable job type, replace)\n");
152         assert_se(manager_add_job(m, JOB_STOP, g, JOB_REPLACE, NULL, NULL, &j) == 0);
153         manager_dump_jobs(m, stdout, "\t");
154 
155         printf("Load4:\n");
156         assert_se(manager_load_startable_unit_or_warn(m, "h.service", NULL, &h) >= 0);
157         manager_dump_units(m, stdout, "\t");
158 
159         printf("Test10: (Unmergeable job type of auxiliary job, fail)\n");
160         assert_se(manager_add_job(m, JOB_START, h, JOB_FAIL, NULL, NULL, &j) == 0);
161         manager_dump_jobs(m, stdout, "\t");
162 
163         printf("Load5:\n");
164         manager_clear_jobs(m);
165         assert_se(manager_load_startable_unit_or_warn(m, "i.service", NULL, &i) >= 0);
166         SERVICE(a)->state = SERVICE_RUNNING;
167         SERVICE(d)->state = SERVICE_RUNNING;
168         manager_dump_units(m, stdout, "\t");
169 
170         printf("Test11: (Start/stop job ordering, execution cycle)\n");
171         assert_se(manager_add_job(m, JOB_START, i, JOB_FAIL, NULL, NULL, &j) == 0);
172         assert_se(unit_has_job_type(a, JOB_STOP));
173         assert_se(unit_has_job_type(d, JOB_STOP));
174         assert_se(unit_has_job_type(b, JOB_START));
175         manager_dump_jobs(m, stdout, "\t");
176 
177         printf("Load6:\n");
178         manager_clear_jobs(m);
179         assert_se(manager_load_startable_unit_or_warn(m, "a-conj.service", NULL, &a_conj) >= 0);
180         SERVICE(a)->state = SERVICE_DEAD;
181         manager_dump_units(m, stdout, "\t");
182 
183         printf("Test12: (Trivial cycle, Unfixable)\n");
184         assert_se(manager_add_job(m, JOB_START, a_conj, JOB_REPLACE, NULL, NULL, &j) == -EDEADLK);
185         manager_dump_jobs(m, stdout, "\t");
186 
187         assert_se(!hashmap_get(unit_get_dependencies(a, UNIT_PROPAGATES_RELOAD_TO), b));
188         assert_se(!hashmap_get(unit_get_dependencies(b, UNIT_RELOAD_PROPAGATED_FROM), a));
189         assert_se(!hashmap_get(unit_get_dependencies(a, UNIT_PROPAGATES_RELOAD_TO), c));
190         assert_se(!hashmap_get(unit_get_dependencies(c, UNIT_RELOAD_PROPAGATED_FROM), a));
191 
192         assert_se(unit_add_dependency(a, UNIT_PROPAGATES_RELOAD_TO, b, true, UNIT_DEPENDENCY_UDEV) == 0);
193         assert_se(unit_add_dependency(a, UNIT_PROPAGATES_RELOAD_TO, c, true, UNIT_DEPENDENCY_PROC_SWAP) == 0);
194 
195         assert_se( hashmap_get(unit_get_dependencies(a, UNIT_PROPAGATES_RELOAD_TO), b));
196         assert_se( hashmap_get(unit_get_dependencies(b, UNIT_RELOAD_PROPAGATED_FROM), a));
197         assert_se( hashmap_get(unit_get_dependencies(a, UNIT_PROPAGATES_RELOAD_TO), c));
198         assert_se( hashmap_get(unit_get_dependencies(c, UNIT_RELOAD_PROPAGATED_FROM), a));
199 
200         unit_remove_dependencies(a, UNIT_DEPENDENCY_UDEV);
201 
202         assert_se(!hashmap_get(unit_get_dependencies(a, UNIT_PROPAGATES_RELOAD_TO), b));
203         assert_se(!hashmap_get(unit_get_dependencies(b, UNIT_RELOAD_PROPAGATED_FROM), a));
204         assert_se( hashmap_get(unit_get_dependencies(a, UNIT_PROPAGATES_RELOAD_TO), c));
205         assert_se( hashmap_get(unit_get_dependencies(c, UNIT_RELOAD_PROPAGATED_FROM), a));
206 
207         unit_remove_dependencies(a, UNIT_DEPENDENCY_PROC_SWAP);
208 
209         assert_se(!hashmap_get(unit_get_dependencies(a, UNIT_PROPAGATES_RELOAD_TO), b));
210         assert_se(!hashmap_get(unit_get_dependencies(b, UNIT_RELOAD_PROPAGATED_FROM), a));
211         assert_se(!hashmap_get(unit_get_dependencies(a, UNIT_PROPAGATES_RELOAD_TO), c));
212         assert_se(!hashmap_get(unit_get_dependencies(c, UNIT_RELOAD_PROPAGATED_FROM), a));
213 
214         assert_se(manager_load_unit(m, "unit-with-multiple-dashes.service", NULL, NULL, &unit_with_multiple_dashes) >= 0);
215 
216         assert_se(strv_equal(unit_with_multiple_dashes->documentation, STRV_MAKE("man:test", "man:override2", "man:override3")));
217         assert_se(streq_ptr(unit_with_multiple_dashes->description, "override4"));
218 
219         /* Now merge a synthetic unit into the existing one */
220         assert_se(unit_new_for_name(m, sizeof(Service), "merged.service", &stub) >= 0);
221         assert_se(unit_add_dependency_by_name(stub, UNIT_AFTER, SPECIAL_BASIC_TARGET, true, UNIT_DEPENDENCY_FILE) >= 0);
222         assert_se(unit_add_dependency_by_name(stub, UNIT_AFTER, "quux.target", true, UNIT_DEPENDENCY_FILE) >= 0);
223         assert_se(unit_add_dependency_by_name(stub, UNIT_AFTER, SPECIAL_ROOT_SLICE, true, UNIT_DEPENDENCY_FILE) >= 0);
224         assert_se(unit_add_dependency_by_name(stub, UNIT_REQUIRES, "non-existing.mount", true, UNIT_DEPENDENCY_FILE) >= 0);
225         assert_se(unit_add_dependency_by_name(stub, UNIT_ON_FAILURE, "non-existing-on-failure.target", true, UNIT_DEPENDENCY_FILE) >= 0);
226         assert_se(unit_add_dependency_by_name(stub, UNIT_ON_SUCCESS, "non-existing-on-success.target", true, UNIT_DEPENDENCY_FILE) >= 0);
227 
228         log_info("/* Merging a+stub, dumps before */");
229         unit_dump(a, stderr, NULL);
230         unit_dump(stub, stderr, NULL);
231         assert_se(unit_merge(a, stub) >= 0);
232         log_info("/* Dump of merged a+stub */");
233         unit_dump(a, stderr, NULL);
234 
235         assert_se( unit_has_dependency(a, UNIT_ATOM_AFTER, manager_get_unit(m, SPECIAL_BASIC_TARGET)));
236         assert_se( unit_has_dependency(a, UNIT_ATOM_AFTER, manager_get_unit(m, "quux.target")));
237         assert_se( unit_has_dependency(a, UNIT_ATOM_AFTER, manager_get_unit(m, SPECIAL_ROOT_SLICE)));
238         assert_se( unit_has_dependency(a, UNIT_ATOM_PULL_IN_START, manager_get_unit(m, "non-existing.mount")));
239         assert_se( unit_has_dependency(a, UNIT_ATOM_RETROACTIVE_START_REPLACE, manager_get_unit(m, "non-existing.mount")));
240         assert_se( unit_has_dependency(a, UNIT_ATOM_ON_FAILURE, manager_get_unit(m, "non-existing-on-failure.target")));
241         assert_se( unit_has_dependency(manager_get_unit(m, "non-existing-on-failure.target"), UNIT_ATOM_ON_FAILURE_OF, a));
242         assert_se( unit_has_dependency(a, UNIT_ATOM_ON_SUCCESS, manager_get_unit(m, "non-existing-on-success.target")));
243         assert_se( unit_has_dependency(manager_get_unit(m, "non-existing-on-success.target"), UNIT_ATOM_ON_SUCCESS_OF, a));
244         assert_se(!unit_has_dependency(a, UNIT_ATOM_ON_FAILURE, manager_get_unit(m, "basic.target")));
245         assert_se(!unit_has_dependency(a, UNIT_ATOM_ON_SUCCESS, manager_get_unit(m, "basic.target")));
246         assert_se(!unit_has_dependency(a, UNIT_ATOM_ON_FAILURE_OF, manager_get_unit(m, "basic.target")));
247         assert_se(!unit_has_dependency(a, UNIT_ATOM_ON_SUCCESS_OF, manager_get_unit(m, "basic.target")));
248         assert_se(!unit_has_dependency(a, UNIT_ATOM_PROPAGATES_RELOAD_TO, manager_get_unit(m, "non-existing-on-failure.target")));
249 
250         assert_se(unit_has_name(a, "a.service"));
251         assert_se(unit_has_name(a, "merged.service"));
252 
253         unsigned mm = 1;
254         Unit *other;
255 
256         UNIT_FOREACH_DEPENDENCY(other, a, UNIT_ATOM_AFTER) {
257                 mm *= unit_has_name(other, SPECIAL_BASIC_TARGET) ? 3 : 1;
258                 mm *= unit_has_name(other, "quux.target") ? 5 : 1;
259                 mm *= unit_has_name(other, SPECIAL_ROOT_SLICE) ? 7 : 1;
260         }
261 
262         UNIT_FOREACH_DEPENDENCY(other, a, UNIT_ATOM_ON_FAILURE)
263                 mm *= unit_has_name(other, "non-existing-on-failure.target") ? 11 : 1;
264 
265         UNIT_FOREACH_DEPENDENCY(other, a, UNIT_ATOM_PULL_IN_START)
266                 mm *= unit_has_name(other, "non-existing.mount") ? 13 : 1;
267 
268         assert_se(mm == 3U*5U*7U*11U*13U);
269 
270         verify_dependency_atoms();
271 
272         /* Test adding multiple Slice= dependencies; only the last should remain */
273         assert_se(unit_new_for_name(m, sizeof(Service), "tomato.service", &tomato) >= 0);
274         assert_se(unit_new_for_name(m, sizeof(Slice), "sauce.slice", &sauce) >= 0);
275         assert_se(unit_new_for_name(m, sizeof(Slice), "fruit.slice", &fruit) >= 0);
276         assert_se(unit_new_for_name(m, sizeof(Slice), "zupa.slice", &zupa) >= 0);
277 
278         unit_set_slice(tomato, sauce);
279         unit_set_slice(tomato, fruit);
280         unit_set_slice(tomato, zupa);
281 
282         assert_se(UNIT_GET_SLICE(tomato) == zupa);
283         assert_se(!unit_has_dependency(tomato, UNIT_ATOM_IN_SLICE, sauce));
284         assert_se(!unit_has_dependency(tomato, UNIT_ATOM_IN_SLICE, fruit));
285         assert_se( unit_has_dependency(tomato, UNIT_ATOM_IN_SLICE, zupa));
286 
287         assert_se(!unit_has_dependency(tomato, UNIT_ATOM_REFERENCES, sauce));
288         assert_se(!unit_has_dependency(tomato, UNIT_ATOM_REFERENCES, fruit));
289         assert_se( unit_has_dependency(tomato, UNIT_ATOM_REFERENCES, zupa));
290 
291         assert_se(!unit_has_dependency(sauce, UNIT_ATOM_SLICE_OF, tomato));
292         assert_se(!unit_has_dependency(fruit, UNIT_ATOM_SLICE_OF, tomato));
293         assert_se( unit_has_dependency(zupa, UNIT_ATOM_SLICE_OF, tomato));
294 
295         assert_se(!unit_has_dependency(sauce, UNIT_ATOM_REFERENCED_BY, tomato));
296         assert_se(!unit_has_dependency(fruit, UNIT_ATOM_REFERENCED_BY, tomato));
297         assert_se( unit_has_dependency(zupa, UNIT_ATOM_REFERENCED_BY, tomato));
298 
299         return 0;
300 }
301