service.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. bool will_restart = (desired_state == service_state_t::STARTED)
  46. && services->get_auto_restart();
  47. for (auto & dependency : depends_on) {
  48. // we signal dependencies in case they are waiting for us to stop:
  49. dependency.get_to()->dependent_stopped();
  50. }
  51. service_state = service_state_t::STOPPED;
  52. if (will_restart) {
  53. // Desired state is "started".
  54. restarting = true;
  55. start(false);
  56. }
  57. else {
  58. becoming_inactive();
  59. if (start_explicit) {
  60. // If we were explicitly started, our required_by count must be at least 1. Use
  61. // release() to correctly release, mark inactive and release dependencies.
  62. start_explicit = false;
  63. release();
  64. }
  65. else if (required_by == 0) {
  66. // This can only be the case if we didn't have start_explicit, since required_by would
  67. // otherwise by non-zero.
  68. prop_release = !prop_require;
  69. prop_require = false;
  70. services->add_prop_queue(this);
  71. services->service_inactive(this);
  72. }
  73. }
  74. // Start failure will have been logged already, only log if we are stopped for other reasons:
  75. if (! start_failed) {
  76. log_service_stopped(service_name);
  77. // If this service chains to another, start the other service now:
  78. if (! will_restart && ! start_on_completion.empty()) {
  79. try {
  80. auto chain_to = services->load_service(start_on_completion.c_str());
  81. chain_to->start();
  82. }
  83. catch (service_load_exc &sle) {
  84. log(loglevel_t::ERROR, "Couldn't chain to service ", start_on_completion, ": ",
  85. "couldn't load ", sle.service_name, ": ", sle.exc_description);
  86. }
  87. catch (std::bad_alloc &bae) {
  88. log(loglevel_t::ERROR, "Couldn't chain to service ", start_on_completion,
  89. ": Out of memory");
  90. }
  91. }
  92. }
  93. notify_listeners(service_event_t::STOPPED);
  94. }
  95. bool service_record::do_auto_restart() noexcept
  96. {
  97. if (auto_restart) {
  98. return services->get_auto_restart();
  99. }
  100. return false;
  101. }
  102. void service_record::require() noexcept
  103. {
  104. if (required_by++ == 0) {
  105. prop_require = !prop_release;
  106. prop_release = false;
  107. services->add_prop_queue(this);
  108. if (service_state != service_state_t::STARTING && service_state != service_state_t::STARTED) {
  109. prop_start = true;
  110. }
  111. }
  112. }
  113. void service_record::release(bool issue_stop) noexcept
  114. {
  115. if (--required_by == 0) {
  116. desired_state = service_state_t::STOPPED;
  117. // Can stop, and can release dependencies now. We don't need to issue a release if
  118. // the require was pending though:
  119. prop_release = !prop_require;
  120. prop_require = false;
  121. services->add_prop_queue(this);
  122. if (service_state == service_state_t::STOPPED) {
  123. services->service_inactive(this);
  124. }
  125. else if (issue_stop) {
  126. stop_reason = stopped_reason_t::NORMAL;
  127. do_stop();
  128. }
  129. }
  130. }
  131. void service_record::release_dependencies() noexcept
  132. {
  133. for (auto & dependency : depends_on) {
  134. service_record * dep_to = dependency.get_to();
  135. if (dependency.holding_acq) {
  136. // We must clear holding_acq before calling release, otherwise the dependency
  137. // may decide to stop, check this link and release itself a second time.
  138. dependency.holding_acq = false;
  139. dep_to->release();
  140. }
  141. }
  142. }
  143. void service_record::start(bool activate) noexcept
  144. {
  145. if (activate && ! start_explicit) {
  146. require();
  147. start_explicit = true;
  148. }
  149. if (desired_state == service_state_t::STARTED && service_state != service_state_t::STOPPED) return;
  150. bool was_active = service_state != service_state_t::STOPPED || desired_state != service_state_t::STOPPED;
  151. desired_state = service_state_t::STARTED;
  152. if (service_state != service_state_t::STOPPED) {
  153. // We're already starting/started, or we are stopping and need to wait for
  154. // that the complete.
  155. if (service_state != service_state_t::STOPPING || ! can_interrupt_stop()) {
  156. return;
  157. }
  158. // We're STOPPING, and that can be interrupted. Our dependencies might be STOPPING,
  159. // but if so they are waiting (for us), so they too can be instantly returned to
  160. // STARTING state.
  161. notify_listeners(service_event_t::STOPCANCELLED);
  162. }
  163. else if (! was_active) {
  164. services->service_active(this);
  165. }
  166. start_failed = false;
  167. start_skipped = false;
  168. service_state = service_state_t::STARTING;
  169. waiting_for_deps = true;
  170. if (start_check_dependencies()) {
  171. services->add_transition_queue(this);
  172. }
  173. }
  174. void service_record::do_propagation() noexcept
  175. {
  176. if (prop_require) {
  177. // Need to require all our dependencies
  178. for (auto & dep : depends_on) {
  179. dep.get_to()->require();
  180. dep.holding_acq = true;
  181. }
  182. prop_require = false;
  183. }
  184. if (prop_release) {
  185. release_dependencies();
  186. prop_release = false;
  187. }
  188. if (prop_failure) {
  189. prop_failure = false;
  190. stop_reason = stopped_reason_t::DEPFAILED;
  191. failed_to_start(true);
  192. }
  193. if (prop_start) {
  194. prop_start = false;
  195. start(false);
  196. }
  197. if (prop_stop) {
  198. prop_stop = false;
  199. do_stop();
  200. }
  201. }
  202. void service_record::execute_transition() noexcept
  203. {
  204. // state is STARTED with restarting set true if we are running a smooth recovery.
  205. if (service_state == service_state_t::STARTING || (service_state == service_state_t::STARTED
  206. && restarting)) {
  207. if (check_deps_started()) {
  208. all_deps_started();
  209. }
  210. }
  211. else if (service_state == service_state_t::STOPPING) {
  212. if (stop_check_dependents()) {
  213. waiting_for_deps = false;
  214. bring_down();
  215. }
  216. }
  217. }
  218. void service_record::do_start() noexcept
  219. {
  220. if (pinned_stopped) return;
  221. if (service_state != service_state_t::STARTING) {
  222. return;
  223. }
  224. service_state = service_state_t::STARTING;
  225. waiting_for_deps = true;
  226. // Ask dependencies to start, mark them as being waited on.
  227. if (check_deps_started()) {
  228. // Once all dependencies are started, we start properly:
  229. all_deps_started();
  230. }
  231. }
  232. void service_record::dependency_started() noexcept
  233. {
  234. // Note that we check for STARTED state here in case the service is in smooth recovery while pinned.
  235. // In that case it will wait for dependencies to start before restarting the process.
  236. if ((service_state == service_state_t::STARTING || service_state == service_state_t::STARTED)
  237. && waiting_for_deps) {
  238. services->add_transition_queue(this);
  239. }
  240. }
  241. bool service_record::start_check_dependencies() noexcept
  242. {
  243. bool all_deps_started = true;
  244. for (auto & dep : depends_on) {
  245. service_record * to = dep.get_to();
  246. if (to->service_state != service_state_t::STARTED) {
  247. if (to->service_state != service_state_t::STARTING) {
  248. to->prop_start = true;
  249. services->add_prop_queue(to);
  250. }
  251. dep.waiting_on = true;
  252. all_deps_started = false;
  253. }
  254. }
  255. return all_deps_started;
  256. }
  257. bool service_record::check_deps_started() noexcept
  258. {
  259. for (auto & dep : depends_on) {
  260. if (dep.waiting_on) {
  261. return false;
  262. }
  263. }
  264. return true;
  265. }
  266. void service_record::all_deps_started() noexcept
  267. {
  268. if (onstart_flags.starts_on_console && ! have_console) {
  269. queue_for_console();
  270. return;
  271. }
  272. waiting_for_deps = false;
  273. if (! can_proceed_to_start()) {
  274. waiting_for_deps = true;
  275. return;
  276. }
  277. bool start_success = bring_up();
  278. if (! start_success) {
  279. failed_to_start();
  280. }
  281. }
  282. void service_record::acquired_console() noexcept
  283. {
  284. waiting_for_console = false;
  285. have_console = true;
  286. if (service_state != service_state_t::STARTING) {
  287. // We got the console but no longer want it.
  288. release_console();
  289. }
  290. else if (check_deps_started()) {
  291. all_deps_started();
  292. }
  293. else {
  294. // We got the console but can't use it yet.
  295. release_console();
  296. }
  297. }
  298. void service_record::started() noexcept
  299. {
  300. // If we start on console but don't keep it, release it now:
  301. if (have_console && ! onstart_flags.runs_on_console) {
  302. bp_sys::tcsetpgrp(0, bp_sys::getpgrp());
  303. release_console();
  304. }
  305. log_service_started(get_name());
  306. service_state = service_state_t::STARTED;
  307. notify_listeners(service_event_t::STARTED);
  308. if (onstart_flags.rw_ready) {
  309. rootfs_is_rw();
  310. }
  311. if (onstart_flags.log_ready) {
  312. setup_external_log();
  313. }
  314. if (force_stop || desired_state == service_state_t::STOPPED) {
  315. // We must now stop.
  316. do_stop();
  317. return;
  318. }
  319. // Notify any dependents whose desired state is STARTED:
  320. for (auto dept : dependents) {
  321. dept->get_from()->dependency_started();
  322. dept->waiting_on = false;
  323. }
  324. }
  325. void service_record::failed_to_start(bool depfailed, bool immediate_stop) noexcept
  326. {
  327. if (waiting_for_console) {
  328. services->unqueue_console(this);
  329. waiting_for_console = false;
  330. }
  331. if (start_explicit) {
  332. start_explicit = false;
  333. release(false);
  334. }
  335. // Cancel start of dependents:
  336. for (auto & dept : dependents) {
  337. switch (dept->dep_type) {
  338. case dependency_type::REGULAR:
  339. case dependency_type::MILESTONE:
  340. if (dept->get_from()->service_state == service_state_t::STARTING) {
  341. dept->get_from()->prop_failure = true;
  342. services->add_prop_queue(dept->get_from());
  343. }
  344. break;
  345. case dependency_type::WAITS_FOR:
  346. case dependency_type::SOFT:
  347. if (dept->waiting_on) {
  348. dept->waiting_on = false;
  349. dept->get_from()->dependency_started();
  350. }
  351. }
  352. // Always release now, so that our desired state will be STOPPED before we call
  353. // stopped() below (if we do so). Otherwise it may decide to restart us.
  354. if (dept->holding_acq) {
  355. dept->holding_acq = false;
  356. release(false);
  357. }
  358. }
  359. start_failed = true;
  360. log_service_failed(get_name());
  361. notify_listeners(service_event_t::FAILEDSTART);
  362. if (immediate_stop) {
  363. stopped();
  364. }
  365. }
  366. bool service_record::bring_up() noexcept
  367. {
  368. // default implementation: there is no process, so we are started.
  369. started();
  370. return true;
  371. }
  372. // Mark this and all dependent services as force-stopped.
  373. void service_record::forced_stop() noexcept
  374. {
  375. if (service_state != service_state_t::STOPPED) {
  376. force_stop = true;
  377. if (! pinned_started) {
  378. prop_stop = true;
  379. services->add_prop_queue(this);
  380. }
  381. }
  382. }
  383. void service_record::dependent_stopped() noexcept
  384. {
  385. if (service_state == service_state_t::STOPPING && waiting_for_deps) {
  386. services->add_transition_queue(this);
  387. }
  388. }
  389. void service_record::stop(bool bring_down) noexcept
  390. {
  391. if (start_explicit) {
  392. start_explicit = false;
  393. release();
  394. }
  395. // If it's a manual bring-down, we'll also break holds from waits-for dependencies, to avoid
  396. // bouncing back up again -- but only if all holds are from waits-for dependencies.
  397. if (bring_down) {
  398. if (std::all_of(dependents.begin(), dependents.end(), [](service_dep * x) {
  399. return x->dep_type == dependency_type::WAITS_FOR || x->dep_type == dependency_type::SOFT; }))
  400. {
  401. for (auto dept : dependents) {
  402. if (dept->waiting_on) {
  403. dept->waiting_on = false;
  404. dept->get_from()->dependency_started();
  405. }
  406. if (dept->holding_acq) {
  407. dept->holding_acq = false;
  408. // release without issuing stop, since we issue stop if necessary below
  409. release(false);
  410. }
  411. }
  412. }
  413. }
  414. if (bring_down && service_state != service_state_t::STOPPED
  415. && service_state != service_state_t::STOPPING) {
  416. stop_reason = stopped_reason_t::NORMAL;
  417. do_stop();
  418. }
  419. }
  420. void service_record::do_stop() noexcept
  421. {
  422. // A service that does actually stop for any reason should have its explicit activation released, unless
  423. // it will restart:
  424. if (start_explicit && ! do_auto_restart()) {
  425. start_explicit = false;
  426. release(false);
  427. }
  428. bool all_deps_stopped = stop_dependents();
  429. if (service_state != service_state_t::STARTED) {
  430. if (service_state == service_state_t::STARTING) {
  431. // If waiting for a dependency, or waiting for the console, we can interrupt start. Otherwise,
  432. // we need to delegate to can_interrupt_start() (which can be overridden).
  433. if (! waiting_for_deps && ! waiting_for_console) {
  434. if (! can_interrupt_start()) {
  435. // Well this is awkward: we're going to have to continue starting. We can stop once
  436. // we've reached the started state.
  437. return;
  438. }
  439. if (! interrupt_start()) {
  440. // Now wait for service startup to actually end; we don't need to handle it here.
  441. notify_listeners(service_event_t::STARTCANCELLED);
  442. return;
  443. }
  444. }
  445. else if (waiting_for_console) {
  446. services->unqueue_console(this);
  447. waiting_for_console = false;
  448. }
  449. // We must have had desired_state == STARTED.
  450. notify_listeners(service_event_t::STARTCANCELLED);
  451. // Reaching this point, we are starting interruptibly - so we
  452. // stop now (by falling through to below).
  453. }
  454. else {
  455. // If we're starting we need to wait for that to complete.
  456. // If we're already stopping/stopped there's nothing to do.
  457. return;
  458. }
  459. }
  460. if (pinned_started) return;
  461. service_state = service_state_t::STOPPING;
  462. waiting_for_deps = true;
  463. if (all_deps_stopped) {
  464. services->add_transition_queue(this);
  465. }
  466. }
  467. bool service_record::stop_check_dependents() noexcept
  468. {
  469. bool all_deps_stopped = true;
  470. for (auto dept : dependents) {
  471. if (dept->dep_type == dependency_type::REGULAR && ! dept->get_from()->is_stopped()) {
  472. all_deps_stopped = false;
  473. break;
  474. }
  475. }
  476. return all_deps_stopped;
  477. }
  478. bool service_record::stop_dependents() noexcept
  479. {
  480. bool all_deps_stopped = true;
  481. for (auto dept : dependents) {
  482. if (dept->dep_type == dependency_type::REGULAR ||
  483. (dept->dep_type == dependency_type::MILESTONE &&
  484. dept->get_from()->service_state != service_state_t::STARTED)) {
  485. if (! dept->get_from()->is_stopped()) {
  486. // Note we check *first* since if the dependent service is not stopped,
  487. // 1. We will issue a stop to it shortly and
  488. // 2. It will notify us when stopped, at which point the stop_check_dependents()
  489. // check is run anyway.
  490. all_deps_stopped = false;
  491. }
  492. if (force_stop) {
  493. // If this service is to be forcefully stopped, dependents must also be.
  494. dept->get_from()->forced_stop();
  495. }
  496. dept->get_from()->prop_stop = true;
  497. services->add_prop_queue(dept->get_from());
  498. }
  499. else {
  500. // waits-for or soft dependency:
  501. if (dept->waiting_on) {
  502. dept->waiting_on = false;
  503. dept->get_from()->dependency_started();
  504. }
  505. if (dept->holding_acq && !auto_restart) {
  506. dept->holding_acq = false;
  507. // release without issuing stop, since we should be called only when this
  508. // service is already stopped/stopping:
  509. release(false);
  510. }
  511. }
  512. }
  513. return all_deps_stopped;
  514. }
  515. // All dependents have stopped; we can stop now, too. Only called when STOPPING.
  516. void service_record::bring_down() noexcept
  517. {
  518. waiting_for_deps = false;
  519. stopped();
  520. }
  521. void service_record::unpin() noexcept
  522. {
  523. if (pinned_started) {
  524. pinned_started = false;
  525. if (desired_state == service_state_t::STOPPED || force_stop) {
  526. do_stop();
  527. services->process_queues();
  528. }
  529. }
  530. if (pinned_stopped) {
  531. pinned_stopped = false;
  532. if (desired_state == service_state_t::STARTED) {
  533. do_start();
  534. services->process_queues();
  535. }
  536. }
  537. }
  538. void service_record::queue_for_console() noexcept
  539. {
  540. waiting_for_console = true;
  541. services->append_console_queue(this);
  542. }
  543. void service_record::release_console() noexcept
  544. {
  545. have_console = false;
  546. services->pull_console_queue();
  547. }
  548. bool service_record::interrupt_start() noexcept
  549. {
  550. return true;
  551. }
  552. void service_set::service_active(service_record *sr) noexcept
  553. {
  554. active_services++;
  555. }
  556. void service_set::service_inactive(service_record *sr) noexcept
  557. {
  558. active_services--;
  559. }