service.cc 23 KB

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