]>
Commit | Line | Data |
---|---|---|
60918275 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
3 | #include <assert.h> | |
4 | #include <errno.h> | |
87d1515d | 5 | #include <string.h> |
60918275 LP |
6 | |
7 | #include "manager.h" | |
8 | #include "hashmap.h" | |
9 | #include "macro.h" | |
10 | #include "strv.h" | |
16354eff | 11 | #include "log.h" |
60918275 LP |
12 | |
13 | Manager* manager_new(void) { | |
14 | Manager *m; | |
15 | ||
16 | if (!(m = new0(Manager, 1))) | |
17 | return NULL; | |
18 | ||
19 | if (!(m->names = hashmap_new(string_hash_func, string_compare_func))) | |
20 | goto fail; | |
21 | ||
22 | if (!(m->jobs = hashmap_new(trivial_hash_func, trivial_compare_func))) | |
23 | goto fail; | |
24 | ||
e5b5ae50 | 25 | if (!(m->transaction_jobs = hashmap_new(trivial_hash_func, trivial_compare_func))) |
60918275 LP |
26 | goto fail; |
27 | ||
28 | return m; | |
29 | ||
30 | fail: | |
31 | manager_free(m); | |
32 | return NULL; | |
33 | } | |
34 | ||
35 | void manager_free(Manager *m) { | |
36 | Name *n; | |
e5b5ae50 | 37 | Job *j; |
60918275 LP |
38 | |
39 | assert(m); | |
40 | ||
41 | while ((n = hashmap_first(m->names))) | |
42 | name_free(n); | |
43 | ||
e5b5ae50 LP |
44 | while ((j = hashmap_steal_first(m->transaction_jobs))) |
45 | job_free(j); | |
46 | ||
60918275 LP |
47 | hashmap_free(m->names); |
48 | hashmap_free(m->jobs); | |
e5b5ae50 | 49 | hashmap_free(m->transaction_jobs); |
60918275 LP |
50 | |
51 | free(m); | |
52 | } | |
53 | ||
302d0040 LP |
54 | static void transaction_delete_job(Manager *m, Job *j) { |
55 | assert(m); | |
56 | assert(j); | |
57 | ||
1ffba6fe LP |
58 | /* Deletes one job from the transaction */ |
59 | ||
302d0040 LP |
60 | manager_transaction_unlink_job(m, j); |
61 | ||
62 | if (!j->linked) | |
63 | job_free(j); | |
64 | } | |
65 | ||
1ffba6fe LP |
66 | static void transaction_delete_name(Manager *m, Name *n) { |
67 | Job *j; | |
68 | ||
69 | /* Deletes all jobs associated with a certain name from the | |
70 | * transaction */ | |
71 | ||
72 | while ((j = hashmap_get(m->transaction_jobs, n))) | |
73 | transaction_delete_job(m, j); | |
74 | } | |
75 | ||
11dd41ce LP |
76 | static void transaction_abort(Manager *m) { |
77 | Job *j; | |
78 | ||
79 | assert(m); | |
11dd41ce | 80 | |
e5b5ae50 LP |
81 | while ((j = hashmap_first(m->transaction_jobs))) |
82 | if (j->linked) | |
302d0040 | 83 | transaction_delete_job(m, j); |
e5b5ae50 LP |
84 | else |
85 | job_free(j); | |
86 | ||
87 | assert(hashmap_isempty(m->transaction_jobs)); | |
88 | assert(!m->transaction_anchor); | |
89 | } | |
90 | ||
91 | static void transaction_find_jobs_that_matter_to_anchor(Manager *m, Job *j, unsigned generation) { | |
92 | JobDependency *l; | |
93 | ||
94 | assert(m); | |
95 | ||
1ffba6fe LP |
96 | /* A recursive sweep through the graph that marks all names |
97 | * that matter to the anchor job, i.e. are directly or | |
98 | * indirectly a dependency of the anchor job via paths that | |
99 | * are fully marked as mattering. */ | |
100 | ||
e5b5ae50 LP |
101 | for (l = j ? j->subject_list : m->transaction_anchor; l; l = l->subject_next) { |
102 | ||
103 | /* This link does not matter */ | |
104 | if (!l->matters) | |
105 | continue; | |
106 | ||
107 | /* This name has already been marked */ | |
108 | if (l->object->generation == generation) | |
109 | continue; | |
110 | ||
111 | l->object->matters_to_anchor = true; | |
112 | l->object->generation = generation; | |
113 | ||
114 | transaction_find_jobs_that_matter_to_anchor(m, l->object, generation); | |
115 | } | |
116 | } | |
117 | ||
7fad411c | 118 | static void transaction_merge_and_delete_job(Manager *m, Job *j, Job *other, JobType t) { |
e5b5ae50 LP |
119 | JobDependency *l, *last; |
120 | ||
121 | assert(j); | |
122 | assert(other); | |
123 | assert(j->name == other->name); | |
124 | assert(!j->linked); | |
125 | ||
1ffba6fe LP |
126 | /* Merges 'other' into 'j' and then deletes j. */ |
127 | ||
e5b5ae50 LP |
128 | j->type = t; |
129 | j->state = JOB_WAITING; | |
130 | ||
131 | j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor; | |
132 | ||
133 | /* Patch us in as new owner of the JobDependency objects */ | |
134 | last = NULL; | |
135 | for (l = other->subject_list; l; l = l->subject_next) { | |
136 | assert(l->subject == other); | |
137 | l->subject = j; | |
138 | last = l; | |
139 | } | |
140 | ||
141 | /* Merge both lists */ | |
142 | if (last) { | |
143 | last->subject_next = j->subject_list; | |
144 | if (j->subject_list) | |
145 | j->subject_list->subject_prev = last; | |
146 | j->subject_list = other->subject_list; | |
147 | } | |
148 | ||
149 | /* Patch us in as new owner of the JobDependency objects */ | |
150 | last = NULL; | |
151 | for (l = other->object_list; l; l = l->object_next) { | |
152 | assert(l->object == other); | |
153 | l->object = j; | |
154 | last = l; | |
155 | } | |
156 | ||
157 | /* Merge both lists */ | |
158 | if (last) { | |
159 | last->object_next = j->object_list; | |
160 | if (j->object_list) | |
161 | j->object_list->object_prev = last; | |
162 | j->object_list = other->object_list; | |
163 | } | |
164 | ||
e5b5ae50 LP |
165 | /* Kill the other job */ |
166 | other->subject_list = NULL; | |
167 | other->object_list = NULL; | |
302d0040 | 168 | transaction_delete_job(m, other); |
e5b5ae50 LP |
169 | } |
170 | ||
1ffba6fe LP |
171 | static int delete_one_unmergable_job(Manager *m, Job *j) { |
172 | Job *k; | |
173 | ||
174 | assert(j); | |
175 | ||
176 | /* Tries to delete one item in the linked list | |
177 | * j->transaction_next->transaction_next->... that conflicts | |
178 | * whith another one, in an attempt to make an inconsistent | |
179 | * transaction work. */ | |
180 | ||
181 | /* We rely here on the fact that if a merged with b does not | |
182 | * merge with c, either a or b merge with c neither */ | |
183 | for (; j; j = j->transaction_next) | |
184 | for (k = j->transaction_next; k; k = k->transaction_next) { | |
185 | Job *d; | |
186 | ||
187 | /* Is this one mergeable? Then skip it */ | |
188 | if (job_type_mergeable(j->type, k->type)) | |
189 | continue; | |
190 | ||
191 | /* Ok, we found two that conflict, let's see if we can | |
192 | * drop one of them */ | |
193 | if (!j->matters_to_anchor) | |
194 | d = j; | |
195 | else if (!k->matters_to_anchor) | |
196 | d = k; | |
197 | else | |
198 | return -ENOEXEC; | |
199 | ||
200 | /* Ok, we can drop one, so let's do so. */ | |
201 | log_debug("Try to fix job merging by deleting job %s", name_id(d->name)); | |
202 | transaction_delete_job(m, d); | |
203 | return 0; | |
204 | } | |
205 | ||
206 | return -EINVAL; | |
207 | } | |
208 | ||
e5b5ae50 | 209 | static int transaction_merge_jobs(Manager *m) { |
11dd41ce | 210 | Job *j; |
e5b5ae50 LP |
211 | void *state; |
212 | int r; | |
213 | ||
214 | assert(m); | |
215 | ||
1ffba6fe LP |
216 | /* First step, check whether any of the jobs for one specific |
217 | * task conflict. If so, try to drop one of them. */ | |
218 | HASHMAP_FOREACH(j, m->transaction_jobs, state) { | |
219 | JobType t; | |
220 | Job *k; | |
221 | ||
222 | t = j->type; | |
223 | for (k = j->transaction_next; k; k = k->transaction_next) { | |
224 | if ((r = job_type_merge(&t, k->type)) >= 0) | |
225 | continue; | |
226 | ||
227 | /* OK, we could not merge all jobs for this | |
228 | * action. Let's see if we can get rid of one | |
229 | * of them */ | |
230 | ||
231 | if ((r = delete_one_unmergable_job(m, j)) >= 0) | |
232 | /* Ok, we managed to drop one, now | |
233 | * let's ask our callers to call us | |
234 | * again after garbage collecting */ | |
235 | return -EAGAIN; | |
236 | ||
237 | /* We couldn't merge anything. Failure */ | |
238 | return r; | |
239 | } | |
240 | } | |
241 | ||
242 | /* Second step, merge the jobs. */ | |
e5b5ae50 LP |
243 | HASHMAP_FOREACH(j, m->transaction_jobs, state) { |
244 | JobType t = j->type; | |
245 | Job *k; | |
246 | ||
e094e853 | 247 | /* Merge all transactions */ |
e5b5ae50 | 248 | for (k = j->transaction_next; k; k = k->transaction_next) |
1ffba6fe | 249 | assert_se(job_type_merge(&t, k->type) == 0); |
e5b5ae50 | 250 | |
e094e853 LP |
251 | /* If an active job is mergable, merge it too */ |
252 | if (j->name->meta.job) | |
253 | job_type_merge(&t, j->name->meta.job->type); /* Might fail. Which is OK */ | |
254 | ||
e5b5ae50 LP |
255 | while ((k = j->transaction_next)) { |
256 | if (j->linked) { | |
7fad411c | 257 | transaction_merge_and_delete_job(m, k, j, t); |
e5b5ae50 LP |
258 | j = k; |
259 | } else | |
7fad411c | 260 | transaction_merge_and_delete_job(m, j, k, t); |
e5b5ae50 LP |
261 | } |
262 | ||
263 | assert(!j->transaction_next); | |
264 | assert(!j->transaction_prev); | |
265 | } | |
266 | ||
7fad411c | 267 | return 0; |
e5b5ae50 LP |
268 | } |
269 | ||
1ffba6fe LP |
270 | static bool name_matters_to_anchor(Name *n, Job *j) { |
271 | assert(n); | |
272 | assert(!j->transaction_prev); | |
273 | ||
274 | /* Checks whether at least one of the jobs for this name | |
275 | * matters to the anchor. */ | |
276 | ||
277 | for (; j; j = j->transaction_next) | |
278 | if (j->matters_to_anchor) | |
279 | return true; | |
280 | ||
281 | return false; | |
282 | } | |
283 | ||
e5b5ae50 LP |
284 | static int transaction_verify_order_one(Manager *m, Job *j, Job *from, unsigned generation) { |
285 | void *state; | |
286 | Name *n; | |
11dd41ce | 287 | int r; |
e5b5ae50 LP |
288 | |
289 | assert(m); | |
290 | assert(j); | |
1ffba6fe LP |
291 | assert(!j->transaction_prev); |
292 | ||
293 | /* Does a recursive sweep through the ordering graph, looking | |
294 | * for a cycle. If we find cycle we try to break it. */ | |
e5b5ae50 | 295 | |
7fad411c | 296 | /* Did we find a cycle? */ |
e5b5ae50 LP |
297 | if (j->marker && j->generation == generation) { |
298 | Job *k; | |
299 | ||
300 | /* So, we already have been here. We have a | |
1ffba6fe LP |
301 | * cycle. Let's try to break it. We go backwards in |
302 | * our path and try to find a suitable job to | |
303 | * remove. We use the marker to find our way back, | |
304 | * since smart how we are we stored our way back in | |
305 | * there. */ | |
e5b5ae50 LP |
306 | |
307 | for (k = from; k; k = (k->generation == generation ? k->marker : NULL)) { | |
1ffba6fe LP |
308 | |
309 | if (!k->linked && | |
310 | !name_matters_to_anchor(k->name, k)) { | |
311 | /* Ok, we can drop this one, so let's | |
312 | * do so. */ | |
16354eff | 313 | log_debug("Breaking order cycle by deleting job %s", name_id(k->name)); |
1ffba6fe | 314 | transaction_delete_name(m, k->name); |
e5b5ae50 LP |
315 | return -EAGAIN; |
316 | } | |
317 | ||
318 | /* Check if this in fact was the beginning of | |
7fad411c | 319 | * the cycle */ |
e5b5ae50 LP |
320 | if (k == j) |
321 | break; | |
322 | } | |
323 | ||
1ffba6fe | 324 | return -ENOEXEC; |
e5b5ae50 LP |
325 | } |
326 | ||
1ffba6fe LP |
327 | /* Make the marker point to where we come from, so that we can |
328 | * find our way backwards if we want to break a cycle */ | |
e5b5ae50 LP |
329 | j->marker = from; |
330 | j->generation = generation; | |
331 | ||
1ffba6fe | 332 | /* We assume that the the dependencies are bidirectional, and |
e5b5ae50 | 333 | * hence can ignore NAME_AFTER */ |
e5b5ae50 LP |
334 | SET_FOREACH(n, j->name->meta.dependencies[NAME_BEFORE], state) { |
335 | Job *o; | |
336 | ||
1ffba6fe | 337 | /* Is there a job for this name? */ |
e5b5ae50 | 338 | if (!(o = hashmap_get(m->transaction_jobs, n))) |
1ffba6fe LP |
339 | |
340 | /* Ok, there is no job for this in the | |
341 | * transaction, but maybe there is already one | |
342 | * running? */ | |
e5b5ae50 LP |
343 | if (!(o = n->meta.job)) |
344 | continue; | |
345 | ||
346 | if ((r = transaction_verify_order_one(m, o, j, generation)) < 0) | |
347 | return r; | |
348 | } | |
349 | ||
350 | return 0; | |
351 | } | |
352 | ||
353 | static int transaction_verify_order(Manager *m, unsigned *generation) { | |
1ffba6fe LP |
354 | Job *j; |
355 | int r; | |
356 | void *state; | |
357 | ||
e5b5ae50 LP |
358 | assert(m); |
359 | assert(generation); | |
360 | ||
1ffba6fe LP |
361 | /* Check if the ordering graph is cyclic. If it is, try to fix |
362 | * that up by dropping one of the jobs. */ | |
e5b5ae50 | 363 | |
1ffba6fe LP |
364 | HASHMAP_FOREACH(j, m->transaction_jobs, state) |
365 | if ((r = transaction_verify_order_one(m, j, NULL, (*generation)++)) < 0) | |
366 | return r; | |
e5b5ae50 LP |
367 | |
368 | return 0; | |
369 | } | |
370 | ||
371 | static void transaction_collect_garbage(Manager *m) { | |
372 | bool again; | |
373 | ||
374 | assert(m); | |
375 | ||
1ffba6fe LP |
376 | /* Drop jobs that are not required by any other job */ |
377 | ||
e5b5ae50 LP |
378 | do { |
379 | void *state; | |
380 | Job *j; | |
381 | ||
382 | again = false; | |
383 | ||
384 | HASHMAP_FOREACH(j, m->transaction_jobs, state) { | |
385 | if (j->object_list) | |
386 | continue; | |
387 | ||
9ea024f6 LP |
388 | log_debug("Garbage collecting job %s", name_id(j->name)); |
389 | ||
302d0040 | 390 | transaction_delete_job(m, j); |
e5b5ae50 LP |
391 | again = true; |
392 | break; | |
393 | } | |
394 | ||
395 | } while (again); | |
396 | } | |
397 | ||
398 | static int transaction_is_destructive(Manager *m, JobMode mode) { | |
11dd41ce | 399 | void *state; |
e5b5ae50 | 400 | Job *j; |
11dd41ce LP |
401 | |
402 | assert(m); | |
11dd41ce | 403 | |
e5b5ae50 LP |
404 | /* Checks whether applying this transaction means that |
405 | * existing jobs would be replaced */ | |
11dd41ce | 406 | |
e094e853 LP |
407 | HASHMAP_FOREACH(j, m->transaction_jobs, state) { |
408 | ||
409 | /* Assume merged */ | |
410 | assert(!j->transaction_prev); | |
411 | assert(!j->transaction_next); | |
412 | ||
1ffba6fe LP |
413 | if (j->name->meta.job && |
414 | j->name->meta.job != j && | |
415 | !job_type_is_superset(j->type, j->name->meta.job->type)) | |
e5b5ae50 | 416 | return -EEXIST; |
e094e853 | 417 | } |
11dd41ce | 418 | |
e5b5ae50 LP |
419 | return 0; |
420 | } | |
421 | ||
e094e853 LP |
422 | static void transaction_minimize_impact(Manager *m) { |
423 | bool again; | |
424 | assert(m); | |
425 | ||
426 | /* Drops all unnecessary jobs that reverse already active jobs | |
427 | * or that stop a running service. */ | |
428 | ||
429 | do { | |
430 | Job *j; | |
431 | void *state; | |
432 | ||
433 | again = false; | |
434 | ||
435 | HASHMAP_FOREACH(j, m->transaction_jobs, state) { | |
436 | for (; j; j = j->transaction_next) { | |
437 | ||
438 | /* If it matters, we shouldn't drop it */ | |
439 | if (j->matters_to_anchor) | |
440 | continue; | |
441 | ||
442 | /* Would this stop a running service? | |
443 | * Would this change an existing job? | |
444 | * If so, let's drop this entry */ | |
445 | if ((j->type != JOB_STOP || name_is_dead(j->name)) && | |
446 | (!j->name->meta.job || job_type_is_conflicting(j->type, j->name->meta.job->state))) | |
447 | continue; | |
448 | ||
449 | /* Ok, let's get rid of this */ | |
450 | transaction_delete_job(m, j); | |
451 | again = true; | |
452 | break; | |
453 | } | |
454 | ||
455 | if (again) | |
456 | break; | |
457 | } | |
458 | ||
459 | } while (again); | |
460 | } | |
461 | ||
e5b5ae50 LP |
462 | static int transaction_apply(Manager *m, JobMode mode) { |
463 | void *state; | |
464 | Job *j; | |
465 | int r; | |
466 | ||
1ffba6fe LP |
467 | /* Moves the transaction jobs to the set of active jobs */ |
468 | ||
e5b5ae50 | 469 | HASHMAP_FOREACH(j, m->transaction_jobs, state) { |
e094e853 LP |
470 | /* Assume merged */ |
471 | assert(!j->transaction_prev); | |
472 | assert(!j->transaction_next); | |
473 | ||
e5b5ae50 LP |
474 | if (j->linked) |
475 | continue; | |
476 | ||
477 | if ((r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j)) < 0) | |
11dd41ce LP |
478 | goto rollback; |
479 | } | |
480 | ||
e5b5ae50 LP |
481 | while ((j = hashmap_steal_first(m->transaction_jobs))) { |
482 | if (j->linked) | |
483 | continue; | |
484 | ||
485 | if (j->name->meta.job) | |
486 | job_free(j->name->meta.job); | |
11dd41ce | 487 | |
11dd41ce LP |
488 | j->name->meta.job = j; |
489 | j->linked = true; | |
11dd41ce | 490 | |
e5b5ae50 LP |
491 | /* We're fully installed. Now let's free data we don't |
492 | * need anymore. */ | |
493 | ||
494 | assert(!j->transaction_next); | |
495 | assert(!j->transaction_prev); | |
496 | ||
497 | while (j->subject_list) | |
498 | job_dependency_free(j->subject_list); | |
499 | while (j->object_list) | |
500 | job_dependency_free(j->object_list); | |
501 | } | |
11dd41ce | 502 | |
1ffba6fe LP |
503 | m->transaction_anchor = NULL; |
504 | ||
11dd41ce LP |
505 | return 0; |
506 | ||
507 | rollback: | |
508 | ||
e5b5ae50 LP |
509 | HASHMAP_FOREACH(j, m->transaction_jobs, state) { |
510 | if (j->linked) | |
511 | continue; | |
512 | ||
513 | hashmap_remove(m->jobs, UINT32_TO_PTR(j->id)); | |
514 | } | |
515 | ||
516 | return r; | |
517 | } | |
518 | ||
e5b5ae50 LP |
519 | static int transaction_activate(Manager *m, JobMode mode) { |
520 | int r; | |
521 | unsigned generation = 1; | |
522 | ||
523 | assert(m); | |
524 | ||
525 | /* This applies the changes recorded in transaction_jobs to | |
526 | * the actual list of jobs, if possible. */ | |
527 | ||
528 | /* First step: figure out which jobs matter */ | |
529 | transaction_find_jobs_that_matter_to_anchor(m, NULL, generation++); | |
530 | ||
e094e853 LP |
531 | /* Second step: Try not to stop any running services if |
532 | * we don't have to. Don't try to reverse running | |
533 | * jobs if we don't have to. */ | |
534 | transaction_minimize_impact(m); | |
535 | ||
1ffba6fe | 536 | for (;;) { |
e094e853 | 537 | /* Third step: Let's remove unneeded jobs that might |
1ffba6fe LP |
538 | * be lurking. */ |
539 | transaction_collect_garbage(m); | |
e5b5ae50 | 540 | |
e094e853 | 541 | /* Fourth step: verify order makes sense and correct |
1ffba6fe LP |
542 | * cycles if necessary and possible */ |
543 | if ((r = transaction_verify_order(m, &generation)) >= 0) | |
544 | break; | |
e5b5ae50 | 545 | |
1ffba6fe LP |
546 | if (r != -EAGAIN) |
547 | goto rollback; | |
e5b5ae50 | 548 | |
1ffba6fe LP |
549 | /* Let's see if the resulting transaction ordering |
550 | * graph is still cyclic... */ | |
551 | } | |
552 | ||
553 | for (;;) { | |
e094e853 | 554 | /* Fifth step: let's drop unmergable entries if |
1ffba6fe LP |
555 | * necessary and possible, merge entries we can |
556 | * merge */ | |
557 | if ((r = transaction_merge_jobs(m)) >= 0) | |
558 | break; | |
559 | ||
560 | if (r != -EAGAIN) | |
561 | goto rollback; | |
562 | ||
e094e853 | 563 | /* Sixth step: an entry got dropped, let's garbage |
1ffba6fe LP |
564 | * collect its dependencies. */ |
565 | transaction_collect_garbage(m); | |
566 | ||
567 | /* Let's see if the resulting transaction still has | |
568 | * unmergable entries ... */ | |
569 | } | |
570 | ||
e094e853 | 571 | /* Seventh step: check whether we can actually apply this */ |
e5b5ae50 LP |
572 | if (mode == JOB_FAIL) |
573 | if ((r = transaction_is_destructive(m, mode)) < 0) | |
574 | goto rollback; | |
575 | ||
e094e853 | 576 | /* Eights step: apply changes */ |
e5b5ae50 LP |
577 | if ((r = transaction_apply(m, mode)) < 0) |
578 | goto rollback; | |
579 | ||
580 | assert(hashmap_isempty(m->transaction_jobs)); | |
581 | assert(!m->transaction_anchor); | |
582 | ||
583 | return 0; | |
11dd41ce | 584 | |
e5b5ae50 | 585 | rollback: |
11dd41ce LP |
586 | transaction_abort(m); |
587 | return r; | |
588 | } | |
589 | ||
ceed3570 | 590 | static Job* transaction_add_one_job(Manager *m, JobType type, Name *name, bool *is_new) { |
e5b5ae50 | 591 | Job *j, *f; |
60918275 LP |
592 | int r; |
593 | ||
594 | assert(m); | |
60918275 | 595 | assert(name); |
60918275 | 596 | |
e5b5ae50 LP |
597 | /* Looks for an axisting prospective job and returns that. If |
598 | * it doesn't exist it is created and added to the prospective | |
599 | * jobs list. */ | |
60918275 | 600 | |
e5b5ae50 | 601 | f = hashmap_get(m->transaction_jobs, name); |
60918275 | 602 | |
e5b5ae50 LP |
603 | for (j = f; j; j = j->transaction_next) { |
604 | assert(j->name == name); | |
60918275 | 605 | |
e5b5ae50 LP |
606 | if (j->type == type) { |
607 | if (is_new) | |
608 | *is_new = false; | |
609 | return j; | |
610 | } | |
611 | } | |
60918275 | 612 | |
e5b5ae50 LP |
613 | if (name->meta.job && name->meta.job->type == type) |
614 | j = name->meta.job; | |
615 | else if (!(j = job_new(m, type, name))) | |
616 | return NULL; | |
60918275 | 617 | |
e5b5ae50 LP |
618 | if ((r = hashmap_replace(m->transaction_jobs, name, j)) < 0) { |
619 | job_free(j); | |
620 | return NULL; | |
60918275 LP |
621 | } |
622 | ||
e5b5ae50 | 623 | j->transaction_next = f; |
60918275 | 624 | |
e5b5ae50 LP |
625 | if (f) |
626 | f->transaction_prev = j; | |
11dd41ce | 627 | |
e5b5ae50 LP |
628 | j->generation = 0; |
629 | j->marker = NULL; | |
630 | j->matters_to_anchor = false; | |
60918275 | 631 | |
e5b5ae50 LP |
632 | if (is_new) |
633 | *is_new = true; | |
60918275 | 634 | |
e5b5ae50 LP |
635 | return j; |
636 | } | |
11dd41ce | 637 | |
302d0040 | 638 | void manager_transaction_unlink_job(Manager *m, Job *j) { |
e5b5ae50 LP |
639 | assert(m); |
640 | assert(j); | |
11dd41ce | 641 | |
e5b5ae50 LP |
642 | if (j->transaction_prev) |
643 | j->transaction_prev->transaction_next = j->transaction_next; | |
644 | else if (j->transaction_next) | |
645 | hashmap_replace(m->transaction_jobs, j->name, j->transaction_next); | |
646 | else | |
647 | hashmap_remove_value(m->transaction_jobs, j->name, j); | |
648 | ||
649 | if (j->transaction_next) | |
650 | j->transaction_next->transaction_prev = j->transaction_prev; | |
651 | ||
652 | j->transaction_prev = j->transaction_next = NULL; | |
653 | ||
654 | while (j->subject_list) | |
655 | job_dependency_free(j->subject_list); | |
1e198baf LP |
656 | |
657 | while (j->object_list) { | |
658 | Job *other = j->object_list->matters ? j->object_list->subject : NULL; | |
659 | ||
e5b5ae50 | 660 | job_dependency_free(j->object_list); |
1e198baf LP |
661 | |
662 | if (other) { | |
9ea024f6 | 663 | log_debug("Deleting job %s as dependency of job %s", name_id(other->name), name_id(j->name)); |
302d0040 | 664 | transaction_delete_job(m, other); |
1e198baf LP |
665 | } |
666 | } | |
e5b5ae50 LP |
667 | } |
668 | ||
ceed3570 | 669 | static int transaction_add_job_and_dependencies(Manager *m, JobType type, Name *name, Job *by, bool matters, bool force, Job **_ret) { |
e5b5ae50 LP |
670 | Job *ret; |
671 | void *state; | |
672 | Name *dep; | |
673 | int r; | |
674 | bool is_new; | |
675 | ||
676 | assert(m); | |
677 | assert(type < _JOB_TYPE_MAX); | |
678 | assert(name); | |
679 | ||
21b293e8 LP |
680 | if (name->meta.state != NAME_LOADED) |
681 | return -EINVAL; | |
682 | ||
cd2dbd7d LP |
683 | if (!job_type_applicable(type, name->meta.type)) |
684 | return -EBADR; | |
685 | ||
e5b5ae50 | 686 | /* First add the job. */ |
ceed3570 | 687 | if (!(ret = transaction_add_one_job(m, type, name, &is_new))) |
e5b5ae50 LP |
688 | return -ENOMEM; |
689 | ||
690 | /* Then, add a link to the job. */ | |
691 | if (!job_dependency_new(by, ret, matters)) | |
692 | return -ENOMEM; | |
693 | ||
694 | if (is_new) { | |
695 | /* Finally, recursively add in all dependencies. */ | |
696 | if (type == JOB_START || type == JOB_RELOAD_OR_START) { | |
697 | SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRES], state) | |
cd2dbd7d | 698 | if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, force, NULL)) != -EBADR) |
e5b5ae50 LP |
699 | goto fail; |
700 | SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUIRES], state) | |
cd2dbd7d | 701 | if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !force, force, NULL)) != -EBADR) |
e5b5ae50 LP |
702 | goto fail; |
703 | SET_FOREACH(dep, ret->name->meta.dependencies[NAME_WANTS], state) | |
cd2dbd7d | 704 | if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, force, NULL)) != -EBADR) |
e5b5ae50 LP |
705 | goto fail; |
706 | SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUISITE], state) | |
cd2dbd7d | 707 | if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_STARTED, dep, ret, true, force, NULL)) != -EBADR) |
e5b5ae50 LP |
708 | goto fail; |
709 | SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUISITE], state) | |
cd2dbd7d | 710 | if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_STARTED, dep, ret, !force, force, NULL)) != -EBADR) |
e5b5ae50 LP |
711 | goto fail; |
712 | SET_FOREACH(dep, ret->name->meta.dependencies[NAME_CONFLICTS], state) | |
cd2dbd7d | 713 | if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, force, NULL)) != -EBADR) |
e5b5ae50 LP |
714 | goto fail; |
715 | ||
716 | } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) { | |
717 | ||
718 | SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRED_BY], state) | |
cd2dbd7d | 719 | if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, force, NULL)) != -EBADR) |
e5b5ae50 LP |
720 | goto fail; |
721 | } | |
722 | ||
723 | /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */ | |
724 | } | |
60918275 LP |
725 | |
726 | return 0; | |
727 | ||
728 | fail: | |
e5b5ae50 LP |
729 | return r; |
730 | } | |
731 | ||
732 | int manager_add_job(Manager *m, JobType type, Name *name, JobMode mode, bool force, Job **_ret) { | |
733 | int r; | |
734 | Job *ret; | |
735 | ||
736 | assert(m); | |
737 | assert(type < _JOB_TYPE_MAX); | |
738 | assert(name); | |
739 | assert(mode < _JOB_MODE_MAX); | |
60918275 | 740 | |
ceed3570 | 741 | if ((r = transaction_add_job_and_dependencies(m, type, name, NULL, true, force, &ret))) { |
11dd41ce | 742 | transaction_abort(m); |
e5b5ae50 LP |
743 | return r; |
744 | } | |
11dd41ce | 745 | |
e5b5ae50 LP |
746 | if ((r = transaction_activate(m, mode)) < 0) |
747 | return r; | |
748 | ||
749 | if (_ret) | |
750 | *_ret = ret; | |
60918275 | 751 | |
e5b5ae50 LP |
752 | return 0; |
753 | } | |
60918275 LP |
754 | |
755 | Job *manager_get_job(Manager *m, uint32_t id) { | |
756 | assert(m); | |
757 | ||
758 | return hashmap_get(m->jobs, UINT32_TO_PTR(id)); | |
759 | } | |
760 | ||
761 | Name *manager_get_name(Manager *m, const char *name) { | |
762 | assert(m); | |
763 | assert(name); | |
764 | ||
765 | return hashmap_get(m->names, name); | |
766 | } | |
767 | ||
60918275 LP |
768 | static int dispatch_load_queue(Manager *m) { |
769 | Meta *meta; | |
770 | ||
771 | assert(m); | |
772 | ||
223dabab LP |
773 | /* Make sure we are not run recursively */ |
774 | if (m->dispatching_load_queue) | |
775 | return 0; | |
776 | ||
777 | m->dispatching_load_queue = true; | |
778 | ||
60918275 LP |
779 | /* Dispatches the load queue. Takes a name from the queue and |
780 | * tries to load its data until the queue is empty */ | |
781 | ||
782 | while ((meta = m->load_queue)) { | |
7fad411c | 783 | name_load(NAME(meta)); |
60918275 LP |
784 | LIST_REMOVE(Meta, m->load_queue, meta); |
785 | } | |
786 | ||
223dabab LP |
787 | m->dispatching_load_queue = false; |
788 | ||
60918275 LP |
789 | return 0; |
790 | } | |
791 | ||
60918275 LP |
792 | int manager_load_name(Manager *m, const char *name, Name **_ret) { |
793 | Name *ret; | |
794 | NameType t; | |
795 | int r; | |
87d1515d | 796 | char *n; |
60918275 LP |
797 | |
798 | assert(m); | |
799 | assert(name); | |
800 | assert(_ret); | |
60918275 | 801 | |
223dabab LP |
802 | if (!name_is_valid(name)) |
803 | return -EINVAL; | |
804 | ||
805 | /* This will load the service information files, but not actually | |
806 | * start any services or anything */ | |
60918275 LP |
807 | |
808 | if ((ret = manager_get_name(m, name))) | |
809 | goto finish; | |
810 | ||
811 | if ((t = name_type_from_string(name)) == _NAME_TYPE_INVALID) | |
812 | return -EINVAL; | |
813 | ||
814 | if (!(ret = name_new(m))) | |
815 | return -ENOMEM; | |
816 | ||
817 | ret->meta.type = t; | |
818 | ||
87d1515d LP |
819 | if (!(n = strdup(name))) { |
820 | name_free(ret); | |
821 | return -ENOMEM; | |
822 | } | |
823 | ||
1ffba6fe | 824 | if ((r = set_put(ret->meta.names, n)) < 0) { |
60918275 | 825 | name_free(ret); |
87d1515d | 826 | free(n); |
1ffba6fe | 827 | return r; |
60918275 LP |
828 | } |
829 | ||
830 | if ((r = name_link(ret)) < 0) { | |
831 | name_free(ret); | |
832 | return r; | |
833 | } | |
834 | ||
835 | /* At this point the new entry is created and linked. However, | |
836 | * not loaded. Now load this entry and all its dependencies | |
837 | * recursively */ | |
838 | ||
839 | dispatch_load_queue(m); | |
840 | ||
841 | finish: | |
842 | ||
843 | *_ret = ret; | |
844 | return 0; | |
845 | } | |
a66d02c3 | 846 | |
cea8e32e | 847 | void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) { |
a66d02c3 LP |
848 | void *state; |
849 | Job *j; | |
850 | ||
851 | assert(s); | |
852 | assert(f); | |
853 | ||
854 | HASHMAP_FOREACH(j, s->jobs, state) | |
cea8e32e | 855 | job_dump(j, f, prefix); |
a66d02c3 LP |
856 | } |
857 | ||
cea8e32e | 858 | void manager_dump_names(Manager *s, FILE *f, const char *prefix) { |
a66d02c3 LP |
859 | void *state; |
860 | Name *n; | |
11dd41ce | 861 | const char *t; |
a66d02c3 LP |
862 | |
863 | assert(s); | |
864 | assert(f); | |
865 | ||
11dd41ce LP |
866 | HASHMAP_FOREACH_KEY(n, t, s->names, state) |
867 | if (name_id(n) == t) | |
cea8e32e | 868 | name_dump(n, f, prefix); |
a66d02c3 | 869 | } |
7fad411c LP |
870 | |
871 | void manager_clear_jobs(Manager *m) { | |
872 | Job *j; | |
873 | ||
874 | assert(m); | |
875 | ||
876 | transaction_abort(m); | |
877 | ||
878 | while ((j = hashmap_first(m->jobs))) | |
879 | job_free(j); | |
880 | } |