baseproc-service.cc 16 KB

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