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());
62 // If we are a soft dependency of another target, break the acquisition from that target now,
63 // so that we don't re-start:
64 for (auto & dependent : dependents) {
65 if (dependent->dep_type != dependency_type::REGULAR) {
66 if (dependent->holding_acq && ! dependent->waiting_on) {
67 dependent->holding_acq = false;
73 bool will_restart = (desired_state == service_state_t::STARTED)
74 && !services->is_shutting_down();
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 // Start failure will have been logged already, only log if we are stopped for other reasons:
108 if (! start_failed) {
109 log_service_stopped(service_name);
111 // If this service chains to another, start the other service now:
112 if (! will_restart && ! start_on_completion.empty()) {
114 auto chain_to = services->load_service(start_on_completion.c_str());
117 catch (service_load_exc &sle) {
118 log(loglevel_t::ERROR, "Couldn't chain to service ", start_on_completion, ": ",
119 "couldn't load ", sle.service_name, ": ", sle.exc_description);
121 catch (std::bad_alloc &bae) {
122 log(loglevel_t::ERROR, "Couldn't chain to service ", start_on_completion,
127 notify_listeners(service_event_t::STOPPED);
130 bool service_record::do_auto_restart() noexcept
133 return !services->is_shutting_down();
138 void service_record::require() noexcept
140 if (required_by++ == 0) {
141 prop_require = !prop_release;
142 prop_release = false;
143 services->add_prop_queue(this);
144 if (service_state != service_state_t::STARTING && service_state != service_state_t::STARTED) {
150 void service_record::release(bool issue_stop) noexcept
152 if (--required_by == 0) {
153 desired_state = service_state_t::STOPPED;
155 // Can stop, and can release dependencies now. We don't need to issue a release if
156 // the require was pending though:
157 prop_release = !prop_require;
158 prop_require = false;
159 services->add_prop_queue(this);
161 if (service_state == service_state_t::STOPPED) {
162 services->service_inactive(this);
164 else if (issue_stop) {
165 stop_reason = stopped_reason_t::NORMAL;
171 void service_record::release_dependencies() noexcept
173 for (auto & dependency : depends_on) {
174 service_record * dep_to = dependency.get_to();
175 if (dependency.holding_acq) {
176 // We must clear holding_acq before calling release, otherwise the dependency
177 // may decide to stop, check this link and release itself a second time.
178 dependency.holding_acq = false;
184 void service_record::start(bool activate) noexcept
186 if (activate && ! start_explicit) {
188 start_explicit = true;
191 if (desired_state == service_state_t::STARTED && service_state != service_state_t::STOPPED) return;
193 bool was_active = service_state != service_state_t::STOPPED || desired_state != service_state_t::STOPPED;
194 desired_state = service_state_t::STARTED;
196 if (service_state != service_state_t::STOPPED) {
197 // We're already starting/started, or we are stopping and need to wait for
198 // that the complete.
199 if (service_state != service_state_t::STOPPING || ! can_interrupt_stop()) {
202 // We're STOPPING, and that can be interrupted. Our dependencies might be STOPPING,
203 // but if so they are waiting (for us), so they too can be instantly returned to
205 notify_listeners(service_event_t::STOPCANCELLED);
207 else if (! was_active) {
208 services->service_active(this);
211 start_failed = false;
212 start_skipped = false;
213 service_state = service_state_t::STARTING;
214 waiting_for_deps = true;
216 if (start_check_dependencies()) {
217 services->add_transition_queue(this);
221 void service_record::do_propagation() noexcept
224 // Need to require all our dependencies
225 for (auto & dep : depends_on) {
226 dep.get_to()->require();
227 dep.holding_acq = true;
229 prop_require = false;
233 release_dependencies();
234 prop_release = false;
238 prop_failure = false;
239 stop_reason = stopped_reason_t::DEPFAILED;
240 failed_to_start(true);
254 void service_record::execute_transition() noexcept
256 // state is STARTED with restarting set true if we are running a smooth recovery.
257 if (service_state == service_state_t::STARTING || (service_state == service_state_t::STARTED
259 if (check_deps_started()) {
263 else if (service_state == service_state_t::STOPPING) {
264 if (stop_check_dependents()) {
265 waiting_for_deps = false;
271 void service_record::do_start() noexcept
273 if (pinned_stopped) return;
275 if (service_state != service_state_t::STARTING) {
279 service_state = service_state_t::STARTING;
281 waiting_for_deps = true;
283 // Ask dependencies to start, mark them as being waited on.
284 if (check_deps_started()) {
285 // Once all dependencies are started, we start properly:
290 void service_record::dependency_started() noexcept
292 // Note that we check for STARTED state here in case the service is in smooth recovery while pinned.
293 // In that case it will wait for dependencies to start before restarting the process.
294 if ((service_state == service_state_t::STARTING || service_state == service_state_t::STARTED)
295 && waiting_for_deps) {
296 services->add_transition_queue(this);
300 bool service_record::start_check_dependencies() noexcept
302 bool all_deps_started = true;
304 for (auto & dep : depends_on) {
305 service_record * to = dep.get_to();
306 if (to->service_state != service_state_t::STARTED) {
307 if (to->service_state != service_state_t::STARTING) {
308 to->prop_start = true;
309 services->add_prop_queue(to);
311 dep.waiting_on = true;
312 all_deps_started = false;
316 return all_deps_started;
319 bool service_record::check_deps_started() noexcept
321 for (auto & dep : depends_on) {
322 if (dep.waiting_on) {
330 void service_record::all_deps_started() noexcept
332 if (onstart_flags.starts_on_console && ! have_console) {
337 waiting_for_deps = false;
339 if (! can_proceed_to_start()) {
340 waiting_for_deps = true;
344 bool start_success = bring_up();
345 if (! start_success) {
350 void service_record::acquired_console() noexcept
352 waiting_for_console = false;
355 if (service_state != service_state_t::STARTING) {
356 // We got the console but no longer want it.
359 else if (check_deps_started()) {
363 // We got the console but can't use it yet.
368 void service_record::started() noexcept
370 // If we start on console but don't keep it, release it now:
371 if (have_console && ! onstart_flags.runs_on_console) {
372 bp_sys::tcsetpgrp(0, bp_sys::getpgrp());
376 log_service_started(get_name());
377 service_state = service_state_t::STARTED;
378 notify_listeners(service_event_t::STARTED);
380 if (onstart_flags.rw_ready) {
383 if (onstart_flags.log_ready) {
384 setup_external_log();
387 if (force_stop || desired_state == service_state_t::STOPPED) {
393 // Notify any dependents whose desired state is STARTED:
394 for (auto dept : dependents) {
395 dept->get_from()->dependency_started();
396 dept->waiting_on = false;
400 void service_record::failed_to_start(bool depfailed, bool immediate_stop) noexcept
402 if (waiting_for_console) {
403 services->unqueue_console(this);
404 waiting_for_console = false;
407 if (start_explicit) {
408 start_explicit = false;
412 // Cancel start of dependents:
413 for (auto & dept : dependents) {
414 switch (dept->dep_type) {
415 case dependency_type::REGULAR:
416 case dependency_type::MILESTONE:
417 if (dept->get_from()->service_state == service_state_t::STARTING) {
418 dept->get_from()->prop_failure = true;
419 services->add_prop_queue(dept->get_from());
422 case dependency_type::WAITS_FOR:
423 case dependency_type::SOFT:
424 if (dept->waiting_on) {
425 dept->waiting_on = false;
426 dept->get_from()->dependency_started();
430 // Always release now, so that our desired state will be STOPPED before we call
431 // stopped() below (if we do so). Otherwise it may decide to restart us.
432 if (dept->holding_acq) {
433 dept->holding_acq = false;
439 log_service_failed(get_name());
440 notify_listeners(service_event_t::FAILEDSTART);
442 if (immediate_stop) {
447 bool service_record::bring_up() noexcept
449 // default implementation: there is no process, so we are started.
454 // Mark this and all dependent services as force-stopped.
455 void service_record::forced_stop() noexcept
457 if (service_state != service_state_t::STOPPED) {
459 if (! pinned_started) {
461 services->add_transition_queue(this);
466 void service_record::dependent_stopped() noexcept
468 if (service_state == service_state_t::STOPPING && waiting_for_deps) {
469 services->add_transition_queue(this);
473 void service_record::stop(bool bring_down) noexcept
475 if (start_explicit) {
476 start_explicit = false;
480 if (bring_down && service_state != service_state_t::STOPPED
481 && service_state != service_state_t::STOPPING) {
482 stop_reason = stopped_reason_t::NORMAL;
487 void service_record::do_stop() noexcept
489 // A service that does actually stop for any reason should have its explicit activation released, unless
491 if (start_explicit && ! do_auto_restart()) {
492 start_explicit = false;
496 bool all_deps_stopped = stop_dependents();
498 if (service_state != service_state_t::STARTED) {
499 if (service_state == service_state_t::STARTING) {
500 // If waiting for a dependency, or waiting for the console, we can interrupt start. Otherwise,
501 // we need to delegate to can_interrupt_start() (which can be overridden).
502 if (! waiting_for_deps && ! waiting_for_console) {
503 if (! can_interrupt_start()) {
504 // Well this is awkward: we're going to have to continue starting. We can stop once
505 // we've reached the started state.
509 if (! interrupt_start()) {
510 // Now wait for service startup to actually end; we don't need to handle it here.
511 notify_listeners(service_event_t::STARTCANCELLED);
515 else if (waiting_for_console) {
516 services->unqueue_console(this);
517 waiting_for_console = false;
520 // We must have had desired_state == STARTED.
521 notify_listeners(service_event_t::STARTCANCELLED);
523 // Reaching this point, we are starting interruptibly - so we
524 // stop now (by falling through to below).
527 // If we're starting we need to wait for that to complete.
528 // If we're already stopping/stopped there's nothing to do.
533 if (pinned_started) return;
535 service_state = service_state_t::STOPPING;
536 waiting_for_deps = true;
537 if (all_deps_stopped) {
538 services->add_transition_queue(this);
542 bool service_record::stop_check_dependents() noexcept
544 bool all_deps_stopped = true;
545 for (auto dept : dependents) {
546 if (dept->dep_type == dependency_type::REGULAR && ! dept->get_from()->is_stopped()) {
547 all_deps_stopped = false;
552 return all_deps_stopped;
555 bool service_record::stop_dependents() noexcept
557 bool all_deps_stopped = true;
558 for (auto dept : dependents) {
559 if (dept->dep_type == dependency_type::REGULAR ||
560 (dept->dep_type == dependency_type::MILESTONE &&
561 dept->get_from()->service_state != service_state_t::STARTED)) {
562 if (! dept->get_from()->is_stopped()) {
563 // Note we check *first* since if the dependent service is not stopped,
564 // 1. We will issue a stop to it shortly and
565 // 2. It will notify us when stopped, at which point the stop_check_dependents()
566 // check is run anyway.
567 all_deps_stopped = false;
571 // If this service is to be forcefully stopped, dependents must also be.
572 dept->get_from()->forced_stop();
575 dept->get_from()->prop_stop = true;
576 services->add_prop_queue(dept->get_from());
579 // waits-for or soft dependency:
580 if (dept->waiting_on) {
581 dept->waiting_on = false;
582 dept->get_from()->dependency_started();
584 if (dept->holding_acq) {
585 dept->holding_acq = false;
586 // release without issuing stop, since we should be called only when this
587 // service is already stopped/stopping:
593 return all_deps_stopped;
596 // All dependents have stopped; we can stop now, too. Only called when STOPPING.
597 void service_record::bring_down() noexcept
599 waiting_for_deps = false;
603 void service_record::unpin() noexcept
605 if (pinned_started) {
606 pinned_started = false;
607 if (desired_state == service_state_t::STOPPED || force_stop) {
609 services->process_queues();
612 if (pinned_stopped) {
613 pinned_stopped = false;
614 if (desired_state == service_state_t::STARTED) {
616 services->process_queues();
621 void service_record::queue_for_console() noexcept
623 waiting_for_console = true;
624 services->append_console_queue(this);
627 void service_record::release_console() noexcept
629 have_console = false;
630 services->pull_console_queue();
633 bool service_record::interrupt_start() noexcept
638 void service_set::service_active(service_record *sr) noexcept
643 void service_set::service_inactive(service_record *sr) noexcept