service.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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 && !pinned_started) {
  135. notify_listeners(service_event_t::STARTCANCELLED);
  136. }
  137. }
  138. desired_state = service_state_t::STOPPED;
  139. if (pinned_started) 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. dep.get_to()->require();
  195. dep.holding_acq = true;
  196. }
  197. prop_require = false;
  198. }
  199. if (prop_release) {
  200. release_dependencies();
  201. prop_release = false;
  202. }
  203. if (prop_failure) {
  204. prop_failure = false;
  205. stop_reason = stopped_reason_t::DEPFAILED;
  206. failed_to_start(true);
  207. }
  208. if (prop_start) {
  209. prop_start = false;
  210. do_start();
  211. }
  212. if (prop_stop) {
  213. prop_stop = false;
  214. do_stop();
  215. }
  216. }
  217. void service_record::execute_transition() noexcept
  218. {
  219. if (service_state == service_state_t::STARTING) {
  220. if (check_deps_started()) {
  221. all_deps_started();
  222. }
  223. }
  224. else if (service_state == service_state_t::STOPPING) {
  225. if (stop_check_dependents()) {
  226. waiting_for_deps = false;
  227. bring_down();
  228. }
  229. }
  230. }
  231. void service_record::do_start() noexcept
  232. {
  233. bool was_active = service_state != service_state_t::STOPPED;
  234. desired_state = service_state_t::STARTED;
  235. if (pinned_stopped) {
  236. if (!was_active) {
  237. failed_to_start(false, false);
  238. }
  239. return;
  240. }
  241. // re-attach any soft dependents, now that we are starting again
  242. if (!was_active) {
  243. for (auto dept : dependents) {
  244. if (!dept->is_hard()) {
  245. service_state_t dept_state = dept->get_from()->service_state;
  246. if (!dept->holding_acq
  247. && (dept_state == service_state_t::STARTED || dept_state == service_state_t::STARTING)) {
  248. dept->holding_acq = true;
  249. ++required_by;
  250. }
  251. }
  252. }
  253. }
  254. if (was_active) {
  255. // We're already starting/started, or we are stopping and need to wait for
  256. // that the complete.
  257. if (service_state != service_state_t::STOPPING) {
  258. return;
  259. }
  260. if (! can_interrupt_stop()) {
  261. return;
  262. }
  263. // We're STOPPING, and that can be interrupted. Our dependencies might be STOPPING,
  264. // but if so they are waiting (for us), so they too can be instantly returned to
  265. // STARTING state.
  266. notify_listeners(service_event_t::STOPCANCELLED);
  267. }
  268. else { // !was_active
  269. services->service_active(this);
  270. prop_require = !prop_release;
  271. prop_release = false;
  272. if (prop_require) {
  273. services->add_prop_queue(this);
  274. }
  275. }
  276. initiate_start();
  277. }
  278. void service_record::dependency_started() noexcept
  279. {
  280. // Note that we check for STARTED state here in case the service is in smooth recovery while pinned.
  281. // In that case it will wait for dependencies to start before restarting the process.
  282. if ((service_state == service_state_t::STARTING || service_state == service_state_t::STARTED)
  283. && waiting_for_deps) {
  284. services->add_transition_queue(this);
  285. }
  286. }
  287. bool service_record::start_check_dependencies() noexcept
  288. {
  289. bool all_deps_started = true;
  290. for (auto & dep : depends_on) {
  291. service_record * to = dep.get_to();
  292. if (to->service_state != service_state_t::STARTED) {
  293. // We don't actually have to issue a start; the require will do that
  294. dep.waiting_on = true;
  295. all_deps_started = false;
  296. }
  297. }
  298. return all_deps_started;
  299. }
  300. bool service_record::check_deps_started() noexcept
  301. {
  302. for (auto & dep : depends_on) {
  303. if (dep.waiting_on) {
  304. return false;
  305. }
  306. }
  307. return true;
  308. }
  309. void service_record::all_deps_started() noexcept
  310. {
  311. if (onstart_flags.starts_on_console && ! have_console) {
  312. queue_for_console();
  313. return;
  314. }
  315. waiting_for_deps = false;
  316. if (! can_proceed_to_start()) {
  317. waiting_for_deps = true;
  318. return;
  319. }
  320. if (!bring_up()) {
  321. failed_to_start();
  322. }
  323. }
  324. void service_record::acquired_console() noexcept
  325. {
  326. waiting_for_console = false;
  327. have_console = true;
  328. if (service_state != service_state_t::STARTING) {
  329. // We got the console but no longer want it.
  330. release_console();
  331. }
  332. else if (check_deps_started()) {
  333. all_deps_started();
  334. }
  335. else {
  336. // We got the console but can't use it yet.
  337. release_console();
  338. }
  339. }
  340. void service_record::started() noexcept
  341. {
  342. // If we start on console but don't keep it, release it now:
  343. if (have_console && ! onstart_flags.runs_on_console) {
  344. bp_sys::tcsetpgrp(0, bp_sys::getpgrp());
  345. release_console();
  346. }
  347. log_service_started(get_name());
  348. service_state = service_state_t::STARTED;
  349. notify_listeners(service_event_t::STARTED);
  350. if (onstart_flags.rw_ready) {
  351. rootfs_is_rw();
  352. }
  353. if (onstart_flags.log_ready) {
  354. setup_external_log();
  355. }
  356. if (force_stop || desired_state == service_state_t::STOPPED) {
  357. // We must now stop.
  358. do_stop();
  359. return;
  360. }
  361. // Notify any dependents whose desired state is STARTED:
  362. for (auto dept : dependents) {
  363. dept->get_from()->dependency_started();
  364. dept->waiting_on = false;
  365. }
  366. }
  367. void service_record::failed_to_start(bool depfailed, bool immediate_stop) noexcept
  368. {
  369. if (waiting_for_console) {
  370. services->unqueue_console(this);
  371. waiting_for_console = false;
  372. }
  373. if (start_explicit) {
  374. start_explicit = false;
  375. release(false);
  376. }
  377. // Cancel start of dependents:
  378. for (auto & dept : dependents) {
  379. switch (dept->dep_type) {
  380. case dependency_type::REGULAR:
  381. case dependency_type::MILESTONE:
  382. // If REGULAR and STARTED, we can't have failed to start i.e. we must be started, so
  383. // we don't worry about that case. If MILESTONE and started the dependency is already
  384. // satisfied so again we don't need to do anything.
  385. if (dept->get_from()->service_state == service_state_t::STARTING) {
  386. dept->get_from()->prop_failure = true;
  387. services->add_prop_queue(dept->get_from());
  388. }
  389. break;
  390. case dependency_type::WAITS_FOR:
  391. case dependency_type::SOFT:
  392. if (dept->waiting_on) {
  393. dept->waiting_on = false;
  394. dept->get_from()->dependency_started();
  395. }
  396. }
  397. // Always release now, so that our desired state will be STOPPED before we call
  398. // stopped() below (if we do so). Otherwise it may decide to restart us.
  399. if (dept->holding_acq) {
  400. dept->holding_acq = false;
  401. release(false);
  402. }
  403. }
  404. start_failed = true;
  405. log_service_failed(get_name());
  406. notify_listeners(service_event_t::FAILEDSTART);
  407. pinned_started = false;
  408. if (immediate_stop) {
  409. stopped();
  410. }
  411. }
  412. void service_record::unrecoverable_stop() noexcept
  413. {
  414. desired_state = service_state_t::STOPPED;
  415. forced_stop();
  416. }
  417. bool service_record::bring_up() noexcept
  418. {
  419. // default implementation: there is no process, so we are started.
  420. started();
  421. return true;
  422. }
  423. // Mark this and all dependent services to be force-stopped.
  424. void service_record::forced_stop() noexcept
  425. {
  426. if (service_state != service_state_t::STOPPED) {
  427. force_stop = true;
  428. if (! pinned_started) {
  429. prop_stop = true;
  430. services->add_prop_queue(this);
  431. }
  432. }
  433. }
  434. void service_record::dependent_stopped() noexcept
  435. {
  436. if (service_state == service_state_t::STOPPING && waiting_for_deps) {
  437. services->add_transition_queue(this);
  438. }
  439. }
  440. void service_record::stop(bool bring_down) noexcept
  441. {
  442. // Stop; remove activation, and don't self-restart.
  443. if (start_explicit) {
  444. start_explicit = false;
  445. required_by--;
  446. }
  447. if (bring_down || required_by == 0) {
  448. // Set desired state to STOPPED, this will inhibit automatic restart (and will be
  449. // propagated to dependents)
  450. desired_state = service_state_t::STOPPED;
  451. }
  452. if (pinned_started) {
  453. return;
  454. }
  455. // If our required_by count is 0, we should treat this as a full manual stop regardless
  456. if (required_by == 0) {
  457. bring_down = true;
  458. prop_release = !prop_require;
  459. if (prop_release) {
  460. services->add_prop_queue(this);
  461. }
  462. }
  463. if (bring_down && service_state != service_state_t::STOPPED
  464. && service_state != service_state_t::STOPPING) {
  465. stop_reason = stopped_reason_t::NORMAL;
  466. do_stop();
  467. }
  468. }
  469. bool service_record::restart() noexcept
  470. {
  471. // Re-start without affecting dependency links/activation.
  472. if (service_state == service_state_t::STARTED) {
  473. stop_reason = stopped_reason_t::NORMAL;
  474. force_stop = true;
  475. do_stop(true);
  476. return true;
  477. }
  478. // Wrong state
  479. return false;
  480. }
  481. void service_record::do_stop(bool with_restart) noexcept
  482. {
  483. // Called when we should definitely stop. We may need to restart afterwards, but we
  484. // won't know that for sure until the execution transition.
  485. if (pinned_started) return;
  486. // Will we restart? desired state of STOPPED inhibits auto-restart
  487. bool for_restart = with_restart || (auto_restart && desired_state == service_state_t::STARTED);
  488. // If we won't restart, release explicit activation:
  489. if (!for_restart) {
  490. if (start_explicit) {
  491. start_explicit = false;
  492. release(false);
  493. }
  494. }
  495. bool all_deps_stopped = stop_dependents(for_restart);
  496. if (service_state != service_state_t::STARTED) {
  497. if (service_state == service_state_t::STARTING) {
  498. // If waiting for a dependency, or waiting for the console, we can interrupt start. Otherwise,
  499. // we need to delegate to can_interrupt_start() (which can be overridden).
  500. if (! waiting_for_deps && ! waiting_for_console) {
  501. if (! can_interrupt_start()) {
  502. // Well this is awkward: we're going to have to continue starting. We can stop once
  503. // we've reached the started state.
  504. return;
  505. }
  506. if (! interrupt_start()) {
  507. // Now wait for service startup to actually end; we don't need to handle it here.
  508. notify_listeners(service_event_t::STARTCANCELLED);
  509. return;
  510. }
  511. }
  512. else if (waiting_for_console) {
  513. services->unqueue_console(this);
  514. waiting_for_console = false;
  515. }
  516. // We must have had desired_state == STARTED.
  517. notify_listeners(service_event_t::STARTCANCELLED);
  518. // Reaching this point, we are starting interruptibly - so we
  519. // stop now (by falling through to below).
  520. }
  521. else {
  522. // If we're starting we need to wait for that to complete.
  523. // If we're already stopping/stopped there's nothing to do.
  524. return;
  525. }
  526. }
  527. service_state = service_state_t::STOPPING;
  528. waiting_for_deps = !all_deps_stopped;
  529. if (all_deps_stopped) {
  530. services->add_transition_queue(this);
  531. }
  532. }
  533. bool service_record::stop_check_dependents() noexcept
  534. {
  535. bool all_deps_stopped = true;
  536. for (auto dept : dependents) {
  537. // Note if the dependent is waiting on us, it must be restarting (since the
  538. // waiting_on flag gets cleared when we stop, and would only be set if the
  539. // service tries to restart). We can treat that as "stopped" for purposes of
  540. // checking whether we can transition to stopped state.
  541. if (dept->is_hard() && dept->holding_acq && !dept->waiting_on) {
  542. all_deps_stopped = false;
  543. break;
  544. }
  545. }
  546. return all_deps_stopped;
  547. }
  548. bool service_record::stop_dependents(bool for_restart) noexcept
  549. {
  550. // We are in either STARTED or STARTING states.
  551. bool all_deps_stopped = true;
  552. for (auto dept : dependents) {
  553. if (!dept->holding_acq) {
  554. continue;
  555. }
  556. if (dept->is_hard()) {
  557. service_record *dep_from = dept->get_from();
  558. if (!dep_from->is_fundamentally_stopped()) {
  559. // Note we check *first* since if the dependent service is not stopped,
  560. // 1. We will issue a stop to it shortly and
  561. // 2. It will notify us when stopped, at which point the stop_check_dependents()
  562. // check is run anyway.
  563. all_deps_stopped = false;
  564. }
  565. if (force_stop) {
  566. // If this service is to be forcefully stopped, dependents must also be.
  567. dep_from->forced_stop();
  568. }
  569. if (dep_from->get_state() != service_state_t::STOPPED
  570. && dep_from->get_state() != service_state_t::STOPPING) {
  571. dep_from->prop_stop = true;
  572. if (desired_state == service_state_t::STOPPED) {
  573. // if we don't want to restart, don't restart dependent
  574. dep_from->desired_state = service_state_t::STOPPED;
  575. if (dep_from->start_explicit) {
  576. dep_from->start_explicit = false;
  577. dep_from->release(true);
  578. }
  579. }
  580. services->add_prop_queue(dep_from);
  581. }
  582. }
  583. // Note that soft dependencies are retained if restarting, but otherwise
  584. // they are broken.
  585. else if (!for_restart && !dept->is_hard()) {
  586. if (dept->waiting_on) {
  587. // Note, milestone which is still waiting is considered a hard dependency and
  588. // is handled above. This is therefore a true soft dependency, and we can just
  589. // break the dependency link.
  590. dept->waiting_on = false;
  591. dept->get_from()->dependency_started();
  592. dept->holding_acq = false;
  593. release(false);
  594. }
  595. else {
  596. dept->holding_acq = false;
  597. release(false);
  598. }
  599. }
  600. }
  601. return all_deps_stopped;
  602. }
  603. // All dependents have stopped; we can stop now, too. Only called when STOPPING.
  604. void service_record::bring_down() noexcept
  605. {
  606. waiting_for_deps = false;
  607. stopped();
  608. }
  609. void service_record::unpin() noexcept
  610. {
  611. if (pinned_started) {
  612. pinned_started = false;
  613. // We only need special handling here if service was in STARTED state
  614. if (service_state == service_state_t::STARTED) {
  615. // If any dependents are stopping, then force_stop should already be set.
  616. // If we reached required_by 0, we need to propagate release now (since it wasn't
  617. // propagated as it normally would be when we hit 0, due to the pin)
  618. if (required_by == 0) {
  619. prop_release = true;
  620. services->add_prop_queue(this);
  621. }
  622. if (desired_state == service_state_t::STOPPED || force_stop) {
  623. do_stop();
  624. services->process_queues();
  625. }
  626. }
  627. }
  628. if (pinned_stopped) {
  629. pinned_stopped = false;
  630. // We don't need to check state. If we're pinned stopped we can't be required and so desired
  631. // state should always be stopped.
  632. }
  633. }
  634. void service_record::queue_for_console() noexcept
  635. {
  636. waiting_for_console = true;
  637. services->append_console_queue(this);
  638. }
  639. void service_record::release_console() noexcept
  640. {
  641. have_console = false;
  642. services->pull_console_queue();
  643. }
  644. bool service_record::interrupt_start() noexcept
  645. {
  646. return true;
  647. }
  648. void service_set::service_active(service_record *sr) noexcept
  649. {
  650. active_services++;
  651. }
  652. void service_set::service_inactive(service_record *sr) noexcept
  653. {
  654. active_services--;
  655. }