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