10 #include <sys/ioctl.h>
12 #include <sys/socket.h>
18 #include "dinit-log.h"
21 void open_control_socket(struct ev_loop *loop) noexcept;
24 // Find the requested service by name
25 static ServiceRecord * findService(const std::list<ServiceRecord *> & records,
26 const char *name) noexcept
29 list<ServiceRecord *>::const_iterator i = records.begin();
30 for ( ; i != records.end(); i++ ) {
31 if (strcmp((*i)->getServiceName(), name) == 0) {
35 return (ServiceRecord *)0;
38 ServiceRecord * ServiceSet::findService(const std::string &name) noexcept
40 return ::findService(records, name.c_str());
43 void ServiceSet::startService(const char *name)
46 ServiceRecord *record = loadServiceRecord(name);
51 void ServiceSet::stopService(const std::string & name) noexcept
53 ServiceRecord *record = findService(name);
54 if (record != nullptr) {
59 // Called when a service has actually stopped.
60 void ServiceRecord::stopped() noexcept
62 if (service_type != ServiceType::SCRIPTED && service_type != ServiceType::BGPROCESS && onstart_flags.runs_on_console) {
63 tcsetpgrp(0, getpgrp());
67 logServiceStopped(service_name);
68 service_state = ServiceState::STOPPED;
71 notifyListeners(ServiceEvent::STOPPED);
73 for (auto dependency : depends_on) {
74 dependency->dependentStopped();
77 if (desired_state == ServiceState::STARTED && service_set->get_auto_restart()) {
78 // Desired state is "started".
82 if (socket_fd != -1) {
87 if (required_by == 0) {
88 // Service is now completely inactive.
89 release_dependencies();
94 void ServiceRecord::process_child_callback(struct ev_loop *loop, ev_child *w, int revents) noexcept
96 ServiceRecord *sr = (ServiceRecord *) w->data;
99 sr->exit_status = w->rstatus;
100 ev_child_stop(loop, w);
102 // Ok, for a process service, any process death which we didn't rig
103 // ourselves is a bit... unexpected. Probably, the child died because
104 // we asked it to (sr->service_state == STOPPING). But even if
105 // we didn't, there's not much we can do.
107 if (sr->waiting_for_execstat) {
108 // We still don't have an exec() status from the forked child, wait for that
109 // before doing any further processing.
113 sr->handle_exit_status();
116 bool ServiceRecord::do_auto_restart() noexcept
119 return service_set->get_auto_restart();
124 void ServiceRecord::handle_exit_status() noexcept
126 if (exit_status != 0 && service_state != ServiceState::STOPPING) {
127 log(LogLevel::ERROR, "Service ", service_name, " process terminated with exit code ", exit_status);
130 if (doing_recovery) {
132 doing_recovery = false;
133 bool need_stop = false;
134 if (exit_status != 0) {
138 // We need to re-read the PID, since it has now changed.
139 if (service_type == ServiceType::BGPROCESS && pid_file.length() != 0) {
140 if (! read_pid_file()) {
147 // Failed startup: no auto-restart.
148 desired_state = ServiceState::STOPPED;
155 if (service_type == ServiceType::PROCESS || service_type == ServiceType::BGPROCESS) {
156 if (service_state == ServiceState::STARTING) {
157 // (only applies to BGPROCESS)
158 if (exit_status == 0) {
165 else if (service_state == ServiceState::STOPPING) {
166 // TODO log non-zero rstatus?
169 else if (smooth_recovery && service_state == ServiceState::STARTED) {
170 // TODO ensure a minimum time between restarts
171 // TODO if we are pinned-started then we should probably check
172 // that dependencies have started before trying to re-start the
174 doing_recovery = (service_type == ServiceType::BGPROCESS);
179 if (! do_auto_restart()) desired_state = ServiceState::STOPPED;
184 if (service_state == ServiceState::STOPPING) {
185 if (exit_status == 0) {
189 // ??? failed to stop! Let's log it as info:
190 log(LogLevel::INFO, "service ", service_name, " stop command failed with exit code ", exit_status);
191 // Just assume that we stopped, so that any dependencies
197 if (exit_status == 0) {
202 log(LogLevel::ERROR, "service ", service_name, " command failed with exit code ", exit_status);
209 void ServiceRecord::process_child_status(struct ev_loop *loop, ev_io * stat_io, int revents) noexcept
211 ServiceRecord *sr = (ServiceRecord *) stat_io->data;
212 sr->waiting_for_execstat = false;
215 int r = read(stat_io->fd, &exec_status, sizeof(int));
217 ev_io_stop(loop, stat_io);
220 // We read an errno code; exec() failed, and the service startup failed.
222 log(LogLevel::ERROR, sr->service_name, ": execution failed: ", strerror(exec_status));
223 if (sr->service_state == ServiceState::STARTING) {
224 sr->failed_to_start();
226 else if (sr->service_state == ServiceState::STOPPING) {
227 // Must be a scripted servce. We've logged the failure, but it's probably better
228 // not to leave the service in STARTED state:
234 if (sr->service_type == ServiceType::PROCESS) {
235 if (sr->service_state != ServiceState::STARTED) {
241 // Somehow the process managed to complete before we even saw the status.
242 sr->handle_exit_status();
247 void ServiceRecord::require() noexcept
249 if (required_by++ == 0) {
250 // Need to require all our dependencies
251 for (sr_iter i = depends_on.begin(); i != depends_on.end(); ++i) {
255 for (auto i = soft_deps.begin(); i != soft_deps.end(); ++i) {
256 ServiceRecord * to = i->getTo();
260 if (service_state == ServiceState::STOPPED) {
261 // (In any other state, the service is already considered active.)
262 service_set->service_active(this);
267 void ServiceRecord::release() noexcept
269 if (--required_by == 0) {
270 desired_state = ServiceState::STOPPED;
271 // Can stop, and release dependencies once we're stopped.
272 if (service_state == ServiceState::STOPPED) {
273 release_dependencies();
281 void ServiceRecord::release_dependencies() noexcept
283 for (sr_iter i = depends_on.begin(); i != depends_on.end(); ++i) {
287 for (auto i = soft_deps.begin(); i != soft_deps.end(); ++i) {
288 ServiceRecord * to = i->getTo();
292 service_set->service_inactive(this);
295 void ServiceRecord::start(bool activate) noexcept
298 if (!start_explicit) require();
299 start_explicit = true;
302 if (desired_state == ServiceState::STARTED && service_state != ServiceState::STOPPED) return;
304 if (required_by == 0) {
305 service_set->service_active(this);
308 desired_state = ServiceState::STARTED;
312 void ServiceRecord::do_start() noexcept
314 if (pinned_stopped) return;
316 if (service_state != ServiceState::STOPPED) {
317 // We're already starting/started, or we are stopping and need to wait for
318 // that the complete.
319 if (service_state != ServiceState::STOPPING || ! can_interrupt_stop()) {
322 // We're STOPPING, and that can be interrupted. Our dependencies might be STOPPING,
323 // but if so they are waiting (for us), so they too can be instantly returned to
325 notifyListeners(ServiceEvent::STOPCANCELLED);
328 service_state = ServiceState::STARTING;
330 waiting_for_deps = true;
332 // Ask dependencies to start, mark them as being waited on.
333 if (! startCheckDependencies(true)) {
337 // Actually start this service.
341 void ServiceRecord::dependencyStarted() noexcept
343 if (service_state != ServiceState::STARTING || ! waiting_for_deps) {
347 if (startCheckDependencies(false)) {
352 bool ServiceRecord::startCheckDependencies(bool start_deps) noexcept
354 bool all_deps_started = true;
356 for (sr_iter i = depends_on.begin(); i != depends_on.end(); ++i) {
357 if ((*i)->service_state != ServiceState::STARTED) {
359 all_deps_started = false;
368 for (auto i = soft_deps.begin(); i != soft_deps.end(); ++i) {
369 ServiceRecord * to = i->getTo();
371 if (to->service_state != ServiceState::STARTED) {
373 i->waiting_on = true;
374 all_deps_started = false;
377 i->waiting_on = false;
380 else if (i->waiting_on) {
381 if (to->service_state != ServiceState::STARTING) {
382 // Service has either started or is no longer starting
383 i->waiting_on = false;
386 // We are still waiting on this service
392 return all_deps_started;
395 bool ServiceRecord::open_socket() noexcept
397 if (socket_path.empty() || socket_fd != -1) {
398 // No socket, or already open
402 const char * saddrname = socket_path.c_str();
403 uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + socket_path.length() + 1;
405 struct sockaddr_un * name = static_cast<sockaddr_un *>(malloc(sockaddr_size));
406 if (name == nullptr) {
407 log(LogLevel::ERROR, service_name, ": Opening activation socket: out of memory");
411 // Un-link any stale socket. TODO: safety check? should at least confirm the path is a socket.
414 name->sun_family = AF_UNIX;
415 strcpy(name->sun_path, saddrname);
417 int sockfd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
419 log(LogLevel::ERROR, service_name, ": Error creating activation socket: ", strerror(errno));
424 if (bind(sockfd, (struct sockaddr *) name, sockaddr_size) == -1) {
425 log(LogLevel::ERROR, service_name, ": Error binding activation socket: ", strerror(errno));
433 // POSIX (1003.1, 2013) says that fchown and fchmod don't necesarily work on sockets. We have to
434 // use chown and chmod instead.
435 if (chown(saddrname, socket_uid, socket_gid)) {
436 log(LogLevel::ERROR, service_name, ": Error setting activation socket owner/group: ", strerror(errno));
441 if (chmod(saddrname, socket_perms) == -1) {
442 log(LogLevel::ERROR, service_name, ": Error setting activation socket permissions: ", strerror(errno));
447 if (listen(sockfd, 128) == -1) { // 128 "seems reasonable".
448 log(LogLevel::ERROR, ": Error listening on activation socket: ", strerror(errno));
457 void ServiceRecord::allDepsStarted(bool has_console) noexcept
459 if (onstart_flags.runs_on_console && ! has_console) {
460 waiting_for_deps = true;
465 waiting_for_deps = false;
467 if (! open_socket()) {
471 if (service_type == ServiceType::PROCESS || service_type == ServiceType::BGPROCESS
472 || service_type == ServiceType::SCRIPTED) {
473 bool start_success = start_ps_process();
474 if (! start_success) {
479 // "internal" service
484 void ServiceRecord::acquiredConsole() noexcept
486 if (service_state != ServiceState::STARTING) {
487 // We got the console but no longer want it.
490 else if (startCheckDependencies(false)) {
491 log_to_console = false;
492 allDepsStarted(true);
495 // We got the console but can't use it yet.
500 bool ServiceRecord::read_pid_file() noexcept
502 const char *pid_file_c = pid_file.c_str();
503 int fd = open(pid_file_c, O_CLOEXEC);
505 char pidbuf[21]; // just enought to hold any 64-bit integer
506 int r = read(fd, pidbuf, 20);
508 pidbuf[r] = 0; // store nul terminator
509 pid = std::atoi(pidbuf);
510 if (kill(pid, 0) == 0) {
511 ev_child_init(&child_listener, process_child_callback, pid, 0);
512 child_listener.data = this;
513 ev_child_start(ev_default_loop(EVFLAG_AUTO), &child_listener);
516 log(LogLevel::ERROR, service_name, ": pid read from pidfile (", pid, ") is not valid");
526 log(LogLevel::ERROR, service_name, ": read pid file: ", strerror(errno));
531 void ServiceRecord::started() noexcept
533 if (onstart_flags.runs_on_console && (service_type == ServiceType::SCRIPTED || service_type == ServiceType::BGPROCESS)) {
534 tcsetpgrp(0, getpgrp());
538 if (service_type == ServiceType::BGPROCESS && pid_file.length() != 0) {
539 if (! read_pid_file()) {
545 logServiceStarted(service_name);
546 service_state = ServiceState::STARTED;
547 notifyListeners(ServiceEvent::STARTED);
549 if (onstart_flags.rw_ready) {
550 open_control_socket(ev_default_loop(EVFLAG_AUTO));
553 if (force_stop || desired_state == ServiceState::STOPPED) {
559 // Notify any dependents whose desired state is STARTED:
560 for (auto i = dependents.begin(); i != dependents.end(); i++) {
561 (*i)->dependencyStarted();
563 for (auto i = soft_dpts.begin(); i != soft_dpts.end(); i++) {
564 (*i)->getFrom()->dependencyStarted();
568 void ServiceRecord::failed_to_start(bool depfailed) noexcept
570 if (!depfailed && onstart_flags.runs_on_console) {
571 tcsetpgrp(0, getpgrp());
575 logServiceFailed(service_name);
576 service_state = ServiceState::STOPPED;
577 notifyListeners(ServiceEvent::FAILEDSTART);
579 // Cancel start of dependents:
580 for (sr_iter i = dependents.begin(); i != dependents.end(); i++) {
581 if ((*i)->service_state == ServiceState::STARTING) {
582 (*i)->failed_to_start(true);
585 for (auto i = soft_dpts.begin(); i != soft_dpts.end(); i++) {
586 // We can send 'start', because this is only a soft dependency.
587 // Our startup failure means that they don't have to wait for us.
588 (*i)->getFrom()->dependencyStarted();
592 bool ServiceRecord::start_ps_process() noexcept
594 return start_ps_process(exec_arg_parts, onstart_flags.runs_on_console);
597 bool ServiceRecord::start_ps_process(const std::vector<const char *> &cmd, bool on_console) noexcept
599 // In general, you can't tell whether fork/exec is successful. We use a pipe to communicate
600 // success/failure from the child to the parent. The pipe is set CLOEXEC so a successful
601 // exec closes the pipe, and the parent sees EOF. If the exec is unsuccessful, the errno
602 // is written to the pipe, and the parent can read it.
605 if (pipe2(pipefd, O_CLOEXEC)) {
610 // Set up the argument array and other data now (before fork), in case memory allocation fails.
612 auto args = cmd.data();
614 const char * logfile = this->logfile.c_str();
616 logfile = "/dev/null";
619 // TODO make sure pipefd's are not 0/1/2 (STDIN/OUT/ERR) - if they are, dup them
620 // until they are not.
622 pid_t forkpid = fork();
630 // If the console already has a session leader, presumably it is us. On the other hand
631 // if it has no session leader, and we don't create one, then control inputs such as
632 // ^C will have no effect.
633 bool do_set_ctty = (tcgetsid(0) == -1);
636 // Child process. Must not allocate memory (or otherwise risk throwing any exception)
637 // from here until exit().
638 ev_default_destroy(); // won't need that on this side, free up fds.
640 constexpr int bufsz = ((CHAR_BIT * sizeof(pid_t) - 1) / 3 + 2) + 11;
641 // "LISTEN_PID=" - 11 characters
644 if (socket_fd != -1) {
646 if (socket_fd != 3) {
650 if (putenv(const_cast<char *>("LISTEN_FDS=1"))) goto failure_out;
652 snprintf(nbuf, bufsz, "LISTEN_PID=%jd", static_cast<intmax_t>(getpid()));
654 if (putenv(nbuf)) goto failure_out;
658 // Re-set stdin, stdout, stderr
659 close(0); close(1); close(2);
661 // TODO rethink this logic. If we open it at not-0, shouldn't we just dup it to 0?:
662 if (open("/dev/null", O_RDONLY) == 0) {
663 // stdin = 0. That's what we should have; proceed with opening
664 // stdout and stderr.
665 open(logfile, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
670 // "run on console" - run as a foreground job on the terminal/console device
673 ioctl(0, TIOCSCTTY, 0);
676 tcsetpgrp(0, getpgrp());
678 // TODO disable suspend (^Z)? (via tcsetattr)
679 // (should be done before TIOCSCTTY)
682 execvp(exec_arg_parts[0], const_cast<char **>(args));
684 // If we got here, the exec failed:
686 int exec_status = errno;
687 write(pipefd[1], &exec_status, sizeof(int));
692 close(pipefd[1]); // close the 'other end' fd
697 ev_io_init(&child_status_listener, process_child_status, pipefd[0], EV_READ);
698 child_status_listener.data = this;
699 ev_io_start(ev_default_loop(EVFLAG_AUTO), &child_status_listener);
701 // Add a process listener so we can detect when the
703 ev_child_init(&child_listener, process_child_callback, pid, 0);
704 child_listener.data = this;
705 ev_child_start(ev_default_loop(EVFLAG_AUTO), &child_listener);
706 waiting_for_execstat = true;
711 // Mark this and all dependent services as force-stopped.
712 void ServiceRecord::forceStop() noexcept
714 if (service_state != ServiceState::STOPPED) {
720 void ServiceRecord::dependentStopped() noexcept
722 if (service_state == ServiceState::STOPPING && waiting_for_deps) {
723 // Check the other dependents before we stop.
724 if (stopCheckDependents()) {
730 void ServiceRecord::stop(bool bring_down) noexcept
732 if (start_explicit) {
733 start_explicit = false;
738 desired_state = ServiceState::STOPPED;
743 void ServiceRecord::do_stop() noexcept
745 if (pinned_started) return;
747 if (service_state != ServiceState::STARTED) {
748 if (service_state == ServiceState::STARTING) {
749 if (! can_interrupt_start()) {
750 // Well this is awkward: we're going to have to continue
751 // starting, but we don't want any dependents to think that
752 // they are still waiting to start.
753 // Make sure they remain stopped:
758 // We must have had desired_state == STARTED.
759 notifyListeners(ServiceEvent::STARTCANCELLED);
761 // Reaching this point, we have can_interrupt_start() == true. So,
762 // we can stop. Dependents might be starting, but they must be
763 // waiting on us, so they should also be immediately stoppable.
764 // Fall through to below,.
767 // If we're starting we need to wait for that to complete.
768 // If we're already stopping/stopped there's nothing to do.
773 service_state = ServiceState::STOPPING;
774 waiting_for_deps = true;
776 // If we get here, we are in STARTED state; stop all dependents.
777 if (stopDependents()) {
782 bool ServiceRecord::stopCheckDependents() noexcept
784 bool all_deps_stopped = true;
785 for (sr_iter i = dependents.begin(); i != dependents.end(); ++i) {
786 if (! (*i)->is_stopped()) {
787 all_deps_stopped = false;
792 return all_deps_stopped;
795 bool ServiceRecord::stopDependents() noexcept
797 bool all_deps_stopped = true;
798 for (sr_iter i = dependents.begin(); i != dependents.end(); ++i) {
799 if (! (*i)->is_stopped()) {
800 all_deps_stopped = false;
810 return all_deps_stopped;
813 // Dependency stopped or is stopping; we must stop too.
814 void ServiceRecord::allDepsStopped()
816 waiting_for_deps = false;
817 if (service_type == ServiceType::PROCESS || service_type == ServiceType::BGPROCESS) {
819 // The process is still kicking on - must actually kill it.
820 if (! onstart_flags.no_sigterm) {
823 if (term_signal != -1) {
824 kill(pid, term_signal);
826 // Now we wait; the rest is done in process_child_callback
829 // The process is already dead.
833 else if (service_type == ServiceType::SCRIPTED) {
835 if (stop_command.length() == 0) {
838 else if (! start_ps_process(stop_arg_parts, false)) {
839 // Couldn't execute stop script, but there's not much we can do:
848 void ServiceRecord::unpin() noexcept
850 if (pinned_started) {
851 pinned_started = false;
852 if (desired_state == ServiceState::STOPPED) {
856 if (pinned_stopped) {
857 pinned_stopped = false;
858 if (desired_state == ServiceState::STARTED) {
864 void ServiceRecord::queueForConsole() noexcept
866 next_for_console = nullptr;
867 auto tail = service_set->consoleQueueTail(this);
868 if (tail == nullptr) {
872 tail->next_for_console = this;
876 void ServiceRecord::releaseConsole() noexcept
878 log_to_console = true;
879 if (next_for_console != nullptr) {
880 next_for_console->acquiredConsole();
883 service_set->consoleQueueTail(nullptr);
887 void ServiceSet::service_active(ServiceRecord *sr) noexcept
892 void ServiceSet::service_inactive(ServiceRecord *sr) noexcept