service.cc 23 KB

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