konf.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * konf.c
  3. *
  4. *
  5. * The client to communicate to konfd configuration daemon.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "konf/net.h"
  11. #include "konf/tree.h"
  12. #include "konf/query.h"
  13. #include "konf/buf.h"
  14. #include "lub/argv.h"
  15. #include "lub/string.h"
  16. #ifndef UNIX_PATH_MAX
  17. #define UNIX_PATH_MAX 108
  18. #endif
  19. #define MAXMSG 1024
  20. static int receive_answer(konf_client_t * client, konf_buf_t **data);
  21. static int receive_data(konf_client_t * client, konf_buf_t *buf, konf_buf_t **data);
  22. static int process_answer(konf_client_t * client, char *str, konf_buf_t *buf, konf_buf_t **data);
  23. /*--------------------------------------------------------- */
  24. int main(int argc, char **argv)
  25. {
  26. int res = -1;
  27. unsigned i;
  28. konf_client_t *client = NULL;
  29. konf_buf_t *buf = NULL;
  30. char *line = NULL;
  31. char *str = NULL;
  32. const char *socket_path = KONFD_SOCKET_PATH;
  33. /* Get request line from the args */
  34. for (i = 1; i < argc; i++) {
  35. char *space = NULL;
  36. if (NULL != line)
  37. lub_string_cat(&line, " ");
  38. space = strchr(argv[i], ' ');
  39. if (space)
  40. lub_string_cat(&line, "\"");
  41. lub_string_cat(&line, argv[i]);
  42. if (space)
  43. lub_string_cat(&line, "\"");
  44. }
  45. if (!line) {
  46. fprintf(stderr, "Not enough arguments.\n");
  47. goto err;
  48. }
  49. #ifdef DEBUG
  50. printf("REQUEST: %s\n", line);
  51. #endif
  52. if (!(client = konf_client_new(socket_path))) {
  53. fprintf(stderr, "Can't create internal data structures.\n");
  54. goto err;
  55. }
  56. if (konf_client_connect(client) < 0) {
  57. fprintf(stderr, "Can't connect to %s socket.\n", socket_path);
  58. goto err;
  59. }
  60. if (konf_client_send(client, line) < 0) {
  61. fprintf(stderr, "Can't connect to %s socket.\n", socket_path);
  62. goto err;
  63. }
  64. if (receive_answer(client, &buf) < 0) {
  65. fprintf(stderr, "Can't get answer from the config daemon.\n");
  66. }
  67. if (buf) {
  68. konf_buf_lseek(buf, 0);
  69. while ((str = konf_buf_preparse(buf))) {
  70. if (strlen(str) == 0) {
  71. lub_string_free(str);
  72. break;
  73. }
  74. fprintf(stdout, "%s\n", str);
  75. lub_string_free(str);
  76. }
  77. konf_buf_delete(buf);
  78. }
  79. res = 0;
  80. err:
  81. lub_string_free(line);
  82. konf_client_free(client);
  83. return res;
  84. }
  85. /*--------------------------------------------------------- */
  86. static int receive_answer(konf_client_t * client, konf_buf_t **data)
  87. {
  88. konf_buf_t *buf;
  89. int nbytes;
  90. char *str;
  91. int retval = 0;
  92. int processed = 0;
  93. if ((konf_client_connect(client) < 0))
  94. return -1;
  95. buf = konf_buf_new(konf_client__get_sock(client));
  96. while ((!processed) && (nbytes = konf_buf_read(buf)) > 0) {
  97. while ((str = konf_buf_parse(buf))) {
  98. konf_buf_t *tmpdata = NULL;
  99. retval = process_answer(client, str, buf, &tmpdata);
  100. lub_string_free(str);
  101. if (retval < 0) {
  102. konf_buf_delete(buf);
  103. return retval;
  104. }
  105. if (retval == 0)
  106. processed = 1;
  107. if (tmpdata) {
  108. if (*data)
  109. konf_buf_delete(*data);
  110. *data = tmpdata;
  111. }
  112. }
  113. }
  114. konf_buf_delete(buf);
  115. return retval;
  116. }
  117. /*--------------------------------------------------------- */
  118. static int receive_data(konf_client_t * client, konf_buf_t *buf, konf_buf_t **data)
  119. {
  120. konf_buf_t *tmpdata;
  121. char *str;
  122. int processed = 0;
  123. if ((konf_client_connect(client) < 0))
  124. return -1;
  125. tmpdata = konf_buf_new(konf_client__get_sock(client));
  126. do {
  127. while ((str = konf_buf_parse(buf))) {
  128. #ifdef DEBUG
  129. fprintf(stderr, "RECV DATA: [%s]\n", str);
  130. #endif
  131. konf_buf_add(tmpdata, str, strlen(str) + 1);
  132. if (strlen(str) == 0) {
  133. processed = 1;
  134. lub_string_free(str);
  135. break;
  136. }
  137. lub_string_free(str);
  138. }
  139. } while ((!processed) && (konf_buf_read(buf)) > 0);
  140. if (!processed) {
  141. konf_buf_delete(tmpdata);
  142. *data = NULL;
  143. return -1;
  144. }
  145. *data = tmpdata;
  146. return 0;
  147. }
  148. /*--------------------------------------------------------- */
  149. static int process_answer(konf_client_t * client, char *str, konf_buf_t *buf, konf_buf_t **data)
  150. {
  151. int res;
  152. konf_query_t *query;
  153. /* Parse query */
  154. query = konf_query_new();
  155. res = konf_query_parse_str(query, str);
  156. if (res < 0) {
  157. konf_query_free(query);
  158. #ifdef DEBUG
  159. fprintf(stderr, "CONFIG error: Cannot parse answer string.\n");
  160. #endif
  161. return -1;
  162. }
  163. #ifdef DEBUG
  164. fprintf(stderr, "ANSWER: %s\n", str);
  165. /* konf_query_dump(query);
  166. */
  167. #endif
  168. switch (konf_query__get_op(query)) {
  169. case KONF_QUERY_OP_OK:
  170. res = 0;
  171. break;
  172. case KONF_QUERY_OP_ERROR:
  173. res = -1;
  174. break;
  175. case KONF_QUERY_OP_STREAM:
  176. if (receive_data(client, buf, data) < 0)
  177. res = -1;
  178. else
  179. res = 1; /* wait for another answer */
  180. break;
  181. default:
  182. res = -1;
  183. break;
  184. }
  185. /* Free resources */
  186. konf_query_free(query);
  187. return res;
  188. }