konf.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. optind = 0;
  64. while(1) {
  65. int opt;
  66. #ifdef HAVE_GETOPT_H
  67. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  68. #else
  69. opt = getopt(argc, argv, shortopts);
  70. #endif
  71. if (-1 == opt)
  72. break;
  73. switch (opt) {
  74. case 's':
  75. socket_path = optarg;
  76. break;
  77. case 'h':
  78. help(0, argv[0]);
  79. exit(0);
  80. break;
  81. case 'v':
  82. version(VERSION);
  83. exit(0);
  84. break;
  85. default:
  86. help(-1, argv[0]);
  87. exit(-1);
  88. break;
  89. }
  90. }
  91. /* Get request line from the args */
  92. for (i = optind; i < argc; i++) {
  93. char *space = NULL;
  94. if (NULL != line)
  95. lub_string_cat(&line, " ");
  96. space = strchr(argv[i], ' ');
  97. if (space)
  98. lub_string_cat(&line, "\"");
  99. str = lub_string_encode(argv[i], escape_chars);
  100. lub_string_cat(&line, str);
  101. lub_string_free(str);
  102. if (space)
  103. lub_string_cat(&line, "\"");
  104. }
  105. if (!line) {
  106. help(-1, argv[0]);
  107. goto err;
  108. }
  109. #ifdef DEBUG
  110. fprintf(stderr, "REQUEST: %s\n", line);
  111. #endif
  112. if (!(client = konf_client_new(socket_path))) {
  113. fprintf(stderr, "Error: Can't create internal data structures.\n");
  114. goto err;
  115. }
  116. if (konf_client_connect(client) < 0) {
  117. fprintf(stderr, "Error: Can't connect to %s socket.\n", socket_path);
  118. goto err;
  119. }
  120. if (konf_client_send(client, line) < 0) {
  121. fprintf(stderr, "Error: Can't send request to %s socket.\n", socket_path);
  122. goto err;
  123. }
  124. if (konf_client_recv_answer(client, &buf) < 0) {
  125. fprintf(stderr, "Error: The error code from the konfd daemon.\n");
  126. goto err;
  127. }
  128. if (buf) {
  129. konf_buf_lseek(buf, 0);
  130. while ((str = konf_buf_preparse(buf))) {
  131. if (strlen(str) == 0) {
  132. lub_string_free(str);
  133. break;
  134. }
  135. fprintf(stdout, "%s\n", str);
  136. lub_string_free(str);
  137. }
  138. konf_buf_delete(buf);
  139. }
  140. res = 0;
  141. err:
  142. lub_string_free(line);
  143. konf_client_free(client);
  144. return res;
  145. }
  146. /*--------------------------------------------------------- */
  147. /* Print help message */
  148. static void help(int status, const char *argv0)
  149. {
  150. const char *name = NULL;
  151. if (!argv0)
  152. return;
  153. /* Find the basename */
  154. name = strrchr(argv0, '/');
  155. if (name)
  156. name++;
  157. else
  158. name = argv0;
  159. if (status != 0) {
  160. fprintf(stderr, "Try `%s -h' for more information.\n",
  161. name);
  162. } else {
  163. printf("Usage: %s [options] -- <command for konfd daemon>\n", name);
  164. printf("Utility for communication to the konfd "
  165. "configuration daemon.\n");
  166. printf("Options:\n");
  167. printf("\t-v, --version\tPrint utility version.\n");
  168. printf("\t-h, --help\tPrint this help.\n");
  169. printf("\t-s <path>, --socket=<path>\tSpecify listen socket "
  170. "of the konfd daemon.\n");
  171. }
  172. }