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