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 // 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 notify_listeners(service_event_t::STOPPED);
114 bool service_record::do_auto_restart() noexcept
117 return services->get_auto_restart();
122 void service_record::require() noexcept
124 if (required_by++ == 0) {
125 prop_require = !prop_release;
126 prop_release = false;
127 services->add_prop_queue(this);
128 if (service_state != service_state_t::STARTING && service_state != service_state_t::STARTED) {
134 void service_record::release(bool issue_stop) noexcept
136 if (--required_by == 0) {
137 desired_state = service_state_t::STOPPED;
139 // Can stop, and can release dependencies now. We don't need to issue a release if
140 // the require was pending though:
141 prop_release = !prop_require;
142 prop_require = false;
143 services->add_prop_queue(this);
145 if (service_state == service_state_t::STOPPED) {
146 services->service_inactive(this);
148 else if (issue_stop) {
154 void service_record::release_dependencies() noexcept
156 for (auto & dependency : depends_on) {
157 service_record * dep_to = dependency.get_to();
158 if (dependency.holding_acq) {
159 // We must clear holding_acq before calling release, otherwise the dependency
160 // may decide to stop, check this link and release itself a second time.
161 dependency.holding_acq = false;
167 void service_record::start(bool activate) noexcept
169 if (activate && ! start_explicit) {
171 start_explicit = true;
174 if (desired_state == service_state_t::STARTED && service_state != service_state_t::STOPPED) return;
176 bool was_active = service_state != service_state_t::STOPPED || desired_state != service_state_t::STOPPED;
177 desired_state = service_state_t::STARTED;
179 if (service_state != service_state_t::STOPPED) {
180 // We're already starting/started, or we are stopping and need to wait for
181 // that the complete.
182 if (service_state != service_state_t::STOPPING || ! can_interrupt_stop()) {
185 // We're STOPPING, and that can be interrupted. Our dependencies might be STOPPING,
186 // but if so they are waiting (for us), so they too can be instantly returned to
188 notify_listeners(service_event_t::STOPCANCELLED);
190 else if (! was_active) {
191 services->service_active(this);
194 start_failed = false;
195 service_state = service_state_t::STARTING;
196 waiting_for_deps = true;
198 if (start_check_dependencies()) {
199 services->add_transition_queue(this);
203 void service_record::do_propagation() noexcept
206 // Need to require all our dependencies
207 for (auto & dep : depends_on) {
208 dep.get_to()->require();
209 dep.holding_acq = true;
211 prop_require = false;
215 release_dependencies();
216 prop_release = false;
220 prop_failure = false;
221 failed_to_start(true);
235 void service_record::execute_transition() noexcept
237 // state is STARTED with restarting set true if we are running a smooth recovery.
238 if (service_state == service_state_t::STARTING || (service_state == service_state_t::STARTED
240 if (check_deps_started()) {
244 else if (service_state == service_state_t::STOPPING) {
245 if (stop_check_dependents()) {
246 waiting_for_deps = false;
252 void service_record::do_start() noexcept
254 if (pinned_stopped) return;
256 if (service_state != service_state_t::STARTING) {
260 service_state = service_state_t::STARTING;
262 waiting_for_deps = true;
264 // Ask dependencies to start, mark them as being waited on.
265 if (check_deps_started()) {
266 // Once all dependencies are started, we start properly:
271 void service_record::dependency_started() noexcept
273 // Note that we check for STARTED state here in case the service is in smooth recovery while pinned.
274 // In that case it will wait for dependencies to start before restarting the process.
275 if ((service_state == service_state_t::STARTING || service_state == service_state_t::STARTED)
276 && waiting_for_deps) {
277 services->add_transition_queue(this);
281 bool service_record::start_check_dependencies() noexcept
283 bool all_deps_started = true;
285 for (auto & dep : depends_on) {
286 service_record * to = dep.get_to();
287 if (to->service_state != service_state_t::STARTED) {
288 if (to->service_state != service_state_t::STARTING) {
289 to->prop_start = true;
290 services->add_prop_queue(to);
292 dep.waiting_on = true;
293 all_deps_started = false;
297 return all_deps_started;
300 bool service_record::check_deps_started() noexcept
302 for (auto & dep : depends_on) {
303 if (dep.waiting_on) {
311 void service_record::all_deps_started() noexcept
313 if (onstart_flags.starts_on_console && ! have_console) {
318 waiting_for_deps = false;
320 if (! can_proceed_to_start()) {
321 waiting_for_deps = true;
325 bool start_success = bring_up();
326 if (! start_success) {
331 void service_record::acquired_console() noexcept
333 waiting_for_console = false;
336 if (service_state != service_state_t::STARTING) {
337 // We got the console but no longer want it.
340 else if (check_deps_started()) {
344 // We got the console but can't use it yet.
349 void service_record::started() noexcept
351 // If we start on console but don't keep it, release it now:
352 if (have_console && ! onstart_flags.runs_on_console) {
353 bp_sys::tcsetpgrp(0, bp_sys::getpgrp());
357 log_service_started(get_name());
358 service_state = service_state_t::STARTED;
359 notify_listeners(service_event_t::STARTED);
361 if (onstart_flags.rw_ready) {
362 open_control_socket();
364 if (onstart_flags.log_ready) {
365 setup_external_log();
368 if (force_stop || desired_state == service_state_t::STOPPED) {
374 // Notify any dependents whose desired state is STARTED:
375 for (auto dept : dependents) {
376 dept->get_from()->dependency_started();
377 dept->waiting_on = false;
381 void service_record::failed_to_start(bool depfailed, bool immediate_stop) noexcept
383 if (waiting_for_console) {
384 services->unqueue_console(this);
385 waiting_for_console = false;
388 if (start_explicit) {
389 start_explicit = false;
393 // Cancel start of dependents:
394 for (auto & dept : dependents) {
395 switch (dept->dep_type) {
396 case dependency_type::REGULAR:
397 case dependency_type::MILESTONE:
398 if (dept->get_from()->service_state == service_state_t::STARTING) {
399 dept->get_from()->prop_failure = true;
400 services->add_prop_queue(dept->get_from());
403 case dependency_type::WAITS_FOR:
404 case dependency_type::SOFT:
405 if (dept->waiting_on) {
406 dept->waiting_on = false;
407 dept->get_from()->dependency_started();
411 // Always release now, so that our desired state will be STOPPED before we call
412 // stopped() below (if we do so). Otherwise it may decide to restart us.
413 if (dept->holding_acq) {
414 dept->holding_acq = false;
420 log_service_failed(get_name());
421 notify_listeners(service_event_t::FAILEDSTART);
423 if (immediate_stop) {
428 bool service_record::bring_up() noexcept
430 // default implementation: there is no process, so we are started.
435 // Mark this and all dependent services as force-stopped.
436 void service_record::forced_stop() noexcept
438 if (service_state != service_state_t::STOPPED) {
440 if (! pinned_started) {
442 services->add_transition_queue(this);
447 void service_record::dependent_stopped() noexcept
449 if (service_state == service_state_t::STOPPING && waiting_for_deps) {
450 services->add_transition_queue(this);
454 void service_record::stop(bool bring_down) noexcept
456 if (start_explicit) {
457 start_explicit = false;
466 void service_record::do_stop() noexcept
468 // A service that does actually stop for any reason should have its explicit activation released, unless
470 if (start_explicit && ! do_auto_restart()) {
471 start_explicit = false;
475 bool all_deps_stopped = stop_dependents();
477 if (service_state != service_state_t::STARTED) {
478 if (service_state == service_state_t::STARTING) {
479 // If waiting for a dependency, or waiting for the console, we can interrupt start. Otherwise,
480 // we need to delegate to can_interrupt_start() (which can be overridden).
481 if (! waiting_for_deps && ! waiting_for_console) {
482 if (! can_interrupt_start()) {
483 // Well this is awkward: we're going to have to continue starting. We can stop once we've
484 // reached the started state.
488 if (! interrupt_start()) {
489 // Now wait for service startup to actually end; we don't need to handle it here.
490 notify_listeners(service_event_t::STARTCANCELLED);
494 else if (waiting_for_console) {
495 services->unqueue_console(this);
496 waiting_for_console = false;
499 // We must have had desired_state == STARTED.
500 notify_listeners(service_event_t::STARTCANCELLED);
502 // Reaching this point, we are starting interruptibly - so we
503 // stop now (by falling through to below).
506 // If we're starting we need to wait for that to complete.
507 // If we're already stopping/stopped there's nothing to do.
512 if (pinned_started) return;
514 service_state = service_state_t::STOPPING;
515 waiting_for_deps = true;
516 if (all_deps_stopped) {
517 services->add_transition_queue(this);
521 bool service_record::stop_check_dependents() noexcept
523 bool all_deps_stopped = true;
524 for (auto dept : dependents) {
525 if (dept->dep_type == dependency_type::REGULAR && ! dept->get_from()->is_stopped()) {
526 all_deps_stopped = false;
531 return all_deps_stopped;
534 bool service_record::stop_dependents() noexcept
536 bool all_deps_stopped = true;
537 for (auto dept : dependents) {
538 if (dept->dep_type == dependency_type::REGULAR ||
539 (dept->dep_type == dependency_type::MILESTONE &&
540 dept->get_from()->service_state != service_state_t::STARTED)) {
541 if (! dept->get_from()->is_stopped()) {
542 // Note we check *first* since if the dependent service is not stopped,
543 // 1. We will issue a stop to it shortly and
544 // 2. It will notify us when stopped, at which point the stop_check_dependents()
545 // check is run anyway.
546 all_deps_stopped = false;
550 // If this service is to be forcefully stopped, dependents must also be.
551 dept->get_from()->forced_stop();
554 dept->get_from()->prop_stop = true;
555 services->add_prop_queue(dept->get_from());
558 // waits-for or soft dependency:
559 if (dept->waiting_on) {
560 dept->waiting_on = false;
561 dept->get_from()->dependency_started();
563 if (dept->holding_acq) {
564 dept->holding_acq = false;
570 return all_deps_stopped;
573 // All dependents have stopped; we can stop now, too. Only called when STOPPING.
574 void service_record::bring_down() noexcept
576 waiting_for_deps = false;
580 void service_record::unpin() noexcept
582 if (pinned_started) {
583 pinned_started = false;
584 if (desired_state == service_state_t::STOPPED || force_stop) {
586 services->process_queues();
589 if (pinned_stopped) {
590 pinned_stopped = false;
591 if (desired_state == service_state_t::STARTED) {
593 services->process_queues();
598 void service_record::queue_for_console() noexcept
600 waiting_for_console = true;
601 services->append_console_queue(this);
604 void service_record::release_console() noexcept
606 have_console = false;
607 services->pull_console_queue();
610 bool service_record::interrupt_start() noexcept
615 void service_set::service_active(service_record *sr) noexcept
620 void service_set::service_inactive(service_record *sr) noexcept