service.cc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. #include <cstring>
  2. #include <cerrno>
  3. #include <iterator>
  4. #include <memory>
  5. #include <cstddef>
  6. #include <sys/ioctl.h>
  7. #include <fcntl.h>
  8. #include <termios.h>
  9. #include "dinit.h"
  10. #include "service.h"
  11. #include "dinit-log.h"
  12. #include "dinit-socket.h"
  13. #include "dinit-util.h"
  14. #include "baseproc-sys.h"
  15. /*
  16. * service.cc - Service management.
  17. * See service.h for details.
  18. */
  19. // Find the requested service by name.
  20. static service_record * find_service(const std::list<service_record *> & records,
  21. const char *name) noexcept
  22. {
  23. using std::list;
  24. list<service_record *>::const_iterator i = records.begin();
  25. for ( ; i != records.end(); ++i ) {
  26. if (strcmp((*i)->get_name().c_str(), name) == 0) {
  27. return *i;
  28. }
  29. }
  30. return nullptr;
  31. }
  32. service_record * service_set::find_service(const std::string &name, bool find_placeholders) noexcept
  33. {
  34. service_record *r = ::find_service(records, name.c_str());
  35. if (r != nullptr && !find_placeholders && r->get_type() == service_type_t::PLACEHOLDER) {
  36. return nullptr;
  37. }
  38. return r;
  39. }
  40. void service_record::prepare_for_unload() noexcept
  41. {
  42. // Remove all dependencies:
  43. for (auto &dep : depends_on) {
  44. service_record *dependency = dep.get_to();
  45. auto &dep_dpts = dependency->dependents;
  46. dep_dpts.erase(std::find(dep_dpts.begin(), dep_dpts.end(), &dep));
  47. if (dep.dep_type == dependency_type::AFTER) {
  48. if (dependency->get_type() == service_type_t::PLACEHOLDER) {
  49. if (dependency->is_unrefd()) {
  50. services->remove_service(dependency);
  51. delete dependency;
  52. }
  53. }
  54. }
  55. }
  56. depends_on.clear();
  57. // Also remove all dependents. This should not be necessary except for "before" links.
  58. for (auto i = dependents.begin(); i != dependents.end();) {
  59. service_record *before_svc = (*i)->get_from();
  60. before_svc->rm_dep(**i++);
  61. if (before_svc->get_type() == service_type_t::PLACEHOLDER && before_svc->is_unrefd()) {
  62. services->remove_service(before_svc);
  63. delete before_svc;
  64. }
  65. }
  66. }
  67. // Called when a service has actually stopped; dependents have stopped already, unless this stop
  68. // is due to an unexpected process termination.
  69. void service_record::stopped() noexcept
  70. {
  71. if (have_console) {
  72. bp_sys::tcsetpgrp(0, bp_sys::getpgrp());
  73. release_console();
  74. }
  75. force_stop = false;
  76. // If we are to re-start, restarting should have been set true and desired_state should be STARTED.
  77. // (A restart could be cancelled via a separately issued stop, including via a shutdown).
  78. bool will_restart = desired_state == service_state_t::STARTED && !pinned_stopped;
  79. // If we won't restart, break soft dependencies now
  80. if (! will_restart) {
  81. for (auto dept : dependents) {
  82. if (!dept->is_hard()) {
  83. // waits-for or soft dependency:
  84. if (dept->waiting_on) {
  85. dept->waiting_on = false;
  86. dept->get_from()->dependency_started();
  87. }
  88. if (dept->holding_acq) {
  89. dept->holding_acq = false;
  90. // release without issuing stop, since we're called only when this
  91. // service is already stopped/stopping:
  92. release(false);
  93. }
  94. }
  95. }
  96. }
  97. for (auto & dependency : depends_on) {
  98. // we signal dependencies in case they are waiting for us to stop:
  99. dependency.get_to()->dependent_stopped();
  100. }
  101. service_state = service_state_t::STOPPED;
  102. if (will_restart) {
  103. // Desired state is "started".
  104. initiate_start();
  105. }
  106. else {
  107. becoming_inactive();
  108. if (start_explicit) {
  109. // If we were explicitly started, our required_by count must be at least 1. Use
  110. // release() to correctly release, mark inactive and release dependencies.
  111. start_explicit = false;
  112. release(false);
  113. }
  114. else if (required_by == 0) {
  115. // This can only be the case if we didn't have start_explicit, since required_by would
  116. // otherwise by non-zero. Since our release(s) above were with state != STOPPED, we now
  117. // must mark inactive (i.e. it won't have been done as part of the release).
  118. services->service_inactive(this);
  119. }
  120. }
  121. // Start failure will have been logged already, only log if we are stopped for other reasons:
  122. if (! start_failed) {
  123. log_service_stopped(service_name);
  124. // If this service chains to another, start the chained service now, if:
  125. // - this service self-terminated (rather than being stopped),
  126. // - ... successfully (i.e. exit code 0)
  127. // - this service won't restart, and
  128. // - a shutdown isn't in progress
  129. if ((onstart_flags.always_chain || (did_finish(stop_reason) && get_exit_status() == 0 && ! will_restart))
  130. && ! start_on_completion.empty() && ! services->is_shutting_down()) {
  131. try {
  132. auto chain_to = services->load_service(start_on_completion.c_str());
  133. chain_to->start();
  134. }
  135. catch (service_load_exc &sle) {
  136. log(loglevel_t::ERROR, "Couldn't chain to service ", start_on_completion, ": ",
  137. "couldn't load ", sle.service_name, ": ", sle.exc_description);
  138. }
  139. catch (std::bad_alloc &bae) {
  140. log(loglevel_t::ERROR, "Couldn't chain to service ", start_on_completion,
  141. ": Out of memory");
  142. }
  143. }
  144. }
  145. notify_listeners(service_event_t::STOPPED);
  146. }
  147. void service_record::require() noexcept
  148. {
  149. if (required_by++ == 0) {
  150. if (service_state != service_state_t::STARTING && service_state != service_state_t::STARTED) {
  151. prop_start = true;
  152. services->add_prop_queue(this);
  153. // Note: pin is checked in start().
  154. // Require will be propagated to dependencies if/when the service actually starts.
  155. }
  156. }
  157. }
  158. void service_record::release(bool issue_stop) noexcept
  159. {
  160. if (--required_by == 0) {
  161. if (service_state == service_state_t::STOPPING) {
  162. // If we are stopping but would have restarted, we now need to notify that the restart
  163. // has been cancelled. Other start-cancelled cases are handled by do_stop() (called
  164. // below).
  165. if (desired_state == service_state_t::STARTED && !is_start_pinned()) {
  166. notify_listeners(service_event_t::STARTCANCELLED);
  167. }
  168. }
  169. desired_state = service_state_t::STOPPED;
  170. if (is_start_pinned()) return;
  171. // Can stop, and can release dependencies now. We don't need to issue a release if
  172. // a require was pending though:
  173. prop_release = !prop_require;
  174. prop_require = false;
  175. if (prop_release) {
  176. services->add_prop_queue(this);
  177. }
  178. if (service_state != service_state_t::STOPPED && service_state != service_state_t::STOPPING
  179. && issue_stop) {
  180. stop_reason = stopped_reason_t::NORMAL;
  181. do_stop();
  182. }
  183. }
  184. }
  185. void service_record::release_dependencies() noexcept
  186. {
  187. for (auto & dependency : depends_on) {
  188. service_record * dep_to = dependency.get_to();
  189. if (dependency.holding_acq) {
  190. // We must clear holding_acq before calling release, otherwise the dependency
  191. // may decide to stop, check this link and release itself a second time.
  192. dependency.holding_acq = false;
  193. dep_to->release();
  194. }
  195. }
  196. }
  197. void service_record::start() noexcept
  198. {
  199. if (pinned_stopped) {
  200. // bail out early for this case, we don't want to set start_explicit
  201. return;
  202. }
  203. if (!start_explicit) {
  204. ++required_by;
  205. start_explicit = true;
  206. }
  207. do_start();
  208. }
  209. void service_record::initiate_start() noexcept
  210. {
  211. start_failed = false;
  212. start_skipped = false;
  213. service_state = service_state_t::STARTING;
  214. waiting_for_deps = true;
  215. if (start_check_dependencies()) {
  216. // Note: we can't set waiting_for_deps false here since ordering dependencies might still kick in
  217. services->add_transition_queue(this);
  218. }
  219. }
  220. void service_record::do_propagation() noexcept
  221. {
  222. if (prop_require) {
  223. // Need to require all our dependencies
  224. for (auto & dep : depends_on) {
  225. if (!dep.is_only_ordering()) {
  226. dep.get_to()->require();
  227. dep.holding_acq = true;
  228. }
  229. }
  230. prop_require = false;
  231. }
  232. if (prop_release) {
  233. release_dependencies();
  234. prop_release = false;
  235. }
  236. if (prop_failure) {
  237. prop_failure = false;
  238. stop_reason = stopped_reason_t::DEPFAILED;
  239. service_state = service_state_t::STOPPED;
  240. failed_to_start(true);
  241. }
  242. if (prop_start) {
  243. prop_start = false;
  244. do_start();
  245. }
  246. if (prop_stop) {
  247. prop_stop = false;
  248. do_stop(in_user_restart);
  249. }
  250. if (prop_pin_dpt) {
  251. bool dept_pin = false;
  252. for (auto *dept : dependents) {
  253. if (dept->is_hard() && dept->get_from()->is_start_pinned()) {
  254. dept_pin = true;
  255. break;
  256. }
  257. }
  258. if (dept_pin != dept_pinned_started) {
  259. dept_pinned_started = dept_pin;
  260. for (auto &dep : depends_on) {
  261. if (dep.is_hard() && dep.get_to()->dept_pinned_started != dept_pin) {
  262. dep.get_to()->prop_pin_dpt = true;
  263. services->add_prop_queue(dep.get_to());
  264. }
  265. }
  266. if (!dept_pinned_started && !pinned_started) {
  267. // No longer pinned at all
  268. if ((desired_state == service_state_t::STOPPED || force_stop)
  269. && service_state == service_state_t::STARTED) {
  270. do_stop();
  271. }
  272. }
  273. }
  274. }
  275. }
  276. void service_record::execute_transition() noexcept
  277. {
  278. if (service_state == service_state_t::STARTING) {
  279. if (check_deps_started()) {
  280. waiting_for_deps = false;
  281. all_deps_started();
  282. }
  283. }
  284. else if (service_state == service_state_t::STOPPING) {
  285. if (stop_check_dependents()) {
  286. waiting_for_deps = false;
  287. if (onstart_flags.kill_all_on_stop) {
  288. log(loglevel_t::NOTICE, true, "Sending TERM/KILL to all processes...\n");
  289. kill(-1, SIGTERM);
  290. sleep(1);
  291. kill(-1, SIGKILL);
  292. }
  293. bring_down();
  294. }
  295. }
  296. }
  297. void service_record::do_start() noexcept
  298. {
  299. bool was_active = service_state != service_state_t::STOPPED;
  300. desired_state = service_state_t::STARTED;
  301. if (pinned_stopped) {
  302. if (!was_active) {
  303. failed_to_start(false, false);
  304. }
  305. return;
  306. }
  307. // re-attach any soft dependents, now that we are starting again
  308. if (!was_active) {
  309. for (auto dept : dependents) {
  310. if (!dept->is_hard()) {
  311. service_state_t dept_state = dept->get_from()->service_state;
  312. if (!dept->holding_acq
  313. && (dept_state == service_state_t::STARTED || dept_state == service_state_t::STARTING)) {
  314. dept->holding_acq = true;
  315. ++required_by;
  316. }
  317. }
  318. }
  319. }
  320. if (was_active) {
  321. // We're already starting/started, or we are stopping and need to wait for
  322. // that the complete.
  323. if (service_state != service_state_t::STOPPING) {
  324. return;
  325. }
  326. if (! can_interrupt_stop()) {
  327. return;
  328. }
  329. // We're STOPPING, and that can be interrupted. Our dependencies might be STOPPING,
  330. // but if so they are waiting (for us), so they too can be instantly returned to
  331. // STARTING state.
  332. notify_listeners(service_event_t::STOPCANCELLED);
  333. }
  334. else { // !was_active
  335. services->service_active(this);
  336. prop_require = !prop_release;
  337. prop_release = false;
  338. if (prop_require) {
  339. services->add_prop_queue(this);
  340. }
  341. }
  342. initiate_start();
  343. }
  344. void service_record::dependency_started() noexcept
  345. {
  346. // Note that we check for STARTED state here in case the service is in smooth recovery while pinned.
  347. // In that case it will wait for dependencies to start before restarting the process.
  348. if ((service_state == service_state_t::STARTING || service_state == service_state_t::STARTED)
  349. && waiting_for_deps) {
  350. services->add_transition_queue(this);
  351. }
  352. }
  353. bool service_record::start_check_dependencies() noexcept
  354. {
  355. bool all_deps_started = true;
  356. for (auto & dep : depends_on) {
  357. service_record * to = dep.get_to();
  358. if (dep.is_only_ordering()
  359. && to->service_state != service_state_t::STARTING) continue;
  360. if (to->service_state != service_state_t::STARTED) {
  361. dep.waiting_on = true;
  362. all_deps_started = false;
  363. }
  364. }
  365. for (auto * dept : dependents) {
  366. if (!dept->waiting_on && dept->is_only_ordering()) {
  367. service_record *from = dept->get_from();
  368. if (from->get_state() == service_state_t::STARTING) {
  369. dept->waiting_on = true;
  370. }
  371. }
  372. }
  373. return all_deps_started;
  374. }
  375. bool service_record::check_deps_started() noexcept
  376. {
  377. for (auto & dep : depends_on) {
  378. if (dep.waiting_on) {
  379. return false;
  380. }
  381. }
  382. return true;
  383. }
  384. void service_record::all_deps_started() noexcept
  385. {
  386. if (onstart_flags.starts_on_console && !have_console) {
  387. queue_for_console();
  388. return;
  389. }
  390. waiting_for_deps = false;
  391. if (!bring_up()) {
  392. service_state = service_state_t::STOPPING;
  393. failed_to_start();
  394. }
  395. }
  396. void service_record::acquired_console() noexcept
  397. {
  398. waiting_for_console = false;
  399. have_console = true;
  400. if (service_state != service_state_t::STARTING) {
  401. // We got the console but no longer want it.
  402. release_console();
  403. }
  404. else if (check_deps_started()) {
  405. all_deps_started();
  406. }
  407. else {
  408. // We got the console but can't use it yet.
  409. release_console();
  410. }
  411. }
  412. void service_record::started() noexcept
  413. {
  414. // If we start on console but don't keep it, release it now:
  415. if (have_console && !onstart_flags.runs_on_console) {
  416. bp_sys::tcsetpgrp(0, bp_sys::getpgrp());
  417. release_console();
  418. }
  419. log_service_started(get_name());
  420. service_state = service_state_t::STARTED;
  421. notify_listeners(service_event_t::STARTED);
  422. if (onstart_flags.rw_ready) {
  423. rootfs_is_rw();
  424. }
  425. if (onstart_flags.log_ready) {
  426. setup_external_log();
  427. }
  428. if (force_stop || desired_state == service_state_t::STOPPED) {
  429. // We must now stop.
  430. do_stop();
  431. return;
  432. }
  433. // Notify any dependents whose desired state is STARTED:
  434. for (auto dept : dependents) {
  435. if (dept->waiting_on) {
  436. dept->get_from()->dependency_started();
  437. dept->waiting_on = false;
  438. }
  439. }
  440. }
  441. void service_record::failed_to_start(bool depfailed, bool immediate_stop) noexcept
  442. {
  443. desired_state = service_state_t::STOPPED;
  444. if (waiting_for_console) {
  445. services->unqueue_console(this);
  446. waiting_for_console = false;
  447. }
  448. if (start_explicit) {
  449. start_explicit = false;
  450. release(false);
  451. }
  452. // Cancel start of dependents:
  453. for (auto & dept : dependents) {
  454. switch (dept->dep_type) {
  455. case dependency_type::REGULAR:
  456. case dependency_type::MILESTONE:
  457. // If REGULAR and STARTED, we can't have failed to start i.e. we must be started, so
  458. // we don't worry about that case. If MILESTONE and started the dependency is already
  459. // satisfied so again we don't need to do anything.
  460. if (dept->get_from()->service_state == service_state_t::STARTING) {
  461. dept->get_from()->prop_failure = true;
  462. services->add_prop_queue(dept->get_from());
  463. }
  464. break;
  465. case dependency_type::WAITS_FOR:
  466. case dependency_type::SOFT:
  467. case dependency_type::BEFORE:
  468. case dependency_type::AFTER:
  469. if (dept->waiting_on) {
  470. dept->waiting_on = false;
  471. dept->get_from()->dependency_started();
  472. }
  473. }
  474. // Always release now, so that our desired state will be STOPPED before we call
  475. // stopped() below (if we do so). Otherwise it may decide to restart us.
  476. if (dept->holding_acq) {
  477. dept->holding_acq = false;
  478. release(false);
  479. }
  480. }
  481. start_failed = true;
  482. log_service_failed(get_name(), depfailed);
  483. notify_listeners(service_event_t::FAILEDSTART);
  484. pinned_started = false;
  485. if (immediate_stop) {
  486. stopped();
  487. }
  488. }
  489. void service_record::unrecoverable_stop() noexcept
  490. {
  491. desired_state = service_state_t::STOPPED;
  492. forced_stop();
  493. }
  494. bool service_record::bring_up() noexcept
  495. {
  496. // default implementation: there is no process, so we are started.
  497. started();
  498. return true;
  499. }
  500. // Mark this and all dependent services to be force-stopped.
  501. void service_record::forced_stop() noexcept
  502. {
  503. if (service_state != service_state_t::STOPPED) {
  504. force_stop = true;
  505. if (!is_start_pinned()) {
  506. prop_stop = true;
  507. services->add_prop_queue(this);
  508. }
  509. }
  510. }
  511. void service_record::dependent_stopped() noexcept
  512. {
  513. if (service_state == service_state_t::STOPPING && waiting_for_deps) {
  514. services->add_transition_queue(this);
  515. }
  516. }
  517. void service_record::stop(bool bring_down) noexcept
  518. {
  519. // Stop; remove activation, and don't self-restart.
  520. if (start_explicit) {
  521. start_explicit = false;
  522. required_by--;
  523. }
  524. if (bring_down || required_by == 0) {
  525. // Set desired state to STOPPED, this will inhibit automatic restart (and will be
  526. // propagated to dependents)
  527. desired_state = service_state_t::STOPPED;
  528. }
  529. if (is_start_pinned()) {
  530. return;
  531. }
  532. // If our required_by count is 0, we should treat this as a full manual stop regardless
  533. if (required_by == 0) {
  534. bring_down = true;
  535. prop_release = !prop_require;
  536. if (prop_release) {
  537. services->add_prop_queue(this);
  538. }
  539. }
  540. if (bring_down && service_state != service_state_t::STOPPED) {
  541. // Note even if we are already STOPPING we should call do_stop for the case where we need
  542. // to interrupt any currently ongoing restart of dependents.
  543. stop_reason = stopped_reason_t::NORMAL;
  544. do_stop();
  545. }
  546. }
  547. bool service_record::restart() noexcept
  548. {
  549. // Re-start without affecting dependency links/activation. Hard dependents will be similarly
  550. // restarted.
  551. if (service_state == service_state_t::STARTED) {
  552. stop_reason = stopped_reason_t::NORMAL;
  553. force_stop = true;
  554. do_stop(true);
  555. return true;
  556. }
  557. // Wrong state
  558. return false;
  559. }
  560. void service_record::do_stop(bool with_restart) noexcept
  561. {
  562. // Called when we should definitely stop. We may need to restart afterwards, but we
  563. // won't know that for sure until the execution transition.
  564. // Note: to inhibit automatic restart, including restart due to dependent still requiring this
  565. // service, caller must first set desired_state to STOPPED
  566. if (is_start_pinned()) return;
  567. in_auto_restart = false;
  568. in_user_restart = false;
  569. // Will we restart? desired state of STOPPED inhibits auto-restart
  570. bool for_restart = with_restart || (auto_restart && desired_state == service_state_t::STARTED);
  571. bool restart_deps = with_restart;
  572. if (!with_restart && for_restart) {
  573. // auto restart - check for restarting too often
  574. for_restart = check_restart();
  575. in_auto_restart = for_restart;
  576. }
  577. // If we won't restart, release explicit activation:
  578. if (!for_restart) {
  579. if (start_explicit) {
  580. start_explicit = false;
  581. release(false);
  582. }
  583. }
  584. bool all_deps_stopped = stop_dependents(for_restart, restart_deps);
  585. if (service_state != service_state_t::STARTED) {
  586. if (service_state == service_state_t::STARTING) {
  587. // If waiting for a dependency, or waiting for the console, we can interrupt start. Otherwise,
  588. // we need to delegate to can_interrupt_start() (which can be overridden).
  589. if (!waiting_for_deps && !waiting_for_console) {
  590. if (!can_interrupt_start()) {
  591. // Well this is awkward: we're going to have to continue starting. We can stop once
  592. // we've reached the started state.
  593. return;
  594. }
  595. if (!interrupt_start()) {
  596. // Now wait for service startup to actually end; we don't need to handle it here.
  597. notify_listeners(service_event_t::STARTCANCELLED);
  598. return;
  599. }
  600. }
  601. else if (waiting_for_console) {
  602. services->unqueue_console(this);
  603. waiting_for_console = false;
  604. }
  605. // We must have had desired_state == STARTED.
  606. notify_listeners(service_event_t::STARTCANCELLED);
  607. // Reaching this point, we are starting interruptibly - so we
  608. // stop now (by falling through to below).
  609. }
  610. else {
  611. // If we're starting we need to wait for that to complete.
  612. // If we're already stopping/stopped there's nothing to do.
  613. return;
  614. }
  615. }
  616. service_state = service_state_t::STOPPING;
  617. waiting_for_deps = !all_deps_stopped;
  618. if (all_deps_stopped) {
  619. services->add_transition_queue(this);
  620. }
  621. }
  622. bool service_record::stop_check_dependents() noexcept
  623. {
  624. bool all_deps_stopped = true;
  625. for (auto dept : dependents) {
  626. // Note if the dependent is waiting on us, it must be restarting (since the
  627. // waiting_on flag gets cleared when we stop, and would only be set if the
  628. // service tries to restart). We can treat that as "stopped" for purposes of
  629. // checking whether we can transition to stopped state.
  630. if (dept->is_hard() && dept->holding_acq && !dept->waiting_on) {
  631. all_deps_stopped = false;
  632. break;
  633. }
  634. }
  635. return all_deps_stopped;
  636. }
  637. bool service_record::stop_dependents(bool for_restart, bool restart_deps) noexcept
  638. {
  639. // We are in either STARTED or STARTING states.
  640. bool all_deps_stopped = true;
  641. for (auto dept : dependents) {
  642. if (dept->is_hard()) {
  643. service_record *dep_from = dept->get_from();
  644. if (!dep_from->is_fundamentally_stopped()) {
  645. // Note we check *first* since if the dependent service is not stopped,
  646. // 1. We will issue a stop to it shortly and
  647. // 2. It will notify us when stopped, at which point the stop_check_dependents()
  648. // check is run anyway.
  649. all_deps_stopped = false;
  650. }
  651. if (force_stop) {
  652. // If this service is to be forcefully stopped, dependents must also be.
  653. if (desired_state == service_state_t::STOPPED) {
  654. // If our target state was forced to STOPPED, this is a failure
  655. dep_from->stop_reason = stopped_reason_t::DEPFAILED;
  656. dep_from->unrecoverable_stop();
  657. }
  658. else {
  659. dep_from->forced_stop();
  660. }
  661. }
  662. if (dep_from->get_state() != service_state_t::STOPPED) {
  663. if (desired_state == service_state_t::STOPPED) {
  664. if (dep_from->desired_state != service_state_t::STOPPED) {
  665. // if we don't want to restart, don't restart dependent
  666. dep_from->desired_state = service_state_t::STOPPED;
  667. if (dep_from->start_explicit) {
  668. dep_from->start_explicit = false;
  669. dep_from->release(true);
  670. }
  671. dep_from->prop_stop = true;
  672. services->add_prop_queue(dep_from);
  673. }
  674. }
  675. else if (restart_deps && dep_from->get_state() != service_state_t::STOPPING) {
  676. // restart dependent and propagate restart to all their hard dependents.
  677. dep_from->stop_reason = stopped_reason_t::DEPRESTART;
  678. dep_from->in_user_restart = true;
  679. dep_from->prop_stop = true;
  680. services->add_prop_queue(dep_from);
  681. }
  682. }
  683. }
  684. // Note that soft dependencies are retained if restarting, but otherwise
  685. // they are broken.
  686. else if (!for_restart) {
  687. if (dept->waiting_on) {
  688. // Note, milestone which is still waiting is considered a hard dependency and
  689. // is handled above. This is therefore a true soft dependency, and we can just
  690. // break the dependency link.
  691. dept->waiting_on = false;
  692. dept->get_from()->dependency_started();
  693. }
  694. if (dept->holding_acq) {
  695. dept->holding_acq = false;
  696. release(false);
  697. }
  698. }
  699. }
  700. return all_deps_stopped;
  701. }
  702. // All dependents have stopped; we can stop now, too. Only called when STOPPING.
  703. void service_record::bring_down() noexcept
  704. {
  705. stopped();
  706. }
  707. void service_record::pin_start() noexcept
  708. {
  709. if (!pinned_started) {
  710. if (!dept_pinned_started) {
  711. for (auto &dep : depends_on) {
  712. if (dep.is_hard() && !dep.get_to()->dept_pinned_started) {
  713. dep.get_to()->prop_pin_dpt = true;
  714. services->add_prop_queue(dep.get_to());
  715. }
  716. }
  717. }
  718. pinned_started = true;
  719. }
  720. }
  721. void service_record::unpin() noexcept
  722. {
  723. if (pinned_started) {
  724. pinned_started = false;
  725. if (dept_pinned_started) return;
  726. // unpin dependencies
  727. for (auto &dep : depends_on) {
  728. if (dep.is_hard() && dep.get_to()->dept_pinned_started) {
  729. dep.get_to()->prop_pin_dpt = true;
  730. services->add_prop_queue(dep.get_to());
  731. }
  732. }
  733. // We only need special handling here if service was in STARTED state
  734. if (service_state == service_state_t::STARTED) {
  735. // If any dependents are stopping, then force_stop should already be set.
  736. // If we reached required_by 0, we need to propagate release now (since it wasn't
  737. // propagated as it normally would be when we hit 0, due to the pin)
  738. if (required_by == 0) {
  739. prop_release = true;
  740. services->add_prop_queue(this);
  741. }
  742. if (desired_state == service_state_t::STOPPED || force_stop) {
  743. do_stop();
  744. services->process_queues();
  745. }
  746. }
  747. }
  748. if (pinned_stopped) {
  749. pinned_stopped = false;
  750. // We don't need to check state. If we're pinned stopped we can't be required and so desired
  751. // state should always be stopped.
  752. }
  753. }
  754. void service_record::queue_for_console() noexcept
  755. {
  756. waiting_for_console = true;
  757. services->append_console_queue(this);
  758. }
  759. void service_record::release_console() noexcept
  760. {
  761. have_console = false;
  762. services->pull_console_queue();
  763. }
  764. bool service_record::interrupt_start() noexcept
  765. {
  766. return true;
  767. }
  768. void service_set::service_active(service_record *sr) noexcept
  769. {
  770. active_services++;
  771. }
  772. void service_set::service_inactive(service_record *sr) noexcept
  773. {
  774. active_services--;
  775. }
  776. bool triggered_service::bring_up() noexcept
  777. {
  778. if (is_triggered) {
  779. started();
  780. }
  781. return true;
  782. }