service.cc 23 KB

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