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