konf.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * konf.c
  3. *
  4. *
  5. * The client to communicate to konfd configuration daemon.
  6. */
  7. #ifdef HAVE_CONFIG_H
  8. #include "config.h"
  9. #endif /* HAVE_CONFIG_H */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <string.h>
  14. #ifdef HAVE_GETOPT_H
  15. #include <getopt.h>
  16. #endif
  17. #include <signal.h>
  18. #include "konf/net.h"
  19. #include "konf/query.h"
  20. #include "konf/buf.h"
  21. #include "lub/string.h"
  22. #ifndef UNIX_PATH_MAX
  23. #define UNIX_PATH_MAX 108
  24. #endif
  25. #define MAXMSG 1024
  26. #ifndef VERSION
  27. #define VERSION 1.2.2
  28. #endif
  29. #define QUOTE(t) #t
  30. #define version(v) printf("%s\n", v)
  31. static void help(int status, const char *argv0);
  32. static const char *escape_chars = "\"\\'";
  33. /*--------------------------------------------------------- */
  34. int main(int argc, char **argv)
  35. {
  36. int res = -1;
  37. konf_client_t *client = NULL;
  38. konf_buf_t *buf = NULL;
  39. char *line = NULL;
  40. char *str = NULL;
  41. const char *socket_path = KONFD_SOCKET_PATH;
  42. unsigned i = 0;
  43. /* Signal vars */
  44. struct sigaction sigpipe_act;
  45. sigset_t sigpipe_set;
  46. static const char *shortopts = "hvs:";
  47. #ifdef HAVE_GETOPT_H
  48. static const struct option longopts[] = {
  49. {"help", 0, NULL, 'h'},
  50. {"version", 0, NULL, 'v'},
  51. {"socket", 1, NULL, 's'},
  52. {NULL, 0, NULL, 0}
  53. };
  54. #endif
  55. /* Ignore SIGPIPE */
  56. sigemptyset(&sigpipe_set);
  57. sigaddset(&sigpipe_set, SIGPIPE);
  58. sigpipe_act.sa_flags = 0;
  59. sigpipe_act.sa_mask = sigpipe_set;
  60. sigpipe_act.sa_handler = SIG_IGN;
  61. sigaction(SIGPIPE, &sigpipe_act, NULL);
  62. /* Parse command line options */
  63. while(1) {
  64. int opt;
  65. #ifdef HAVE_GETOPT_H
  66. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  67. #else
  68. opt = getopt(argc, argv, shortopts);
  69. #endif
  70. if (-1 == opt)
  71. break;
  72. switch (opt) {
  73. case 's':
  74. socket_path = optarg;
  75. break;
  76. case 'h':
  77. help(0, argv[0]);
  78. exit(0);
  79. break;
  80. case 'v':
  81. version(VERSION);
  82. exit(0);
  83. break;
  84. default:
  85. help(-1, argv[0]);
  86. exit(-1);
  87. break;
  88. }
  89. }
  90. /* Get request line from the args */
  91. for (i = optind; i < argc; i++) {
  92. char *space = NULL;
  93. if (NULL != line)
  94. lub_string_cat(&line, " ");
  95. space = strchr(argv[i], ' ');
  96. if (space)
  97. lub_string_cat(&line, "\"");
  98. str = lub_string_encode(argv[i], escape_chars);
  99. lub_string_cat(&line, str);
  100. lub_string_free(str);
  101. if (space)
  102. lub_string_cat(&line, "\"");
  103. }
  104. if (!line) {
  105. help(-1, argv[0]);
  106. goto err;
  107. }
  108. #ifdef DEBUG
  109. fprintf(stderr, "REQUEST: %s\n", line);
  110. #endif
  111. if (!(client = konf_client_new(socket_path))) {
  112. fprintf(stderr, "Error: Can't create internal data structures.\n");
  113. goto err;
  114. }
  115. if (konf_client_connect(client) < 0) {
  116. fprintf(stderr, "Error: Can't connect to %s socket.\n", socket_path);
  117. goto err;
  118. }
  119. if (konf_client_send(client, line) < 0) {
  120. fprintf(stderr, "Error: Can't send request to %s socket.\n", socket_path);
  121. goto err;
  122. }
  123. if (konf_client_recv_answer(client, &buf) < 0) {
  124. fprintf(stderr, "Error: The error code from the konfd daemon.\n");
  125. goto err;
  126. }
  127. if (buf) {
  128. konf_buf_lseek(buf, 0);
  129. while ((str = konf_buf_preparse(buf))) {
  130. if (strlen(str) == 0) {
  131. lub_string_free(str);
  132. break;
  133. }
  134. fprintf(stdout, "%s\n", str);
  135. lub_string_free(str);
  136. }
  137. konf_buf_delete(buf);
  138. }
  139. res = 0;
  140. err:
  141. lub_string_free(line);
  142. konf_client_free(client);
  143. return res;
  144. }
  145. /*--------------------------------------------------------- */
  146. /* Print help message */
  147. static void help(int status, const char *argv0)
  148. {
  149. const char *name = NULL;
  150. if (!argv0)
  151. return;
  152. /* Find the basename */
  153. name = strrchr(argv0, '/');
  154. if (name)
  155. name++;
  156. else
  157. name = argv0;
  158. if (status != 0) {
  159. fprintf(stderr, "Try `%s -h' for more information.\n",
  160. name);
  161. } else {
  162. printf("Usage: %s [options] -- <command for konfd daemon>\n", name);
  163. printf("Utility for communication to the konfd "
  164. "configuration daemon.\n");
  165. printf("Options:\n");
  166. printf("\t-v, --version\tPrint utility version.\n");
  167. printf("\t-h, --help\tPrint this help.\n");
  168. printf("\t-s <path>, --socket=<path>\tSpecify listen socket "
  169. "of the konfd daemon.\n");
  170. }
  171. }