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