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