| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #ifndef NETDATA_SPAWN_SERVER_INTERNALS_H |
| 4 | #define NETDATA_SPAWN_SERVER_INTERNALS_H |
| 5 | |
| 6 | #include "../libnetdata.h" |
| 7 | #include "spawn_server.h" |
| 8 | #include "spawn_library.h" |
| 9 | #include "log-forwarder.h" |
| 10 | |
| 11 | // grace period spawn_server_exec_kill() waits after SIGTERM before escalating to SIGKILL, |
| 12 | // used when the caller passes a non-positive timeout_ms (i.e. no explicit grace) |
| 13 | #define SPAWN_KILL_DEFAULT_GRACE_MS 2000 |
| 14 | |
| 15 | #if defined(OS_WINDOWS) |
| 16 | #define SPAWN_SERVER_VERSION_WINDOWS 1 |
| 17 | // #define SPAWN_SERVER_VERSION_UV 1 |
| 18 | // #define SPAWN_SERVER_VERSION_POSIX_SPAWN 1 |
| 19 | #else |
| 20 | #define SPAWN_SERVER_VERSION_NOFORK 1 |
| 21 | // #define SPAWN_SERVER_VERSION_UV 1 |
| 22 | // #define SPAWN_SERVER_VERSION_POSIX_SPAWN 1 |
| 23 | #endif |
| 24 | |
| 25 | struct spawn_server { |
| 26 | size_t id; |
| 27 | size_t request_id; |
| 28 | const char *name; |
| 29 | |
| 30 | #if defined(SPAWN_SERVER_VERSION_UV) |
| 31 | uv_loop_t *loop; |
| 32 | uv_thread_t thread; |
| 33 | uv_async_t async; |
| 34 | bool stopping; |
| 35 | |
| 36 | SPINLOCK spinlock; |
| 37 | struct work_item *work_queue; |
| 38 | #endif |
| 39 | |
| 40 | #if defined(SPAWN_SERVER_VERSION_NOFORK) |
| 41 | SPAWN_SERVER_OPTIONS options; |
| 42 | |
| 43 | ND_UUID magic; // for authorizing requests, the client needs to know our random UUID |
| 44 | // it is ignored for PING requests |
| 45 | |
| 46 | int pipe[2]; |
| 47 | int sock; // the listening socket of the server |
| 48 | pid_t server_pid; |
| 49 | char *path; |
| 50 | spawn_request_callback_t cb; |
| 51 | |
| 52 | int argc; |
| 53 | const char **argv; |
| 54 | #endif |
| 55 | |
| 56 | #if defined(SPAWN_SERVER_VERSION_POSIX_SPAWN) |
| 57 | #endif |
| 58 | |
| 59 | #if defined(SPAWN_SERVER_VERSION_WINDOWS) |
| 60 | LOG_FORWARDER *log_forwarder; |
| 61 | #endif |
| 62 | }; |
| 63 | |
| 64 | struct spawn_instance { |
| 65 | size_t request_id; |
| 66 | int sock; |
| 67 | int write_fd; // the child's input pipe, writing side |
| 68 | int read_fd; // the child's output pipe, reading side |
| 69 | int stderr_fd; |
| 70 | pid_t child_pid; |
| 71 | |
| 72 | #if defined(SPAWN_SERVER_VERSION_UV) |
| 73 | uv_process_t process; |
| 74 | int exit_code; |
| 75 | uv_sem_t sem; |
| 76 | #endif |
| 77 | |
| 78 | #if defined(SPAWN_SERVER_VERSION_NOFORK) |
| 79 | #endif |
| 80 | |
| 81 | #if defined(SPAWN_SERVER_VERSION_POSIX_SPAWN) |
| 82 | const char *cmdline; |
| 83 | bool exited; |
| 84 | int waitpid_status; |
| 85 | struct spawn_instance *prev, *next; |
| 86 | #endif |
| 87 | |
| 88 | #if defined(SPAWN_SERVER_VERSION_WINDOWS) |
| 89 | HANDLE process_handle; |
| 90 | DWORD dwProcessId; |
| 91 | #endif |
| 92 | }; |
| 93 | |
| 94 | #endif //NETDATA_SPAWN_SERVER_INTERNALS_H |