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 if ((service_state == service_state_t::STARTING || service_state == service_state_t::STARTED)
258 && waiting_for_deps) {
259 services->add_transition_queue(this);
263 bool service_record::start_check_dependencies() noexcept
265 bool all_deps_started = true;
267 for (auto & dep : depends_on) {
268 service_record * to = dep.get_to();
269 if (to->service_state != service_state_t::STARTED) {
270 if (to->service_state != service_state_t::STARTING) {
271 to->prop_start = true;
272 services->add_prop_queue(to);
274 dep.waiting_on = true;
275 all_deps_started = false;
279 return all_deps_started;
282 bool service_record::check_deps_started() noexcept
284 for (auto & dep : depends_on) {
285 if (dep.waiting_on) {
293 void service_record::all_deps_started() noexcept
295 if (onstart_flags.starts_on_console && ! have_console) {
300 waiting_for_deps = false;
302 if (! can_proceed_to_start()) {
303 waiting_for_deps = true;
307 bool start_success = bring_up();
308 if (! start_success) {
313 void service_record::acquired_console() noexcept
315 waiting_for_console = false;
318 if (service_state != service_state_t::STARTING) {
319 // We got the console but no longer want it.
322 else if (check_deps_started()) {
326 // We got the console but can't use it yet.
331 void service_record::started() noexcept
333 // If we start on console but don't keep it, release it now:
334 if (have_console && ! onstart_flags.runs_on_console) {
335 bp_sys::tcsetpgrp(0, bp_sys::getpgrp());
339 log_service_started(get_name());
340 service_state = service_state_t::STARTED;
341 notify_listeners(service_event_t::STARTED);
343 if (onstart_flags.rw_ready) {
344 open_control_socket();
346 if (onstart_flags.log_ready) {
347 setup_external_log();
350 if (force_stop || desired_state == service_state_t::STOPPED) {
356 // Notify any dependents whose desired state is STARTED:
357 for (auto dept : dependents) {
358 dept->get_from()->dependency_started();
359 dept->waiting_on = false;
363 void service_record::failed_to_start(bool depfailed) noexcept
366 bp_sys::tcsetpgrp(0, bp_sys::getpgrp());
369 if (waiting_for_console) {
370 services->unqueue_console(this);
371 waiting_for_console = false;
374 log_service_failed(get_name());
375 service_state = service_state_t::STOPPED;
376 if (start_explicit) {
377 start_explicit = false;
380 notify_listeners(service_event_t::FAILEDSTART);
382 // Cancel start of dependents:
383 for (auto & dept : dependents) {
384 switch (dept->dep_type) {
385 case dependency_type::REGULAR:
386 case dependency_type::MILESTONE:
387 if (dept->get_from()->service_state == service_state_t::STARTING) {
388 dept->get_from()->prop_failure = true;
389 services->add_prop_queue(dept->get_from());
392 case dependency_type::WAITS_FOR:
393 case dependency_type::SOFT:
394 if (dept->waiting_on) {
395 dept->waiting_on = false;
396 dept->get_from()->dependency_started();
398 if (dept->holding_acq) {
399 dept->holding_acq = false;
406 bool service_record::bring_up() noexcept
408 // default implementation: there is no process, so we are started.
413 // Mark this and all dependent services as force-stopped.
414 void service_record::forced_stop() noexcept
416 if (service_state != service_state_t::STOPPED) {
418 if (! pinned_started) {
420 services->add_transition_queue(this);
425 void service_record::dependent_stopped() noexcept
427 if (service_state == service_state_t::STOPPING && waiting_for_deps) {
428 services->add_transition_queue(this);
432 void service_record::stop(bool bring_down) noexcept
434 if (start_explicit) {
435 start_explicit = false;
444 void service_record::do_stop() noexcept
446 if (pinned_started) return;
448 if (start_explicit && ! do_auto_restart()) {
449 start_explicit = false;
453 bool all_deps_stopped = stop_dependents();
455 if (service_state != service_state_t::STARTED) {
456 if (service_state == service_state_t::STARTING) {
457 // If waiting for a dependency, or waiting for the console, we can interrupt start. Otherwise,
458 // we need to delegate to can_interrupt_start() (which can be overridden).
459 if (! waiting_for_deps && ! waiting_for_console) {
460 if (! can_interrupt_start()) {
461 // Well this is awkward: we're going to have to continue starting. We can stop once we've
462 // reached the started state.
466 if (! interrupt_start()) {
467 // Now wait for service startup to actually end; we don't need to handle it here.
471 else if (waiting_for_console) {
472 services->unqueue_console(this);
473 waiting_for_console = false;
476 // We must have had desired_state == STARTED.
477 notify_listeners(service_event_t::STARTCANCELLED);
479 // Reaching this point, we are starting interruptibly - so we
480 // stop now (by falling through to below).
483 // If we're starting we need to wait for that to complete.
484 // If we're already stopping/stopped there's nothing to do.
489 service_state = service_state_t::STOPPING;
490 waiting_for_deps = true;
491 if (all_deps_stopped) {
492 services->add_transition_queue(this);
496 bool service_record::stop_check_dependents() noexcept
498 bool all_deps_stopped = true;
499 for (auto dept : dependents) {
500 if (dept->dep_type == dependency_type::REGULAR && ! dept->get_from()->is_stopped()) {
501 all_deps_stopped = false;
506 return all_deps_stopped;
509 bool service_record::stop_dependents() noexcept
511 bool all_deps_stopped = true;
512 for (auto dept : dependents) {
513 if (dept->dep_type == dependency_type::REGULAR) {
514 if (! dept->get_from()->is_stopped()) {
515 // Note we check *first* since if the dependent service is not stopped,
516 // 1. We will issue a stop to it shortly and
517 // 2. It will notify us when stopped, at which point the stop_check_dependents()
518 // check is run anyway.
519 all_deps_stopped = false;
523 // If this service is to be forcefully stopped, dependents must also be.
524 dept->get_from()->forced_stop();
527 dept->get_from()->prop_stop = true;
528 services->add_prop_queue(dept->get_from());
532 return all_deps_stopped;
535 // All dependents have stopped; we can stop now, too. Only called when STOPPING.
536 void service_record::bring_down() noexcept
538 waiting_for_deps = false;
542 void service_record::unpin() noexcept
544 if (pinned_started) {
545 pinned_started = false;
546 if (desired_state == service_state_t::STOPPED || force_stop) {
548 services->process_queues();
551 if (pinned_stopped) {
552 pinned_stopped = false;
553 if (desired_state == service_state_t::STARTED) {
555 services->process_queues();
560 void service_record::queue_for_console() noexcept
562 waiting_for_console = true;
563 services->append_console_queue(this);
566 void service_record::release_console() noexcept
568 have_console = false;
569 services->pull_console_queue();
572 bool service_record::interrupt_start() noexcept
574 if (onstart_flags.starts_on_console) {
575 services->unqueue_console(this);
580 void service_set::service_active(service_record *sr) noexcept
585 void service_set::service_inactive(service_record *sr) noexcept