konf.c 5.3 KB

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