10 #include <sys/ioctl.h>
12 #include <sys/socket.h>
19 #include "dinit-log.h"
20 #include "dinit-socket.h"
21 #include "dinit-util.h"
24 * service.cc - Service management.
25 * See service.h for details.
28 // Find the requested service by name
29 static service_record * find_service(const std::list<service_record *> & records,
30 const char *name) noexcept
33 list<service_record *>::const_iterator i = records.begin();
34 for ( ; i != records.end(); i++ ) {
35 if (strcmp((*i)->get_name().c_str(), name) == 0) {
42 service_record * service_set::find_service(const std::string &name) noexcept
44 return ::find_service(records, name.c_str());
47 void service_set::stop_service(const std::string & name) noexcept
49 service_record *record = find_service(name);
50 if (record != nullptr) {
56 // Called when a service has actually stopped; dependents have stopped already, unless this stop
57 // is due to an unexpected process termination.
58 void service_record::stopped() noexcept
60 if (onstart_flags.runs_on_console) {
61 tcsetpgrp(0, getpgrp());
62 discard_console_log_buffer();
68 // If we are a soft dependency of another target, break the acquisition from that target now:
69 for (auto & dependent : dependents) {
70 if (dependent->dep_type != dependency_type::REGULAR) {
71 if (dependent->holding_acq) {
72 dependent->holding_acq = false;
78 bool will_restart = (desired_state == service_state_t::STARTED)
79 && services->get_auto_restart();
81 for (auto dependency : depends_on) {
82 // we signal dependencies in case they are waiting for us to stop:
83 dependency.get_to()->dependent_stopped();
86 service_state = service_state_t::STOPPED;
89 // Desired state is "started".
94 if (socket_fd != -1) {
100 start_explicit = false;
103 else if (required_by == 0) {
104 services->service_inactive(this);
108 log_service_stopped(service_name);
109 notify_listeners(service_event_t::STOPPED);
113 bool service_record::do_auto_restart() noexcept
116 return services->get_auto_restart();
121 void service_record::emergency_stop() noexcept
123 if (! do_auto_restart() && start_explicit) {
124 start_explicit = false;
133 void service_record::require() noexcept
135 if (required_by++ == 0) {
136 prop_require = !prop_release;
137 prop_release = false;
138 services->add_prop_queue(this);
142 void service_record::release(bool issue_stop) noexcept
144 if (--required_by == 0) {
145 desired_state = service_state_t::STOPPED;
147 // Can stop, and can release dependencies now. We don't need to issue a release if
148 // the require was pending though:
149 prop_release = !prop_require;
150 prop_require = false;
151 services->add_prop_queue(this);
153 if (service_state == service_state_t::STOPPED) {
154 services->service_inactive(this);
156 else if (issue_stop) {
162 void service_record::release_dependencies() noexcept
164 for (auto & dependency : depends_on) {
165 service_record * dep_to = dependency.get_to();
166 if (dependency.holding_acq) {
168 dependency.holding_acq = false;
173 void service_record::start(bool activate) noexcept
175 if (activate && ! start_explicit) {
177 start_explicit = true;
180 if (desired_state == service_state_t::STARTED && service_state != service_state_t::STOPPED) return;
182 bool was_active = service_state != service_state_t::STOPPED || desired_state != service_state_t::STOPPED;
183 desired_state = service_state_t::STARTED;
185 if (service_state != service_state_t::STOPPED) {
186 // We're already starting/started, or we are stopping and need to wait for
187 // that the complete.
188 if (service_state != service_state_t::STOPPING || ! can_interrupt_stop()) {
191 // We're STOPPING, and that can be interrupted. Our dependencies might be STOPPING,
192 // but if so they are waiting (for us), so they too can be instantly returned to
194 notify_listeners(service_event_t::STOPCANCELLED);
196 else if (! was_active) {
197 services->service_active(this);
200 service_state = service_state_t::STARTING;
201 waiting_for_deps = true;
203 if (start_check_dependencies()) {
204 services->add_transition_queue(this);
208 void service_record::do_propagation() noexcept
211 // Need to require all our dependencies
212 for (auto & dep : depends_on) {
213 dep.get_to()->require();
214 dep.holding_acq = true;
216 prop_require = false;
220 release_dependencies();
221 prop_release = false;
225 prop_failure = false;
226 failed_to_start(true);
240 void service_record::execute_transition() noexcept
242 // state is STARTED with restarting set true if we are running a smooth recovery.
243 if (service_state == service_state_t::STARTING || (service_state == service_state_t::STARTED
245 if (check_deps_started()) {
246 bool have_console = service_state == service_state_t::STARTED && onstart_flags.runs_on_console;
247 all_deps_started(have_console);
250 else if (service_state == service_state_t::STOPPING) {
251 if (stop_check_dependents()) {
257 void service_record::do_start() noexcept
259 if (pinned_stopped) return;
261 if (service_state != service_state_t::STARTING) {
265 service_state = service_state_t::STARTING;
267 waiting_for_deps = true;
269 // Ask dependencies to start, mark them as being waited on.
270 if (check_deps_started()) {
271 // Once all dependencies are started, we start properly:
276 void service_record::dependency_started() noexcept
278 if ((service_state == service_state_t::STARTING || service_state == service_state_t::STARTED)
279 && waiting_for_deps) {
280 services->add_transition_queue(this);
284 bool service_record::start_check_dependencies() noexcept
286 bool all_deps_started = true;
288 for (auto & dep : depends_on) {
289 service_record * to = dep.get_to();
290 if (to->service_state != service_state_t::STARTED) {
291 if (to->service_state != service_state_t::STARTING) {
292 to->prop_start = true;
293 services->add_prop_queue(to);
295 dep.waiting_on = true;
296 all_deps_started = false;
300 return all_deps_started;
303 bool service_record::check_deps_started() noexcept
305 for (auto & dep : depends_on) {
306 if (dep.waiting_on) {
314 bool service_record::open_socket() noexcept
316 if (socket_path.empty() || socket_fd != -1) {
317 // No socket, or already open
321 const char * saddrname = socket_path.c_str();
323 // Check the specified socket path
324 struct stat stat_buf;
325 if (stat(saddrname, &stat_buf) == 0) {
326 if ((stat_buf.st_mode & S_IFSOCK) == 0) {
328 log(loglevel_t::ERROR, service_name, ": Activation socket file exists (and is not a socket)");
332 else if (errno != ENOENT) {
334 log(loglevel_t::ERROR, service_name, ": Error checking activation socket: ", strerror(errno));
338 // Remove stale socket file (if it exists).
339 // We won't test the return from unlink - if it fails other than due to ENOENT, we should get an
340 // error when we try to create the socket anyway.
343 uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + socket_path.length() + 1;
344 struct sockaddr_un * name = static_cast<sockaddr_un *>(malloc(sockaddr_size));
345 if (name == nullptr) {
346 log(loglevel_t::ERROR, service_name, ": Opening activation socket: out of memory");
350 name->sun_family = AF_UNIX;
351 strcpy(name->sun_path, saddrname);
353 int sockfd = dinit_socket(AF_UNIX, SOCK_STREAM, 0, SOCK_NONBLOCK | SOCK_CLOEXEC);
355 log(loglevel_t::ERROR, service_name, ": Error creating activation socket: ", strerror(errno));
360 if (bind(sockfd, (struct sockaddr *) name, sockaddr_size) == -1) {
361 log(loglevel_t::ERROR, service_name, ": Error binding activation socket: ", strerror(errno));
369 // POSIX (1003.1, 2013) says that fchown and fchmod don't necessarily work on sockets. We have to
370 // use chown and chmod instead.
371 if (chown(saddrname, socket_uid, socket_gid)) {
372 log(loglevel_t::ERROR, service_name, ": Error setting activation socket owner/group: ", strerror(errno));
377 if (chmod(saddrname, socket_perms) == -1) {
378 log(loglevel_t::ERROR, service_name, ": Error setting activation socket permissions: ", strerror(errno));
383 if (listen(sockfd, 128) == -1) { // 128 "seems reasonable".
384 log(loglevel_t::ERROR, ": Error listening on activation socket: ", strerror(errno));
393 void service_record::all_deps_started(bool has_console) noexcept
395 if (onstart_flags.starts_on_console && ! has_console) {
396 waiting_for_deps = true;
401 waiting_for_deps = false;
403 if (! can_proceed_to_start()) {
404 waiting_for_deps = true;
408 if (! open_socket()) {
412 bool start_success = bring_up();
413 if (! start_success) {
418 void service_record::acquired_console() noexcept
420 if (service_state != service_state_t::STARTING) {
421 // We got the console but no longer want it.
424 else if (check_deps_started()) {
425 all_deps_started(true);
428 // We got the console but can't use it yet.
434 void service_record::started() noexcept
436 if (onstart_flags.starts_on_console && ! onstart_flags.runs_on_console) {
437 tcsetpgrp(0, getpgrp());
441 log_service_started(get_name());
442 service_state = service_state_t::STARTED;
443 notify_listeners(service_event_t::STARTED);
445 if (onstart_flags.rw_ready) {
446 open_control_socket();
448 if (onstart_flags.log_ready) {
449 setup_external_log();
452 if (force_stop || desired_state == service_state_t::STOPPED) {
458 // Notify any dependents whose desired state is STARTED:
459 for (auto dept : dependents) {
460 dept->get_from()->dependency_started();
461 dept->waiting_on = false;
465 void service_record::failed_to_start(bool depfailed) noexcept
467 if (!depfailed && onstart_flags.starts_on_console) {
468 tcsetpgrp(0, getpgrp());
472 log_service_failed(get_name());
473 service_state = service_state_t::STOPPED;
474 if (start_explicit) {
475 start_explicit = false;
478 notify_listeners(service_event_t::FAILEDSTART);
480 // Cancel start of dependents:
481 for (auto & dept : dependents) {
482 switch (dept->dep_type) {
483 case dependency_type::REGULAR:
484 case dependency_type::MILESTONE:
485 if (dept->get_from()->service_state == service_state_t::STARTING) {
486 dept->get_from()->prop_failure = true;
487 services->add_prop_queue(dept->get_from());
490 case dependency_type::WAITS_FOR:
491 case dependency_type::SOFT:
492 if (dept->waiting_on) {
493 dept->waiting_on = false;
494 dept->get_from()->dependency_started();
496 if (dept->holding_acq) {
497 dept->holding_acq = false;
504 bool service_record::bring_up() noexcept
506 // default implementation: there is no process, so we are started.
511 // Mark this and all dependent services as force-stopped.
512 void service_record::forced_stop() noexcept
514 if (service_state != service_state_t::STOPPED) {
516 services->add_transition_queue(this);
520 void service_record::dependent_stopped() noexcept
522 if (service_state == service_state_t::STOPPING && waiting_for_deps) {
523 services->add_transition_queue(this);
527 void service_record::stop(bool bring_down) noexcept
529 if (start_explicit) {
530 start_explicit = false;
539 void service_record::do_stop() noexcept
541 if (pinned_started) return;
543 if (start_explicit && ! do_auto_restart()) {
544 start_explicit = false;
548 bool all_deps_stopped = stop_dependents();
550 if (service_state != service_state_t::STARTED) {
551 if (service_state == service_state_t::STARTING) {
552 if (! waiting_for_deps) {
553 if (! can_interrupt_start()) {
554 // Well this is awkward: we're going to have to continue starting. We can stop once we've
555 // reached the started state.
559 if (! interrupt_start()) {
560 // Now wait for service startup to actually end; we don't need to handle it here.
565 // We must have had desired_state == STARTED.
566 notify_listeners(service_event_t::STARTCANCELLED);
568 // Reaching this point, we are starting interruptibly - so we
569 // stop now (by falling through to below).
572 // If we're starting we need to wait for that to complete.
573 // If we're already stopping/stopped there's nothing to do.
578 service_state = service_state_t::STOPPING;
579 waiting_for_deps = true;
580 if (all_deps_stopped) {
581 services->add_transition_queue(this);
585 bool service_record::stop_check_dependents() noexcept
587 bool all_deps_stopped = true;
588 for (auto dept : dependents) {
589 if (dept->dep_type == dependency_type::REGULAR && ! dept->get_from()->is_stopped()) {
590 all_deps_stopped = false;
595 return all_deps_stopped;
598 bool service_record::stop_dependents() noexcept
600 bool all_deps_stopped = true;
601 for (auto dept : dependents) {
602 if (dept->dep_type == dependency_type::REGULAR) {
603 if (! dept->get_from()->is_stopped()) {
604 // Note we check *first* since if the dependent service is not stopped,
605 // 1. We will issue a stop to it shortly and
606 // 2. It will notify us when stopped, at which point the stop_check_dependents()
607 // check is run anyway.
608 all_deps_stopped = false;
612 // If this service is to be forcefully stopped, dependents must also be.
613 dept->get_from()->forced_stop();
616 dept->get_from()->prop_stop = true;
617 services->add_prop_queue(dept->get_from());
621 return all_deps_stopped;
624 // All dependents have stopped; we can stop now, too. Only called when STOPPING.
625 void service_record::bring_down() noexcept
627 waiting_for_deps = false;
631 void service_record::unpin() noexcept
633 if (pinned_started) {
634 pinned_started = false;
635 if (desired_state == service_state_t::STOPPED || force_stop) {
637 services->process_queues();
640 if (pinned_stopped) {
641 pinned_stopped = false;
642 if (desired_state == service_state_t::STARTED) {
644 services->process_queues();
649 void service_record::queue_for_console() noexcept
651 services->append_console_queue(this);
654 void service_record::release_console() noexcept
656 services->pull_console_queue();
659 bool service_record::interrupt_start() noexcept
661 services->unqueue_console(this);
665 void service_set::service_active(service_record *sr) noexcept
670 void service_set::service_inactive(service_record *sr) noexcept