konf.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 <getopt.h>
  11. #include "konf/net.h"
  12. #include "konf/tree.h"
  13. #include "konf/query.h"
  14. #include "konf/buf.h"
  15. #include "lub/argv.h"
  16. #include "lub/string.h"
  17. #ifndef UNIX_PATH_MAX
  18. #define UNIX_PATH_MAX 108
  19. #endif
  20. #define MAXMSG 1024
  21. #define VER_MAJ 1
  22. #define VER_MIN 2
  23. #define VER_BUG 2
  24. static int receive_answer(konf_client_t * client, konf_buf_t **data);
  25. static int receive_data(konf_client_t * client, konf_buf_t *buf, konf_buf_t **data);
  26. static int process_answer(konf_client_t * client, char *str, konf_buf_t *buf, konf_buf_t **data);
  27. static void help(int status, const char *argv0);
  28. static void version(void);
  29. static const char *escape_chars = "\"\\'";
  30. /*--------------------------------------------------------- */
  31. int main(int argc, char **argv)
  32. {
  33. int res = -1;
  34. konf_client_t *client = NULL;
  35. konf_buf_t *buf = NULL;
  36. char *line = NULL;
  37. char *str = NULL;
  38. const char *socket_path = KONFD_SOCKET_PATH;
  39. unsigned i = 0;
  40. static const char *shortopts = "hvs:";
  41. /* static const struct option longopts[] = {
  42. {"help", 0, NULL, 'h'},
  43. {"socket", 1, NULL, 's'},
  44. {NULL, 0, NULL, 0}
  45. };
  46. */
  47. /* Parse command line options */
  48. optind = 0;
  49. while(1) {
  50. int opt;
  51. /* opt = getopt_long(argc, argv, shortopts, longopts, NULL); */
  52. opt = getopt(argc, argv, shortopts);
  53. if (-1 == opt)
  54. break;
  55. switch (opt) {
  56. case 's':
  57. socket_path = optarg;
  58. break;
  59. case 'h':
  60. help(0, argv[0]);
  61. exit(0);
  62. break;
  63. case 'v':
  64. version();
  65. exit(0);
  66. break;
  67. default:
  68. help(-1, argv[0]);
  69. exit(-1);
  70. break;
  71. }
  72. }
  73. /* Get request line from the args */
  74. for (i = optind; i < argc; i++) {
  75. char *space = NULL;
  76. if (NULL != line)
  77. lub_string_cat(&line, " ");
  78. space = strchr(argv[i], ' ');
  79. if (space)
  80. lub_string_cat(&line, "\"");
  81. str = lub_string_encode(argv[i], escape_chars);
  82. lub_string_cat(&line, str);
  83. lub_string_free(str);
  84. if (space)
  85. lub_string_cat(&line, "\"");
  86. }
  87. if (!line) {
  88. fprintf(stderr, "Not enough arguments.\n");
  89. goto err;
  90. }
  91. #ifdef DEBUG
  92. printf("REQUEST: %s\n", line);
  93. #endif
  94. if (!(client = konf_client_new(socket_path))) {
  95. fprintf(stderr, "Can't create internal data structures.\n");
  96. goto err;
  97. }
  98. if (konf_client_connect(client) < 0) {
  99. fprintf(stderr, "Can't connect to %s socket.\n", socket_path);
  100. goto err;
  101. }
  102. if (konf_client_send(client, line) < 0) {
  103. fprintf(stderr, "Can't connect to %s socket.\n", socket_path);
  104. goto err;
  105. }
  106. if (receive_answer(client, &buf) < 0) {
  107. fprintf(stderr, "Can't get answer from the config daemon.\n");
  108. }
  109. if (buf) {
  110. konf_buf_lseek(buf, 0);
  111. while ((str = konf_buf_preparse(buf))) {
  112. if (strlen(str) == 0) {
  113. lub_string_free(str);
  114. break;
  115. }
  116. fprintf(stdout, "%s\n", str);
  117. lub_string_free(str);
  118. }
  119. konf_buf_delete(buf);
  120. }
  121. res = 0;
  122. err:
  123. lub_string_free(line);
  124. konf_client_free(client);
  125. return res;
  126. }
  127. /*--------------------------------------------------------- */
  128. static int receive_answer(konf_client_t * client, konf_buf_t **data)
  129. {
  130. konf_buf_t *buf;
  131. int nbytes;
  132. char *str;
  133. int retval = 0;
  134. int processed = 0;
  135. if ((konf_client_connect(client) < 0))
  136. return -1;
  137. buf = konf_buf_new(konf_client__get_sock(client));
  138. while ((!processed) && (nbytes = konf_buf_read(buf)) > 0) {
  139. while ((str = konf_buf_parse(buf))) {
  140. konf_buf_t *tmpdata = NULL;
  141. retval = process_answer(client, str, buf, &tmpdata);
  142. lub_string_free(str);
  143. if (retval < 0) {
  144. konf_buf_delete(buf);
  145. return retval;
  146. }
  147. if (retval == 0)
  148. processed = 1;
  149. if (tmpdata) {
  150. if (*data)
  151. konf_buf_delete(*data);
  152. *data = tmpdata;
  153. }
  154. }
  155. }
  156. konf_buf_delete(buf);
  157. return retval;
  158. }
  159. /*--------------------------------------------------------- */
  160. static int receive_data(konf_client_t * client, konf_buf_t *buf, konf_buf_t **data)
  161. {
  162. konf_buf_t *tmpdata;
  163. char *str;
  164. int processed = 0;
  165. if ((konf_client_connect(client) < 0))
  166. return -1;
  167. tmpdata = konf_buf_new(konf_client__get_sock(client));
  168. do {
  169. while ((str = konf_buf_parse(buf))) {
  170. #ifdef DEBUG
  171. fprintf(stderr, "RECV DATA: [%s]\n", str);
  172. #endif
  173. konf_buf_add(tmpdata, str, strlen(str) + 1);
  174. if (strlen(str) == 0) {
  175. processed = 1;
  176. lub_string_free(str);
  177. break;
  178. }
  179. lub_string_free(str);
  180. }
  181. } while ((!processed) && (konf_buf_read(buf)) > 0);
  182. if (!processed) {
  183. konf_buf_delete(tmpdata);
  184. *data = NULL;
  185. return -1;
  186. }
  187. *data = tmpdata;
  188. return 0;
  189. }
  190. /*--------------------------------------------------------- */
  191. static int process_answer(konf_client_t * client, char *str, konf_buf_t *buf, konf_buf_t **data)
  192. {
  193. int res;
  194. konf_query_t *query;
  195. /* Parse query */
  196. query = konf_query_new();
  197. res = konf_query_parse_str(query, str);
  198. if (res < 0) {
  199. konf_query_free(query);
  200. #ifdef DEBUG
  201. fprintf(stderr, "CONFIG error: Cannot parse answer string.\n");
  202. #endif
  203. return -1;
  204. }
  205. #ifdef DEBUG
  206. fprintf(stderr, "ANSWER: %s\n", str);
  207. /* konf_query_dump(query);
  208. */
  209. #endif
  210. switch (konf_query__get_op(query)) {
  211. case KONF_QUERY_OP_OK:
  212. res = 0;
  213. break;
  214. case KONF_QUERY_OP_ERROR:
  215. res = -1;
  216. break;
  217. case KONF_QUERY_OP_STREAM:
  218. if (receive_data(client, buf, data) < 0)
  219. res = -1;
  220. else
  221. res = 1; /* wait for another answer */
  222. break;
  223. default:
  224. res = -1;
  225. break;
  226. }
  227. /* Free resources */
  228. konf_query_free(query);
  229. return res;
  230. }
  231. /*--------------------------------------------------------- */
  232. /* Print help message */
  233. static void help(int status, const char *argv0)
  234. {
  235. const char *name = NULL;
  236. if (!argv0)
  237. return;
  238. /* Find the basename */
  239. name = strrchr(argv0, '/');
  240. if (name)
  241. name++;
  242. else
  243. name = argv0;
  244. if (status != 0) {
  245. fprintf(stderr, "Try `%s -h' for more information.\n",
  246. name);
  247. } else {
  248. printf("Usage: %s [options] -- [command]\n", name);
  249. printf("Utility for communication to the konfd "
  250. "configuration daemon.\n");
  251. printf("Options:\n");
  252. printf("\t-v --version\tPrint utility version.\n");
  253. printf("\t-h --help\tPrint this help.\n");
  254. printf("\t-s --socket\tSpecify listen socket "
  255. "of konfd daemon.\n");
  256. }
  257. }
  258. /*--------------------------------------------------------- */
  259. /* Print version */
  260. static void version(void)
  261. {
  262. printf("%u.%u.%u\n", VER_MAJ, VER_MIN, VER_BUG);
  263. }