service.cc 26 KB

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