baseproc-service.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. #include <cstring>
  2. #include <cstdlib>
  3. #include <sys/un.h>
  4. #include <sys/socket.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8. #include "dinit.h"
  9. #include "dinit-log.h"
  10. #include "dinit-socket.h"
  11. #include "proc-service.h"
  12. #include "baseproc-sys.h"
  13. /*
  14. * Base process implementation (base_process_service).
  15. *
  16. * See proc-service.h for interface documentation.
  17. */
  18. void base_process_service::do_smooth_recovery() noexcept
  19. {
  20. if (! restart_ps_process()) {
  21. unrecoverable_stop();
  22. services->process_queues();
  23. }
  24. }
  25. bool base_process_service::bring_up() noexcept
  26. {
  27. if (!open_socket()) {
  28. return false;
  29. }
  30. bool start_success;
  31. if (in_auto_restart) {
  32. start_success = restart_ps_process();
  33. }
  34. else {
  35. restart_interval_count = 0;
  36. start_success = start_ps_process(exec_arg_parts,
  37. onstart_flags.starts_on_console || onstart_flags.shares_console);
  38. // start_ps_process updates last_start_time, use it also for restart_interval_time:
  39. restart_interval_time = last_start_time;
  40. // Arm start timer. (For restarts, this is only done once the restart interval expires).
  41. if (start_success) {
  42. if (start_timeout != time_val(0,0)) {
  43. process_timer.arm_timer_rel(event_loop, start_timeout);
  44. waiting_stopstart_timer = true;
  45. }
  46. else if (waiting_stopstart_timer) {
  47. process_timer.stop_timer(event_loop);
  48. waiting_stopstart_timer = false;
  49. }
  50. }
  51. }
  52. return start_success;
  53. }
  54. void base_process_service::handle_unexpected_termination() noexcept
  55. {
  56. // unexpected termination, with possible restart
  57. stop_reason = stopped_reason_t::TERMINATED;
  58. // We want to circumvent the normal process of waiting for dependents to stop before we
  59. // attempt to restart, for two reasons:
  60. // 1) we can restart more quickly
  61. // 2) we can use the restart rate-limiting logic from restart_ps_process rather than
  62. // the usual start_ps_process (the usual bring-up).
  63. // But we need to issue a forced stop and process queues, to discover our eventual target
  64. // state (so we know whether we actually want to restart or not).
  65. // Note we can't call forced_stop() directly here, because we need to set in_auto_restart in
  66. // between do_stop() and processing queues (so that it is set correctly if restart occurs):
  67. force_stop = true;
  68. do_stop();
  69. in_auto_restart = true;
  70. services->process_queues();
  71. if (get_state() == service_state_t::STOPPING) {
  72. // We must be waiting for dependents;
  73. // If we're going to restart, we can kick that off now:
  74. if (get_target_state() == service_state_t::STARTED && !pinned_stopped) {
  75. initiate_start();
  76. services->process_queues();
  77. }
  78. }
  79. }
  80. bool base_process_service::start_ps_process(const std::vector<const char *> &cmd, bool on_console) noexcept
  81. {
  82. // In general, you can't tell whether fork/exec is successful. We use a pipe to communicate
  83. // success/failure from the child to the parent. The pipe is set CLOEXEC so a successful
  84. // exec closes the pipe, and the parent sees EOF. If the exec is unsuccessful, the errno
  85. // is written to the pipe, and the parent can read it.
  86. event_loop.get_time(last_start_time, clock_type::MONOTONIC);
  87. int pipefd[2];
  88. if (bp_sys::pipe2(pipefd, O_CLOEXEC)) {
  89. log(loglevel_t::ERROR, get_name(), ": can't create status check pipe: ", strerror(errno));
  90. return false;
  91. }
  92. const char * logfile = this->logfile.c_str();
  93. if (*logfile == 0) {
  94. logfile = "/dev/null";
  95. }
  96. bool child_status_registered = false;
  97. control_conn_t *control_conn = nullptr;
  98. int control_socket[2] = {-1, -1};
  99. int notify_pipe[2] = {-1, -1};
  100. bool have_notify = !notification_var.empty() || force_notification_fd != -1;
  101. ready_notify_watcher * rwatcher = have_notify ? get_ready_watcher() : nullptr;
  102. bool ready_watcher_registered = false;
  103. if (onstart_flags.pass_cs_fd) {
  104. if (dinit_socketpair(AF_UNIX, SOCK_STREAM, /* protocol */ 0, control_socket, SOCK_NONBLOCK)) {
  105. log(loglevel_t::ERROR, get_name(), ": can't create control socket: ", strerror(errno));
  106. goto out_p;
  107. }
  108. // Make the server side socket close-on-exec:
  109. int fdflags = bp_sys::fcntl(control_socket[0], F_GETFD);
  110. bp_sys::fcntl(control_socket[0], F_SETFD, fdflags | FD_CLOEXEC);
  111. try {
  112. control_conn = new control_conn_t(event_loop, services, control_socket[0]);
  113. }
  114. catch (std::exception &exc) {
  115. log(loglevel_t::ERROR, get_name(), ": can't launch process; out of memory");
  116. goto out_cs;
  117. }
  118. }
  119. if (have_notify) {
  120. // Create a notification pipe:
  121. if (bp_sys::pipe2(notify_pipe, 0) != 0) {
  122. log(loglevel_t::ERROR, get_name(), ": can't create notification pipe: ", strerror(errno));
  123. goto out_cs_h;
  124. }
  125. // Set the read side as close-on-exec:
  126. int fdflags = bp_sys::fcntl(notify_pipe[0], F_GETFD);
  127. bp_sys::fcntl(notify_pipe[0], F_SETFD, fdflags | FD_CLOEXEC);
  128. // add, but don't yet enable, readiness watcher:
  129. try {
  130. rwatcher->add_watch(event_loop, notify_pipe[0], dasynq::IN_EVENTS, false);
  131. ready_watcher_registered = true;
  132. }
  133. catch (std::exception &exc) {
  134. log(loglevel_t::ERROR, get_name(), ": can't add notification watch: ", exc.what());
  135. }
  136. }
  137. // Set up complete, now fork and exec:
  138. pid_t forkpid;
  139. try {
  140. child_status_listener.add_watch(event_loop, pipefd[0], dasynq::IN_EVENTS);
  141. child_status_registered = true;
  142. // We specify a high priority (i.e. low priority value) so that process termination is
  143. // handled early. This means we have always recorded that the process is terminated by the
  144. // time that we handle events that might otherwise cause us to signal the process, so we
  145. // avoid sending a signal to an invalid (and possibly recycled) process ID.
  146. forkpid = child_listener.fork(event_loop, reserved_child_watch, dasynq::DEFAULT_PRIORITY - 10);
  147. reserved_child_watch = true;
  148. }
  149. catch (std::exception &e) {
  150. log(loglevel_t::ERROR, get_name(), ": could not fork: ", e.what());
  151. goto out_cs_h;
  152. }
  153. if (forkpid == 0) {
  154. const char * working_dir_c = nullptr;
  155. if (! working_dir.empty()) working_dir_c = working_dir.c_str();
  156. after_fork(getpid());
  157. run_proc_params run_params{cmd.data(), working_dir_c, logfile, pipefd[1], run_as_uid, run_as_gid, rlimits};
  158. run_params.on_console = on_console;
  159. run_params.in_foreground = !onstart_flags.shares_console;
  160. run_params.csfd = control_socket[1];
  161. run_params.socket_fd = socket_fd;
  162. run_params.notify_fd = notify_pipe[1];
  163. run_params.force_notify_fd = force_notification_fd;
  164. run_params.notify_var = notification_var.c_str();
  165. run_params.env_file = env_file.c_str();
  166. #if SUPPORT_CGROUPS
  167. run_params.run_in_cgroup = run_in_cgroup.c_str();
  168. #endif
  169. run_child_proc(run_params);
  170. }
  171. else {
  172. // Parent process
  173. pid = forkpid;
  174. bp_sys::close(pipefd[1]); // close the 'other end' fd
  175. if (control_socket[1] != -1) bp_sys::close(control_socket[1]);
  176. if (notify_pipe[1] != -1) bp_sys::close(notify_pipe[1]);
  177. notification_fd = notify_pipe[0];
  178. waiting_for_execstat = true;
  179. return true;
  180. }
  181. // Failure exit:
  182. out_cs_h:
  183. if (child_status_registered) {
  184. child_status_listener.deregister(event_loop);
  185. }
  186. if (notify_pipe[0] != -1) bp_sys::close(notify_pipe[0]);
  187. if (notify_pipe[1] != -1) bp_sys::close(notify_pipe[1]);
  188. if (ready_watcher_registered) {
  189. rwatcher->deregister(event_loop);
  190. }
  191. if (onstart_flags.pass_cs_fd) {
  192. delete control_conn;
  193. out_cs:
  194. bp_sys::close(control_socket[0]);
  195. bp_sys::close(control_socket[1]);
  196. }
  197. out_p:
  198. bp_sys::close(pipefd[0]);
  199. bp_sys::close(pipefd[1]);
  200. return false;
  201. }
  202. base_process_service::base_process_service(service_set *sset, string name,
  203. service_type_t service_type_p, string &&command,
  204. const std::list<std::pair<unsigned,unsigned>> &command_offsets,
  205. const std::list<prelim_dep> &deplist_p)
  206. : service_record(sset, name, service_type_p, deplist_p), child_listener(this),
  207. child_status_listener(this), process_timer(this)
  208. {
  209. program_name = std::move(command);
  210. exec_arg_parts = separate_args(program_name, command_offsets);
  211. restart_interval_count = 0;
  212. restart_interval_time = {0, 0};
  213. process_timer.service = this;
  214. process_timer.add_timer(event_loop);
  215. // By default, allow a maximum of 3 restarts within 10.0 seconds:
  216. restart_interval.seconds() = 10;
  217. restart_interval.nseconds() = 0;
  218. max_restart_interval_count = 3;
  219. waiting_restart_timer = false;
  220. waiting_stopstart_timer = false;
  221. reserved_child_watch = false;
  222. tracking_child = false;
  223. }
  224. void base_process_service::do_restart() noexcept
  225. {
  226. // Actually perform process restart. We may be in smooth recovery (state = STARTED) or this may
  227. // be a regular restart.
  228. waiting_restart_timer = false;
  229. restart_interval_count++;
  230. auto service_state = get_state();
  231. if (!start_ps_process(exec_arg_parts, have_console || onstart_flags.shares_console)) {
  232. if (service_state == service_state_t::STARTING) {
  233. failed_to_start();
  234. }
  235. else {
  236. // smooth recovery failure
  237. unrecoverable_stop();
  238. }
  239. services->process_queues();
  240. }
  241. else {
  242. // started process successfully (at least as far as fork)
  243. if (start_timeout != time_val(0,0)) {
  244. process_timer.arm_timer_rel(event_loop, start_timeout);
  245. waiting_stopstart_timer = true;
  246. }
  247. }
  248. }
  249. bool base_process_service::restart_ps_process() noexcept
  250. {
  251. using time_val = dasynq::time_val;
  252. time_val current_time;
  253. event_loop.get_time(current_time, clock_type::MONOTONIC);
  254. if (max_restart_interval_count != 0) {
  255. // Check whether we're still in the most recent restart check interval:
  256. time_val int_diff = current_time - restart_interval_time;
  257. if (int_diff < restart_interval) {
  258. if (restart_interval_count >= max_restart_interval_count) {
  259. log(loglevel_t::ERROR, "Service ", get_name(), " restarting too quickly; stopping.");
  260. return false;
  261. }
  262. }
  263. else {
  264. restart_interval_time = current_time;
  265. restart_interval_count = 0;
  266. }
  267. }
  268. // Check if enough time has lapsed since the previous restart. If not, start a timer:
  269. time_val tdiff = current_time - last_start_time;
  270. if (restart_delay <= tdiff) {
  271. // > restart delay (normally 200ms)
  272. do_restart();
  273. }
  274. else {
  275. time_val timeout = restart_delay - tdiff;
  276. process_timer.arm_timer_rel(event_loop, timeout);
  277. waiting_restart_timer = true;
  278. }
  279. return true;
  280. }
  281. bool base_process_service::interrupt_start() noexcept
  282. {
  283. if (waiting_restart_timer) {
  284. process_timer.stop_timer(event_loop);
  285. waiting_restart_timer = false;
  286. return service_record::interrupt_start();
  287. }
  288. else {
  289. log(loglevel_t::WARN, "Interrupting start of service ", get_name(), " with pid ", pid,
  290. " (with SIGINT).");
  291. kill_pg(SIGINT);
  292. if (stop_timeout != time_val(0,0)) {
  293. process_timer.arm_timer_rel(event_loop, stop_timeout);
  294. waiting_stopstart_timer = true;
  295. }
  296. else if (waiting_stopstart_timer) {
  297. process_timer.stop_timer(event_loop);
  298. waiting_stopstart_timer = false;
  299. }
  300. set_state(service_state_t::STOPPING);
  301. return false;
  302. }
  303. }
  304. void base_process_service::kill_with_fire() noexcept
  305. {
  306. if (pid != -1) {
  307. log(loglevel_t::WARN, "Service ", get_name(), " with pid ", pid,
  308. " exceeded allowed stop time; killing.");
  309. kill_pg(SIGKILL);
  310. }
  311. }
  312. void base_process_service::kill_pg(int signo) noexcept
  313. {
  314. if (onstart_flags.signal_process_only) {
  315. bp_sys::kill(pid, signo);
  316. }
  317. else {
  318. pid_t pgid = bp_sys::getpgid(pid);
  319. if (pgid == -1) {
  320. // On some OSes (eg OpenBSD) we aren't allowed to get the pgid of a process in a different
  321. // session. If the process is in a different session, however, it must be a process group
  322. // leader and the pgid must equal the process id.
  323. pgid = pid;
  324. }
  325. bp_sys::kill(-pgid, signo);
  326. }
  327. }
  328. void base_process_service::timer_expired() noexcept
  329. {
  330. waiting_stopstart_timer = false;
  331. // Timer expires if:
  332. // We are stopping, including after having startup cancelled (stop timeout, state is STOPPING); We are
  333. // starting (start timeout, state is STARTING); We are waiting for restart timer before restarting,
  334. // including smooth recovery (restart timeout, state is STARTING or STARTED).
  335. if (get_state() == service_state_t::STOPPING) {
  336. kill_with_fire();
  337. }
  338. else if (pid != -1) {
  339. // Starting, start timed out.
  340. log(loglevel_t::WARN, "Service ", get_name(), " with pid ", pid,
  341. " exceeded allowed start time; cancelling.");
  342. interrupt_start();
  343. stop_reason = stopped_reason_t::TIMEDOUT;
  344. failed_to_start(false, false);
  345. //services->process_queues();
  346. }
  347. else {
  348. // STARTING / STARTED, and we have no pid: must be restarting (smooth recovery if STARTED)
  349. do_restart();
  350. }
  351. }
  352. void base_process_service::becoming_inactive() noexcept
  353. {
  354. if (socket_fd != -1) {
  355. close(socket_fd);
  356. socket_fd = -1;
  357. }
  358. }
  359. bool base_process_service::open_socket() noexcept
  360. {
  361. if (socket_path.empty() || socket_fd != -1) {
  362. // No socket, or already open
  363. return true;
  364. }
  365. const char * saddrname = socket_path.c_str();
  366. // Check the specified socket path
  367. struct stat stat_buf;
  368. if (stat(saddrname, &stat_buf) == 0) {
  369. if ((stat_buf.st_mode & S_IFSOCK) == 0) {
  370. // Not a socket
  371. log(loglevel_t::ERROR, get_name(), ": activation socket file exists (and is not a socket)");
  372. return false;
  373. }
  374. }
  375. else if (errno != ENOENT) {
  376. // Other error
  377. log(loglevel_t::ERROR, get_name(), ": error checking activation socket: ", strerror(errno));
  378. return false;
  379. }
  380. // Remove stale socket file (if it exists).
  381. // We won't test the return from unlink - if it fails other than due to ENOENT, we should get an
  382. // error when we try to create the socket anyway.
  383. unlink(saddrname);
  384. uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + socket_path.length() + 1;
  385. struct sockaddr_un * name = static_cast<sockaddr_un *>(malloc(sockaddr_size));
  386. if (name == nullptr) {
  387. log(loglevel_t::ERROR, get_name(), ": opening activation socket: out of memory");
  388. return false;
  389. }
  390. name->sun_family = AF_UNIX;
  391. strcpy(name->sun_path, saddrname);
  392. int sockfd = dinit_socket(AF_UNIX, SOCK_STREAM, 0, SOCK_NONBLOCK | SOCK_CLOEXEC);
  393. if (sockfd == -1) {
  394. log(loglevel_t::ERROR, get_name(), ": error creating activation socket: ", strerror(errno));
  395. free(name);
  396. return false;
  397. }
  398. if (bind(sockfd, (struct sockaddr *) name, sockaddr_size) == -1) {
  399. log(loglevel_t::ERROR, get_name(), ": error binding activation socket: ", strerror(errno));
  400. close(sockfd);
  401. free(name);
  402. return false;
  403. }
  404. free(name);
  405. // POSIX (1003.1, 2013) says that fchown and fchmod don't necessarily work on sockets. We have to
  406. // use chown and chmod instead.
  407. if (chown(saddrname, socket_uid, socket_gid)) {
  408. log(loglevel_t::ERROR, get_name(), ": error setting activation socket owner/group: ",
  409. strerror(errno));
  410. close(sockfd);
  411. return false;
  412. }
  413. if (chmod(saddrname, socket_perms) == -1) {
  414. log(loglevel_t::ERROR, get_name(), ": Error setting activation socket permissions: ",
  415. strerror(errno));
  416. close(sockfd);
  417. return false;
  418. }
  419. if (listen(sockfd, 128) == -1) { // 128 "seems reasonable".
  420. log(loglevel_t::ERROR, ": error listening on activation socket: ", strerror(errno));
  421. close(sockfd);
  422. return false;
  423. }
  424. socket_fd = sockfd;
  425. return true;
  426. }