konf.c 4.5 KB

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