spawn.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_SPAWN_H
  3. #define NETDATA_SPAWN_H 1
  4. #include "daemon/common.h"
  5. #define SPAWN_SERVER_COMMAND_LINE_ARGUMENT "--special-spawn-server"
  6. typedef enum spawn_protocol {
  7. SPAWN_PROT_EXEC_CMD = 0,
  8. SPAWN_PROT_SPAWN_RESULT,
  9. SPAWN_PROT_CMD_EXIT_STATUS
  10. } spawn_prot_t;
  11. struct spawn_prot_exec_cmd {
  12. uint16_t command_length;
  13. char command_to_run[];
  14. };
  15. struct spawn_prot_spawn_result {
  16. pid_t exec_pid; /* 0 if failed to spawn */
  17. time_t exec_run_timestamp; /* time of successfully spawning the command */
  18. };
  19. struct spawn_prot_cmd_exit_status {
  20. int exec_exit_status;
  21. };
  22. struct spawn_prot_header {
  23. spawn_prot_t opcode;
  24. void *handle;
  25. };
  26. #undef SPAWN_DEBUG /* define to enable debug prints */
  27. #define SPAWN_MAX_OUTSTANDING (32768)
  28. #define SPAWN_CMD_PROCESSED 0x00000001
  29. #define SPAWN_CMD_IN_PROGRESS 0x00000002
  30. #define SPAWN_CMD_FAILED_TO_SPAWN 0x00000004
  31. #define SPAWN_CMD_DONE 0x00000008
  32. struct spawn_cmd_info {
  33. avl_t avl;
  34. /* concurrency control per command */
  35. uv_mutex_t mutex;
  36. uv_cond_t cond; /* users block here until command has finished */
  37. uint64_t serial;
  38. char *command_to_run;
  39. int exit_status;
  40. pid_t pid;
  41. unsigned long flags;
  42. time_t exec_run_timestamp; /* time of successfully spawning the command */
  43. };
  44. /* spawn command queue */
  45. struct spawn_queue {
  46. avl_tree_type cmd_tree;
  47. /* concurrency control of command queue */
  48. uv_mutex_t mutex;
  49. uv_cond_t cond;
  50. volatile unsigned size;
  51. uint64_t latest_serial;
  52. };
  53. struct write_context {
  54. uv_write_t write_req;
  55. struct spawn_prot_header header;
  56. struct spawn_prot_cmd_exit_status exit_status;
  57. struct spawn_prot_spawn_result spawn_result;
  58. struct spawn_prot_exec_cmd payload;
  59. };
  60. extern int spawn_thread_error;
  61. extern int spawn_thread_shutdown;
  62. extern uv_async_t spawn_async;
  63. void spawn_init(void);
  64. void spawn_server(void);
  65. void spawn_client(void *arg);
  66. void destroy_spawn_cmd(struct spawn_cmd_info *cmdinfo);
  67. uint64_t spawn_enq_cmd(const char *command_to_run);
  68. void spawn_wait_cmd(uint64_t serial, int *exit_status, time_t *exec_run_timestamp);
  69. void spawn_deq_cmd(struct spawn_cmd_info *cmdinfo);
  70. struct spawn_cmd_info *spawn_get_unprocessed_cmd(void);
  71. int create_spawn_server(uv_loop_t *loop, uv_pipe_t *spawn_channel, uv_process_t *process);
  72. /*
  73. * Copies from the source buffer to the protocol buffer. It advances the source buffer by the amount copied. It
  74. * subtracts the amount copied from the source length.
  75. */
  76. static inline void copy_to_prot_buffer(char *prot_buffer, unsigned *prot_buffer_len, unsigned max_to_copy,
  77. char **source, unsigned *source_len)
  78. {
  79. unsigned to_copy;
  80. to_copy = MIN(max_to_copy, *source_len);
  81. memcpy(prot_buffer + *prot_buffer_len, *source, to_copy);
  82. *prot_buffer_len += to_copy;
  83. *source += to_copy;
  84. *source_len -= to_copy;
  85. }
  86. #endif //NETDATA_SPAWN_H