Largish re-work of how dependencies are handled. The "start" and
[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
20 // from dinit.cc:
21 void open_control_socket(struct ev_loop *loop) noexcept;
22
23
24 // Find the requested service by name
25 static ServiceRecord * findService(const std::list<ServiceRecord *> & records,
26                                     const char *name) noexcept
27 {
28     using std::list;
29     list<ServiceRecord *>::const_iterator i = records.begin();
30     for ( ; i != records.end(); i++ ) {
31         if (strcmp((*i)->getServiceName(), name) == 0) {
32             return *i;
33         }
34     }
35     return (ServiceRecord *)0;
36 }
37
38 ServiceRecord * ServiceSet::findService(const std::string &name) noexcept
39 {
40     return ::findService(records, name.c_str());
41 }
42
43 void ServiceSet::startService(const char *name)
44 {
45     using namespace std;
46     ServiceRecord *record = loadServiceRecord(name);
47     
48     record->start(false);
49 }
50
51 void ServiceSet::stopService(const std::string & name) noexcept
52 {
53     ServiceRecord *record = findService(name);
54     if (record != nullptr) {
55         record->stop();
56     }
57 }
58
59 // Called when a service has actually stopped.
60 void ServiceRecord::stopped() noexcept
61 {
62     if (service_type != ServiceType::SCRIPTED && service_type != ServiceType::BGPROCESS && onstart_flags.runs_on_console) {
63         tcsetpgrp(0, getpgrp());
64         releaseConsole();
65     }
66
67     logServiceStopped(service_name);
68     service_state = ServiceState::STOPPED;
69     force_stop = false;
70     
71     notifyListeners(ServiceEvent::STOPPED);
72     
73     for (auto dependency : depends_on) {
74         dependency->dependentStopped();
75     }
76     
77     if (desired_state == ServiceState::STARTED) {
78         // Desired state is "started".
79         do_start();
80     }
81     else {
82         if (socket_fd != -1) {
83             close(socket_fd);
84             socket_fd = -1;
85         }
86         
87         release_dependencies();
88     }
89 }
90
91 void ServiceRecord::process_child_callback(struct ev_loop *loop, ev_child *w, int revents) noexcept
92 {
93     ServiceRecord *sr = (ServiceRecord *) w->data;
94
95     sr->pid = -1;
96     sr->exit_status = w->rstatus;
97     ev_child_stop(loop, w);
98     
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.
103     
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.
107         return;
108     }
109     
110     sr->handle_exit_status();
111 }
112
113 void ServiceRecord::handle_exit_status() noexcept
114 {
115     if (exit_status != 0 && service_state != ServiceState::STOPPING) {
116         log(LogLevel::ERROR, "Service ", service_name, " process terminated with exit code ", exit_status);
117     }
118
119     if (doing_recovery) {
120         // (BGPROCESS only)
121         doing_recovery = false;
122         bool need_stop = false;
123         if (exit_status != 0) {
124             need_stop = true;
125         }
126         else {
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()) {
130                     need_stop = true;
131                 }
132             }
133         }
134         
135         if (need_stop) {
136             do_stop();
137         }
138         
139         return;
140     }
141     
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) {
146                 started();
147             }
148             else {
149                 failed_to_start();
150             }
151         }
152         else if (service_state == ServiceState::STOPPING) {
153             // TODO log non-zero rstatus?
154             stopped();
155         }
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
160             //      service process.
161             doing_recovery = (service_type == ServiceType::BGPROCESS);
162             start_ps_process();
163             return;
164         }
165         else {
166             forceStop();
167         }
168     }
169     else {  // SCRIPTED
170         if (service_state == ServiceState::STOPPING) {
171             if (exit_status == 0) {
172                 stopped();
173             }
174             else {
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
178                 // can be stopped:
179                 stopped();
180             }
181         }
182         else { // STARTING
183             if (exit_status == 0) {
184                 started();
185             }
186             else {
187                 // failed to start
188                 log(LogLevel::ERROR, "service ", service_name, " command failed with exit code ", exit_status);
189                 failed_to_start();
190             }
191         }
192     }
193 }
194
195 void ServiceRecord::process_child_status(struct ev_loop *loop, ev_io * stat_io, int revents) noexcept
196 {
197     ServiceRecord *sr = (ServiceRecord *) stat_io->data;
198     sr->waiting_for_execstat = false;
199     
200     int exec_status;
201     int r = read(stat_io->fd, &exec_status, sizeof(int));
202     close(stat_io->fd);
203     ev_io_stop(loop, stat_io);
204     
205     if (r != 0) {
206         // We read an errno code; exec() failed, and the service startup failed.
207         sr->pid = -1;
208         log(LogLevel::ERROR, sr->service_name, ": execution failed: ", strerror(exec_status));
209         if (sr->service_state == ServiceState::STARTING) {
210             sr->failed_to_start();
211         }
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:
215             sr->stopped();
216         }
217     }
218     else {
219         // exec() succeeded.
220         if (sr->service_type == ServiceType::PROCESS) {
221             if (sr->service_state != ServiceState::STARTED) {
222                 sr->started();
223             }
224         }
225         
226         if (sr->pid == -1) {
227             // Somehow the process managed to complete before we even saw the status.
228             sr->handle_exit_status();
229         }
230     }
231 }
232
233 void ServiceRecord::require() noexcept
234 {
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) {
238             (*i)->require();
239         }
240
241         for (auto i = soft_deps.begin(); i != soft_deps.end(); ++i) {
242             ServiceRecord * to = i->getTo();
243             to->require();
244         }
245     }
246 }
247
248 void ServiceRecord::release() noexcept
249 {
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();
255         }
256         else {
257             do_stop();
258         }
259     }
260 }
261
262 void ServiceRecord::release_dependencies() noexcept
263 {
264     for (sr_iter i = depends_on.begin(); i != depends_on.end(); ++i) {
265         (*i)->release();
266     }
267
268     for (auto i = soft_deps.begin(); i != soft_deps.end(); ++i) {
269         ServiceRecord * to = i->getTo();
270         to->release();
271     }
272     
273     service_set->service_inactive(this);
274 }
275
276 void ServiceRecord::start(bool transitive) noexcept
277 {
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);
283     }
284
285     if (!transitive) {
286         if (!start_explicit) require();
287         start_explicit = true;
288     }
289     
290     if (desired_state == ServiceState::STARTED && service_state != ServiceState::STOPPED) return;
291
292     desired_state = ServiceState::STARTED;
293     service_set->service_active(this);
294     do_start();
295 }
296
297 void ServiceRecord::do_start() noexcept
298 {
299     if (pinned_stopped) return;
300     
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()) {
305             return;
306         }
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
309         // STARTING state.
310         notifyListeners(ServiceEvent::STOPCANCELLED);
311     }
312     
313     service_state = ServiceState::STARTING;
314
315     waiting_for_deps = true;
316
317     // Ask dependencies to start, mark them as being waited on.
318     if (! startCheckDependencies(true)) {
319         return;
320     }
321
322     // Actually start this service.
323     allDepsStarted();
324 }
325
326 void ServiceRecord::dependencyStarted() noexcept
327 {
328     if (service_state != ServiceState::STARTING || ! waiting_for_deps) {
329         return;
330     }
331
332     if (startCheckDependencies(false)) {
333         allDepsStarted();
334     }
335 }
336
337 bool ServiceRecord::startCheckDependencies(bool start_deps) noexcept
338 {
339     bool all_deps_started = true;
340
341     for (sr_iter i = depends_on.begin(); i != depends_on.end(); ++i) {
342         if ((*i)->service_state != ServiceState::STARTED) {
343             if (start_deps) {
344                 all_deps_started = false;
345                 (*i)->start(true);
346             }
347             else {
348                 return false;
349             }
350         }
351     }
352
353     for (auto i = soft_deps.begin(); i != soft_deps.end(); ++i) {
354         ServiceRecord * to = i->getTo();
355         if (start_deps) {
356             if (to->service_state != ServiceState::STARTED) {
357                 to->start(true);
358                 i->waiting_on = true;
359                 all_deps_started = false;
360             }
361             else {
362                 i->waiting_on = false;
363             }
364         }
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;
369             }
370             else {
371                 // We are still waiting on this service
372                 return false;
373             }
374         }
375     }
376     
377     return all_deps_started;
378 }
379
380 bool ServiceRecord::open_socket() noexcept
381 {
382     if (socket_path.empty() || socket_fd != -1) {
383         // No socket, or already open
384         return true;
385     }
386     
387     const char * saddrname = socket_path.c_str();
388     uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + socket_path.length() + 1;
389
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");
393         return false;
394     }
395     
396     // Un-link any stale socket. TODO: safety check? should at least confirm the path is a socket.
397     unlink(saddrname);
398
399     name->sun_family = AF_UNIX;
400     strcpy(name->sun_path, saddrname);
401
402     int sockfd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
403     if (sockfd == -1) {
404         log(LogLevel::ERROR, service_name, ": Error creating activation socket: ", strerror(errno));
405         free(name);
406         return false;
407     }
408
409     if (bind(sockfd, (struct sockaddr *) name, sockaddr_size) == -1) {
410         log(LogLevel::ERROR, service_name, ": Error binding activation socket: ", strerror(errno));
411         close(sockfd);
412         free(name);
413         return false;
414     }
415     
416     free(name);
417     
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));
422         close(sockfd);
423         return false;
424     }
425     
426     if (chmod(saddrname, socket_perms) == -1) {
427         log(LogLevel::ERROR, service_name, ": Error setting activation socket permissions: ", strerror(errno));
428         close(sockfd);
429         return false;
430     }
431
432     if (listen(sockfd, 128) == -1) { // 128 "seems reasonable".
433         log(LogLevel::ERROR, ": Error listening on activation socket: ", strerror(errno));
434         close(sockfd);
435         return false;
436     }
437     
438     socket_fd = sockfd;
439     return true;
440 }
441
442 void ServiceRecord::allDepsStarted(bool has_console) noexcept
443 {
444     if (onstart_flags.runs_on_console && ! has_console) {
445         waiting_for_deps = true;
446         queueForConsole();
447         return;
448     }
449     
450     waiting_for_deps = false;
451
452     if (! open_socket()) {
453         failed_to_start();
454     }
455
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) {
460             failed_to_start();
461         }
462     }
463     else {
464         // "internal" service
465         started();
466     }
467 }
468
469 void ServiceRecord::acquiredConsole() noexcept
470 {
471     if (service_state != ServiceState::STARTING) {
472         // We got the console but no longer want it.
473         releaseConsole();
474     }
475     else if (startCheckDependencies(false)) {
476         log_to_console = false;
477         allDepsStarted(true);
478     }
479     else {
480         // We got the console but can't use it yet.
481         releaseConsole();
482     }
483 }
484
485 bool ServiceRecord::read_pid_file() noexcept
486 {
487     const char *pid_file_c = pid_file.c_str();
488     int fd = open(pid_file_c, O_CLOEXEC);
489     if (fd != -1) {
490         char pidbuf[21]; // just enought to hold any 64-bit integer
491         int r = read(fd, pidbuf, 20);
492         if (r > 0) {
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);
499             }
500             else {
501                 log(LogLevel::ERROR, service_name, ": pid read from pidfile (", pid, ") is not valid");
502                 pid = -1;
503                 close(fd);
504                 return false;
505             }
506         }
507         close(fd);
508         return true;
509     }
510     else {
511         log(LogLevel::ERROR, service_name, ": read pid file: ", strerror(errno));
512         return false;
513     }
514 }
515
516 void ServiceRecord::started() noexcept
517 {
518     if (onstart_flags.runs_on_console && (service_type == ServiceType::SCRIPTED || service_type == ServiceType::BGPROCESS)) {
519         tcsetpgrp(0, getpgrp());
520         releaseConsole();
521     }
522     
523     if (service_type == ServiceType::BGPROCESS && pid_file.length() != 0) {
524         if (! read_pid_file()) {
525             failed_to_start();
526             return;
527         }
528     }
529     
530     logServiceStarted(service_name);
531     service_state = ServiceState::STARTED;
532     notifyListeners(ServiceEvent::STARTED);
533
534     if (onstart_flags.rw_ready) {
535         open_control_socket(ev_default_loop(EVFLAG_AUTO));
536     }
537
538     if (force_stop || desired_state == ServiceState::STOPPED) {
539         // We must now stop.
540         do_stop();
541         return;
542     }
543
544     // Notify any dependents whose desired state is STARTED:
545     for (auto i = dependents.begin(); i != dependents.end(); i++) {
546         (*i)->dependencyStarted();
547     }
548     for (auto i = soft_dpts.begin(); i != soft_dpts.end(); i++) {
549         (*i)->getFrom()->dependencyStarted();
550     }
551 }
552
553 void ServiceRecord::failed_to_start(bool depfailed) noexcept
554 {
555     if (!depfailed && onstart_flags.runs_on_console) {
556         tcsetpgrp(0, getpgrp());
557         releaseConsole();
558     }
559     
560     logServiceFailed(service_name);
561     service_state = ServiceState::STOPPED;
562     notifyListeners(ServiceEvent::FAILEDSTART);
563     
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);
568         }
569     }    
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();
574     }
575 }
576
577 bool ServiceRecord::start_ps_process() noexcept
578 {
579     return start_ps_process(exec_arg_parts, onstart_flags.runs_on_console);
580 }
581
582 bool ServiceRecord::start_ps_process(const std::vector<const char *> &cmd, bool on_console) noexcept
583 {
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.
588
589     int pipefd[2];
590     if (pipe2(pipefd, O_CLOEXEC)) {
591         // TODO log error
592         return false;
593     }
594     
595     // Set up the argument array and other data now (before fork), in case memory allocation fails.
596     
597     auto args = cmd.data();
598     
599     const char * logfile = this->logfile.c_str();
600     if (*logfile == 0) {
601         logfile = "/dev/null";
602     }
603
604     // TODO make sure pipefd's are not 0/1/2 (STDIN/OUT/ERR) - if they are, dup them
605     // until they are not.
606
607     pid_t forkpid = fork();
608     if (forkpid == -1) {
609         // TODO log error
610         close(pipefd[0]);
611         close(pipefd[1]);
612         return false;
613     }
614
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);
619
620     if (forkpid == 0) {
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.
624
625         constexpr int bufsz = ((CHAR_BIT * sizeof(pid_t) - 1) / 3 + 2) + 11;
626         // "LISTEN_PID=" - 11 characters
627         char nbuf[bufsz];
628
629         if (socket_fd != -1) {
630             dup2(socket_fd, 3);
631             if (socket_fd != 3) {
632                 close(socket_fd);
633             }
634             
635             if (putenv(const_cast<char *>("LISTEN_FDS=1"))) goto failure_out;
636             
637             snprintf(nbuf, bufsz, "LISTEN_PID=%jd", static_cast<intmax_t>(getpid()));
638             
639             if (putenv(nbuf)) goto failure_out;
640         }
641
642         if (! on_console) {
643             // Re-set stdin, stdout, stderr
644             close(0); close(1); close(2);
645
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);
651               dup2(1, 2);
652             }
653         }
654         else {
655             // "run on console" - run as a foreground job on the terminal/console device
656             if (do_set_ctty) {
657                 setsid();
658                 ioctl(0, TIOCSCTTY, 0);
659             }
660             setpgid(0,0);
661             tcsetpgrp(0, getpgrp());
662             
663             // TODO disable suspend (^Z)? (via tcsetattr)
664             //      (should be done before TIOCSCTTY)
665         }
666
667         execvp(exec_arg_parts[0], const_cast<char **>(args));
668
669         // If we got here, the exec failed:
670         failure_out:
671         int exec_status = errno;
672         write(pipefd[1], &exec_status, sizeof(int));
673         exit(0);
674     }
675     else {
676         // Parent process
677         close(pipefd[1]); // close the 'other end' fd
678
679         pid = forkpid;
680
681         // Listen for status
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);
685
686         // Add a process listener so we can detect when the
687         // service stops
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;
692         return true;
693     }
694 }
695
696 // Mark this and all dependent services as force-stopped.
697 void ServiceRecord::forceStop() noexcept
698 {
699     if (service_state != ServiceState::STOPPED) {
700         force_stop = true;
701         do_stop();
702     }
703 }
704
705 void ServiceRecord::dependentStopped() noexcept
706 {
707     if (service_state == ServiceState::STOPPING && waiting_for_deps) {
708         // Check the other dependents before we stop.
709         if (stopCheckDependents()) {
710             allDepsStopped();
711         }
712     }
713 }
714
715 void ServiceRecord::stop() noexcept
716 {
717     if (desired_state == ServiceState::STOPPED && service_state != ServiceState::STARTED) return;
718     
719     if (start_explicit) {
720         start_explicit = false;
721         release();
722     }
723 }
724
725 void ServiceRecord::do_stop() noexcept
726 {
727     if (pinned_started) return;
728
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:
736                 stopDependents();
737                 return;
738             }
739
740             // We must have had desired_state == STARTED.
741             notifyListeners(ServiceEvent::STARTCANCELLED);
742             
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,.
747         }
748         else {
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.
751             return;
752         }
753     }
754     
755     service_state = ServiceState::STOPPING;
756     waiting_for_deps = true;
757     
758     // If we get here, we are in STARTED state; stop all dependents.
759     if (stopDependents()) {
760         allDepsStopped();
761     }
762 }
763
764 bool ServiceRecord::stopCheckDependents() noexcept
765 {
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;
770             break;
771         }
772     }
773     
774     return all_deps_stopped;
775 }
776
777 bool ServiceRecord::stopDependents() noexcept
778 {
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;
783             if (force_stop) {
784                 (*i)->forceStop();
785             }
786             else {
787                 (*i)->do_stop();
788             }
789         }
790     }
791     
792     return all_deps_stopped;
793 }
794
795 // Dependency stopped or is stopping; we must stop too.
796 void ServiceRecord::allDepsStopped()
797 {
798     waiting_for_deps = false;
799     if (service_type == ServiceType::PROCESS || service_type == ServiceType::BGPROCESS) {
800         if (pid != -1) {
801             // The process is still kicking on - must actually kill it.
802             if (! onstart_flags.no_sigterm) {
803                 kill(pid, SIGTERM);
804             }
805             if (term_signal != -1) {
806                 kill(pid, term_signal);
807             }
808             // Now we wait; the rest is done in process_child_callback
809         }
810         else {
811             // The process is already dead.
812             stopped();
813         }
814     }
815     else if (service_type == ServiceType::SCRIPTED) {
816         // Scripted service.
817         if (stop_command.length() == 0) {
818             stopped();
819         }
820         else if (! start_ps_process(stop_arg_parts, false)) {
821             // Couldn't execute stop script, but there's not much we can do:
822             stopped();
823         }
824     }
825     else {
826         stopped();
827     }
828 }
829
830 void ServiceRecord::pinStart() noexcept
831 {
832     start(false);
833     pinned_started = true;
834 }
835
836 void ServiceRecord::pinStop() noexcept
837 {
838     stop();
839     pinned_stopped = true;
840 }
841
842 void ServiceRecord::unpin() noexcept
843 {
844     if (pinned_started) {
845         pinned_started = false;
846         if (desired_state == ServiceState::STOPPED) {
847             stop();
848         }
849     }
850     if (pinned_stopped) {
851         pinned_stopped = false;
852         if (desired_state == ServiceState::STARTED) {
853             do_start();
854         }
855     }
856 }
857
858 void ServiceRecord::queueForConsole() noexcept
859 {
860     next_for_console = nullptr;
861     auto tail = service_set->consoleQueueTail(this);
862     if (tail == nullptr) {
863         acquiredConsole();
864     }
865     else {
866         tail->next_for_console = this;
867     }
868 }
869
870 void ServiceRecord::releaseConsole() noexcept
871 {
872     log_to_console = true;
873     if (next_for_console != nullptr) {
874         next_for_console->acquiredConsole();
875     }
876     else {
877         service_set->consoleQueueTail(nullptr);
878     }
879 }
880
881 void ServiceSet::service_active(ServiceRecord *sr) noexcept
882 {
883     active_services++;
884 }
885
886 void ServiceSet::service_inactive(ServiceRecord *sr) noexcept
887 {
888     active_services--;
889 }