service.cc 27 KB

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