service: check error from kill when checking ability to track child.
[oweals/dinit.git] / src / service.cc
1 #include <cstring>
2 #include <cerrno>
3 #include <sstream>
4 #include <iterator>
5 #include <memory>
6 #include <cstddef>
7
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <sys/ioctl.h>
11 #include <sys/un.h>
12 #include <sys/socket.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include <termios.h>
16
17 #include "service.h"
18 #include "dinit-log.h"
19 #include "dinit-socket.h"
20 #include "dinit-util.h"
21
22 /*
23  * service.cc - Service management.
24  * See service.h for details.
25  */
26
27 // from dinit.cc:
28 void open_control_socket(bool report_ro_failure = true) noexcept;
29 void setup_external_log() noexcept;
30 extern eventloop_t eventLoop;
31
32 // Find the requested service by name
33 static service_record * find_service(const std::list<service_record *> & records,
34                                     const char *name) noexcept
35 {
36     using std::list;
37     list<service_record *>::const_iterator i = records.begin();
38     for ( ; i != records.end(); i++ ) {
39         if (strcmp((*i)->getServiceName().c_str(), name) == 0) {
40             return *i;
41         }
42     }
43     return (service_record *)0;
44 }
45
46 service_record * service_set::find_service(const std::string &name) noexcept
47 {
48     return ::find_service(records, name.c_str());
49 }
50
51 void service_set::stopService(const std::string & name) noexcept
52 {
53     service_record *record = find_service(name);
54     if (record != nullptr) {
55         record->stop();
56         process_queues();
57     }
58 }
59
60 // Called when a service has actually stopped; dependents have stopped already, unless this stop
61 // is due to an unexpected process termination.
62 void service_record::stopped() noexcept
63 {
64     if (onstart_flags.runs_on_console) {
65         tcsetpgrp(0, getpgrp());
66         discard_console_log_buffer();
67         release_console();
68     }
69
70     force_stop = false;
71
72     // If we are a soft dependency of another target, break the acquisition from that target now:
73     for (auto dependent : soft_dpts) {
74         if (dependent->holding_acq) {
75             dependent->holding_acq = false;
76             release();
77         }
78     }
79
80     bool will_restart = (desired_state == service_state_t::STARTED)
81             && services->get_auto_restart();
82
83     for (auto dependency : depends_on) {
84         // we signal dependencies in case they are waiting for us to stop:
85         dependency->dependent_stopped();
86     }
87
88     service_state = service_state_t::STOPPED;
89
90     if (will_restart) {
91         // Desired state is "started".
92         restarting = true;
93         start(false);
94     }
95     else {
96         if (socket_fd != -1) {
97             close(socket_fd);
98             socket_fd = -1;
99         }
100         
101         if (start_explicit) {
102             start_explicit = false;
103             release();
104         }
105         else if (required_by == 0) {
106             services->service_inactive(this);
107         }
108     }
109
110     logServiceStopped(service_name);
111     notify_listeners(service_event::STOPPED);
112 }
113
114 dasynq::rearm service_child_watcher::status_change(eventloop_t &loop, pid_t child, int status) noexcept
115 {
116     base_process_service *sr = service;
117     
118     sr->pid = -1;
119     sr->exit_status = status;
120     
121     // Ok, for a process service, any process death which we didn't rig
122     // ourselves is a bit... unexpected. Probably, the child died because
123     // we asked it to (sr->service_state == STOPPING). But even if
124     // we didn't, there's not much we can do.
125     
126     if (sr->waiting_for_execstat) {
127         // We still don't have an exec() status from the forked child, wait for that
128         // before doing any further processing.
129         return rearm::NOOP; // hold watch reservation
130     }
131     
132     // Must stop watch now since handle_exit_status might result in re-launch:
133     // (stop_watch instead of deregister, so that we hold watch reservation).
134     stop_watch(loop);
135     
136     sr->handle_exit_status(status);
137     return rearm::NOOP;
138 }
139
140 bool service_record::do_auto_restart() noexcept
141 {
142     if (auto_restart) {
143         return services->get_auto_restart();
144     }
145     return false;
146 }
147
148 void service_record::emergency_stop() noexcept
149 {
150     if (! do_auto_restart() && start_explicit) {
151         start_explicit = false;
152         release();
153     }
154     forceStop();
155     stop_dependents();
156     stopped();
157 }
158
159 void process_service::handle_exit_status(int exit_status) noexcept
160 {
161     bool did_exit = WIFEXITED(exit_status);
162     bool was_signalled = WIFSIGNALED(exit_status);
163
164     if (exit_status != 0 && service_state != service_state_t::STOPPING) {
165         if (did_exit) {
166             log(LogLevel::ERROR, "Service ", service_name, " process terminated with exit code ", WEXITSTATUS(exit_status));
167         }
168         else if (was_signalled) {
169             log(LogLevel::ERROR, "Service ", service_name, " terminated due to signal ", WTERMSIG(exit_status));
170         }
171     }
172
173     if (service_state == service_state_t::STARTING) {
174         if (did_exit && WEXITSTATUS(exit_status) == 0) {
175             started();
176         }
177         else {
178             failed_to_start();
179         }
180     }
181     else if (service_state == service_state_t::STOPPING) {
182         // We won't log a non-zero exit status or termination due to signal here -
183         // we assume that the process died because we signalled it.
184         stopped();
185     }
186     else if (smooth_recovery && service_state == service_state_t::STARTED && desired_state == service_state_t::STARTED) {
187         // TODO if we are pinned-started then we should probably check
188         //      that dependencies have started before trying to re-start the
189         //      service process.
190         if (! restart_ps_process()) {
191             emergency_stop();
192             services->process_queues();
193         }
194         return;
195     }
196     else {
197         emergency_stop();
198     }
199     services->process_queues();
200 }
201
202 void bgproc_service::handle_exit_status(int exit_status) noexcept
203 {
204     begin:
205     bool did_exit = WIFEXITED(exit_status);
206     bool was_signalled = WIFSIGNALED(exit_status);
207
208     if (exit_status != 0 && service_state != service_state_t::STOPPING) {
209         if (did_exit) {
210             log(LogLevel::ERROR, "Service ", service_name, " process terminated with exit code ", WEXITSTATUS(exit_status));
211         }
212         else if (was_signalled) {
213             log(LogLevel::ERROR, "Service ", service_name, " terminated due to signal ", WTERMSIG(exit_status));
214         }
215     }
216
217     if (doing_recovery) {
218         doing_recovery = false;
219         bool need_stop = false;
220         if ((did_exit && WEXITSTATUS(exit_status) != 0) || was_signalled) {
221             need_stop = true;
222         }
223         else {
224             // We need to re-read the PID, since it has now changed.
225             if (pid_file.length() != 0) {
226                 auto pid_result = read_pid_file(&exit_status);
227                 switch (pid_result) {
228                     case pid_result_t::FAILED:
229                         // Failed startup: no auto-restart.
230                         need_stop = true;
231                         break;
232                     case pid_result_t::TERMINATED:
233                         goto begin;
234                     case pid_result_t::OK:
235                         break;
236                 }
237             }
238         }
239
240         if (need_stop) {
241             // Failed startup: no auto-restart.
242             emergency_stop();
243             services->process_queues();
244         }
245
246         return;
247     }
248
249     if (service_state == service_state_t::STARTING) {
250         // POSIX requires that if the process exited clearly with a status code of 0,
251         // the exit status value will be 0:
252         if (exit_status == 0) {
253             auto pid_result = read_pid_file(&exit_status);
254             switch (pid_result) {
255                 case pid_result_t::FAILED:
256                     // Failed startup: no auto-restart.
257                     failed_to_start();
258                     break;
259                 case pid_result_t::TERMINATED:
260                     // started, but immediately terminated
261                     started();
262                     goto begin;
263                 case pid_result_t::OK:
264                     started();
265                     break;
266             }
267         }
268         else {
269             failed_to_start();
270         }
271     }
272     else if (service_state == service_state_t::STOPPING) {
273         // We won't log a non-zero exit status or termination due to signal here -
274         // we assume that the process died because we signalled it.
275         stopped();
276     }
277     else if (smooth_recovery && service_state == service_state_t::STARTED && desired_state == service_state_t::STARTED) {
278         // TODO if we are pinned-started then we should probably check
279         //      that dependencies have started before trying to re-start the
280         //      service process.
281         doing_recovery = true;
282         if (! restart_ps_process()) {
283             emergency_stop();
284             services->process_queues();
285         }
286         return;
287     }
288     else {
289         // we must be STARTED
290         if (! do_auto_restart() && start_explicit) {
291             start_explicit = false;
292             release();
293         }
294         forceStop();
295         stop_dependents();
296         stopped();
297     }
298     services->process_queues();
299 }
300
301 void scripted_service::handle_exit_status(int exit_status) noexcept
302 {
303     bool did_exit = WIFEXITED(exit_status);
304     bool was_signalled = WIFSIGNALED(exit_status);
305
306     if (service_state == service_state_t::STOPPING) {
307         if (did_exit && WEXITSTATUS(exit_status) == 0) {
308             stopped();
309         }
310         else {
311             // ??? failed to stop! Let's log it as info:
312             if (did_exit) {
313                 log(LogLevel::INFO, "Service ", service_name, " stop command failed with exit code ", WEXITSTATUS(exit_status));
314             }
315             else if (was_signalled) {
316                 log(LogLevel::INFO, "Serivice ", service_name, " stop command terminated due to signal ", WTERMSIG(exit_status));
317             }
318             // Just assume that we stopped, so that any dependencies
319             // can be stopped:
320             stopped();
321         }
322         services->process_queues();
323     }
324     else { // STARTING
325         if (exit_status == 0) {
326             started();
327         }
328         else {
329             // failed to start
330             if (did_exit) {
331                 log(LogLevel::ERROR, "Service ", service_name, " command failed with exit code ", WEXITSTATUS(exit_status));
332             }
333             else if (was_signalled) {
334                 log(LogLevel::ERROR, "Service ", service_name, " command terminated due to signal ", WTERMSIG(exit_status));
335             }
336             failed_to_start();
337         }
338         services->process_queues();
339     }
340 }
341
342 rearm exec_status_pipe_watcher::fd_event(eventloop_t &loop, int fd, int flags) noexcept
343 {
344     base_process_service *sr = service;
345     sr->waiting_for_execstat = false;
346     
347     int exec_status;
348     int r = read(get_watched_fd(), &exec_status, sizeof(int));
349     deregister(loop);
350     close(get_watched_fd());
351     
352     if (r > 0) {
353         // We read an errno code; exec() failed, and the service startup failed.
354         if (sr->pid != -1) {
355             sr->child_listener.deregister(eventLoop, sr->pid);
356             sr->reserved_child_watch = false;
357         }
358         sr->pid = -1;
359         log(LogLevel::ERROR, sr->service_name, ": execution failed: ", strerror(exec_status));
360         if (sr->service_state == service_state_t::STARTING) {
361             sr->failed_to_start();
362         }
363         else if (sr->service_state == service_state_t::STOPPING) {
364             // Must be a scripted service. We've logged the failure, but it's probably better
365             // not to leave the service in STARTED state:
366             sr->stopped();
367         }
368     }
369     else {
370         // exec() succeeded.
371         if (sr->record_type == service_type::PROCESS) {
372             // This could be a smooth recovery (state already STARTED). Even more, the process
373             // might be stopped (and killed via a signal) during smooth recovery.  We don't to
374             // process startup again in either case, so we check for state STARTING:
375             if (sr->service_state == service_state_t::STARTING) {
376                 sr->started();
377             }
378         }
379         
380         if (sr->pid == -1) {
381             // Somehow the process managed to complete before we even saw the status.
382             sr->handle_exit_status(sr->exit_status);
383         }
384     }
385     
386     sr->services->process_queues();
387     
388     return rearm::REMOVED;
389 }
390
391 void service_record::require() noexcept
392 {
393     if (required_by++ == 0) {
394         prop_require = !prop_release;
395         prop_release = false;
396         services->addToPropQueue(this);
397     }
398 }
399
400 void service_record::release() noexcept
401 {
402     if (--required_by == 0) {
403         desired_state = service_state_t::STOPPED;
404
405         // Can stop, and can release dependencies now. We don't need to issue a release if
406         // the require was pending though:
407         prop_release = !prop_require;
408         prop_require = false;
409         services->addToPropQueue(this);
410
411         if (service_state == service_state_t::STOPPED) {
412             services->service_inactive(this);
413         }
414         else {
415             do_stop();
416         }
417     }
418 }
419
420 void service_record::release_dependencies() noexcept
421 {
422     for (sr_iter i = depends_on.begin(); i != depends_on.end(); ++i) {
423         (*i)->release();
424     }
425
426     for (auto i = soft_deps.begin(); i != soft_deps.end(); ++i) {
427         service_record * to = i->getTo();
428         if (i->holding_acq) {
429             to->release();
430             i->holding_acq = false;
431         }
432     }
433 }
434
435 void service_record::start(bool activate) noexcept
436 {
437     if (activate && ! start_explicit) {
438         require();
439         start_explicit = true;
440     }
441     
442     if (desired_state == service_state_t::STARTED && service_state != service_state_t::STOPPED) return;
443
444     bool was_active = service_state != service_state_t::STOPPED || desired_state != service_state_t::STOPPED;
445     desired_state = service_state_t::STARTED;
446     
447     if (service_state != service_state_t::STOPPED) {
448         // We're already starting/started, or we are stopping and need to wait for
449         // that the complete.
450         if (service_state != service_state_t::STOPPING || ! can_interrupt_stop()) {
451             return;
452         }
453         // We're STOPPING, and that can be interrupted. Our dependencies might be STOPPING,
454         // but if so they are waiting (for us), so they too can be instantly returned to
455         // STARTING state.
456         notify_listeners(service_event::STOPCANCELLED);
457     }
458     else if (! was_active) {
459         services->service_active(this);
460     }
461
462     service_state = service_state_t::STARTING;
463     waiting_for_deps = true;
464
465     if (start_check_dependencies(true)) {
466         services->addToStartQueue(this);
467     }
468 }
469
470 void service_record::do_propagation() noexcept
471 {
472     if (prop_require) {
473         // Need to require all our dependencies
474         for (sr_iter i = depends_on.begin(); i != depends_on.end(); ++i) {
475             (*i)->require();
476         }
477
478         for (auto i = soft_deps.begin(); i != soft_deps.end(); ++i) {
479             service_record * to = i->getTo();
480             to->require();
481             i->holding_acq = true;
482         }
483         
484         prop_require = false;
485     }
486     
487     if (prop_release) {
488         release_dependencies();
489         prop_release = false;
490     }
491     
492     if (prop_failure) {
493         prop_failure = false;
494         failed_to_start(true);
495     }
496     
497     if (prop_start) {
498         prop_start = false;
499         start(false);
500     }
501
502     if (prop_stop) {
503         prop_stop = false;
504         do_stop();
505     }
506 }
507
508 void service_record::execute_transition() noexcept
509 {
510     if (service_state == service_state_t::STARTING) {
511         if (start_check_dependencies(false)) {
512             all_deps_started(false);
513         }
514     }
515     else if (service_state == service_state_t::STOPPING) {
516         if (stop_check_dependents()) {
517             all_deps_stopped();
518         }
519     }
520 }
521
522 void service_record::do_start() noexcept
523 {
524     if (pinned_stopped) return;
525     
526     if (service_state != service_state_t::STARTING) {
527         return;
528     }
529     
530     service_state = service_state_t::STARTING;
531
532     waiting_for_deps = true;
533
534     // Ask dependencies to start, mark them as being waited on.
535     if (start_check_dependencies(false)) {
536         // Once all dependencies are started, we start properly:
537         all_deps_started();
538     }
539 }
540
541 void service_record::dependencyStarted() noexcept
542 {
543     if (service_state == service_state_t::STARTING && waiting_for_deps) {
544         services->addToStartQueue(this);
545     }
546 }
547
548 bool service_record::start_check_dependencies(bool start_deps) noexcept
549 {
550     bool all_deps_started = true;
551
552     for (sr_iter i = depends_on.begin(); i != depends_on.end(); ++i) {
553         if ((*i)->service_state != service_state_t::STARTED) {
554             if (start_deps) {
555                 all_deps_started = false;
556                 (*i)->prop_start = true;
557                 services->addToPropQueue(*i);
558             }
559             else {
560                 return false;
561             }
562         }
563     }
564
565     for (auto i = soft_deps.begin(); i != soft_deps.end(); ++i) {
566         service_record * to = i->getTo();
567         if (start_deps) {
568             if (to->service_state != service_state_t::STARTED) {
569                 to->prop_start = true;
570                 services->addToPropQueue(to);
571                 i->waiting_on = true;
572                 all_deps_started = false;
573             }
574             else {
575                 i->waiting_on = false;
576             }
577         }
578         else if (i->waiting_on) {
579             if (to->service_state != service_state_t::STARTING) {
580                 // Service has either started or is no longer starting
581                 i->waiting_on = false;
582             }
583             else {
584                 // We are still waiting on this service
585                 return false;
586             }
587         }
588     }
589     
590     return all_deps_started;
591 }
592
593 bool service_record::open_socket() noexcept
594 {
595     if (socket_path.empty() || socket_fd != -1) {
596         // No socket, or already open
597         return true;
598     }
599     
600     const char * saddrname = socket_path.c_str();
601     
602     // Check the specified socket path
603     struct stat stat_buf;
604     if (stat(saddrname, &stat_buf) == 0) {
605         if ((stat_buf.st_mode & S_IFSOCK) == 0) {
606             // Not a socket
607             log(LogLevel::ERROR, service_name, ": Activation socket file exists (and is not a socket)");
608             return false;
609         }
610     }
611     else if (errno != ENOENT) {
612         // Other error
613         log(LogLevel::ERROR, service_name, ": Error checking activation socket: ", strerror(errno));
614         return false;
615     }
616
617     // Remove stale socket file (if it exists).
618     // We won't test the return from unlink - if it fails other than due to ENOENT, we should get an
619     // error when we try to create the socket anyway.
620     unlink(saddrname);
621
622     uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + socket_path.length() + 1;
623     struct sockaddr_un * name = static_cast<sockaddr_un *>(malloc(sockaddr_size));
624     if (name == nullptr) {
625         log(LogLevel::ERROR, service_name, ": Opening activation socket: out of memory");
626         return false;
627     }
628
629     name->sun_family = AF_UNIX;
630     strcpy(name->sun_path, saddrname);
631
632     int sockfd = dinit_socket(AF_UNIX, SOCK_STREAM, 0, SOCK_NONBLOCK | SOCK_CLOEXEC);
633     if (sockfd == -1) {
634         log(LogLevel::ERROR, service_name, ": Error creating activation socket: ", strerror(errno));
635         free(name);
636         return false;
637     }
638
639     if (bind(sockfd, (struct sockaddr *) name, sockaddr_size) == -1) {
640         log(LogLevel::ERROR, service_name, ": Error binding activation socket: ", strerror(errno));
641         close(sockfd);
642         free(name);
643         return false;
644     }
645     
646     free(name);
647     
648     // POSIX (1003.1, 2013) says that fchown and fchmod don't necessarily work on sockets. We have to
649     // use chown and chmod instead.
650     if (chown(saddrname, socket_uid, socket_gid)) {
651         log(LogLevel::ERROR, service_name, ": Error setting activation socket owner/group: ", strerror(errno));
652         close(sockfd);
653         return false;
654     }
655     
656     if (chmod(saddrname, socket_perms) == -1) {
657         log(LogLevel::ERROR, service_name, ": Error setting activation socket permissions: ", strerror(errno));
658         close(sockfd);
659         return false;
660     }
661
662     if (listen(sockfd, 128) == -1) { // 128 "seems reasonable".
663         log(LogLevel::ERROR, ": Error listening on activation socket: ", strerror(errno));
664         close(sockfd);
665         return false;
666     }
667     
668     socket_fd = sockfd;
669     return true;
670 }
671
672 void service_record::all_deps_started(bool has_console) noexcept
673 {
674     if (onstart_flags.starts_on_console && ! has_console) {
675         waiting_for_deps = true;
676         queue_for_console();
677         return;
678     }
679     
680     waiting_for_deps = false;
681
682     // We overload can_interrupt_start to check whether there is any other
683     // process (eg restart timer) that needs to finish before starting.
684     if (can_interrupt_start()) {
685         waiting_for_deps = true;
686         return;
687     }
688
689     if (! open_socket()) {
690         failed_to_start();
691     }
692
693     bool start_success = start_ps_process();
694     if (! start_success) {
695         failed_to_start();
696     }
697 }
698
699 void service_record::acquiredConsole() noexcept
700 {
701     if (service_state != service_state_t::STARTING) {
702         // We got the console but no longer want it.
703         release_console();
704     }
705     else if (start_check_dependencies(false)) {
706         all_deps_started(true);
707     }
708     else {
709         // We got the console but can't use it yet.
710         release_console();
711     }
712 }
713
714 bgproc_service::pid_result_t
715 bgproc_service::read_pid_file(int *exit_status) noexcept
716 {
717     const char *pid_file_c = pid_file.c_str();
718     int fd = open(pid_file_c, O_CLOEXEC);
719     if (fd == -1) {
720         log(LogLevel::ERROR, service_name, ": read pid file: ", strerror(errno));
721         return pid_result_t::FAILED;
722     }
723
724     char pidbuf[21]; // just enough to hold any 64-bit integer
725     int r = ss_read(fd, pidbuf, 20);
726     if (r < 0) {
727         // Could not read from PID file
728         log(LogLevel::ERROR, service_name, ": could not read from pidfile; ", strerror(errno));
729         close(fd);
730         return pid_result_t::FAILED;
731     }
732
733     close(fd);
734     pidbuf[r] = 0; // store nul terminator
735
736     bool valid_pid = false;
737     try {
738         unsigned long long v = std::stoull(pidbuf, nullptr, 0);
739         if (v <= std::numeric_limits<pid_t>::max()) {
740             pid = (pid_t) v;
741             valid_pid = true;
742         }
743     }
744     catch (std::out_of_range &exc) {
745         // Too large?
746     }
747     catch (std::invalid_argument &exc) {
748         // Ok, so it doesn't look like a number: proceed...
749     }
750
751     if (valid_pid) {
752         pid_t wait_r = waitpid(pid, exit_status, WNOHANG);
753         if (wait_r == -1 && errno == ECHILD) {
754             // We can't track this child - check process exists:
755             if (kill(pid, 0) == 0 || errno != ESRCH) {
756                 tracking_child = false;
757                 return pid_result_t::OK;
758             }
759             else {
760                 log(LogLevel::ERROR, service_name, ": pid read from pidfile (", pid, ") is not valid");
761                 pid = -1;
762                 return pid_result_t::FAILED;
763             }
764         }
765         else if (wait_r == pid) {
766             pid = -1;
767             return pid_result_t::TERMINATED;
768         }
769         else if (wait_r == 0) {
770             // We can track the child
771             child_listener.add_reserved(eventLoop, pid, DEFAULT_PRIORITY - 10);
772             tracking_child = true;
773             reserved_child_watch = true;
774             return pid_result_t::OK;
775         }
776     }
777
778     log(LogLevel::ERROR, service_name, ": pid read from pidfile (", pid, ") is not valid");
779     pid = -1;
780     return pid_result_t::FAILED;
781 }
782
783 void service_record::started() noexcept
784 {
785     if (onstart_flags.starts_on_console && ! onstart_flags.runs_on_console) {
786         tcsetpgrp(0, getpgrp());
787         release_console();
788     }
789
790     logServiceStarted(service_name);
791     service_state = service_state_t::STARTED;
792     notify_listeners(service_event::STARTED);
793
794     if (onstart_flags.rw_ready) {
795         open_control_socket();
796     }
797     if (onstart_flags.log_ready) {
798         setup_external_log();
799     }
800
801     if (force_stop || desired_state == service_state_t::STOPPED) {
802         // We must now stop.
803         do_stop();
804         return;
805     }
806
807     // Notify any dependents whose desired state is STARTED:
808     for (auto i = dependents.begin(); i != dependents.end(); i++) {
809         (*i)->dependencyStarted();
810     }
811     for (auto i = soft_dpts.begin(); i != soft_dpts.end(); i++) {
812         (*i)->getFrom()->dependencyStarted();
813     }
814 }
815
816 void service_record::failed_to_start(bool depfailed) noexcept
817 {
818     if (!depfailed && onstart_flags.starts_on_console) {
819         tcsetpgrp(0, getpgrp());
820         release_console();
821     }
822     
823     logServiceFailed(service_name);
824     service_state = service_state_t::STOPPED;
825     if (start_explicit) {
826         start_explicit = false;
827         release();
828     }
829     notify_listeners(service_event::FAILEDSTART);
830     
831     // Cancel start of dependents:
832     for (sr_iter i = dependents.begin(); i != dependents.end(); i++) {
833         if ((*i)->service_state == service_state_t::STARTING) {
834             (*i)->prop_failure = true;
835             services->addToPropQueue(*i);
836         }
837     }    
838     for (auto i = soft_dpts.begin(); i != soft_dpts.end(); i++) {
839         // We can send 'start', because this is only a soft dependency.
840         // Our startup failure means that they don't have to wait for us.
841         if ((*i)->waiting_on) {
842             (*i)->holding_acq = false;
843             (*i)->waiting_on = false;
844             (*i)->getFrom()->dependencyStarted();
845             release();
846         }
847     }
848 }
849
850 bool service_record::start_ps_process() noexcept
851 {
852     // default implementation: there is no process, so we are started.
853     started();
854     return true;
855 }
856
857 bool base_process_service::start_ps_process() noexcept
858 {
859     if (restarting) {
860         return restart_ps_process();
861     }
862     else {
863         eventLoop.get_time(restart_interval_time, clock_type::MONOTONIC);
864         restart_interval_count = 0;
865         return start_ps_process(exec_arg_parts, onstart_flags.starts_on_console);
866     }
867 }
868
869 bool base_process_service::start_ps_process(const std::vector<const char *> &cmd, bool on_console) noexcept
870 {
871     // In general, you can't tell whether fork/exec is successful. We use a pipe to communicate
872     // success/failure from the child to the parent. The pipe is set CLOEXEC so a successful
873     // exec closes the pipe, and the parent sees EOF. If the exec is unsuccessful, the errno
874     // is written to the pipe, and the parent can read it.
875
876     eventLoop.get_time(last_start_time, clock_type::MONOTONIC);
877
878     int pipefd[2];
879     if (pipe2(pipefd, O_CLOEXEC)) {
880         log(LogLevel::ERROR, service_name, ": can't create status check pipe: ", strerror(errno));
881         return false;
882     }
883
884     const char * logfile = this->logfile.c_str();
885     if (*logfile == 0) {
886         logfile = "/dev/null";
887     }
888
889     bool child_status_registered = false;
890     control_conn_t *control_conn = nullptr;
891     
892     int control_socket[2] = {-1, -1};
893     if (onstart_flags.pass_cs_fd) {
894         if (dinit_socketpair(AF_UNIX, SOCK_STREAM, /* protocol */ 0, control_socket, SOCK_NONBLOCK)) {
895             log(LogLevel::ERROR, service_name, ": can't create control socket: ", strerror(errno));
896             goto out_p;
897         }
898         
899         // Make the server side socket close-on-exec:
900         int fdflags = fcntl(control_socket[0], F_GETFD);
901         fcntl(control_socket[0], F_SETFD, fdflags | FD_CLOEXEC);
902         
903         try {
904             control_conn = new control_conn_t(&eventLoop, services, control_socket[0]);
905         }
906         catch (std::exception &exc) {
907             log(LogLevel::ERROR, service_name, ": can't launch process; out of memory");
908             goto out_cs;
909         }
910     }
911     
912     // Set up complete, now fork and exec:
913     
914     pid_t forkpid;
915     
916     try {
917         child_status_listener.add_watch(eventLoop, pipefd[0], IN_EVENTS);
918         child_status_registered = true;
919         
920         // We specify a high priority (i.e. low priority value) so that process termination is
921         // handled early. This means we have always recorded that the process is terminated by the
922         // time that we handle events that might otherwise cause us to signal the process, so we
923         // avoid sending a signal to an invalid (and possibly recycled) process ID.
924         forkpid = child_listener.fork(eventLoop, reserved_child_watch, DEFAULT_PRIORITY - 10);
925         reserved_child_watch = true;
926     }
927     catch (std::exception &e) {
928         log(LogLevel::ERROR, service_name, ": Could not fork: ", e.what());
929         goto out_cs_h;
930     }
931
932     if (forkpid == 0) {
933         run_child_proc(cmd.data(), logfile, on_console, pipefd[1], control_socket[1]);
934     }
935     else {
936         // Parent process
937         close(pipefd[1]); // close the 'other end' fd
938         if (control_socket[1] != -1) {
939             close(control_socket[1]);
940         }
941         pid = forkpid;
942
943         waiting_for_execstat = true;
944         return true;
945     }
946
947     // Failure exit:
948     
949     out_cs_h:
950     if (child_status_registered) {
951         child_status_listener.deregister(eventLoop);
952     }
953     
954     if (onstart_flags.pass_cs_fd) {
955         delete control_conn;
956     
957         out_cs:
958         close(control_socket[0]);
959         close(control_socket[1]);
960     }
961     
962     out_p:
963     close(pipefd[0]);
964     close(pipefd[1]);
965     
966     return false;
967 }
968
969 void service_record::run_child_proc(const char * const *args, const char *logfile, bool on_console,
970         int wpipefd, int csfd) noexcept
971 {
972     // Child process. Must not allocate memory (or otherwise risk throwing any exception)
973     // from here until exit().
974
975     // If the console already has a session leader, presumably it is us. On the other hand
976     // if it has no session leader, and we don't create one, then control inputs such as
977     // ^C will have no effect.
978     bool do_set_ctty = (tcgetsid(0) == -1);
979     
980     // Copy signal mask, but unmask signals that we masked on startup. For the moment, we'll
981     // also block all signals, since apparently dup() can be interrupted (!!! really, POSIX??).
982     sigset_t sigwait_set;
983     sigset_t sigall_set;
984     sigfillset(&sigall_set);
985     sigprocmask(SIG_SETMASK, &sigall_set, &sigwait_set);
986     sigdelset(&sigwait_set, SIGCHLD);
987     sigdelset(&sigwait_set, SIGINT);
988     sigdelset(&sigwait_set, SIGTERM);
989     sigdelset(&sigwait_set, SIGQUIT);
990     
991     constexpr int bufsz = ((CHAR_BIT * sizeof(pid_t)) / 3 + 2) + 11;
992     // "LISTEN_PID=" - 11 characters; the expression above gives a conservative estimate
993     // on the maxiumum number of bytes required for LISTEN=nnn, including nul terminator,
994     // where nnn is a pid_t in decimal (i.e. one decimal digit is worth just over 3 bits).
995     char nbuf[bufsz];
996     
997     // "DINIT_CS_FD=" - 12 bytes. (we -1 from sizeof(int) in account of sign bit).
998     constexpr int csenvbufsz = ((CHAR_BIT * sizeof(int) - 1) / 3 + 2) + 12;
999     char csenvbuf[csenvbufsz];
1000     
1001     int minfd = (socket_fd == -1) ? 3 : 4;
1002
1003     // Move wpipefd/csfd to another fd if necessary
1004     if (wpipefd < minfd) {
1005         wpipefd = fcntl(wpipefd, F_DUPFD_CLOEXEC, minfd);
1006         if (wpipefd == -1) goto failure_out;
1007     }
1008     
1009     if (csfd != -1 && csfd < minfd) {
1010         csfd = fcntl(csfd, F_DUPFD, minfd);
1011         if (csfd == -1) goto failure_out;
1012     }
1013     
1014     if (socket_fd != -1) {
1015         
1016         if (dup2(socket_fd, 3) == -1) goto failure_out;
1017         if (socket_fd != 3) {
1018             close(socket_fd);
1019         }
1020         
1021         if (putenv(const_cast<char *>("LISTEN_FDS=1"))) goto failure_out;
1022         snprintf(nbuf, bufsz, "LISTEN_PID=%jd", static_cast<intmax_t>(getpid()));
1023         if (putenv(nbuf)) goto failure_out;
1024     }
1025     
1026     if (csfd != -1) {
1027         snprintf(csenvbuf, csenvbufsz, "DINIT_CS_FD=%d", csfd);
1028         if (putenv(csenvbuf)) goto failure_out;
1029     }
1030
1031     if (! on_console) {
1032         // Re-set stdin, stdout, stderr
1033         close(0); close(1); close(2);
1034
1035         if (open("/dev/null", O_RDONLY) == 0) {
1036             // stdin = 0. That's what we should have; proceed with opening
1037             // stdout and stderr.
1038             if (open(logfile, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR) != 1) {
1039                 goto failure_out;
1040             }
1041             if (dup2(1, 2) != 2) {
1042                 goto failure_out;
1043             }
1044         }
1045         else goto failure_out;
1046         
1047         // We have the option of creating a session and process group, or just a new process
1048         // group. If we just create a new process group, the child process cannot make itself
1049         // a session leader if it wants to do that (eg getty/login will generally want this).
1050         // If we do neither, and we are running with a controlling terminal, a ^C or similar
1051         // will also affect the child process (which probably isn't so bad, though since we
1052         // will handle the shutdown ourselves it's not necessary). Creating a new session
1053         // (and a new process group as part of that) seems like a safe bet, and has the
1054         // advantage of letting us signal the process as part of a process group.
1055         setsid();
1056     }
1057     else {
1058         // "run on console" - run as a foreground job on the terminal/console device
1059         
1060         // if do_set_ctty is false, we are the session leader; we are probably running
1061         // as a user process. Don't create a new session leader in that case, and run
1062         // as part of the parent session. Otherwise, the new session cannot claim the
1063         // terminal as a controlling terminal (it is already claimed), meaning that it
1064         // will not see control signals from ^C etc.
1065         
1066         if (do_set_ctty) {
1067             // Disable suspend (^Z) (and on some systems, delayed suspend / ^Y)
1068             signal(SIGTSTP, SIG_IGN);
1069             
1070             // Become session leader
1071             setsid();
1072             ioctl(0, TIOCSCTTY, 0);
1073         }
1074         setpgid(0,0);
1075         tcsetpgrp(0, getpgrp());
1076     }
1077     
1078     sigprocmask(SIG_SETMASK, &sigwait_set, nullptr);
1079     
1080     execvp(args[0], const_cast<char **>(args));
1081     
1082     // If we got here, the exec failed:
1083     failure_out:
1084     int exec_status = errno;
1085     write(wpipefd, &exec_status, sizeof(int));
1086     _exit(0);
1087 }
1088
1089 // Mark this and all dependent services as force-stopped.
1090 void service_record::forceStop() noexcept
1091 {
1092     if (service_state != service_state_t::STOPPED) {
1093         force_stop = true;
1094         services->addToStopQueue(this);
1095     }
1096 }
1097
1098 void service_record::dependent_stopped() noexcept
1099 {
1100     if (service_state == service_state_t::STOPPING && waiting_for_deps) {
1101         services->addToStopQueue(this);
1102     }
1103 }
1104
1105 void service_record::stop(bool bring_down) noexcept
1106 {
1107     if (start_explicit) {
1108         start_explicit = false;
1109         release();
1110     }
1111
1112     if (bring_down) {
1113         do_stop();
1114     }
1115 }
1116
1117 void service_record::do_stop() noexcept
1118 {
1119     if (pinned_started) return;
1120
1121     if (start_explicit && ! do_auto_restart()) {
1122         start_explicit = false;
1123         release();
1124         if (required_by == 0) return; // release will re-call us anyway
1125     }
1126
1127     if (service_state != service_state_t::STARTED) {
1128         if (service_state == service_state_t::STARTING) {
1129             if (! can_interrupt_start()) {
1130                 // Well this is awkward: we're going to have to continue
1131                 // starting, but we don't want any dependents to think that
1132                 // they are still waiting to start.
1133                 // Make sure they remain stopped:
1134                 stop_dependents();
1135                 return;
1136             }
1137
1138             // We must have had desired_state == STARTED.
1139             notify_listeners(service_event::STARTCANCELLED);
1140             
1141             interrupt_start();
1142
1143             // Reaching this point, we are starting interruptibly - so we
1144             // stop now (by falling through to below).
1145         }
1146         else {
1147             // If we're starting we need to wait for that to complete.
1148             // If we're already stopping/stopped there's nothing to do.
1149             return;
1150         }
1151     }
1152
1153     service_state = service_state_t::STOPPING;
1154     waiting_for_deps = true;
1155     if (stop_dependents()) {
1156         services->addToStopQueue(this);
1157     }
1158 }
1159
1160 bool service_record::stop_check_dependents() noexcept
1161 {
1162     bool all_deps_stopped = true;
1163     for (sr_iter i = dependents.begin(); i != dependents.end(); ++i) {
1164         if (! (*i)->is_stopped()) {
1165             all_deps_stopped = false;
1166             break;
1167         }
1168     }
1169     
1170     return all_deps_stopped;
1171 }
1172
1173 bool service_record::stop_dependents() noexcept
1174 {
1175     bool all_deps_stopped = true;
1176     for (sr_iter i = dependents.begin(); i != dependents.end(); ++i) {
1177         if (! (*i)->is_stopped()) {
1178             // Note we check *first* since if the dependent service is not stopped,
1179             // 1. We will issue a stop to it shortly and
1180             // 2. It will notify us when stopped, at which point the stop_check_dependents()
1181             //    check is run anyway.
1182             all_deps_stopped = false;
1183         }
1184
1185         if (force_stop) {
1186             // If this service is to be forcefully stopped, dependents must also be.
1187             (*i)->forceStop();
1188         }
1189
1190         (*i)->prop_stop = true;
1191         services->addToPropQueue(*i);
1192     }
1193
1194     return all_deps_stopped;
1195 }
1196
1197 // All dependents have stopped; we can stop now, too. Only called when STOPPING.
1198 void service_record::all_deps_stopped() noexcept
1199 {
1200     waiting_for_deps = false;
1201     stopped();
1202 }
1203
1204 void base_process_service::all_deps_stopped() noexcept
1205 {
1206     waiting_for_deps = false;
1207     if (pid != -1) {
1208         // The process is still kicking on - must actually kill it. We signal the process
1209         // group (-pid) rather than just the process as there's less risk then of creating
1210         // an orphaned process group:
1211         if (! onstart_flags.no_sigterm) {
1212             kill(-pid, SIGTERM);
1213         }
1214         if (term_signal != -1) {
1215             kill(-pid, term_signal);
1216         }
1217
1218         // In most cases, the rest is done in handle_exit_status.
1219         // If we are a BGPROCESS and the process is not our immediate child, however, that
1220         // won't work - check for this now:
1221         if (record_type == service_type::BGPROCESS && ! tracking_child) {
1222             stopped();
1223         }
1224     }
1225     else {
1226         // The process is already dead.
1227         stopped();
1228     }
1229 }
1230
1231 void scripted_service::all_deps_stopped() noexcept
1232 {
1233     waiting_for_deps = false;
1234     if (stop_command.length() == 0) {
1235         stopped();
1236     }
1237     else if (! start_ps_process(stop_arg_parts, false)) {
1238         // Couldn't execute stop script, but there's not much we can do:
1239         stopped();
1240     }
1241 }
1242
1243 void service_record::unpin() noexcept
1244 {
1245     if (pinned_started) {
1246         pinned_started = false;
1247         if (desired_state == service_state_t::STOPPED) {
1248             do_stop();
1249             services->process_queues();
1250         }
1251     }
1252     if (pinned_stopped) {
1253         pinned_stopped = false;
1254         if (desired_state == service_state_t::STARTED) {
1255             do_start();
1256             services->process_queues();
1257         }
1258     }
1259 }
1260
1261 void service_record::queue_for_console() noexcept
1262 {
1263     services->append_console_queue(this);
1264 }
1265
1266 void service_record::release_console() noexcept
1267 {
1268     services->pull_console_queue();
1269 }
1270
1271 void service_record::interrupt_start() noexcept
1272 {
1273     services->unqueue_console(this);
1274 }
1275
1276 void service_set::service_active(service_record *sr) noexcept
1277 {
1278     active_services++;
1279 }
1280
1281 void service_set::service_inactive(service_record *sr) noexcept
1282 {
1283     active_services--;
1284 }
1285
1286 base_process_service::base_process_service(service_set *sset, string name, service_type service_type_p, string &&command,
1287         std::list<std::pair<unsigned,unsigned>> &command_offsets,
1288         sr_list &&pdepends_on, const sr_list &pdepends_soft)
1289      : service_record(sset, name, service_type_p, std::move(command), command_offsets,
1290          std::move(pdepends_on), pdepends_soft), child_listener(this), child_status_listener(this)
1291 {
1292     restart_interval_count = 0;
1293     restart_interval_time = {0, 0};
1294     restart_timer.service = this;
1295     restart_timer.add_timer(eventLoop);
1296
1297     // By default, allow a maximum of 3 restarts within 10.0 seconds:
1298     restart_interval.tv_sec = 10;
1299     restart_interval.tv_nsec = 0;
1300     max_restart_interval_count = 3;
1301
1302     waiting_restart_timer = false;
1303     reserved_child_watch = false;
1304     tracking_child = false;
1305 }
1306
1307 void base_process_service::do_restart() noexcept
1308 {
1309     restarting = false;
1310     waiting_restart_timer = false;
1311     restart_interval_count++;
1312
1313     // We may be STARTING (regular restart) or STARTED ("smooth recovery"). This affects whether
1314     // the process should be granted access to the console:
1315     bool on_console = service_state == service_state_t::STARTING
1316             ? onstart_flags.starts_on_console : onstart_flags.runs_on_console;
1317
1318     if (! start_ps_process(exec_arg_parts, on_console)) {
1319         if (service_state == service_state_t::STARTING) {
1320             failed_to_start();
1321         }
1322         else {
1323             desired_state = service_state_t::STOPPED;
1324             forceStop();
1325         }
1326         services->process_queues();
1327     }
1328 }
1329
1330 bool base_process_service::restart_ps_process() noexcept
1331 {
1332     using time_val = eventloop_t::time_val;
1333
1334     time_val current_time;
1335     eventLoop.get_time(current_time, clock_type::MONOTONIC);
1336
1337     if (max_restart_interval_count != 0) {
1338         // Check whether we're still in the most recent restart check interval:
1339         time_val int_diff = current_time - restart_interval_time;
1340         if (int_diff < restart_interval) {
1341             if (restart_interval_count >= max_restart_interval_count) {
1342                 log(LogLevel::ERROR, "Service ", service_name, " restarting too quickly; stopping.");
1343                 return false;
1344             }
1345         }
1346         else {
1347             restart_interval_time = current_time;
1348             restart_interval_count = 0;
1349         }
1350     }
1351
1352     // Check if enough time has lapsed since the prevous restart. If not, start a timer:
1353     time_val tdiff = current_time - last_start_time;
1354     if (restart_delay <= tdiff) {
1355         // > restart delay (normally 200ms)
1356         do_restart();
1357     }
1358     else {
1359         time_val timeout = restart_delay - tdiff;
1360         restart_timer.arm_timer_rel(eventLoop, timeout);
1361         waiting_restart_timer = true;
1362     }
1363     return true;
1364 }
1365
1366 void base_process_service::interrupt_start() noexcept
1367 {
1368     // overridden in subclasses
1369     if (waiting_restart_timer) {
1370         restart_timer.stop_timer(eventLoop);
1371         waiting_restart_timer = false;
1372     }
1373     service_record::interrupt_start();
1374 }
1375
1376 dasynq::rearm process_restart_timer::timer_expiry(eventloop_t &, int expiry_count)
1377 {
1378     service->do_restart();
1379     return dasynq::rearm::DISARM;
1380 }