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