]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
7c0436b9 | 2 | |
f2b68789 | 3 | #include <fcntl.h> |
cf0fbc49 | 4 | #include <unistd.h> |
f2b68789 | 5 | |
b5efdb8a | 6 | #include "alloc-util.h" |
96aad8d1 | 7 | #include "bus-common-errors.h" |
718db961 | 8 | #include "bus-error.h" |
5cfa33e0 ZJS |
9 | #include "dbus-unit.h" |
10 | #include "strv.h" | |
288a74cc | 11 | #include "terminal-util.h" |
b5efdb8a | 12 | #include "transaction.h" |
75778e21 MS |
13 | |
14 | static void transaction_unlink_job(Transaction *tr, Job *j, bool delete_dependencies); | |
15 | ||
16 | static void transaction_delete_job(Transaction *tr, Job *j, bool delete_dependencies) { | |
17 | assert(tr); | |
18 | assert(j); | |
19 | ||
20 | /* Deletes one job from the transaction */ | |
21 | ||
22 | transaction_unlink_job(tr, j, delete_dependencies); | |
23 | ||
d6a093d0 | 24 | job_free(j); |
75778e21 MS |
25 | } |
26 | ||
27 | static void transaction_delete_unit(Transaction *tr, Unit *u) { | |
28 | Job *j; | |
29 | ||
30 | /* Deletes all jobs associated with a certain unit from the | |
31 | * transaction */ | |
32 | ||
33 | while ((j = hashmap_get(tr->jobs, u))) | |
34 | transaction_delete_job(tr, j, true); | |
35 | } | |
36 | ||
37 | void transaction_abort(Transaction *tr) { | |
38 | Job *j; | |
39 | ||
40 | assert(tr); | |
41 | ||
42 | while ((j = hashmap_first(tr->jobs))) | |
1b9cea0c | 43 | transaction_delete_job(tr, j, false); |
75778e21 MS |
44 | |
45 | assert(hashmap_isempty(tr->jobs)); | |
75778e21 MS |
46 | } |
47 | ||
0d9989aa | 48 | static void transaction_find_jobs_that_matter_to_anchor(Job *j, unsigned generation) { |
03677889 | 49 | assert(j); |
75778e21 | 50 | |
75778e21 MS |
51 | /* A recursive sweep through the graph that marks all units |
52 | * that matter to the anchor job, i.e. are directly or | |
53 | * indirectly a dependency of the anchor job via paths that | |
54 | * are fully marked as mattering. */ | |
55 | ||
0d9989aa MS |
56 | j->matters_to_anchor = true; |
57 | j->generation = generation; | |
75778e21 | 58 | |
0d9989aa | 59 | LIST_FOREACH(subject, l, j->subject_list) { |
75778e21 MS |
60 | |
61 | /* This link does not matter */ | |
62 | if (!l->matters) | |
63 | continue; | |
64 | ||
65 | /* This unit has already been marked */ | |
66 | if (l->object->generation == generation) | |
67 | continue; | |
68 | ||
0d9989aa | 69 | transaction_find_jobs_that_matter_to_anchor(l->object, generation); |
75778e21 MS |
70 | } |
71 | } | |
72 | ||
73 | static void transaction_merge_and_delete_job(Transaction *tr, Job *j, Job *other, JobType t) { | |
03677889 | 74 | JobDependency *last; |
75778e21 MS |
75 | |
76 | assert(j); | |
77 | assert(other); | |
78 | assert(j->unit == other->unit); | |
79 | assert(!j->installed); | |
80 | ||
81 | /* Merges 'other' into 'j' and then deletes 'other'. */ | |
82 | ||
83 | j->type = t; | |
84 | j->state = JOB_WAITING; | |
23ade460 | 85 | j->irreversible = j->irreversible || other->irreversible; |
75778e21 MS |
86 | j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor; |
87 | ||
88 | /* Patch us in as new owner of the JobDependency objects */ | |
89 | last = NULL; | |
90 | LIST_FOREACH(subject, l, other->subject_list) { | |
91 | assert(l->subject == other); | |
92 | l->subject = j; | |
93 | last = l; | |
94 | } | |
95 | ||
96 | /* Merge both lists */ | |
97 | if (last) { | |
98 | last->subject_next = j->subject_list; | |
99 | if (j->subject_list) | |
100 | j->subject_list->subject_prev = last; | |
101 | j->subject_list = other->subject_list; | |
102 | } | |
103 | ||
104 | /* Patch us in as new owner of the JobDependency objects */ | |
105 | last = NULL; | |
106 | LIST_FOREACH(object, l, other->object_list) { | |
107 | assert(l->object == other); | |
108 | l->object = j; | |
109 | last = l; | |
110 | } | |
111 | ||
112 | /* Merge both lists */ | |
113 | if (last) { | |
114 | last->object_next = j->object_list; | |
115 | if (j->object_list) | |
116 | j->object_list->object_prev = last; | |
117 | j->object_list = other->object_list; | |
118 | } | |
119 | ||
120 | /* Kill the other job */ | |
121 | other->subject_list = NULL; | |
122 | other->object_list = NULL; | |
123 | transaction_delete_job(tr, other, true); | |
124 | } | |
125 | ||
44a6b1b6 | 126 | _pure_ static bool job_is_conflicted_by(Job *j) { |
75778e21 MS |
127 | assert(j); |
128 | ||
129 | /* Returns true if this job is pulled in by a least one | |
130 | * ConflictedBy dependency. */ | |
131 | ||
132 | LIST_FOREACH(object, l, j->object_list) | |
133 | if (l->conflicts) | |
134 | return true; | |
135 | ||
136 | return false; | |
137 | } | |
138 | ||
03677889 YW |
139 | static int delete_one_unmergeable_job(Transaction *tr, Job *job) { |
140 | assert(job); | |
75778e21 MS |
141 | |
142 | /* Tries to delete one item in the linked list | |
143 | * j->transaction_next->transaction_next->... that conflicts | |
144 | * with another one, in an attempt to make an inconsistent | |
145 | * transaction work. */ | |
146 | ||
147 | /* We rely here on the fact that if a merged with b does not | |
148 | * merge with c, either a or b merge with c neither */ | |
03677889 | 149 | LIST_FOREACH(transaction, j, job) |
75778e21 MS |
150 | LIST_FOREACH(transaction, k, j->transaction_next) { |
151 | Job *d; | |
152 | ||
153 | /* Is this one mergeable? Then skip it */ | |
154 | if (job_type_is_mergeable(j->type, k->type)) | |
155 | continue; | |
156 | ||
157 | /* Ok, we found two that conflict, let's see if we can | |
158 | * drop one of them */ | |
159 | if (!j->matters_to_anchor && !k->matters_to_anchor) { | |
160 | ||
161 | /* Both jobs don't matter, so let's | |
162 | * find the one that is smarter to | |
163 | * remove. Let's think positive and | |
164 | * rather remove stops then starts -- | |
165 | * except if something is being | |
166 | * stopped because it is conflicted by | |
167 | * another unit in which case we | |
168 | * rather remove the start. */ | |
169 | ||
f2341e0a | 170 | log_unit_debug(j->unit, |
66870f90 ZJS |
171 | "Looking at job %s/%s conflicted_by=%s", |
172 | j->unit->id, job_type_to_string(j->type), | |
173 | yes_no(j->type == JOB_STOP && job_is_conflicted_by(j))); | |
f2341e0a | 174 | log_unit_debug(k->unit, |
66870f90 ZJS |
175 | "Looking at job %s/%s conflicted_by=%s", |
176 | k->unit->id, job_type_to_string(k->type), | |
177 | yes_no(k->type == JOB_STOP && job_is_conflicted_by(k))); | |
75778e21 MS |
178 | |
179 | if (j->type == JOB_STOP) { | |
180 | ||
181 | if (job_is_conflicted_by(j)) | |
182 | d = k; | |
183 | else | |
184 | d = j; | |
185 | ||
186 | } else if (k->type == JOB_STOP) { | |
187 | ||
188 | if (job_is_conflicted_by(k)) | |
189 | d = j; | |
190 | else | |
191 | d = k; | |
192 | } else | |
193 | d = j; | |
194 | ||
195 | } else if (!j->matters_to_anchor) | |
196 | d = j; | |
197 | else if (!k->matters_to_anchor) | |
198 | d = k; | |
199 | else | |
200 | return -ENOEXEC; | |
201 | ||
202 | /* Ok, we can drop one, so let's do so. */ | |
f2341e0a | 203 | log_unit_debug(d->unit, |
75cb8502 ZJS |
204 | "Fixing conflicting jobs %s/%s,%s/%s by deleting job %s/%s", |
205 | j->unit->id, job_type_to_string(j->type), | |
206 | k->unit->id, job_type_to_string(k->type), | |
66870f90 | 207 | d->unit->id, job_type_to_string(d->type)); |
75778e21 MS |
208 | transaction_delete_job(tr, d, true); |
209 | return 0; | |
210 | } | |
211 | ||
212 | return -EINVAL; | |
213 | } | |
214 | ||
718db961 | 215 | static int transaction_merge_jobs(Transaction *tr, sd_bus_error *e) { |
75778e21 | 216 | Job *j; |
75778e21 MS |
217 | int r; |
218 | ||
219 | assert(tr); | |
220 | ||
221 | /* First step, check whether any of the jobs for one specific | |
222 | * task conflict. If so, try to drop one of them. */ | |
90e74a66 | 223 | HASHMAP_FOREACH(j, tr->jobs) { |
75778e21 | 224 | JobType t; |
75778e21 MS |
225 | |
226 | t = j->type; | |
227 | LIST_FOREACH(transaction, k, j->transaction_next) { | |
e0209d83 | 228 | if (job_type_merge_and_collapse(&t, k->type, j->unit) >= 0) |
75778e21 MS |
229 | continue; |
230 | ||
231 | /* OK, we could not merge all jobs for this | |
232 | * action. Let's see if we can get rid of one | |
233 | * of them */ | |
234 | ||
235 | r = delete_one_unmergeable_job(tr, j); | |
236 | if (r >= 0) | |
237 | /* Ok, we managed to drop one, now | |
238 | * let's ask our callers to call us | |
239 | * again after garbage collecting */ | |
240 | return -EAGAIN; | |
241 | ||
242 | /* We couldn't merge anything. Failure */ | |
7358dc02 ZJS |
243 | return sd_bus_error_setf(e, BUS_ERROR_TRANSACTION_JOBS_CONFLICTING, |
244 | "Transaction contains conflicting jobs '%s' and '%s' for %s. " | |
245 | "Probably contradicting requirement dependencies configured.", | |
246 | job_type_to_string(t), | |
247 | job_type_to_string(k->type), | |
248 | k->unit->id); | |
75778e21 MS |
249 | } |
250 | } | |
251 | ||
252 | /* Second step, merge the jobs. */ | |
90e74a66 | 253 | HASHMAP_FOREACH(j, tr->jobs) { |
75778e21 | 254 | JobType t = j->type; |
75778e21 | 255 | |
e0209d83 | 256 | /* Merge all transaction jobs for j->unit */ |
75778e21 | 257 | LIST_FOREACH(transaction, k, j->transaction_next) |
e0209d83 | 258 | assert_se(job_type_merge_and_collapse(&t, k->type, j->unit) == 0); |
75778e21 | 259 | |
03677889 | 260 | Job *k; |
75778e21 | 261 | while ((k = j->transaction_next)) { |
656bbffc | 262 | if (tr->anchor_job == k) { |
75778e21 MS |
263 | transaction_merge_and_delete_job(tr, k, j, t); |
264 | j = k; | |
265 | } else | |
266 | transaction_merge_and_delete_job(tr, j, k, t); | |
267 | } | |
268 | ||
75778e21 MS |
269 | assert(!j->transaction_next); |
270 | assert(!j->transaction_prev); | |
271 | } | |
272 | ||
273 | return 0; | |
274 | } | |
275 | ||
cc479760 | 276 | static void transaction_drop_redundant(Transaction *tr) { |
ca006fc6 | 277 | bool again; |
75778e21 | 278 | |
ca006fc6 LP |
279 | /* Goes through the transaction and removes all jobs of the units whose jobs are all noops. If not |
280 | * all of a unit's jobs are redundant, they are kept. */ | |
75778e21 | 281 | |
055163ad | 282 | assert(tr); |
75778e21 | 283 | |
ca006fc6 | 284 | do { |
ca006fc6 LP |
285 | Job *j; |
286 | ||
287 | again = false; | |
288 | ||
90e74a66 | 289 | HASHMAP_FOREACH(j, tr->jobs) { |
ca006fc6 | 290 | bool keep = false; |
ca006fc6 LP |
291 | |
292 | LIST_FOREACH(transaction, k, j) | |
293 | if (tr->anchor_job == k || | |
cc479760 | 294 | !job_type_is_redundant(k->type, unit_active_state(k->unit)) || |
ca006fc6 LP |
295 | (k->unit->job && job_type_is_conflicting(k->type, k->unit->job->type))) { |
296 | keep = true; | |
297 | break; | |
298 | } | |
299 | ||
300 | if (!keep) { | |
301 | log_trace("Found redundant job %s/%s, dropping from transaction.", | |
302 | j->unit->id, job_type_to_string(j->type)); | |
303 | transaction_delete_job(tr, j, false); | |
304 | again = true; | |
305 | break; | |
306 | } | |
75778e21 | 307 | } |
ca006fc6 | 308 | } while (again); |
75778e21 MS |
309 | } |
310 | ||
03677889 | 311 | _pure_ static bool unit_matters_to_anchor(Unit *u, Job *job) { |
75778e21 | 312 | assert(u); |
03677889 YW |
313 | assert(job); |
314 | assert(!job->transaction_prev); | |
75778e21 MS |
315 | |
316 | /* Checks whether at least one of the jobs for this unit | |
317 | * matters to the anchor. */ | |
318 | ||
03677889 | 319 | LIST_FOREACH(transaction, j, job) |
75778e21 MS |
320 | if (j->matters_to_anchor) |
321 | return true; | |
322 | ||
323 | return false; | |
324 | } | |
325 | ||
366eced4 YW |
326 | static char* merge_unit_ids(const char* unit_log_field, char * const* pairs) { |
327 | _cleanup_free_ char *ans = NULL; | |
328 | size_t size = 0; | |
924775e8 ZJS |
329 | |
330 | STRV_FOREACH_PAIR(unit_id, job_type, pairs) { | |
366eced4 YW |
331 | size_t next; |
332 | ||
333 | if (size > 0) | |
334 | ans[size - 1] = '\n'; | |
335 | ||
924775e8 | 336 | next = strlen(unit_log_field) + strlen(*unit_id); |
319a4f4b | 337 | if (!GREEDY_REALLOC(ans, size + next + 1)) |
366eced4 | 338 | return NULL; |
924775e8 ZJS |
339 | |
340 | sprintf(ans + size, "%s%s", unit_log_field, *unit_id); | |
924775e8 ZJS |
341 | size += next + 1; |
342 | } | |
343 | ||
366eced4 | 344 | return TAKE_PTR(ans); |
924775e8 ZJS |
345 | } |
346 | ||
718db961 | 347 | static int transaction_verify_order_one(Transaction *tr, Job *j, Job *from, unsigned generation, sd_bus_error *e) { |
15ed3c3a LP |
348 | |
349 | static const UnitDependencyAtom directions[] = { | |
350 | UNIT_ATOM_BEFORE, | |
351 | UNIT_ATOM_AFTER, | |
dfd79eca | 352 | }; |
15ed3c3a LP |
353 | |
354 | int r; | |
75778e21 MS |
355 | |
356 | assert(tr); | |
357 | assert(j); | |
358 | assert(!j->transaction_prev); | |
359 | ||
defe63b0 LP |
360 | /* Does a recursive sweep through the ordering graph, looking for a cycle. If we find a cycle we try |
361 | * to break it. */ | |
75778e21 MS |
362 | |
363 | /* Have we seen this before? */ | |
364 | if (j->generation == generation) { | |
924775e8 ZJS |
365 | Job *k, *delete = NULL; |
366 | _cleanup_free_ char **array = NULL, *unit_ids = NULL; | |
75778e21 | 367 | |
defe63b0 LP |
368 | /* If the marker is NULL we have been here already and decided the job was loop-free from |
369 | * here. Hence shortcut things and return right-away. */ | |
75778e21 MS |
370 | if (!j->marker) |
371 | return 0; | |
372 | ||
defe63b0 LP |
373 | /* So, the marker is not NULL and we already have been here. We have a cycle. Let's try to |
374 | * break it. We go backwards in our path and try to find a suitable job to remove. We use the | |
375 | * marker to find our way back, since smart how we are we stored our way back in there. */ | |
75778e21 MS |
376 | for (k = from; k; k = ((k->generation == generation && k->marker != k) ? k->marker : NULL)) { |
377 | ||
924775e8 ZJS |
378 | /* For logging below */ |
379 | if (strv_push_pair(&array, k->unit->id, (char*) job_type_to_string(k->type)) < 0) | |
380 | log_oom(); | |
75778e21 | 381 | |
ece174c5 | 382 | if (!delete && hashmap_get(tr->jobs, k->unit) && !unit_matters_to_anchor(k->unit, k)) |
924775e8 | 383 | /* Ok, we can drop this one, so let's do so. */ |
75778e21 | 384 | delete = k; |
75778e21 | 385 | |
924775e8 | 386 | /* Check if this in fact was the beginning of the cycle */ |
75778e21 MS |
387 | if (k == j) |
388 | break; | |
389 | } | |
390 | ||
924775e8 ZJS |
391 | unit_ids = merge_unit_ids(j->manager->unit_log_field, array); /* ignore error */ |
392 | ||
393 | STRV_FOREACH_PAIR(unit_id, job_type, array) | |
394 | /* logging for j not k here to provide a consistent narrative */ | |
395 | log_struct(LOG_WARNING, | |
f66020ab ZJS |
396 | LOG_UNIT_MESSAGE(j->unit, |
397 | "Found %s on %s/%s", | |
398 | unit_id == array ? "ordering cycle" : "dependency", | |
399 | *unit_id, *job_type), | |
7325a2b2 | 400 | "%s", unit_ids); |
75778e21 MS |
401 | |
402 | if (delete) { | |
dc9b5816 | 403 | const char *status; |
924775e8 ZJS |
404 | /* logging for j not k here to provide a consistent narrative */ |
405 | log_struct(LOG_ERR, | |
f66020ab ZJS |
406 | LOG_UNIT_MESSAGE(j->unit, |
407 | "Job %s/%s deleted to break ordering cycle starting with %s/%s", | |
408 | delete->unit->id, job_type_to_string(delete->type), | |
409 | j->unit->id, job_type_to_string(j->type)), | |
7325a2b2 | 410 | "%s", unit_ids); |
dc9b5816 ZJS |
411 | |
412 | if (log_get_show_color()) | |
413 | status = ANSI_HIGHLIGHT_RED " SKIP " ANSI_NORMAL; | |
414 | else | |
415 | status = " SKIP "; | |
416 | ||
5bcf34eb ZJS |
417 | unit_status_printf(delete->unit, |
418 | STATUS_TYPE_NOTICE, | |
419 | status, | |
04d232d8 ZJS |
420 | "Ordering cycle found, skipping %s", |
421 | unit_status_string(delete->unit, NULL)); | |
75778e21 MS |
422 | transaction_delete_unit(tr, delete->unit); |
423 | return -EAGAIN; | |
424 | } | |
425 | ||
924775e8 | 426 | log_struct(LOG_ERR, |
f66020ab ZJS |
427 | LOG_UNIT_MESSAGE(j->unit, "Unable to break cycle starting with %s/%s", |
428 | j->unit->id, job_type_to_string(j->type)), | |
7325a2b2 | 429 | "%s", unit_ids); |
75778e21 | 430 | |
7358dc02 ZJS |
431 | return sd_bus_error_setf(e, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC, |
432 | "Transaction order is cyclic. See system logs for details."); | |
75778e21 MS |
433 | } |
434 | ||
435 | /* Make the marker point to where we come from, so that we can | |
436 | * find our way backwards if we want to break a cycle. We use | |
437 | * a special marker for the beginning: we point to | |
438 | * ourselves. */ | |
439 | j->marker = from ? from : j; | |
440 | j->generation = generation; | |
441 | ||
dfd79eca MK |
442 | /* Actual ordering of jobs depends on the unit ordering dependency and job types. We need to traverse |
443 | * the graph over 'before' edges in the actual job execution order. We traverse over both unit | |
444 | * ordering dependencies and we test with job_compare() whether it is the 'before' edge in the job | |
445 | * execution ordering. */ | |
15ed3c3a LP |
446 | for (size_t d = 0; d < ELEMENTSOF(directions); d++) { |
447 | Unit *u; | |
448 | ||
449 | UNIT_FOREACH_DEPENDENCY(u, j->unit, directions[d]) { | |
dfd79eca MK |
450 | Job *o; |
451 | ||
452 | /* Is there a job for this unit? */ | |
453 | o = hashmap_get(tr->jobs, u); | |
454 | if (!o) { | |
defe63b0 LP |
455 | /* Ok, there is no job for this in the transaction, but maybe there is |
456 | * already one running? */ | |
dfd79eca MK |
457 | o = u->job; |
458 | if (!o) | |
459 | continue; | |
460 | } | |
461 | ||
462 | /* Cut traversing if the job j is not really *before* o. */ | |
463 | if (job_compare(j, o, directions[d]) >= 0) | |
75778e21 | 464 | continue; |
75778e21 | 465 | |
dfd79eca MK |
466 | r = transaction_verify_order_one(tr, o, j, generation, e); |
467 | if (r < 0) | |
468 | return r; | |
469 | } | |
75778e21 MS |
470 | } |
471 | ||
472 | /* Ok, let's backtrack, and remember that this entry is not on | |
473 | * our path anymore. */ | |
474 | j->marker = NULL; | |
475 | ||
476 | return 0; | |
477 | } | |
478 | ||
718db961 | 479 | static int transaction_verify_order(Transaction *tr, unsigned *generation, sd_bus_error *e) { |
75778e21 MS |
480 | Job *j; |
481 | int r; | |
75778e21 MS |
482 | unsigned g; |
483 | ||
484 | assert(tr); | |
485 | assert(generation); | |
486 | ||
487 | /* Check if the ordering graph is cyclic. If it is, try to fix | |
488 | * that up by dropping one of the jobs. */ | |
489 | ||
490 | g = (*generation)++; | |
491 | ||
90e74a66 | 492 | HASHMAP_FOREACH(j, tr->jobs) { |
3cc2aff1 LP |
493 | r = transaction_verify_order_one(tr, j, NULL, g, e); |
494 | if (r < 0) | |
75778e21 | 495 | return r; |
3cc2aff1 | 496 | } |
75778e21 MS |
497 | |
498 | return 0; | |
499 | } | |
500 | ||
501 | static void transaction_collect_garbage(Transaction *tr) { | |
ca006fc6 | 502 | bool again; |
75778e21 MS |
503 | |
504 | assert(tr); | |
505 | ||
506 | /* Drop jobs that are not required by any other job */ | |
507 | ||
ca006fc6 | 508 | do { |
ca006fc6 LP |
509 | Job *j; |
510 | ||
511 | again = false; | |
512 | ||
90e74a66 | 513 | HASHMAP_FOREACH(j, tr->jobs) { |
ca006fc6 LP |
514 | if (tr->anchor_job == j) |
515 | continue; | |
516 | ||
517 | if (!j->object_list) { | |
518 | log_trace("Garbage collecting job %s/%s", j->unit->id, job_type_to_string(j->type)); | |
519 | transaction_delete_job(tr, j, true); | |
520 | again = true; | |
521 | break; | |
522 | } | |
523 | ||
78218e62 JK |
524 | log_trace("Keeping job %s/%s because of %s/%s", |
525 | j->unit->id, job_type_to_string(j->type), | |
526 | j->object_list->subject ? j->object_list->subject->unit->id : "root", | |
527 | j->object_list->subject ? job_type_to_string(j->object_list->subject->type) : "root"); | |
75778e21 MS |
528 | } |
529 | ||
ca006fc6 | 530 | } while (again); |
75778e21 MS |
531 | } |
532 | ||
718db961 | 533 | static int transaction_is_destructive(Transaction *tr, JobMode mode, sd_bus_error *e) { |
75778e21 MS |
534 | Job *j; |
535 | ||
536 | assert(tr); | |
537 | ||
538 | /* Checks whether applying this transaction means that | |
539 | * existing jobs would be replaced */ | |
540 | ||
90e74a66 | 541 | HASHMAP_FOREACH(j, tr->jobs) { |
75778e21 MS |
542 | |
543 | /* Assume merged */ | |
544 | assert(!j->transaction_prev); | |
545 | assert(!j->transaction_next); | |
546 | ||
23ade460 | 547 | if (j->unit->job && (mode == JOB_FAIL || j->unit->job->irreversible) && |
c21b92ff | 548 | job_type_is_conflicting(j->unit->job->type, j->type)) |
7358dc02 | 549 | return sd_bus_error_setf(e, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE, |
cf99f8ea LP |
550 | "Transaction for %s/%s is destructive (%s has '%s' job queued, but '%s' is included in transaction).", |
551 | tr->anchor_job->unit->id, job_type_to_string(tr->anchor_job->type), | |
552 | j->unit->id, job_type_to_string(j->unit->job->type), job_type_to_string(j->type)); | |
75778e21 MS |
553 | } |
554 | ||
555 | return 0; | |
556 | } | |
557 | ||
558 | static void transaction_minimize_impact(Transaction *tr) { | |
03677889 | 559 | Job *head; |
055163ad | 560 | |
75778e21 MS |
561 | assert(tr); |
562 | ||
563 | /* Drops all unnecessary jobs that reverse already active jobs | |
564 | * or that stop a running service. */ | |
565 | ||
055163ad | 566 | rescan: |
03677889 YW |
567 | HASHMAP_FOREACH(head, tr->jobs) { |
568 | LIST_FOREACH(transaction, j, head) { | |
055163ad | 569 | bool stops_running_service, changes_existing_job; |
75778e21 | 570 | |
055163ad MS |
571 | /* If it matters, we shouldn't drop it */ |
572 | if (j->matters_to_anchor) | |
573 | continue; | |
75778e21 | 574 | |
055163ad MS |
575 | /* Would this stop a running service? |
576 | * Would this change an existing job? | |
577 | * If so, let's drop this entry */ | |
75778e21 | 578 | |
055163ad MS |
579 | stops_running_service = |
580 | j->type == JOB_STOP && UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j->unit)); | |
75778e21 | 581 | |
055163ad MS |
582 | changes_existing_job = |
583 | j->unit->job && | |
584 | job_type_is_conflicting(j->type, j->unit->job->type); | |
75778e21 | 585 | |
055163ad MS |
586 | if (!stops_running_service && !changes_existing_job) |
587 | continue; | |
75778e21 | 588 | |
055163ad | 589 | if (stops_running_service) |
f2341e0a | 590 | log_unit_debug(j->unit, |
66870f90 ZJS |
591 | "%s/%s would stop a running service.", |
592 | j->unit->id, job_type_to_string(j->type)); | |
75778e21 | 593 | |
055163ad | 594 | if (changes_existing_job) |
f2341e0a | 595 | log_unit_debug(j->unit, |
66870f90 ZJS |
596 | "%s/%s would change existing job.", |
597 | j->unit->id, job_type_to_string(j->type)); | |
75778e21 | 598 | |
055163ad | 599 | /* Ok, let's get rid of this */ |
f2341e0a | 600 | log_unit_debug(j->unit, |
66870f90 ZJS |
601 | "Deleting %s/%s to minimize impact.", |
602 | j->unit->id, job_type_to_string(j->type)); | |
75778e21 | 603 | |
055163ad MS |
604 | transaction_delete_job(tr, j, true); |
605 | goto rescan; | |
75778e21 | 606 | } |
055163ad | 607 | } |
75778e21 MS |
608 | } |
609 | ||
50cbaba4 LP |
610 | static int transaction_apply( |
611 | Transaction *tr, | |
612 | Manager *m, | |
613 | JobMode mode, | |
614 | Set *affected_jobs) { | |
615 | ||
75778e21 MS |
616 | Job *j; |
617 | int r; | |
618 | ||
619 | /* Moves the transaction jobs to the set of active jobs */ | |
620 | ||
3742095b | 621 | if (IN_SET(mode, JOB_ISOLATE, JOB_FLUSH)) { |
75778e21 MS |
622 | |
623 | /* When isolating first kill all installed jobs which | |
624 | * aren't part of the new transaction */ | |
90e74a66 | 625 | HASHMAP_FOREACH(j, m->jobs) { |
75778e21 MS |
626 | assert(j->installed); |
627 | ||
2de0b9e9 MO |
628 | if (j->unit->ignore_on_isolate) |
629 | continue; | |
630 | ||
75778e21 MS |
631 | if (hashmap_get(tr->jobs, j->unit)) |
632 | continue; | |
633 | ||
5273510e MS |
634 | /* Not invalidating recursively. Avoids triggering |
635 | * OnFailure= actions of dependent jobs. Also avoids | |
636 | * invalidating our iterator. */ | |
833f92ad | 637 | job_finish_and_invalidate(j, JOB_CANCELED, false, false); |
75778e21 MS |
638 | } |
639 | } | |
640 | ||
90e74a66 | 641 | HASHMAP_FOREACH(j, tr->jobs) { |
75778e21 MS |
642 | /* Assume merged */ |
643 | assert(!j->transaction_prev); | |
644 | assert(!j->transaction_next); | |
645 | ||
acf56b72 | 646 | r = hashmap_ensure_put(&m->jobs, NULL, UINT32_TO_PTR(j->id), j); |
75778e21 MS |
647 | if (r < 0) |
648 | goto rollback; | |
649 | } | |
650 | ||
651 | while ((j = hashmap_steal_first(tr->jobs))) { | |
656bbffc MS |
652 | Job *installed_job; |
653 | ||
75778e21 MS |
654 | /* Clean the job dependencies */ |
655 | transaction_unlink_job(tr, j, false); | |
656 | ||
656bbffc MS |
657 | installed_job = job_install(j); |
658 | if (installed_job != j) { | |
659 | /* j has been merged into a previously installed job */ | |
660 | if (tr->anchor_job == j) | |
661 | tr->anchor_job = installed_job; | |
662 | hashmap_remove(m->jobs, UINT32_TO_PTR(j->id)); | |
663 | job_free(j); | |
664 | j = installed_job; | |
665 | } | |
05d576f1 | 666 | |
75778e21 MS |
667 | job_add_to_run_queue(j); |
668 | job_add_to_dbus_queue(j); | |
a2df3ea4 | 669 | job_start_timer(j, false); |
c65eb836 | 670 | job_shutdown_magic(j); |
50cbaba4 LP |
671 | |
672 | /* When 'affected' is specified, let's track all in it all jobs that were touched because of | |
673 | * this transaction. */ | |
674 | if (affected_jobs) | |
675 | (void) set_put(affected_jobs, j); | |
75778e21 MS |
676 | } |
677 | ||
75778e21 MS |
678 | return 0; |
679 | ||
680 | rollback: | |
681 | ||
90e74a66 | 682 | HASHMAP_FOREACH(j, tr->jobs) |
75778e21 | 683 | hashmap_remove(m->jobs, UINT32_TO_PTR(j->id)); |
75778e21 MS |
684 | |
685 | return r; | |
686 | } | |
687 | ||
50cbaba4 LP |
688 | int transaction_activate( |
689 | Transaction *tr, | |
690 | Manager *m, | |
691 | JobMode mode, | |
692 | Set *affected_jobs, | |
693 | sd_bus_error *e) { | |
694 | ||
4e7bd268 | 695 | Job *j; |
75778e21 MS |
696 | int r; |
697 | unsigned generation = 1; | |
698 | ||
699 | assert(tr); | |
700 | ||
701 | /* This applies the changes recorded in tr->jobs to | |
702 | * the actual list of jobs, if possible. */ | |
703 | ||
4e7bd268 MS |
704 | /* Reset the generation counter of all installed jobs. The detection of cycles |
705 | * looks at installed jobs. If they had a non-zero generation from some previous | |
706 | * walk of the graph, the algorithm would break. */ | |
90e74a66 | 707 | HASHMAP_FOREACH(j, m->jobs) |
4e7bd268 MS |
708 | j->generation = 0; |
709 | ||
75778e21 | 710 | /* First step: figure out which jobs matter */ |
0d9989aa | 711 | transaction_find_jobs_that_matter_to_anchor(tr->anchor_job, generation++); |
75778e21 MS |
712 | |
713 | /* Second step: Try not to stop any running services if | |
714 | * we don't have to. Don't try to reverse running | |
715 | * jobs if we don't have to. */ | |
716 | if (mode == JOB_FAIL) | |
717 | transaction_minimize_impact(tr); | |
718 | ||
719 | /* Third step: Drop redundant jobs */ | |
cc479760 | 720 | transaction_drop_redundant(tr); |
75778e21 MS |
721 | |
722 | for (;;) { | |
723 | /* Fourth step: Let's remove unneeded jobs that might | |
724 | * be lurking. */ | |
725 | if (mode != JOB_ISOLATE) | |
726 | transaction_collect_garbage(tr); | |
727 | ||
728 | /* Fifth step: verify order makes sense and correct | |
729 | * cycles if necessary and possible */ | |
730 | r = transaction_verify_order(tr, &generation, e); | |
731 | if (r >= 0) | |
732 | break; | |
733 | ||
4ae25393 YW |
734 | if (r != -EAGAIN) |
735 | return log_warning_errno(r, "Requested transaction contains an unfixable cyclic ordering dependency: %s", bus_error_message(e, r)); | |
75778e21 MS |
736 | |
737 | /* Let's see if the resulting transaction ordering | |
738 | * graph is still cyclic... */ | |
739 | } | |
740 | ||
741 | for (;;) { | |
742 | /* Sixth step: let's drop unmergeable entries if | |
743 | * necessary and possible, merge entries we can | |
744 | * merge */ | |
745 | r = transaction_merge_jobs(tr, e); | |
746 | if (r >= 0) | |
747 | break; | |
748 | ||
4ae25393 YW |
749 | if (r != -EAGAIN) |
750 | return log_warning_errno(r, "Requested transaction contains unmergeable jobs: %s", bus_error_message(e, r)); | |
75778e21 MS |
751 | |
752 | /* Seventh step: an entry got dropped, let's garbage | |
753 | * collect its dependencies. */ | |
754 | if (mode != JOB_ISOLATE) | |
755 | transaction_collect_garbage(tr); | |
756 | ||
757 | /* Let's see if the resulting transaction still has | |
758 | * unmergeable entries ... */ | |
759 | } | |
760 | ||
761 | /* Eights step: Drop redundant jobs again, if the merging now allows us to drop more. */ | |
cc479760 | 762 | transaction_drop_redundant(tr); |
75778e21 MS |
763 | |
764 | /* Ninth step: check whether we can actually apply this */ | |
23ade460 | 765 | r = transaction_is_destructive(tr, mode, e); |
4ae25393 YW |
766 | if (r < 0) |
767 | return log_notice_errno(r, "Requested transaction contradicts existing jobs: %s", bus_error_message(e, r)); | |
75778e21 MS |
768 | |
769 | /* Tenth step: apply changes */ | |
50cbaba4 | 770 | r = transaction_apply(tr, m, mode, affected_jobs); |
23bbb0de MS |
771 | if (r < 0) |
772 | return log_warning_errno(r, "Failed to apply transaction: %m"); | |
75778e21 MS |
773 | |
774 | assert(hashmap_isempty(tr->jobs)); | |
75778e21 | 775 | |
f2b68789 LP |
776 | if (!hashmap_isempty(m->jobs)) { |
777 | /* Are there any jobs now? Then make sure we have the | |
778 | * idle pipe around. We don't really care too much | |
779 | * whether this works or not, as the idle pipe is a | |
780 | * feature for cosmetics, not actually useful for | |
781 | * anything beyond that. */ | |
782 | ||
31a7eb86 ZJS |
783 | if (m->idle_pipe[0] < 0 && m->idle_pipe[1] < 0 && |
784 | m->idle_pipe[2] < 0 && m->idle_pipe[3] < 0) { | |
1afaa7e8 LP |
785 | (void) pipe2(m->idle_pipe, O_NONBLOCK|O_CLOEXEC); |
786 | (void) pipe2(m->idle_pipe + 2, O_NONBLOCK|O_CLOEXEC); | |
31a7eb86 | 787 | } |
f2b68789 LP |
788 | } |
789 | ||
75778e21 MS |
790 | return 0; |
791 | } | |
792 | ||
4bd29fe5 | 793 | static Job* transaction_add_one_job(Transaction *tr, JobType type, Unit *unit, bool *is_new) { |
75778e21 MS |
794 | Job *j, *f; |
795 | ||
796 | assert(tr); | |
797 | assert(unit); | |
798 | ||
799 | /* Looks for an existing prospective job and returns that. If | |
800 | * it doesn't exist it is created and added to the prospective | |
801 | * jobs list. */ | |
802 | ||
803 | f = hashmap_get(tr->jobs, unit); | |
804 | ||
03677889 YW |
805 | LIST_FOREACH(transaction, i, f) { |
806 | assert(i->unit == unit); | |
75778e21 | 807 | |
03677889 | 808 | if (i->type == type) { |
75778e21 MS |
809 | if (is_new) |
810 | *is_new = false; | |
03677889 | 811 | return i; |
75778e21 MS |
812 | } |
813 | } | |
814 | ||
3c956cfe MS |
815 | j = job_new(unit, type); |
816 | if (!j) | |
817 | return NULL; | |
75778e21 MS |
818 | |
819 | j->generation = 0; | |
820 | j->marker = NULL; | |
821 | j->matters_to_anchor = false; | |
23ade460 | 822 | j->irreversible = tr->irreversible; |
75778e21 | 823 | |
71fda00f | 824 | LIST_PREPEND(transaction, f, j); |
75778e21 MS |
825 | |
826 | if (hashmap_replace(tr->jobs, unit, f) < 0) { | |
71fda00f | 827 | LIST_REMOVE(transaction, f, j); |
75778e21 MS |
828 | job_free(j); |
829 | return NULL; | |
830 | } | |
831 | ||
832 | if (is_new) | |
833 | *is_new = true; | |
834 | ||
78218e62 | 835 | log_trace("Added job %s/%s to transaction.", unit->id, job_type_to_string(type)); |
75778e21 MS |
836 | |
837 | return j; | |
838 | } | |
839 | ||
840 | static void transaction_unlink_job(Transaction *tr, Job *j, bool delete_dependencies) { | |
841 | assert(tr); | |
842 | assert(j); | |
843 | ||
844 | if (j->transaction_prev) | |
845 | j->transaction_prev->transaction_next = j->transaction_next; | |
846 | else if (j->transaction_next) | |
847 | hashmap_replace(tr->jobs, j->unit, j->transaction_next); | |
848 | else | |
849 | hashmap_remove_value(tr->jobs, j->unit, j); | |
850 | ||
851 | if (j->transaction_next) | |
852 | j->transaction_next->transaction_prev = j->transaction_prev; | |
853 | ||
854 | j->transaction_prev = j->transaction_next = NULL; | |
855 | ||
856 | while (j->subject_list) | |
e6eda1f2 | 857 | job_dependency_free(j->subject_list); |
75778e21 MS |
858 | |
859 | while (j->object_list) { | |
860 | Job *other = j->object_list->matters ? j->object_list->subject : NULL; | |
861 | ||
e6eda1f2 | 862 | job_dependency_free(j->object_list); |
75778e21 MS |
863 | |
864 | if (other && delete_dependencies) { | |
f2341e0a | 865 | log_unit_debug(other->unit, |
66870f90 ZJS |
866 | "Deleting job %s/%s as dependency of job %s/%s", |
867 | other->unit->id, job_type_to_string(other->type), | |
868 | j->unit->id, job_type_to_string(j->type)); | |
75778e21 MS |
869 | transaction_delete_job(tr, other, delete_dependencies); |
870 | } | |
871 | } | |
872 | } | |
873 | ||
15d167f8 | 874 | void transaction_add_propagate_reload_jobs(Transaction *tr, Unit *unit, Job *by, bool ignore_order, sd_bus_error *e) { |
15d167f8 | 875 | JobType nt; |
eef85c4a | 876 | Unit *dep; |
15d167f8 JW |
877 | int r; |
878 | ||
879 | assert(tr); | |
880 | assert(unit); | |
881 | ||
15ed3c3a | 882 | UNIT_FOREACH_DEPENDENCY(dep, unit, UNIT_ATOM_PROPAGATES_RELOAD_TO) { |
15d167f8 JW |
883 | nt = job_type_collapse(JOB_TRY_RELOAD, dep); |
884 | if (nt == JOB_NOP) | |
885 | continue; | |
886 | ||
887 | r = transaction_add_job_and_dependencies(tr, nt, dep, by, false, false, false, ignore_order, e); | |
888 | if (r < 0) { | |
889 | log_unit_warning(dep, | |
890 | "Cannot add dependency reload job, ignoring: %s", | |
891 | bus_error_message(e, r)); | |
892 | sd_bus_error_free(e); | |
893 | } | |
894 | } | |
895 | } | |
896 | ||
75778e21 MS |
897 | int transaction_add_job_and_dependencies( |
898 | Transaction *tr, | |
899 | JobType type, | |
900 | Unit *unit, | |
901 | Job *by, | |
902 | bool matters, | |
75778e21 MS |
903 | bool conflicts, |
904 | bool ignore_requirements, | |
905 | bool ignore_order, | |
718db961 | 906 | sd_bus_error *e) { |
eef85c4a LP |
907 | |
908 | bool is_new; | |
75778e21 | 909 | Unit *dep; |
eef85c4a | 910 | Job *ret; |
75778e21 | 911 | int r; |
75778e21 MS |
912 | |
913 | assert(tr); | |
914 | assert(type < _JOB_TYPE_MAX); | |
e0209d83 | 915 | assert(type < _JOB_TYPE_MAX_IN_TRANSACTION); |
75778e21 MS |
916 | assert(unit); |
917 | ||
43706330 IS |
918 | /* Before adding jobs for this unit, let's ensure that its state has been loaded |
919 | * This matters when jobs are spawned as part of coldplugging itself (see e. g. path_coldplug()). | |
920 | * This way, we "recursively" coldplug units, ensuring that we do not look at state of | |
921 | * not-yet-coldplugged units. */ | |
2c289ea8 | 922 | if (MANAGER_IS_RELOADING(unit->manager)) |
43706330 IS |
923 | unit_coldplug(unit); |
924 | ||
78218e62 JK |
925 | if (by) |
926 | log_trace("Pulling in %s/%s from %s/%s", unit->id, job_type_to_string(type), by->unit->id, job_type_to_string(by->type)); | |
75778e21 | 927 | |
c4555ad8 LP |
928 | /* Safety check that the unit is a valid state, i.e. not in UNIT_STUB or UNIT_MERGED which should only be set |
929 | * temporarily. */ | |
0377cd29 | 930 | if (!UNIT_IS_LOAD_COMPLETE(unit->load_state)) |
c6497ccb | 931 | return sd_bus_error_setf(e, BUS_ERROR_LOAD_FAILED, "Unit %s is not loaded properly.", unit->id); |
75778e21 | 932 | |
ee87525c | 933 | if (type != JOB_STOP) { |
e49da001 | 934 | r = bus_unit_validate_load_state(unit, e); |
cda66772 LB |
935 | /* The time-based cache allows to start new units without daemon-reload, |
936 | * but if they are already referenced (because of dependencies or ordering) | |
937 | * then we have to force a load of the fragment. As an optimization, check | |
938 | * first if anything in the usual paths was modified since the last time | |
939 | * the cache was loaded. Also check if the last time an attempt to load the | |
940 | * unit was made was before the most recent cache refresh, so that we know | |
81be2388 | 941 | * we need to try again — even if the cache is current, it might have been |
cda66772 LB |
942 | * updated in a different context before we had a chance to retry loading |
943 | * this particular unit. | |
81be2388 | 944 | * |
cda66772 LB |
945 | * Given building up the transaction is a synchronous operation, attempt |
946 | * to load the unit immediately. */ | |
81be2388 | 947 | if (r < 0 && manager_unit_cache_should_retry_load(unit)) { |
94efaa31 | 948 | sd_bus_error_free(e); |
cda66772 LB |
949 | unit->load_state = UNIT_STUB; |
950 | r = unit_load(unit); | |
951 | if (r < 0 || unit->load_state == UNIT_STUB) | |
952 | unit->load_state = UNIT_NOT_FOUND; | |
953 | r = bus_unit_validate_load_state(unit, e); | |
954 | } | |
ee87525c FB |
955 | if (r < 0) |
956 | return r; | |
75778e21 MS |
957 | } |
958 | ||
7358dc02 ZJS |
959 | if (!unit_job_is_applicable(unit, type)) |
960 | return sd_bus_error_setf(e, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE, | |
961 | "Job type %s is not applicable for unit %s.", | |
962 | job_type_to_string(type), unit->id); | |
75778e21 | 963 | |
75778e21 | 964 | /* First add the job. */ |
4bd29fe5 | 965 | ret = transaction_add_one_job(tr, type, unit, &is_new); |
75778e21 MS |
966 | if (!ret) |
967 | return -ENOMEM; | |
968 | ||
969 | ret->ignore_order = ret->ignore_order || ignore_order; | |
970 | ||
971 | /* Then, add a link to the job. */ | |
e6eda1f2 MS |
972 | if (by) { |
973 | if (!job_dependency_new(by, ret, matters, conflicts)) | |
974 | return -ENOMEM; | |
975 | } else { | |
976 | /* If the job has no parent job, it is the anchor job. */ | |
4483f694 MS |
977 | assert(!tr->anchor_job); |
978 | tr->anchor_job = ret; | |
b94fbd30 | 979 | } |
e0209d83 MS |
980 | |
981 | if (is_new && !ignore_requirements && type != JOB_NOP) { | |
fd55b2e5 | 982 | _cleanup_set_free_ Set *following = NULL; |
75778e21 MS |
983 | |
984 | /* If we are following some other unit, make sure we | |
985 | * add all dependencies of everybody following. */ | |
fd55b2e5 | 986 | if (unit_following_set(ret->unit, &following) > 0) |
90e74a66 | 987 | SET_FOREACH(dep, following) { |
4bd29fe5 | 988 | r = transaction_add_job_and_dependencies(tr, type, dep, ret, false, false, false, ignore_order, e); |
75778e21 | 989 | if (r < 0) { |
8ed6f81b YW |
990 | log_unit_full_errno(dep, r == -ERFKILL ? LOG_INFO : LOG_WARNING, r, |
991 | "Cannot add dependency job, ignoring: %s", | |
992 | bus_error_message(e, r)); | |
69301c17 | 993 | sd_bus_error_free(e); |
75778e21 MS |
994 | } |
995 | } | |
996 | ||
75778e21 | 997 | /* Finally, recursively add in all dependencies. */ |
3742095b | 998 | if (IN_SET(type, JOB_START, JOB_RESTART)) { |
15ed3c3a | 999 | UNIT_FOREACH_DEPENDENCY(dep, ret->unit, UNIT_ATOM_PULL_IN_START) { |
4bd29fe5 | 1000 | r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, true, false, false, ignore_order, e); |
75778e21 | 1001 | if (r < 0) { |
114400df | 1002 | if (r != -EBADR) /* job type not applicable */ |
65a2493e | 1003 | return r; |
75778e21 | 1004 | |
69301c17 | 1005 | sd_bus_error_free(e); |
75778e21 MS |
1006 | } |
1007 | } | |
1008 | ||
15ed3c3a | 1009 | UNIT_FOREACH_DEPENDENCY(dep, ret->unit, UNIT_ATOM_PULL_IN_START_IGNORED) { |
4bd29fe5 | 1010 | r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, false, false, false, ignore_order, e); |
75778e21 | 1011 | if (r < 0) { |
114400df | 1012 | /* unit masked, job type not applicable and unit not found are not considered as errors. */ |
8ed6f81b YW |
1013 | log_unit_full_errno(dep, |
1014 | IN_SET(r, -ERFKILL, -EBADR, -ENOENT) ? LOG_DEBUG : LOG_WARNING, | |
1015 | r, "Cannot add dependency job, ignoring: %s", | |
1016 | bus_error_message(e, r)); | |
69301c17 | 1017 | sd_bus_error_free(e); |
75778e21 MS |
1018 | } |
1019 | } | |
1020 | ||
15ed3c3a | 1021 | UNIT_FOREACH_DEPENDENCY(dep, ret->unit, UNIT_ATOM_PULL_IN_VERIFY) { |
4bd29fe5 | 1022 | r = transaction_add_job_and_dependencies(tr, JOB_VERIFY_ACTIVE, dep, ret, true, false, false, ignore_order, e); |
75778e21 | 1023 | if (r < 0) { |
114400df | 1024 | if (r != -EBADR) /* job type not applicable */ |
65a2493e | 1025 | return r; |
75778e21 | 1026 | |
69301c17 | 1027 | sd_bus_error_free(e); |
75778e21 MS |
1028 | } |
1029 | } | |
1030 | ||
15ed3c3a | 1031 | UNIT_FOREACH_DEPENDENCY(dep, ret->unit, UNIT_ATOM_PULL_IN_STOP) { |
4bd29fe5 | 1032 | r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, true, true, false, ignore_order, e); |
75778e21 | 1033 | if (r < 0) { |
114400df | 1034 | if (r != -EBADR) /* job type not applicable */ |
65a2493e | 1035 | return r; |
75778e21 | 1036 | |
69301c17 | 1037 | sd_bus_error_free(e); |
75778e21 MS |
1038 | } |
1039 | } | |
1040 | ||
15ed3c3a | 1041 | UNIT_FOREACH_DEPENDENCY(dep, ret->unit, UNIT_ATOM_PULL_IN_STOP_IGNORED) { |
4bd29fe5 | 1042 | r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, false, false, false, ignore_order, e); |
75778e21 | 1043 | if (r < 0) { |
f2341e0a LP |
1044 | log_unit_warning(dep, |
1045 | "Cannot add dependency job, ignoring: %s", | |
1046 | bus_error_message(e, r)); | |
69301c17 | 1047 | sd_bus_error_free(e); |
75778e21 MS |
1048 | } |
1049 | } | |
75778e21 MS |
1050 | } |
1051 | ||
3742095b | 1052 | if (IN_SET(type, JOB_STOP, JOB_RESTART)) { |
017a7ba4 | 1053 | _cleanup_set_free_ Set *propagated_restart = NULL; |
15ed3c3a | 1054 | /* We propagate STOP as STOP, but RESTART only as TRY_RESTART, in order not to start |
c6497ccb | 1055 | * dependencies that are not around. */ |
c6497ccb | 1056 | |
017a7ba4 MY |
1057 | if (type == JOB_RESTART) |
1058 | UNIT_FOREACH_DEPENDENCY(dep, ret->unit, UNIT_ATOM_PROPAGATE_RESTART) { | |
1059 | JobType nt; | |
1060 | ||
1061 | r = set_ensure_put(&propagated_restart, NULL, dep); | |
1062 | if (r < 0) | |
1063 | return r; | |
1064 | ||
1065 | nt = job_type_collapse(JOB_TRY_RESTART, dep); | |
1066 | if (nt == JOB_NOP) | |
1067 | continue; | |
1068 | ||
1069 | r = transaction_add_job_and_dependencies(tr, nt, dep, ret, true, false, false, ignore_order, e); | |
1070 | if (r < 0) { | |
1071 | if (r != -EBADR) /* job type not applicable */ | |
1072 | return r; | |
1073 | ||
1074 | sd_bus_error_free(e); | |
1075 | } | |
1076 | } | |
85e9a101 | 1077 | |
017a7ba4 MY |
1078 | /* The 'stop' part of a restart job is also propagated to |
1079 | * units with UNIT_ATOM_PROPAGATE_STOP */ | |
1080 | UNIT_FOREACH_DEPENDENCY(dep, ret->unit, UNIT_ATOM_PROPAGATE_STOP) { | |
1081 | /* Units experienced restart propagation are skipped */ | |
1082 | if (set_contains(propagated_restart, dep)) | |
15ed3c3a | 1083 | continue; |
48894cd0 | 1084 | |
017a7ba4 | 1085 | r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, true, false, false, ignore_order, e); |
15ed3c3a LP |
1086 | if (r < 0) { |
1087 | if (r != -EBADR) /* job type not applicable */ | |
65a2493e | 1088 | return r; |
85e9a101 | 1089 | |
15ed3c3a | 1090 | sd_bus_error_free(e); |
85e9a101 | 1091 | } |
15ed3c3a | 1092 | } |
75778e21 MS |
1093 | } |
1094 | ||
15d167f8 JW |
1095 | if (type == JOB_RELOAD) |
1096 | transaction_add_propagate_reload_jobs(tr, ret->unit, ret, ignore_order, e); | |
75778e21 | 1097 | |
78218e62 | 1098 | /* JOB_VERIFY_ACTIVE requires no dependency handling */ |
75778e21 MS |
1099 | } |
1100 | ||
75778e21 | 1101 | return 0; |
75778e21 MS |
1102 | } |
1103 | ||
32d6707d LP |
1104 | static bool shall_stop_on_isolate(Transaction *tr, Unit *u) { |
1105 | assert(tr); | |
1106 | assert(u); | |
1107 | ||
1108 | if (u->ignore_on_isolate) | |
1109 | return false; | |
1110 | ||
1111 | /* Is there already something listed for this? */ | |
1112 | if (hashmap_get(tr->jobs, u)) | |
1113 | return false; | |
1114 | ||
1115 | return true; | |
1116 | } | |
1117 | ||
75778e21 | 1118 | int transaction_add_isolate_jobs(Transaction *tr, Manager *m) { |
75778e21 MS |
1119 | Unit *u; |
1120 | char *k; | |
1121 | int r; | |
1122 | ||
1123 | assert(tr); | |
1124 | assert(m); | |
1125 | ||
90e74a66 | 1126 | HASHMAP_FOREACH_KEY(u, k, m->units) { |
32d6707d | 1127 | Unit *o; |
75778e21 | 1128 | |
32d6707d | 1129 | /* Ignore aliases */ |
75778e21 MS |
1130 | if (u->id != k) |
1131 | continue; | |
1132 | ||
32d6707d LP |
1133 | /* No need to stop inactive units */ |
1134 | if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)) && !u->job) | |
75778e21 MS |
1135 | continue; |
1136 | ||
32d6707d | 1137 | if (!shall_stop_on_isolate(tr, u)) |
75778e21 MS |
1138 | continue; |
1139 | ||
32d6707d LP |
1140 | /* Keep units that are triggered by units we want to keep around. */ |
1141 | bool keep = false; | |
1142 | UNIT_FOREACH_DEPENDENCY(o, u, UNIT_ATOM_TRIGGERED_BY) | |
1143 | if (!shall_stop_on_isolate(tr, o)) { | |
1144 | keep = true; | |
1145 | break; | |
1146 | } | |
1147 | if (keep) | |
75778e21 MS |
1148 | continue; |
1149 | ||
4bd29fe5 | 1150 | r = transaction_add_job_and_dependencies(tr, JOB_STOP, u, tr->anchor_job, true, false, false, false, NULL); |
75778e21 | 1151 | if (r < 0) |
f2341e0a | 1152 | log_unit_warning_errno(u, r, "Cannot add isolate job, ignoring: %m"); |
75778e21 MS |
1153 | } |
1154 | ||
1155 | return 0; | |
1156 | } | |
1157 | ||
1f0f9f21 | 1158 | int transaction_add_triggering_jobs(Transaction *tr, Unit *u) { |
1f0f9f21 KK |
1159 | Unit *trigger; |
1160 | int r; | |
1161 | ||
1162 | assert(tr); | |
132e0b53 | 1163 | assert(u); |
1f0f9f21 | 1164 | |
15ed3c3a LP |
1165 | UNIT_FOREACH_DEPENDENCY(trigger, u, UNIT_ATOM_TRIGGERED_BY) { |
1166 | ||
1f0f9f21 KK |
1167 | /* No need to stop inactive jobs */ |
1168 | if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(trigger)) && !trigger->job) | |
1169 | continue; | |
1170 | ||
1171 | /* Is there already something listed for this? */ | |
1172 | if (hashmap_get(tr->jobs, trigger)) | |
1173 | continue; | |
1174 | ||
1175 | r = transaction_add_job_and_dependencies(tr, JOB_STOP, trigger, tr->anchor_job, true, false, false, false, NULL); | |
1176 | if (r < 0) | |
1177 | log_unit_warning_errno(u, r, "Cannot add triggered by job, ignoring: %m"); | |
1178 | } | |
1179 | ||
1180 | return 0; | |
1181 | } | |
1182 | ||
23ade460 | 1183 | Transaction *transaction_new(bool irreversible) { |
75778e21 MS |
1184 | Transaction *tr; |
1185 | ||
1186 | tr = new0(Transaction, 1); | |
1187 | if (!tr) | |
1188 | return NULL; | |
1189 | ||
d5099efc | 1190 | tr->jobs = hashmap_new(NULL); |
6b430fdb ZJS |
1191 | if (!tr->jobs) |
1192 | return mfree(tr); | |
75778e21 | 1193 | |
23ade460 MS |
1194 | tr->irreversible = irreversible; |
1195 | ||
75778e21 MS |
1196 | return tr; |
1197 | } | |
1198 | ||
1199 | void transaction_free(Transaction *tr) { | |
1200 | assert(hashmap_isempty(tr->jobs)); | |
1201 | hashmap_free(tr->jobs); | |
1202 | free(tr); | |
1203 | } |