cli.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "cli.h"
  3. void error_int(int is_collector __maybe_unused, const char *prefix __maybe_unused, const char *file __maybe_unused, const char *function __maybe_unused, const unsigned long line __maybe_unused, const char *fmt, ... ) {
  4. FILE *fp = stderr;
  5. va_list args;
  6. va_start( args, fmt );
  7. vfprintf(fp, fmt, args );
  8. va_end( args );
  9. }
  10. #ifdef NETDATA_INTERNAL_CHECKS
  11. uint64_t debug_flags;
  12. void debug_int( const char *file __maybe_unused , const char *function __maybe_unused , const unsigned long line __maybe_unused, const char *fmt __maybe_unused, ... )
  13. {
  14. }
  15. void fatal_int( const char *file __maybe_unused, const char *function __maybe_unused, const unsigned long line __maybe_unused, const char *fmt __maybe_unused, ... )
  16. {
  17. abort();
  18. };
  19. #endif
  20. #ifdef NETDATA_TRACE_ALLOCATIONS
  21. void *callocz_int(size_t nmemb, size_t size, const char *file __maybe_unused, const char *function __maybe_unused, size_t line __maybe_unused)
  22. {
  23. void *p = calloc(nmemb, size);
  24. if (unlikely(!p)) {
  25. error("Cannot allocate %zu bytes of memory.", nmemb * size);
  26. exit(1);
  27. }
  28. return p;
  29. }
  30. void *mallocz_int(size_t size, const char *file __maybe_unused, const char *function __maybe_unused, size_t line __maybe_unused)
  31. {
  32. void *p = malloc(size);
  33. if (unlikely(!p)) {
  34. error("Cannot allocate %zu bytes of memory.", size);
  35. exit(1);
  36. }
  37. return p;
  38. }
  39. void *reallocz_int(void *ptr, size_t size, const char *file __maybe_unused, const char *function __maybe_unused, size_t line __maybe_unused)
  40. {
  41. void *p = realloc(ptr, size);
  42. if (unlikely(!p)) {
  43. error("Cannot allocate %zu bytes of memory.", size);
  44. exit(1);
  45. }
  46. return p;
  47. }
  48. void freez_int(void *ptr, const char *file __maybe_unused, const char *function __maybe_unused, size_t line __maybe_unused)
  49. {
  50. free(ptr);
  51. }
  52. #else
  53. void freez(void *ptr) {
  54. free(ptr);
  55. }
  56. void *mallocz(size_t size) {
  57. void *p = malloc(size);
  58. if (unlikely(!p)) {
  59. error("Cannot allocate %zu bytes of memory.", size);
  60. exit(1);
  61. }
  62. return p;
  63. }
  64. void *callocz(size_t nmemb, size_t size) {
  65. void *p = calloc(nmemb, size);
  66. if (unlikely(!p)) {
  67. error("Cannot allocate %zu bytes of memory.", nmemb * size);
  68. exit(1);
  69. }
  70. return p;
  71. }
  72. void *reallocz(void *ptr, size_t size) {
  73. void *p = realloc(ptr, size);
  74. if (unlikely(!p)) {
  75. error("Cannot allocate %zu bytes of memory.", size);
  76. exit(1);
  77. }
  78. return p;
  79. }
  80. #endif
  81. int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args) {
  82. if(unlikely(!n)) return 0;
  83. int size = vsnprintf(dst, n, fmt, args);
  84. dst[n - 1] = '\0';
  85. if (unlikely((size_t) size > n)) size = (int)n;
  86. return size;
  87. }
  88. static uv_pipe_t client_pipe;
  89. static uv_write_t write_req;
  90. static uv_shutdown_t shutdown_req;
  91. static char command_string[MAX_COMMAND_LENGTH];
  92. static unsigned command_string_size;
  93. static int exit_status;
  94. struct command_context {
  95. uv_work_t work;
  96. uv_stream_t *client;
  97. cmd_t idx;
  98. char *args;
  99. char *message;
  100. cmd_status_t status;
  101. };
  102. static void parse_command_reply(BUFFER *buf)
  103. {
  104. char *response_string = (char *) buffer_tostring(buf);
  105. unsigned response_string_size = buffer_strlen(buf);
  106. FILE *stream = NULL;
  107. char *pos;
  108. int syntax_error = 0;
  109. for (pos = response_string ;
  110. pos < response_string + response_string_size && !syntax_error ;
  111. ++pos) {
  112. /* Skip white-space characters */
  113. for ( ; isspace(*pos) && ('\0' != *pos); ++pos) {;}
  114. if ('\0' == *pos)
  115. continue;
  116. switch (*pos) {
  117. case CMD_PREFIX_EXIT_CODE:
  118. exit_status = atoi(++pos);
  119. break;
  120. case CMD_PREFIX_INFO:
  121. stream = stdout;
  122. break;
  123. case CMD_PREFIX_ERROR:
  124. stream = stderr;
  125. break;
  126. default:
  127. syntax_error = 1;
  128. fprintf(stderr, "Syntax error, failed to parse command response.\n");
  129. break;
  130. }
  131. if (stream) {
  132. fprintf(stream, "%s\n", ++pos);
  133. pos += strlen(pos);
  134. stream = NULL;
  135. }
  136. }
  137. }
  138. static void pipe_read_cb(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf)
  139. {
  140. BUFFER *response = client->data;
  141. if (0 == nread)
  142. fprintf(stderr, "%s: Zero bytes read by command pipe.\n", __func__);
  143. else if (UV_EOF == nread)
  144. parse_command_reply(response);
  145. else if (nread < 0) {
  146. fprintf(stderr, "%s: %s\n", __func__, uv_strerror(nread));
  147. (void)uv_read_stop((uv_stream_t *)client);
  148. }
  149. else
  150. buffer_fast_rawcat(response, buf->base, nread);
  151. if (buf && buf->len)
  152. free(buf->base);
  153. }
  154. static void alloc_cb(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
  155. {
  156. (void)handle;
  157. buf->base = malloc(suggested_size);
  158. buf->len = suggested_size;
  159. }
  160. static void shutdown_cb(uv_shutdown_t* req, int status)
  161. {
  162. int ret;
  163. (void)req;
  164. (void)status;
  165. /* receive reply */
  166. client_pipe.data = req->data;
  167. ret = uv_read_start((uv_stream_t *)&client_pipe, alloc_cb, pipe_read_cb);
  168. if (ret) {
  169. fprintf(stderr, "uv_read_start(): %s\n", uv_strerror(ret));
  170. uv_close((uv_handle_t *)&client_pipe, NULL);
  171. return;
  172. }
  173. }
  174. static void pipe_write_cb(uv_write_t* req, int status)
  175. {
  176. int ret;
  177. (void)status;
  178. uv_pipe_t *clientp = req->data;
  179. shutdown_req.data = clientp->data;
  180. ret = uv_shutdown(&shutdown_req, (uv_stream_t *)&client_pipe, shutdown_cb);
  181. if (ret) {
  182. fprintf(stderr, "uv_shutdown(): %s\n", uv_strerror(ret));
  183. uv_close((uv_handle_t *)&client_pipe, NULL);
  184. return;
  185. }
  186. }
  187. static void connect_cb(uv_connect_t* req, int status)
  188. {
  189. int ret;
  190. uv_buf_t write_buf;
  191. char *s;
  192. (void)req;
  193. if (status) {
  194. fprintf(stderr, "uv_pipe_connect(): %s\n", uv_strerror(status));
  195. fprintf(stderr, "Make sure the netdata service is running.\n");
  196. exit(-1);
  197. }
  198. if (0 == command_string_size) {
  199. s = fgets(command_string, MAX_COMMAND_LENGTH - 1, stdin);
  200. }
  201. (void)s; /* We don't need input to communicate with the server */
  202. command_string_size = strlen(command_string);
  203. client_pipe.data = req->data;
  204. write_req.data = &client_pipe;
  205. write_buf.base = command_string;
  206. write_buf.len = command_string_size;
  207. ret = uv_write(&write_req, (uv_stream_t *)&client_pipe, &write_buf, 1, pipe_write_cb);
  208. if (ret) {
  209. fprintf(stderr, "uv_write(): %s\n", uv_strerror(ret));
  210. }
  211. // fprintf(stderr, "COMMAND: Sending command: \"%s\"\n", command_string);
  212. }
  213. int main(int argc, char **argv)
  214. {
  215. int ret, i;
  216. static uv_loop_t* loop;
  217. uv_connect_t req;
  218. exit_status = -1; /* default status for when there is no command response from server */
  219. loop = uv_default_loop();
  220. ret = uv_pipe_init(loop, &client_pipe, 1);
  221. if (ret) {
  222. fprintf(stderr, "uv_pipe_init(): %s\n", uv_strerror(ret));
  223. return exit_status;
  224. }
  225. command_string_size = 0;
  226. command_string[0] = '\0';
  227. for (i = 1 ; i < argc ; ++i) {
  228. size_t to_copy;
  229. to_copy = MIN(strlen(argv[i]), MAX_COMMAND_LENGTH - 1 - command_string_size);
  230. strncpy(command_string + command_string_size, argv[i], to_copy);
  231. command_string_size += to_copy;
  232. command_string[command_string_size] = '\0';
  233. if (command_string_size < MAX_COMMAND_LENGTH - 1) {
  234. command_string[command_string_size++] = ' ';
  235. } else {
  236. break;
  237. }
  238. }
  239. req.data = buffer_create(128, NULL);
  240. uv_pipe_connect(&req, &client_pipe, PIPENAME, connect_cb);
  241. uv_run(loop, UV_RUN_DEFAULT);
  242. uv_close((uv_handle_t *)&client_pipe, NULL);
  243. buffer_free(client_pipe.data);
  244. return exit_status;
  245. }