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