cli.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "cli.h"
  3. #include "libnetdata/required_dummies.h"
  4. static uv_pipe_t client_pipe;
  5. static uv_write_t write_req;
  6. static uv_shutdown_t shutdown_req;
  7. static char command_string[MAX_COMMAND_LENGTH];
  8. static unsigned command_string_size;
  9. static char response_string[MAX_COMMAND_LENGTH];
  10. static unsigned response_string_size;
  11. static int exit_status;
  12. struct command_context {
  13. uv_work_t work;
  14. uv_stream_t *client;
  15. cmd_t idx;
  16. char *args;
  17. char *message;
  18. cmd_status_t status;
  19. };
  20. static void parse_command_reply(void)
  21. {
  22. FILE *stream = NULL;
  23. char *pos;
  24. int syntax_error = 0;
  25. for (pos = response_string ;
  26. pos < response_string + response_string_size && !syntax_error ;
  27. ++pos) {
  28. /* Skip white-space characters */
  29. for ( ; isspace(*pos) && ('\0' != *pos); ++pos) {;}
  30. if ('\0' == *pos)
  31. continue;
  32. switch (*pos) {
  33. case CMD_PREFIX_EXIT_CODE:
  34. exit_status = atoi(++pos);
  35. break;
  36. case CMD_PREFIX_INFO:
  37. stream = stdout;
  38. break;
  39. case CMD_PREFIX_ERROR:
  40. stream = stderr;
  41. break;
  42. default:
  43. syntax_error = 1;
  44. fprintf(stderr, "Syntax error, failed to parse command response.\n");
  45. break;
  46. }
  47. if (stream) {
  48. fprintf(stream, "%s\n", ++pos);
  49. pos += strlen(pos);
  50. stream = NULL;
  51. }
  52. }
  53. }
  54. static void pipe_read_cb(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf)
  55. {
  56. if (0 == nread) {
  57. fprintf(stderr, "%s: Zero bytes read by command pipe.\n", __func__);
  58. } else if (UV_EOF == nread) {
  59. // fprintf(stderr, "EOF found in command pipe.\n");
  60. parse_command_reply();
  61. } else if (nread < 0) {
  62. fprintf(stderr, "%s: %s\n", __func__, uv_strerror(nread));
  63. }
  64. if (nread < 0) { /* stop stream due to EOF or error */
  65. (void)uv_read_stop((uv_stream_t *)client);
  66. } else if (nread) {
  67. size_t to_copy;
  68. to_copy = MIN(nread, MAX_COMMAND_LENGTH - 1 - response_string_size);
  69. memcpy(response_string + response_string_size, buf->base, to_copy);
  70. response_string_size += to_copy;
  71. response_string[response_string_size] = '\0';
  72. }
  73. if (buf && buf->len) {
  74. free(buf->base);
  75. }
  76. }
  77. static void alloc_cb(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
  78. {
  79. (void)handle;
  80. buf->base = malloc(suggested_size);
  81. buf->len = suggested_size;
  82. }
  83. static void shutdown_cb(uv_shutdown_t* req, int status)
  84. {
  85. int ret;
  86. (void)req;
  87. (void)status;
  88. /* receive reply */
  89. response_string_size = 0;
  90. response_string[0] = '\0';
  91. ret = uv_read_start((uv_stream_t *)&client_pipe, alloc_cb, pipe_read_cb);
  92. if (ret) {
  93. fprintf(stderr, "uv_read_start(): %s\n", uv_strerror(ret));
  94. uv_close((uv_handle_t *)&client_pipe, NULL);
  95. return;
  96. }
  97. }
  98. static void pipe_write_cb(uv_write_t* req, int status)
  99. {
  100. int ret;
  101. (void)req;
  102. (void)status;
  103. ret = uv_shutdown(&shutdown_req, (uv_stream_t *)&client_pipe, shutdown_cb);
  104. if (ret) {
  105. fprintf(stderr, "uv_shutdown(): %s\n", uv_strerror(ret));
  106. uv_close((uv_handle_t *)&client_pipe, NULL);
  107. return;
  108. }
  109. }
  110. static void connect_cb(uv_connect_t* req, int status)
  111. {
  112. int ret;
  113. uv_buf_t write_buf;
  114. char *s;
  115. (void)req;
  116. if (status) {
  117. fprintf(stderr, "uv_pipe_connect(): %s\n", uv_strerror(status));
  118. fprintf(stderr, "Make sure the netdata service is running.\n");
  119. exit(-1);
  120. }
  121. if (0 == command_string_size) {
  122. s = fgets(command_string, MAX_COMMAND_LENGTH, stdin);
  123. }
  124. (void)s; /* We don't need input to communicate with the server */
  125. command_string_size = strlen(command_string);
  126. write_req.data = &client_pipe;
  127. write_buf.base = command_string;
  128. write_buf.len = command_string_size;
  129. ret = uv_write(&write_req, (uv_stream_t *)&client_pipe, &write_buf, 1, pipe_write_cb);
  130. if (ret) {
  131. fprintf(stderr, "uv_write(): %s\n", uv_strerror(ret));
  132. }
  133. // fprintf(stderr, "COMMAND: Sending command: \"%s\"\n", command_string);
  134. }
  135. int main(int argc, char **argv)
  136. {
  137. int ret, i;
  138. static uv_loop_t* loop;
  139. uv_connect_t req;
  140. exit_status = -1; /* default status for when there is no command response from server */
  141. loop = uv_default_loop();
  142. ret = uv_pipe_init(loop, &client_pipe, 1);
  143. if (ret) {
  144. fprintf(stderr, "uv_pipe_init(): %s\n", uv_strerror(ret));
  145. return exit_status;
  146. }
  147. command_string_size = 0;
  148. command_string[0] = '\0';
  149. for (i = 1 ; i < argc ; ++i) {
  150. size_t to_copy;
  151. to_copy = MIN(strlen(argv[i]), MAX_COMMAND_LENGTH - 1 - command_string_size);
  152. strncpyz(command_string + command_string_size, argv[i], to_copy);
  153. command_string_size += to_copy;
  154. if (command_string_size < MAX_COMMAND_LENGTH - 1) {
  155. command_string[command_string_size++] = ' ';
  156. } else {
  157. break;
  158. }
  159. }
  160. uv_pipe_connect(&req, &client_pipe, PIPENAME, connect_cb);
  161. uv_run(loop, UV_RUN_DEFAULT);
  162. uv_close((uv_handle_t *)&client_pipe, NULL);
  163. return exit_status;
  164. }