Minor cleanup: remove unnecessary aliases.
[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 #include <algorithm>
10
11 #include "dasynq.h"
12
13 #include "control.h"
14 #include "service-listener.h"
15 #include "service-constants.h"
16 #include "dinit-ll.h"
17
18 /*
19  * This header defines service_record, a data record maintaining information about a service,
20  * and service_set, a set of interdependent service records. It also defines some associated
21  * types and exceptions.
22  *
23  * Service states
24  * --------------
25  * Services have both a current state and a desired state. The desired state can be
26  * either STARTED or STOPPED. The current state can also be STARTING or STOPPING.
27  * A service can be "pinned" in either the STARTED or STOPPED states to prevent it
28  * from leaving that state until it is unpinned.
29  *
30  * The total state is a combination of the two, current and desired:
31  *      STOPPED/STOPPED  : stopped and will remain stopped
32  *      STOPPED/STARTED  : stopped (pinned), must be unpinned to start
33  *      STARTING/STARTED : starting, but not yet started. Dependencies may also be starting.
34  *      STARTING/STOPPED : as above, but the service will be stopped again as soon as it has
35  *                         completed startup.
36  *      STARTED/STARTED  : running and will continue running.
37  *      STARTED/STOPPED  : started (pinned), must be unpinned to stop
38  *      STOPPING/STOPPED : stopping and will stop. Dependents may be stopping.
39  *      STOPPING/STARTED : as above, but the service will be re-started again once it stops.
40  *
41  * A scripted service is in the STARTING/STOPPING states during the script execution.
42  * A process service is in the STOPPING state when it has been signalled to stop, and is
43  * in the STARTING state when waiting for dependencies to start or for the exec() call in
44  * the forked child to complete and return a status.
45  *
46  * Acquisition/release:
47  * ------------------
48  * Each service has a dependent-count ("required_by"). This starts at 0, adds 1 if the
49  * service has explicitly been started (i.e. "start_explicit" is true), and adds 1 for
50  * each dependent service which is not STOPPED (including dependents with a soft dependency).
51  * When required_by transitions to 0, the service is stopped (unless it is pinned). When
52  * require_by transitions from 0, the service is started (unless pinned).
53  *
54  * So, in general, the dependent-count determines the desired state (STARTED if the count
55  * is greater than 0, otherwise STOPPED). However, a service can be issued a stop-and-take
56  * down order (via `stop(true)'); this will first stop dependent services, which may restart
57  * and cancel the stop of the former service. Finally, a service can be force-stopped, which
58  * means that its stop process cannot be cancelled (though it may still be put in a desired
59  * state of STARTED, meaning it will start immediately upon stopping).
60  *
61  * Pinning
62  * -------
63  * A service may be "pinned" in either STARTED or STOPPED states (or even both). Once it
64  * reaches a pinned state, a service will not leave that state, though its desired state
65  * may still be set. (Note that pinning prevents, but never causes, state transition).
66  *
67  * The priority of the different state deciders is:
68  *  - pins
69  *  - force stop flag
70  *  - desired state (which is manipulated by require/release operations)
71  *
72  * So a forced stop cannot occur until the service is not pinned started, for instance.
73  *
74  * Two-phase transition
75  * --------------------
76  * Transition between states occurs in two phases: propagation and execution. In both phases
77  * a linked-list queue is used to keep track of which services need processing; this avoids
78  * recursion (which would be of unknown depth and therefore liable to stack overflow).
79  *
80  * In the propagation phase, acquisition/release messages are processed, and desired state may be
81  * altered accordingly. Start and stop requests are also propagated in this phase. The state may
82  * be set to STARTING or STOPPING to reflect the desired state, but will never be set to STARTED
83  * or STOPPED (that happens in the execution phase).
84  *
85  * Propagation variables:
86  *   prop_acquire:  the service has transitioned to an acquired state and must issue an acquire
87  *                  on its dependencies
88  *   prop_release:  the service has transitioned to a released state and must issue a release on
89  *                  its dependencies.
90  *
91  *   prop_start:    the service should start
92  *   prop_stop:     the service should stop
93  *
94  * Note that "prop_acquire"/"prop_release" form a pair which cannot both be set at the same time
95  * which is enforced via explicit checks. For "prop_start"/"prop_stop" this occurs implicitly.
96  *
97  * In the execution phase, actions are taken to achieve the desired state. Actual state may
98  * transition according to the current and desired states. Processes can be sent signals, etc
99  * in order to stop them. A process can restart if it stops, but it does so by raising prop_start
100  * which needs to be processed in a second transition phase. Seeing as starting never causes
101  * another process to stop, the transition-execute-transition cycle always ends at the 2nd
102  * transition stage, at the latest.
103  */
104
105 struct onstart_flags_t {
106     bool rw_ready : 1;  // file system should be writable once this service starts
107     bool log_ready : 1; // syslog should be available once this service starts
108     
109     // Not actually "onstart" commands:
110     bool no_sigterm : 1;  // do not send SIGTERM
111     bool runs_on_console : 1;  // run "in the foreground"
112     bool starts_on_console : 1; // starts in the foreground
113     bool pass_cs_fd : 1;  // pass this service a control socket connection via fd
114     
115     onstart_flags_t() noexcept : rw_ready(false), log_ready(false),
116             no_sigterm(false), runs_on_console(false), starts_on_console(false), pass_cs_fd(false)
117     {
118     }
119 };
120
121 // Exception while loading a service
122 class service_load_exc
123 {
124     public:
125     std::string serviceName;
126     std::string excDescription;
127     
128     protected:
129     service_load_exc(std::string serviceName, std::string &&desc) noexcept
130         : serviceName(serviceName), excDescription(std::move(desc))
131     {
132     }
133 };
134
135 class service_not_found : public service_load_exc
136 {
137     public:
138     service_not_found(std::string serviceName) noexcept
139         : service_load_exc(serviceName, "Service description not found.")
140     {
141     }
142 };
143
144 class service_cyclic_dependency : public service_load_exc
145 {
146     public:
147     service_cyclic_dependency(std::string serviceName) noexcept
148         : service_load_exc(serviceName, "Has cyclic dependency.")
149     {
150     }
151 };
152
153 class service_description_exc : public service_load_exc
154 {
155     public:
156     service_description_exc(std::string serviceName, std::string &&extraInfo) noexcept
157         : service_load_exc(serviceName, std::move(extraInfo))
158     {
159     }    
160 };
161
162 class service_record;
163 class service_set;
164 class base_process_service;
165
166 enum class dependency_type
167 {
168     REGULAR,
169     SOFT,       // dependency starts in parallel, failure/stop does not affect dependent
170     WAITS_FOR,  // as for SOFT, but dependent waits until dependency starts/fails before starting
171     MILESTONE   // dependency must start successfully, but once started the dependency becomes soft
172 };
173
174 /* Service dependency record */
175 class service_dep
176 {
177     service_record * from;
178     service_record * to;
179
180     public:
181     /* Whether the 'from' service is waiting for the 'to' service to start */
182     bool waiting_on;
183     /* Whether the 'from' service is holding an acquire on the 'to' service */
184     bool holding_acq;
185
186     const dependency_type dep_type;
187
188     service_dep(service_record * from, service_record * to, dependency_type dep_type_p) noexcept
189             : from(from), to(to), waiting_on(false), holding_acq(false), dep_type(dep_type_p)
190     {  }
191
192     service_record * get_from() noexcept
193     {
194         return from;
195     }
196
197     service_record * get_to() noexcept
198     {
199         return to;
200     }
201 };
202
203 /* preliminary service dependency information */
204 class prelim_dep
205 {
206     public:
207     service_record * const to;
208     dependency_type const dep_type;
209
210     prelim_dep(service_record *to_p, dependency_type dep_type_p) : to(to_p), dep_type(dep_type_p)
211     {
212         //
213     }
214 };
215
216 class service_child_watcher : public eventloop_t::child_proc_watcher_impl<service_child_watcher>
217 {
218     public:
219     base_process_service * service;
220     dasynq::rearm status_change(eventloop_t &eloop, pid_t child, int status) noexcept;
221     
222     service_child_watcher(base_process_service * sr) noexcept : service(sr) { }
223 };
224
225 // Watcher for the pipe used to receive exec() failure status errno
226 class exec_status_pipe_watcher : public eventloop_t::fd_watcher_impl<exec_status_pipe_watcher>
227 {
228     public:
229     base_process_service * service;
230     dasynq::rearm fd_event(eventloop_t &eloop, int fd, int flags) noexcept;
231     
232     exec_status_pipe_watcher(base_process_service * sr) noexcept : service(sr) { }
233 };
234
235 // service_record: base class for service record containing static information
236 // and current state of each service.
237 //
238 // This abstract base class defines the dependency behaviour of services. The actions to actually bring a
239 // service up or down are specified by subclasses in the virtual methods (see especially bring_up() and
240 // bring_down()).
241 //
242 class service_record
243 {
244     protected:
245     using string = std::string;
246     using time_val = dasynq::time_val;
247     
248     private:
249     string service_name;
250     service_type_t record_type;  /* ServiceType::DUMMY, PROCESS, SCRIPTED, INTERNAL */
251     service_state_t service_state = service_state_t::STOPPED; /* service_state_t::STOPPED, STARTING, STARTED, STOPPING */
252     service_state_t desired_state = service_state_t::STOPPED; /* service_state_t::STOPPED / STARTED */
253
254     protected:
255     string pid_file;
256     
257     onstart_flags_t onstart_flags;
258
259     string logfile;           // log file name, empty string specifies /dev/null
260     
261     bool auto_restart : 1;    // whether to restart this (process) if it dies unexpectedly
262     bool smooth_recovery : 1; // whether the service process can restart without bringing down service
263     
264     bool pinned_stopped : 1;
265     bool pinned_started : 1;
266     bool waiting_for_deps : 1;  // if STARTING, whether we are waiting for dependencies (inc console) to start
267                                 // if STOPPING, whether we are waiting for dependents to stop
268     bool waiting_for_execstat : 1;  // if we are waiting for exec status after fork()
269     bool start_explicit : 1;    // whether we are are explicitly required to be started
270
271     bool prop_require : 1;      // require must be propagated
272     bool prop_release : 1;      // release must be propagated
273     bool prop_failure : 1;      // failure to start must be propagated
274     bool prop_start   : 1;
275     bool prop_stop    : 1;
276
277     bool restarting   : 1;      // re-starting after unexpected termination
278     
279     int required_by = 0;        // number of dependents wanting this service to be started
280
281     // list of dependencies
282     typedef std::list<service_dep> dep_list;
283     
284     // list of dependents
285     typedef std::list<service_dep *> dpt_list;
286     
287     dep_list depends_on;  // services this one depends on
288     dpt_list dependents;  // services depending on this one
289     
290     service_set *services; // the set this service belongs to
291     
292     std::unordered_set<service_listener *> listeners;
293     
294     // Process services:
295     bool force_stop; // true if the service must actually stop. This is the
296                      // case if for example the process dies; the service,
297                      // and all its dependencies, MUST be stopped.
298     
299     int term_signal = -1;  // signal to use for process termination
300     
301     string socket_path; // path to the socket for socket-activation service
302     int socket_perms;   // socket permissions ("mode")
303     uid_t socket_uid = -1;  // socket user id or -1
304     gid_t socket_gid = -1;  // socket group id or -1
305
306     // Implementation details
307     
308     pid_t pid = -1;  // PID of the process. If state is STARTING or STOPPING,
309                      //   this is PID of the service script; otherwise it is the
310                      //   PID of the process itself (process service).
311     int exit_status; // Exit status, if the process has exited (pid == -1).
312     int socket_fd = -1;  // For socket-activation services, this is the file
313                          // descriptor for the socket.
314     
315     // Data for use by service_set
316     public:
317     
318     // Console queue.
319     lld_node<service_record> console_queue_node;
320     
321     // Propagation and start/stop queues
322     lls_node<service_record> prop_queue_node;
323     lls_node<service_record> stop_queue_node;
324     
325     protected:
326     
327     // stop immediately
328     void emergency_stop() noexcept;
329     
330     // Service has actually stopped (includes having all dependents
331     // reaching STOPPED state).
332     void stopped() noexcept;
333     
334     // Service has successfully started
335     void started() noexcept;
336     
337     // Service failed to start (only called when in STARTING state).
338     //   dep_failed: whether failure is recorded due to a dependency failing
339     void failed_to_start(bool dep_failed = false) noexcept;
340
341     void run_child_proc(const char * const *args, const char *logfile, bool on_console, int wpipefd,
342             int csfd) noexcept;
343     
344     // A dependency has reached STARTED state
345     void dependency_started() noexcept;
346     
347     void all_deps_started(bool haveConsole = false) noexcept;
348
349     // Open the activation socket, return false on failure
350     bool open_socket() noexcept;
351
352     // Start all dependencies, return true if all have started
353     bool start_check_dependencies() noexcept;
354
355     // Check whether all dependencies have started (i.e. whether we can start now)
356     bool check_deps_started() noexcept;
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 dependent_stopped() noexcept;
366
367     // check if all dependents have stopped
368     bool stop_check_dependents() noexcept;
369     
370     // issue a stop to all dependents, return true if they are all already stopped
371     bool stop_dependents() 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 == service_state_t::STOPPED
381             || (service_state == service_state_t::STARTING && waiting_for_deps);
382     }
383     
384     void notify_listeners(service_event_t event) noexcept
385     {
386         for (auto l : listeners) {
387             l->service_event(this, event);
388         }
389     }
390     
391     // Queue to run on the console. 'acquired_console()' will be called when the console is available.
392     // Has no effect if the service has already queued for console.
393     void queue_for_console() noexcept;
394     
395     // Release console (console must be currently held by this service)
396     void release_console() noexcept;
397     
398     bool do_auto_restart() noexcept;
399
400     // Started state reached
401     bool process_started() noexcept;
402
403     // Called on transition of desired state from stopped to started (or unpinned stop)
404     void do_start() noexcept;
405
406     // Called on transition of desired state from started to stopped (or unpinned start)
407     void do_stop() noexcept;
408
409     // Set the service state
410     void set_state(service_state_t new_state) noexcept
411     {
412         service_state = new_state;
413     }
414
415     // Virtual functions, to be implemented by service implementations:
416
417     // Do any post-dependency startup; return false on failure
418     virtual bool bring_up() noexcept;
419
420     // All dependents have stopped, and this service should proceed to stop.
421     virtual void bring_down() noexcept;
422
423     // Whether a STARTING service can immediately transition to STOPPED (as opposed to
424     // having to wait for it reach STARTED and then go through STOPPING).
425     virtual bool can_interrupt_start() noexcept
426     {
427         return waiting_for_deps;
428     }
429
430     // Whether a STARTING service can transition to its STARTED state, once all
431     // dependencies have started.
432     virtual bool can_proceed_to_start() noexcept
433     {
434         return true;
435     }
436
437     // Interrupt startup. Returns true if service start is fully cancelled; returns false if cancel order
438     // issued but service has not yet responded (state will be set to STOPPING).
439     virtual bool interrupt_start() noexcept;
440
441     public:
442
443     service_record(service_set *set, string name)
444         : service_state(service_state_t::STOPPED), desired_state(service_state_t::STOPPED),
445             auto_restart(false), smooth_recovery(false),
446             pinned_stopped(false), pinned_started(false), waiting_for_deps(false),
447             waiting_for_execstat(false), start_explicit(false),
448             prop_require(false), prop_release(false), prop_failure(false),
449             prop_start(false), prop_stop(false), restarting(false), force_stop(false)
450     {
451         services = set;
452         service_name = name;
453         record_type = service_type_t::DUMMY;
454         socket_perms = 0;
455         exit_status = 0;
456     }
457
458     service_record(service_set *set, string name, service_type_t record_type_p,
459             const std::list<prelim_dep> &deplist_p)
460         : service_record(set, name)
461     {
462         services = set;
463         service_name = name;
464         this->record_type = record_type_p;
465
466         for (auto & pdep : deplist_p) {
467             auto b = depends_on.emplace(depends_on.end(), this, pdep.to, pdep.dep_type);
468             pdep.to->dependents.push_back(&(*b));
469         }
470     }
471
472     virtual ~service_record() noexcept
473     {
474     }
475     
476     // Get the type of this service record
477     service_type_t get_type() noexcept
478     {
479         return record_type;
480     }
481
482     // begin transition from stopped to started state or vice versa depending on current and desired state
483     void execute_transition() noexcept;
484     
485     void do_propagation() noexcept;
486
487     // Console is available.
488     void acquired_console() noexcept;
489     
490     // Get the target (aka desired) state.
491     service_state_t get_target_state() noexcept
492     {
493         return desired_state;
494     }
495
496     // Set logfile, should be done before service is started
497     void set_log_file(string logfile)
498     {
499         this->logfile = logfile;
500     }
501     
502     // Set whether this service should automatically restart when it dies
503     void set_auto_restart(bool auto_restart) noexcept
504     {
505         this->auto_restart = auto_restart;
506     }
507     
508     void set_smooth_recovery(bool smooth_recovery) noexcept
509     {
510         this->smooth_recovery = smooth_recovery;
511     }
512     
513     // Set "on start" flags (commands)
514     void set_flags(onstart_flags_t flags) noexcept
515     {
516         this->onstart_flags = flags;
517     }
518     
519     // Set an additional signal (other than SIGTERM) to be used to terminate the process
520     void set_extra_termination_signal(int signo) noexcept
521     {
522         this->term_signal = signo;
523     }
524     
525     void set_pid_file(string &&pid_file) noexcept
526     {
527         this->pid_file = std::move(pid_file);
528     }
529     
530     void set_socket_details(string &&socket_path, int socket_perms, uid_t socket_uid, uid_t socket_gid) noexcept
531     {
532         this->socket_path = std::move(socket_path);
533         this->socket_perms = socket_perms;
534         this->socket_uid = socket_uid;
535         this->socket_gid = socket_gid;
536     }
537
538     const std::string &get_name() const noexcept { return service_name; }
539     service_state_t get_state() const noexcept { return service_state; }
540     
541     void start(bool activate = true) noexcept;  // start the service
542     void stop(bool bring_down = true) noexcept;   // stop the service
543     
544     void forced_stop() noexcept; // force-stop this service and all dependents
545     
546     // Pin the service in "started" state (when it reaches the state)
547     void pin_start() noexcept
548     {
549         pinned_started = true;
550     }
551     
552     // Pin the service in "stopped" state (when it reaches the state)
553     void pin_stop() noexcept
554     {
555         pinned_stopped = true;
556     }
557     
558     // Remove both "started" and "stopped" pins. If the service is currently pinned
559     // in either state but would naturally be in the opposite state, it will immediately
560     // commence starting/stopping.
561     void unpin() noexcept;
562     
563     bool isDummy() noexcept
564     {
565         return record_type == service_type_t::DUMMY;
566     }
567     
568     // Add a listener. A listener must only be added once. May throw std::bad_alloc.
569     void addListener(service_listener * listener)
570     {
571         listeners.insert(listener);
572     }
573     
574     // Remove a listener.    
575     void removeListener(service_listener * listener) noexcept
576     {
577         listeners.erase(listener);
578     }
579 };
580
581 inline auto extract_prop_queue(service_record *sr) -> decltype(sr->prop_queue_node) &
582 {
583     return sr->prop_queue_node;
584 }
585
586 inline auto extract_stop_queue(service_record *sr) -> decltype(sr->stop_queue_node) &
587 {
588     return sr->stop_queue_node;
589 }
590
591 inline auto extract_console_queue(service_record *sr) -> decltype(sr->console_queue_node) &
592 {
593     return sr->console_queue_node;
594 }
595
596 /*
597  * A service_set, as the name suggests, manages a set of services.
598  *
599  * Other than the ability to find services by name, the service set manages various queues.
600  * One is the queue for processes wishing to acquire the console. There is also a set of
601  * processes that want to start, and another set of those that want to stop. These latter
602  * two "queues" (not really queues since their order is not important) are used to prevent too
603  * much recursion and to prevent service states from "bouncing" too rapidly.
604  * 
605  * A service that wishes to start or stop puts itself on the start/stop queue; a service that
606  * needs to propagate changes to dependent services or dependencies puts itself on the
607  * propagation queue. Any operation that potentially manipulates the queues must be followed
608  * by a "process queues" order (processQueues() method).
609  *
610  * Note that processQueues always repeatedly processes both queues until they are empty. The
611  * process is finite because starting a service can never cause services to stop, unless they
612  * fail to start, which should cause them to stop semi-permanently.
613  */
614 class service_set
615 {
616     protected:
617     int active_services;
618     std::list<service_record *> records;
619     bool restart_enabled; // whether automatic restart is enabled (allowed)
620     
621     shutdown_type_t shutdown_type = shutdown_type_t::CONTINUE;  // Shutdown type, if stopping
622     
623     // Services waiting for exclusive access to the console
624     dlist<service_record, extract_console_queue> console_queue;
625
626     // Propagation and start/stop "queues" - list of services waiting for processing
627     slist<service_record, extract_prop_queue> prop_queue;
628     slist<service_record, extract_stop_queue> stop_queue;
629     
630     public:
631     service_set()
632     {
633         active_services = 0;
634         restart_enabled = true;
635     }
636     
637     virtual ~service_set()
638     {
639         for (auto * s : records) {
640             delete s;
641         }
642     }
643
644     // Start the specified service. The service will be marked active.
645     void start_service(service_record *svc)
646     {
647         svc->start();
648         process_queues();
649     }
650
651     // Stop the specified service. Its active mark will be cleared.
652     void stop_service(service_record *svc)
653     {
654         svc->stop(true);
655         process_queues();
656     }
657
658     // Locate an existing service record.
659     service_record *find_service(const std::string &name) noexcept;
660
661     // Load a service description, and dependencies, if there is no existing
662     // record for the given name.
663     // Throws:
664     //   ServiceLoadException (or subclass) on problem with service description
665     //   std::bad_alloc on out-of-memory condition
666     virtual service_record *load_service(const char *name)
667     {
668         auto r = find_service(name);
669         if (r == nullptr) {
670             throw service_not_found(name);
671         }
672         return r;
673     }
674
675     // Start the service with the given name. The named service will begin
676     // transition to the 'started' state.
677     //
678     // Throws a ServiceLoadException (or subclass) if the service description
679     // cannot be loaded or is invalid;
680     // Throws std::bad_alloc if out of memory.
681     void start_service(const char *name)
682     {
683         using namespace std;
684         service_record *record = load_service(name);
685         service_set::start_service(record);
686     }
687     
688     void add_service(service_record *svc)
689     {
690         records.push_back(svc);
691     }
692     
693     void remove_service(service_record *svc)
694     {
695         std::remove(records.begin(), records.end(), svc);
696     }
697
698     // Get the list of all loaded services.
699     const std::list<service_record *> &list_services() noexcept
700     {
701         return records;
702     }
703     
704     // Stop the service with the given name. The named service will begin
705     // transition to the 'stopped' state.
706     void stop_service(const std::string &name) noexcept;
707     
708     // Add a service record to the state propagation queue. The service record will have its
709     // do_propagation() method called when the queue is processed.
710     void add_prop_queue(service_record *service) noexcept
711     {
712         if (! prop_queue.is_queued(service)) {
713             prop_queue.insert(service);
714         }
715     }
716     
717     // Add a service record to the stop queue. The service record will have its
718     // execute_transition() method called when the queue is processed.
719     void add_transition_queue(service_record *service) noexcept
720     {
721         if (! stop_queue.is_queued(service)) {
722             stop_queue.insert(service);
723         }
724     }
725     
726     // Process state propagation and start/stop queues, until they are empty.
727     void process_queues() noexcept
728     {
729         while (! stop_queue.is_empty() || ! prop_queue.is_empty()) {
730             while (! prop_queue.is_empty()) {
731                 auto next = prop_queue.pop_front();
732                 next->do_propagation();
733             }
734             while (! stop_queue.is_empty()) {
735                 auto next = stop_queue.pop_front();
736                 next->execute_transition();
737             }
738         }
739     }
740     
741     // Set the console queue tail (returns previous tail)
742     void append_console_queue(service_record * newTail) noexcept
743     {
744         bool was_empty = console_queue.is_empty();
745         console_queue.append(newTail);
746         if (was_empty) {
747             enable_console_log(false);
748         }
749     }
750     
751     // Pull and dispatch a waiter from the console queue
752     void pull_console_queue() noexcept
753     {
754         if (console_queue.is_empty()) {
755             enable_console_log(true);
756         }
757         else {
758             service_record * front = console_queue.pop_front();
759             front->acquired_console();
760         }
761     }
762     
763     void unqueue_console(service_record * service) noexcept
764     {
765         if (console_queue.is_queued(service)) {
766             console_queue.unlink(service);
767         }
768     }
769
770     // Notification from service that it is active (state != STOPPED)
771     // Only to be called on the transition from inactive to active.
772     void service_active(service_record *) noexcept;
773     
774     // Notification from service that it is inactive (STOPPED)
775     // Only to be called on the transition from active to inactive.
776     void service_inactive(service_record *) noexcept;
777     
778     // Find out how many services are active (starting, running or stopping,
779     // but not stopped).
780     int count_active_services() noexcept
781     {
782         return active_services;
783     }
784     
785     void stop_all_services(shutdown_type_t type = shutdown_type_t::HALT) noexcept
786     {
787         restart_enabled = false;
788         shutdown_type = type;
789         for (std::list<service_record *>::iterator i = records.begin(); i != records.end(); ++i) {
790             (*i)->stop(false);
791             (*i)->unpin();
792         }
793         process_queues();
794     }
795     
796     void set_auto_restart(bool restart) noexcept
797     {
798         restart_enabled = restart;
799     }
800     
801     bool get_auto_restart() noexcept
802     {
803         return restart_enabled;
804     }
805     
806     shutdown_type_t getShutdownType() noexcept
807     {
808         return shutdown_type;
809     }
810 };
811
812 class dirload_service_set : public service_set
813 {
814     const char *service_dir;  // directory containing service descriptions
815
816     public:
817     dirload_service_set(const char *service_dir_p) : service_set(), service_dir(service_dir_p)
818     {
819     }
820
821     service_record *load_service(const char *name) override;
822 };
823
824 #endif