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