10 #include <sys/ioctl.h>
12 #include <sys/socket.h>
18 #include "dinit-log.h"
19 #include "dinit-socket.h"
22 * service.cc - Service management.
23 * See service.h for details.
27 void open_control_socket(bool report_ro_failure = true) noexcept;
28 void setup_external_log() noexcept;
29 extern eventloop_t eventLoop;
31 // Find the requested service by name
32 static service_record * find_service(const std::list<service_record *> & records,
33 const char *name) noexcept
36 list<service_record *>::const_iterator i = records.begin();
37 for ( ; i != records.end(); i++ ) {
38 if (strcmp((*i)->getServiceName().c_str(), name) == 0) {
42 return (service_record *)0;
45 service_record * service_set::find_service(const std::string &name) noexcept
47 return ::find_service(records, name.c_str());
50 void service_set::startService(const char *name)
53 service_record *record = loadServiceRecord(name);
59 void service_set::stopService(const std::string & name) noexcept
61 service_record *record = find_service(name);
62 if (record != nullptr) {
68 // Called when a service has actually stopped; dependents have stopped already, unless this stop
69 // is due to an unexpected process termination.
70 void service_record::stopped() noexcept
72 if (onstart_flags.runs_on_console) {
73 tcsetpgrp(0, getpgrp());
74 discard_console_log_buffer();
80 // If we are a soft dependency of another target, break the acquisition from that target now:
81 for (auto dependent : soft_dpts) {
82 if (dependent->holding_acq) {
83 dependent->holding_acq = false;
88 bool will_restart = (desired_state == service_state_t::STARTED)
89 && services->get_auto_restart();
91 for (auto dependency : depends_on) {
92 // we signal dependencies in case they are waiting for us to stop:
93 dependency->dependentStopped();
96 service_state = service_state_t::STOPPED;
99 // Desired state is "started".
104 if (socket_fd != -1) {
109 if (start_explicit) {
110 start_explicit = false;
113 else if (required_by == 0) {
114 services->service_inactive(this);
118 logServiceStopped(service_name);
119 notifyListeners(service_event::STOPPED);
122 dasynq::rearm service_child_watcher::status_change(eventloop_t &loop, pid_t child, int status) noexcept
124 base_process_service *sr = service;
127 sr->exit_status = status;
129 // Ok, for a process service, any process death which we didn't rig
130 // ourselves is a bit... unexpected. Probably, the child died because
131 // we asked it to (sr->service_state == STOPPING). But even if
132 // we didn't, there's not much we can do.
134 if (sr->waiting_for_execstat) {
135 // We still don't have an exec() status from the forked child, wait for that
136 // before doing any further processing.
137 return rearm::REMOVE;
140 // Must deregister now since handle_exit_status might result in re-launch:
141 deregister(loop, child);
143 sr->handle_exit_status(status);
144 return rearm::REMOVED;
147 bool service_record::do_auto_restart() noexcept
150 return services->get_auto_restart();
155 void service_record::emergency_stop() noexcept
157 if (! do_auto_restart() && start_explicit) {
158 start_explicit = false;
166 void process_service::handle_exit_status(int exit_status) noexcept
168 bool did_exit = WIFEXITED(exit_status);
169 bool was_signalled = WIFSIGNALED(exit_status);
171 if (exit_status != 0 && service_state != service_state_t::STOPPING) {
173 log(LogLevel::ERROR, "Service ", service_name, " process terminated with exit code ", WEXITSTATUS(exit_status));
175 else if (was_signalled) {
176 log(LogLevel::ERROR, "Service ", service_name, " terminated due to signal ", WTERMSIG(exit_status));
180 if (service_state == service_state_t::STARTING) {
181 if (did_exit && WEXITSTATUS(exit_status) == 0) {
188 else if (service_state == service_state_t::STOPPING) {
189 // We won't log a non-zero exit status or termination due to signal here -
190 // we assume that the process died because we signalled it.
193 else if (smooth_recovery && service_state == service_state_t::STARTED && desired_state == service_state_t::STARTED) {
194 // TODO if we are pinned-started then we should probably check
195 // that dependencies have started before trying to re-start the
197 if (! restart_ps_process()) {
199 services->processQueues(false);
206 services->processQueues(false);
209 void bgproc_service::handle_exit_status(int exit_status) noexcept
211 bool did_exit = WIFEXITED(exit_status);
212 bool was_signalled = WIFSIGNALED(exit_status);
214 if (exit_status != 0 && service_state != service_state_t::STOPPING) {
216 log(LogLevel::ERROR, "Service ", service_name, " process terminated with exit code ", WEXITSTATUS(exit_status));
218 else if (was_signalled) {
219 log(LogLevel::ERROR, "Service ", service_name, " terminated due to signal ", WTERMSIG(exit_status));
223 if (doing_recovery) {
225 doing_recovery = false;
226 bool need_stop = false;
227 if ((did_exit && WEXITSTATUS(exit_status) != 0) || was_signalled) {
231 // We need to re-read the PID, since it has now changed.
232 if (pid_file.length() != 0) {
233 if (! read_pid_file()) {
240 // Failed startup: no auto-restart.
242 services->processQueues(false);
248 if (service_state == service_state_t::STARTING) {
249 // POSIX requires that if the process exited clearly with a status code of 0,
250 // the exit status value will be 0:
251 if (exit_status == 0) {
252 if (pid_file.length() != 0 && ! read_pid_file()) {
263 else if (service_state == service_state_t::STOPPING) {
264 // We won't log a non-zero exit status or termination due to signal here -
265 // we assume that the process died because we signalled it.
268 else if (smooth_recovery && service_state == service_state_t::STARTED && desired_state == service_state_t::STARTED) {
269 // TODO if we are pinned-started then we should probably check
270 // that dependencies have started before trying to re-start the
272 doing_recovery = true;
273 if (! restart_ps_process()) {
275 services->processQueues();
280 // we must be STARTED
281 if (! do_auto_restart() && start_explicit) {
282 start_explicit = false;
289 services->processQueues(false);
292 void scripted_service::handle_exit_status(int exit_status) noexcept
294 bool did_exit = WIFEXITED(exit_status);
295 bool was_signalled = WIFSIGNALED(exit_status);
297 if (service_state == service_state_t::STOPPING) {
298 if (did_exit && WEXITSTATUS(exit_status) == 0) {
302 // ??? failed to stop! Let's log it as info:
304 log(LogLevel::INFO, "Service ", service_name, " stop command failed with exit code ", WEXITSTATUS(exit_status));
306 else if (was_signalled) {
307 log(LogLevel::INFO, "Serivice ", service_name, " stop command terminated due to signal ", WTERMSIG(exit_status));
309 // Just assume that we stopped, so that any dependencies
313 services->processQueues(false);
316 if (exit_status == 0) {
322 log(LogLevel::ERROR, "Service ", service_name, " command failed with exit code ", WEXITSTATUS(exit_status));
324 else if (was_signalled) {
325 log(LogLevel::ERROR, "Service ", service_name, " command terminated due to signal ", WTERMSIG(exit_status));
329 services->processQueues(true);
333 rearm exec_status_pipe_watcher::fd_event(eventloop_t &loop, int fd, int flags) noexcept
335 base_process_service *sr = service;
336 sr->waiting_for_execstat = false;
339 int r = read(get_watched_fd(), &exec_status, sizeof(int));
341 close(get_watched_fd());
344 // We read an errno code; exec() failed, and the service startup failed.
346 sr->child_listener.deregister(eventLoop, sr->pid);
349 log(LogLevel::ERROR, sr->service_name, ": execution failed: ", strerror(exec_status));
350 if (sr->service_state == service_state_t::STARTING) {
351 sr->failed_to_start();
353 else if (sr->service_state == service_state_t::STOPPING) {
354 // Must be a scripted service. We've logged the failure, but it's probably better
355 // not to leave the service in STARTED state:
361 if (sr->record_type == service_type::PROCESS) {
362 // This could be a smooth recovery (state already STARTED). Even more, the process
363 // might be stopped (and killed via a signal) during smooth recovery. We don't to
364 // process startup again in either case, so we check for state STARTING:
365 if (sr->service_state == service_state_t::STARTING) {
371 // Somehow the process managed to complete before we even saw the status.
372 sr->handle_exit_status(sr->exit_status);
376 sr->services->processQueues(true);
378 return rearm::REMOVED;
381 void service_record::require() noexcept
383 if (required_by++ == 0) {
384 prop_require = !prop_release;
385 prop_release = false;
386 services->addToPropQueue(this);
390 void service_record::release() noexcept
392 if (--required_by == 0) {
393 desired_state = service_state_t::STOPPED;
395 // Can stop, and can release dependencies now. We don't need to issue a release if
396 // the require was pending though:
397 prop_release = !prop_require;
398 prop_require = false;
399 services->addToPropQueue(this);
401 if (service_state == service_state_t::STOPPED) {
402 services->service_inactive(this);
410 void service_record::release_dependencies() noexcept
412 for (sr_iter i = depends_on.begin(); i != depends_on.end(); ++i) {
416 for (auto i = soft_deps.begin(); i != soft_deps.end(); ++i) {
417 service_record * to = i->getTo();
418 if (i->holding_acq) {
420 i->holding_acq = false;
425 void service_record::start(bool activate) noexcept
427 if (activate && ! start_explicit) {
429 start_explicit = true;
432 if (desired_state == service_state_t::STARTED && service_state != service_state_t::STOPPED) return;
434 bool was_active = service_state != service_state_t::STOPPED || desired_state != service_state_t::STOPPED;
435 desired_state = service_state_t::STARTED;
437 if (service_state != service_state_t::STOPPED) {
438 // We're already starting/started, or we are stopping and need to wait for
439 // that the complete.
440 if (service_state != service_state_t::STOPPING || ! can_interrupt_stop()) {
443 // We're STOPPING, and that can be interrupted. Our dependencies might be STOPPING,
444 // but if so they are waiting (for us), so they too can be instantly returned to
446 notifyListeners(service_event::STOPCANCELLED);
448 else if (! was_active) {
449 services->service_active(this);
452 service_state = service_state_t::STARTING;
453 waiting_for_deps = true;
455 if (startCheckDependencies(true)) {
456 services->addToStartQueue(this);
460 void service_record::do_propagation() noexcept
463 // Need to require all our dependencies
464 for (sr_iter i = depends_on.begin(); i != depends_on.end(); ++i) {
468 for (auto i = soft_deps.begin(); i != soft_deps.end(); ++i) {
469 service_record * to = i->getTo();
471 i->holding_acq = true;
474 prop_require = false;
478 release_dependencies();
479 prop_release = false;
483 prop_failure = false;
484 failed_to_start(true);
498 void service_record::execute_transition() noexcept
500 if (service_state == service_state_t::STARTING) {
501 if (startCheckDependencies(false)) {
502 allDepsStarted(false);
505 else if (service_state == service_state_t::STOPPING) {
506 if (stopCheckDependents()) {
512 void service_record::do_start() noexcept
514 if (pinned_stopped) return;
516 if (service_state != service_state_t::STARTING) {
520 service_state = service_state_t::STARTING;
522 waiting_for_deps = true;
524 // Ask dependencies to start, mark them as being waited on.
525 if (startCheckDependencies(false)) {
526 // Once all dependencies are started, we start properly:
531 void service_record::dependencyStarted() noexcept
533 if (service_state == service_state_t::STARTING && waiting_for_deps) {
534 services->addToStartQueue(this);
538 bool service_record::startCheckDependencies(bool start_deps) noexcept
540 bool all_deps_started = true;
542 for (sr_iter i = depends_on.begin(); i != depends_on.end(); ++i) {
543 if ((*i)->service_state != service_state_t::STARTED) {
545 all_deps_started = false;
546 (*i)->prop_start = true;
547 services->addToPropQueue(*i);
555 for (auto i = soft_deps.begin(); i != soft_deps.end(); ++i) {
556 service_record * to = i->getTo();
558 if (to->service_state != service_state_t::STARTED) {
559 to->prop_start = true;
560 services->addToPropQueue(to);
561 i->waiting_on = true;
562 all_deps_started = false;
565 i->waiting_on = false;
568 else if (i->waiting_on) {
569 if (to->service_state != service_state_t::STARTING) {
570 // Service has either started or is no longer starting
571 i->waiting_on = false;
574 // We are still waiting on this service
580 return all_deps_started;
583 bool service_record::open_socket() noexcept
585 if (socket_path.empty() || socket_fd != -1) {
586 // No socket, or already open
590 const char * saddrname = socket_path.c_str();
591 uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + socket_path.length() + 1;
593 struct sockaddr_un * name = static_cast<sockaddr_un *>(malloc(sockaddr_size));
594 if (name == nullptr) {
595 log(LogLevel::ERROR, service_name, ": Opening activation socket: out of memory");
599 // Un-link any stale socket. TODO: safety check? should at least confirm the path is a socket.
602 name->sun_family = AF_UNIX;
603 strcpy(name->sun_path, saddrname);
605 int sockfd = dinit_socket(AF_UNIX, SOCK_STREAM, 0, SOCK_NONBLOCK | SOCK_CLOEXEC);
607 log(LogLevel::ERROR, service_name, ": Error creating activation socket: ", strerror(errno));
612 if (bind(sockfd, (struct sockaddr *) name, sockaddr_size) == -1) {
613 log(LogLevel::ERROR, service_name, ": Error binding activation socket: ", strerror(errno));
621 // POSIX (1003.1, 2013) says that fchown and fchmod don't necesarily work on sockets. We have to
622 // use chown and chmod instead.
623 if (chown(saddrname, socket_uid, socket_gid)) {
624 log(LogLevel::ERROR, service_name, ": Error setting activation socket owner/group: ", strerror(errno));
629 if (chmod(saddrname, socket_perms) == -1) {
630 log(LogLevel::ERROR, service_name, ": Error setting activation socket permissions: ", strerror(errno));
635 if (listen(sockfd, 128) == -1) { // 128 "seems reasonable".
636 log(LogLevel::ERROR, ": Error listening on activation socket: ", strerror(errno));
645 void service_record::allDepsStarted(bool has_console) noexcept
647 if (onstart_flags.starts_on_console && ! has_console) {
648 waiting_for_deps = true;
653 waiting_for_deps = false;
655 // We overload can_interrupt_start to check whether there is any other
656 // process (eg restart timer) that needs to finish before starting.
657 if (can_interrupt_start()) {
658 waiting_for_deps = true;
662 if (! open_socket()) {
666 bool start_success = start_ps_process();
667 if (! start_success) {
672 void service_record::acquiredConsole() noexcept
674 if (service_state != service_state_t::STARTING) {
675 // We got the console but no longer want it.
678 else if (startCheckDependencies(false)) {
679 allDepsStarted(true);
682 // We got the console but can't use it yet.
687 bool bgproc_service::read_pid_file() noexcept
689 const char *pid_file_c = pid_file.c_str();
690 int fd = open(pid_file_c, O_CLOEXEC);
692 char pidbuf[21]; // just enought to hold any 64-bit integer
693 int r = read(fd, pidbuf, 20);
695 pidbuf[r] = 0; // store nul terminator
696 pid = std::atoi(pidbuf);
697 if (kill(pid, 0) == 0) {
698 child_listener.add_watch(eventLoop, pid);
701 log(LogLevel::ERROR, service_name, ": pid read from pidfile (", pid, ") is not valid");
711 log(LogLevel::ERROR, service_name, ": read pid file: ", strerror(errno));
716 void service_record::started() noexcept
718 if (onstart_flags.starts_on_console && ! onstart_flags.runs_on_console) {
719 tcsetpgrp(0, getpgrp());
723 logServiceStarted(service_name);
724 service_state = service_state_t::STARTED;
725 notifyListeners(service_event::STARTED);
727 if (onstart_flags.rw_ready) {
728 open_control_socket();
730 if (onstart_flags.log_ready) {
731 setup_external_log();
734 if (force_stop || desired_state == service_state_t::STOPPED) {
740 // Notify any dependents whose desired state is STARTED:
741 for (auto i = dependents.begin(); i != dependents.end(); i++) {
742 (*i)->dependencyStarted();
744 for (auto i = soft_dpts.begin(); i != soft_dpts.end(); i++) {
745 (*i)->getFrom()->dependencyStarted();
749 void service_record::failed_to_start(bool depfailed) noexcept
751 if (!depfailed && onstart_flags.starts_on_console) {
752 tcsetpgrp(0, getpgrp());
756 logServiceFailed(service_name);
757 service_state = service_state_t::STOPPED;
758 if (start_explicit) {
759 start_explicit = false;
762 notifyListeners(service_event::FAILEDSTART);
764 // Cancel start of dependents:
765 for (sr_iter i = dependents.begin(); i != dependents.end(); i++) {
766 if ((*i)->service_state == service_state_t::STARTING) {
767 (*i)->prop_failure = true;
768 services->addToPropQueue(*i);
771 for (auto i = soft_dpts.begin(); i != soft_dpts.end(); i++) {
772 // We can send 'start', because this is only a soft dependency.
773 // Our startup failure means that they don't have to wait for us.
774 if ((*i)->waiting_on) {
775 (*i)->holding_acq = false;
776 (*i)->waiting_on = false;
777 (*i)->getFrom()->dependencyStarted();
783 bool service_record::start_ps_process() noexcept
785 // default implementation: there is no process, so we are started.
790 bool base_process_service::start_ps_process() noexcept
793 return restart_ps_process();
796 eventLoop.get_time(restart_interval_time, clock_type::MONOTONIC);
797 restart_interval_count = 0;
798 return start_ps_process(exec_arg_parts, onstart_flags.starts_on_console);
802 bool base_process_service::start_ps_process(const std::vector<const char *> &cmd, bool on_console) noexcept
804 // In general, you can't tell whether fork/exec is successful. We use a pipe to communicate
805 // success/failure from the child to the parent. The pipe is set CLOEXEC so a successful
806 // exec closes the pipe, and the parent sees EOF. If the exec is unsuccessful, the errno
807 // is written to the pipe, and the parent can read it.
809 eventLoop.get_time(last_start_time, clock_type::MONOTONIC);
812 if (pipe2(pipefd, O_CLOEXEC)) {
813 log(LogLevel::ERROR, service_name, ": can't create status check pipe: ", strerror(errno));
817 const char * logfile = this->logfile.c_str();
819 logfile = "/dev/null";
822 bool child_status_registered = false;
823 control_conn_t *control_conn = nullptr;
825 int control_socket[2] = {-1, -1};
826 if (onstart_flags.pass_cs_fd) {
827 if (dinit_socketpair(AF_UNIX, SOCK_STREAM, /* protocol */ 0, control_socket, SOCK_NONBLOCK)) {
828 log(LogLevel::ERROR, service_name, ": can't create control socket: ", strerror(errno));
832 // Make the server side socket close-on-exec:
833 int fdflags = fcntl(control_socket[0], F_GETFD);
834 fcntl(control_socket[0], F_SETFD, fdflags | FD_CLOEXEC);
837 control_conn = new control_conn_t(&eventLoop, services, control_socket[0]);
839 catch (std::exception &exc) {
840 log(LogLevel::ERROR, service_name, ": can't launch process; out of memory");
845 // Set up complete, now fork and exec:
850 // We add the status listener with a high priority (i.e. low priority value) so that process
851 // termination is handled early. This means we have always recorded that the process is
852 // terminated by the time that we handle events that might otherwise cause us to signal the
853 // process, so we avoid sending a signal to an invalid (and possibly recycled) process ID.
854 child_status_listener.add_watch(eventLoop, pipefd[0], IN_EVENTS, true, DEFAULT_PRIORITY - 10);
855 child_status_registered = true;
857 forkpid = child_listener.fork(eventLoop);
859 catch (std::exception &e) {
860 log(LogLevel::ERROR, service_name, ": Could not fork: ", e.what());
865 run_child_proc(cmd.data(), logfile, on_console, pipefd[1], control_socket[1]);
869 close(pipefd[1]); // close the 'other end' fd
870 if (control_socket[1] != -1) {
871 close(control_socket[1]);
875 waiting_for_execstat = true;
882 if (child_status_registered) {
883 child_status_listener.deregister(eventLoop);
886 if (onstart_flags.pass_cs_fd) {
890 close(control_socket[0]);
891 close(control_socket[1]);
901 void service_record::run_child_proc(const char * const *args, const char *logfile, bool on_console,
902 int wpipefd, int csfd) noexcept
904 // Child process. Must not allocate memory (or otherwise risk throwing any exception)
905 // from here until exit().
907 // If the console already has a session leader, presumably it is us. On the other hand
908 // if it has no session leader, and we don't create one, then control inputs such as
909 // ^C will have no effect.
910 bool do_set_ctty = (tcgetsid(0) == -1);
912 // Copy signal mask, but unmask signals that we masked on startup. For the moment, we'll
913 // also block all signals, since apparently dup() can be interrupted (!!! really, POSIX??).
914 sigset_t sigwait_set;
916 sigfillset(&sigall_set);
917 sigprocmask(SIG_SETMASK, &sigall_set, &sigwait_set);
918 sigdelset(&sigwait_set, SIGCHLD);
919 sigdelset(&sigwait_set, SIGINT);
920 sigdelset(&sigwait_set, SIGTERM);
921 sigdelset(&sigwait_set, SIGQUIT);
923 constexpr int bufsz = ((CHAR_BIT * sizeof(pid_t)) / 3 + 2) + 11;
924 // "LISTEN_PID=" - 11 characters; the expression above gives a conservative estimate
925 // on the maxiumum number of bytes required for LISTEN=nnn, including nul terminator,
926 // where nnn is a pid_t in decimal (i.e. one decimal digit is worth just over 3 bits).
929 // "DINIT_CS_FD=" - 12 bytes. (we -1 from sizeof(int) in account of sign bit).
930 constexpr int csenvbufsz = ((CHAR_BIT * sizeof(int) - 1) / 3 + 2) + 12;
931 char csenvbuf[csenvbufsz];
933 int minfd = (socket_fd == -1) ? 3 : 4;
935 // Move wpipefd/csfd to another fd if necessary
936 if (wpipefd < minfd) {
937 wpipefd = fcntl(wpipefd, F_DUPFD_CLOEXEC, minfd);
938 if (wpipefd == -1) goto failure_out;
941 if (csfd != -1 && csfd < minfd) {
942 csfd = fcntl(csfd, F_DUPFD, minfd);
943 if (csfd == -1) goto failure_out;
946 if (socket_fd != -1) {
948 if (dup2(socket_fd, 3) == -1) goto failure_out;
949 if (socket_fd != 3) {
953 if (putenv(const_cast<char *>("LISTEN_FDS=1"))) goto failure_out;
954 snprintf(nbuf, bufsz, "LISTEN_PID=%jd", static_cast<intmax_t>(getpid()));
955 if (putenv(nbuf)) goto failure_out;
959 snprintf(csenvbuf, csenvbufsz, "DINIT_CS_FD=%d", csfd);
960 if (putenv(csenvbuf)) goto failure_out;
964 // Re-set stdin, stdout, stderr
965 close(0); close(1); close(2);
967 if (open("/dev/null", O_RDONLY) == 0) {
968 // stdin = 0. That's what we should have; proceed with opening
969 // stdout and stderr.
970 if (open(logfile, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR) != 1) {
973 if (dup2(1, 2) != 2) {
977 else goto failure_out;
979 // We have the option of creating a session and process group, or just a new process
980 // group. If we just create a new process group, the child process cannot make itself
981 // a session leader if it wants to do that (eg getty/login will generally want this).
982 // If we do neither, and we are running with a controlling terminal, a ^C or similar
983 // will also affect the child process (which probably isn't so bad, though since we
984 // will handle the shutdown ourselves it's not necessary). Creating a new session
985 // (and a new process group as part of that) seems like a safe bet, and has the
986 // advantage of letting us signal the process as part of a process group.
990 // "run on console" - run as a foreground job on the terminal/console device
992 // if do_set_ctty is false, we are the session leader; we are probably running
993 // as a user process. Don't create a new session leader in that case, and run
994 // as part of the parent session. Otherwise, the new session cannot claim the
995 // terminal as a controlling terminal (it is already claimed), meaning that it
996 // will not see control signals from ^C etc.
999 // Disable suspend (^Z) (and on some systems, delayed suspend / ^Y)
1000 signal(SIGTSTP, SIG_IGN);
1002 // Become session leader
1004 ioctl(0, TIOCSCTTY, 0);
1007 tcsetpgrp(0, getpgrp());
1010 sigprocmask(SIG_SETMASK, &sigwait_set, nullptr);
1012 execvp(args[0], const_cast<char **>(args));
1014 // If we got here, the exec failed:
1016 int exec_status = errno;
1017 write(wpipefd, &exec_status, sizeof(int));
1021 // Mark this and all dependent services as force-stopped.
1022 void service_record::forceStop() noexcept
1024 if (service_state != service_state_t::STOPPED) {
1026 services->addToStopQueue(this);
1030 void service_record::dependentStopped() noexcept
1032 if (service_state == service_state_t::STOPPING && waiting_for_deps) {
1033 services->addToStopQueue(this);
1037 void service_record::stop(bool bring_down) noexcept
1039 if (start_explicit) {
1040 start_explicit = false;
1049 void service_record::do_stop() noexcept
1051 if (pinned_started) return;
1053 if (service_state != service_state_t::STARTED) {
1054 if (service_state == service_state_t::STARTING) {
1055 if (! can_interrupt_start()) {
1056 // Well this is awkward: we're going to have to continue
1057 // starting, but we don't want any dependents to think that
1058 // they are still waiting to start.
1059 // Make sure they remain stopped:
1064 // We must have had desired_state == STARTED.
1065 notifyListeners(service_event::STARTCANCELLED);
1069 // Reaching this point, we are starting interruptibly - so we
1070 // stop now (by falling through to below).
1073 // If we're starting we need to wait for that to complete.
1074 // If we're already stopping/stopped there's nothing to do.
1079 service_state = service_state_t::STOPPING;
1080 waiting_for_deps = true;
1081 if (stopDependents()) {
1082 services->addToStopQueue(this);
1086 bool service_record::stopCheckDependents() noexcept
1088 bool all_deps_stopped = true;
1089 for (sr_iter i = dependents.begin(); i != dependents.end(); ++i) {
1090 if (! (*i)->is_stopped()) {
1091 all_deps_stopped = false;
1096 return all_deps_stopped;
1099 bool service_record::stopDependents() noexcept
1101 bool all_deps_stopped = true;
1102 for (sr_iter i = dependents.begin(); i != dependents.end(); ++i) {
1103 if (! (*i)->is_stopped()) {
1104 // Note we check *first* since if the dependent service is not stopped,
1105 // 1. We will issue a stop to it shortly and
1106 // 2. It will notify us when stopped, at which point the stopCheckDependents()
1107 // check is run anyway.
1108 all_deps_stopped = false;
1112 // If this service is to be forcefully stopped, dependents must also be.
1116 (*i)->prop_stop = true;
1117 services->addToPropQueue(*i);
1120 return all_deps_stopped;
1123 // All dependents have stopped; we can stop now, too. Only called when STOPPING.
1124 void service_record::all_deps_stopped() noexcept
1126 waiting_for_deps = false;
1130 void base_process_service::all_deps_stopped() noexcept
1132 waiting_for_deps = false;
1134 // The process is still kicking on - must actually kill it. We signal the process
1135 // group (-pid) rather than just the process as there's less risk then of creating
1136 // an orphaned process group:
1137 if (! onstart_flags.no_sigterm) {
1138 kill(-pid, SIGTERM);
1140 if (term_signal != -1) {
1141 kill(-pid, term_signal);
1144 // In most cases, the rest is done in handle_exit_status.
1145 // If we are a BGPROCESS and the process is not our immediate child, however, that
1146 // won't work - check for this now:
1147 if (record_type == service_type::BGPROCESS) {
1149 pid_t r = waitpid(pid, &status, WNOHANG);
1150 if (r == -1 && errno == ECHILD) {
1151 // We can't track this child (or it's terminated already)
1154 else if (r == pid) {
1155 // Process may have died due to signal since we explicitly requested it to
1156 // stop by signalling it; no need to log any termination status.
1162 // The process is already dead.
1167 void scripted_service::all_deps_stopped() noexcept
1169 waiting_for_deps = false;
1170 if (stop_command.length() == 0) {
1173 else if (! start_ps_process(stop_arg_parts, false)) {
1174 // Couldn't execute stop script, but there's not much we can do:
1179 void service_record::unpin() noexcept
1181 if (pinned_started) {
1182 pinned_started = false;
1183 if (desired_state == service_state_t::STOPPED) {
1185 services->processQueues(false);
1188 if (pinned_stopped) {
1189 pinned_stopped = false;
1190 if (desired_state == service_state_t::STARTED) {
1192 services->processQueues(true);
1197 void service_record::queue_for_console() noexcept
1199 services->append_console_queue(this);
1202 void service_record::release_console() noexcept
1204 services->pull_console_queue();
1207 void service_record::interrupt_start() noexcept
1209 services->unqueue_console(this);
1212 void service_set::service_active(service_record *sr) noexcept
1217 void service_set::service_inactive(service_record *sr) noexcept
1222 base_process_service::base_process_service(service_set *sset, string name, service_type service_type_p, string &&command,
1223 std::list<std::pair<unsigned,unsigned>> &command_offsets,
1224 sr_list * pdepends_on, sr_list * pdepends_soft)
1225 : service_record(sset, name, service_type_p, std::move(command), command_offsets,
1226 pdepends_on, pdepends_soft), child_listener(this), child_status_listener(this)
1228 restart_interval_count = 0;
1229 restart_interval_time = {0, 0};
1230 restart_timer.service = this;
1231 restart_timer.add_timer(eventLoop);
1233 // By default, allow a maximum of 3 restarts within 10.0 seconds:
1234 restart_interval.tv_sec = 10;
1235 restart_interval.tv_nsec = 0;
1236 max_restart_interval_count = 3;
1239 void base_process_service::do_restart() noexcept
1242 waiting_restart_timer = false;
1243 restart_interval_count++;
1245 // We may be STARTING (regular restart) or STARTED ("smooth recovery"). This affects whether
1246 // the process should be granted access to the console:
1247 bool on_console = service_state == service_state_t::STARTING
1248 ? onstart_flags.starts_on_console : onstart_flags.runs_on_console;
1250 if (! start_ps_process(exec_arg_parts, on_console)) {
1251 if (service_state == service_state_t::STARTING) {
1255 desired_state = service_state_t::STOPPED;
1258 services->processQueues();
1262 bool base_process_service::restart_ps_process() noexcept
1264 using time_val = eventloop_t::time_val;
1266 time_val current_time;
1267 eventLoop.get_time(current_time, clock_type::MONOTONIC);
1269 if (max_restart_interval_count != 0) {
1270 // Check whether we're still in the most recent restart check interval:
1271 time_val int_diff = current_time - restart_interval_time;
1272 if (int_diff < restart_interval) {
1273 if (restart_interval_count >= max_restart_interval_count) {
1274 log(LogLevel::ERROR, "Service ", service_name, " restarting too quickly; stopping.");
1279 restart_interval_time = current_time;
1280 restart_interval_count = 0;
1284 // Check if enough time has lapsed since the prevous restart. If not, start a timer:
1285 time_val tdiff = current_time - last_start_time;
1286 if (restart_delay < tdiff) {
1287 // > restart delay (normally 200ms)
1291 time_val timeout = restart_delay - tdiff;
1292 restart_timer.arm_timer_rel(eventLoop, timeout);
1293 waiting_restart_timer = true;
1298 void base_process_service::interrupt_start() noexcept
1300 // overridden in subclasses
1301 if (waiting_restart_timer) {
1302 restart_timer.stop_timer(eventLoop);
1303 waiting_restart_timer = false;
1305 service_record::interrupt_start();
1308 dasynq::rearm process_restart_timer::timer_expiry(eventloop_t &, int expiry_count)
1310 service->do_restart();
1311 return dasynq::rearm::DISARM;