]>
git.ipfire.org Git - people/ms/systemd.git/blob - manager.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
8 #include <sys/signalfd.h>
19 Manager
* manager_new(void) {
22 struct epoll_event ev
;
24 if (!(m
= new0(Manager
, 1)))
27 m
->signal_fd
= m
->epoll_fd
= -1;
29 if (!(m
->names
= hashmap_new(string_hash_func
, string_compare_func
)))
32 if (!(m
->jobs
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
35 if (!(m
->transaction_jobs
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
38 if (!(m
->watch_pids
= hashmap_new(trivial_hash_func
, trivial_compare_func
)))
41 if ((m
->epoll_fd
= epoll_create1(EPOLL_CLOEXEC
)) < 0)
44 assert_se(sigemptyset(&mask
) == 0);
45 assert_se(sigaddset(&mask
, SIGCHLD
) == 0);
46 assert_se(sigprocmask(SIG_SETMASK
, &mask
, NULL
) == 0);
48 if ((m
->signal_fd
= signalfd(-1, &mask
, SFD_NONBLOCK
|SFD_CLOEXEC
)) < 0)
53 ev
.data
.fd
= m
->signal_fd
;
55 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, m
->signal_fd
, &ev
) < 0)
65 void manager_free(Manager
*m
) {
71 while ((n
= hashmap_first(m
->names
)))
74 while ((j
= hashmap_steal_first(m
->transaction_jobs
)))
77 hashmap_free(m
->names
);
78 hashmap_free(m
->jobs
);
79 hashmap_free(m
->transaction_jobs
);
80 hashmap_free(m
->watch_pids
);
83 close_nointr(m
->epoll_fd
);
84 if (m
->signal_fd
>= 0)
85 close_nointr(m
->signal_fd
);
90 static void transaction_delete_job(Manager
*m
, Job
*j
) {
94 /* Deletes one job from the transaction */
96 manager_transaction_unlink_job(m
, j
);
102 static void transaction_delete_name(Manager
*m
, Name
*n
) {
105 /* Deletes all jobs associated with a certain name from the
108 while ((j
= hashmap_get(m
->transaction_jobs
, n
)))
109 transaction_delete_job(m
, j
);
112 static void transaction_abort(Manager
*m
) {
117 while ((j
= hashmap_first(m
->transaction_jobs
)))
119 transaction_delete_job(m
, j
);
123 assert(hashmap_isempty(m
->transaction_jobs
));
124 assert(!m
->transaction_anchor
);
127 static void transaction_find_jobs_that_matter_to_anchor(Manager
*m
, Job
*j
, unsigned generation
) {
132 /* A recursive sweep through the graph that marks all names
133 * that matter to the anchor job, i.e. are directly or
134 * indirectly a dependency of the anchor job via paths that
135 * are fully marked as mattering. */
137 for (l
= j
? j
->subject_list
: m
->transaction_anchor
; l
; l
= l
->subject_next
) {
139 /* This link does not matter */
143 /* This name has already been marked */
144 if (l
->object
->generation
== generation
)
147 l
->object
->matters_to_anchor
= true;
148 l
->object
->generation
= generation
;
150 transaction_find_jobs_that_matter_to_anchor(m
, l
->object
, generation
);
154 static void transaction_merge_and_delete_job(Manager
*m
, Job
*j
, Job
*other
, JobType t
) {
155 JobDependency
*l
, *last
;
159 assert(j
->name
== other
->name
);
162 /* Merges 'other' into 'j' and then deletes j. */
165 j
->state
= JOB_WAITING
;
166 j
->forced
= j
->forced
|| other
->forced
;
168 j
->matters_to_anchor
= j
->matters_to_anchor
|| other
->matters_to_anchor
;
170 /* Patch us in as new owner of the JobDependency objects */
172 for (l
= other
->subject_list
; l
; l
= l
->subject_next
) {
173 assert(l
->subject
== other
);
178 /* Merge both lists */
180 last
->subject_next
= j
->subject_list
;
182 j
->subject_list
->subject_prev
= last
;
183 j
->subject_list
= other
->subject_list
;
186 /* Patch us in as new owner of the JobDependency objects */
188 for (l
= other
->object_list
; l
; l
= l
->object_next
) {
189 assert(l
->object
== other
);
194 /* Merge both lists */
196 last
->object_next
= j
->object_list
;
198 j
->object_list
->object_prev
= last
;
199 j
->object_list
= other
->object_list
;
202 /* Kill the other job */
203 other
->subject_list
= NULL
;
204 other
->object_list
= NULL
;
205 transaction_delete_job(m
, other
);
208 static int delete_one_unmergeable_job(Manager
*m
, Job
*j
) {
213 /* Tries to delete one item in the linked list
214 * j->transaction_next->transaction_next->... that conflicts
215 * whith another one, in an attempt to make an inconsistent
216 * transaction work. */
218 /* We rely here on the fact that if a merged with b does not
219 * merge with c, either a or b merge with c neither */
220 LIST_FOREACH(transaction
, j
, j
)
221 LIST_FOREACH(transaction
, k
, j
->transaction_next
) {
224 /* Is this one mergeable? Then skip it */
225 if (job_type_is_mergeable(j
->type
, k
->type
))
228 /* Ok, we found two that conflict, let's see if we can
229 * drop one of them */
230 if (!j
->matters_to_anchor
)
232 else if (!k
->matters_to_anchor
)
237 /* Ok, we can drop one, so let's do so. */
238 log_debug("Try to fix job merging by deleting job %s/%s", name_id(d
->name
), job_type_to_string(d
->type
));
239 transaction_delete_job(m
, d
);
246 static int transaction_merge_jobs(Manager
*m
) {
253 /* First step, check whether any of the jobs for one specific
254 * task conflict. If so, try to drop one of them. */
255 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
260 LIST_FOREACH(transaction
, k
, j
->transaction_next
) {
261 if ((r
= job_type_merge(&t
, k
->type
)) >= 0)
264 /* OK, we could not merge all jobs for this
265 * action. Let's see if we can get rid of one
268 if ((r
= delete_one_unmergeable_job(m
, j
)) >= 0)
269 /* Ok, we managed to drop one, now
270 * let's ask our callers to call us
271 * again after garbage collecting */
274 /* We couldn't merge anything. Failure */
279 /* Second step, merge the jobs. */
280 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
284 /* Merge all transactions */
285 LIST_FOREACH(transaction
, k
, j
->transaction_next
)
286 assert_se(job_type_merge(&t
, k
->type
) == 0);
288 /* If an active job is mergeable, merge it too */
289 if (j
->name
->meta
.job
)
290 job_type_merge(&t
, j
->name
->meta
.job
->type
); /* Might fail. Which is OK */
292 while ((k
= j
->transaction_next
)) {
294 transaction_merge_and_delete_job(m
, k
, j
, t
);
297 transaction_merge_and_delete_job(m
, j
, k
, t
);
300 assert(!j
->transaction_next
);
301 assert(!j
->transaction_prev
);
307 static bool name_matters_to_anchor(Name
*n
, Job
*j
) {
309 assert(!j
->transaction_prev
);
311 /* Checks whether at least one of the jobs for this name
312 * matters to the anchor. */
314 LIST_FOREACH(transaction
, j
, j
)
315 if (j
->matters_to_anchor
)
321 static int transaction_verify_order_one(Manager
*m
, Job
*j
, Job
*from
, unsigned generation
) {
328 assert(!j
->transaction_prev
);
330 /* Does a recursive sweep through the ordering graph, looking
331 * for a cycle. If we find cycle we try to break it. */
333 /* Did we find a cycle? */
334 if (j
->marker
&& j
->generation
== generation
) {
337 /* So, we already have been here. We have a
338 * cycle. Let's try to break it. We go backwards in
339 * our path and try to find a suitable job to
340 * remove. We use the marker to find our way back,
341 * since smart how we are we stored our way back in
344 for (k
= from
; k
; k
= (k
->generation
== generation
? k
->marker
: NULL
)) {
347 !name_matters_to_anchor(k
->name
, k
)) {
348 /* Ok, we can drop this one, so let's
350 log_debug("Breaking order cycle by deleting job %s/%s", name_id(k
->name
), job_type_to_string(k
->type
));
351 transaction_delete_name(m
, k
->name
);
355 /* Check if this in fact was the beginning of
364 /* Make the marker point to where we come from, so that we can
365 * find our way backwards if we want to break a cycle */
367 j
->generation
= generation
;
369 /* We assume that the the dependencies are bidirectional, and
370 * hence can ignore NAME_AFTER */
371 SET_FOREACH(n
, j
->name
->meta
.dependencies
[NAME_BEFORE
], i
) {
374 /* Is there a job for this name? */
375 if (!(o
= hashmap_get(m
->transaction_jobs
, n
)))
377 /* Ok, there is no job for this in the
378 * transaction, but maybe there is already one
380 if (!(o
= n
->meta
.job
))
383 if ((r
= transaction_verify_order_one(m
, o
, j
, generation
)) < 0)
390 static int transaction_verify_order(Manager
*m
, unsigned *generation
) {
398 /* Check if the ordering graph is cyclic. If it is, try to fix
399 * that up by dropping one of the jobs. */
401 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
)
402 if ((r
= transaction_verify_order_one(m
, j
, NULL
, (*generation
)++)) < 0)
408 static void transaction_collect_garbage(Manager
*m
) {
413 /* Drop jobs that are not required by any other job */
421 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
425 log_debug("Garbage collecting job %s/%s", name_id(j
->name
), job_type_to_string(j
->type
));
426 transaction_delete_job(m
, j
);
434 static int transaction_is_destructive(Manager
*m
, JobMode mode
) {
440 /* Checks whether applying this transaction means that
441 * existing jobs would be replaced */
443 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
446 assert(!j
->transaction_prev
);
447 assert(!j
->transaction_next
);
449 if (j
->name
->meta
.job
&&
450 j
->name
->meta
.job
!= j
&&
451 !job_type_is_superset(j
->type
, j
->name
->meta
.job
->type
))
458 static void transaction_minimize_impact(Manager
*m
) {
462 /* Drops all unnecessary jobs that reverse already active jobs
463 * or that stop a running service. */
471 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
472 LIST_FOREACH(transaction
, j
, j
) {
474 /* If it matters, we shouldn't drop it */
475 if (j
->matters_to_anchor
)
478 /* Would this stop a running service?
479 * Would this change an existing job?
480 * If so, let's drop this entry */
481 if ((j
->type
!= JOB_STOP
|| NAME_IS_INACTIVE_OR_DEACTIVATING(name_active_state(j
->name
))) &&
482 (!j
->name
->meta
.job
|| job_type_is_conflicting(j
->type
, j
->name
->meta
.job
->state
)))
485 /* Ok, let's get rid of this */
486 log_debug("Deleting %s/%s to minimize impact", name_id(j
->name
), job_type_to_string(j
->type
));
487 transaction_delete_job(m
, j
);
499 static int transaction_apply(Manager
*m
, JobMode mode
) {
504 /* Moves the transaction jobs to the set of active jobs */
506 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
508 assert(!j
->transaction_prev
);
509 assert(!j
->transaction_next
);
514 if ((r
= hashmap_put(m
->jobs
, UINT32_TO_PTR(j
->id
), j
)) < 0)
518 while ((j
= hashmap_steal_first(m
->transaction_jobs
))) {
522 if (j
->name
->meta
.job
)
523 job_free(j
->name
->meta
.job
);
525 j
->name
->meta
.job
= j
;
528 /* We're fully installed. Now let's free data we don't
531 assert(!j
->transaction_next
);
532 assert(!j
->transaction_prev
);
534 while (j
->subject_list
)
535 job_dependency_free(j
->subject_list
);
536 while (j
->object_list
)
537 job_dependency_free(j
->object_list
);
540 m
->transaction_anchor
= NULL
;
546 HASHMAP_FOREACH(j
, m
->transaction_jobs
, i
) {
550 hashmap_remove(m
->jobs
, UINT32_TO_PTR(j
->id
));
556 static int transaction_activate(Manager
*m
, JobMode mode
) {
558 unsigned generation
= 1;
562 /* This applies the changes recorded in transaction_jobs to
563 * the actual list of jobs, if possible. */
565 /* First step: figure out which jobs matter */
566 transaction_find_jobs_that_matter_to_anchor(m
, NULL
, generation
++);
568 /* Second step: Try not to stop any running services if
569 * we don't have to. Don't try to reverse running
570 * jobs if we don't have to. */
571 transaction_minimize_impact(m
);
574 /* Third step: Let's remove unneeded jobs that might
576 transaction_collect_garbage(m
);
578 /* Fourth step: verify order makes sense and correct
579 * cycles if necessary and possible */
580 if ((r
= transaction_verify_order(m
, &generation
)) >= 0)
586 /* Let's see if the resulting transaction ordering
587 * graph is still cyclic... */
591 /* Fifth step: let's drop unmergeable entries if
592 * necessary and possible, merge entries we can
594 if ((r
= transaction_merge_jobs(m
)) >= 0)
600 /* Sixth step: an entry got dropped, let's garbage
601 * collect its dependencies. */
602 transaction_collect_garbage(m
);
604 /* Let's see if the resulting transaction still has
605 * unmergeable entries ... */
608 /* Seventh step: check whether we can actually apply this */
609 if (mode
== JOB_FAIL
)
610 if ((r
= transaction_is_destructive(m
, mode
)) < 0)
613 /* Eights step: apply changes */
614 if ((r
= transaction_apply(m
, mode
)) < 0)
617 assert(hashmap_isempty(m
->transaction_jobs
));
618 assert(!m
->transaction_anchor
);
623 transaction_abort(m
);
627 static Job
* transaction_add_one_job(Manager
*m
, JobType type
, Name
*name
, bool force
, bool *is_new
) {
634 /* Looks for an axisting prospective job and returns that. If
635 * it doesn't exist it is created and added to the prospective
638 f
= hashmap_get(m
->transaction_jobs
, name
);
640 LIST_FOREACH(transaction
, j
, f
) {
641 assert(j
->name
== name
);
643 if (j
->type
== type
) {
650 if (name
->meta
.job
&& name
->meta
.job
->type
== type
)
652 else if (!(j
= job_new(m
, type
, name
)))
657 j
->matters_to_anchor
= false;
660 LIST_PREPEND(Job
, transaction
, f
, j
);
662 if ((r
= hashmap_replace(m
->transaction_jobs
, name
, f
)) < 0) {
673 void manager_transaction_unlink_job(Manager
*m
, Job
*j
) {
677 if (j
->transaction_prev
)
678 j
->transaction_prev
->transaction_next
= j
->transaction_next
;
679 else if (j
->transaction_next
)
680 hashmap_replace(m
->transaction_jobs
, j
->name
, j
->transaction_next
);
682 hashmap_remove_value(m
->transaction_jobs
, j
->name
, j
);
684 if (j
->transaction_next
)
685 j
->transaction_next
->transaction_prev
= j
->transaction_prev
;
687 j
->transaction_prev
= j
->transaction_next
= NULL
;
689 while (j
->subject_list
)
690 job_dependency_free(j
->subject_list
);
692 while (j
->object_list
) {
693 Job
*other
= j
->object_list
->matters
? j
->object_list
->subject
: NULL
;
695 job_dependency_free(j
->object_list
);
698 log_debug("Deleting job %s/%s as dependency of job %s/%s",
699 name_id(other
->name
), job_type_to_string(other
->type
),
700 name_id(j
->name
), job_type_to_string(j
->type
));
701 transaction_delete_job(m
, other
);
706 static int transaction_add_job_and_dependencies(Manager
*m
, JobType type
, Name
*name
, Job
*by
, bool matters
, bool force
, Job
**_ret
) {
714 assert(type
< _JOB_TYPE_MAX
);
717 if (name
->meta
.load_state
!= NAME_LOADED
)
720 if (!name_job_is_applicable(name
, type
))
723 /* First add the job. */
724 if (!(ret
= transaction_add_one_job(m
, type
, name
, force
, &is_new
)))
727 /* Then, add a link to the job. */
728 if (!job_dependency_new(by
, ret
, matters
))
732 /* Finally, recursively add in all dependencies. */
733 if (type
== JOB_START
|| type
== JOB_RELOAD_OR_START
) {
734 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_REQUIRES
], i
)
735 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
737 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_SOFT_REQUIRES
], i
)
738 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, !force
, force
, NULL
)) < 0 && r
!= -EBADR
)
740 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_WANTS
], i
)
741 if ((r
= transaction_add_job_and_dependencies(m
, JOB_START
, dep
, ret
, false, force
, NULL
)) < 0 && r
!= -EBADR
)
743 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_REQUISITE
], i
)
744 if ((r
= transaction_add_job_and_dependencies(m
, JOB_VERIFY_ACTIVE
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
746 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_SOFT_REQUISITE
], i
)
747 if ((r
= transaction_add_job_and_dependencies(m
, JOB_VERIFY_ACTIVE
, dep
, ret
, !force
, force
, NULL
)) < 0 && r
!= -EBADR
)
749 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_CONFLICTS
], i
)
750 if ((r
= transaction_add_job_and_dependencies(m
, JOB_STOP
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
753 } else if (type
== JOB_STOP
|| type
== JOB_RESTART
|| type
== JOB_TRY_RESTART
) {
755 SET_FOREACH(dep
, ret
->name
->meta
.dependencies
[NAME_REQUIRED_BY
], i
)
756 if ((r
= transaction_add_job_and_dependencies(m
, type
, dep
, ret
, true, force
, NULL
)) < 0 && r
!= -EBADR
)
760 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
769 int manager_add_job(Manager
*m
, JobType type
, Name
*name
, JobMode mode
, bool force
, Job
**_ret
) {
774 assert(type
< _JOB_TYPE_MAX
);
776 assert(mode
< _JOB_MODE_MAX
);
778 if ((r
= transaction_add_job_and_dependencies(m
, type
, name
, NULL
, true, force
, &ret
))) {
779 transaction_abort(m
);
783 if ((r
= transaction_activate(m
, mode
)) < 0)
792 Job
*manager_get_job(Manager
*m
, uint32_t id
) {
795 return hashmap_get(m
->jobs
, UINT32_TO_PTR(id
));
798 Name
*manager_get_name(Manager
*m
, const char *name
) {
802 return hashmap_get(m
->names
, name
);
805 static void dispatch_load_queue(Manager
*m
) {
810 /* Make sure we are not run recursively */
811 if (m
->dispatching_load_queue
)
814 m
->dispatching_load_queue
= true;
816 /* Dispatches the load queue. Takes a name from the queue and
817 * tries to load its data until the queue is empty */
819 while ((meta
= m
->load_queue
)) {
820 assert(meta
->linked
);
821 assert(meta
->in_load_queue
);
823 name_load(NAME(meta
));
826 m
->dispatching_load_queue
= false;
829 int manager_load_name(Manager
*m
, const char *name
, Name
**_ret
) {
837 /* This will load the service information files, but not actually
838 * start any services or anything */
840 if ((ret
= manager_get_name(m
, name
))) {
845 if (!(ret
= name_new(m
)))
848 if ((r
= name_add_name(ret
, name
)) < 0) {
853 if ((r
= name_link(ret
)) < 0) {
858 /* At this point the new entry is created and linked. However
859 * not loaded. Now load this entry and all its dependencies
862 dispatch_load_queue(m
);
868 void manager_dump_jobs(Manager
*s
, FILE *f
, const char *prefix
) {
875 HASHMAP_FOREACH(j
, s
->jobs
, i
)
876 job_dump(j
, f
, prefix
);
879 void manager_dump_names(Manager
*s
, FILE *f
, const char *prefix
) {
887 HASHMAP_FOREACH_KEY(n
, t
, s
->names
, i
)
889 name_dump(n
, f
, prefix
);
892 void manager_clear_jobs(Manager
*m
) {
897 transaction_abort(m
);
899 while ((j
= hashmap_first(m
->jobs
)))
903 void manager_dispatch_run_queue(Manager
*m
) {
906 if (m
->dispatching_run_queue
)
909 m
->dispatching_run_queue
= true;
911 while ((j
= m
->run_queue
)) {
913 assert(j
->in_run_queue
);
915 job_run_and_invalidate(j
);
918 m
->dispatching_run_queue
= false;
921 static int manager_dispatch_sigchld(Manager
*m
) {
929 if (waitid(P_ALL
, 0, &si
, WNOHANG
) < 0)
935 if (si
.si_code
!= CLD_EXITED
&& si
.si_code
!= CLD_KILLED
&& si
.si_code
!= CLD_DUMPED
)
938 if (!(n
= hashmap_remove(m
->watch_pids
, UINT32_TO_PTR(si
.si_pid
))))
941 NAME_VTABLE(n
)->sigchld_event(n
, si
.si_pid
, si
.si_code
, si
.si_status
);
947 static int manager_process_signal_fd(Manager
*m
) {
949 struct signalfd_siginfo sfsi
;
950 bool sigchld
= false;
955 if ((n
= read(m
->signal_fd
, &sfsi
, sizeof(sfsi
))) != sizeof(sfsi
)) {
966 if (sfsi
.ssi_signo
== SIGCHLD
)
971 return manager_dispatch_sigchld(m
);
976 static int process_event(Manager
*m
, struct epoll_event
*ev
) {
982 switch (ev
->data
.u32
) {
985 assert(ev
->data
.fd
== m
->signal_fd
);
987 /* An incoming signal? */
988 if (ev
->events
!= POLLIN
)
991 if ((r
= manager_process_signal_fd(m
)) < 0)
999 /* Some fd event, to be dispatched to the names */
1000 assert_se(n
= ev
->data
.ptr
);
1001 NAME_VTABLE(n
)->fd_event(n
, ev
->data
.fd
, ev
->events
);
1005 case MANAGER_TIMER
: {
1010 /* Some timer event, to be dispatched to the names */
1011 if ((k
= read(ev
->data
.fd
, &u
, sizeof(u
))) != sizeof(u
)) {
1013 if (k
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
1016 return k
< 0 ? -errno
: -EIO
;
1019 assert_se(n
= ev
->data
.ptr
);
1020 NAME_VTABLE(n
)->timer_event(n
, ev
->data
.fd
, u
);
1025 assert_not_reached("Unknown epoll event type.");
1031 int manager_loop(Manager
*m
) {
1037 struct epoll_event events
[32];
1040 manager_dispatch_run_queue(m
);
1042 if ((n
= epoll_wait(m
->epoll_fd
, events
, ELEMENTSOF(events
), -1)) < 0) {
1044 if (errno
== -EINTR
)
1050 for (i
= 0; i
< n
; i
++)
1051 if ((r
= process_event(m
, events
+ i
)) < 0)