spawn_server.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "spawn.h"
  3. static uv_loop_t *loop;
  4. static uv_pipe_t server_pipe;
  5. static int server_shutdown = 0;
  6. static uv_thread_t thread;
  7. /* spawn outstanding execution structure */
  8. static avl_tree_lock spawn_outstanding_exec_tree;
  9. static char prot_buffer[MAX_COMMAND_LENGTH];
  10. static unsigned prot_buffer_len = 0;
  11. struct spawn_execution_info {
  12. avl_t avl;
  13. void *handle;
  14. int exit_status;
  15. pid_t pid;
  16. struct spawn_execution_info *next;
  17. };
  18. int spawn_exec_compare(void *a, void *b)
  19. {
  20. struct spawn_execution_info *spwna = a, *spwnb = b;
  21. if (spwna->pid < spwnb->pid) return -1;
  22. if (spwna->pid > spwnb->pid) return 1;
  23. return 0;
  24. }
  25. /* wake up waiter thread to reap the spawned processes */
  26. static uv_mutex_t wait_children_mutex;
  27. static uv_cond_t wait_children_cond;
  28. static uint8_t spawned_processes;
  29. static struct spawn_execution_info *child_waited_list;
  30. static uv_async_t child_waited_async;
  31. static inline struct spawn_execution_info *dequeue_child_waited_list(void)
  32. {
  33. struct spawn_execution_info *exec_info;
  34. uv_mutex_lock(&wait_children_mutex);
  35. if (NULL == child_waited_list) {
  36. exec_info = NULL;
  37. } else {
  38. exec_info = child_waited_list;
  39. child_waited_list = exec_info->next;
  40. }
  41. uv_mutex_unlock(&wait_children_mutex);
  42. return exec_info;
  43. }
  44. static inline void enqueue_child_waited_list(struct spawn_execution_info *exec_info)
  45. {
  46. uv_mutex_lock(&wait_children_mutex);
  47. exec_info->next = child_waited_list;
  48. child_waited_list = exec_info;
  49. uv_mutex_unlock(&wait_children_mutex);
  50. }
  51. static void after_pipe_write(uv_write_t *req, int status)
  52. {
  53. (void)status;
  54. #ifdef SPAWN_DEBUG
  55. fprintf(stderr, "SERVER %s called status=%d\n", __func__, status);
  56. #endif
  57. void **data = req->data;
  58. freez(data[0]);
  59. freez(data[1]);
  60. freez(data);
  61. }
  62. static void child_waited_async_cb(uv_async_t *async_handle)
  63. {
  64. uv_buf_t *writebuf;
  65. int ret;
  66. struct spawn_execution_info *exec_info;
  67. struct write_context *write_ctx;
  68. (void)async_handle;
  69. while (NULL != (exec_info = dequeue_child_waited_list())) {
  70. write_ctx = mallocz(sizeof(*write_ctx));
  71. void **data = callocz(2, sizeof(void *));
  72. writebuf = callocz(2, sizeof(uv_buf_t));
  73. data[0] = write_ctx;
  74. data[1] = writebuf;
  75. write_ctx->write_req.data = data;
  76. write_ctx->header.opcode = SPAWN_PROT_CMD_EXIT_STATUS;
  77. write_ctx->header.handle = exec_info->handle;
  78. write_ctx->exit_status.exec_exit_status = exec_info->exit_status;
  79. writebuf[0] = uv_buf_init((char *) &write_ctx->header, sizeof(write_ctx->header));
  80. writebuf[1] = uv_buf_init((char *) &write_ctx->exit_status, sizeof(write_ctx->exit_status));
  81. #ifdef SPAWN_DEBUG
  82. fprintf(stderr, "SERVER %s SPAWN_PROT_CMD_EXIT_STATUS\n", __func__);
  83. #endif
  84. ret = uv_write(&write_ctx->write_req, (uv_stream_t *) &server_pipe, writebuf, 2, after_pipe_write);
  85. fatal_assert(ret == 0);
  86. freez(exec_info);
  87. }
  88. }
  89. static void wait_children(void *arg)
  90. {
  91. siginfo_t i;
  92. struct spawn_execution_info tmp, *exec_info;
  93. avl_t *ret_avl;
  94. (void)arg;
  95. while (!server_shutdown) {
  96. uv_mutex_lock(&wait_children_mutex);
  97. while (!spawned_processes) {
  98. uv_cond_wait(&wait_children_cond, &wait_children_mutex);
  99. }
  100. spawned_processes = 0;
  101. uv_mutex_unlock(&wait_children_mutex);
  102. while (!server_shutdown) {
  103. i.si_pid = 0;
  104. if (waitid(P_ALL, (id_t) 0, &i, WEXITED) == -1) {
  105. if (errno != ECHILD)
  106. fprintf(stderr, "SPAWN: Failed to wait: %s\n", strerror(errno));
  107. break;
  108. }
  109. if (i.si_pid == 0) {
  110. fprintf(stderr, "SPAWN: No child exited.\n");
  111. break;
  112. }
  113. #ifdef SPAWN_DEBUG
  114. fprintf(stderr, "SPAWN: Successfully waited for pid:%d.\n", (int) i.si_pid);
  115. #endif
  116. fatal_assert(CLD_EXITED == i.si_code);
  117. tmp.pid = (pid_t)i.si_pid;
  118. while (NULL == (ret_avl = avl_remove_lock(&spawn_outstanding_exec_tree, (avl_t *)&tmp))) {
  119. fprintf(stderr,
  120. "SPAWN: race condition detected, waiting for child process %d to be indexed.\n",
  121. (int)tmp.pid);
  122. (void)sleep_usec(10000); /* 10 msec */
  123. }
  124. exec_info = (struct spawn_execution_info *)ret_avl;
  125. exec_info->exit_status = i.si_status;
  126. enqueue_child_waited_list(exec_info);
  127. /* wake up event loop */
  128. fatal_assert(0 == uv_async_send(&child_waited_async));
  129. }
  130. }
  131. }
  132. void spawn_protocol_execute_command(void *handle, char *command_to_run, uint16_t command_length)
  133. {
  134. uv_buf_t *writebuf;
  135. int ret;
  136. avl_t *avl_ret;
  137. struct spawn_execution_info *exec_info;
  138. struct write_context *write_ctx;
  139. write_ctx = mallocz(sizeof(*write_ctx));
  140. void **data = callocz(2, sizeof(void *));
  141. writebuf = callocz(2, sizeof(uv_buf_t));
  142. data[0] = write_ctx;
  143. data[1] = writebuf;
  144. write_ctx->write_req.data = data;
  145. command_to_run[command_length] = '\0';
  146. #ifdef SPAWN_DEBUG
  147. fprintf(stderr, "SPAWN: executing command '%s'\n", command_to_run);
  148. #endif
  149. if (netdata_spawn(command_to_run, &write_ctx->spawn_result.exec_pid)) {
  150. fprintf(stderr, "SPAWN: Cannot spawn(\"%s\", \"r\").\n", command_to_run);
  151. write_ctx->spawn_result.exec_pid = 0;
  152. } else { /* successfully spawned command */
  153. write_ctx->spawn_result.exec_run_timestamp = now_realtime_sec();
  154. /* record it for when the process finishes execution */
  155. exec_info = mallocz(sizeof(*exec_info));
  156. exec_info->handle = handle;
  157. exec_info->pid = write_ctx->spawn_result.exec_pid;
  158. avl_ret = avl_insert_lock(&spawn_outstanding_exec_tree, (avl_t *)exec_info);
  159. fatal_assert(avl_ret == (avl_t *)exec_info);
  160. /* wake up the thread that blocks waiting for processes to exit */
  161. uv_mutex_lock(&wait_children_mutex);
  162. spawned_processes = 1;
  163. uv_cond_signal(&wait_children_cond);
  164. uv_mutex_unlock(&wait_children_mutex);
  165. }
  166. write_ctx->header.opcode = SPAWN_PROT_SPAWN_RESULT;
  167. write_ctx->header.handle = handle;
  168. writebuf[0] = uv_buf_init((char *)&write_ctx->header, sizeof(write_ctx->header));
  169. writebuf[1] = uv_buf_init((char *)&write_ctx->spawn_result, sizeof(write_ctx->spawn_result));
  170. #ifdef SPAWN_DEBUG
  171. fprintf(stderr, "SERVER %s SPAWN_PROT_SPAWN_RESULT\n", __func__);
  172. #endif
  173. ret = uv_write(&write_ctx->write_req, (uv_stream_t *)&server_pipe, writebuf, 2, after_pipe_write);
  174. fatal_assert(ret == 0);
  175. }
  176. static void server_parse_spawn_protocol(unsigned source_len, char *source)
  177. {
  178. unsigned required_len;
  179. struct spawn_prot_header *header;
  180. struct spawn_prot_exec_cmd *payload;
  181. uint16_t command_length;
  182. while (source_len) {
  183. required_len = sizeof(*header);
  184. if (prot_buffer_len < required_len)
  185. copy_to_prot_buffer(prot_buffer, &prot_buffer_len, required_len - prot_buffer_len, &source, &source_len);
  186. if (prot_buffer_len < required_len)
  187. return; /* Source buffer ran out */
  188. header = (struct spawn_prot_header *)prot_buffer;
  189. fatal_assert(SPAWN_PROT_EXEC_CMD == header->opcode);
  190. fatal_assert(NULL != header->handle);
  191. required_len += sizeof(*payload);
  192. if (prot_buffer_len < required_len)
  193. copy_to_prot_buffer(prot_buffer, &prot_buffer_len, required_len - prot_buffer_len, &source, &source_len);
  194. if (prot_buffer_len < required_len)
  195. return; /* Source buffer ran out */
  196. payload = (struct spawn_prot_exec_cmd *)(header + 1);
  197. command_length = payload->command_length;
  198. required_len += command_length;
  199. if (unlikely(required_len > MAX_COMMAND_LENGTH - 1)) {
  200. fprintf(stderr, "SPAWN: Ran out of protocol buffer space.\n");
  201. command_length = (MAX_COMMAND_LENGTH - 1) - (sizeof(*header) + sizeof(*payload));
  202. required_len = MAX_COMMAND_LENGTH - 1;
  203. }
  204. if (prot_buffer_len < required_len)
  205. copy_to_prot_buffer(prot_buffer, &prot_buffer_len, required_len - prot_buffer_len, &source, &source_len);
  206. if (prot_buffer_len < required_len)
  207. return; /* Source buffer ran out */
  208. spawn_protocol_execute_command(header->handle, payload->command_to_run, command_length);
  209. prot_buffer_len = 0;
  210. }
  211. }
  212. static void on_pipe_read(uv_stream_t *pipe, ssize_t nread, const uv_buf_t *buf)
  213. {
  214. if (0 == nread) {
  215. fprintf(stderr, "SERVER %s: Zero bytes read from spawn pipe.\n", __func__);
  216. } else if (UV_EOF == nread) {
  217. fprintf(stderr, "EOF found in spawn pipe.\n");
  218. } else if (nread < 0) {
  219. fprintf(stderr, "%s: %s\n", __func__, uv_strerror(nread));
  220. }
  221. if (nread < 0) { /* stop spawn server due to EOF or error */
  222. int error;
  223. uv_mutex_lock(&wait_children_mutex);
  224. server_shutdown = 1;
  225. spawned_processes = 1;
  226. uv_cond_signal(&wait_children_cond);
  227. uv_mutex_unlock(&wait_children_mutex);
  228. fprintf(stderr, "Shutting down spawn server event loop.\n");
  229. /* cleanup operations of the event loop */
  230. (void)uv_read_stop((uv_stream_t *) pipe);
  231. uv_close((uv_handle_t *)&server_pipe, NULL);
  232. error = uv_thread_join(&thread);
  233. if (error) {
  234. fprintf(stderr, "uv_thread_create(): %s", uv_strerror(error));
  235. }
  236. /* After joining it is safe to destroy child_waited_async */
  237. uv_close((uv_handle_t *)&child_waited_async, NULL);
  238. } else if (nread) {
  239. #ifdef SPAWN_DEBUG
  240. fprintf(stderr, "SERVER %s nread %u\n", __func__, (unsigned)nread);
  241. #endif
  242. server_parse_spawn_protocol(nread, buf->base);
  243. }
  244. if (buf && buf->len) {
  245. freez(buf->base);
  246. }
  247. }
  248. static void on_read_alloc(uv_handle_t *handle,
  249. size_t suggested_size,
  250. uv_buf_t* buf)
  251. {
  252. (void)handle;
  253. buf->base = mallocz(suggested_size);
  254. buf->len = suggested_size;
  255. }
  256. static void ignore_signal_handler(int signo) {
  257. /*
  258. * By having a signal handler we allow spawned processes to reset default signal dispositions. Setting SIG_IGN
  259. * would be inherited by the spawned children which is not desirable.
  260. */
  261. (void)signo;
  262. }
  263. void spawn_server(void)
  264. {
  265. int error;
  266. // initialize the system clocks
  267. clocks_init();
  268. // close all open file descriptors, except the standard ones
  269. // the caller may have left open files (lxc-attach has this issue)
  270. int fd;
  271. for(fd = (int)(sysconf(_SC_OPEN_MAX) - 1) ; fd > 2 ; --fd)
  272. if(fd_is_valid(fd))
  273. close(fd);
  274. // Have the libuv IPC pipe be closed when forking child processes
  275. (void) fcntl(0, F_SETFD, FD_CLOEXEC);
  276. fprintf(stderr, "Spawn server is up.\n");
  277. // Define signals we want to ignore
  278. struct sigaction sa;
  279. int signals_to_ignore[] = {SIGPIPE, SIGINT, SIGQUIT, SIGTERM, SIGHUP, SIGUSR1, SIGUSR2, SIGBUS, SIGCHLD};
  280. unsigned ignore_length = sizeof(signals_to_ignore) / sizeof(signals_to_ignore[0]);
  281. unsigned i;
  282. for (i = 0; i < ignore_length ; ++i) {
  283. sa.sa_flags = 0;
  284. sigemptyset(&sa.sa_mask);
  285. sa.sa_handler = ignore_signal_handler;
  286. if(sigaction(signals_to_ignore[i], &sa, NULL) == -1)
  287. fprintf(stderr, "SPAWN: Failed to change signal handler for signal: %d.\n", signals_to_ignore[i]);
  288. }
  289. signals_unblock();
  290. loop = uv_default_loop();
  291. loop->data = NULL;
  292. error = uv_pipe_init(loop, &server_pipe, 1);
  293. if (error) {
  294. fprintf(stderr, "uv_pipe_init(): %s\n", uv_strerror(error));
  295. exit(error);
  296. }
  297. fatal_assert(server_pipe.ipc);
  298. error = uv_pipe_open(&server_pipe, 0 /* UV_STDIN_FD */);
  299. if (error) {
  300. fprintf(stderr, "uv_pipe_open(): %s\n", uv_strerror(error));
  301. exit(error);
  302. }
  303. avl_init_lock(&spawn_outstanding_exec_tree, spawn_exec_compare);
  304. spawned_processes = 0;
  305. fatal_assert(0 == uv_cond_init(&wait_children_cond));
  306. fatal_assert(0 == uv_mutex_init(&wait_children_mutex));
  307. child_waited_list = NULL;
  308. error = uv_async_init(loop, &child_waited_async, child_waited_async_cb);
  309. if (error) {
  310. fprintf(stderr, "uv_async_init(): %s\n", uv_strerror(error));
  311. exit(error);
  312. }
  313. error = uv_thread_create(&thread, wait_children, NULL);
  314. if (error) {
  315. fprintf(stderr, "uv_thread_create(): %s\n", uv_strerror(error));
  316. exit(error);
  317. }
  318. prot_buffer_len = 0;
  319. error = uv_read_start((uv_stream_t *)&server_pipe, on_read_alloc, on_pipe_read);
  320. fatal_assert(error == 0);
  321. while (!server_shutdown) {
  322. uv_run(loop, UV_RUN_DEFAULT);
  323. }
  324. fprintf(stderr, "Shutting down spawn server loop complete.\n");
  325. fatal_assert(0 == uv_loop_close(loop));
  326. exit(0);
  327. }