konf.c 6.1 KB

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