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