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) {
78 // Desired state is "started".
82 if (socket_fd != -1) {
87 release_dependencies();
91 void ServiceRecord::process_child_callback(struct ev_loop *loop, ev_child *w, int revents) noexcept
93 ServiceRecord *sr = (ServiceRecord *) w->data;
96 sr->exit_status = w->rstatus;
97 ev_child_stop(loop, w);
99 // Ok, for a process service, any process death which we didn't rig
100 // ourselves is a bit... unexpected. Probably, the child died because
101 // we asked it to (sr->service_state == STOPPING). But even if
102 // we didn't, there's not much we can do.
104 if (sr->waiting_for_execstat) {
105 // We still don't have an exec() status from the forked child, wait for that
106 // before doing any further processing.
110 sr->handle_exit_status();
113 void ServiceRecord::handle_exit_status() noexcept
115 if (exit_status != 0 && service_state != ServiceState::STOPPING) {
116 log(LogLevel::ERROR, "Service ", service_name, " process terminated with exit code ", exit_status);
119 if (doing_recovery) {
121 doing_recovery = false;
122 bool need_stop = false;
123 if (exit_status != 0) {
127 // We need to re-read the PID, since it has now changed.
128 if (service_type == ServiceType::BGPROCESS && pid_file.length() != 0) {
129 if (! read_pid_file()) {
142 if (service_type == ServiceType::PROCESS || service_type == ServiceType::BGPROCESS) {
143 if (service_state == ServiceState::STARTING) {
144 // (only applies to BGPROCESS)
145 if (exit_status == 0) {
152 else if (service_state == ServiceState::STOPPING) {
153 // TODO log non-zero rstatus?
156 else if (smooth_recovery && service_state == ServiceState::STARTED) {
157 // TODO ensure a minimum time between restarts
158 // TODO if we are pinned-started then we should probably check
159 // that dependencies have started before trying to re-start the
161 doing_recovery = (service_type == ServiceType::BGPROCESS);
170 if (service_state == ServiceState::STOPPING) {
171 if (exit_status == 0) {
175 // ??? failed to stop! Let's log it as info:
176 log(LogLevel::INFO, "service ", service_name, " stop command failed with exit code ", exit_status);
177 // Just assume that we stopped, so that any dependencies
183 if (exit_status == 0) {
188 log(LogLevel::ERROR, "service ", service_name, " command failed with exit code ", exit_status);
195 void ServiceRecord::process_child_status(struct ev_loop *loop, ev_io * stat_io, int revents) noexcept
197 ServiceRecord *sr = (ServiceRecord *) stat_io->data;
198 sr->waiting_for_execstat = false;
201 int r = read(stat_io->fd, &exec_status, sizeof(int));
203 ev_io_stop(loop, stat_io);
206 // We read an errno code; exec() failed, and the service startup failed.
208 log(LogLevel::ERROR, sr->service_name, ": execution failed: ", strerror(exec_status));
209 if (sr->service_state == ServiceState::STARTING) {
210 sr->failed_to_start();
212 else if (sr->service_state == ServiceState::STOPPING) {
213 // Must be a scripted servce. We've logged the failure, but it's probably better
214 // not to leave the service in STARTED state:
220 if (sr->service_type == ServiceType::PROCESS) {
221 if (sr->service_state != ServiceState::STARTED) {
227 // Somehow the process managed to complete before we even saw the status.
228 sr->handle_exit_status();
233 void ServiceRecord::require() noexcept
235 if (required_by++ == 0) {
236 // Need to require all our dependencies
237 for (sr_iter i = depends_on.begin(); i != depends_on.end(); ++i) {
241 for (auto i = soft_deps.begin(); i != soft_deps.end(); ++i) {
242 ServiceRecord * to = i->getTo();
248 void ServiceRecord::release() noexcept
250 if (--required_by == 0) {
251 desired_state = ServiceState::STOPPED;
252 // Can stop, and release dependencies once we're stopped.
253 if (service_state == ServiceState::STOPPED) {
254 release_dependencies();
262 void ServiceRecord::release_dependencies() noexcept
264 for (sr_iter i = depends_on.begin(); i != depends_on.end(); ++i) {
268 for (auto i = soft_deps.begin(); i != soft_deps.end(); ++i) {
269 ServiceRecord * to = i->getTo();
273 service_set->service_inactive(this);
276 void ServiceRecord::start(bool transitive) noexcept
278 if ((service_state == ServiceState::STARTING || service_state == ServiceState::STARTED)
279 && desired_state == ServiceState::STOPPED) {
280 // This service was starting, or started, but was set to be stopped.
281 // Cancel the stop (and continue starting/running).
282 notifyListeners(ServiceEvent::STOPCANCELLED);
286 if (!start_explicit) require();
287 start_explicit = true;
290 if (desired_state == ServiceState::STARTED && service_state != ServiceState::STOPPED) return;
292 desired_state = ServiceState::STARTED;
293 service_set->service_active(this);
297 void ServiceRecord::do_start() noexcept
299 if (pinned_stopped) return;
301 if (service_state != ServiceState::STOPPED) {
302 // We're already starting/started, or we are stopping and need to wait for
303 // that the complete.
304 if (service_state != ServiceState::STOPPING || ! can_interrupt_stop()) {
307 // We're STOPPING, and that can be interrupted. Our dependencies might be STOPPING,
308 // but if so they are waiting (for us), so they too can be instantly returned to
310 notifyListeners(ServiceEvent::STOPCANCELLED);
313 service_state = ServiceState::STARTING;
315 waiting_for_deps = true;
317 // Ask dependencies to start, mark them as being waited on.
318 if (! startCheckDependencies(true)) {
322 // Actually start this service.
326 void ServiceRecord::dependencyStarted() noexcept
328 if (service_state != ServiceState::STARTING || ! waiting_for_deps) {
332 if (startCheckDependencies(false)) {
337 bool ServiceRecord::startCheckDependencies(bool start_deps) noexcept
339 bool all_deps_started = true;
341 for (sr_iter i = depends_on.begin(); i != depends_on.end(); ++i) {
342 if ((*i)->service_state != ServiceState::STARTED) {
344 all_deps_started = false;
353 for (auto i = soft_deps.begin(); i != soft_deps.end(); ++i) {
354 ServiceRecord * to = i->getTo();
356 if (to->service_state != ServiceState::STARTED) {
358 i->waiting_on = true;
359 all_deps_started = false;
362 i->waiting_on = false;
365 else if (i->waiting_on) {
366 if (to->service_state != ServiceState::STARTING) {
367 // Service has either started or is no longer starting
368 i->waiting_on = false;
371 // We are still waiting on this service
377 return all_deps_started;
380 bool ServiceRecord::open_socket() noexcept
382 if (socket_path.empty() || socket_fd != -1) {
383 // No socket, or already open
387 const char * saddrname = socket_path.c_str();
388 uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + socket_path.length() + 1;
390 struct sockaddr_un * name = static_cast<sockaddr_un *>(malloc(sockaddr_size));
391 if (name == nullptr) {
392 log(LogLevel::ERROR, service_name, ": Opening activation socket: out of memory");
396 // Un-link any stale socket. TODO: safety check? should at least confirm the path is a socket.
399 name->sun_family = AF_UNIX;
400 strcpy(name->sun_path, saddrname);
402 int sockfd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
404 log(LogLevel::ERROR, service_name, ": Error creating activation socket: ", strerror(errno));
409 if (bind(sockfd, (struct sockaddr *) name, sockaddr_size) == -1) {
410 log(LogLevel::ERROR, service_name, ": Error binding activation socket: ", strerror(errno));
418 // POSIX (1003.1, 2013) says that fchown and fchmod don't necesarily work on sockets. We have to
419 // use chown and chmod instead.
420 if (chown(saddrname, socket_uid, socket_gid)) {
421 log(LogLevel::ERROR, service_name, ": Error setting activation socket owner/group: ", strerror(errno));
426 if (chmod(saddrname, socket_perms) == -1) {
427 log(LogLevel::ERROR, service_name, ": Error setting activation socket permissions: ", strerror(errno));
432 if (listen(sockfd, 128) == -1) { // 128 "seems reasonable".
433 log(LogLevel::ERROR, ": Error listening on activation socket: ", strerror(errno));
442 void ServiceRecord::allDepsStarted(bool has_console) noexcept
444 if (onstart_flags.runs_on_console && ! has_console) {
445 waiting_for_deps = true;
450 waiting_for_deps = false;
452 if (! open_socket()) {
456 if (service_type == ServiceType::PROCESS || service_type == ServiceType::BGPROCESS
457 || service_type == ServiceType::SCRIPTED) {
458 bool start_success = start_ps_process();
459 if (! start_success) {
464 // "internal" service
469 void ServiceRecord::acquiredConsole() noexcept
471 if (service_state != ServiceState::STARTING) {
472 // We got the console but no longer want it.
475 else if (startCheckDependencies(false)) {
476 log_to_console = false;
477 allDepsStarted(true);
480 // We got the console but can't use it yet.
485 bool ServiceRecord::read_pid_file() noexcept
487 const char *pid_file_c = pid_file.c_str();
488 int fd = open(pid_file_c, O_CLOEXEC);
490 char pidbuf[21]; // just enought to hold any 64-bit integer
491 int r = read(fd, pidbuf, 20);
493 pidbuf[r] = 0; // store nul terminator
494 pid = std::atoi(pidbuf);
495 if (kill(pid, 0) == 0) {
496 ev_child_init(&child_listener, process_child_callback, pid, 0);
497 child_listener.data = this;
498 ev_child_start(ev_default_loop(EVFLAG_AUTO), &child_listener);
501 log(LogLevel::ERROR, service_name, ": pid read from pidfile (", pid, ") is not valid");
511 log(LogLevel::ERROR, service_name, ": read pid file: ", strerror(errno));
516 void ServiceRecord::started() noexcept
518 if (onstart_flags.runs_on_console && (service_type == ServiceType::SCRIPTED || service_type == ServiceType::BGPROCESS)) {
519 tcsetpgrp(0, getpgrp());
523 if (service_type == ServiceType::BGPROCESS && pid_file.length() != 0) {
524 if (! read_pid_file()) {
530 logServiceStarted(service_name);
531 service_state = ServiceState::STARTED;
532 notifyListeners(ServiceEvent::STARTED);
534 if (onstart_flags.rw_ready) {
535 open_control_socket(ev_default_loop(EVFLAG_AUTO));
538 if (force_stop || desired_state == ServiceState::STOPPED) {
544 // Notify any dependents whose desired state is STARTED:
545 for (auto i = dependents.begin(); i != dependents.end(); i++) {
546 (*i)->dependencyStarted();
548 for (auto i = soft_dpts.begin(); i != soft_dpts.end(); i++) {
549 (*i)->getFrom()->dependencyStarted();
553 void ServiceRecord::failed_to_start(bool depfailed) noexcept
555 if (!depfailed && onstart_flags.runs_on_console) {
556 tcsetpgrp(0, getpgrp());
560 logServiceFailed(service_name);
561 service_state = ServiceState::STOPPED;
562 notifyListeners(ServiceEvent::FAILEDSTART);
564 // Cancel start of dependents:
565 for (sr_iter i = dependents.begin(); i != dependents.end(); i++) {
566 if ((*i)->service_state == ServiceState::STARTING) {
567 (*i)->failed_to_start(true);
570 for (auto i = soft_dpts.begin(); i != soft_dpts.end(); i++) {
571 // We can send 'start', because this is only a soft dependency.
572 // Our startup failure means that they don't have to wait for us.
573 (*i)->getFrom()->dependencyStarted();
577 bool ServiceRecord::start_ps_process() noexcept
579 return start_ps_process(exec_arg_parts, onstart_flags.runs_on_console);
582 bool ServiceRecord::start_ps_process(const std::vector<const char *> &cmd, bool on_console) noexcept
584 // In general, you can't tell whether fork/exec is successful. We use a pipe to communicate
585 // success/failure from the child to the parent. The pipe is set CLOEXEC so a successful
586 // exec closes the pipe, and the parent sees EOF. If the exec is unsuccessful, the errno
587 // is written to the pipe, and the parent can read it.
590 if (pipe2(pipefd, O_CLOEXEC)) {
595 // Set up the argument array and other data now (before fork), in case memory allocation fails.
597 auto args = cmd.data();
599 const char * logfile = this->logfile.c_str();
601 logfile = "/dev/null";
604 // TODO make sure pipefd's are not 0/1/2 (STDIN/OUT/ERR) - if they are, dup them
605 // until they are not.
607 pid_t forkpid = fork();
615 // If the console already has a session leader, presumably it is us. On the other hand
616 // if it has no session leader, and we don't create one, then control inputs such as
617 // ^C will have no effect.
618 bool do_set_ctty = (tcgetsid(0) == -1);
621 // Child process. Must not allocate memory (or otherwise risk throwing any exception)
622 // from here until exit().
623 ev_default_destroy(); // won't need that on this side, free up fds.
625 constexpr int bufsz = ((CHAR_BIT * sizeof(pid_t) - 1) / 3 + 2) + 11;
626 // "LISTEN_PID=" - 11 characters
629 if (socket_fd != -1) {
631 if (socket_fd != 3) {
635 if (putenv(const_cast<char *>("LISTEN_FDS=1"))) goto failure_out;
637 snprintf(nbuf, bufsz, "LISTEN_PID=%jd", static_cast<intmax_t>(getpid()));
639 if (putenv(nbuf)) goto failure_out;
643 // Re-set stdin, stdout, stderr
644 close(0); close(1); close(2);
646 // TODO rethink this logic. If we open it at not-0, shouldn't we just dup it to 0?:
647 if (open("/dev/null", O_RDONLY) == 0) {
648 // stdin = 0. That's what we should have; proceed with opening
649 // stdout and stderr.
650 open(logfile, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
655 // "run on console" - run as a foreground job on the terminal/console device
658 ioctl(0, TIOCSCTTY, 0);
661 tcsetpgrp(0, getpgrp());
663 // TODO disable suspend (^Z)? (via tcsetattr)
664 // (should be done before TIOCSCTTY)
667 execvp(exec_arg_parts[0], const_cast<char **>(args));
669 // If we got here, the exec failed:
671 int exec_status = errno;
672 write(pipefd[1], &exec_status, sizeof(int));
677 close(pipefd[1]); // close the 'other end' fd
682 ev_io_init(&child_status_listener, process_child_status, pipefd[0], EV_READ);
683 child_status_listener.data = this;
684 ev_io_start(ev_default_loop(EVFLAG_AUTO), &child_status_listener);
686 // Add a process listener so we can detect when the
688 ev_child_init(&child_listener, process_child_callback, pid, 0);
689 child_listener.data = this;
690 ev_child_start(ev_default_loop(EVFLAG_AUTO), &child_listener);
691 waiting_for_execstat = true;
696 // Mark this and all dependent services as force-stopped.
697 void ServiceRecord::forceStop() noexcept
699 if (service_state != ServiceState::STOPPED) {
705 void ServiceRecord::dependentStopped() noexcept
707 if (service_state == ServiceState::STOPPING && waiting_for_deps) {
708 // Check the other dependents before we stop.
709 if (stopCheckDependents()) {
715 void ServiceRecord::stop() noexcept
717 if (desired_state == ServiceState::STOPPED && service_state != ServiceState::STARTED) return;
719 if (start_explicit) {
720 start_explicit = false;
725 void ServiceRecord::do_stop() noexcept
727 if (pinned_started) return;
729 if (service_state != ServiceState::STARTED) {
730 if (service_state == ServiceState::STARTING) {
731 if (! can_interrupt_start()) {
732 // Well this is awkward: we're going to have to continue
733 // starting, but we don't want any dependents to think that
734 // they are still waiting to start.
735 // Make sure they remain stopped:
740 // We must have had desired_state == STARTED.
741 notifyListeners(ServiceEvent::STARTCANCELLED);
743 // Reaching this point, we have can_interrupt_start() == true. So,
744 // we can stop. Dependents might be starting, but they must be
745 // waiting on us, so they should also be immediately stoppable.
746 // Fall through to below,.
749 // If we're starting we need to wait for that to complete.
750 // If we're already stopping/stopped there's nothing to do.
755 service_state = ServiceState::STOPPING;
756 waiting_for_deps = true;
758 // If we get here, we are in STARTED state; stop all dependents.
759 if (stopDependents()) {
764 bool ServiceRecord::stopCheckDependents() noexcept
766 bool all_deps_stopped = true;
767 for (sr_iter i = dependents.begin(); i != dependents.end(); ++i) {
768 if (! (*i)->is_stopped()) {
769 all_deps_stopped = false;
774 return all_deps_stopped;
777 bool ServiceRecord::stopDependents() noexcept
779 bool all_deps_stopped = true;
780 for (sr_iter i = dependents.begin(); i != dependents.end(); ++i) {
781 if (! (*i)->is_stopped()) {
782 all_deps_stopped = false;
792 return all_deps_stopped;
795 // Dependency stopped or is stopping; we must stop too.
796 void ServiceRecord::allDepsStopped()
798 waiting_for_deps = false;
799 if (service_type == ServiceType::PROCESS || service_type == ServiceType::BGPROCESS) {
801 // The process is still kicking on - must actually kill it.
802 if (! onstart_flags.no_sigterm) {
805 if (term_signal != -1) {
806 kill(pid, term_signal);
808 // Now we wait; the rest is done in process_child_callback
811 // The process is already dead.
815 else if (service_type == ServiceType::SCRIPTED) {
817 if (stop_command.length() == 0) {
820 else if (! start_ps_process(stop_arg_parts, false)) {
821 // Couldn't execute stop script, but there's not much we can do:
830 void ServiceRecord::pinStart() noexcept
833 pinned_started = true;
836 void ServiceRecord::pinStop() noexcept
839 pinned_stopped = true;
842 void ServiceRecord::unpin() noexcept
844 if (pinned_started) {
845 pinned_started = false;
846 if (desired_state == ServiceState::STOPPED) {
850 if (pinned_stopped) {
851 pinned_stopped = false;
852 if (desired_state == ServiceState::STARTED) {
858 void ServiceRecord::queueForConsole() noexcept
860 next_for_console = nullptr;
861 auto tail = service_set->consoleQueueTail(this);
862 if (tail == nullptr) {
866 tail->next_for_console = this;
870 void ServiceRecord::releaseConsole() noexcept
872 log_to_console = true;
873 if (next_for_console != nullptr) {
874 next_for_console->acquiredConsole();
877 service_set->consoleQueueTail(nullptr);
881 void ServiceSet::service_active(ServiceRecord *sr) noexcept
886 void ServiceSet::service_inactive(ServiceRecord *sr) noexcept