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() 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);
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.
512 void service_record::run_child_proc(const char * const *args, const char *logfile, bool on_console,
513 int wpipefd, int csfd) noexcept
515 // Child process. Must not allocate memory (or otherwise risk throwing any exception)
516 // from here until exit().
518 // If the console already has a session leader, presumably it is us. On the other hand
519 // if it has no session leader, and we don't create one, then control inputs such as
520 // ^C will have no effect.
521 bool do_set_ctty = (tcgetsid(0) == -1);
523 // Copy signal mask, but unmask signals that we masked on startup. For the moment, we'll
524 // also block all signals, since apparently dup() can be interrupted (!!! really, POSIX??).
525 sigset_t sigwait_set;
527 sigfillset(&sigall_set);
528 sigprocmask(SIG_SETMASK, &sigall_set, &sigwait_set);
529 sigdelset(&sigwait_set, SIGCHLD);
530 sigdelset(&sigwait_set, SIGINT);
531 sigdelset(&sigwait_set, SIGTERM);
532 sigdelset(&sigwait_set, SIGQUIT);
534 constexpr int bufsz = ((CHAR_BIT * sizeof(pid_t)) / 3 + 2) + 11;
535 // "LISTEN_PID=" - 11 characters; the expression above gives a conservative estimate
536 // on the maxiumum number of bytes required for LISTEN=nnn, including nul terminator,
537 // where nnn is a pid_t in decimal (i.e. one decimal digit is worth just over 3 bits).
540 // "DINIT_CS_FD=" - 12 bytes. (we -1 from sizeof(int) in account of sign bit).
541 constexpr int csenvbufsz = ((CHAR_BIT * sizeof(int) - 1) / 3 + 2) + 12;
542 char csenvbuf[csenvbufsz];
544 int minfd = (socket_fd == -1) ? 3 : 4;
546 // Move wpipefd/csfd to another fd if necessary
547 if (wpipefd < minfd) {
548 wpipefd = fcntl(wpipefd, F_DUPFD_CLOEXEC, minfd);
549 if (wpipefd == -1) goto failure_out;
552 if (csfd != -1 && csfd < minfd) {
553 csfd = fcntl(csfd, F_DUPFD, minfd);
554 if (csfd == -1) goto failure_out;
557 if (socket_fd != -1) {
559 if (dup2(socket_fd, 3) == -1) goto failure_out;
560 if (socket_fd != 3) {
564 if (putenv(const_cast<char *>("LISTEN_FDS=1"))) goto failure_out;
565 snprintf(nbuf, bufsz, "LISTEN_PID=%jd", static_cast<intmax_t>(getpid()));
566 if (putenv(nbuf)) goto failure_out;
570 snprintf(csenvbuf, csenvbufsz, "DINIT_CS_FD=%d", csfd);
571 if (putenv(csenvbuf)) goto failure_out;
575 // Re-set stdin, stdout, stderr
576 close(0); close(1); close(2);
578 if (open("/dev/null", O_RDONLY) == 0) {
579 // stdin = 0. That's what we should have; proceed with opening
580 // stdout and stderr.
581 if (open(logfile, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR) != 1) {
584 if (dup2(1, 2) != 2) {
588 else goto failure_out;
590 // We have the option of creating a session and process group, or just a new process
591 // group. If we just create a new process group, the child process cannot make itself
592 // a session leader if it wants to do that (eg getty/login will generally want this).
593 // If we do neither, and we are running with a controlling terminal, a ^C or similar
594 // will also affect the child process (which probably isn't so bad, though since we
595 // will handle the shutdown ourselves it's not necessary). Creating a new session
596 // (and a new process group as part of that) seems like a safe bet, and has the
597 // advantage of letting us signal the process as part of a process group.
601 // "run on console" - run as a foreground job on the terminal/console device
603 // if do_set_ctty is false, we are the session leader; we are probably running
604 // as a user process. Don't create a new session leader in that case, and run
605 // as part of the parent session. Otherwise, the new session cannot claim the
606 // terminal as a controlling terminal (it is already claimed), meaning that it
607 // will not see control signals from ^C etc.
610 // Disable suspend (^Z) (and on some systems, delayed suspend / ^Y)
611 signal(SIGTSTP, SIG_IGN);
613 // Become session leader
615 ioctl(0, TIOCSCTTY, 0);
618 tcsetpgrp(0, getpgrp());
621 sigprocmask(SIG_SETMASK, &sigwait_set, nullptr);
623 execvp(args[0], const_cast<char **>(args));
625 // If we got here, the exec failed:
627 int exec_status = errno;
628 write(wpipefd, &exec_status, sizeof(int));
632 // Mark this and all dependent services as force-stopped.
633 void service_record::forced_stop() noexcept
635 if (service_state != service_state_t::STOPPED) {
637 services->add_transition_queue(this);
641 void service_record::dependent_stopped() noexcept
643 if (service_state == service_state_t::STOPPING && waiting_for_deps) {
644 services->add_transition_queue(this);
648 void service_record::stop(bool bring_down) noexcept
650 if (start_explicit) {
651 start_explicit = false;
660 void service_record::do_stop() noexcept
662 if (pinned_started) return;
664 if (start_explicit && ! do_auto_restart()) {
665 start_explicit = false;
667 if (required_by == 0) return; // release will re-call us anyway
670 bool all_deps_stopped = stop_dependents();
672 if (service_state != service_state_t::STARTED) {
673 if (service_state == service_state_t::STARTING) {
674 if (! waiting_for_deps) {
675 if (! can_interrupt_start()) {
676 // Well this is awkward: we're going to have to continue starting. We can stop once we've
677 // reached the started state.
681 if (! interrupt_start()) {
682 // Now wait for service startup to actually end; we don't need to handle it here.
687 // We must have had desired_state == STARTED.
688 notify_listeners(service_event_t::STARTCANCELLED);
690 // Reaching this point, we are starting interruptibly - so we
691 // stop now (by falling through to below).
694 // If we're starting we need to wait for that to complete.
695 // If we're already stopping/stopped there's nothing to do.
700 service_state = service_state_t::STOPPING;
701 waiting_for_deps = true;
702 if (all_deps_stopped) {
703 services->add_transition_queue(this);
707 bool service_record::stop_check_dependents() noexcept
709 bool all_deps_stopped = true;
710 for (auto dept : dependents) {
711 if (dept->dep_type == dependency_type::REGULAR && ! dept->get_from()->is_stopped()) {
712 all_deps_stopped = false;
717 return all_deps_stopped;
720 bool service_record::stop_dependents() noexcept
722 bool all_deps_stopped = true;
723 for (auto dept : dependents) {
724 if (dept->dep_type == dependency_type::REGULAR) {
725 if (! dept->get_from()->is_stopped()) {
726 // Note we check *first* since if the dependent service is not stopped,
727 // 1. We will issue a stop to it shortly and
728 // 2. It will notify us when stopped, at which point the stop_check_dependents()
729 // check is run anyway.
730 all_deps_stopped = false;
734 // If this service is to be forcefully stopped, dependents must also be.
735 dept->get_from()->forced_stop();
738 dept->get_from()->prop_stop = true;
739 services->add_prop_queue(dept->get_from());
743 return all_deps_stopped;
746 // All dependents have stopped; we can stop now, too. Only called when STOPPING.
747 void service_record::bring_down() noexcept
749 waiting_for_deps = false;
753 void service_record::unpin() noexcept
755 if (pinned_started) {
756 pinned_started = false;
757 if (desired_state == service_state_t::STOPPED || force_stop) {
759 services->process_queues();
762 if (pinned_stopped) {
763 pinned_stopped = false;
764 if (desired_state == service_state_t::STARTED) {
766 services->process_queues();
771 void service_record::queue_for_console() noexcept
773 services->append_console_queue(this);
776 void service_record::release_console() noexcept
778 services->pull_console_queue();
781 bool service_record::interrupt_start() noexcept
783 services->unqueue_console(this);
787 void service_set::service_active(service_record *sr) noexcept
792 void service_set::service_inactive(service_record *sr) noexcept