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