1
0

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 <string.h>
  13. #ifdef HAVE_GETOPT_H
  14. #include <getopt.h>
  15. #endif
  16. #include <signal.h>
  17. #include "konf/net.h"
  18. #include "konf/query.h"
  19. #include "konf/buf.h"
  20. #include "lub/string.h"
  21. #ifndef UNIX_PATH_MAX
  22. #define UNIX_PATH_MAX 108
  23. #endif
  24. #define MAXMSG 1024
  25. #ifndef VERSION
  26. #define VERSION 1.2.2
  27. #endif
  28. #define QUOTE(t) #t
  29. #define version(v) printf("%s\n", v)
  30. static void help(int status, const char *argv0);
  31. static const char *escape_chars = "\"\\'";
  32. /*--------------------------------------------------------- */
  33. int main(int argc, char **argv)
  34. {
  35. int res = -1;
  36. konf_client_t *client = NULL;
  37. konf_buf_t *buf = NULL;
  38. char *line = NULL;
  39. char *str = NULL;
  40. const char *socket_path = KONFD_SOCKET_PATH;
  41. unsigned i = 0;
  42. /* Signal vars */
  43. struct sigaction sigpipe_act;
  44. sigset_t sigpipe_set;
  45. static const char *shortopts = "hvs:";
  46. #ifdef HAVE_GETOPT_H
  47. static const struct option longopts[] = {
  48. {"help", 0, NULL, 'h'},
  49. {"version", 0, NULL, 'v'},
  50. {"socket", 1, NULL, 's'},
  51. {NULL, 0, NULL, 0}
  52. };
  53. #endif
  54. /* Ignore SIGPIPE */
  55. sigemptyset(&sigpipe_set);
  56. sigaddset(&sigpipe_set, SIGPIPE);
  57. sigpipe_act.sa_flags = 0;
  58. sigpipe_act.sa_mask = sigpipe_set;
  59. sigpipe_act.sa_handler = SIG_IGN;
  60. sigaction(SIGPIPE, &sigpipe_act, NULL);
  61. /* Parse command line options */
  62. optind = 0;
  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. }