10 #include <sys/ioctl.h>
12 #include <sys/socket.h>
18 #include "dinit-log.h"
21 * service.cc - Service management.
22 * See service.h for details.
26 void open_control_socket(bool report_ro_failure = true) noexcept;
27 void setup_external_log() noexcept;
28 extern EventLoop_t eventLoop;
30 // Find the requested service by name
31 static ServiceRecord * findService(const std::list<ServiceRecord *> & records,
32 const char *name) noexcept
35 list<ServiceRecord *>::const_iterator i = records.begin();
36 for ( ; i != records.end(); i++ ) {
37 if (strcmp((*i)->getServiceName().c_str(), name) == 0) {
41 return (ServiceRecord *)0;
44 ServiceRecord * ServiceSet::findService(const std::string &name) noexcept
46 return ::findService(records, name.c_str());
49 void ServiceSet::startService(const char *name)
52 ServiceRecord *record = loadServiceRecord(name);
58 void ServiceSet::stopService(const std::string & name) noexcept
60 ServiceRecord *record = findService(name);
61 if (record != nullptr) {
67 // Called when a service has actually stopped.
68 void ServiceRecord::stopped() noexcept
70 if (service_type != ServiceType::SCRIPTED && service_type != ServiceType::BGPROCESS && onstart_flags.runs_on_console) {
71 tcsetpgrp(0, getpgrp());
72 discard_console_log_buffer();
76 service_state = ServiceState::STOPPED;
79 logServiceStopped(service_name);
80 notifyListeners(ServiceEvent::STOPPED);
82 bool will_restart = (desired_state == ServiceState::STARTED) && service_set->get_auto_restart();
83 for (auto dependency : depends_on) {
84 if (! will_restart || ! dependency->can_interrupt_stop()) {
85 dependency->dependentStopped();
89 // If we are a soft dependency of another target, break the acquisition from that target now:
91 for (auto dependency : soft_dpts) {
92 if (dependency->holding_acq) {
93 dependency->holding_acq = false;
100 // Desired state is "started".
101 service_set->addToStartQueue(this);
104 desired_state = ServiceState::STOPPED;
106 if (socket_fd != -1) {
111 if (start_explicit) {
112 start_explicit = false;
116 if (required_by == 0) {
117 // Service is now completely inactive.
118 release_dependencies();
123 dasynq::rearm ServiceChildWatcher::childStatus(EventLoop_t &loop, pid_t child, int status) noexcept
125 ServiceRecord *sr = service;
128 sr->exit_status = status;
130 // Ok, for a process service, any process death which we didn't rig
131 // ourselves is a bit... unexpected. Probably, the child died because
132 // we asked it to (sr->service_state == STOPPING). But even if
133 // we didn't, there's not much we can do.
135 if (sr->waiting_for_execstat) {
136 // We still don't have an exec() status from the forked child, wait for that
137 // before doing any further processing.
138 return rearm::REMOVE;
141 // Must deregister now since handle_exit_status might result in re-launch:
142 deregister(loop, child);
144 sr->handle_exit_status();
145 return rearm::REMOVED;
148 bool ServiceRecord::do_auto_restart() noexcept
151 return service_set->get_auto_restart();
156 void ServiceRecord::handle_exit_status() noexcept
158 bool did_exit = WIFEXITED(exit_status);
159 bool was_signalled = WIFSIGNALED(exit_status);
161 if (service_type != ServiceType::SCRIPTED && exit_status != 0 && service_state != ServiceState::STOPPING) {
163 log(LogLevel::ERROR, "Service ", service_name, " process terminated with exit code ", WEXITSTATUS(exit_status));
165 else if (was_signalled) {
166 log(LogLevel::ERROR, "Service ", service_name, " terminated due to signal ", WTERMSIG(exit_status));
170 if (doing_recovery) {
172 doing_recovery = false;
173 bool need_stop = false;
174 if ((did_exit && WEXITSTATUS(exit_status) != 0) || was_signalled) {
178 // We need to re-read the PID, since it has now changed.
179 if (service_type == ServiceType::BGPROCESS && pid_file.length() != 0) {
180 if (! read_pid_file()) {
187 // Failed startup: no auto-restart.
188 desired_state = ServiceState::STOPPED;
190 service_set->processQueues(false);
196 if (service_type == ServiceType::PROCESS || service_type == ServiceType::BGPROCESS) {
197 if (service_state == ServiceState::STARTING) {
198 // (only applies to BGPROCESS)
199 if (did_exit && WEXITSTATUS(exit_status) == 0) {
206 else if (service_state == ServiceState::STOPPING) {
207 // We won't log a non-zero exit status or termination due to signal here -
208 // we assume that the process died because we signalled it.
211 else if (smooth_recovery && service_state == ServiceState::STARTED && desired_state == ServiceState::STARTED) {
212 // TODO ensure a minimum time between restarts
213 // TODO if we are pinned-started then we should probably check
214 // that dependencies have started before trying to re-start the
216 doing_recovery = (service_type == ServiceType::BGPROCESS);
221 if (! do_auto_restart()) desired_state = ServiceState::STOPPED;
224 service_set->processQueues(false);
227 if (service_state == ServiceState::STOPPING) {
228 if (did_exit && WEXITSTATUS(exit_status) == 0) {
232 // ??? failed to stop! Let's log it as info:
234 log(LogLevel::INFO, "Service ", service_name, " stop command failed with exit code ", WEXITSTATUS(exit_status));
236 else if (was_signalled) {
237 log(LogLevel::INFO, "Serivice ", service_name, " stop command terminated due to signal ", WTERMSIG(exit_status));
239 // Just assume that we stopped, so that any dependencies
243 service_set->processQueues(false);
246 if (exit_status == 0) {
252 log(LogLevel::ERROR, "Service ", service_name, " command failed with exit code ", WEXITSTATUS(exit_status));
254 else if (was_signalled) {
255 log(LogLevel::ERROR, "Service ", service_name, " command terminated due to signal ", WTERMSIG(exit_status));
259 service_set->processQueues(true);
264 rearm ServiceIoWatcher::fdEvent(EventLoop_t &loop, int fd, int flags) noexcept
266 ServiceRecord *sr = service;
267 sr->waiting_for_execstat = false;
270 int r = read(getWatchedFd(), &exec_status, sizeof(int));
272 close(getWatchedFd());
275 // We read an errno code; exec() failed, and the service startup failed.
277 log(LogLevel::ERROR, sr->service_name, ": execution failed: ", strerror(exec_status));
278 if (sr->service_state == ServiceState::STARTING) {
279 sr->failed_to_start();
281 else if (sr->service_state == ServiceState::STOPPING) {
282 // Must be a scripted service. We've logged the failure, but it's probably better
283 // not to leave the service in STARTED state:
289 if (sr->service_type == ServiceType::PROCESS) {
290 // This could be a smooth recovery (state already STARTED). Even more, the process
291 // might be stopped (and killed via a signal) during smooth recovery. We don't to
292 // process startup again in either case, so we check for state STARTING:
293 if (sr->service_state == ServiceState::STARTING) {
299 // Somehow the process managed to complete before we even saw the status.
300 sr->handle_exit_status();
304 sr->service_set->processQueues(true);
306 return rearm::REMOVED;
309 void ServiceRecord::require() noexcept
311 if (required_by++ == 0) {
313 if (! prop_require) {
315 prop_release = false;
316 service_set->addToPropQueue(this);
319 if (service_state == ServiceState::STOPPED) {
320 // (In any other state, the service is already considered active.)
321 service_set->service_active(this);
326 void ServiceRecord::release() noexcept
328 if (--required_by == 0) {
329 desired_state = ServiceState::STOPPED;
330 // Can stop, and release dependencies once we're stopped.
331 if (service_state == ServiceState::STOPPED) {
333 prop_require = false;
334 service_set->addToPropQueue(this);
337 service_set->addToStopQueue(this);
342 void ServiceRecord::release_dependencies() noexcept
344 for (sr_iter i = depends_on.begin(); i != depends_on.end(); ++i) {
348 for (auto i = soft_deps.begin(); i != soft_deps.end(); ++i) {
349 ServiceRecord * to = i->getTo();
350 if (i->holding_acq) {
352 i->holding_acq = false;
356 service_set->service_inactive(this);
359 void ServiceRecord::start(bool activate) noexcept
361 if (activate && ! start_explicit) {
363 start_explicit = true;
366 if (desired_state == ServiceState::STARTED && service_state != ServiceState::STOPPED) return;
368 desired_state = ServiceState::STARTED;
369 service_set->addToStartQueue(this);
372 void ServiceRecord::do_propagation() noexcept
375 // Need to require all our dependencies
376 for (sr_iter i = depends_on.begin(); i != depends_on.end(); ++i) {
380 for (auto i = soft_deps.begin(); i != soft_deps.end(); ++i) {
381 ServiceRecord * to = i->getTo();
383 i->holding_acq = true;
386 prop_require = false;
390 release_dependencies();
391 prop_release = false;
395 prop_failure = false;
396 failed_to_start(true);
399 if (waiting_for_deps) {
400 if (service_state == ServiceState::STARTING) {
401 if (startCheckDependencies(false)) {
405 else if (service_state == ServiceState::STOPPING) {
406 if (stopCheckDependents()) {
413 void ServiceRecord::execute_transition() noexcept
415 bool is_started = (service_state == ServiceState::STARTED)
416 || (service_state == ServiceState::STARTING && can_interrupt_start());
417 bool is_stopped = (service_state == ServiceState::STOPPED)
418 || (service_state == ServiceState::STOPPING && can_interrupt_stop());
420 if (is_started && (desired_state == ServiceState::STOPPED || force_stop)) {
421 if (! pinned_started) {
425 else if (is_stopped && desired_state == ServiceState::STARTED) {
426 if (! pinned_stopped) {
432 void ServiceRecord::do_start() noexcept
434 if (pinned_stopped) return;
436 if (service_state != ServiceState::STOPPED) {
437 // We're already starting/started, or we are stopping and need to wait for
438 // that the complete.
439 if (service_state != ServiceState::STOPPING || ! can_interrupt_stop()) {
442 // We're STOPPING, and that can be interrupted. Our dependencies might be STOPPING,
443 // but if so they are waiting (for us), so they too can be instantly returned to
445 notifyListeners(ServiceEvent::STOPCANCELLED);
448 service_state = ServiceState::STARTING;
450 waiting_for_deps = true;
452 // Ask dependencies to start, mark them as being waited on.
453 if (! startCheckDependencies(true)) {
457 // Actually start this service.
461 void ServiceRecord::dependencyStarted() noexcept
463 if (service_state == ServiceState::STARTING && waiting_for_deps) {
464 service_set->addToPropQueue(this);
468 bool ServiceRecord::startCheckDependencies(bool start_deps) noexcept
470 bool all_deps_started = true;
472 for (sr_iter i = depends_on.begin(); i != depends_on.end(); ++i) {
473 if ((*i)->service_state != ServiceState::STARTED) {
475 all_deps_started = false;
484 for (auto i = soft_deps.begin(); i != soft_deps.end(); ++i) {
485 ServiceRecord * to = i->getTo();
487 if (to->service_state != ServiceState::STARTED) {
489 i->waiting_on = true;
490 all_deps_started = false;
493 i->waiting_on = false;
496 else if (i->waiting_on) {
497 if (to->service_state != ServiceState::STARTING) {
498 // Service has either started or is no longer starting
499 i->waiting_on = false;
502 // We are still waiting on this service
508 return all_deps_started;
511 bool ServiceRecord::open_socket() noexcept
513 if (socket_path.empty() || socket_fd != -1) {
514 // No socket, or already open
518 const char * saddrname = socket_path.c_str();
519 uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + socket_path.length() + 1;
521 struct sockaddr_un * name = static_cast<sockaddr_un *>(malloc(sockaddr_size));
522 if (name == nullptr) {
523 log(LogLevel::ERROR, service_name, ": Opening activation socket: out of memory");
527 // Un-link any stale socket. TODO: safety check? should at least confirm the path is a socket.
530 name->sun_family = AF_UNIX;
531 strcpy(name->sun_path, saddrname);
533 int sockfd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
535 log(LogLevel::ERROR, service_name, ": Error creating activation socket: ", strerror(errno));
540 if (bind(sockfd, (struct sockaddr *) name, sockaddr_size) == -1) {
541 log(LogLevel::ERROR, service_name, ": Error binding activation socket: ", strerror(errno));
549 // POSIX (1003.1, 2013) says that fchown and fchmod don't necesarily work on sockets. We have to
550 // use chown and chmod instead.
551 if (chown(saddrname, socket_uid, socket_gid)) {
552 log(LogLevel::ERROR, service_name, ": Error setting activation socket owner/group: ", strerror(errno));
557 if (chmod(saddrname, socket_perms) == -1) {
558 log(LogLevel::ERROR, service_name, ": Error setting activation socket permissions: ", strerror(errno));
563 if (listen(sockfd, 128) == -1) { // 128 "seems reasonable".
564 log(LogLevel::ERROR, ": Error listening on activation socket: ", strerror(errno));
573 void ServiceRecord::allDepsStarted(bool has_console) noexcept
575 if (onstart_flags.runs_on_console && ! has_console) {
576 waiting_for_deps = true;
581 waiting_for_deps = false;
583 if (! open_socket()) {
587 if (service_type == ServiceType::PROCESS || service_type == ServiceType::BGPROCESS
588 || service_type == ServiceType::SCRIPTED) {
589 bool start_success = start_ps_process();
590 if (! start_success) {
595 // "internal" service
600 void ServiceRecord::acquiredConsole() noexcept
602 if (service_state != ServiceState::STARTING) {
603 // We got the console but no longer want it.
606 else if (startCheckDependencies(false)) {
607 allDepsStarted(true);
610 // We got the console but can't use it yet.
615 bool ServiceRecord::read_pid_file() noexcept
617 const char *pid_file_c = pid_file.c_str();
618 int fd = open(pid_file_c, O_CLOEXEC);
620 char pidbuf[21]; // just enought to hold any 64-bit integer
621 int r = read(fd, pidbuf, 20);
623 pidbuf[r] = 0; // store nul terminator
624 pid = std::atoi(pidbuf);
625 if (kill(pid, 0) == 0) {
626 child_listener.addWatch(eventLoop, pid);
629 log(LogLevel::ERROR, service_name, ": pid read from pidfile (", pid, ") is not valid");
639 log(LogLevel::ERROR, service_name, ": read pid file: ", strerror(errno));
644 void ServiceRecord::started() noexcept
646 if (onstart_flags.runs_on_console && (service_type == ServiceType::SCRIPTED || service_type == ServiceType::BGPROCESS)) {
647 tcsetpgrp(0, getpgrp());
651 if (service_type == ServiceType::BGPROCESS && pid_file.length() != 0) {
652 if (! read_pid_file()) {
658 logServiceStarted(service_name);
659 service_state = ServiceState::STARTED;
660 notifyListeners(ServiceEvent::STARTED);
662 if (onstart_flags.rw_ready) {
663 open_control_socket();
665 if (onstart_flags.log_ready) {
666 setup_external_log();
669 if (force_stop || desired_state == ServiceState::STOPPED) {
671 service_set->addToStopQueue(this);
675 // Notify any dependents whose desired state is STARTED:
676 for (auto i = dependents.begin(); i != dependents.end(); i++) {
677 (*i)->dependencyStarted();
679 for (auto i = soft_dpts.begin(); i != soft_dpts.end(); i++) {
680 (*i)->getFrom()->dependencyStarted();
684 void ServiceRecord::failed_to_start(bool depfailed) noexcept
686 if (!depfailed && onstart_flags.runs_on_console) {
687 tcsetpgrp(0, getpgrp());
691 logServiceFailed(service_name);
692 service_state = ServiceState::STOPPED;
693 if (start_explicit) {
694 start_explicit = false;
697 notifyListeners(ServiceEvent::FAILEDSTART);
699 // Cancel start of dependents:
700 for (sr_iter i = dependents.begin(); i != dependents.end(); i++) {
701 if ((*i)->service_state == ServiceState::STARTING) {
702 (*i)->prop_failure = true;
703 service_set->addToPropQueue(*i);
706 for (auto i = soft_dpts.begin(); i != soft_dpts.end(); i++) {
707 // We can send 'start', because this is only a soft dependency.
708 // Our startup failure means that they don't have to wait for us.
709 if ((*i)->waiting_on) {
710 (*i)->holding_acq = false;
711 (*i)->waiting_on = false;
712 (*i)->getFrom()->dependencyStarted();
718 bool ServiceRecord::start_ps_process() noexcept
720 return start_ps_process(exec_arg_parts, onstart_flags.runs_on_console);
723 bool ServiceRecord::start_ps_process(const std::vector<const char *> &cmd, bool on_console) noexcept
725 // In general, you can't tell whether fork/exec is successful. We use a pipe to communicate
726 // success/failure from the child to the parent. The pipe is set CLOEXEC so a successful
727 // exec closes the pipe, and the parent sees EOF. If the exec is unsuccessful, the errno
728 // is written to the pipe, and the parent can read it.
731 if (pipe2(pipefd, O_CLOEXEC)) {
732 log(LogLevel::ERROR, service_name, ": can't create status check pipe: ", strerror(errno));
736 const char * logfile = this->logfile.c_str();
738 logfile = "/dev/null";
741 bool child_status_registered = false;
742 ControlConn *control_conn = nullptr;
744 int control_socket[2] = {-1, -1};
745 if (onstart_flags.pass_cs_fd) {
746 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, /* protocol */ 0, control_socket)) {
747 log(LogLevel::ERROR, service_name, ": can't create control socket: ", strerror(errno));
751 // Make the server side socket close-on-exec:
752 int fdflags = fcntl(control_socket[0], F_GETFD);
753 fcntl(control_socket[0], F_SETFD, fdflags | FD_CLOEXEC);
756 control_conn = new ControlConn(&eventLoop, service_set, control_socket[0]);
758 catch (std::exception &exc) {
759 log(LogLevel::ERROR, service_name, ": can't launch process; out of memory");
764 // Set up complete, now fork and exec:
769 child_status_listener.addWatch(eventLoop, pipefd[0], IN_EVENTS);
770 child_status_registered = true;
772 forkpid = child_listener.fork(eventLoop);
774 catch (std::exception &e) {
775 log(LogLevel::ERROR, service_name, ": Could not fork: ", e.what());
780 run_child_proc(cmd.data(), logfile, on_console, pipefd[1], control_socket[1]);
784 close(pipefd[1]); // close the 'other end' fd
785 if (control_socket[1] != -1) {
786 close(control_socket[1]);
790 waiting_for_execstat = true;
797 if (child_status_registered) {
798 child_status_listener.deregister(eventLoop);
801 if (onstart_flags.pass_cs_fd) {
805 close(control_socket[0]);
806 close(control_socket[1]);
816 void ServiceRecord::run_child_proc(const char * const *args, const char *logfile, bool on_console,
817 int wpipefd, int csfd) noexcept
819 // Child process. Must not allocate memory (or otherwise risk throwing any exception)
820 // from here until exit().
822 // If the console already has a session leader, presumably it is us. On the other hand
823 // if it has no session leader, and we don't create one, then control inputs such as
824 // ^C will have no effect.
825 bool do_set_ctty = (tcgetsid(0) == -1);
827 // Copy signal mask, but unmask signals that we masked on startup. For the moment, we'll
828 // also block all signals, since apparently dup() can be interrupted (!!! really, POSIX??).
829 sigset_t sigwait_set;
831 sigfillset(&sigall_set);
832 sigprocmask(SIG_SETMASK, &sigall_set, &sigwait_set);
833 sigdelset(&sigwait_set, SIGCHLD);
834 sigdelset(&sigwait_set, SIGINT);
835 sigdelset(&sigwait_set, SIGTERM);
837 constexpr int bufsz = ((CHAR_BIT * sizeof(pid_t)) / 3 + 2) + 11;
838 // "LISTEN_PID=" - 11 characters; the expression above gives a conservative estimate
839 // on the maxiumum number of bytes required for LISTEN=xxx, including nul terminator,
840 // where xxx is a pid_t in decimal (i.e. one decimal digit is worth just over 3 bits).
843 // "DINIT_CS_FD=" - 12 bytes. (we -1 from sizeof(int) in account of sign bit).
844 constexpr int csenvbufsz = ((CHAR_BIT * sizeof(int) - 1) / 3 + 2) + 12;
845 char csenvbuf[csenvbufsz];
847 int minfd = (socket_fd == -1) ? 3 : 4;
849 // Move wpipefd/csfd to another fd if necessary
850 if (wpipefd < minfd) {
851 wpipefd = fcntl(wpipefd, F_DUPFD_CLOEXEC, minfd);
852 if (wpipefd == -1) goto failure_out;
855 if (csfd != -1 && csfd < minfd) {
856 csfd = fcntl(csfd, F_DUPFD, minfd);
857 if (csfd == -1) goto failure_out;
860 if (socket_fd != -1) {
862 if (dup2(socket_fd, 3) == -1) goto failure_out;
863 if (socket_fd != 3) {
867 if (putenv(const_cast<char *>("LISTEN_FDS=1"))) goto failure_out;
868 snprintf(nbuf, bufsz, "LISTEN_PID=%jd", static_cast<intmax_t>(getpid()));
869 if (putenv(nbuf)) goto failure_out;
873 snprintf(csenvbuf, csenvbufsz, "DINIT_CS_FD=%d", csfd);
874 if (putenv(csenvbuf)) goto failure_out;
878 // Re-set stdin, stdout, stderr
879 close(0); close(1); close(2);
881 if (open("/dev/null", O_RDONLY) == 0) {
882 // stdin = 0. That's what we should have; proceed with opening
883 // stdout and stderr.
884 if (open(logfile, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR) != 1) {
887 if (dup2(1, 2) != 2) {
891 else goto failure_out;
893 // We have the option of creating a new process group and/or session. If
894 // we just create a new process group, the child process cannot make itself
895 // a session leader if it wants to do that (eg getty/login will generally
896 // want this). If we do neither, and we are running with a controlling
897 // terminal, a ^C or similar will also affect the child process.
901 // "run on console" - run as a foreground job on the terminal/console device
903 // if do_set_ctty is false, we are the session leader; we are probably running
904 // as a user process. Don't create a new session leader in that case, and run
905 // as part of the parent session. Otherwise, the new session cannot claim the
906 // terminal as a controlling terminal (it is already claimed), meaning that it
907 // will not see control signals from ^C etc.
910 // Disable suspend (^Z) (and on some systems, delayed suspend / ^Y)
911 signal(SIGTSTP, SIG_IGN);
913 // Become session leader
915 ioctl(0, TIOCSCTTY, 0);
918 tcsetpgrp(0, getpgrp());
921 sigprocmask(SIG_SETMASK, &sigwait_set, nullptr);
923 execvp(args[0], const_cast<char **>(args));
925 // If we got here, the exec failed:
927 int exec_status = errno;
928 write(wpipefd, &exec_status, sizeof(int));
932 // Mark this and all dependent services as force-stopped.
933 void ServiceRecord::forceStop() noexcept
935 if (service_state != ServiceState::STOPPED) {
937 service_set->addToStopQueue(this);
941 void ServiceRecord::dependentStopped() noexcept
943 if (service_state == ServiceState::STOPPING && waiting_for_deps) {
944 service_set->addToPropQueue(this);
948 void ServiceRecord::stop(bool bring_down) noexcept
950 if (start_explicit) {
951 start_explicit = false;
955 if (bring_down && desired_state != ServiceState::STOPPED) {
956 desired_state = ServiceState::STOPPED;
957 service_set->addToStopQueue(this);
961 void ServiceRecord::do_stop() noexcept
963 if (pinned_started) return;
965 if (service_state != ServiceState::STARTED) {
966 if (service_state == ServiceState::STARTING) {
967 if (! can_interrupt_start()) {
968 // Well this is awkward: we're going to have to continue
969 // starting, but we don't want any dependents to think that
970 // they are still waiting to start.
971 // Make sure they remain stopped:
976 // We must have had desired_state == STARTED.
977 notifyListeners(ServiceEvent::STARTCANCELLED);
979 // Reaching this point, we have can_interrupt_start() == true. So,
980 // we can stop. Dependents might be starting, but they must be
981 // waiting on us, so they should also be immediately stoppable.
982 // Fall through to below,.
985 // If we're starting we need to wait for that to complete.
986 // If we're already stopping/stopped there's nothing to do.
991 service_state = ServiceState::STOPPING;
992 waiting_for_deps = true;
994 // If we get here, we are in STARTED state; stop all dependents.
995 if (stopDependents()) {
1000 bool ServiceRecord::stopCheckDependents() noexcept
1002 bool all_deps_stopped = true;
1003 for (sr_iter i = dependents.begin(); i != dependents.end(); ++i) {
1004 if (! (*i)->is_stopped()) {
1005 all_deps_stopped = false;
1010 return all_deps_stopped;
1013 bool ServiceRecord::stopDependents() noexcept
1015 bool all_deps_stopped = true;
1016 for (sr_iter i = dependents.begin(); i != dependents.end(); ++i) {
1017 if (! (*i)->is_stopped()) {
1018 // Note we check *first* since if the dependent service is not stopped,
1019 // 1. We will issue a stop to it shortly and
1020 // 2. It will notify us when stopped, at which point the stopCheckDependents()
1021 // check is run anyway.
1022 all_deps_stopped = false;
1028 service_set->addToStopQueue(*i);
1032 return all_deps_stopped;
1035 // All dependents have stopped; we can stop now, too. Only called when STOPPING.
1036 void ServiceRecord::allDepsStopped()
1038 waiting_for_deps = false;
1039 if (service_type == ServiceType::PROCESS || service_type == ServiceType::BGPROCESS) {
1041 // The process is still kicking on - must actually kill it.
1042 if (! onstart_flags.no_sigterm) {
1045 if (term_signal != -1) {
1046 kill(pid, term_signal);
1049 // In most cases, the rest is done in process_child_callback.
1050 // If we are a BGPROCESS and the process is not our immediate child, however, that
1051 // won't work - check for this now:
1052 if (service_type == ServiceType::BGPROCESS) {
1054 pid_t r = waitpid(pid, &status, WNOHANG);
1055 if (r == -1 && errno == ECHILD) {
1056 // We can't track this child (or it's terminated already)
1059 else if (r == pid) {
1060 // TODO, examine status and log anything unusual.
1066 // The process is already dead.
1070 else if (service_type == ServiceType::SCRIPTED) {
1071 // Scripted service.
1072 if (stop_command.length() == 0) {
1075 else if (! start_ps_process(stop_arg_parts, false)) {
1076 // Couldn't execute stop script, but there's not much we can do:
1085 void ServiceRecord::unpin() noexcept
1087 if (pinned_started) {
1088 pinned_started = false;
1089 if (desired_state == ServiceState::STOPPED) {
1091 service_set->processQueues(false);
1094 if (pinned_stopped) {
1095 pinned_stopped = false;
1096 if (desired_state == ServiceState::STARTED) {
1098 service_set->processQueues(true);
1103 void ServiceRecord::queueForConsole() noexcept
1105 service_set->consoleQueueTail(this);
1108 void ServiceRecord::releaseConsole() noexcept
1110 service_set->pullConsoleQueue();
1113 void ServiceSet::service_active(ServiceRecord *sr) noexcept
1118 void ServiceSet::service_inactive(ServiceRecord *sr) noexcept