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
55 if (onstart_flags.runs_on_console) {
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 start_explicit = false;
95 else if (required_by == 0) {
96 services->service_inactive(this);
100 log_service_stopped(service_name);
101 notify_listeners(service_event_t::STOPPED);
104 bool service_record::do_auto_restart() noexcept
107 return services->get_auto_restart();
112 void service_record::require() noexcept
114 if (required_by++ == 0) {
115 prop_require = !prop_release;
116 prop_release = false;
117 services->add_prop_queue(this);
121 void service_record::release(bool issue_stop) noexcept
123 if (--required_by == 0) {
124 desired_state = service_state_t::STOPPED;
126 // Can stop, and can release dependencies now. We don't need to issue a release if
127 // the require was pending though:
128 prop_release = !prop_require;
129 prop_require = false;
130 services->add_prop_queue(this);
132 if (service_state == service_state_t::STOPPED) {
133 services->service_inactive(this);
135 else if (issue_stop) {
141 void service_record::release_dependencies() noexcept
143 for (auto & dependency : depends_on) {
144 service_record * dep_to = dependency.get_to();
145 if (dependency.holding_acq) {
147 dependency.holding_acq = false;
152 void service_record::start(bool activate) noexcept
154 if (activate && ! start_explicit) {
156 start_explicit = true;
159 if (desired_state == service_state_t::STARTED && service_state != service_state_t::STOPPED) return;
161 bool was_active = service_state != service_state_t::STOPPED || desired_state != service_state_t::STOPPED;
162 desired_state = service_state_t::STARTED;
164 if (service_state != service_state_t::STOPPED) {
165 // We're already starting/started, or we are stopping and need to wait for
166 // that the complete.
167 if (service_state != service_state_t::STOPPING || ! can_interrupt_stop()) {
170 // We're STOPPING, and that can be interrupted. Our dependencies might be STOPPING,
171 // but if so they are waiting (for us), so they too can be instantly returned to
173 notify_listeners(service_event_t::STOPCANCELLED);
175 else if (! was_active) {
176 services->service_active(this);
179 service_state = service_state_t::STARTING;
180 waiting_for_deps = true;
182 if (start_check_dependencies()) {
183 services->add_transition_queue(this);
187 void service_record::do_propagation() noexcept
190 // Need to require all our dependencies
191 for (auto & dep : depends_on) {
192 dep.get_to()->require();
193 dep.holding_acq = true;
195 prop_require = false;
199 release_dependencies();
200 prop_release = false;
204 prop_failure = false;
205 failed_to_start(true);
219 void service_record::execute_transition() noexcept
221 // state is STARTED with restarting set true if we are running a smooth recovery.
222 if (service_state == service_state_t::STARTING || (service_state == service_state_t::STARTED
224 if (check_deps_started()) {
228 else if (service_state == service_state_t::STOPPING) {
229 if (stop_check_dependents()) {
230 waiting_for_deps = false;
236 void service_record::do_start() noexcept
238 if (pinned_stopped) return;
240 if (service_state != service_state_t::STARTING) {
244 service_state = service_state_t::STARTING;
246 waiting_for_deps = true;
248 // Ask dependencies to start, mark them as being waited on.
249 if (check_deps_started()) {
250 // Once all dependencies are started, we start properly:
255 void service_record::dependency_started() noexcept
257 // Note that we check for STARTED state here in case the service is in smooth recovery while pinned.
258 // In that case it will wait for dependencies to start before restarting the process.
259 if ((service_state == service_state_t::STARTING || service_state == service_state_t::STARTED)
260 && waiting_for_deps) {
261 services->add_transition_queue(this);
265 bool service_record::start_check_dependencies() noexcept
267 bool all_deps_started = true;
269 for (auto & dep : depends_on) {
270 service_record * to = dep.get_to();
271 if (to->service_state != service_state_t::STARTED) {
272 if (to->service_state != service_state_t::STARTING) {
273 to->prop_start = true;
274 services->add_prop_queue(to);
276 dep.waiting_on = true;
277 all_deps_started = false;
281 return all_deps_started;
284 bool service_record::check_deps_started() noexcept
286 for (auto & dep : depends_on) {
287 if (dep.waiting_on) {
295 void service_record::all_deps_started() noexcept
297 if (onstart_flags.starts_on_console && ! have_console) {
302 waiting_for_deps = false;
304 if (! can_proceed_to_start()) {
305 waiting_for_deps = true;
309 bool start_success = bring_up();
310 if (! start_success) {
315 void service_record::acquired_console() noexcept
317 waiting_for_console = false;
320 if (service_state != service_state_t::STARTING) {
321 // We got the console but no longer want it.
324 else if (check_deps_started()) {
328 // We got the console but can't use it yet.
333 void service_record::started() noexcept
335 // If we start on console but don't keep it, release it now:
336 if (have_console && ! onstart_flags.runs_on_console) {
337 bp_sys::tcsetpgrp(0, bp_sys::getpgrp());
341 log_service_started(get_name());
342 service_state = service_state_t::STARTED;
343 notify_listeners(service_event_t::STARTED);
345 if (onstart_flags.rw_ready) {
346 open_control_socket();
348 if (onstart_flags.log_ready) {
349 setup_external_log();
352 if (force_stop || desired_state == service_state_t::STOPPED) {
358 // Notify any dependents whose desired state is STARTED:
359 for (auto dept : dependents) {
360 dept->get_from()->dependency_started();
361 dept->waiting_on = false;
365 void service_record::failed_to_start(bool depfailed) noexcept
368 bp_sys::tcsetpgrp(0, bp_sys::getpgrp());
371 if (waiting_for_console) {
372 services->unqueue_console(this);
373 waiting_for_console = false;
376 log_service_failed(get_name());
377 service_state = service_state_t::STOPPED;
378 if (start_explicit) {
379 start_explicit = false;
382 notify_listeners(service_event_t::FAILEDSTART);
384 // Cancel start of dependents:
385 for (auto & dept : dependents) {
386 switch (dept->dep_type) {
387 case dependency_type::REGULAR:
388 case dependency_type::MILESTONE:
389 if (dept->get_from()->service_state == service_state_t::STARTING) {
390 dept->get_from()->prop_failure = true;
391 services->add_prop_queue(dept->get_from());
394 case dependency_type::WAITS_FOR:
395 case dependency_type::SOFT:
396 if (dept->waiting_on) {
397 dept->waiting_on = false;
398 dept->get_from()->dependency_started();
400 if (dept->holding_acq) {
401 dept->holding_acq = false;
408 bool service_record::bring_up() noexcept
410 // default implementation: there is no process, so we are started.
415 // Mark this and all dependent services as force-stopped.
416 void service_record::forced_stop() noexcept
418 if (service_state != service_state_t::STOPPED) {
420 if (! pinned_started) {
422 services->add_transition_queue(this);
427 void service_record::dependent_stopped() noexcept
429 if (service_state == service_state_t::STOPPING && waiting_for_deps) {
430 services->add_transition_queue(this);
434 void service_record::stop(bool bring_down) noexcept
436 if (start_explicit) {
437 start_explicit = false;
446 void service_record::do_stop() noexcept
448 if (pinned_started) return;
450 // A service that does actually stop for any reason should have its explicit activation released, unless
452 if (start_explicit && ! do_auto_restart()) {
453 start_explicit = false;
457 bool all_deps_stopped = stop_dependents();
459 if (service_state != service_state_t::STARTED) {
460 if (service_state == service_state_t::STARTING) {
461 // If waiting for a dependency, or waiting for the console, we can interrupt start. Otherwise,
462 // we need to delegate to can_interrupt_start() (which can be overridden).
463 if (! waiting_for_deps && ! waiting_for_console) {
464 if (! can_interrupt_start()) {
465 // Well this is awkward: we're going to have to continue starting. We can stop once we've
466 // reached the started state.
470 if (! interrupt_start()) {
471 // Now wait for service startup to actually end; we don't need to handle it here.
475 else if (waiting_for_console) {
476 services->unqueue_console(this);
477 waiting_for_console = false;
480 // We must have had desired_state == STARTED.
481 notify_listeners(service_event_t::STARTCANCELLED);
483 // Reaching this point, we are starting interruptibly - so we
484 // stop now (by falling through to below).
487 // If we're starting we need to wait for that to complete.
488 // If we're already stopping/stopped there's nothing to do.
493 service_state = service_state_t::STOPPING;
494 waiting_for_deps = true;
495 if (all_deps_stopped) {
496 services->add_transition_queue(this);
500 bool service_record::stop_check_dependents() noexcept
502 bool all_deps_stopped = true;
503 for (auto dept : dependents) {
504 if (dept->dep_type == dependency_type::REGULAR && ! dept->get_from()->is_stopped()) {
505 all_deps_stopped = false;
510 return all_deps_stopped;
513 bool service_record::stop_dependents() noexcept
515 bool all_deps_stopped = true;
516 for (auto dept : dependents) {
517 if (dept->dep_type == dependency_type::REGULAR) {
518 if (! dept->get_from()->is_stopped()) {
519 // Note we check *first* since if the dependent service is not stopped,
520 // 1. We will issue a stop to it shortly and
521 // 2. It will notify us when stopped, at which point the stop_check_dependents()
522 // check is run anyway.
523 all_deps_stopped = false;
527 // If this service is to be forcefully stopped, dependents must also be.
528 dept->get_from()->forced_stop();
531 dept->get_from()->prop_stop = true;
532 services->add_prop_queue(dept->get_from());
536 return all_deps_stopped;
539 // All dependents have stopped; we can stop now, too. Only called when STOPPING.
540 void service_record::bring_down() noexcept
542 waiting_for_deps = false;
546 void service_record::unpin() noexcept
548 if (pinned_started) {
549 pinned_started = false;
550 if (desired_state == service_state_t::STOPPED || force_stop) {
552 services->process_queues();
555 if (pinned_stopped) {
556 pinned_stopped = false;
557 if (desired_state == service_state_t::STARTED) {
559 services->process_queues();
564 void service_record::queue_for_console() noexcept
566 waiting_for_console = true;
567 services->append_console_queue(this);
570 void service_record::release_console() noexcept
572 have_console = false;
573 services->pull_console_queue();
576 bool service_record::interrupt_start() noexcept
578 if (onstart_flags.starts_on_console) {
579 services->unqueue_console(this);
584 void service_set::service_active(service_record *sr) noexcept
589 void service_set::service_inactive(service_record *sr) noexcept