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