service.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. #include <cstring>
  2. #include <cerrno>
  3. #include <sstream>
  4. #include <iterator>
  5. #include <memory>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include "service.h"
  11. #include "dinit-log.h"
  12. // from dinit.cc:
  13. void open_control_socket(struct ev_loop *loop);
  14. // Find the requested service by name
  15. static ServiceRecord * findService(const std::list<ServiceRecord *> & records,
  16. const char *name)
  17. {
  18. using std::list;
  19. list<ServiceRecord *>::const_iterator i = records.begin();
  20. for ( ; i != records.end(); i++ ) {
  21. if (strcmp((*i)->getServiceName(), name) == 0) {
  22. return *i;
  23. }
  24. }
  25. return (ServiceRecord *)0;
  26. }
  27. ServiceRecord * ServiceSet::findService(std::string name)
  28. {
  29. return ::findService(records, name.c_str());
  30. }
  31. void ServiceSet::startService(const char *name)
  32. {
  33. using namespace std;
  34. ServiceRecord *record = loadServiceRecord(name);
  35. record->start();
  36. }
  37. void ServiceSet::stopService(const std::string & name)
  38. {
  39. ServiceRecord *record = findService(name);
  40. if (record != nullptr) {
  41. record->stop();
  42. }
  43. }
  44. // Called when a service has actually stopped.
  45. void ServiceRecord::stopped()
  46. {
  47. logServiceStopped(service_name);
  48. service_state = ServiceState::STOPPED;
  49. force_stop = false;
  50. // Stop any dependencies whose desired state is STOPPED:
  51. for (sr_iter i = depends_on.begin(); i != depends_on.end(); i++) {
  52. (*i)->dependentStopped();
  53. }
  54. service_set->service_inactive(this);
  55. // TODO inform listeners.
  56. if (desired_state == ServiceState::STARTED) {
  57. // Desired state is "started".
  58. start();
  59. }
  60. }
  61. void ServiceRecord::process_child_callback(struct ev_loop *loop, ev_child *w, int revents)
  62. {
  63. ServiceRecord *sr = (ServiceRecord *) w->data;
  64. sr->pid = -1;
  65. ev_child_stop(ev_default_loop(EVFLAG_AUTO), &sr->child_listener);
  66. // Ok, for a process service, any process death which we didn't rig
  67. // ourselves is a bit... unexpected. Probably, the child died because
  68. // we asked it to (sr->service_state == STOPPING). But even if
  69. // we didn't, there's not much we can do.
  70. if (sr->service_type == ServiceType::PROCESS) {
  71. // TODO log non-zero rstatus?
  72. if (sr->service_state == ServiceState::STOPPING) {
  73. sr->stopped();
  74. }
  75. else {
  76. sr->forceStop();
  77. }
  78. if (sr->auto_restart && sr->service_set->get_auto_restart()) {
  79. sr->start();
  80. }
  81. }
  82. else { // SCRIPTED
  83. if (sr->service_state == ServiceState::STOPPING) {
  84. if (w->rstatus == 0) {
  85. sr->stopped();
  86. }
  87. else {
  88. // TODO
  89. // ??? failed to stop!
  90. // For now just pretend we stopped, so that any dependencies
  91. // can be stopped:
  92. sr->stopped();
  93. }
  94. }
  95. else { // STARTING
  96. if (w->rstatus == 0) {
  97. sr->started();
  98. }
  99. else {
  100. // failed to start
  101. sr->failed_to_start();
  102. }
  103. }
  104. }
  105. }
  106. void ServiceRecord::start()
  107. {
  108. if ((service_state == ServiceState::STARTING || service_state == ServiceState::STARTED)
  109. && desired_state == ServiceState::STOPPED) {
  110. // This service was starting, or started, but was set to be stopped.
  111. // Cancel the stop (and continue starting/running).
  112. // TODO any listeners waiting for stop should be notified of
  113. // its cancellation
  114. }
  115. auto old_desired_state = desired_state;
  116. desired_state = ServiceState::STARTED;
  117. if (service_state == ServiceState::STARTED || service_state == ServiceState::STARTING) {
  118. // We couldn't be started or starting unless all dependencies have
  119. // already started: so there's nothing left to do.
  120. return;
  121. }
  122. bool all_deps_started = true;
  123. // Ask dependencies to start, mark them as being waited on.
  124. for (sr_iter i = depends_on.begin(); i != depends_on.end(); ++i) {
  125. // Note, we cannot treat a dependency as started if its force_stop
  126. // flag is set.
  127. if ((*i)->service_state != ServiceState::STARTED || (*i)->force_stop) {
  128. all_deps_started = false;
  129. (*i)->start();
  130. }
  131. }
  132. if (old_desired_state != ServiceState::STARTED) {
  133. // This is a fresh start, so we mark all soft dependencies as 'waiting on' and ask them
  134. // to start:
  135. for (auto i = soft_deps.begin(); i != soft_deps.end(); ++i) {
  136. if (i->getTo()->service_state != ServiceState::STARTED) {
  137. all_deps_started = false;
  138. i->getTo()->start();
  139. i->waiting_on = true;
  140. }
  141. }
  142. }
  143. else {
  144. // This is (or at least may be) a notification that a dependency is ready; let's
  145. // just check them:
  146. for (auto i = soft_deps.begin(); i != soft_deps.end(); ++i) {
  147. ServiceRecord * to = i->getTo();
  148. if (i->waiting_on) {
  149. if ((to->desired_state != ServiceState::STARTED && to->service_state != ServiceState::STARTING) || to->service_state == ServiceState::STARTED) {
  150. // Service has either started or is no longer starting
  151. i->waiting_on = false;
  152. }
  153. else {
  154. all_deps_started = false;
  155. }
  156. }
  157. }
  158. }
  159. if (! all_deps_started) {
  160. // The dependencies will notify this service once they've started.
  161. return;
  162. }
  163. // Actually start this service.
  164. service_state = ServiceState::STARTING;
  165. service_set->service_active(this);
  166. if (service_type == ServiceType::PROCESS) {
  167. bool start_success = start_ps_process();
  168. if (start_success) {
  169. started();
  170. }
  171. else {
  172. failed_to_start();
  173. }
  174. }
  175. else if (service_type == ServiceType::SCRIPTED) {
  176. // Script-controlled service
  177. bool start_success = start_ps_process(std::vector<std::string>(1, "start"));
  178. if (! start_success) {
  179. failed_to_start();
  180. }
  181. }
  182. else {
  183. // "internal" service
  184. started();
  185. }
  186. }
  187. void ServiceRecord::started()
  188. {
  189. logServiceStarted(service_name);
  190. service_state = ServiceState::STARTED;
  191. // TODO - inform listeners
  192. if (onstart_flags.release_console) {
  193. log_to_console = false;
  194. }
  195. if (onstart_flags.rw_ready) {
  196. open_control_socket(ev_default_loop(EVFLAG_AUTO));
  197. }
  198. if (desired_state == ServiceState::STARTED) {
  199. // Start any dependents whose desired state is STARTED:
  200. for (auto i = dependents.begin(); i != dependents.end(); i++) {
  201. if ((*i)->desired_state == ServiceState::STARTED) {
  202. (*i)->start();
  203. }
  204. }
  205. for (auto i = soft_dpts.begin(); i != soft_dpts.end(); i++) {
  206. if ((*i)->getFrom()->desired_state == ServiceState::STARTED) {
  207. (*i)->getFrom()->start();
  208. }
  209. }
  210. }
  211. else {
  212. stop();
  213. }
  214. }
  215. void ServiceRecord::failed_to_start()
  216. {
  217. logServiceFailed(service_name);
  218. service_state = ServiceState::STOPPED;
  219. desired_state = ServiceState::STOPPED;
  220. service_set->service_inactive(this);
  221. // failure to start
  222. // Cancel start of dependents:
  223. for (sr_iter i = dependents.begin(); i != dependents.end(); i++) {
  224. if ((*i)->desired_state == ServiceState::STARTED) {
  225. (*i)->failed_dependency();
  226. }
  227. }
  228. for (auto i = soft_dpts.begin(); i != soft_dpts.end(); i++) {
  229. if ((*i)->getFrom()->desired_state == ServiceState::STARTED) {
  230. // We can send 'start', because this is only a soft dependency.
  231. // Our startup failure means that they don't have to wait for us.
  232. (*i)->getFrom()->start();
  233. }
  234. }
  235. }
  236. bool ServiceRecord::start_ps_process() noexcept
  237. {
  238. try {
  239. return start_ps_process(std::vector<std::string>());
  240. }
  241. catch (std::bad_alloc & bad_alloc_exc) {
  242. // TODO log error
  243. return false;
  244. }
  245. }
  246. // TODO this can currently throw std::bad_alloc, fix that (in the worst case,
  247. // return failure instead).
  248. bool ServiceRecord::start_ps_process(const std::vector<std::string> &pargs) noexcept
  249. {
  250. // In general, you can't tell whether fork/exec is successful. We use a pipe to communicate
  251. // success/failure from the child to the parent. The pipe is set CLOEXEC so a successful
  252. // exec closes the pipe, and the parent sees EOF. If the exec is unsuccessful, the errno
  253. // is written to the pipe, and the parent can read it.
  254. // TODO should NOT wait for the exec to succeed or fail here, as that could (when/if we allow
  255. // running child processes with lower priority) result in priority inversion.
  256. using std::vector;
  257. using std::string;
  258. int pipefd[2];
  259. if (pipe2(pipefd, O_CLOEXEC)) {
  260. // TODO log error
  261. return false;
  262. }
  263. // Set up the argument array and other data now (before fork), in case memory allocation fails.
  264. try {
  265. //auto argsv = std::vector<const char *>(num_args + pargs.size() + 1);
  266. auto argsv = std::vector<const char *>(num_args + pargs.size() + 1);
  267. auto args = argsv.data();
  268. int i;
  269. for (i = 0; i < num_args; i++) {
  270. args[i] = exec_arg_parts[i];
  271. }
  272. for (auto progarg : pargs) {
  273. args[i] = progarg.c_str();
  274. i++;
  275. }
  276. args[i] = nullptr;
  277. string logfile = this->logfile;
  278. if (logfile.length() == 0) {
  279. logfile = "/dev/null";
  280. }
  281. // TODO make sure pipefd's are not 0/1/2 (STDIN/OUT/ERR) - if they are, dup them
  282. // until they are not.
  283. pid_t forkpid = fork();
  284. if (forkpid == -1) {
  285. // TODO log error
  286. close(pipefd[0]);
  287. close(pipefd[1]);
  288. return false;
  289. }
  290. if (forkpid == 0) {
  291. // Child process. Must not allocate memory (or otherwise risk throwing any exception)
  292. // from here until exit().
  293. ev_default_destroy(); // won't need that on this side, free up fds.
  294. // Re-set stdin, stdout, stderr
  295. close(0); close(1); close(2);
  296. // TODO rethink this logic. If we open it at not-0, shouldn't we just dup it to 0?:
  297. if (open("/dev/null", O_RDONLY) == 0) {
  298. // stdin = 0. That's what we should have; proceed with opening
  299. // stdout and stderr.
  300. open(logfile.c_str(), O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
  301. dup2(1, 2);
  302. }
  303. execvp(exec_arg_parts[0], const_cast<char **>(args));
  304. // If we got here, the exec failed:
  305. int exec_status = errno;
  306. write(pipefd[1], &exec_status, sizeof(int));
  307. exit(0);
  308. }
  309. else {
  310. // Parent process
  311. close(pipefd[1]); // close the 'other end' fd
  312. int exec_status;
  313. if (read(pipefd[0], &exec_status, sizeof(int)) == 0) {
  314. // pipe closed; success
  315. pid = forkpid;
  316. // Add a process listener so we can detect when the
  317. // service stops
  318. ev_child_init(&child_listener, process_child_callback, pid, 0);
  319. child_listener.data = this;
  320. ev_child_start(ev_default_loop(EVFLAG_AUTO), &child_listener);
  321. close(pipefd[0]);
  322. return true;
  323. }
  324. else {
  325. // TODO log error
  326. close(pipefd[0]);
  327. return false;
  328. }
  329. }
  330. }
  331. catch (std::bad_alloc &bad_alloc_exc) {
  332. // TODO log error
  333. return false;
  334. }
  335. }
  336. // Mark this and all dependent services as force-stopped.
  337. void ServiceRecord::forceStop()
  338. {
  339. force_stop = true;
  340. stop();
  341. for (sr_iter i = dependents.begin(); i != dependents.end(); i++) {
  342. (*i)->forceStop();
  343. }
  344. // We don't want to force stop soft dependencies, however.
  345. }
  346. // A dependency of this service failed to start.
  347. void ServiceRecord::failed_dependency()
  348. {
  349. desired_state = ServiceState::STOPPED;
  350. // Presumably, we were starting. So now we're not.
  351. service_state = ServiceState::STOPPED;
  352. // Notify dependents of this service also
  353. for (auto i = dependents.begin(); i != dependents.end(); i++) {
  354. if ((*i)->desired_state == ServiceState::STARTED) {
  355. (*i)->failed_dependency();
  356. }
  357. }
  358. for (auto i = soft_dpts.begin(); i != soft_dpts.end(); i++) {
  359. if ((*i)->getFrom()->desired_state == ServiceState::STARTED) {
  360. // It's a soft dependency, so send them 'started' rather than
  361. // 'failed dep'.
  362. (*i)->getFrom()->started();
  363. }
  364. }
  365. }
  366. void ServiceRecord::dependentStopped()
  367. {
  368. if (service_state != ServiceState::STOPPED && (desired_state == ServiceState::STOPPED || force_stop)) {
  369. // Check the other dependents before we stop.
  370. if (stopCheckDependents()) {
  371. stopping();
  372. }
  373. }
  374. }
  375. void ServiceRecord::stop()
  376. {
  377. if ((service_state == ServiceState::STOPPING || service_state == ServiceState::STOPPED)
  378. && desired_state == ServiceState::STARTED) {
  379. // The service *was* stopped/stopping, but it was going to restart.
  380. // Now, we'll cancel the restart.
  381. // TODO inform listeners waiting for start of cancellation
  382. }
  383. if (desired_state == ServiceState::STOPPED) return;
  384. desired_state = ServiceState::STOPPED;
  385. if (service_state != ServiceState::STARTED) {
  386. if (service_state == ServiceState::STARTING) {
  387. // Well this is awkward: we're going to have to continue
  388. // starting, but we don't want any dependents to think that
  389. // they are still waiting to start.
  390. // Make sure they remain stopped:
  391. stopDependents();
  392. }
  393. // If we're starting we need to wait for that to complete.
  394. // If we're already stopping/stopped there's nothing to do.
  395. return;
  396. }
  397. // If we get here, we are in STARTED state; stop all dependents.
  398. if (stopCheckDependents()) {
  399. stopping();
  400. }
  401. }
  402. bool ServiceRecord::stopCheckDependents()
  403. {
  404. bool all_deps_stopped = true;
  405. for (sr_iter i = dependents.begin(); i != dependents.end(); ++i) {
  406. if ((*i)->service_state != ServiceState::STOPPED) {
  407. all_deps_stopped = false;
  408. break;
  409. }
  410. }
  411. return all_deps_stopped;
  412. }
  413. bool ServiceRecord::stopDependents()
  414. {
  415. bool all_deps_stopped = true;
  416. for (sr_iter i = dependents.begin(); i != dependents.end(); ++i) {
  417. if ((*i)->service_state != ServiceState::STOPPED) {
  418. all_deps_stopped = false;
  419. (*i)->stop();
  420. }
  421. }
  422. return all_deps_stopped;
  423. }
  424. // Dependency stopped or is stopping; we must stop too.
  425. void ServiceRecord::stopping()
  426. {
  427. service_state = ServiceState::STOPPING;
  428. if (service_type == ServiceType::PROCESS) {
  429. if (pid != -1) {
  430. // The process is still kicking on - must actually kill it.
  431. kill(pid, SIGTERM);
  432. // Now we wait; the rest is done in process_child_callback
  433. }
  434. else {
  435. // The process is already dead.
  436. stopped();
  437. }
  438. }
  439. else {
  440. // Scripted service.
  441. start_ps_process(std::vector<string>(1, "stop"));
  442. }
  443. }
  444. void ServiceSet::service_active(ServiceRecord *sr)
  445. {
  446. active_services++;
  447. }
  448. void ServiceSet::service_inactive(ServiceRecord *sr)
  449. {
  450. active_services--;
  451. }