]>
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 | ||
247 | for (k = j->transaction_next; k; k = k->transaction_next) | |
1ffba6fe | 248 | assert_se(job_type_merge(&t, k->type) == 0); |
e5b5ae50 LP |
249 | |
250 | while ((k = j->transaction_next)) { | |
251 | if (j->linked) { | |
7fad411c | 252 | transaction_merge_and_delete_job(m, k, j, t); |
e5b5ae50 LP |
253 | j = k; |
254 | } else | |
7fad411c | 255 | transaction_merge_and_delete_job(m, j, k, t); |
e5b5ae50 LP |
256 | } |
257 | ||
258 | assert(!j->transaction_next); | |
259 | assert(!j->transaction_prev); | |
260 | } | |
261 | ||
7fad411c | 262 | return 0; |
e5b5ae50 LP |
263 | } |
264 | ||
1ffba6fe LP |
265 | static bool name_matters_to_anchor(Name *n, Job *j) { |
266 | assert(n); | |
267 | assert(!j->transaction_prev); | |
268 | ||
269 | /* Checks whether at least one of the jobs for this name | |
270 | * matters to the anchor. */ | |
271 | ||
272 | for (; j; j = j->transaction_next) | |
273 | if (j->matters_to_anchor) | |
274 | return true; | |
275 | ||
276 | return false; | |
277 | } | |
278 | ||
e5b5ae50 LP |
279 | static int transaction_verify_order_one(Manager *m, Job *j, Job *from, unsigned generation) { |
280 | void *state; | |
281 | Name *n; | |
11dd41ce | 282 | int r; |
e5b5ae50 LP |
283 | |
284 | assert(m); | |
285 | assert(j); | |
1ffba6fe LP |
286 | assert(!j->transaction_prev); |
287 | ||
288 | /* Does a recursive sweep through the ordering graph, looking | |
289 | * for a cycle. If we find cycle we try to break it. */ | |
e5b5ae50 | 290 | |
7fad411c | 291 | /* Did we find a cycle? */ |
e5b5ae50 LP |
292 | if (j->marker && j->generation == generation) { |
293 | Job *k; | |
294 | ||
295 | /* So, we already have been here. We have a | |
1ffba6fe LP |
296 | * cycle. Let's try to break it. We go backwards in |
297 | * our path and try to find a suitable job to | |
298 | * remove. We use the marker to find our way back, | |
299 | * since smart how we are we stored our way back in | |
300 | * there. */ | |
e5b5ae50 LP |
301 | |
302 | for (k = from; k; k = (k->generation == generation ? k->marker : NULL)) { | |
1ffba6fe LP |
303 | |
304 | if (!k->linked && | |
305 | !name_matters_to_anchor(k->name, k)) { | |
306 | /* Ok, we can drop this one, so let's | |
307 | * do so. */ | |
16354eff | 308 | log_debug("Breaking order cycle by deleting job %s", name_id(k->name)); |
1ffba6fe | 309 | transaction_delete_name(m, k->name); |
e5b5ae50 LP |
310 | return -EAGAIN; |
311 | } | |
312 | ||
313 | /* Check if this in fact was the beginning of | |
7fad411c | 314 | * the cycle */ |
e5b5ae50 LP |
315 | if (k == j) |
316 | break; | |
317 | } | |
318 | ||
1ffba6fe | 319 | return -ENOEXEC; |
e5b5ae50 LP |
320 | } |
321 | ||
1ffba6fe LP |
322 | /* Make the marker point to where we come from, so that we can |
323 | * find our way backwards if we want to break a cycle */ | |
e5b5ae50 LP |
324 | j->marker = from; |
325 | j->generation = generation; | |
326 | ||
1ffba6fe | 327 | /* We assume that the the dependencies are bidirectional, and |
e5b5ae50 | 328 | * hence can ignore NAME_AFTER */ |
e5b5ae50 LP |
329 | SET_FOREACH(n, j->name->meta.dependencies[NAME_BEFORE], state) { |
330 | Job *o; | |
331 | ||
1ffba6fe | 332 | /* Is there a job for this name? */ |
e5b5ae50 | 333 | if (!(o = hashmap_get(m->transaction_jobs, n))) |
1ffba6fe LP |
334 | |
335 | /* Ok, there is no job for this in the | |
336 | * transaction, but maybe there is already one | |
337 | * running? */ | |
e5b5ae50 LP |
338 | if (!(o = n->meta.job)) |
339 | continue; | |
340 | ||
341 | if ((r = transaction_verify_order_one(m, o, j, generation)) < 0) | |
342 | return r; | |
343 | } | |
344 | ||
345 | return 0; | |
346 | } | |
347 | ||
348 | static int transaction_verify_order(Manager *m, unsigned *generation) { | |
1ffba6fe LP |
349 | Job *j; |
350 | int r; | |
351 | void *state; | |
352 | ||
e5b5ae50 LP |
353 | assert(m); |
354 | assert(generation); | |
355 | ||
1ffba6fe LP |
356 | /* Check if the ordering graph is cyclic. If it is, try to fix |
357 | * that up by dropping one of the jobs. */ | |
e5b5ae50 | 358 | |
1ffba6fe LP |
359 | HASHMAP_FOREACH(j, m->transaction_jobs, state) |
360 | if ((r = transaction_verify_order_one(m, j, NULL, (*generation)++)) < 0) | |
361 | return r; | |
e5b5ae50 LP |
362 | |
363 | return 0; | |
364 | } | |
365 | ||
366 | static void transaction_collect_garbage(Manager *m) { | |
367 | bool again; | |
368 | ||
369 | assert(m); | |
370 | ||
1ffba6fe LP |
371 | /* Drop jobs that are not required by any other job */ |
372 | ||
e5b5ae50 LP |
373 | do { |
374 | void *state; | |
375 | Job *j; | |
376 | ||
377 | again = false; | |
378 | ||
379 | HASHMAP_FOREACH(j, m->transaction_jobs, state) { | |
380 | if (j->object_list) | |
381 | continue; | |
382 | ||
9ea024f6 LP |
383 | log_debug("Garbage collecting job %s", name_id(j->name)); |
384 | ||
302d0040 | 385 | transaction_delete_job(m, j); |
e5b5ae50 LP |
386 | again = true; |
387 | break; | |
388 | } | |
389 | ||
390 | } while (again); | |
391 | } | |
392 | ||
393 | static int transaction_is_destructive(Manager *m, JobMode mode) { | |
11dd41ce | 394 | void *state; |
e5b5ae50 | 395 | Job *j; |
11dd41ce LP |
396 | |
397 | assert(m); | |
11dd41ce | 398 | |
e5b5ae50 LP |
399 | /* Checks whether applying this transaction means that |
400 | * existing jobs would be replaced */ | |
11dd41ce | 401 | |
e5b5ae50 | 402 | HASHMAP_FOREACH(j, m->transaction_jobs, state) |
1ffba6fe LP |
403 | if (j->name->meta.job && |
404 | j->name->meta.job != j && | |
405 | !job_type_is_superset(j->type, j->name->meta.job->type)) | |
e5b5ae50 | 406 | return -EEXIST; |
11dd41ce | 407 | |
e5b5ae50 LP |
408 | return 0; |
409 | } | |
410 | ||
411 | static int transaction_apply(Manager *m, JobMode mode) { | |
412 | void *state; | |
413 | Job *j; | |
414 | int r; | |
415 | ||
1ffba6fe LP |
416 | /* Moves the transaction jobs to the set of active jobs */ |
417 | ||
e5b5ae50 LP |
418 | HASHMAP_FOREACH(j, m->transaction_jobs, state) { |
419 | if (j->linked) | |
420 | continue; | |
421 | ||
422 | if ((r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j)) < 0) | |
11dd41ce LP |
423 | goto rollback; |
424 | } | |
425 | ||
e5b5ae50 LP |
426 | while ((j = hashmap_steal_first(m->transaction_jobs))) { |
427 | if (j->linked) | |
428 | continue; | |
429 | ||
430 | if (j->name->meta.job) | |
431 | job_free(j->name->meta.job); | |
11dd41ce | 432 | |
11dd41ce LP |
433 | j->name->meta.job = j; |
434 | j->linked = true; | |
11dd41ce | 435 | |
e5b5ae50 LP |
436 | /* We're fully installed. Now let's free data we don't |
437 | * need anymore. */ | |
438 | ||
439 | assert(!j->transaction_next); | |
440 | assert(!j->transaction_prev); | |
441 | ||
442 | while (j->subject_list) | |
443 | job_dependency_free(j->subject_list); | |
444 | while (j->object_list) | |
445 | job_dependency_free(j->object_list); | |
446 | } | |
11dd41ce | 447 | |
1ffba6fe LP |
448 | m->transaction_anchor = NULL; |
449 | ||
11dd41ce LP |
450 | return 0; |
451 | ||
452 | rollback: | |
453 | ||
e5b5ae50 LP |
454 | HASHMAP_FOREACH(j, m->transaction_jobs, state) { |
455 | if (j->linked) | |
456 | continue; | |
457 | ||
458 | hashmap_remove(m->jobs, UINT32_TO_PTR(j->id)); | |
459 | } | |
460 | ||
461 | return r; | |
462 | } | |
463 | ||
464 | ||
465 | static int transaction_activate(Manager *m, JobMode mode) { | |
466 | int r; | |
467 | unsigned generation = 1; | |
468 | ||
469 | assert(m); | |
470 | ||
471 | /* This applies the changes recorded in transaction_jobs to | |
472 | * the actual list of jobs, if possible. */ | |
473 | ||
474 | /* First step: figure out which jobs matter */ | |
475 | transaction_find_jobs_that_matter_to_anchor(m, NULL, generation++); | |
476 | ||
1ffba6fe LP |
477 | for (;;) { |
478 | /* Second step: Let's remove unneeded jobs that might | |
479 | * be lurking. */ | |
480 | transaction_collect_garbage(m); | |
e5b5ae50 | 481 | |
1ffba6fe LP |
482 | /* Third step: verify order makes sense and correct |
483 | * cycles if necessary and possible */ | |
484 | if ((r = transaction_verify_order(m, &generation)) >= 0) | |
485 | break; | |
e5b5ae50 | 486 | |
1ffba6fe LP |
487 | if (r != -EAGAIN) |
488 | goto rollback; | |
e5b5ae50 | 489 | |
1ffba6fe LP |
490 | /* Let's see if the resulting transaction ordering |
491 | * graph is still cyclic... */ | |
492 | } | |
493 | ||
494 | for (;;) { | |
495 | /* Fourth step: let's drop unmergable entries if | |
496 | * necessary and possible, merge entries we can | |
497 | * merge */ | |
498 | if ((r = transaction_merge_jobs(m)) >= 0) | |
499 | break; | |
500 | ||
501 | if (r != -EAGAIN) | |
502 | goto rollback; | |
503 | ||
504 | /* Fifth step: an entry got dropped, let's garbage | |
505 | * collect its dependencies. */ | |
506 | transaction_collect_garbage(m); | |
507 | ||
508 | /* Let's see if the resulting transaction still has | |
509 | * unmergable entries ... */ | |
510 | } | |
511 | ||
512 | /* Sith step: check whether we can actually apply this */ | |
e5b5ae50 LP |
513 | if (mode == JOB_FAIL) |
514 | if ((r = transaction_is_destructive(m, mode)) < 0) | |
515 | goto rollback; | |
516 | ||
1ffba6fe | 517 | /* Seventh step: apply changes */ |
e5b5ae50 LP |
518 | if ((r = transaction_apply(m, mode)) < 0) |
519 | goto rollback; | |
520 | ||
521 | assert(hashmap_isempty(m->transaction_jobs)); | |
522 | assert(!m->transaction_anchor); | |
523 | ||
524 | return 0; | |
11dd41ce | 525 | |
e5b5ae50 | 526 | rollback: |
11dd41ce LP |
527 | transaction_abort(m); |
528 | return r; | |
529 | } | |
530 | ||
ceed3570 | 531 | static Job* transaction_add_one_job(Manager *m, JobType type, Name *name, bool *is_new) { |
e5b5ae50 | 532 | Job *j, *f; |
60918275 LP |
533 | int r; |
534 | ||
535 | assert(m); | |
60918275 | 536 | assert(name); |
60918275 | 537 | |
e5b5ae50 LP |
538 | /* Looks for an axisting prospective job and returns that. If |
539 | * it doesn't exist it is created and added to the prospective | |
540 | * jobs list. */ | |
60918275 | 541 | |
e5b5ae50 | 542 | f = hashmap_get(m->transaction_jobs, name); |
60918275 | 543 | |
e5b5ae50 LP |
544 | for (j = f; j; j = j->transaction_next) { |
545 | assert(j->name == name); | |
60918275 | 546 | |
e5b5ae50 LP |
547 | if (j->type == type) { |
548 | if (is_new) | |
549 | *is_new = false; | |
550 | return j; | |
551 | } | |
552 | } | |
60918275 | 553 | |
e5b5ae50 LP |
554 | if (name->meta.job && name->meta.job->type == type) |
555 | j = name->meta.job; | |
556 | else if (!(j = job_new(m, type, name))) | |
557 | return NULL; | |
60918275 | 558 | |
e5b5ae50 LP |
559 | if ((r = hashmap_replace(m->transaction_jobs, name, j)) < 0) { |
560 | job_free(j); | |
561 | return NULL; | |
60918275 LP |
562 | } |
563 | ||
e5b5ae50 | 564 | j->transaction_next = f; |
60918275 | 565 | |
e5b5ae50 LP |
566 | if (f) |
567 | f->transaction_prev = j; | |
11dd41ce | 568 | |
e5b5ae50 LP |
569 | j->generation = 0; |
570 | j->marker = NULL; | |
571 | j->matters_to_anchor = false; | |
60918275 | 572 | |
e5b5ae50 LP |
573 | if (is_new) |
574 | *is_new = true; | |
60918275 | 575 | |
e5b5ae50 LP |
576 | return j; |
577 | } | |
11dd41ce | 578 | |
302d0040 | 579 | void manager_transaction_unlink_job(Manager *m, Job *j) { |
e5b5ae50 LP |
580 | assert(m); |
581 | assert(j); | |
11dd41ce | 582 | |
e5b5ae50 LP |
583 | if (j->transaction_prev) |
584 | j->transaction_prev->transaction_next = j->transaction_next; | |
585 | else if (j->transaction_next) | |
586 | hashmap_replace(m->transaction_jobs, j->name, j->transaction_next); | |
587 | else | |
588 | hashmap_remove_value(m->transaction_jobs, j->name, j); | |
589 | ||
590 | if (j->transaction_next) | |
591 | j->transaction_next->transaction_prev = j->transaction_prev; | |
592 | ||
593 | j->transaction_prev = j->transaction_next = NULL; | |
594 | ||
595 | while (j->subject_list) | |
596 | job_dependency_free(j->subject_list); | |
1e198baf LP |
597 | |
598 | while (j->object_list) { | |
599 | Job *other = j->object_list->matters ? j->object_list->subject : NULL; | |
600 | ||
e5b5ae50 | 601 | job_dependency_free(j->object_list); |
1e198baf LP |
602 | |
603 | if (other) { | |
9ea024f6 | 604 | log_debug("Deleting job %s as dependency of job %s", name_id(other->name), name_id(j->name)); |
302d0040 | 605 | transaction_delete_job(m, other); |
1e198baf LP |
606 | } |
607 | } | |
e5b5ae50 LP |
608 | } |
609 | ||
ceed3570 | 610 | static int transaction_add_job_and_dependencies(Manager *m, JobType type, Name *name, Job *by, bool matters, bool force, Job **_ret) { |
e5b5ae50 LP |
611 | Job *ret; |
612 | void *state; | |
613 | Name *dep; | |
614 | int r; | |
615 | bool is_new; | |
616 | ||
617 | assert(m); | |
618 | assert(type < _JOB_TYPE_MAX); | |
619 | assert(name); | |
620 | ||
21b293e8 LP |
621 | if (name->meta.state != NAME_LOADED) |
622 | return -EINVAL; | |
623 | ||
e5b5ae50 | 624 | /* First add the job. */ |
ceed3570 | 625 | if (!(ret = transaction_add_one_job(m, type, name, &is_new))) |
e5b5ae50 LP |
626 | return -ENOMEM; |
627 | ||
628 | /* Then, add a link to the job. */ | |
629 | if (!job_dependency_new(by, ret, matters)) | |
630 | return -ENOMEM; | |
631 | ||
632 | if (is_new) { | |
633 | /* Finally, recursively add in all dependencies. */ | |
634 | if (type == JOB_START || type == JOB_RELOAD_OR_START) { | |
635 | SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRES], state) | |
ceed3570 | 636 | if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, force, NULL)) < 0) |
e5b5ae50 LP |
637 | goto fail; |
638 | SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUIRES], state) | |
ceed3570 | 639 | if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !force, force, NULL)) < 0) |
e5b5ae50 LP |
640 | goto fail; |
641 | SET_FOREACH(dep, ret->name->meta.dependencies[NAME_WANTS], state) | |
ceed3570 | 642 | if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, force, NULL)) < 0) |
e5b5ae50 LP |
643 | goto fail; |
644 | SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUISITE], state) | |
ceed3570 | 645 | if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_STARTED, dep, ret, true, force, NULL)) < 0) |
e5b5ae50 LP |
646 | goto fail; |
647 | SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUISITE], state) | |
ceed3570 | 648 | if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_STARTED, dep, ret, !force, force, NULL)) < 0) |
e5b5ae50 LP |
649 | goto fail; |
650 | SET_FOREACH(dep, ret->name->meta.dependencies[NAME_CONFLICTS], state) | |
ceed3570 | 651 | if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, force, NULL)) < 0) |
e5b5ae50 LP |
652 | goto fail; |
653 | ||
654 | } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) { | |
655 | ||
656 | SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRED_BY], state) | |
ceed3570 | 657 | if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, force, NULL)) < 0) |
e5b5ae50 LP |
658 | goto fail; |
659 | } | |
660 | ||
661 | /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */ | |
662 | } | |
60918275 LP |
663 | |
664 | return 0; | |
665 | ||
666 | fail: | |
e5b5ae50 LP |
667 | return r; |
668 | } | |
669 | ||
670 | int manager_add_job(Manager *m, JobType type, Name *name, JobMode mode, bool force, Job **_ret) { | |
671 | int r; | |
672 | Job *ret; | |
673 | ||
674 | assert(m); | |
675 | assert(type < _JOB_TYPE_MAX); | |
676 | assert(name); | |
677 | assert(mode < _JOB_MODE_MAX); | |
60918275 | 678 | |
ceed3570 | 679 | if ((r = transaction_add_job_and_dependencies(m, type, name, NULL, true, force, &ret))) { |
11dd41ce | 680 | transaction_abort(m); |
e5b5ae50 LP |
681 | return r; |
682 | } | |
11dd41ce | 683 | |
e5b5ae50 LP |
684 | if ((r = transaction_activate(m, mode)) < 0) |
685 | return r; | |
686 | ||
687 | if (_ret) | |
688 | *_ret = ret; | |
60918275 | 689 | |
e5b5ae50 LP |
690 | return 0; |
691 | } | |
60918275 LP |
692 | |
693 | Job *manager_get_job(Manager *m, uint32_t id) { | |
694 | assert(m); | |
695 | ||
696 | return hashmap_get(m->jobs, UINT32_TO_PTR(id)); | |
697 | } | |
698 | ||
699 | Name *manager_get_name(Manager *m, const char *name) { | |
700 | assert(m); | |
701 | assert(name); | |
702 | ||
703 | return hashmap_get(m->names, name); | |
704 | } | |
705 | ||
60918275 LP |
706 | static int dispatch_load_queue(Manager *m) { |
707 | Meta *meta; | |
708 | ||
709 | assert(m); | |
710 | ||
223dabab LP |
711 | /* Make sure we are not run recursively */ |
712 | if (m->dispatching_load_queue) | |
713 | return 0; | |
714 | ||
715 | m->dispatching_load_queue = true; | |
716 | ||
60918275 LP |
717 | /* Dispatches the load queue. Takes a name from the queue and |
718 | * tries to load its data until the queue is empty */ | |
719 | ||
720 | while ((meta = m->load_queue)) { | |
7fad411c | 721 | name_load(NAME(meta)); |
60918275 LP |
722 | LIST_REMOVE(Meta, m->load_queue, meta); |
723 | } | |
724 | ||
223dabab LP |
725 | m->dispatching_load_queue = false; |
726 | ||
60918275 LP |
727 | return 0; |
728 | } | |
729 | ||
60918275 LP |
730 | int manager_load_name(Manager *m, const char *name, Name **_ret) { |
731 | Name *ret; | |
732 | NameType t; | |
733 | int r; | |
87d1515d | 734 | char *n; |
60918275 LP |
735 | |
736 | assert(m); | |
737 | assert(name); | |
738 | assert(_ret); | |
60918275 | 739 | |
223dabab LP |
740 | if (!name_is_valid(name)) |
741 | return -EINVAL; | |
742 | ||
743 | /* This will load the service information files, but not actually | |
744 | * start any services or anything */ | |
60918275 LP |
745 | |
746 | if ((ret = manager_get_name(m, name))) | |
747 | goto finish; | |
748 | ||
749 | if ((t = name_type_from_string(name)) == _NAME_TYPE_INVALID) | |
750 | return -EINVAL; | |
751 | ||
752 | if (!(ret = name_new(m))) | |
753 | return -ENOMEM; | |
754 | ||
755 | ret->meta.type = t; | |
756 | ||
87d1515d LP |
757 | if (!(n = strdup(name))) { |
758 | name_free(ret); | |
759 | return -ENOMEM; | |
760 | } | |
761 | ||
1ffba6fe | 762 | if ((r = set_put(ret->meta.names, n)) < 0) { |
60918275 | 763 | name_free(ret); |
87d1515d | 764 | free(n); |
1ffba6fe | 765 | return r; |
60918275 LP |
766 | } |
767 | ||
768 | if ((r = name_link(ret)) < 0) { | |
769 | name_free(ret); | |
770 | return r; | |
771 | } | |
772 | ||
773 | /* At this point the new entry is created and linked. However, | |
774 | * not loaded. Now load this entry and all its dependencies | |
775 | * recursively */ | |
776 | ||
777 | dispatch_load_queue(m); | |
778 | ||
779 | finish: | |
780 | ||
781 | *_ret = ret; | |
782 | return 0; | |
783 | } | |
a66d02c3 | 784 | |
cea8e32e | 785 | void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) { |
a66d02c3 LP |
786 | void *state; |
787 | Job *j; | |
788 | ||
789 | assert(s); | |
790 | assert(f); | |
791 | ||
792 | HASHMAP_FOREACH(j, s->jobs, state) | |
cea8e32e | 793 | job_dump(j, f, prefix); |
a66d02c3 LP |
794 | } |
795 | ||
cea8e32e | 796 | void manager_dump_names(Manager *s, FILE *f, const char *prefix) { |
a66d02c3 LP |
797 | void *state; |
798 | Name *n; | |
11dd41ce | 799 | const char *t; |
a66d02c3 LP |
800 | |
801 | assert(s); | |
802 | assert(f); | |
803 | ||
11dd41ce LP |
804 | HASHMAP_FOREACH_KEY(n, t, s->names, state) |
805 | if (name_id(n) == t) | |
cea8e32e | 806 | name_dump(n, f, prefix); |
a66d02c3 | 807 | } |
7fad411c LP |
808 | |
809 | void manager_clear_jobs(Manager *m) { | |
810 | Job *j; | |
811 | ||
812 | assert(m); | |
813 | ||
814 | transaction_abort(m); | |
815 | ||
816 | while ((j = hashmap_first(m->jobs))) | |
817 | job_free(j); | |
818 | } |