]> git.ipfire.org Git - thirdparty/systemd.git/blame - manager.c
execute: when running in session mode, still enforce proper ordering of logger socket
[thirdparty/systemd.git] / manager.c
CommitLineData
60918275
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
a7334b09
LP
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
60918275
LP
22#include <assert.h>
23#include <errno.h>
87d1515d 24#include <string.h>
9152c765
LP
25#include <sys/epoll.h>
26#include <signal.h>
27#include <sys/signalfd.h>
28#include <sys/wait.h>
29#include <unistd.h>
30#include <sys/poll.h>
e1414003
LP
31#include <sys/reboot.h>
32#include <sys/ioctl.h>
33#include <linux/kd.h>
60918275
LP
34
35#include "manager.h"
36#include "hashmap.h"
37#include "macro.h"
38#include "strv.h"
16354eff 39#include "log.h"
2a987ee8 40#include "util.h"
ea430986 41#include "ratelimit.h"
60918275 42
ce578209 43static int manager_setup_signals(Manager *m) {
9152c765
LP
44 sigset_t mask;
45 struct epoll_event ev;
60918275 46
ce578209
LP
47 assert(m);
48
49 assert_se(reset_all_signal_handlers() == 0);
50
51 assert_se(sigemptyset(&mask) == 0);
52 assert_se(sigaddset(&mask, SIGCHLD) == 0);
53 assert_se(sigaddset(&mask, SIGINT) == 0); /* Kernel sends us this on control-alt-del */
54 assert_se(sigaddset(&mask, SIGWINCH) == 0); /* Kernel sends us this on kbrequest (alt-arrowup) */
55 assert_se(sigaddset(&mask, SIGTERM) == 0);
56 assert_se(sigaddset(&mask, SIGHUP) == 0);
57 assert_se(sigaddset(&mask, SIGUSR1) == 0);
58 assert_se(sigaddset(&mask, SIGUSR2) == 0);
59 assert_se(sigaddset(&mask, SIGPIPE) == 0);
e1414003
LP
60 assert_se(sigaddset(&mask, SIGPWR) == 0);
61 assert_se(sigaddset(&mask, SIGTTIN) == 0);
ce578209
LP
62 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
63
ef734fd6 64 m->signal_watch.type = WATCH_SIGNAL;
ce578209
LP
65 if ((m->signal_watch.fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0)
66 return -errno;
67
68 zero(ev);
69 ev.events = EPOLLIN;
70 ev.data.ptr = &m->signal_watch;
71
72 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->signal_watch.fd, &ev) < 0)
73 return -errno;
74
e1414003
LP
75 if (m->running_as == MANAGER_INIT) {
76 /* Enable that we get SIGINT on control-alt-del */
77 if (reboot(RB_DISABLE_CAD) < 0)
78 log_warning("Failed to enable ctrl-alt-del handling: %s", strerror(errno));
79
80 /* Enable that we get SIGWINCH on kbrequest */
81 if (ioctl(0, KDSIGACCEPT, SIGWINCH) < 0)
82 log_warning("Failed to enable kbrequest handling: %s", strerror(errno));
83 }
84
ce578209
LP
85 return 0;
86}
87
036643a2
LP
88static char** session_dirs(void) {
89 const char *home, *e;
90 char *config_home = NULL, *data_home = NULL;
91 char **config_dirs = NULL, **data_dirs = NULL;
92 char **r = NULL, **t;
93
94 /* Implement the mechanisms defined in
95 *
96 * http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
97 *
98 * We look in both the config and the data dirs because we
99 * want to encourage that distributors ship their unit files
100 * as data, and allow overriding as configuration.
101 */
102
103 home = getenv("HOME");
104
105 if ((e = getenv("XDG_CONFIG_HOME"))) {
106 if (asprintf(&config_home, "%s/systemd/session", e) < 0)
107 goto fail;
108
109 } else if (home) {
110 if (asprintf(&config_home, "%s/.config/systemd/session", home) < 0)
111 goto fail;
112 }
113
114 if ((e = getenv("XDG_CONFIG_DIRS")))
115 config_dirs = strv_split(e, ":");
116 else
117 config_dirs = strv_new("/etc/xdg", NULL);
118
119 if (!config_dirs)
120 goto fail;
121
122 if ((e = getenv("XDG_DATA_HOME"))) {
123 if (asprintf(&data_home, "%s/systemd/session", e) < 0)
124 goto fail;
125
126 } else if (home) {
127 if (asprintf(&data_home, "%s/.local/share/systemd/session", home) < 0)
128 goto fail;
129 }
130
131 if ((e = getenv("XDG_DATA_DIRS")))
132 data_dirs = strv_split(e, ":");
133 else
134 data_dirs = strv_new("/usr/local/share", "/usr/share", NULL);
135
136 if (!data_dirs)
137 goto fail;
138
139 /* Now merge everything we found. */
140 if (config_home) {
141 if (!(t = strv_append(r, config_home)))
142 goto fail;
143 strv_free(r);
144 r = t;
145 }
146
147 if (!(t = strv_merge_concat(r, config_dirs, "/systemd/session")))
148 goto finish;
149 strv_free(r);
150 r = t;
151
152 if (!(t = strv_append(r, SESSION_CONFIG_UNIT_PATH)))
153 goto fail;
154 strv_free(r);
155 r = t;
156
157 if (data_home) {
158 if (!(t = strv_append(r, data_home)))
159 goto fail;
160 strv_free(r);
161 r = t;
162 }
163
164 if (!(t = strv_merge_concat(r, data_dirs, "/systemd/session")))
165 goto fail;
166 strv_free(r);
167 r = t;
168
169 if (!(t = strv_append(r, SESSION_DATA_UNIT_PATH)))
170 goto fail;
171 strv_free(r);
172 r = t;
173
174 if (!strv_path_make_absolute_cwd(r))
175 goto fail;
176
177finish:
178 free(config_home);
179 strv_free(config_dirs);
180 free(data_home);
181 strv_free(data_dirs);
182
183 return r;
184
185fail:
186 strv_free(r);
187 r = NULL;
188 goto finish;
189}
190
191static int manager_find_paths(Manager *m) {
192 const char *e;
193 char *t;
194 assert(m);
195
196 /* First priority is whatever has been passed to us via env
197 * vars */
198 if ((e = getenv("SYSTEMD_UNIT_PATH")))
199 if (!(m->unit_path = split_path_and_make_absolute(e)))
200 return -ENOMEM;
201
202 if (strv_isempty(m->unit_path)) {
203
204 /* Nothing is set, so let's figure something out. */
205 strv_free(m->unit_path);
206
207 if (m->running_as == MANAGER_SESSION) {
208 if (!(m->unit_path = session_dirs()))
209 return -ENOMEM;
210 } else
211 if (!(m->unit_path = strv_new(
212 SYSTEM_CONFIG_UNIT_PATH, /* /etc/systemd/system/ */
213 SYSTEM_DATA_UNIT_PATH, /* /lib/systemd/system/ */
214 NULL)))
215 return -ENOMEM;
216 }
217
e1414003 218 if (m->running_as == MANAGER_INIT) {
036643a2
LP
219 /* /etc/init.d/ compativility does not matter to users */
220
221 if ((e = getenv("SYSTEMD_SYSVINIT_PATH")))
222 if (!(m->sysvinit_path = split_path_and_make_absolute(e)))
223 return -ENOMEM;
224
225 if (strv_isempty(m->sysvinit_path)) {
226 strv_free(m->sysvinit_path);
227
228 if (!(m->sysvinit_path = strv_new(
229 SYSTEM_SYSVINIT_PATH, /* /etc/init.d/ */
230 NULL)))
231 return -ENOMEM;
232 }
233 }
234
235 strv_uniq(m->unit_path);
236 strv_uniq(m->sysvinit_path);
237
238 assert(!strv_isempty(m->unit_path));
239 if (!(t = strv_join(m->unit_path, "\n\t")))
240 return -ENOMEM;
241 log_debug("Looking for unit files in:\n\t%s", t);
242 free(t);
243
244 if (!strv_isempty(m->sysvinit_path)) {
245
246 if (!(t = strv_join(m->sysvinit_path, "\n\t")))
247 return -ENOMEM;
248
249 log_debug("Looking for SysV init scripts in:\n\t%s", t);
250 free(t);
251 } else
252 log_debug("Ignoring SysV init scripts.");
253
254 return 0;
255}
256
ce578209
LP
257Manager* manager_new(void) {
258 Manager *m;
259
60918275
LP
260 if (!(m = new0(Manager, 1)))
261 return NULL;
262
f94ea366 263 m->signal_watch.fd = m->mount_watch.fd = m->udev_watch.fd = m->epoll_fd = -1;
ea430986 264 m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */
9152c765 265
87f0e418 266 if (!(m->units = hashmap_new(string_hash_func, string_compare_func)))
60918275
LP
267 goto fail;
268
269 if (!(m->jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
270 goto fail;
271
e5b5ae50 272 if (!(m->transaction_jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
60918275
LP
273 goto fail;
274
9152c765
LP
275 if (!(m->watch_pids = hashmap_new(trivial_hash_func, trivial_compare_func)))
276 goto fail;
277
278 if ((m->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0)
279 goto fail;
280
e1414003
LP
281 if (getpid() == 1)
282 m->running_as = MANAGER_INIT;
283 else if (getuid() == 0)
284 m->running_as = MANAGER_SYSTEM;
285 else
286 m->running_as = MANAGER_SESSION;
287
288 log_debug("systemd running in %s mode.", manager_running_as_to_string(m->running_as));
289
290 if (manager_find_paths(m) < 0)
291 goto fail;
292
293 if (chdir("/") < 0)
294 log_warning("Failed to chdir to /: %s", strerror(errno));
295
296 /* Become a session leader if we aren't one yet. */
297 setsid();
298
ce578209 299 if (manager_setup_signals(m) < 0)
9152c765
LP
300 goto fail;
301
ea430986
LP
302 /* FIXME: this should be called only when the D-Bus bus daemon is running */
303 if (bus_init(m) < 0)
304 goto fail;
305
60918275
LP
306 return m;
307
308fail:
309 manager_free(m);
310 return NULL;
311}
312
313void manager_free(Manager *m) {
7824bbeb 314 UnitType c;
87f0e418 315 Unit *u;
e5b5ae50 316 Job *j;
60918275
LP
317
318 assert(m);
319
87f0e418 320 while ((j = hashmap_first(m->transaction_jobs)))
e5b5ae50
LP
321 job_free(j);
322
87f0e418
LP
323 while ((u = hashmap_first(m->units)))
324 unit_free(u);
325
7824bbeb
LP
326 for (c = 0; c < _UNIT_TYPE_MAX; c++)
327 if (unit_vtable[c]->shutdown)
328 unit_vtable[c]->shutdown(m);
329
ea430986
LP
330 bus_done(m);
331
87f0e418 332 hashmap_free(m->units);
60918275 333 hashmap_free(m->jobs);
e5b5ae50 334 hashmap_free(m->transaction_jobs);
9152c765
LP
335 hashmap_free(m->watch_pids);
336
337 if (m->epoll_fd >= 0)
338 close_nointr(m->epoll_fd);
acbb0225
LP
339 if (m->signal_watch.fd >= 0)
340 close_nointr(m->signal_watch.fd);
60918275 341
036643a2
LP
342 strv_free(m->unit_path);
343 strv_free(m->sysvinit_path);
344
60918275
LP
345 free(m);
346}
347
f50e0a01
LP
348int manager_coldplug(Manager *m) {
349 int r;
350 UnitType c;
351 Iterator i;
352 Unit *u;
353 char *k;
354
355 assert(m);
356
357 /* First, let's ask every type to load all units from
358 * disk/kernel that it might know */
359 for (c = 0; c < _UNIT_TYPE_MAX; c++)
360 if (unit_vtable[c]->enumerate)
361 if ((r = unit_vtable[c]->enumerate(m)) < 0)
362 return r;
363
364 manager_dispatch_load_queue(m);
365
366 /* Then, let's set up their initial state. */
367 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
368
369 /* ignore aliases */
370 if (unit_id(u) != k)
371 continue;
372
373 if (UNIT_VTABLE(u)->coldplug)
374 if ((r = UNIT_VTABLE(u)->coldplug(u)) < 0)
375 return r;
376 }
377
378 return 0;
379}
380
302d0040
LP
381static void transaction_delete_job(Manager *m, Job *j) {
382 assert(m);
383 assert(j);
384
1ffba6fe
LP
385 /* Deletes one job from the transaction */
386
302d0040
LP
387 manager_transaction_unlink_job(m, j);
388
ac1135be 389 if (!j->installed)
302d0040
LP
390 job_free(j);
391}
392
87f0e418 393static void transaction_delete_unit(Manager *m, Unit *u) {
1ffba6fe
LP
394 Job *j;
395
87f0e418 396 /* Deletes all jobs associated with a certain unit from the
1ffba6fe
LP
397 * transaction */
398
87f0e418 399 while ((j = hashmap_get(m->transaction_jobs, u)))
1ffba6fe
LP
400 transaction_delete_job(m, j);
401}
402
f04fa1d5
LP
403static void transaction_clean_dependencies(Manager *m) {
404 Iterator i;
405 Job *j;
406
407 assert(m);
408
409 /* Drops all dependencies of all installed jobs */
410
411 HASHMAP_FOREACH(j, m->jobs, i) {
412 while (j->subject_list)
413 job_dependency_free(j->subject_list);
414 while (j->object_list)
415 job_dependency_free(j->object_list);
416 }
417
418 assert(!m->transaction_anchor);
419}
420
11dd41ce
LP
421static void transaction_abort(Manager *m) {
422 Job *j;
423
424 assert(m);
11dd41ce 425
e5b5ae50 426 while ((j = hashmap_first(m->transaction_jobs)))
ac1135be 427 if (j->installed)
302d0040 428 transaction_delete_job(m, j);
e5b5ae50
LP
429 else
430 job_free(j);
431
432 assert(hashmap_isempty(m->transaction_jobs));
f04fa1d5
LP
433
434 transaction_clean_dependencies(m);
e5b5ae50
LP
435}
436
437static void transaction_find_jobs_that_matter_to_anchor(Manager *m, Job *j, unsigned generation) {
438 JobDependency *l;
439
440 assert(m);
441
87f0e418 442 /* A recursive sweep through the graph that marks all units
1ffba6fe
LP
443 * that matter to the anchor job, i.e. are directly or
444 * indirectly a dependency of the anchor job via paths that
445 * are fully marked as mattering. */
446
44d8db9e
LP
447 if (j)
448 l = j->subject_list;
449 else
450 l = m->transaction_anchor;
451
452 LIST_FOREACH(subject, l, l) {
e5b5ae50
LP
453
454 /* This link does not matter */
455 if (!l->matters)
456 continue;
457
87f0e418 458 /* This unit has already been marked */
e5b5ae50
LP
459 if (l->object->generation == generation)
460 continue;
461
462 l->object->matters_to_anchor = true;
463 l->object->generation = generation;
464
465 transaction_find_jobs_that_matter_to_anchor(m, l->object, generation);
466 }
467}
468
7fad411c 469static void transaction_merge_and_delete_job(Manager *m, Job *j, Job *other, JobType t) {
e5b5ae50
LP
470 JobDependency *l, *last;
471
472 assert(j);
473 assert(other);
87f0e418 474 assert(j->unit == other->unit);
ac1135be 475 assert(!j->installed);
e5b5ae50 476
1ffba6fe
LP
477 /* Merges 'other' into 'j' and then deletes j. */
478
e5b5ae50
LP
479 j->type = t;
480 j->state = JOB_WAITING;
5cb5a6ff 481 j->forced = j->forced || other->forced;
e5b5ae50
LP
482
483 j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor;
484
485 /* Patch us in as new owner of the JobDependency objects */
486 last = NULL;
44d8db9e 487 LIST_FOREACH(subject, l, other->subject_list) {
e5b5ae50
LP
488 assert(l->subject == other);
489 l->subject = j;
490 last = l;
491 }
492
493 /* Merge both lists */
494 if (last) {
495 last->subject_next = j->subject_list;
496 if (j->subject_list)
497 j->subject_list->subject_prev = last;
498 j->subject_list = other->subject_list;
499 }
500
501 /* Patch us in as new owner of the JobDependency objects */
502 last = NULL;
44d8db9e 503 LIST_FOREACH(object, l, other->object_list) {
e5b5ae50
LP
504 assert(l->object == other);
505 l->object = j;
506 last = l;
507 }
508
509 /* Merge both lists */
510 if (last) {
511 last->object_next = j->object_list;
512 if (j->object_list)
513 j->object_list->object_prev = last;
514 j->object_list = other->object_list;
515 }
516
e5b5ae50
LP
517 /* Kill the other job */
518 other->subject_list = NULL;
519 other->object_list = NULL;
302d0040 520 transaction_delete_job(m, other);
e5b5ae50
LP
521}
522
5cb5a6ff 523static int delete_one_unmergeable_job(Manager *m, Job *j) {
1ffba6fe
LP
524 Job *k;
525
526 assert(j);
527
528 /* Tries to delete one item in the linked list
529 * j->transaction_next->transaction_next->... that conflicts
530 * whith another one, in an attempt to make an inconsistent
531 * transaction work. */
532
533 /* We rely here on the fact that if a merged with b does not
534 * merge with c, either a or b merge with c neither */
034c6ed7
LP
535 LIST_FOREACH(transaction, j, j)
536 LIST_FOREACH(transaction, k, j->transaction_next) {
1ffba6fe
LP
537 Job *d;
538
539 /* Is this one mergeable? Then skip it */
5cb5a6ff 540 if (job_type_is_mergeable(j->type, k->type))
1ffba6fe
LP
541 continue;
542
543 /* Ok, we found two that conflict, let's see if we can
544 * drop one of them */
545 if (!j->matters_to_anchor)
546 d = j;
547 else if (!k->matters_to_anchor)
548 d = k;
549 else
550 return -ENOEXEC;
551
552 /* Ok, we can drop one, so let's do so. */
87f0e418 553 log_debug("Try to fix job merging by deleting job %s/%s", unit_id(d->unit), job_type_to_string(d->type));
1ffba6fe
LP
554 transaction_delete_job(m, d);
555 return 0;
556 }
557
558 return -EINVAL;
559}
560
e5b5ae50 561static int transaction_merge_jobs(Manager *m) {
11dd41ce 562 Job *j;
034c6ed7 563 Iterator i;
e5b5ae50
LP
564 int r;
565
566 assert(m);
567
1ffba6fe
LP
568 /* First step, check whether any of the jobs for one specific
569 * task conflict. If so, try to drop one of them. */
034c6ed7 570 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
1ffba6fe
LP
571 JobType t;
572 Job *k;
573
574 t = j->type;
034c6ed7 575 LIST_FOREACH(transaction, k, j->transaction_next) {
1ffba6fe
LP
576 if ((r = job_type_merge(&t, k->type)) >= 0)
577 continue;
578
579 /* OK, we could not merge all jobs for this
580 * action. Let's see if we can get rid of one
581 * of them */
582
5cb5a6ff 583 if ((r = delete_one_unmergeable_job(m, j)) >= 0)
1ffba6fe
LP
584 /* Ok, we managed to drop one, now
585 * let's ask our callers to call us
586 * again after garbage collecting */
587 return -EAGAIN;
588
589 /* We couldn't merge anything. Failure */
590 return r;
591 }
592 }
593
594 /* Second step, merge the jobs. */
034c6ed7 595 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
e5b5ae50
LP
596 JobType t = j->type;
597 Job *k;
598
e094e853 599 /* Merge all transactions */
034c6ed7 600 LIST_FOREACH(transaction, k, j->transaction_next)
1ffba6fe 601 assert_se(job_type_merge(&t, k->type) == 0);
e5b5ae50 602
5cb5a6ff 603 /* If an active job is mergeable, merge it too */
87f0e418
LP
604 if (j->unit->meta.job)
605 job_type_merge(&t, j->unit->meta.job->type); /* Might fail. Which is OK */
e094e853 606
e5b5ae50 607 while ((k = j->transaction_next)) {
ac1135be 608 if (j->installed) {
7fad411c 609 transaction_merge_and_delete_job(m, k, j, t);
e5b5ae50
LP
610 j = k;
611 } else
7fad411c 612 transaction_merge_and_delete_job(m, j, k, t);
e5b5ae50
LP
613 }
614
615 assert(!j->transaction_next);
616 assert(!j->transaction_prev);
617 }
618
7fad411c 619 return 0;
e5b5ae50
LP
620}
621
87f0e418
LP
622static bool unit_matters_to_anchor(Unit *u, Job *j) {
623 assert(u);
1ffba6fe
LP
624 assert(!j->transaction_prev);
625
87f0e418 626 /* Checks whether at least one of the jobs for this unit
1ffba6fe
LP
627 * matters to the anchor. */
628
034c6ed7 629 LIST_FOREACH(transaction, j, j)
1ffba6fe
LP
630 if (j->matters_to_anchor)
631 return true;
632
633 return false;
634}
635
e5b5ae50 636static int transaction_verify_order_one(Manager *m, Job *j, Job *from, unsigned generation) {
034c6ed7 637 Iterator i;
87f0e418 638 Unit *u;
11dd41ce 639 int r;
e5b5ae50
LP
640
641 assert(m);
642 assert(j);
1ffba6fe
LP
643 assert(!j->transaction_prev);
644
645 /* Does a recursive sweep through the ordering graph, looking
646 * for a cycle. If we find cycle we try to break it. */
e5b5ae50 647
7fad411c 648 /* Did we find a cycle? */
e5b5ae50
LP
649 if (j->marker && j->generation == generation) {
650 Job *k;
651
652 /* So, we already have been here. We have a
1ffba6fe
LP
653 * cycle. Let's try to break it. We go backwards in
654 * our path and try to find a suitable job to
655 * remove. We use the marker to find our way back,
656 * since smart how we are we stored our way back in
657 * there. */
e5b5ae50 658
9f04bd52
LP
659 log_debug("Found cycle on %s/%s", unit_id(j->unit), job_type_to_string(j->type));
660
e5b5ae50 661 for (k = from; k; k = (k->generation == generation ? k->marker : NULL)) {
1ffba6fe 662
9f04bd52
LP
663 log_debug("Walked on cycle path to %s/%s", unit_id(j->unit), job_type_to_string(j->type));
664
ac1135be 665 if (!k->installed &&
87f0e418 666 !unit_matters_to_anchor(k->unit, k)) {
1ffba6fe
LP
667 /* Ok, we can drop this one, so let's
668 * do so. */
87f0e418
LP
669 log_debug("Breaking order cycle by deleting job %s/%s", unit_id(k->unit), job_type_to_string(k->type));
670 transaction_delete_unit(m, k->unit);
e5b5ae50
LP
671 return -EAGAIN;
672 }
673
674 /* Check if this in fact was the beginning of
7fad411c 675 * the cycle */
e5b5ae50
LP
676 if (k == j)
677 break;
678 }
679
9f04bd52
LP
680 log_debug("Unable to break cycle");
681
1ffba6fe 682 return -ENOEXEC;
e5b5ae50
LP
683 }
684
1ffba6fe
LP
685 /* Make the marker point to where we come from, so that we can
686 * find our way backwards if we want to break a cycle */
e5b5ae50
LP
687 j->marker = from;
688 j->generation = generation;
689
1ffba6fe 690 /* We assume that the the dependencies are bidirectional, and
87f0e418
LP
691 * hence can ignore UNIT_AFTER */
692 SET_FOREACH(u, j->unit->meta.dependencies[UNIT_BEFORE], i) {
e5b5ae50
LP
693 Job *o;
694
87f0e418
LP
695 /* Is there a job for this unit? */
696 if (!(o = hashmap_get(m->transaction_jobs, u)))
1ffba6fe
LP
697
698 /* Ok, there is no job for this in the
699 * transaction, but maybe there is already one
700 * running? */
87f0e418 701 if (!(o = u->meta.job))
e5b5ae50
LP
702 continue;
703
704 if ((r = transaction_verify_order_one(m, o, j, generation)) < 0)
705 return r;
706 }
707
9f04bd52
LP
708 /* Ok, let's backtrack, and remember that this entry is not on
709 * our path anymore. */
710 j->marker = NULL;
711
e5b5ae50
LP
712 return 0;
713}
714
715static int transaction_verify_order(Manager *m, unsigned *generation) {
1ffba6fe
LP
716 Job *j;
717 int r;
034c6ed7 718 Iterator i;
1ffba6fe 719
e5b5ae50
LP
720 assert(m);
721 assert(generation);
722
1ffba6fe
LP
723 /* Check if the ordering graph is cyclic. If it is, try to fix
724 * that up by dropping one of the jobs. */
e5b5ae50 725
034c6ed7 726 HASHMAP_FOREACH(j, m->transaction_jobs, i)
1ffba6fe
LP
727 if ((r = transaction_verify_order_one(m, j, NULL, (*generation)++)) < 0)
728 return r;
e5b5ae50
LP
729
730 return 0;
731}
732
733static void transaction_collect_garbage(Manager *m) {
734 bool again;
735
736 assert(m);
737
1ffba6fe
LP
738 /* Drop jobs that are not required by any other job */
739
e5b5ae50 740 do {
034c6ed7 741 Iterator i;
e5b5ae50
LP
742 Job *j;
743
744 again = false;
745
034c6ed7 746 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
e5b5ae50
LP
747 if (j->object_list)
748 continue;
749
87f0e418 750 log_debug("Garbage collecting job %s/%s", unit_id(j->unit), job_type_to_string(j->type));
302d0040 751 transaction_delete_job(m, j);
e5b5ae50
LP
752 again = true;
753 break;
754 }
755
756 } while (again);
757}
758
759static int transaction_is_destructive(Manager *m, JobMode mode) {
034c6ed7 760 Iterator i;
e5b5ae50 761 Job *j;
11dd41ce
LP
762
763 assert(m);
11dd41ce 764
e5b5ae50
LP
765 /* Checks whether applying this transaction means that
766 * existing jobs would be replaced */
11dd41ce 767
034c6ed7 768 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
e094e853
LP
769
770 /* Assume merged */
771 assert(!j->transaction_prev);
772 assert(!j->transaction_next);
773
87f0e418
LP
774 if (j->unit->meta.job &&
775 j->unit->meta.job != j &&
776 !job_type_is_superset(j->type, j->unit->meta.job->type))
e5b5ae50 777 return -EEXIST;
e094e853 778 }
11dd41ce 779
e5b5ae50
LP
780 return 0;
781}
782
e094e853
LP
783static void transaction_minimize_impact(Manager *m) {
784 bool again;
785 assert(m);
786
787 /* Drops all unnecessary jobs that reverse already active jobs
788 * or that stop a running service. */
789
790 do {
791 Job *j;
034c6ed7 792 Iterator i;
e094e853
LP
793
794 again = false;
795
034c6ed7
LP
796 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
797 LIST_FOREACH(transaction, j, j) {
c20cae32 798 bool stops_running_service, changes_existing_job;
e094e853
LP
799
800 /* If it matters, we shouldn't drop it */
801 if (j->matters_to_anchor)
802 continue;
803
804 /* Would this stop a running service?
805 * Would this change an existing job?
806 * If so, let's drop this entry */
c20cae32
LP
807
808 stops_running_service =
809 j->type == JOB_STOP && UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j->unit));
810
811 changes_existing_job =
812 j->unit->meta.job && job_type_is_conflicting(j->type, j->unit->meta.job->state);
813
814 if (!stops_running_service && !changes_existing_job)
e094e853
LP
815 continue;
816
c20cae32
LP
817 if (stops_running_service)
818 log_debug("%s/%s would stop a running service.", unit_id(j->unit), job_type_to_string(j->type));
819
820 if (changes_existing_job)
821 log_debug("%s/%s would change existing job.", unit_id(j->unit), job_type_to_string(j->type));
822
e094e853 823 /* Ok, let's get rid of this */
c20cae32
LP
824 log_debug("Deleting %s/%s to minimize impact.", unit_id(j->unit), job_type_to_string(j->type));
825
e094e853
LP
826 transaction_delete_job(m, j);
827 again = true;
828 break;
829 }
830
831 if (again)
832 break;
833 }
834
835 } while (again);
836}
837
e5b5ae50 838static int transaction_apply(Manager *m, JobMode mode) {
034c6ed7 839 Iterator i;
e5b5ae50
LP
840 Job *j;
841 int r;
842
1ffba6fe
LP
843 /* Moves the transaction jobs to the set of active jobs */
844
034c6ed7 845 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
e094e853
LP
846 /* Assume merged */
847 assert(!j->transaction_prev);
848 assert(!j->transaction_next);
849
ac1135be 850 if (j->installed)
e5b5ae50
LP
851 continue;
852
853 if ((r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j)) < 0)
11dd41ce
LP
854 goto rollback;
855 }
856
e5b5ae50 857 while ((j = hashmap_steal_first(m->transaction_jobs))) {
ac1135be 858 if (j->installed)
e5b5ae50
LP
859 continue;
860
87f0e418
LP
861 if (j->unit->meta.job)
862 job_free(j->unit->meta.job);
11dd41ce 863
87f0e418 864 j->unit->meta.job = j;
ac1135be 865 j->installed = true;
11dd41ce 866
e5b5ae50
LP
867 /* We're fully installed. Now let's free data we don't
868 * need anymore. */
869
870 assert(!j->transaction_next);
871 assert(!j->transaction_prev);
872
c1e1601e
LP
873 job_add_to_run_queue(j);
874 job_add_to_dbus_queue(j);
01184e04
LP
875 }
876
877 /* As last step, kill all remaining job dependencies. */
f04fa1d5 878 transaction_clean_dependencies(m);
1ffba6fe 879
11dd41ce
LP
880 return 0;
881
882rollback:
883
034c6ed7 884 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
ac1135be 885 if (j->installed)
e5b5ae50
LP
886 continue;
887
888 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
889 }
890
891 return r;
892}
893
e5b5ae50
LP
894static int transaction_activate(Manager *m, JobMode mode) {
895 int r;
896 unsigned generation = 1;
897
898 assert(m);
899
900 /* This applies the changes recorded in transaction_jobs to
901 * the actual list of jobs, if possible. */
902
903 /* First step: figure out which jobs matter */
904 transaction_find_jobs_that_matter_to_anchor(m, NULL, generation++);
905
e094e853
LP
906 /* Second step: Try not to stop any running services if
907 * we don't have to. Don't try to reverse running
908 * jobs if we don't have to. */
909 transaction_minimize_impact(m);
910
1ffba6fe 911 for (;;) {
e094e853 912 /* Third step: Let's remove unneeded jobs that might
1ffba6fe
LP
913 * be lurking. */
914 transaction_collect_garbage(m);
e5b5ae50 915
e094e853 916 /* Fourth step: verify order makes sense and correct
1ffba6fe
LP
917 * cycles if necessary and possible */
918 if ((r = transaction_verify_order(m, &generation)) >= 0)
919 break;
e5b5ae50 920
9f04bd52
LP
921 if (r != -EAGAIN) {
922 log_debug("Requested transaction contains an unfixable cyclic ordering dependency: %s", strerror(-r));
1ffba6fe 923 goto rollback;
9f04bd52 924 }
e5b5ae50 925
1ffba6fe
LP
926 /* Let's see if the resulting transaction ordering
927 * graph is still cyclic... */
928 }
929
930 for (;;) {
5cb5a6ff 931 /* Fifth step: let's drop unmergeable entries if
1ffba6fe
LP
932 * necessary and possible, merge entries we can
933 * merge */
934 if ((r = transaction_merge_jobs(m)) >= 0)
935 break;
936
9f04bd52
LP
937 if (r != -EAGAIN) {
938 log_debug("Requested transaction contains unmergable jobs: %s", strerror(-r));
1ffba6fe 939 goto rollback;
9f04bd52 940 }
1ffba6fe 941
e094e853 942 /* Sixth step: an entry got dropped, let's garbage
1ffba6fe
LP
943 * collect its dependencies. */
944 transaction_collect_garbage(m);
945
946 /* Let's see if the resulting transaction still has
5cb5a6ff 947 * unmergeable entries ... */
1ffba6fe
LP
948 }
949
e094e853 950 /* Seventh step: check whether we can actually apply this */
e5b5ae50 951 if (mode == JOB_FAIL)
9f04bd52
LP
952 if ((r = transaction_is_destructive(m, mode)) < 0) {
953 log_debug("Requested transaction contradicts existing jobs: %s", strerror(-r));
e5b5ae50 954 goto rollback;
9f04bd52 955 }
e5b5ae50 956
e094e853 957 /* Eights step: apply changes */
9f04bd52
LP
958 if ((r = transaction_apply(m, mode)) < 0) {
959 log_debug("Failed to apply transaction: %s", strerror(-r));
e5b5ae50 960 goto rollback;
9f04bd52 961 }
e5b5ae50
LP
962
963 assert(hashmap_isempty(m->transaction_jobs));
964 assert(!m->transaction_anchor);
965
966 return 0;
11dd41ce 967
e5b5ae50 968rollback:
11dd41ce
LP
969 transaction_abort(m);
970 return r;
971}
972
87f0e418 973static Job* transaction_add_one_job(Manager *m, JobType type, Unit *unit, bool force, bool *is_new) {
e5b5ae50 974 Job *j, *f;
60918275
LP
975 int r;
976
977 assert(m);
87f0e418 978 assert(unit);
60918275 979
e5b5ae50
LP
980 /* Looks for an axisting prospective job and returns that. If
981 * it doesn't exist it is created and added to the prospective
982 * jobs list. */
60918275 983
87f0e418 984 f = hashmap_get(m->transaction_jobs, unit);
60918275 985
034c6ed7 986 LIST_FOREACH(transaction, j, f) {
87f0e418 987 assert(j->unit == unit);
60918275 988
e5b5ae50
LP
989 if (j->type == type) {
990 if (is_new)
991 *is_new = false;
992 return j;
993 }
994 }
60918275 995
87f0e418
LP
996 if (unit->meta.job && unit->meta.job->type == type)
997 j = unit->meta.job;
998 else if (!(j = job_new(m, type, unit)))
e5b5ae50 999 return NULL;
60918275 1000
e5b5ae50
LP
1001 j->generation = 0;
1002 j->marker = NULL;
1003 j->matters_to_anchor = false;
5cb5a6ff 1004 j->forced = force;
60918275 1005
034c6ed7
LP
1006 LIST_PREPEND(Job, transaction, f, j);
1007
87f0e418 1008 if ((r = hashmap_replace(m->transaction_jobs, unit, f)) < 0) {
034c6ed7
LP
1009 job_free(j);
1010 return NULL;
1011 }
1012
e5b5ae50
LP
1013 if (is_new)
1014 *is_new = true;
60918275 1015
e5b5ae50
LP
1016 return j;
1017}
11dd41ce 1018
302d0040 1019void manager_transaction_unlink_job(Manager *m, Job *j) {
e5b5ae50
LP
1020 assert(m);
1021 assert(j);
11dd41ce 1022
e5b5ae50
LP
1023 if (j->transaction_prev)
1024 j->transaction_prev->transaction_next = j->transaction_next;
1025 else if (j->transaction_next)
87f0e418 1026 hashmap_replace(m->transaction_jobs, j->unit, j->transaction_next);
e5b5ae50 1027 else
87f0e418 1028 hashmap_remove_value(m->transaction_jobs, j->unit, j);
e5b5ae50
LP
1029
1030 if (j->transaction_next)
1031 j->transaction_next->transaction_prev = j->transaction_prev;
1032
1033 j->transaction_prev = j->transaction_next = NULL;
1034
1035 while (j->subject_list)
1036 job_dependency_free(j->subject_list);
1e198baf
LP
1037
1038 while (j->object_list) {
1039 Job *other = j->object_list->matters ? j->object_list->subject : NULL;
1040
e5b5ae50 1041 job_dependency_free(j->object_list);
1e198baf
LP
1042
1043 if (other) {
5cb5a6ff 1044 log_debug("Deleting job %s/%s as dependency of job %s/%s",
87f0e418
LP
1045 unit_id(other->unit), job_type_to_string(other->type),
1046 unit_id(j->unit), job_type_to_string(j->type));
302d0040 1047 transaction_delete_job(m, other);
1e198baf
LP
1048 }
1049 }
e5b5ae50
LP
1050}
1051
87f0e418 1052static int transaction_add_job_and_dependencies(Manager *m, JobType type, Unit *unit, Job *by, bool matters, bool force, Job **_ret) {
e5b5ae50 1053 Job *ret;
034c6ed7 1054 Iterator i;
87f0e418 1055 Unit *dep;
e5b5ae50
LP
1056 int r;
1057 bool is_new;
1058
1059 assert(m);
1060 assert(type < _JOB_TYPE_MAX);
87f0e418 1061 assert(unit);
e5b5ae50 1062
87f0e418 1063 if (unit->meta.load_state != UNIT_LOADED)
21b293e8
LP
1064 return -EINVAL;
1065
87f0e418 1066 if (!unit_job_is_applicable(unit, type))
cd2dbd7d
LP
1067 return -EBADR;
1068
e5b5ae50 1069 /* First add the job. */
87f0e418 1070 if (!(ret = transaction_add_one_job(m, type, unit, force, &is_new)))
e5b5ae50
LP
1071 return -ENOMEM;
1072
1073 /* Then, add a link to the job. */
1074 if (!job_dependency_new(by, ret, matters))
1075 return -ENOMEM;
1076
1077 if (is_new) {
1078 /* Finally, recursively add in all dependencies. */
1079 if (type == JOB_START || type == JOB_RELOAD_OR_START) {
87f0e418 1080 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRES], i)
542563ba 1081 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, force, NULL)) < 0 && r != -EBADR)
e5b5ae50 1082 goto fail;
87f0e418 1083 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_SOFT_REQUIRES], i)
542563ba 1084 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !force, force, NULL)) < 0 && r != -EBADR)
e5b5ae50 1085 goto fail;
87f0e418 1086 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_WANTS], i)
542563ba 1087 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, force, NULL)) < 0 && r != -EBADR)
e5b5ae50 1088 goto fail;
87f0e418 1089 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUISITE], i)
542563ba 1090 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, true, force, NULL)) < 0 && r != -EBADR)
e5b5ae50 1091 goto fail;
87f0e418 1092 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_SOFT_REQUISITE], i)
542563ba 1093 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, !force, force, NULL)) < 0 && r != -EBADR)
e5b5ae50 1094 goto fail;
87f0e418 1095 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_CONFLICTS], i)
542563ba 1096 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, force, NULL)) < 0 && r != -EBADR)
e5b5ae50
LP
1097 goto fail;
1098
1099 } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
1100
87f0e418 1101 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRED_BY], i)
542563ba 1102 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, force, NULL)) < 0 && r != -EBADR)
e5b5ae50
LP
1103 goto fail;
1104 }
1105
1106 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
1107 }
60918275 1108
c0dafa48
LP
1109 if (_ret)
1110 *_ret = ret;
1111
60918275
LP
1112 return 0;
1113
1114fail:
e5b5ae50
LP
1115 return r;
1116}
1117
87f0e418 1118int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool force, Job **_ret) {
e5b5ae50
LP
1119 int r;
1120 Job *ret;
1121
1122 assert(m);
1123 assert(type < _JOB_TYPE_MAX);
87f0e418 1124 assert(unit);
e5b5ae50 1125 assert(mode < _JOB_MODE_MAX);
60918275 1126
9f04bd52
LP
1127 log_debug("Trying to enqueue job %s/%s", unit_id(unit), job_type_to_string(type));
1128
c0dafa48 1129 if ((r = transaction_add_job_and_dependencies(m, type, unit, NULL, true, force, &ret)) < 0) {
11dd41ce 1130 transaction_abort(m);
e5b5ae50
LP
1131 return r;
1132 }
11dd41ce 1133
e5b5ae50
LP
1134 if ((r = transaction_activate(m, mode)) < 0)
1135 return r;
1136
c0dafa48 1137 log_debug("Enqueued job %s/%s as %u", unit_id(unit), job_type_to_string(type), (unsigned) ret->id);
f50e0a01 1138
e5b5ae50
LP
1139 if (_ret)
1140 *_ret = ret;
60918275 1141
e5b5ae50
LP
1142 return 0;
1143}
60918275
LP
1144
1145Job *manager_get_job(Manager *m, uint32_t id) {
1146 assert(m);
1147
1148 return hashmap_get(m->jobs, UINT32_TO_PTR(id));
1149}
1150
87f0e418 1151Unit *manager_get_unit(Manager *m, const char *name) {
60918275
LP
1152 assert(m);
1153 assert(name);
1154
87f0e418 1155 return hashmap_get(m->units, name);
60918275
LP
1156}
1157
c1e1601e 1158unsigned manager_dispatch_load_queue(Manager *m) {
60918275 1159 Meta *meta;
c1e1601e 1160 unsigned n = 0;
60918275
LP
1161
1162 assert(m);
1163
223dabab
LP
1164 /* Make sure we are not run recursively */
1165 if (m->dispatching_load_queue)
c1e1601e 1166 return 0;
223dabab
LP
1167
1168 m->dispatching_load_queue = true;
1169
87f0e418 1170 /* Dispatches the load queue. Takes a unit from the queue and
60918275
LP
1171 * tries to load its data until the queue is empty */
1172
1173 while ((meta = m->load_queue)) {
034c6ed7
LP
1174 assert(meta->in_load_queue);
1175
87f0e418 1176 unit_load(UNIT(meta));
c1e1601e 1177 n++;
60918275
LP
1178 }
1179
223dabab 1180 m->dispatching_load_queue = false;
c1e1601e 1181 return n;
60918275
LP
1182}
1183
0301abf4 1184int manager_load_unit(Manager *m, const char *path, Unit **_ret) {
87f0e418 1185 Unit *ret;
60918275 1186 int r;
0301abf4 1187 const char *name;
60918275
LP
1188
1189 assert(m);
0301abf4 1190 assert(path);
60918275 1191 assert(_ret);
60918275 1192
223dabab 1193 /* This will load the service information files, but not actually
0301abf4
LP
1194 * start any services or anything. */
1195
1196 name = file_name_from_path(path);
60918275 1197
87f0e418 1198 if ((ret = manager_get_unit(m, name))) {
034c6ed7
LP
1199 *_ret = ret;
1200 return 0;
1201 }
60918275 1202
87f0e418 1203 if (!(ret = unit_new(m)))
60918275
LP
1204 return -ENOMEM;
1205
0301abf4 1206 if (is_path(path)) {
6be1e7d5 1207 if (!(ret->meta.fragment_path = strdup(path))) {
0301abf4
LP
1208 unit_free(ret);
1209 return -ENOMEM;
1210 }
1211 }
1212
87f0e418
LP
1213 if ((r = unit_add_name(ret, name)) < 0) {
1214 unit_free(ret);
1ffba6fe 1215 return r;
60918275
LP
1216 }
1217
87f0e418 1218 unit_add_to_load_queue(ret);
c1e1601e
LP
1219 unit_add_to_dbus_queue(ret);
1220
f50e0a01 1221 manager_dispatch_load_queue(m);
60918275 1222
60918275
LP
1223 *_ret = ret;
1224 return 0;
1225}
a66d02c3 1226
cea8e32e 1227void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) {
034c6ed7 1228 Iterator i;
a66d02c3
LP
1229 Job *j;
1230
1231 assert(s);
1232 assert(f);
1233
034c6ed7 1234 HASHMAP_FOREACH(j, s->jobs, i)
cea8e32e 1235 job_dump(j, f, prefix);
a66d02c3
LP
1236}
1237
87f0e418 1238void manager_dump_units(Manager *s, FILE *f, const char *prefix) {
034c6ed7 1239 Iterator i;
87f0e418 1240 Unit *u;
11dd41ce 1241 const char *t;
a66d02c3
LP
1242
1243 assert(s);
1244 assert(f);
1245
87f0e418
LP
1246 HASHMAP_FOREACH_KEY(u, t, s->units, i)
1247 if (unit_id(u) == t)
1248 unit_dump(u, f, prefix);
a66d02c3 1249}
7fad411c
LP
1250
1251void manager_clear_jobs(Manager *m) {
1252 Job *j;
1253
1254 assert(m);
1255
1256 transaction_abort(m);
1257
1258 while ((j = hashmap_first(m->jobs)))
1259 job_free(j);
1260}
83c60c9f 1261
c1e1601e 1262unsigned manager_dispatch_run_queue(Manager *m) {
83c60c9f 1263 Job *j;
c1e1601e 1264 unsigned n = 0;
83c60c9f 1265
034c6ed7 1266 if (m->dispatching_run_queue)
c1e1601e 1267 return 0;
034c6ed7
LP
1268
1269 m->dispatching_run_queue = true;
9152c765 1270
034c6ed7 1271 while ((j = m->run_queue)) {
ac1135be 1272 assert(j->installed);
034c6ed7
LP
1273 assert(j->in_run_queue);
1274
1275 job_run_and_invalidate(j);
c1e1601e 1276 n++;
9152c765 1277 }
034c6ed7
LP
1278
1279 m->dispatching_run_queue = false;
c1e1601e
LP
1280 return n;
1281}
1282
1283unsigned manager_dispatch_dbus_queue(Manager *m) {
1284 Job *j;
1285 Meta *meta;
1286 unsigned n = 0;
1287
1288 assert(m);
1289
1290 if (m->dispatching_dbus_queue)
1291 return 0;
1292
1293 m->dispatching_dbus_queue = true;
1294
1295 while ((meta = m->dbus_unit_queue)) {
1296 Unit *u = (Unit*) meta;
1297 assert(u->meta.in_dbus_queue);
1298
1299 bus_unit_send_change_signal(u);
1300 n++;
1301 }
1302
1303 while ((j = m->dbus_job_queue)) {
1304 assert(j->in_dbus_queue);
1305
1306 bus_job_send_change_signal(j);
1307 n++;
1308 }
1309
1310 m->dispatching_dbus_queue = false;
1311 return n;
9152c765
LP
1312}
1313
034c6ed7 1314static int manager_dispatch_sigchld(Manager *m) {
9152c765
LP
1315 assert(m);
1316
acbb0225
LP
1317 log_debug("dispatching SIGCHLD");
1318
9152c765
LP
1319 for (;;) {
1320 siginfo_t si;
87f0e418 1321 Unit *u;
9152c765
LP
1322
1323 zero(si);
acbb0225
LP
1324 if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG) < 0) {
1325
1326 if (errno == ECHILD)
1327 break;
1328
9152c765 1329 return -errno;
acbb0225 1330 }
9152c765
LP
1331
1332 if (si.si_pid == 0)
1333 break;
1334
034c6ed7
LP
1335 if (si.si_code != CLD_EXITED && si.si_code != CLD_KILLED && si.si_code != CLD_DUMPED)
1336 continue;
1337
94f04347 1338 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 1339
87f0e418 1340 if (!(u = hashmap_remove(m->watch_pids, UINT32_TO_PTR(si.si_pid))))
9152c765
LP
1341 continue;
1342
87f0e418 1343 UNIT_VTABLE(u)->sigchld_event(u, si.si_pid, si.si_code, si.si_status);
9152c765
LP
1344 }
1345
1346 return 0;
1347}
1348
b9cd2ec1 1349static int manager_process_signal_fd(Manager *m, bool *quit) {
9152c765
LP
1350 ssize_t n;
1351 struct signalfd_siginfo sfsi;
1352 bool sigchld = false;
1353
1354 assert(m);
1355
1356 for (;;) {
acbb0225 1357 if ((n = read(m->signal_watch.fd, &sfsi, sizeof(sfsi))) != sizeof(sfsi)) {
9152c765
LP
1358
1359 if (n >= 0)
1360 return -EIO;
1361
1362 if (errno == EAGAIN)
acbb0225 1363 break;
9152c765
LP
1364
1365 return -errno;
1366 }
1367
b9cd2ec1
LP
1368 switch (sfsi.ssi_signo) {
1369
1370 case SIGCHLD:
9152c765 1371 sigchld = true;
b9cd2ec1
LP
1372 break;
1373
1374 case SIGINT:
6632c602 1375 case SIGTERM:
84e9af1e
LP
1376
1377 if (m->running_as != MANAGER_INIT) {
1378 *quit = true;
1379 return 0;
1380
1381 } else {
1382 Unit *target;
1383 int r;
1384
1385 if ((r = manager_load_unit(m, SPECIAL_CTRL_ALT_DEL_TARGET, &target)) < 0)
1386 log_error("Failed to load ctrl-alt-del target: %s", strerror(-r));
1387 else if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, true, NULL)) < 0)
1388 log_error("Failed to enqueue ctrl-alt-del job: %s", strerror(-r));
1389
1390 break;
1391 }
1392
1393 case SIGWINCH:
1394
1395 if (m->running_as == MANAGER_INIT) {
1396 Unit *target;
1397 int r;
1398
1399 if ((r = manager_load_unit(m, SPECIAL_KBREQUEST_TARGET, &target)) < 0)
1400 log_error("Failed to load kbrequest target: %s", strerror(-r));
1401 else if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, true, NULL)) < 0)
1402 log_error("Failed to enqueue kbrequest job: %s", strerror(-r));
1403
1404 break;
1405 }
1406
1407 /* This is a nop on non-init systemd's */
1408
1409 break;
6632c602
LP
1410
1411 default:
1412 log_info("Got unhandled signal <%s>.", strsignal(sfsi.ssi_signo));
b9cd2ec1 1413 }
9152c765
LP
1414 }
1415
1416 if (sigchld)
034c6ed7
LP
1417 return manager_dispatch_sigchld(m);
1418
1419 return 0;
1420}
1421
b9cd2ec1 1422static int process_event(Manager *m, struct epoll_event *ev, bool *quit) {
034c6ed7 1423 int r;
acbb0225 1424 Watch *w;
034c6ed7
LP
1425
1426 assert(m);
1427 assert(ev);
1428
acbb0225 1429 assert(w = ev->data.ptr);
034c6ed7 1430
acbb0225 1431 switch (w->type) {
034c6ed7 1432
ef734fd6 1433 case WATCH_SIGNAL:
034c6ed7 1434
acbb0225 1435 /* An incoming signal? */
f94ea366 1436 if (ev->events != EPOLLIN)
acbb0225 1437 return -EINVAL;
034c6ed7 1438
b9cd2ec1 1439 if ((r = manager_process_signal_fd(m, quit)) < 0)
acbb0225 1440 return r;
034c6ed7 1441
acbb0225 1442 break;
034c6ed7 1443
acbb0225 1444 case WATCH_FD:
034c6ed7 1445
acbb0225 1446 /* Some fd event, to be dispatched to the units */
ea430986 1447 UNIT_VTABLE(w->data.unit)->fd_event(w->data.unit, w->fd, ev->events, w);
acbb0225 1448 break;
034c6ed7 1449
acbb0225
LP
1450 case WATCH_TIMER: {
1451 uint64_t v;
1452 ssize_t k;
034c6ed7 1453
acbb0225 1454 /* Some timer event, to be dispatched to the units */
be888ebb 1455 if ((k = read(w->fd, &v, sizeof(v))) != sizeof(v)) {
034c6ed7 1456
acbb0225
LP
1457 if (k < 0 && (errno == EINTR || errno == EAGAIN))
1458 break;
034c6ed7 1459
acbb0225 1460 return k < 0 ? -errno : -EIO;
034c6ed7
LP
1461 }
1462
ea430986 1463 UNIT_VTABLE(w->data.unit)->timer_event(w->data.unit, v, w);
acbb0225
LP
1464 break;
1465 }
1466
ef734fd6
LP
1467 case WATCH_MOUNT:
1468 /* Some mount table change, intended for the mount subsystem */
1469 mount_fd_event(m, ev->events);
1470 break;
1471
f94ea366
LP
1472 case WATCH_UDEV:
1473 /* Some notification from udev, intended for the device subsystem */
1474 device_fd_event(m, ev->events);
1475 break;
1476
ea430986
LP
1477 case WATCH_DBUS_WATCH:
1478 bus_watch_event(m, w, ev->events);
1479 break;
1480
1481 case WATCH_DBUS_TIMEOUT:
1482 bus_timeout_event(m, w, ev->events);
1483 break;
1484
acbb0225
LP
1485 default:
1486 assert_not_reached("Unknown epoll event type.");
034c6ed7 1487 }
9152c765
LP
1488
1489 return 0;
1490}
1491
1492int manager_loop(Manager *m) {
1493 int r;
b9cd2ec1 1494 bool quit = false;
9152c765 1495
ea430986
LP
1496 RATELIMIT_DEFINE(rl, 1*USEC_PER_SEC, 1000);
1497
9152c765
LP
1498 assert(m);
1499
1500 for (;;) {
957ca890
LP
1501 struct epoll_event event;
1502 int n;
9152c765 1503
ea430986
LP
1504 if (!ratelimit_test(&rl)) {
1505 /* Yay, something is going seriously wrong, pause a little */
1506 log_warning("Looping too fast. Throttling execution a little.");
1507 sleep(1);
1508 }
1509
c1e1601e
LP
1510 if (manager_dispatch_load_queue(m) > 0)
1511 continue;
034c6ed7 1512
c1e1601e
LP
1513 if (manager_dispatch_run_queue(m) > 0)
1514 continue;
1515
1516 if (bus_dispatch(m) > 0)
1517 continue;
1518
1519 if (manager_dispatch_dbus_queue(m) > 0)
ea430986 1520 continue;
ea430986 1521
957ca890 1522 if ((n = epoll_wait(m->epoll_fd, &event, 1, -1)) < 0) {
9152c765
LP
1523
1524 if (errno == -EINTR)
1525 continue;
1526
1527 return -errno;
1528 }
1529
957ca890 1530 assert(n == 1);
b9cd2ec1 1531
957ca890
LP
1532 if ((r = process_event(m, &event, &quit)) < 0)
1533 return r;
1534
1535 if (quit)
1536 return 0;
83c60c9f
LP
1537 }
1538}
ea430986
LP
1539
1540int manager_get_unit_from_dbus_path(Manager *m, const char *s, Unit **_u) {
1541 char *n;
1542 Unit *u;
1543
1544 assert(m);
1545 assert(s);
1546 assert(_u);
1547
1548 if (!startswith(s, "/org/freedesktop/systemd1/unit/"))
1549 return -EINVAL;
1550
1551 if (!(n = bus_path_unescape(s+31)))
1552 return -ENOMEM;
1553
1554 u = manager_get_unit(m, n);
1555 free(n);
1556
1557 if (!u)
1558 return -ENOENT;
1559
1560 *_u = u;
1561
1562 return 0;
1563}
86fbf370
LP
1564
1565int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) {
1566 Job *j;
1567 unsigned id;
1568 int r;
1569
1570 assert(m);
1571 assert(s);
1572 assert(_j);
1573
1574 if (!startswith(s, "/org/freedesktop/systemd1/job/"))
1575 return -EINVAL;
1576
1577 if ((r = safe_atou(s + 30, &id)) < 0)
1578 return r;
1579
1580 if (!(j = manager_get_job(m, id)))
1581 return -ENOENT;
1582
1583 *_j = j;
1584
1585 return 0;
1586}
dfcd764e
LP
1587
1588static const char* const manager_running_as_table[_MANAGER_RUNNING_AS_MAX] = {
1589 [MANAGER_INIT] = "init",
1590 [MANAGER_SYSTEM] = "system",
036643a2 1591 [MANAGER_SESSION] = "session"
dfcd764e
LP
1592};
1593
1594DEFINE_STRING_TABLE_LOOKUP(manager_running_as, ManagerRunningAs);