baseproc-service.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. services->process_queues();
  70. if (get_state() == service_state_t::STOPPING) {
  71. // We must be waiting for dependents;
  72. // If we're going to restart, we can kick that off now:
  73. if (get_target_state() == service_state_t::STARTED && !pinned_stopped) {
  74. initiate_start();
  75. services->process_queues();
  76. }
  77. }
  78. }
  79. bool base_process_service::start_ps_process(const std::vector<const char *> &cmd, bool on_console) noexcept
  80. {
  81. // In general, you can't tell whether fork/exec is successful. We use a pipe to communicate
  82. // success/failure from the child to the parent. The pipe is set CLOEXEC so a successful
  83. // exec closes the pipe, and the parent sees EOF. If the exec is unsuccessful, the errno
  84. // is written to the pipe, and the parent can read it.
  85. event_loop.get_time(last_start_time, clock_type::MONOTONIC);
  86. int pipefd[2];
  87. if (bp_sys::pipe2(pipefd, O_CLOEXEC)) {
  88. log(loglevel_t::ERROR, get_name(), ": can't create status check pipe: ", strerror(errno));
  89. return false;
  90. }
  91. int input_fd;
  92. if (!get_input_fd(&input_fd)) {
  93. return false;
  94. }
  95. const char * logfile = this->logfile.c_str();
  96. if (this->log_type == log_type_id::LOGFILE || this->log_type == log_type_id::NONE) {
  97. // Note: if log_type == NONE, logfile should be empty.
  98. if (*logfile == 0) {
  99. logfile = "/dev/null";
  100. }
  101. }
  102. else /* log_type_id::BUFFER or ::PIPE */ {
  103. if (this->log_output_fd == -1) {
  104. int logfd[2];
  105. // Note: we set CLOEXEC on the file descriptors here; when the output file descriptor is dup'd
  106. // to stdout, this will be effectively removed for the output end
  107. if (bp_sys::pipe2(logfd, O_CLOEXEC)) {
  108. log(loglevel_t::ERROR, get_name(), ": can't create output pipe: ", strerror(errno));
  109. goto out_p;
  110. }
  111. this->log_input_fd = logfd[0];
  112. this->log_output_fd = logfd[1];
  113. if (this->log_type == log_type_id::BUFFER) {
  114. try {
  115. this->log_output_listener.add_watch(event_loop, logfd[0], dasynq::IN_EVENTS,
  116. false /* not enabled */);
  117. }
  118. catch (...) {
  119. log(loglevel_t::ERROR, get_name(), ": can't add output watch (insufficient resources)");
  120. bp_sys::close(this->log_input_fd);
  121. bp_sys::close(this->log_output_fd);
  122. this->log_input_fd = -1;
  123. this->log_output_fd = -1;
  124. goto out_p;
  125. }
  126. }
  127. }
  128. // (More is done below, after we have performed additional setup)
  129. }
  130. {
  131. bool child_status_registered = false;
  132. control_conn_t *control_conn = nullptr;
  133. int control_socket[2] = {-1, -1};
  134. int notify_pipe[2] = {-1, -1};
  135. bool have_notify = !notification_var.empty() || force_notification_fd != -1;
  136. ready_notify_watcher * rwatcher = have_notify ? get_ready_watcher() : nullptr;
  137. bool ready_watcher_registered = false;
  138. if (onstart_flags.pass_cs_fd) {
  139. if (dinit_socketpair(AF_UNIX, SOCK_STREAM, /* protocol */ 0, control_socket, SOCK_NONBLOCK)) {
  140. log(loglevel_t::ERROR, get_name(), ": can't create control socket: ", strerror(errno));
  141. goto out_lfd;
  142. }
  143. // Make the server side socket close-on-exec:
  144. int fdflags = bp_sys::fcntl(control_socket[0], F_GETFD);
  145. bp_sys::fcntl(control_socket[0], F_SETFD, fdflags | FD_CLOEXEC);
  146. try {
  147. control_conn = new control_conn_t(event_loop, services, control_socket[0]);
  148. }
  149. catch (std::exception &exc) {
  150. log(loglevel_t::ERROR, get_name(), ": can't launch process; out of memory");
  151. goto out_cs;
  152. }
  153. }
  154. if (have_notify) {
  155. // Create a notification pipe:
  156. if (bp_sys::pipe2(notify_pipe, 0) != 0) {
  157. log(loglevel_t::ERROR, get_name(), ": can't create notification pipe: ", strerror(errno));
  158. goto out_cs_h;
  159. }
  160. // Set the read side as close-on-exec:
  161. int fdflags = bp_sys::fcntl(notify_pipe[0], F_GETFD);
  162. bp_sys::fcntl(notify_pipe[0], F_SETFD, fdflags | FD_CLOEXEC);
  163. // add, but don't yet enable, readiness watcher:
  164. try {
  165. rwatcher->add_watch(event_loop, notify_pipe[0], dasynq::IN_EVENTS, false);
  166. ready_watcher_registered = true;
  167. }
  168. catch (std::exception &exc) {
  169. log(loglevel_t::ERROR, get_name(), ": can't add notification watch: ", exc.what());
  170. goto out_cs_h;
  171. }
  172. }
  173. if (log_type == log_type_id::BUFFER) {
  174. // Set watcher enabled if space in buffer
  175. if (log_buf_size > 0) {
  176. // Append a "restarted" message to buffer contents
  177. const char *restarting_msg = "\n(dinit: note: service restarted)\n";
  178. unsigned restarting_msg_len = strlen(restarting_msg);
  179. bool trailing_nl = log_buffer[log_buf_size - 1] == '\n';
  180. if (trailing_nl) {
  181. ++restarting_msg; // trim leading newline
  182. --restarting_msg_len;
  183. }
  184. if (log_buf_size + restarting_msg_len >= log_buf_max) {
  185. goto skip_enable_log_watch;
  186. }
  187. if (!ensure_log_buffer_backing(log_buf_size + restarting_msg_len)) {
  188. goto skip_enable_log_watch;
  189. }
  190. memcpy(log_buffer.data() + log_buf_size, restarting_msg, restarting_msg_len);
  191. log_buf_size += restarting_msg_len;
  192. }
  193. log_output_listener.set_enabled(event_loop, true);
  194. }
  195. skip_enable_log_watch: ;
  196. // Set up complete, now fork and exec:
  197. pid_t forkpid;
  198. try {
  199. child_status_listener.add_watch(event_loop, pipefd[0], dasynq::IN_EVENTS);
  200. child_status_registered = true;
  201. // We specify a high priority (i.e. low priority value) so that process termination is
  202. // handled early. This means we have always recorded that the process is terminated by the
  203. // time that we handle events that might otherwise cause us to signal the process, so we
  204. // avoid sending a signal to an invalid (and possibly recycled) process ID.
  205. forkpid = child_listener.fork(event_loop, reserved_child_watch, dasynq::DEFAULT_PRIORITY - 10);
  206. reserved_child_watch = true;
  207. }
  208. catch (std::exception &e) {
  209. log(loglevel_t::ERROR, get_name(), ": could not fork: ", e.what());
  210. goto out_cs_h;
  211. }
  212. if (forkpid == 0) {
  213. const char * working_dir_c = service_dsc_dir;
  214. if (!working_dir.empty()) working_dir_c = working_dir.c_str();
  215. after_fork(getpid());
  216. run_proc_params run_params{cmd.data(), working_dir_c, logfile, pipefd[1], run_as_uid, run_as_gid, rlimits};
  217. run_params.on_console = on_console;
  218. run_params.in_foreground = !onstart_flags.shares_console;
  219. run_params.unmask_sigint = onstart_flags.unmask_intr;
  220. run_params.csfd = control_socket[1];
  221. run_params.socket_fd = socket_fd;
  222. run_params.notify_fd = notify_pipe[1];
  223. run_params.force_notify_fd = force_notification_fd;
  224. run_params.notify_var = notification_var.c_str();
  225. run_params.env_file = env_file.c_str();
  226. run_params.output_fd = log_output_fd;
  227. run_params.input_fd = input_fd;
  228. #if SUPPORT_CGROUPS
  229. run_params.run_in_cgroup = run_in_cgroup.c_str();
  230. #endif
  231. run_child_proc(run_params);
  232. }
  233. else {
  234. // Parent process
  235. pid = forkpid;
  236. bp_sys::close(pipefd[1]); // close the 'other end' fd
  237. if (control_socket[1] != -1) bp_sys::close(control_socket[1]);
  238. if (notify_pipe[1] != -1) bp_sys::close(notify_pipe[1]);
  239. notification_fd = notify_pipe[0];
  240. waiting_for_execstat = true;
  241. return true;
  242. }
  243. // Failure exit:
  244. out_cs_h:
  245. if (child_status_registered) {
  246. child_status_listener.deregister(event_loop);
  247. }
  248. if (notify_pipe[0] != -1) bp_sys::close(notify_pipe[0]);
  249. if (notify_pipe[1] != -1) bp_sys::close(notify_pipe[1]);
  250. if (ready_watcher_registered) {
  251. rwatcher->deregister(event_loop);
  252. }
  253. if (onstart_flags.pass_cs_fd) {
  254. delete control_conn;
  255. out_cs:
  256. bp_sys::close(control_socket[0]);
  257. bp_sys::close(control_socket[1]);
  258. }
  259. }
  260. out_lfd:
  261. if (log_input_fd != -1) {
  262. log_output_listener.deregister(event_loop);
  263. bp_sys::close(log_input_fd);
  264. bp_sys::close(log_output_fd);
  265. log_input_fd = -1;
  266. log_output_fd = -1;
  267. }
  268. out_p:
  269. bp_sys::close(pipefd[0]);
  270. bp_sys::close(pipefd[1]);
  271. return false;
  272. }
  273. base_process_service::base_process_service(service_set *sset, string name,
  274. service_type_t service_type_p, ha_string &&command,
  275. const std::list<std::pair<unsigned,unsigned>> &command_offsets,
  276. const std::list<prelim_dep> &deplist_p)
  277. : service_record(sset, name, service_type_p, deplist_p), child_listener(this),
  278. child_status_listener(this), process_timer(this), log_output_listener(this)
  279. {
  280. program_name = std::move(command);
  281. exec_arg_parts = separate_args(program_name, command_offsets);
  282. restart_interval_count = 0;
  283. restart_interval_time = {0, 0};
  284. process_timer.service = this;
  285. process_timer.add_timer(event_loop);
  286. // By default, allow a maximum of 3 restarts within 10.0 seconds:
  287. restart_interval.seconds() = 10;
  288. restart_interval.nseconds() = 0;
  289. max_restart_interval_count = 3;
  290. waiting_restart_timer = false;
  291. waiting_stopstart_timer = false;
  292. reserved_child_watch = false;
  293. tracking_child = false;
  294. }
  295. void base_process_service::do_restart() noexcept
  296. {
  297. // Actually perform process restart. We may be in smooth recovery (state = STARTED) or this may
  298. // be a regular restart.
  299. waiting_restart_timer = false;
  300. auto service_state = get_state();
  301. if (!start_ps_process(exec_arg_parts, have_console || onstart_flags.shares_console)) {
  302. if (service_state == service_state_t::STARTING) {
  303. failed_to_start();
  304. }
  305. else {
  306. // smooth recovery failure
  307. unrecoverable_stop();
  308. }
  309. services->process_queues();
  310. }
  311. else {
  312. // started process successfully (at least as far as fork)
  313. if (start_timeout != time_val(0,0)) {
  314. process_timer.arm_timer_rel(event_loop, start_timeout);
  315. waiting_stopstart_timer = true;
  316. }
  317. }
  318. }
  319. bool base_process_service::restart_ps_process() noexcept
  320. {
  321. using time_val = dasynq::time_val;
  322. time_val current_time;
  323. event_loop.get_time(current_time, clock_type::MONOTONIC);
  324. // Check if enough time has lapsed since the previous restart. If not, start a timer:
  325. time_val tdiff = current_time - last_start_time;
  326. if (restart_delay <= tdiff) {
  327. // > restart delay (normally 200ms)
  328. do_restart();
  329. }
  330. else {
  331. time_val timeout = restart_delay - tdiff;
  332. process_timer.arm_timer_rel(event_loop, timeout);
  333. waiting_restart_timer = true;
  334. }
  335. return true;
  336. }
  337. bool base_process_service::interrupt_start() noexcept
  338. {
  339. if (waiting_restart_timer) {
  340. process_timer.stop_timer(event_loop);
  341. waiting_restart_timer = false;
  342. return service_record::interrupt_start();
  343. }
  344. else {
  345. log(loglevel_t::WARN, "Interrupting start of service ", get_name(), " with pid ", pid,
  346. " (with SIGINT).");
  347. kill_pg(SIGINT);
  348. if (stop_timeout != time_val(0,0)) {
  349. process_timer.arm_timer_rel(event_loop, stop_timeout);
  350. waiting_stopstart_timer = true;
  351. }
  352. else if (waiting_stopstart_timer) {
  353. process_timer.stop_timer(event_loop);
  354. waiting_stopstart_timer = false;
  355. }
  356. set_state(service_state_t::STOPPING);
  357. return false;
  358. }
  359. }
  360. void base_process_service::kill_with_fire() noexcept
  361. {
  362. if (pid != -1) {
  363. log(loglevel_t::WARN, "Service ", get_name(), " with pid ", pid,
  364. " exceeded allowed stop time; killing.");
  365. kill_pg(SIGKILL);
  366. }
  367. }
  368. void base_process_service::kill_pg(int signo) noexcept
  369. {
  370. if (onstart_flags.signal_process_only) {
  371. bp_sys::kill(pid, signo);
  372. }
  373. else {
  374. pid_t pgid = bp_sys::getpgid(pid);
  375. if (pgid == -1) {
  376. // On some OSes (eg OpenBSD) we aren't allowed to get the pgid of a process in a different
  377. // session. If the process is in a different session, however, it must be a process group
  378. // leader and the pgid must equal the process id.
  379. pgid = pid;
  380. }
  381. bp_sys::kill(-pgid, signo);
  382. }
  383. }
  384. void base_process_service::timer_expired() noexcept
  385. {
  386. waiting_stopstart_timer = false;
  387. // Timer expires if:
  388. // We are stopping, including after having startup cancelled (stop timeout, state is STOPPING); We are
  389. // starting (start timeout, state is STARTING); We are waiting for restart timer before restarting,
  390. // including smooth recovery (restart timeout, state is STARTING or STARTED).
  391. if (get_state() == service_state_t::STOPPING) {
  392. kill_with_fire();
  393. }
  394. else if (pid != -1) {
  395. // Starting, start timed out.
  396. log(loglevel_t::WARN, "Service ", get_name(), " with pid ", pid,
  397. " exceeded allowed start time; cancelling.");
  398. interrupt_start();
  399. stop_reason = stopped_reason_t::TIMEDOUT;
  400. failed_to_start(false, false);
  401. }
  402. else {
  403. // STARTING / STARTED, and we have no pid: must be restarting (smooth recovery if STARTED)
  404. do_restart();
  405. }
  406. }
  407. void base_process_service::becoming_inactive() noexcept
  408. {
  409. if (socket_fd != -1) {
  410. close(socket_fd);
  411. socket_fd = -1;
  412. }
  413. }
  414. bool base_process_service::open_socket() noexcept
  415. {
  416. if (socket_path.empty() || socket_fd != -1) {
  417. // No socket, or already open
  418. return true;
  419. }
  420. const char * saddrname = socket_path.c_str();
  421. // Check the specified socket path
  422. struct stat stat_buf;
  423. if (stat(saddrname, &stat_buf) == 0) {
  424. if ((stat_buf.st_mode & S_IFSOCK) == 0) {
  425. // Not a socket
  426. log(loglevel_t::ERROR, get_name(), ": activation socket file exists (and is not a socket)");
  427. return false;
  428. }
  429. }
  430. else if (errno != ENOENT) {
  431. // Other error
  432. log(loglevel_t::ERROR, get_name(), ": error checking activation socket: ", strerror(errno));
  433. return false;
  434. }
  435. // Remove stale socket file (if it exists).
  436. // We won't test the return from unlink - if it fails other than due to ENOENT, we should get an
  437. // error when we try to create the socket anyway.
  438. unlink(saddrname);
  439. uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + socket_path.length() + 1;
  440. struct sockaddr_un * name = static_cast<sockaddr_un *>(malloc(sockaddr_size));
  441. if (name == nullptr) {
  442. log(loglevel_t::ERROR, get_name(), ": opening activation socket: out of memory");
  443. return false;
  444. }
  445. name->sun_family = AF_UNIX;
  446. strcpy(name->sun_path, saddrname);
  447. int sockfd = dinit_socket(AF_UNIX, SOCK_STREAM, 0, SOCK_NONBLOCK | SOCK_CLOEXEC);
  448. if (sockfd == -1) {
  449. log(loglevel_t::ERROR, get_name(), ": error creating activation socket: ", strerror(errno));
  450. free(name);
  451. return false;
  452. }
  453. if (bind(sockfd, (struct sockaddr *) name, sockaddr_size) == -1) {
  454. log(loglevel_t::ERROR, get_name(), ": error binding activation socket: ", strerror(errno));
  455. close(sockfd);
  456. free(name);
  457. return false;
  458. }
  459. free(name);
  460. // POSIX (1003.1, 2013) says that fchown and fchmod don't necessarily work on sockets. We have to
  461. // use chown and chmod instead.
  462. if (chown(saddrname, socket_uid, socket_gid)) {
  463. log(loglevel_t::ERROR, get_name(), ": error setting activation socket owner/group: ",
  464. strerror(errno));
  465. close(sockfd);
  466. return false;
  467. }
  468. if (chmod(saddrname, socket_perms) == -1) {
  469. log(loglevel_t::ERROR, get_name(), ": Error setting activation socket permissions: ",
  470. strerror(errno));
  471. close(sockfd);
  472. return false;
  473. }
  474. if (listen(sockfd, 128) == -1) { // 128 "seems reasonable".
  475. log(loglevel_t::ERROR, ": error listening on activation socket: ", strerror(errno));
  476. close(sockfd);
  477. return false;
  478. }
  479. socket_fd = sockfd;
  480. return true;
  481. }
  482. bool base_process_service::ensure_log_buffer_backing(unsigned new_size) noexcept
  483. {
  484. // Note: we manage capacity manually to avoid it exceeding maximum
  485. if (log_buffer.size() < new_size) {
  486. if (log_buffer.capacity() < new_size) {
  487. try {
  488. unsigned new_capacity = std::max((unsigned)log_buffer.capacity() * 2, new_size);
  489. new_capacity = std::min(new_capacity, log_buf_max);
  490. log_buffer.reserve(new_capacity);
  491. log_buffer.resize(new_capacity);
  492. }
  493. catch (std::bad_alloc &badalloc) {
  494. log(loglevel_t::WARN, get_name(), ": cannot increase log buffer; out-of-memory");
  495. return false;
  496. }
  497. }
  498. else {
  499. log_buffer.resize(new_size);
  500. }
  501. }
  502. return true;
  503. }