service.cc 20 KB

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