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