service.h 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  1. #ifndef SERVICE_H
  2. #define SERVICE_H
  3. #include <string>
  4. #include <list>
  5. #include <vector>
  6. #include <csignal>
  7. #include <unordered_set>
  8. #include <algorithm>
  9. #include <dasynq.h>
  10. #include <dinit.h>
  11. #include <control.h>
  12. #include <service-listener.h>
  13. #include <service-constants.h>
  14. #include <load-service.h>
  15. #include <dinit-ll.h>
  16. #include <dinit-log.h>
  17. #include <service-dir.h>
  18. #include <dinit-env.h>
  19. /*
  20. * This header defines service_record, a data record maintaining information about a service,
  21. * and service_set, a set of interdependent service records. It also defines some associated
  22. * types and exceptions.
  23. *
  24. * Service states
  25. * --------------
  26. * Services have both a current state and a desired state. The desired state can be
  27. * either STARTED or STOPPED. The current state can also be STARTING or STOPPING.
  28. * A service can be "pinned" in either the STARTED or STOPPED states to prevent it
  29. * from leaving that state until it is unpinned.
  30. *
  31. * The total state is a combination of the two, current and desired:
  32. * STOPPED/STOPPED : stopped and will remain stopped
  33. * STOPPED/STARTED : stopped (pinned), must be unpinned to start
  34. * STARTING/STARTED : starting, but not yet started. Dependencies may also be starting.
  35. * STARTING/STOPPED : as above, but the service will be stopped again as soon as it has
  36. * completed startup.
  37. * STARTED/STARTED : running and will continue running.
  38. * STARTED/STOPPED : started (pinned), must be unpinned to stop
  39. * STOPPING/STOPPED : stopping and will stop. Dependents may be stopping.
  40. * STOPPING/STARTED : as above, but the service will be re-started again once it stops.
  41. *
  42. * A scripted service is in the STARTING/STOPPING states during the script execution.
  43. * A process service is in the STOPPING state when it has been signalled to stop, and is
  44. * in the STARTING state when waiting for dependencies to start or for the exec() call in
  45. * the forked child to complete and return a status.
  46. *
  47. * Interrupted transitions
  48. * -----------------------
  49. * A service that is STOPPING may be issued a start order, or a service that is STARTING may be
  50. * issued a stop. In some cases a STOPPING or STARTING transition can be interrupted and immediately
  51. * switch to the other kind of transition. We don't normally want to do this if we're waiting on an
  52. * external process, since simply killing that process might leave a mess. However, if a service is
  53. * waiting for its dependencies or dependents, its start or stop can usually be interrupted.
  54. *
  55. * Acquisition/release:
  56. * ------------------
  57. * Each service has a dependent-count ("required_by"). This starts at 0, adds 1 if the service has
  58. * explicitly been started (i.e. "start_explicit" is true), and adds 1 for each dependent service
  59. * which is not STOPPED/STOPPING (including dependents via a "soft" dependency relationship).
  60. * When required_by transitions to 0, the service is stopped and it will release its own dependencies
  61. * (unless it is pinned started). Conversely, when required_by transitions from 0 to 1, the service is
  62. * started and dependencies will be required (unless pinned stopped).
  63. *
  64. * In general, therefore, the dependent-count determines the target state (STARTED if the count is greater
  65. * than 0, otherwise STOPPED). Explicit activation counts effectively increments the count and sets the
  66. * target state as STARTED.
  67. *
  68. * An exception is that setting the target state to STOPPED is used to inhibit restart. This is propagated
  69. * to dependent services (i.e. their target state will also be set to STOPPED). The required_by count for a
  70. * service in this scenario will settle to 0 anyway (because dependents will be stopped).
  71. *
  72. * Another exception is that a service may become STOPPED while it is still required (for example if a
  73. * process terminates unexpectedly). This will force hard dependents to stop also, so again, the required_by
  74. * count will settle to 0, unless the service is active and set to auto-restart (or a dependent is).
  75. *
  76. * When a service stops, any soft dependency links to its dependents must be broken, or otherwise the
  77. * target state will remain as STARTED and the service will always restart. If the auto-restart option is
  78. * enabled for the service, or if the service is explicitly being restarted (restart == true), this is
  79. * actually the desired behaviour, and so in these cases the dependency links are not broken.
  80. *
  81. * Force stop
  82. * ----------
  83. * A service can be issued a stop-and-take down order (via `stop(true)'); this will first stop
  84. * dependent services, which may restart and cancel the stop of the former service. However, a
  85. * service can be force-stopped, which means that its stop process cannot be cancelled (though
  86. * it may still be put in a desired state of STARTED, meaning it will start immediately upon
  87. * stopping). Force-stop is achieved via a flag in the service record which is checked before
  88. * interrupting a stop operation.
  89. *
  90. * Pinning
  91. * -------
  92. * A service may be "pinned" in either STARTED or STOPPED states (or even both). Once it
  93. * reaches a pinned state, a service will not leave that state. A service that is pinned
  94. * STOPPED cannot be started or marked active and any attempt to start it will fail. A service
  95. * that is pinned STARTED remains started even if the service is not marked active and no other
  96. * active service depends on it; this is the only scenario in which a service with a required_by
  97. * count of 0 will remain started. Once unpinned the service will stop.
  98. *
  99. * (Note that pinning prevents, but never causes, state transition).
  100. *
  101. * The priority of the different state deciders is:
  102. * - pins
  103. * - force stop flag
  104. * - desired state (which is manipulated by require/release operations)
  105. *
  106. * So a forced stop cannot occur until the service is not pinned started, for instance. (if
  107. * pinned, the forced stop remains pending until the pin is released).
  108. *
  109. * Two-phase transition
  110. * --------------------
  111. * Transition between states occurs in two phases: propagation and execution. In both phases
  112. * a linked-list queue is used to keep track of which services need processing; this avoids
  113. * recursion (which would be of unknown depth and therefore liable to cause stack overflow).
  114. *
  115. * In the propagation phase, acquisition/release messages are processed, and desired state may be
  116. * altered accordingly. Start and stop requests are also propagated in this phase. The state may
  117. * be set to STARTING or STOPPING to reflect the desired state, but will never be set to STARTED
  118. * or STOPPED (that happens in the execution phase).
  119. *
  120. * The two-phase transition is needed to avoid problem where a service that becomes STOPPED has
  121. * an incorrect acquisition count, which may cause it to restart when it should not. The
  122. * propagation phase allows the acquisition count to settle before the transition to the STOPPED
  123. * state occurs, and the decision whether to restart can then be made based on the (correct)
  124. * acquisition count. See "acquisition/release" above for details of when this can occur.
  125. *
  126. * Propagation variables:
  127. * prop_acquire: the service has transitioned to an acquired state and must issue an acquire
  128. * on its dependencies
  129. * prop_release: the service has transitioned to a released state and must issue a release on
  130. * its dependencies.
  131. *
  132. * prop_start: the service should start
  133. * prop_stop: the service should stop
  134. *
  135. * Note that "prop_acquire"/"prop_release" form a pair which cannot both be set at the same time
  136. * which is enforced via explicit checks. For "prop_start"/"prop_stop" this occurs implicitly.
  137. *
  138. * In the execution phase, actions are taken to achieve the desired state. Actual state may
  139. * transition according to the current and desired states. Processes can be sent signals, etc
  140. * in order to stop them.
  141. */
  142. class service_record;
  143. class service_set;
  144. class base_process_service;
  145. class process_service;
  146. /* Service dependency record */
  147. class service_dep
  148. {
  149. service_record * from;
  150. service_record * to;
  151. public:
  152. /* Whether the 'from' service is waiting for the 'to' service to start */
  153. bool waiting_on;
  154. /* Whether the 'from' service is holding an acquire on the 'to' service */
  155. bool holding_acq;
  156. const dependency_type dep_type;
  157. // Check if the dependency is a hard dependency (including milestone still waiting).
  158. bool is_hard()
  159. {
  160. return dep_type == dependency_type::REGULAR
  161. || (dep_type == dependency_type::MILESTONE && waiting_on);
  162. }
  163. // Check if the dependency represents only an ordering constraint (not really a dependency)
  164. bool is_only_ordering()
  165. {
  166. return (dep_type == dependency_type::BEFORE) || (dep_type == dependency_type::AFTER);
  167. }
  168. service_dep(service_record * from, service_record * to, dependency_type dep_type_p) noexcept
  169. : from(from), to(to), waiting_on(false), holding_acq(false), dep_type(dep_type_p)
  170. { }
  171. service_dep(const service_dep &) = delete;
  172. void operator=(const service_dep &) = delete;
  173. service_record * get_from() const noexcept
  174. {
  175. return from;
  176. }
  177. service_record * get_to() const noexcept
  178. {
  179. return to;
  180. }
  181. void set_to(service_record *new_to) noexcept
  182. {
  183. to = new_to;
  184. }
  185. void set_from(service_record *new_from) noexcept
  186. {
  187. from = new_from;
  188. }
  189. };
  190. /* preliminary service dependency information */
  191. class prelim_dep
  192. {
  193. public:
  194. service_record * const to;
  195. dependency_type const dep_type;
  196. prelim_dep(service_record *to_p, dependency_type dep_type_p) : to(to_p), dep_type(dep_type_p)
  197. {
  198. // (constructor)
  199. }
  200. };
  201. // log a service load exception
  202. inline void log_service_load_failure(service_description_exc &exc)
  203. {
  204. if (exc.line_num != (unsigned)-1) {
  205. if (exc.setting_name == nullptr) {
  206. log(loglevel_t::ERROR, "Error in service description for '", exc.service_name, "' (line ", exc.line_num, "): ",
  207. exc.exc_description);
  208. }
  209. else {
  210. log(loglevel_t::ERROR, "Error in service description for '", exc.service_name, "': setting '", exc.setting_name, "' "
  211. "(on line ", exc.line_num, "): ",
  212. exc.exc_description);
  213. }
  214. }
  215. else {
  216. // If no line number, setting name must be present
  217. log(loglevel_t::ERROR, "Error in service description for '", exc.service_name, "' setting '", exc.setting_name, "': ",
  218. exc.exc_description);
  219. }
  220. }
  221. // service_record: base class for service record containing static information
  222. // and current state of each service.
  223. //
  224. // This abstract base class defines the dependency behaviour of services. The actions to actually bring a
  225. // service up or down are specified by subclasses in the virtual methods (see especially bring_up() and
  226. // bring_down()).
  227. //
  228. class service_record
  229. {
  230. protected:
  231. using string = std::string;
  232. using time_val = dasynq::time_val;
  233. private:
  234. string service_name;
  235. service_type_t record_type;
  236. // 'service_state' can be any valid state: STARTED, STARTING, STOPPING, STOPPED.
  237. // 'desired_state' is only set to final states: STARTED or STOPPED.
  238. service_state_t service_state = service_state_t::STOPPED;
  239. service_state_t desired_state = service_state_t::STOPPED;
  240. protected:
  241. service_flags_t onstart_flags;
  242. environment service_env; // holds the environment populated during load
  243. const char *service_dsc_dir = nullptr; // directory containing service description file
  244. bool auto_restart : 1; // whether to restart this (process) if it dies unexpectedly
  245. bool smooth_recovery : 1; // whether the service process can restart without bringing down service
  246. // Pins. Start pins are directly transitive (hence pinned_started and dept_pinned_started) whereas
  247. // for stop pins, the effect is transitive since a stop-pinned service will "fail" to start anyway.
  248. bool pinned_stopped : 1;
  249. bool pinned_started : 1;
  250. bool dept_pinned_started : 1; // pinned started due to dependent
  251. bool waiting_for_deps : 1; // if STARTING, whether we are waiting for dependencies/console
  252. // if STOPPING, whether we are waiting for dependents to stop
  253. bool waiting_for_console : 1; // waiting for exclusive console access (while STARTING)
  254. bool have_console : 1; // whether we have exclusive console access (STARTING/STARTED)
  255. bool waiting_for_execstat : 1; // if we are waiting for exec status after fork()
  256. bool start_explicit : 1; // whether we are are explicitly required to be started
  257. bool prop_require : 1; // require must be propagated
  258. bool prop_release : 1; // release must be propagated
  259. bool prop_failure : 1; // failure to start must be propagated
  260. bool prop_start : 1;
  261. bool prop_stop : 1;
  262. bool prop_pin_dpt : 1;
  263. bool start_failed : 1; // failed to start (reset when begins starting)
  264. bool start_skipped : 1; // start was skipped by interrupt
  265. bool in_auto_restart : 1;
  266. bool in_user_restart : 1;
  267. bool is_loading : 1; // used to detect cyclic dependencies when loading a service
  268. int required_by = 0; // number of dependents wanting this service to be started
  269. // list of dependencies
  270. typedef std::list<service_dep> dep_list;
  271. // list of dependents
  272. typedef std::list<service_dep *> dpt_list;
  273. dep_list depends_on; // services this one depends on
  274. dpt_list dependents; // services depending on this one
  275. service_set *services; // the set this service belongs to
  276. std::unordered_set<service_listener *> listeners;
  277. process_service *log_consumer = nullptr;
  278. // Process services:
  279. bool force_stop; // true if the service must actually stop. This is the
  280. // case if for example the process dies; the service,
  281. // and all its dependencies, MUST be stopped.
  282. int term_signal = SIGTERM; // signal to use for process termination
  283. string socket_path; // path to the socket for socket-activation service
  284. int socket_perms = 0; // socket permissions ("mode")
  285. uid_t socket_uid = -1; // socket user id or -1
  286. gid_t socket_gid = -1; // socket group id or -1
  287. stopped_reason_t stop_reason = stopped_reason_t::NORMAL; // reason why stopped
  288. string start_on_completion; // service to start when this one completes
  289. // Data for use by service_set
  290. public:
  291. // Console queue.
  292. lld_node<service_record> console_queue_node;
  293. // Propagation and start/stop queues
  294. lls_node<service_record> prop_queue_node;
  295. lls_node<service_record> stop_queue_node;
  296. protected:
  297. // Service has actually stopped (includes having all dependents
  298. // reaching STOPPED state).
  299. void stopped() noexcept;
  300. // Service has successfully started
  301. void started() noexcept;
  302. // Service failed to start (should be called with state set to STOPPING or STOPPED).
  303. // dep_failed: whether failure is recorded due to a dependency failing
  304. // immediate_stop: whether to set state as STOPPED and handle complete stop.
  305. void failed_to_start(bool dep_failed = false, bool immediate_stop = true) noexcept;
  306. // Service stopped (failed), unrecoverably; dependents should not auto-restart
  307. void unrecoverable_stop() noexcept;
  308. // A dependency has reached STARTED state
  309. void dependency_started() noexcept;
  310. void all_deps_started() noexcept;
  311. // Start all dependencies, return true if all have started
  312. bool start_check_dependencies() noexcept;
  313. // Check whether all dependencies have started (i.e. whether we can start now)
  314. bool check_deps_started() noexcept;
  315. // Whether a STOPPING service can immediately transition to STARTED.
  316. bool can_interrupt_stop() noexcept
  317. {
  318. return waiting_for_deps && ! force_stop;
  319. }
  320. // A dependent has reached STOPPED state
  321. void dependent_stopped() noexcept;
  322. // check if all dependents have stopped
  323. bool stop_check_dependents() noexcept;
  324. // issue a stop to all dependents, return true if they are all already stopped
  325. bool stop_dependents(bool with_restart, bool for_restart) noexcept;
  326. // issue a restart to all hard dependents
  327. bool restart_dependents() noexcept;
  328. void require() noexcept;
  329. void release(bool issue_stop = true) noexcept;
  330. void release_dependencies() noexcept;
  331. // Check if service is, fundamentally, stopped. It is either in the stopped state, or
  332. // starting but waiting for dependents, i.e. it can be trivially set to stopped state.
  333. bool is_fundamentally_stopped() noexcept
  334. {
  335. return service_state == service_state_t::STOPPED
  336. || (service_state == service_state_t::STARTING && waiting_for_deps);
  337. }
  338. void notify_listeners(service_event_t event) noexcept
  339. {
  340. for (auto l : listeners) {
  341. l->service_event(this, event);
  342. }
  343. }
  344. // Queue to run on the console. 'acquired_console()' will be called when the console is available.
  345. // Has no effect if the service has already queued for console.
  346. void queue_for_console() noexcept;
  347. // Release console (console must be currently held by this service)
  348. void release_console() noexcept;
  349. // Initiate definite startup
  350. void initiate_start() noexcept;
  351. // Called on transition of desired state from stopped to started (or unpinned stop)
  352. void do_start() noexcept;
  353. // Begin stopping, release activation.
  354. void do_stop(bool with_restart = false) noexcept;
  355. // Set the service state
  356. void set_state(service_state_t new_state) noexcept
  357. {
  358. service_state = new_state;
  359. }
  360. // Set the target state
  361. void set_target_state(service_state_t new_target_state) noexcept
  362. {
  363. desired_state = new_target_state;
  364. }
  365. // Virtual functions, to be implemented by service implementations:
  366. // Do any post-dependency startup; return false on failure. Should return true if service
  367. // has started or is in the process of starting.
  368. virtual bool bring_up() noexcept;
  369. // All dependents have stopped, and this service should proceed to stop.
  370. virtual void bring_down() noexcept;
  371. // Whether a STARTING service can immediately transition to STOPPED (as opposed to
  372. // having to wait for it reach STARTED and then go through STOPPING). Note that the
  373. // waiting_for_deps flag being set may override this check.
  374. virtual bool can_interrupt_start() noexcept
  375. {
  376. return waiting_for_deps;
  377. }
  378. // Interrupt startup. Returns true if service start is fully cancelled; returns false if cancel order
  379. // issued but service has not yet responded (state will be set to STOPPING).
  380. virtual bool interrupt_start() noexcept;
  381. // The service is becoming inactive - i.e. it has stopped and will not be immediately restarted. Perform
  382. // any appropriate cleanup.
  383. virtual void becoming_inactive() noexcept { }
  384. // Check whether the service should automatically restart (assuming auto_restart is true)
  385. virtual bool check_restart() noexcept
  386. {
  387. return true;
  388. }
  389. public:
  390. class loading_tag_cls { };
  391. static const loading_tag_cls LOADING_TAG;
  392. service_record(service_set *set, const string &name)
  393. : service_name(name), service_state(service_state_t::STOPPED),
  394. desired_state(service_state_t::STOPPED), auto_restart(false), smooth_recovery(false),
  395. pinned_stopped(false), pinned_started(false), dept_pinned_started(false),
  396. waiting_for_deps(false), waiting_for_console(false), have_console(false),
  397. waiting_for_execstat(false), start_explicit(false), prop_require(false), prop_release(false),
  398. prop_failure(false), prop_start(false), prop_stop(false), prop_pin_dpt(false),
  399. start_failed(false), start_skipped(false), in_auto_restart(false), in_user_restart(false),
  400. is_loading(false), force_stop(false)
  401. {
  402. services = set;
  403. record_type = service_type_t::PLACEHOLDER;
  404. }
  405. // Construct a service record and mark it as a placeholder for a loading service
  406. service_record(service_set *set, const string &name, loading_tag_cls tag)
  407. : service_record(set, name) {
  408. is_loading = true;
  409. }
  410. service_record(service_set *set, const string &name, service_type_t record_type_p,
  411. const std::list<prelim_dep> &deplist_p)
  412. : service_record(set, name)
  413. {
  414. services = set;
  415. service_name = name;
  416. this->record_type = record_type_p;
  417. try {
  418. for (auto & pdep : deplist_p) {
  419. auto b = depends_on.emplace(depends_on.end(), this, pdep.to, pdep.dep_type);
  420. try {
  421. pdep.to->dependents.push_back(&(*b));
  422. }
  423. catch (...) {
  424. // we'll roll back one now and re-throw:
  425. depends_on.pop_back();
  426. throw;
  427. }
  428. }
  429. }
  430. catch (...) {
  431. for (auto & dep : depends_on) {
  432. dep.get_to()->dependents.pop_back();
  433. }
  434. throw;
  435. }
  436. }
  437. service_record(const service_record &) = delete;
  438. void operator=(const service_record &) = delete;
  439. virtual ~service_record() noexcept
  440. {
  441. }
  442. // Get the type of this service record
  443. service_type_t get_type() noexcept
  444. {
  445. return record_type;
  446. }
  447. // begin transition from stopped to started state or vice versa depending on current and desired state
  448. void execute_transition() noexcept;
  449. void do_propagation() noexcept;
  450. // Console is available.
  451. void acquired_console() noexcept;
  452. // Get the target (aka desired) state.
  453. service_state_t get_target_state() noexcept
  454. {
  455. return desired_state;
  456. }
  457. // Is the service explicitly marked active?
  458. bool is_marked_active() noexcept
  459. {
  460. return start_explicit;
  461. }
  462. // Set directory containing service description file
  463. void set_service_dsc_dir(const char *dsc_dir) noexcept
  464. {
  465. service_dsc_dir = dsc_dir;
  466. }
  467. const char *get_service_dsc_dir() noexcept
  468. {
  469. return service_dsc_dir;
  470. }
  471. void set_environment(environment &&env) noexcept
  472. {
  473. this->service_env = std::move(env);
  474. }
  475. // Set whether this service should automatically restart when it dies
  476. void set_auto_restart(bool auto_restart) noexcept
  477. {
  478. this->auto_restart = auto_restart;
  479. }
  480. void set_smooth_recovery(bool smooth_recovery) noexcept
  481. {
  482. this->smooth_recovery = smooth_recovery;
  483. }
  484. // Set "on start" flags (commands)
  485. void set_flags(service_flags_t flags) noexcept
  486. {
  487. this->onstart_flags = flags;
  488. }
  489. service_flags_t get_flags() noexcept
  490. {
  491. return onstart_flags;
  492. }
  493. void set_socket_details(string &&socket_path, int socket_perms, uid_t socket_uid, uid_t socket_gid)
  494. noexcept
  495. {
  496. this->socket_path = std::move(socket_path);
  497. this->socket_perms = socket_perms;
  498. this->socket_uid = socket_uid;
  499. this->socket_gid = socket_gid;
  500. }
  501. // Set the service that this one "chains" to. When this service completes, the named service is started.
  502. void set_chain_to(string &&chain_to) noexcept
  503. {
  504. start_on_completion = std::move(chain_to);
  505. }
  506. void set_log_consumer(process_service *consumer)
  507. {
  508. log_consumer = consumer;
  509. }
  510. process_service *get_log_consumer()
  511. {
  512. return log_consumer;
  513. }
  514. const std::string &get_name() const noexcept { return service_name; }
  515. service_state_t get_state() const noexcept { return service_state; }
  516. void start() noexcept; // start the service
  517. void stop(bool bring_down = true) noexcept; // stop the service
  518. bool restart() noexcept; // restart the service, returns true iff restart issued
  519. void forced_stop() noexcept; // force-stop this service and all dependents
  520. // Pin the service in "started" state (when it reaches the state)
  521. void pin_start() noexcept;
  522. // Pin the service in "stopped" state (when it reaches the state)
  523. void pin_stop() noexcept
  524. {
  525. pinned_stopped = true;
  526. }
  527. // Remove both "started" and "stopped" pins. If the service is currently pinned
  528. // in either state but would naturally be in the opposite state, it will immediately
  529. // commence starting/stopping.
  530. void unpin() noexcept;
  531. bool is_start_pinned() noexcept
  532. {
  533. return pinned_started || dept_pinned_started;
  534. }
  535. bool is_stop_pinned() noexcept
  536. {
  537. return pinned_stopped;
  538. }
  539. // Is this a dummy service (used only when loading a new service)?
  540. bool check_is_loading() noexcept
  541. {
  542. return is_loading;
  543. }
  544. bool did_start_fail() noexcept
  545. {
  546. return start_failed;
  547. }
  548. bool was_start_skipped() noexcept
  549. {
  550. return start_skipped;
  551. }
  552. // Add a listener. A listener must only be added once. May throw std::bad_alloc.
  553. void add_listener(service_listener * listener)
  554. {
  555. listeners.insert(listener);
  556. }
  557. // Remove a listener.
  558. void remove_listener(service_listener * listener) noexcept
  559. {
  560. listeners.erase(listener);
  561. }
  562. // Assuming there is one reference (from a control link), return true if this is the only reference,
  563. // or false if there are others (including dependents, excluding dependents via "before" and "after"
  564. // links).
  565. bool has_lone_ref(bool check_deps = true) noexcept
  566. {
  567. if (check_deps) {
  568. for (auto *dept : dependents) {
  569. // BEFORE links don't count because they are actually specified via the "to" service i.e.
  570. // this service. AFTER links don't count because, although they come from the dependent,
  571. // they do not reflect an actual dependency, just an ordering, and this service can still
  572. // be unloaded if they exist (and replaced with a placeholder).
  573. if (!value(dept->dep_type).is_in(dependency_type::BEFORE, dependency_type::AFTER)) {
  574. return false;
  575. }
  576. }
  577. }
  578. auto i = listeners.begin();
  579. return (++i == listeners.end());
  580. }
  581. // Check whether this service has no dependents/dependencies/references that would keep it from
  582. // unloading. Does not check listeners (i.e. mostly useful for placeholder services).
  583. bool is_unrefd() noexcept
  584. {
  585. return depends_on.empty() && dependents.empty() && log_consumer == nullptr;
  586. }
  587. // Get fd corresponding to the read end of the pipe/socket connected to the write end used by the
  588. // service process (if any). Opens the pipe if not already open; returns -1 if the service output
  589. // cannot be piped (failure to open pipe, wrong service type, etc).
  590. virtual int get_output_pipe_fd() noexcept
  591. {
  592. return -1;
  593. }
  594. // Prepare this service to be unloaded.
  595. void prepare_for_unload() noexcept;
  596. // Why did the service stop?
  597. stopped_reason_t get_stop_reason()
  598. {
  599. return stop_reason;
  600. }
  601. bool is_waiting_for_console()
  602. {
  603. return waiting_for_console;
  604. }
  605. bool has_console()
  606. {
  607. return have_console;
  608. }
  609. virtual pid_t get_pid()
  610. {
  611. return -1;
  612. }
  613. virtual int get_exit_status()
  614. {
  615. return 0;
  616. }
  617. dep_list & get_dependencies()
  618. {
  619. return depends_on;
  620. }
  621. dpt_list & get_dependents()
  622. {
  623. return dependents;
  624. }
  625. // Add a dependency. Caller must ensure that the services are in an appropriate state and that
  626. // a circular dependency chain is not created. Propagation queues should be processed after
  627. // calling this (if dependency may be required to start). May throw std::bad_alloc.
  628. service_dep & add_dep(service_record *to, dependency_type dep_type)
  629. {
  630. return add_dep(to, dep_type, depends_on.end());
  631. }
  632. // Add a dependency. Caller must ensure that the services are in an appropriate state and that
  633. // a circular dependency chain is not created. Propagation queues should be processed after
  634. // calling this (if dependency may be required to start). May throw std::bad_alloc.
  635. // i - where to insert the dependency (in dependencies list)
  636. service_dep & add_dep(service_record *to, dependency_type dep_type, dep_list::iterator i)
  637. {
  638. auto pre_i = depends_on.emplace(i, this, to, dep_type);
  639. try {
  640. to->dependents.push_back(&(*pre_i));
  641. }
  642. catch (...) {
  643. depends_on.erase(pre_i);
  644. throw;
  645. }
  646. if (dep_type != dependency_type::BEFORE && dep_type != dependency_type::AFTER) {
  647. if (dep_type == dependency_type::REGULAR
  648. || to->get_state() == service_state_t::STARTED
  649. || to->get_state() == service_state_t::STARTING) {
  650. if (service_state == service_state_t::STARTING
  651. || service_state == service_state_t::STARTED) {
  652. to->require();
  653. pre_i->holding_acq = true;
  654. }
  655. }
  656. }
  657. return *pre_i;
  658. }
  659. // Remove a dependency, of the given type, to the given service. Returns true if the specified
  660. // dependency was found (and removed). Propagation queues should be processed after calling.
  661. bool rm_dep(service_record *to, dependency_type dep_type) noexcept
  662. {
  663. for (auto i = depends_on.begin(); i != depends_on.end(); ++i) {
  664. auto & dep = *i;
  665. if (dep.get_to() == to && dep.dep_type == dep_type) {
  666. rm_dep(i);
  667. return true;
  668. }
  669. }
  670. return false;
  671. }
  672. void rm_dep(service_dep &dep) noexcept
  673. {
  674. for (auto i = depends_on.begin(); i != depends_on.end(); ++i) {
  675. if (&(*i) == &dep) {
  676. rm_dep(i);
  677. return;
  678. }
  679. }
  680. }
  681. dep_list::iterator rm_dep(dep_list::iterator i) noexcept
  682. {
  683. auto to = i->get_to();
  684. for (auto j = to->dependents.begin(); ; ++j) {
  685. if (*j == &(*i)) {
  686. to->dependents.erase(j);
  687. break;
  688. }
  689. }
  690. if (i->holding_acq) {
  691. to->release();
  692. }
  693. return depends_on.erase(i);
  694. }
  695. // Start a specific dependency of this service. Should only be called if this service is in an
  696. // appropriate state (started, starting). The dependency is marked as holding acquired; when
  697. // this service stops, the dependency will be released and may also stop.
  698. void start_dep(service_dep &dep) noexcept
  699. {
  700. if (!dep.holding_acq) {
  701. dep.get_to()->require();
  702. dep.holding_acq = true;
  703. }
  704. }
  705. // Transfer the file descriptors representing the output (logging) pipe for this service. The file
  706. // descriptors are returned as a pair (read,write) (possibly with -1,-1 if there is no log pipe open)
  707. // and disassociated from this service.
  708. // This is used when a process is reloaded (for example) to transfer the pipe from the original service
  709. // record to the new service record.
  710. virtual std::pair<int,int> transfer_output_pipe() noexcept
  711. {
  712. return {-1,-1};
  713. }
  714. };
  715. class placeholder_service : public service_record {
  716. int log_output_fd = -1; // write end of the output pipe
  717. int log_input_fd = -1; // read end of the output pipe
  718. public:
  719. placeholder_service(service_set *set, const string &name)
  720. : service_record(set, name, service_type_t::PLACEHOLDER, {}) {
  721. }
  722. int get_output_pipe_fd() noexcept override
  723. {
  724. if (log_input_fd != -1) {
  725. return log_input_fd;
  726. }
  727. int pipefds[2];
  728. if (bp_sys::pipe2(pipefds, O_CLOEXEC) == -1) {
  729. log(loglevel_t::ERROR, get_name(), " (placeholder): Can't open output pipe: ", std::strerror(errno));
  730. return -1;
  731. }
  732. log_input_fd = pipefds[0];
  733. log_output_fd = pipefds[1];
  734. return log_input_fd;
  735. }
  736. std::pair<int,int> transfer_output_pipe() noexcept override
  737. {
  738. std::pair<int,int> r { log_input_fd, log_output_fd };
  739. log_input_fd = log_output_fd = -1;
  740. return r;
  741. }
  742. void set_output_pipe(std::pair<int,int> fds) noexcept
  743. {
  744. log_input_fd = fds.first;
  745. log_output_fd = fds.second;
  746. }
  747. ~placeholder_service()
  748. {
  749. if (log_output_fd != -1) {
  750. bp_sys::close(log_output_fd);
  751. bp_sys::close(log_input_fd);
  752. }
  753. }
  754. };
  755. // Externally triggered service. This is essentially the same as an internal service, but does not start
  756. // until the external trigger is set.
  757. class triggered_service : public service_record {
  758. private:
  759. bool is_triggered = false;
  760. public:
  761. using service_record::service_record;
  762. bool bring_up() noexcept override;
  763. bool can_interrupt_start() noexcept override
  764. {
  765. return true;
  766. }
  767. void set_trigger(bool new_trigger) noexcept
  768. {
  769. is_triggered = new_trigger;
  770. if (is_triggered && get_state() == service_state_t::STARTING && !waiting_for_deps) {
  771. started();
  772. }
  773. }
  774. };
  775. inline auto extract_prop_queue(service_record *sr) -> decltype(sr->prop_queue_node) &
  776. {
  777. return sr->prop_queue_node;
  778. }
  779. inline auto extract_stop_queue(service_record *sr) -> decltype(sr->stop_queue_node) &
  780. {
  781. return sr->stop_queue_node;
  782. }
  783. inline auto extract_console_queue(service_record *sr) -> decltype(sr->console_queue_node) &
  784. {
  785. return sr->console_queue_node;
  786. }
  787. /*
  788. * A service_set, as the name suggests, manages a set of services.
  789. *
  790. * Other than the ability to find services by name, the service set manages various queues.
  791. * One is the queue for processes wishing to acquire the console. There is also a set of
  792. * processes that want to start, and another set of those that want to stop. These latter
  793. * two "queues" (not really queues since their order is not important) are used to prevent too
  794. * much recursion and to prevent service states from "bouncing" too rapidly.
  795. *
  796. * A service that wishes to start or stop puts itself on the start/stop queue; a service that
  797. * needs to propagate changes to dependent services or dependencies puts itself on the
  798. * propagation queue. Any operation that potentially manipulates the queues must be followed
  799. * by a "process queues" order (processQueues() method).
  800. *
  801. * Note that processQueues always repeatedly processes both queues until they are empty. The
  802. * process is finite because starting a service can never cause services to stop, unless they
  803. * fail to start, which should cause them to stop semi-permanently.
  804. */
  805. class service_set
  806. {
  807. protected:
  808. int active_services;
  809. std::list<service_record *> records;
  810. bool restart_enabled; // whether automatic restart is enabled (allowed)
  811. shutdown_type_t shutdown_type = shutdown_type_t::NONE; // Shutdown type, if stopping
  812. // Services waiting for exclusive access to the console
  813. dlist<service_record, extract_console_queue> console_queue;
  814. // Propagation and start/stop "queues" - list of services waiting for processing
  815. slist<service_record, extract_prop_queue> prop_queue;
  816. slist<service_record, extract_stop_queue> stop_queue;
  817. public:
  818. service_set() noexcept
  819. {
  820. active_services = 0;
  821. restart_enabled = true;
  822. }
  823. virtual ~service_set() noexcept
  824. {
  825. for (auto * s : records) {
  826. delete s;
  827. }
  828. }
  829. // Start the specified service. The service will be marked active.
  830. void start_service(service_record *svc) noexcept
  831. {
  832. svc->start();
  833. process_queues();
  834. }
  835. // Stop the specified service. Its active mark will be cleared.
  836. void stop_service(service_record *svc) noexcept
  837. {
  838. svc->stop(true);
  839. process_queues();
  840. }
  841. // Locate an existing service record.
  842. service_record *find_service(const std::string &name, bool find_placeholders = false) noexcept;
  843. // Load a service description, and dependencies, if there is no existing
  844. // record for the given name.
  845. // Throws:
  846. // service_load_exc (or subclass) on problem with service description
  847. // std::bad_alloc on out-of-memory condition
  848. virtual service_record *load_service(const char *name)
  849. {
  850. auto r = find_service(name);
  851. if (r == nullptr) {
  852. throw service_not_found(name);
  853. }
  854. return r;
  855. }
  856. // Re-load a service description from file. If the service type changes then this returns
  857. // a new service instead (the old pointer is no longer valid).
  858. // Throws:
  859. // service_load_exc (or subclass) on problem with service description
  860. // std::bad_alloc on out-of-memory condition
  861. virtual service_record *reload_service(service_record *service)
  862. {
  863. return service;
  864. }
  865. // Start the service with the given name. The named service will begin
  866. // transition to the 'started' state.
  867. //
  868. // Throws a service_load_exc (or subclass) if the service description
  869. // cannot be loaded or is invalid;
  870. // Throws std::bad_alloc if out of memory.
  871. void start_service(const char *name)
  872. {
  873. using namespace std;
  874. service_record *record = load_service(name);
  875. service_set::start_service(record);
  876. }
  877. void add_service(service_record *svc)
  878. {
  879. records.push_back(svc);
  880. }
  881. void remove_service(service_record *svc) noexcept
  882. {
  883. records.erase(std::find(records.begin(), records.end(), svc));
  884. }
  885. void replace_service(service_record *orig, service_record *replacement) noexcept
  886. {
  887. auto i = std::find(records.begin(), records.end(), orig);
  888. *i = replacement;
  889. }
  890. // Unload a service, possibly replacing it with a placeholder service. This can fail with bad_alloc.
  891. // svc is no longer valid on return.
  892. void unload_service(service_record *svc)
  893. {
  894. // Check if there are any "after" dependents, or any "before" dependencies. We need a placeholder
  895. // if so. Note that the placeholder is created before making any destructive changes, so if we fail
  896. // to create it no rollback is needed.
  897. placeholder_service *placeholder = nullptr;
  898. auto make_placeholder = [&]() {
  899. if (placeholder == nullptr) {
  900. std::unique_ptr<placeholder_service> ph { new placeholder_service(this, svc->get_name()) };
  901. add_service(ph.get());
  902. placeholder = ph.release();
  903. }
  904. };
  905. auto *consumer = svc->get_log_consumer();
  906. if (consumer != nullptr) {
  907. make_placeholder();
  908. placeholder->set_output_pipe(svc->transfer_output_pipe());
  909. }
  910. auto &svc_depts = svc->get_dependents();
  911. for (auto i = svc_depts.begin(); i != svc_depts.end(); ) {
  912. auto *dept = *i;
  913. if (dept->dep_type == dependency_type::AFTER) {
  914. make_placeholder();
  915. dept->set_to(placeholder);
  916. auto &ph_depts = placeholder->get_dependents();
  917. ph_depts.splice(ph_depts.end(), svc_depts, i++);
  918. continue;
  919. }
  920. ++i;
  921. }
  922. auto &svc_deps = svc->get_dependencies();
  923. for (auto i = svc_deps.begin(); i != svc_deps.end(); ) {
  924. auto &dep = *i;
  925. if (dep.dep_type == dependency_type::BEFORE) {
  926. make_placeholder();
  927. dep.set_from(placeholder);
  928. auto &ph_deps = placeholder->get_dependencies();
  929. ph_deps.splice(ph_deps.end(), svc_deps, i++);
  930. continue;
  931. }
  932. ++i;
  933. }
  934. // Proceed with unload.
  935. svc->prepare_for_unload();
  936. remove_service(svc);
  937. delete svc;
  938. }
  939. // Get the list of all loaded services.
  940. const std::list<service_record *> &list_services() noexcept
  941. {
  942. return records;
  943. }
  944. // Add a service record to the state propagation queue. The service record will have its
  945. // do_propagation() method called when the queue is processed.
  946. void add_prop_queue(service_record *service) noexcept
  947. {
  948. if (! prop_queue.is_queued(service)) {
  949. prop_queue.insert(service);
  950. }
  951. }
  952. // Add a service record to the stop queue. The service record will have its
  953. // execute_transition() method called when the queue is processed.
  954. void add_transition_queue(service_record *service) noexcept
  955. {
  956. if (! stop_queue.is_queued(service)) {
  957. stop_queue.insert(service);
  958. }
  959. }
  960. // Process state propagation and start/stop queues, until they are empty.
  961. void process_queues() noexcept
  962. {
  963. while (! stop_queue.is_empty() || ! prop_queue.is_empty()) {
  964. while (! prop_queue.is_empty()) {
  965. auto next = prop_queue.pop_front();
  966. next->do_propagation();
  967. }
  968. if (! stop_queue.is_empty()) {
  969. auto next = stop_queue.pop_front();
  970. next->execute_transition();
  971. }
  972. }
  973. }
  974. // Set the console queue tail (returns previous tail)
  975. void append_console_queue(service_record * newTail) noexcept
  976. {
  977. bool was_empty = console_queue.is_empty();
  978. console_queue.append(newTail);
  979. if (was_empty) {
  980. enable_console_log(false);
  981. }
  982. }
  983. // Pull and dispatch a waiter from the console queue
  984. void pull_console_queue() noexcept
  985. {
  986. if (console_queue.is_empty()) {
  987. // Discard the log buffer now, because we've potentially blocked output for a while
  988. // and allowed it to fill with stale messages. (If not much time has passed, the
  989. // request to discard will be ignored anyway).
  990. discard_console_log_buffer();
  991. enable_console_log(true);
  992. }
  993. else {
  994. service_record * front = console_queue.pop_front();
  995. front->acquired_console();
  996. }
  997. }
  998. void unqueue_console(service_record * service) noexcept
  999. {
  1000. if (console_queue.is_queued(service)) {
  1001. console_queue.unlink(service);
  1002. }
  1003. }
  1004. // Check if console queue is empty (possibly due to console already having
  1005. // been assigned to the only queueing service)
  1006. bool is_console_queue_empty() noexcept
  1007. {
  1008. return console_queue.is_empty();
  1009. }
  1010. // Check whether a service is queued for the console
  1011. bool is_queued_for_console(service_record * service) noexcept
  1012. {
  1013. return console_queue.is_queued(service);
  1014. }
  1015. // Notification from service that it is active (state != STOPPED)
  1016. // Only to be called on the transition from inactive to active.
  1017. void service_active(service_record *) noexcept;
  1018. // Notification from service that it is inactive (STOPPED)
  1019. // Only to be called on the transition from active to inactive.
  1020. void service_inactive(service_record *) noexcept;
  1021. // Find out how many services are active (starting, running or stopping,
  1022. // but not stopped).
  1023. int count_active_services() noexcept
  1024. {
  1025. return active_services;
  1026. }
  1027. void stop_all_services(shutdown_type_t type = shutdown_type_t::HALT) noexcept
  1028. {
  1029. restart_enabled = false;
  1030. shutdown_type = type;
  1031. for (std::list<service_record *>::iterator i = records.begin(); i != records.end(); ++i) {
  1032. (*i)->stop(false);
  1033. (*i)->unpin();
  1034. }
  1035. process_queues();
  1036. }
  1037. bool is_shutting_down() noexcept
  1038. {
  1039. return !restart_enabled;
  1040. }
  1041. shutdown_type_t get_shutdown_type() noexcept
  1042. {
  1043. return shutdown_type;
  1044. }
  1045. // Get an identifier for the run-time type of the service set (similar to typeid, but without
  1046. // requiring RTTI to be enabled during compilation).
  1047. virtual int get_set_type_id() noexcept
  1048. {
  1049. return SSET_TYPE_NONE;
  1050. }
  1051. };
  1052. // A service set which loads services from one of several service directories.
  1053. class dirload_service_set : public service_set
  1054. {
  1055. service_dir_pathlist service_dirs;
  1056. // Implementation of service load/reload.
  1057. // Find a service record, or load it from file. If the service has dependencies, load those also.
  1058. //
  1059. // If reload_svc != nullptr, then reload the specified service (with name specified by name). The return
  1060. // points to the new service, which may be a new service record, in which case the caller must remove
  1061. // the original record.
  1062. //
  1063. // If avoid_circular != nullptr (but reload_svc == nullptr), and if the service name refers to the
  1064. // service referred to by avoid_circular, a circular dependency is recognised. (A circular dependency
  1065. // is otherwise recognised on services as they are loading; this mechanism should be used when
  1066. // reloading a service, to prevent new dependencies forming on the original service).
  1067. //
  1068. // Throws service_load_exc (or subclass) if a dependency cycle is found or if another
  1069. // problem occurs (I/O error, service description not found etc). Throws std::bad_alloc
  1070. // if a memory allocation failure occurs.
  1071. //
  1072. service_record *load_reload_service(const char *name, service_record *reload_svc,
  1073. const service_record *avoid_circular);
  1074. public:
  1075. dirload_service_set() noexcept : service_set()
  1076. {
  1077. // nothing to do.
  1078. }
  1079. dirload_service_set(service_dir_pathlist &&pathlist) noexcept : service_set(), service_dirs(std::move(pathlist))
  1080. {
  1081. // nothing to do.
  1082. }
  1083. dirload_service_set(const char *path, bool dyn_alloc = false)
  1084. {
  1085. service_dirs.emplace_back(path, dyn_alloc);
  1086. }
  1087. dirload_service_set(const dirload_service_set &) = delete;
  1088. int get_service_dir_count() noexcept
  1089. {
  1090. return service_dirs.size();
  1091. }
  1092. const char * get_service_dir(int n) noexcept
  1093. {
  1094. return service_dirs[n].get_dir();
  1095. }
  1096. service_record *load_service(const char *name) override
  1097. {
  1098. return load_service(name, nullptr);
  1099. }
  1100. service_record *load_service(const char *name, const service_record *avoid_circular);
  1101. service_record *reload_service(service_record *service) override;
  1102. int get_set_type_id() noexcept override
  1103. {
  1104. return SSET_TYPE_DIRLOAD;
  1105. }
  1106. };
  1107. #endif