8 #include <unordered_set>
11 #include "service-listener.h"
12 #include "service-constants.h"
15 * Possible service states
17 * Services have both a current state and a desired state. The desired state can be
18 * either STARTED or STOPPED. The current state can also be STARTING or STOPPING.
19 * A service can be "pinned" in either the STARTED or STOPPED states to prevent it
20 * from leaving that state until it is unpinned.
22 * The total state is a combination of the two, current and desired:
23 * STOPPED/STOPPED : stopped and will remain stopped
24 * STOPPED/STARTED : stopped (pinned), must be unpinned to start
25 * STARTING/STARTED : starting, but not yet started. Dependencies may also be starting.
26 * STARTING/STOPPED : as above, but the service will be stopped again as soon as it has
28 * STARTED/STARTED : running and will continue running.
29 * STARTED/STOPPED : started (pinned), must be unpinned to stop
30 * STOPPING/STOPPED : stopping and will stop. Dependents may be stopping.
31 * STOPPING/STARTED : as above, but the service will be re-started again once it stops.
33 * A scripted service is in the STARTING/STOPPING states during the script execution.
34 * A process service is in the STOPPING state when it has been signalled to stop, and is
35 * in the STARTING state when waiting for dependencies to start or for the exec() call in
36 * the forked child to complete and return a status.
42 // Not actually "onstart" commands:
43 bool no_sigterm : 1; // do not send SIGTERM
44 bool runs_on_console : 1; // run "in the foreground"
46 OnstartFlags() noexcept : rw_ready(false),
47 no_sigterm(false), runs_on_console(false)
52 // Exception while loading a service
56 std::string serviceName;
57 const char *excDescription;
60 ServiceLoadExc(std::string serviceName) noexcept
61 : serviceName(serviceName)
66 class ServiceNotFound : public ServiceLoadExc
69 ServiceNotFound(std::string serviceName) noexcept
70 : ServiceLoadExc(serviceName)
72 excDescription = "Service description not found.";
76 class ServiceCyclicDependency : public ServiceLoadExc
79 ServiceCyclicDependency(std::string serviceName) noexcept
80 : ServiceLoadExc(serviceName)
82 excDescription = "Has cyclic dependency.";
86 class ServiceDescriptionExc : public ServiceLoadExc
89 std::string extraInfo;
91 ServiceDescriptionExc(std::string serviceName, std::string extraInfo) noexcept
92 : ServiceLoadExc(serviceName), extraInfo(extraInfo)
94 excDescription = extraInfo.c_str();
98 class ServiceRecord; // forward declaration
99 class ServiceSet; // forward declaration
101 /* Service dependency record */
104 ServiceRecord * from;
108 /* Whether the 'from' service is waiting for the 'to' service to start */
111 ServiceDep(ServiceRecord * from, ServiceRecord * to) noexcept : from(from), to(to), waiting_on(false)
114 ServiceRecord * getFrom() noexcept
119 ServiceRecord * getTo() noexcept
125 // Given a string and a list of pairs of (start,end) indices for each argument in that string,
126 // store a null terminator for the argument. Return a `char *` vector containing the beginning
127 // of each argument and a trailing nullptr. (The returned array is invalidated if the string is later modified).
128 static std::vector<const char *> separate_args(std::string &s, std::list<std::pair<unsigned,unsigned>> &arg_indices)
130 std::vector<const char *> r;
131 r.reserve(arg_indices.size() + 1);
133 // First store nul terminator for each part:
134 for (auto index_pair : arg_indices) {
135 if (index_pair.second < s.length()) {
136 s[index_pair.second] = 0;
140 // Now we can get the C string (c_str) and store offsets into it:
141 const char * cstr = s.c_str();
142 for (auto index_pair : arg_indices) {
143 r.push_back(cstr + index_pair.first);
145 r.push_back(nullptr);
152 typedef std::string string;
155 ServiceType service_type; /* ServiceType::DUMMY, PROCESS, SCRIPTED, INTERNAL */
156 ServiceState service_state = ServiceState::STOPPED; /* ServiceState::STOPPED, STARTING, STARTED, STOPPING */
157 ServiceState desired_state = ServiceState::STOPPED; /* ServiceState::STOPPED / STARTED */
159 string program_name; /* storage for program/script and arguments */
160 std::vector<const char *> exec_arg_parts; /* pointer to each argument/part of the program_name */
162 string stop_command; /* storage for stop program/script and arguments */
163 std::vector<const char *> stop_arg_parts; /* pointer to each argument/part of the stop_command */
167 OnstartFlags onstart_flags;
169 string logfile; // log file name, empty string specifies /dev/null
170 bool auto_restart : 1; // whether to restart this (process) if it dies unexpectedly
171 bool smooth_recovery : 1; // whether the service process can restart without bringing down service
173 bool pinned_stopped : 1;
174 bool pinned_started : 1;
175 bool waiting_for_deps : 1; // if STARTING, whether we are waiting for dependencies (inc console) to start
176 bool waiting_for_execstat : 1; // if we are waiting for exec status after fork()
177 bool doing_recovery : 1; // if we are currently recovering a BGPROCESS (restarting process, while
178 // holding STARTED service state)
180 typedef std::list<ServiceRecord *> sr_list;
181 typedef sr_list::iterator sr_iter;
183 // list of soft dependencies
184 typedef std::list<ServiceDep> softdep_list;
186 // list of soft dependents
187 typedef std::list<ServiceDep *> softdpt_list;
189 sr_list depends_on; // services this one depends on
190 sr_list dependents; // services depending on this one
191 softdep_list soft_deps; // services this one depends on via a soft dependency
192 softdpt_list soft_dpts; // services depending on this one via a soft dependency
194 // unsigned wait_count; /* if we are waiting for dependents/dependencies to
195 // start/stop, this is how many we're waiting for */
197 ServiceSet *service_set; // the set this service belongs to
199 // Next service (after this one) in the queue for the console:
200 ServiceRecord *next_for_console;
202 std::unordered_set<ServiceListener *> listeners;
205 bool force_stop; // true if the service must actually stop. This is the
206 // case if for example the process dies; the service,
207 // and all its dependencies, MUST be stopped.
209 int term_signal = -1; // signal to use for process termination
211 string socket_path; // path to the socket for socket-activation service
212 int socket_perms; // socket permissions ("mode")
213 uid_t socket_uid = -1; // socket user id or -1
214 gid_t socket_gid = -1; // sockget group id or -1
216 // Implementation details
218 pid_t pid = -1; // PID of the process. If state is STARTING or STOPPING,
219 // this is PID of the service script; otherwise it is the
220 // PID of the process itself (process service).
221 int exit_status; // Exit status, if the process has exited (pid == -1).
222 int socket_fd = -1; // For socket-activation services, this is the file
223 // descriptor for the socket.
225 ev_child child_listener;
226 ev_io child_status_listener;
228 // All dependents have stopped.
229 void allDepsStopped();
231 // Service has actually stopped (includes having all dependents
232 // reaching STOPPED state).
233 void stopped() noexcept;
235 // Service has successfully started
236 void started() noexcept;
238 // Service failed to start
239 void failed_to_start();
241 // A dependency of this service failed to start.
242 void failed_dependency();
244 // For process services, start the process, return true on success
245 bool start_ps_process() noexcept;
246 bool start_ps_process(const std::vector<const char *> &args, bool on_console) noexcept;
248 // Callback from libev when a child process dies
249 static void process_child_callback(struct ev_loop *loop, struct ev_child *w,
250 int revents) noexcept;
252 static void process_child_status(struct ev_loop *loop, ev_io * stat_io,
253 int revents) noexcept;
255 void handle_exit_status() noexcept;
257 // A dependency has reached STARTED state
258 void dependencyStarted() noexcept;
260 void allDepsStarted(bool haveConsole = false) noexcept;
262 // Read the pid-file, return false on failure
263 bool read_pid_file() noexcept;
265 // Open the activation socket, return false on failure
266 bool open_socket() noexcept;
268 // Check whether dependencies have started, and optionally ask them to start
269 bool startCheckDependencies(bool do_start) noexcept;
271 // Whether a STARTING service can immediately transition to STOPPED (as opposed to
272 // having to wait for it reach STARTED and then go through STOPPING).
273 bool can_interrupt_start() noexcept
275 return waiting_for_deps;
278 // Whether a STOPPING service can immediately transition to STARTED.
279 bool can_interrupt_stop() noexcept
281 return waiting_for_deps && ! force_stop;
284 // A dependent has reached STOPPED state
285 void dependentStopped() noexcept;
287 // check if all dependents have stopped
288 bool stopCheckDependents() noexcept;
290 // issue a stop to all dependents, return true if they are all already stopped
291 bool stopDependents() noexcept;
293 void forceStop() noexcept; // force-stop this service and all dependents
295 void notifyListeners(ServiceEvent event) noexcept
297 for (auto l : listeners) {
298 l->serviceEvent(this, event);
302 // Queue to run on the console. 'acquiredConsole()' will be called when the console is available.
303 void queueForConsole() noexcept;
305 // Console is available.
306 void acquiredConsole() noexcept;
308 // Release console (console must be currently held by this service)
309 void releaseConsole() noexcept;
313 ServiceRecord(ServiceSet *set, string name)
314 : service_state(ServiceState::STOPPED), desired_state(ServiceState::STOPPED), auto_restart(false),
315 pinned_stopped(false), pinned_started(false), waiting_for_deps(false),
316 waiting_for_execstat(false), doing_recovery(false), force_stop(false)
320 service_type = ServiceType::DUMMY;
323 ServiceRecord(ServiceSet *set, string name, ServiceType service_type, string &&command, std::list<std::pair<unsigned,unsigned>> &command_offsets,
324 sr_list * pdepends_on, sr_list * pdepends_soft)
325 : ServiceRecord(set, name)
329 this->service_type = service_type;
330 this->depends_on = std::move(*pdepends_on);
332 program_name = command;
333 exec_arg_parts = separate_args(program_name, command_offsets);
335 for (sr_iter i = depends_on.begin(); i != depends_on.end(); ++i) {
336 (*i)->dependents.push_back(this);
340 auto b_iter = soft_deps.end();
341 for (sr_iter i = pdepends_soft->begin(); i != pdepends_soft->end(); ++i) {
342 b_iter = soft_deps.emplace(b_iter, this, *i);
343 (*i)->soft_dpts.push_back(&(*b_iter));
348 // TODO write a destructor
350 // Set the stop command and arguments (may throw std::bad_alloc)
351 void setStopCommand(std::string command, std::list<std::pair<unsigned,unsigned>> &stop_command_offsets)
353 stop_command = command;
354 stop_arg_parts = separate_args(stop_command, stop_command_offsets);
357 // Get the current service state.
358 ServiceState getState() noexcept
360 return service_state;
363 // Get the target (aka desired) state.
364 ServiceState getTargetState() noexcept
366 return desired_state;
369 // Set logfile, should be done before service is started
370 void setLogfile(string logfile)
372 this->logfile = logfile;
375 // Set whether this service should automatically restart when it dies
376 void setAutoRestart(bool auto_restart) noexcept
378 this->auto_restart = auto_restart;
381 void setSmoothRecovery(bool smooth_recovery) noexcept
383 this->smooth_recovery = smooth_recovery;
386 // Set "on start" flags (commands)
387 void setOnstartFlags(OnstartFlags flags) noexcept
389 this->onstart_flags = flags;
392 // Set an additional signal (other than SIGTERM) to be used to terminate the process
393 void setExtraTerminationSignal(int signo) noexcept
395 this->term_signal = signo;
398 void set_pid_file(string &&pid_file) noexcept
400 this->pid_file = pid_file;
403 void set_socket_details(string &&socket_path, int socket_perms, uid_t socket_uid, uid_t socket_gid) noexcept
405 this->socket_path = socket_path;
406 this->socket_perms = socket_perms;
407 this->socket_uid = socket_uid;
408 this->socket_gid = socket_gid;
411 const char *getServiceName() const noexcept { return service_name.c_str(); }
412 ServiceState getState() const noexcept { return service_state; }
414 void start() noexcept; // start the service
415 void stop() noexcept; // stop the service
417 void pinStart() noexcept; // start the service and pin it
418 void pinStop() noexcept; // stop the service and pin it
419 void unpin() noexcept; // unpin the service
421 bool isDummy() noexcept
423 return service_type == ServiceType::DUMMY;
426 // Add a listener. A listener must only be added once. May throw std::bad_alloc.
427 void addListener(ServiceListener * listener)
429 listeners.insert(listener);
432 // Remove a listener.
433 void removeListener(ServiceListener * listener) noexcept
435 listeners.erase(listener);
443 std::list<ServiceRecord *> records;
444 const char *service_dir; // directory containing service descriptions
445 bool restart_enabled; // whether automatic restart is enabled (allowed)
447 ShutdownType shutdown_type = ShutdownType::CONTINUE; // Shutdown type, if stopping
449 ServiceRecord * console_queue_tail = nullptr; // last record in console queue
453 // Load a service description, and dependencies, if there is no existing
454 // record for the given name.
456 // ServiceLoadException (or subclass) on problem with service description
457 // std::bad_alloc on out-of-memory condition
458 ServiceRecord *loadServiceRecord(const char *name);
463 ServiceSet(const char *service_dir)
465 this->service_dir = service_dir;
467 restart_enabled = true;
470 // Start the service with the given name. The named service will begin
471 // transition to the 'started' state.
473 // Throws a ServiceLoadException (or subclass) if the service description
474 // cannot be loaded or is invalid;
475 // Throws std::bad_alloc if out of memory.
476 void startService(const char *name);
478 // Locate an existing service record.
479 ServiceRecord *findService(const std::string &name) noexcept;
481 // Find a loaded service record, or load it if it is not loaded.
483 // ServiceLoadException (or subclass) on problem with service description
484 // std::bad_alloc on out-of-memory condition
485 ServiceRecord *loadService(const std::string &name)
487 ServiceRecord *record = findService(name);
488 if (record == nullptr) {
489 record = loadServiceRecord(name.c_str());
494 // Stop the service with the given name. The named service will begin
495 // transition to the 'stopped' state.
496 void stopService(const std::string &name) noexcept;
498 // Set the console queue tail (returns previous tail)
499 ServiceRecord * consoleQueueTail(ServiceRecord * newTail) noexcept
501 auto prev_tail = console_queue_tail;
502 console_queue_tail = newTail;
506 // Notification from service that it is active (state != STOPPED)
507 // Only to be called on the transition from inactive to active.
508 void service_active(ServiceRecord *) noexcept;
510 // Notification from service that it is inactive (STOPPED)
511 // Only to be called on the transition from active to inactive.
512 void service_inactive(ServiceRecord *) noexcept;
514 // Find out how many services are active (starting, running or stopping,
516 int count_active_services() noexcept
518 return active_services;
521 void stop_all_services(ShutdownType type = ShutdownType::HALT) noexcept
523 restart_enabled = false;
524 shutdown_type = type;
525 for (std::list<ServiceRecord *>::iterator i = records.begin(); i != records.end(); ++i) {
531 void set_auto_restart(bool restart) noexcept
533 restart_enabled = restart;
536 bool get_auto_restart() noexcept
538 return restart_enabled;
541 ShutdownType getShutdownType() noexcept
543 return shutdown_type;