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 start_skipped = false;
196 service_state = service_state_t::STARTING;
197 waiting_for_deps = true;
199 if (start_check_dependencies()) {
200 services->add_transition_queue(this);
204 void service_record::do_propagation() noexcept
207 // Need to require all our dependencies
208 for (auto & dep : depends_on) {
209 dep.get_to()->require();
210 dep.holding_acq = true;
212 prop_require = false;
216 release_dependencies();
217 prop_release = false;
221 prop_failure = false;
222 failed_to_start(true);
236 void service_record::execute_transition() noexcept
238 // state is STARTED with restarting set true if we are running a smooth recovery.
239 if (service_state == service_state_t::STARTING || (service_state == service_state_t::STARTED
241 if (check_deps_started()) {
245 else if (service_state == service_state_t::STOPPING) {
246 if (stop_check_dependents()) {
247 waiting_for_deps = false;
253 void service_record::do_start() noexcept
255 if (pinned_stopped) return;
257 if (service_state != service_state_t::STARTING) {
261 service_state = service_state_t::STARTING;
263 waiting_for_deps = true;
265 // Ask dependencies to start, mark them as being waited on.
266 if (check_deps_started()) {
267 // Once all dependencies are started, we start properly:
272 void service_record::dependency_started() noexcept
274 // Note that we check for STARTED state here in case the service is in smooth recovery while pinned.
275 // In that case it will wait for dependencies to start before restarting the process.
276 if ((service_state == service_state_t::STARTING || service_state == service_state_t::STARTED)
277 && waiting_for_deps) {
278 services->add_transition_queue(this);
282 bool service_record::start_check_dependencies() noexcept
284 bool all_deps_started = true;
286 for (auto & dep : depends_on) {
287 service_record * to = dep.get_to();
288 if (to->service_state != service_state_t::STARTED) {
289 if (to->service_state != service_state_t::STARTING) {
290 to->prop_start = true;
291 services->add_prop_queue(to);
293 dep.waiting_on = true;
294 all_deps_started = false;
298 return all_deps_started;
301 bool service_record::check_deps_started() noexcept
303 for (auto & dep : depends_on) {
304 if (dep.waiting_on) {
312 void service_record::all_deps_started() noexcept
314 if (onstart_flags.starts_on_console && ! have_console) {
319 waiting_for_deps = false;
321 if (! can_proceed_to_start()) {
322 waiting_for_deps = true;
326 bool start_success = bring_up();
327 if (! start_success) {
332 void service_record::acquired_console() noexcept
334 waiting_for_console = false;
337 if (service_state != service_state_t::STARTING) {
338 // We got the console but no longer want it.
341 else if (check_deps_started()) {
345 // We got the console but can't use it yet.
350 void service_record::started() noexcept
352 // If we start on console but don't keep it, release it now:
353 if (have_console && ! onstart_flags.runs_on_console) {
354 bp_sys::tcsetpgrp(0, bp_sys::getpgrp());
358 log_service_started(get_name());
359 service_state = service_state_t::STARTED;
360 notify_listeners(service_event_t::STARTED);
362 if (onstart_flags.rw_ready) {
363 open_control_socket();
365 if (onstart_flags.log_ready) {
366 setup_external_log();
369 if (force_stop || desired_state == service_state_t::STOPPED) {
375 // Notify any dependents whose desired state is STARTED:
376 for (auto dept : dependents) {
377 dept->get_from()->dependency_started();
378 dept->waiting_on = false;
382 void service_record::failed_to_start(bool depfailed, bool immediate_stop) noexcept
384 if (waiting_for_console) {
385 services->unqueue_console(this);
386 waiting_for_console = false;
389 if (start_explicit) {
390 start_explicit = false;
394 // Cancel start of dependents:
395 for (auto & dept : dependents) {
396 switch (dept->dep_type) {
397 case dependency_type::REGULAR:
398 case dependency_type::MILESTONE:
399 if (dept->get_from()->service_state == service_state_t::STARTING) {
400 dept->get_from()->prop_failure = true;
401 services->add_prop_queue(dept->get_from());
404 case dependency_type::WAITS_FOR:
405 case dependency_type::SOFT:
406 if (dept->waiting_on) {
407 dept->waiting_on = false;
408 dept->get_from()->dependency_started();
412 // Always release now, so that our desired state will be STOPPED before we call
413 // stopped() below (if we do so). Otherwise it may decide to restart us.
414 if (dept->holding_acq) {
415 dept->holding_acq = false;
421 log_service_failed(get_name());
422 notify_listeners(service_event_t::FAILEDSTART);
424 if (immediate_stop) {
429 bool service_record::bring_up() noexcept
431 // default implementation: there is no process, so we are started.
436 // Mark this and all dependent services as force-stopped.
437 void service_record::forced_stop() noexcept
439 if (service_state != service_state_t::STOPPED) {
441 if (! pinned_started) {
443 services->add_transition_queue(this);
448 void service_record::dependent_stopped() noexcept
450 if (service_state == service_state_t::STOPPING && waiting_for_deps) {
451 services->add_transition_queue(this);
455 void service_record::stop(bool bring_down) noexcept
457 if (start_explicit) {
458 start_explicit = false;
462 if (bring_down && service_state != service_state_t::STOPPED) {
467 void service_record::do_stop() noexcept
469 // A service that does actually stop for any reason should have its explicit activation released, unless
471 if (start_explicit && ! do_auto_restart()) {
472 start_explicit = false;
476 bool all_deps_stopped = stop_dependents();
478 if (service_state != service_state_t::STARTED) {
479 if (service_state == service_state_t::STARTING) {
480 // If waiting for a dependency, or waiting for the console, we can interrupt start. Otherwise,
481 // we need to delegate to can_interrupt_start() (which can be overridden).
482 if (! waiting_for_deps && ! waiting_for_console) {
483 if (! can_interrupt_start()) {
484 // Well this is awkward: we're going to have to continue starting. We can stop once we've
485 // reached the started state.
489 if (! interrupt_start()) {
490 // Now wait for service startup to actually end; we don't need to handle it here.
491 notify_listeners(service_event_t::STARTCANCELLED);
495 else if (waiting_for_console) {
496 services->unqueue_console(this);
497 waiting_for_console = false;
500 // We must have had desired_state == STARTED.
501 notify_listeners(service_event_t::STARTCANCELLED);
503 // Reaching this point, we are starting interruptibly - so we
504 // stop now (by falling through to below).
507 // If we're starting we need to wait for that to complete.
508 // If we're already stopping/stopped there's nothing to do.
513 if (pinned_started) return;
515 service_state = service_state_t::STOPPING;
516 waiting_for_deps = true;
517 if (all_deps_stopped) {
518 services->add_transition_queue(this);
522 bool service_record::stop_check_dependents() noexcept
524 bool all_deps_stopped = true;
525 for (auto dept : dependents) {
526 if (dept->dep_type == dependency_type::REGULAR && ! dept->get_from()->is_stopped()) {
527 all_deps_stopped = false;
532 return all_deps_stopped;
535 bool service_record::stop_dependents() noexcept
537 bool all_deps_stopped = true;
538 for (auto dept : dependents) {
539 if (dept->dep_type == dependency_type::REGULAR ||
540 (dept->dep_type == dependency_type::MILESTONE &&
541 dept->get_from()->service_state != service_state_t::STARTED)) {
542 if (! dept->get_from()->is_stopped()) {
543 // Note we check *first* since if the dependent service is not stopped,
544 // 1. We will issue a stop to it shortly and
545 // 2. It will notify us when stopped, at which point the stop_check_dependents()
546 // check is run anyway.
547 all_deps_stopped = false;
551 // If this service is to be forcefully stopped, dependents must also be.
552 dept->get_from()->forced_stop();
555 dept->get_from()->prop_stop = true;
556 services->add_prop_queue(dept->get_from());
559 // waits-for or soft dependency:
560 if (dept->waiting_on) {
561 dept->waiting_on = false;
562 dept->get_from()->dependency_started();
564 if (dept->holding_acq) {
565 dept->holding_acq = false;
571 return all_deps_stopped;
574 // All dependents have stopped; we can stop now, too. Only called when STOPPING.
575 void service_record::bring_down() noexcept
577 waiting_for_deps = false;
581 void service_record::unpin() noexcept
583 if (pinned_started) {
584 pinned_started = false;
585 if (desired_state == service_state_t::STOPPED || force_stop) {
587 services->process_queues();
590 if (pinned_stopped) {
591 pinned_stopped = false;
592 if (desired_state == service_state_t::STARTED) {
594 services->process_queues();
599 void service_record::queue_for_console() noexcept
601 waiting_for_console = true;
602 services->append_console_queue(this);
605 void service_record::release_console() noexcept
607 have_console = false;
608 services->pull_console_queue();
611 bool service_record::interrupt_start() noexcept
616 void service_set::service_active(service_record *sr) noexcept
621 void service_set::service_inactive(service_record *sr) noexcept