Minor cleanups.
[oweals/dinit.git] / src / service.h
1 #ifndef SERVICE_H
2 #define SERVICE_H
3
4 #include <string>
5 #include <list>
6 #include <vector>
7 #include <csignal>
8 #include <unordered_set>
9
10 #include "dasynq.h"
11
12 #include "control.h"
13 #include "service-listener.h"
14 #include "service-constants.h"
15
16 /*
17  * This header defines ServiceRecord, a data record maintaining information about a service,
18  * and ServiceSet, a set of interdependent service records. It also defines some associated
19  * types and exceptions.
20  *
21  * Service states
22  * --------------
23  * Services have both a current state and a desired state. The desired state can be
24  * either STARTED or STOPPED. The current state can also be STARTING or STOPPING.
25  * A service can be "pinned" in either the STARTED or STOPPED states to prevent it
26  * from leaving that state until it is unpinned.
27  *
28  * The total state is a combination of the two, current and desired:
29  *      STOPPED/STOPPED  : stopped and will remain stopped
30  *      STOPPED/STARTED  : stopped (pinned), must be unpinned to start
31  *      STARTING/STARTED : starting, but not yet started. Dependencies may also be starting.
32  *      STARTING/STOPPED : as above, but the service will be stopped again as soon as it has
33  *                         completed startup.
34  *      STARTED/STARTED  : running and will continue running.
35  *      STARTED/STOPPED  : started (pinned), must be unpinned to stop
36  *      STOPPING/STOPPED : stopping and will stop. Dependents may be stopping.
37  *      STOPPING/STARTED : as above, but the service will be re-started again once it stops.
38  *
39  * A scripted service is in the STARTING/STOPPING states during the script execution.
40  * A process service is in the STOPPING state when it has been signalled to stop, and is
41  * in the STARTING state when waiting for dependencies to start or for the exec() call in
42  * the forked child to complete and return a status.
43  *
44  * Aquisition/release:
45  * ------------------
46  * Each service has a dependent-count ("required_by"). This starts at 0, adds 1 if the
47  * service has explicitly been started (i.e. "start_explicit" is true), and adds 1 for
48  * each dependent service which is not STOPPED (including dependents with a soft dependency).
49  * When required_by transitions to 0, the service is stopped (unless it is pinned). When
50  * require_by transitions from 0, the service is started (unless pinned).
51  *
52  * So, in general, the dependent-count determines the desired state (STARTED if the count
53  * is greater than 0, otherwise STOPPED). However, a service can be issued a stop-and-take
54  * down order (via `stop(true)'); this will first stop dependent services, which may restart
55  * and cancel the stop of the former service. Finally, a service can be force-stopped, which
56  * means that its stop process cannot be cancelled (though it may still be put in a desired
57  * state of STARTED, meaning it will start immediately upon stopping).
58  *
59  * Pinning
60  * -------
61  * A service may be "pinned" in either STARTED or STOPPED states (or even both). Once it
62  * reaches a pinned state, a service will not leave that state, though its desired state
63  * may still be set. (Note that pinning prevents, but never causes, state transition).
64  *
65  * The priority of the different state deciders is:
66  *  - pins
67  *  - force stop flag
68  *  - desired state (which is manipulated by require/release operations)
69  *
70  * So a forced stop cannot occur until the service is not pinned started, for instance.
71  *
72  * Two-phase transition
73  * --------------------
74  * Transition between states occurs in two phases: propagation and execution. In the
75  * propagation phase, acquisition/release messages are processed, and desired state may be
76  * altered accordingly. Desired state of dependencies/dependents should not be examined in
77  * this phase, since it may change during the phase (i.e. its current value at any point
78  * may not reflect the true final value).
79  *
80  * In the execution phase, actions are taken to achieve the desired state. Actual state may
81  * transition according to the current and desired states.
82  */
83
84 struct OnstartFlags {
85     bool rw_ready : 1;
86     bool log_ready : 1;
87     
88     // Not actually "onstart" commands:
89     bool no_sigterm : 1;  // do not send SIGTERM
90     bool runs_on_console : 1;  // run "in the foreground"
91     bool pass_cs_fd : 1;  // pass this service a control socket connection via fd
92     
93     OnstartFlags() noexcept : rw_ready(false), log_ready(false),
94             no_sigterm(false), runs_on_console(false), pass_cs_fd(false)
95     {
96     }
97 };
98
99 // Exception while loading a service
100 class ServiceLoadExc
101 {
102     public:
103     std::string serviceName;
104     const char *excDescription;
105     
106     protected:
107     ServiceLoadExc(std::string serviceName, const char *desc) noexcept
108         : serviceName(serviceName), excDescription(desc)
109     {
110     }
111 };
112
113 class ServiceNotFound : public ServiceLoadExc
114 {
115     public:
116     ServiceNotFound(std::string serviceName) noexcept
117         : ServiceLoadExc(serviceName, "Service description not found.")
118     {
119     }
120 };
121
122 class ServiceCyclicDependency : public ServiceLoadExc
123 {
124     public:
125     ServiceCyclicDependency(std::string serviceName) noexcept
126         : ServiceLoadExc(serviceName, "Has cyclic dependency.")
127     {
128     }
129 };
130
131 class ServiceDescriptionExc : public ServiceLoadExc
132 {
133     public:
134     ServiceDescriptionExc(std::string serviceName, std::string extraInfo) noexcept
135         : ServiceLoadExc(serviceName, extraInfo.c_str())
136     {
137     }    
138 };
139
140 class ServiceRecord; // forward declaration
141 class ServiceSet; // forward declaration
142
143 /* Service dependency record */
144 class ServiceDep
145 {
146     ServiceRecord * from;
147     ServiceRecord * to;
148
149     public:
150     /* Whether the 'from' service is waiting for the 'to' service to start */
151     bool waiting_on;
152     /* Whether the 'from' service is holding an acquire on the 'to' service */
153     bool holding_acq;
154
155     ServiceDep(ServiceRecord * from, ServiceRecord * to) noexcept : from(from), to(to), waiting_on(false), holding_acq(false)
156     {  }
157
158     ServiceRecord * getFrom() noexcept
159     {
160         return from;
161     }
162
163     ServiceRecord * getTo() noexcept
164     {
165         return to;
166     }
167 };
168
169 // Given a string and a list of pairs of (start,end) indices for each argument in that string,
170 // store a null terminator for the argument. Return a `char *` vector containing the beginning
171 // of each argument and a trailing nullptr. (The returned array is invalidated if the string is later modified).
172 static std::vector<const char *> separate_args(std::string &s, std::list<std::pair<unsigned,unsigned>> &arg_indices)
173 {
174     std::vector<const char *> r;
175     r.reserve(arg_indices.size() + 1);
176
177     // First store nul terminator for each part:
178     for (auto index_pair : arg_indices) {
179         if (index_pair.second < s.length()) {
180             s[index_pair.second] = 0;
181         }
182     }
183
184     // Now we can get the C string (c_str) and store offsets into it:
185     const char * cstr = s.c_str();
186     for (auto index_pair : arg_indices) {
187         r.push_back(cstr + index_pair.first);
188     }
189     r.push_back(nullptr);
190     return r;
191 }
192
193 class ServiceChildWatcher : public EventLoop_t::child_proc_watcher_impl<ServiceChildWatcher>
194 {
195     public:
196     ServiceRecord * service;
197     rearm child_status(EventLoop_t &eloop, pid_t child, int status) noexcept;
198     
199     ServiceChildWatcher(ServiceRecord * sr) noexcept : service(sr) { }
200 };
201
202 class ServiceIoWatcher : public EventLoop_t::fd_watcher_impl<ServiceIoWatcher>
203 {
204     public:
205     ServiceRecord * service;
206     rearm fd_event(EventLoop_t &eloop, int fd, int flags) noexcept;
207     
208     ServiceIoWatcher(ServiceRecord * sr) noexcept : service(sr) { }
209 };
210
211 class ServiceRecord
212 {
213     friend class ServiceChildWatcher;
214     friend class ServiceIoWatcher;
215     
216     typedef std::string string;
217     
218     string service_name;
219     ServiceType service_type;  /* ServiceType::DUMMY, PROCESS, SCRIPTED, INTERNAL */
220     ServiceState service_state = ServiceState::STOPPED; /* ServiceState::STOPPED, STARTING, STARTED, STOPPING */
221     ServiceState desired_state = ServiceState::STOPPED; /* ServiceState::STOPPED / STARTED */
222
223     string program_name;          // storage for program/script and arguments
224     std::vector<const char *> exec_arg_parts; // pointer to each argument/part of the program_name, and nullptr
225     
226     string stop_command;          // storage for stop program/script and arguments
227     std::vector<const char *> stop_arg_parts; // pointer to each argument/part of the stop_command, and nullptr
228     
229     string pid_file;
230     
231     OnstartFlags onstart_flags;
232
233     string logfile;           // log file name, empty string specifies /dev/null
234     
235     bool auto_restart : 1;    // whether to restart this (process) if it dies unexpectedly
236     bool smooth_recovery : 1; // whether the service process can restart without bringing down service
237     
238     bool pinned_stopped : 1;
239     bool pinned_started : 1;
240     bool waiting_for_deps : 1;  // if STARTING, whether we are waiting for dependencies (inc console) to start
241     bool waiting_for_execstat : 1;  // if we are waiting for exec status after fork()
242     bool doing_recovery : 1;    // if we are currently recovering a BGPROCESS (restarting process, while
243                                 //   holding STARTED service state)
244     bool start_explicit : 1;    // whether we are are explictly required to be started
245
246     bool prop_require : 1;      // require must be propagated
247     bool prop_release : 1;      // release must be propagated
248     bool prop_failure : 1;      // failure to start must be propagated
249     
250     int required_by = 0;        // number of dependents wanting this service to be started
251
252     typedef std::list<ServiceRecord *> sr_list;
253     typedef sr_list::iterator sr_iter;
254     
255     // list of soft dependencies
256     typedef std::list<ServiceDep> softdep_list;
257     
258     // list of soft dependents
259     typedef std::list<ServiceDep *> softdpt_list;
260     
261     sr_list depends_on; // services this one depends on
262     sr_list dependents; // services depending on this one
263     softdep_list soft_deps;  // services this one depends on via a soft dependency
264     softdpt_list soft_dpts;  // services depending on this one via a soft dependency
265     
266     // unsigned wait_count;  /* if we are waiting for dependents/dependencies to
267     //                         start/stop, this is how many we're waiting for */
268     
269     ServiceSet *service_set; // the set this service belongs to
270     
271     std::unordered_set<ServiceListener *> listeners;
272     
273     // Process services:
274     bool force_stop; // true if the service must actually stop. This is the
275                      // case if for example the process dies; the service,
276                      // and all its dependencies, MUST be stopped.
277     
278     int term_signal = -1;  // signal to use for process termination
279     
280     string socket_path; // path to the socket for socket-activation service
281     int socket_perms;   // socket permissions ("mode")
282     uid_t socket_uid = -1;  // socket user id or -1
283     gid_t socket_gid = -1;  // sockget group id or -1
284
285     // Implementation details
286     
287     pid_t pid = -1;  // PID of the process. If state is STARTING or STOPPING,
288                      //   this is PID of the service script; otherwise it is the
289                      //   PID of the process itself (process service).
290     int exit_status; // Exit status, if the process has exited (pid == -1).
291     int socket_fd = -1;  // For socket-activation services, this is the file
292                          // descriptor for the socket.
293     
294     ServiceChildWatcher child_listener;
295     ServiceIoWatcher child_status_listener;
296     
297     // Data for use by ServiceSet
298     public:
299     
300     // Next service (after this one) in the queue for the console. Intended to only be used by ServiceSet class.
301     ServiceRecord *next_for_console;
302     
303     // Propagation and start/stop queues
304     ServiceRecord *next_in_prop_queue = nullptr;
305     ServiceRecord *next_in_stop_queue = nullptr;
306     
307     
308     private:
309     
310     // All dependents have stopped.
311     void allDepsStopped();
312     
313     // Service has actually stopped (includes having all dependents
314     // reaching STOPPED state).
315     void stopped() noexcept;
316     
317     // Service has successfully started
318     void started() noexcept;
319     
320     // Service failed to start (only called when in STARTING state).
321     //   dep_failed: whether failure is recorded due to a dependency failing
322     void failed_to_start(bool dep_failed = false) noexcept;
323
324     // For process services, start the process, return true on success
325     bool start_ps_process() noexcept;
326     bool start_ps_process(const std::vector<const char *> &args, bool on_console) noexcept;
327     
328     void run_child_proc(const char * const *args, const char *logfile, bool on_console, int wpipefd,
329             int csfd) noexcept;
330     
331     // Callback from libev when a child process dies
332     static void process_child_callback(EventLoop_t *loop, ServiceChildWatcher *w,
333             int revents) noexcept;
334     
335     void handle_exit_status() noexcept;
336
337     // A dependency has reached STARTED state
338     void dependencyStarted() noexcept;
339     
340     void allDepsStarted(bool haveConsole = false) noexcept;
341     
342     // Read the pid-file, return false on failure
343     bool read_pid_file() noexcept;
344     
345     // Open the activation socket, return false on failure
346     bool open_socket() noexcept;
347
348     // Check whether dependencies have started, and optionally ask them to start
349     bool startCheckDependencies(bool do_start) noexcept;
350
351     // Whether a STARTING service can immediately transition to STOPPED (as opposed to
352     // having to wait for it reach STARTED and then go through STOPPING).
353     bool can_interrupt_start() noexcept
354     {
355         return waiting_for_deps;
356     }
357     
358     // Whether a STOPPING service can immediately transition to STARTED.
359     bool can_interrupt_stop() noexcept
360     {
361         return waiting_for_deps && ! force_stop;
362     }
363
364     // A dependent has reached STOPPED state
365     void dependentStopped() noexcept;
366
367     // check if all dependents have stopped
368     bool stopCheckDependents() noexcept;
369     
370     // issue a stop to all dependents, return true if they are all already stopped
371     bool stopDependents() noexcept;
372     
373     void require() noexcept;
374     void release() noexcept;
375     void release_dependencies() noexcept;
376     
377     // Check if service is, fundamentally, stopped.
378     bool is_stopped() noexcept
379     {
380         return service_state == ServiceState::STOPPED
381             || (service_state == ServiceState::STARTING && waiting_for_deps);
382     }
383     
384     void notifyListeners(ServiceEvent event) noexcept
385     {
386         for (auto l : listeners) {
387             l->serviceEvent(this, event);
388         }
389     }
390     
391     // Queue to run on the console. 'acquiredConsole()' will be called when the console is available.
392     void queueForConsole() noexcept;
393     
394     // Release console (console must be currently held by this service)
395     void releaseConsole() noexcept;
396     
397     bool do_auto_restart() noexcept;
398     
399     public:
400
401     ServiceRecord(ServiceSet *set, string name)
402         : service_state(ServiceState::STOPPED), desired_state(ServiceState::STOPPED), auto_restart(false),
403             pinned_stopped(false), pinned_started(false), waiting_for_deps(false),
404             waiting_for_execstat(false), doing_recovery(false),
405             start_explicit(false), prop_require(false), prop_release(false), prop_failure(false),
406             force_stop(false), child_listener(this), child_status_listener(this)
407     {
408         service_set = set;
409         service_name = name;
410         service_type = ServiceType::DUMMY;
411     }
412     
413     ServiceRecord(ServiceSet *set, string name, ServiceType service_type, string &&command, std::list<std::pair<unsigned,unsigned>> &command_offsets,
414             sr_list * pdepends_on, sr_list * pdepends_soft)
415         : ServiceRecord(set, name)
416     {
417         service_set = set;
418         service_name = name;
419         this->service_type = service_type;
420         this->depends_on = std::move(*pdepends_on);
421
422         program_name = command;
423         exec_arg_parts = separate_args(program_name, command_offsets);
424
425         for (sr_iter i = depends_on.begin(); i != depends_on.end(); ++i) {
426             (*i)->dependents.push_back(this);
427         }
428
429         // Soft dependencies
430         auto b_iter = soft_deps.end();
431         for (sr_iter i = pdepends_soft->begin(); i != pdepends_soft->end(); ++i) {
432             b_iter = soft_deps.emplace(b_iter, this, *i);
433             (*i)->soft_dpts.push_back(&(*b_iter));
434             ++b_iter;
435         }
436     }
437     
438     // TODO write a destructor
439     
440     // begin transition from stopped to started state or vice versa depending on current and desired state
441     void execute_transition() noexcept;
442     
443     void do_propagation() noexcept;
444     
445     // Called on transition of desired state from stopped to started (or unpinned stop)
446     void do_start() noexcept;
447
448     // Called on transition of desired state from started to stopped (or unpinned start)
449     void do_stop() noexcept;
450     
451     // Console is available.
452     void acquiredConsole() noexcept;
453     
454     // Set the stop command and arguments (may throw std::bad_alloc)
455     void setStopCommand(std::string command, std::list<std::pair<unsigned,unsigned>> &stop_command_offsets)
456     {
457         stop_command = command;
458         stop_arg_parts = separate_args(stop_command, stop_command_offsets);
459     }
460     
461     // Get the current service state.
462     ServiceState getState() noexcept
463     {
464         return service_state;
465     }
466     
467     // Get the target (aka desired) state.
468     ServiceState getTargetState() noexcept
469     {
470         return desired_state;
471     }
472
473     // Set logfile, should be done before service is started
474     void setLogfile(string logfile)
475     {
476         this->logfile = logfile;
477     }
478     
479     // Set whether this service should automatically restart when it dies
480     void setAutoRestart(bool auto_restart) noexcept
481     {
482         this->auto_restart = auto_restart;
483     }
484     
485     void setSmoothRecovery(bool smooth_recovery) noexcept
486     {
487         this->smooth_recovery = smooth_recovery;
488     }
489     
490     // Set "on start" flags (commands)
491     void setOnstartFlags(OnstartFlags flags) noexcept
492     {
493         this->onstart_flags = flags;
494     }
495     
496     // Set an additional signal (other than SIGTERM) to be used to terminate the process
497     void setExtraTerminationSignal(int signo) noexcept
498     {
499         this->term_signal = signo;
500     }
501     
502     void set_pid_file(string &&pid_file) noexcept
503     {
504         this->pid_file = pid_file;
505     }
506     
507     void set_socket_details(string &&socket_path, int socket_perms, uid_t socket_uid, uid_t socket_gid) noexcept
508     {
509         this->socket_path = socket_path;
510         this->socket_perms = socket_perms;
511         this->socket_uid = socket_uid;
512         this->socket_gid = socket_gid;
513     }
514
515     const std::string &getServiceName() const noexcept { return service_name; }
516     ServiceState getState() const noexcept { return service_state; }
517     
518     void start(bool activate = true) noexcept;  // start the service
519     void stop(bool bring_down = true) noexcept;   // stop the service
520     
521     void forceStop() noexcept; // force-stop this service and all dependents
522     
523     // Pin the service in "started" state (when it reaches the state)
524     void pinStart() noexcept
525     {
526         pinned_started = true;
527     }
528     
529     // Pin the service in "stopped" state (when it reaches the state)
530     void pinStop() noexcept
531     {
532         pinned_stopped = true;
533     }
534     
535     // Remove both "started" and "stopped" pins. If the service is currently pinned
536     // in either state but would naturally be in the opposite state, it will immediately
537     // commence starting/stopping.
538     void unpin() noexcept;
539     
540     bool isDummy() noexcept
541     {
542         return service_type == ServiceType::DUMMY;
543     }
544     
545     // Add a listener. A listener must only be added once. May throw std::bad_alloc.
546     void addListener(ServiceListener * listener)
547     {
548         listeners.insert(listener);
549     }
550     
551     // Remove a listener.    
552     void removeListener(ServiceListener * listener) noexcept
553     {
554         listeners.erase(listener);
555     }
556 };
557
558 /*
559  * A ServiceSet, as the name suggests, manages a set of services.
560  *
561  * Other than the ability to find services by name, the service set manages various queues.
562  * One is the queue for processes wishing to acquire the console. There is also a set of
563  * processes that want to start, and another set of those that want to stop. These latter
564  * two "queues" (not really queues since their order is not important) are used to prevent too
565  * much recursion and to prevent service states from "bouncing" too rapidly.
566  * 
567  * A service that wishes to start or stop puts itself on the start/stop queue; a service that
568  * needs to propagate changes to dependent services or dependencies puts itself on the
569  * propagation queue. Any operation that potentially manipulates the queues must be followed
570  * by a "process queues" order (processQueues() method).
571  *
572  * Note that processQueues always repeatedly processes both queues until they are empty. The
573  * process is finite because starting a service can never cause services to stop, unless they
574  * fail to start, which should cause them to stop semi-permanently.
575  */
576 class ServiceSet
577 {
578     int active_services;
579     std::list<ServiceRecord *> records;
580     const char *service_dir;  // directory containing service descriptions
581     bool restart_enabled; // whether automatic restart is enabled (allowed)
582     
583     ShutdownType shutdown_type = ShutdownType::CONTINUE;  // Shutdown type, if stopping
584     
585     ServiceRecord * console_queue_head = nullptr; // first record in console queue
586     ServiceRecord * console_queue_tail = nullptr; // last record in console queue
587
588     // Propagation and start/stop "queues" - list of services waiting for processing
589     ServiceRecord * first_prop_queue = nullptr;
590     ServiceRecord * first_stop_queue = nullptr;
591     
592     // Private methods
593         
594     // Load a service description, and dependencies, if there is no existing
595     // record for the given name.
596     // Throws:
597     //   ServiceLoadException (or subclass) on problem with service description
598     //   std::bad_alloc on out-of-memory condition
599     ServiceRecord *loadServiceRecord(const char *name);
600
601     // Public
602     
603     public:
604     ServiceSet(const char *service_dir)
605     {
606         this->service_dir = service_dir;
607         active_services = 0;
608         restart_enabled = true;
609     }
610     
611     // Start the service with the given name. The named service will begin
612     // transition to the 'started' state.
613     //
614     // Throws a ServiceLoadException (or subclass) if the service description
615     // cannot be loaded or is invalid;
616     // Throws std::bad_alloc if out of memory.
617     void startService(const char *name);
618     
619     // Locate an existing service record.
620     ServiceRecord *find_service(const std::string &name) noexcept;
621     
622     // Find a loaded service record, or load it if it is not loaded.
623     // Throws:
624     //   ServiceLoadException (or subclass) on problem with service description
625     //   std::bad_alloc on out-of-memory condition 
626     ServiceRecord *loadService(const std::string &name)
627     {
628         ServiceRecord *record = find_service(name);
629         if (record == nullptr) {
630             record = loadServiceRecord(name.c_str());
631         }
632         return record;
633     }
634     
635     // Get the list of all loaded services.
636     const std::list<ServiceRecord *> &listServices()
637     {
638         return records;
639     }
640     
641     // Stop the service with the given name. The named service will begin
642     // transition to the 'stopped' state.
643     void stopService(const std::string &name) noexcept;
644     
645     // Add a service record to the state propogation queue
646     void addToPropQueue(ServiceRecord *service) noexcept
647     {
648         if (service->next_in_prop_queue == nullptr && first_prop_queue != service) {
649             service->next_in_prop_queue = first_prop_queue;
650             first_prop_queue = service;
651         }
652     }
653     
654     // Add a service record to the start queue; called by service record
655     void addToStartQueue(ServiceRecord *service) noexcept
656     {
657         // The start/stop queue is actually one queue:
658         addToStopQueue(service);
659     }
660     
661     // Add a service to the stop queue; called by service record
662     void addToStopQueue(ServiceRecord *service) noexcept
663     {
664         if (service->next_in_stop_queue == nullptr && first_stop_queue != service) {
665             service->next_in_stop_queue = first_stop_queue;
666             first_stop_queue = service;
667         }
668     }
669     
670     // Process state propagation and start/stop queues, until they are empty.
671     // TODO remove the pointless parameter
672     void processQueues(bool ignoredparam = false) noexcept
673     {
674         while (first_stop_queue != nullptr || first_prop_queue != nullptr) {
675             while (first_prop_queue != nullptr) {
676                 auto next = first_prop_queue;
677                 first_prop_queue = next->next_in_prop_queue;
678                 next->next_in_prop_queue = nullptr;
679                 next->do_propagation();
680             }
681             while (first_stop_queue != nullptr) {
682                 auto next = first_stop_queue;
683                 first_stop_queue = next->next_in_stop_queue;
684                 next->next_in_stop_queue = nullptr;
685                 next->execute_transition();
686             }
687         }
688     }
689     
690     // Set the console queue tail (returns previous tail)
691     ServiceRecord * append_console_queue(ServiceRecord * newTail) noexcept
692     {
693         auto prev_tail = console_queue_tail;
694         console_queue_tail = newTail;
695         newTail->next_for_console = nullptr;
696         if (! prev_tail) {
697             console_queue_head = newTail;
698             enable_console_log(false);
699         }
700         else {
701             prev_tail->next_for_console = newTail;
702         }
703         return prev_tail;
704     }
705     
706     // Retrieve the current console queue head and remove it from the queue
707     ServiceRecord * pullConsoleQueue() noexcept
708     {
709         auto prev_head = console_queue_head;
710         if (prev_head) {
711             prev_head->acquiredConsole();
712             console_queue_head = prev_head->next_for_console;
713             if (! console_queue_head) {
714                 console_queue_tail = nullptr;
715             }
716         }
717         else {
718             enable_console_log(true);
719         }
720         return prev_head;
721     }
722     
723     // Notification from service that it is active (state != STOPPED)
724     // Only to be called on the transition from inactive to active.
725     void service_active(ServiceRecord *) noexcept;
726     
727     // Notification from service that it is inactive (STOPPED)
728     // Only to be called on the transition from active to inactive.
729     void service_inactive(ServiceRecord *) noexcept;
730     
731     // Find out how many services are active (starting, running or stopping,
732     // but not stopped).
733     int count_active_services() noexcept
734     {
735         return active_services;
736     }
737     
738     void stop_all_services(ShutdownType type = ShutdownType::HALT) noexcept
739     {
740         restart_enabled = false;
741         shutdown_type = type;
742         for (std::list<ServiceRecord *>::iterator i = records.begin(); i != records.end(); ++i) {
743             (*i)->stop(false);
744             (*i)->unpin();
745         }
746         processQueues(false);
747     }
748     
749     void set_auto_restart(bool restart) noexcept
750     {
751         restart_enabled = restart;
752     }
753     
754     bool get_auto_restart() noexcept
755     {
756         return restart_enabled;
757     }
758     
759     ShutdownType getShutdownType() noexcept
760     {
761         return shutdown_type;
762     }
763 };
764
765 #endif