13 #include "dinit-log.h"
14 #include "dinit-socket.h"
15 #include "dinit-util.h"
16 #include "baseproc-sys.h"
19 * service.cc - Service management.
20 * See service.h for details.
23 // Find the requested service by name
24 static service_record * find_service(const std::list<service_record *> & records,
25 const char *name) noexcept
28 list<service_record *>::const_iterator i = records.begin();
29 for ( ; i != records.end(); i++ ) {
30 if (strcmp((*i)->get_name().c_str(), name) == 0) {
37 service_record * service_set::find_service(const std::string &name) noexcept
39 return ::find_service(records, name.c_str());
42 void service_set::stop_service(const std::string & name) noexcept
44 service_record *record = find_service(name);
45 if (record != nullptr) {
51 // Called when a service has actually stopped; dependents have stopped already, unless this stop
52 // is due to an unexpected process termination.
53 void service_record::stopped() noexcept
56 bp_sys::tcsetpgrp(0, bp_sys::getpgrp());
57 discard_console_log_buffer();
63 // If we are a soft dependency of another target, break the acquisition from that target now:
64 for (auto & dependent : dependents) {
65 if (dependent->dep_type != dependency_type::REGULAR) {
66 if (dependent->holding_acq) {
67 dependent->holding_acq = false;
73 bool will_restart = (desired_state == service_state_t::STARTED)
74 && services->get_auto_restart();
76 for (auto dependency : depends_on) {
77 // we signal dependencies in case they are waiting for us to stop:
78 dependency.get_to()->dependent_stopped();
81 service_state = service_state_t::STOPPED;
84 // Desired state is "started".
92 // If we were explicitly started, our required_by count must be at least 1. Use
93 // release() to correctly release, mark inactive and release dependencies.
94 start_explicit = false;
97 else if (required_by == 0) {
98 // This can only be the case if we didn't have start_explicit, since required_by would
99 // otherwise by non-zero.
100 prop_release = !prop_require;
101 prop_require = false;
102 services->add_prop_queue(this);
103 services->service_inactive(this);
107 log_service_stopped(service_name);
108 notify_listeners(service_event_t::STOPPED);
111 bool service_record::do_auto_restart() noexcept
114 return services->get_auto_restart();
119 void service_record::require() noexcept
121 if (required_by++ == 0) {
122 prop_require = !prop_release;
123 prop_release = false;
124 services->add_prop_queue(this);
125 if (service_state != service_state_t::STARTING && service_state != service_state_t::STARTED) {
131 void service_record::release(bool issue_stop) noexcept
133 if (--required_by == 0) {
134 desired_state = service_state_t::STOPPED;
136 // Can stop, and can release dependencies now. We don't need to issue a release if
137 // the require was pending though:
138 prop_release = !prop_require;
139 prop_require = false;
140 services->add_prop_queue(this);
142 if (service_state == service_state_t::STOPPED) {
143 services->service_inactive(this);
145 else if (issue_stop) {
151 void service_record::release_dependencies() noexcept
153 for (auto & dependency : depends_on) {
154 service_record * dep_to = dependency.get_to();
155 if (dependency.holding_acq) {
156 // We must clear holding_acq before calling release, otherwise the dependency
157 // may decide to stop, check this link and release itself a second time.
158 dependency.holding_acq = false;
164 void service_record::start(bool activate) noexcept
166 if (activate && ! start_explicit) {
168 start_explicit = true;
171 if (desired_state == service_state_t::STARTED && service_state != service_state_t::STOPPED) return;
173 bool was_active = service_state != service_state_t::STOPPED || desired_state != service_state_t::STOPPED;
174 desired_state = service_state_t::STARTED;
176 if (service_state != service_state_t::STOPPED) {
177 // We're already starting/started, or we are stopping and need to wait for
178 // that the complete.
179 if (service_state != service_state_t::STOPPING || ! can_interrupt_stop()) {
182 // We're STOPPING, and that can be interrupted. Our dependencies might be STOPPING,
183 // but if so they are waiting (for us), so they too can be instantly returned to
185 notify_listeners(service_event_t::STOPCANCELLED);
187 else if (! was_active) {
188 services->service_active(this);
191 service_state = service_state_t::STARTING;
192 waiting_for_deps = true;
194 if (start_check_dependencies()) {
195 services->add_transition_queue(this);
199 void service_record::do_propagation() noexcept
202 // Need to require all our dependencies
203 for (auto & dep : depends_on) {
204 dep.get_to()->require();
205 dep.holding_acq = true;
207 prop_require = false;
211 release_dependencies();
212 prop_release = false;
216 prop_failure = false;
217 failed_to_start(true);
231 void service_record::execute_transition() noexcept
233 // state is STARTED with restarting set true if we are running a smooth recovery.
234 if (service_state == service_state_t::STARTING || (service_state == service_state_t::STARTED
236 if (check_deps_started()) {
240 else if (service_state == service_state_t::STOPPING) {
241 if (stop_check_dependents()) {
242 waiting_for_deps = false;
248 void service_record::do_start() noexcept
250 if (pinned_stopped) return;
252 if (service_state != service_state_t::STARTING) {
256 service_state = service_state_t::STARTING;
258 waiting_for_deps = true;
260 // Ask dependencies to start, mark them as being waited on.
261 if (check_deps_started()) {
262 // Once all dependencies are started, we start properly:
267 void service_record::dependency_started() noexcept
269 // Note that we check for STARTED state here in case the service is in smooth recovery while pinned.
270 // In that case it will wait for dependencies to start before restarting the process.
271 if ((service_state == service_state_t::STARTING || service_state == service_state_t::STARTED)
272 && waiting_for_deps) {
273 services->add_transition_queue(this);
277 bool service_record::start_check_dependencies() noexcept
279 bool all_deps_started = true;
281 for (auto & dep : depends_on) {
282 service_record * to = dep.get_to();
283 if (to->service_state != service_state_t::STARTED) {
284 if (to->service_state != service_state_t::STARTING) {
285 to->prop_start = true;
286 services->add_prop_queue(to);
288 dep.waiting_on = true;
289 all_deps_started = false;
293 return all_deps_started;
296 bool service_record::check_deps_started() noexcept
298 for (auto & dep : depends_on) {
299 if (dep.waiting_on) {
307 void service_record::all_deps_started() noexcept
309 if (onstart_flags.starts_on_console && ! have_console) {
314 waiting_for_deps = false;
316 if (! can_proceed_to_start()) {
317 waiting_for_deps = true;
321 bool start_success = bring_up();
322 if (! start_success) {
327 void service_record::acquired_console() noexcept
329 waiting_for_console = false;
332 if (service_state != service_state_t::STARTING) {
333 // We got the console but no longer want it.
336 else if (check_deps_started()) {
340 // We got the console but can't use it yet.
345 void service_record::started() noexcept
347 // If we start on console but don't keep it, release it now:
348 if (have_console && ! onstart_flags.runs_on_console) {
349 bp_sys::tcsetpgrp(0, bp_sys::getpgrp());
353 log_service_started(get_name());
354 service_state = service_state_t::STARTED;
355 notify_listeners(service_event_t::STARTED);
357 if (onstart_flags.rw_ready) {
358 open_control_socket();
360 if (onstart_flags.log_ready) {
361 setup_external_log();
364 if (force_stop || desired_state == service_state_t::STOPPED) {
370 // Notify any dependents whose desired state is STARTED:
371 for (auto dept : dependents) {
372 dept->get_from()->dependency_started();
373 dept->waiting_on = false;
377 void service_record::failed_to_start(bool depfailed, bool immediate_stop) noexcept
379 if (waiting_for_console) {
380 services->unqueue_console(this);
381 waiting_for_console = false;
384 if (start_explicit) {
385 start_explicit = false;
389 // Cancel start of dependents:
390 for (auto & dept : dependents) {
391 switch (dept->dep_type) {
392 case dependency_type::REGULAR:
393 case dependency_type::MILESTONE:
394 if (dept->get_from()->service_state == service_state_t::STARTING) {
395 dept->get_from()->prop_failure = true;
396 services->add_prop_queue(dept->get_from());
399 case dependency_type::WAITS_FOR:
400 case dependency_type::SOFT:
401 if (dept->waiting_on) {
402 dept->waiting_on = false;
403 dept->get_from()->dependency_started();
407 // Always release now, so that our desired state will be STOPPED before we call
408 // stopped() below (if we do so). Otherwise it may decide to restart us.
409 if (dept->holding_acq) {
410 dept->holding_acq = false;
415 log_service_failed(get_name());
416 notify_listeners(service_event_t::FAILEDSTART);
418 if (immediate_stop) {
423 bool service_record::bring_up() noexcept
425 // default implementation: there is no process, so we are started.
430 // Mark this and all dependent services as force-stopped.
431 void service_record::forced_stop() noexcept
433 if (service_state != service_state_t::STOPPED) {
435 if (! pinned_started) {
437 services->add_transition_queue(this);
442 void service_record::dependent_stopped() noexcept
444 if (service_state == service_state_t::STOPPING && waiting_for_deps) {
445 services->add_transition_queue(this);
449 void service_record::stop(bool bring_down) noexcept
451 if (start_explicit) {
452 start_explicit = false;
461 void service_record::do_stop() noexcept
463 // A service that does actually stop for any reason should have its explicit activation released, unless
465 if (start_explicit && ! do_auto_restart()) {
466 start_explicit = false;
470 bool all_deps_stopped = stop_dependents();
472 if (service_state != service_state_t::STARTED) {
473 if (service_state == service_state_t::STARTING) {
474 // If waiting for a dependency, or waiting for the console, we can interrupt start. Otherwise,
475 // we need to delegate to can_interrupt_start() (which can be overridden).
476 if (! waiting_for_deps && ! waiting_for_console) {
477 if (! can_interrupt_start()) {
478 // Well this is awkward: we're going to have to continue starting. We can stop once we've
479 // reached the started state.
483 if (! interrupt_start()) {
484 // Now wait for service startup to actually end; we don't need to handle it here.
485 notify_listeners(service_event_t::STARTCANCELLED);
489 else if (waiting_for_console) {
490 services->unqueue_console(this);
491 waiting_for_console = false;
494 // We must have had desired_state == STARTED.
495 notify_listeners(service_event_t::STARTCANCELLED);
497 // Reaching this point, we are starting interruptibly - so we
498 // stop now (by falling through to below).
501 // If we're starting we need to wait for that to complete.
502 // If we're already stopping/stopped there's nothing to do.
507 if (pinned_started) return;
509 service_state = service_state_t::STOPPING;
510 waiting_for_deps = true;
511 if (all_deps_stopped) {
512 services->add_transition_queue(this);
516 bool service_record::stop_check_dependents() noexcept
518 bool all_deps_stopped = true;
519 for (auto dept : dependents) {
520 if (dept->dep_type == dependency_type::REGULAR && ! dept->get_from()->is_stopped()) {
521 all_deps_stopped = false;
526 return all_deps_stopped;
529 bool service_record::stop_dependents() noexcept
531 bool all_deps_stopped = true;
532 for (auto dept : dependents) {
533 if (dept->dep_type == dependency_type::REGULAR ||
534 (dept->dep_type == dependency_type::MILESTONE &&
535 dept->get_from()->service_state != service_state_t::STARTED)) {
536 if (! dept->get_from()->is_stopped()) {
537 // Note we check *first* since if the dependent service is not stopped,
538 // 1. We will issue a stop to it shortly and
539 // 2. It will notify us when stopped, at which point the stop_check_dependents()
540 // check is run anyway.
541 all_deps_stopped = false;
545 // If this service is to be forcefully stopped, dependents must also be.
546 dept->get_from()->forced_stop();
549 dept->get_from()->prop_stop = true;
550 services->add_prop_queue(dept->get_from());
553 // waits-for or soft dependency:
554 if (dept->waiting_on) {
555 dept->waiting_on = false;
556 dept->get_from()->dependency_started();
558 if (dept->holding_acq) {
559 dept->holding_acq = false;
565 return all_deps_stopped;
568 // All dependents have stopped; we can stop now, too. Only called when STOPPING.
569 void service_record::bring_down() noexcept
571 waiting_for_deps = false;
575 void service_record::unpin() noexcept
577 if (pinned_started) {
578 pinned_started = false;
579 if (desired_state == service_state_t::STOPPED || force_stop) {
581 services->process_queues();
584 if (pinned_stopped) {
585 pinned_stopped = false;
586 if (desired_state == service_state_t::STARTED) {
588 services->process_queues();
593 void service_record::queue_for_console() noexcept
595 waiting_for_console = true;
596 services->append_console_queue(this);
599 void service_record::release_console() noexcept
601 have_console = false;
602 services->pull_console_queue();
605 bool service_record::interrupt_start() noexcept
610 void service_set::service_active(service_record *sr) noexcept
615 void service_set::service_inactive(service_record *sr) noexcept