1
0

konf.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. printf("REQUEST: %s\n", line);
  110. #endif
  111. if (!(client = konf_client_new(socket_path))) {
  112. fprintf(stderr, "Can't create internal data structures.\n");
  113. goto err;
  114. }
  115. if (konf_client_connect(client) < 0) {
  116. fprintf(stderr, "Can't connect to %s socket.\n", socket_path);
  117. goto err;
  118. }
  119. if (konf_client_send(client, line) < 0) {
  120. fprintf(stderr, "Can't connect to %s socket.\n", socket_path);
  121. goto err;
  122. }
  123. if (konf_client_recv_answer(client, &buf) < 0) {
  124. fprintf(stderr, "The error while request to the konfd daemon.\n");
  125. }
  126. if (buf) {
  127. konf_buf_lseek(buf, 0);
  128. while ((str = konf_buf_preparse(buf))) {
  129. if (strlen(str) == 0) {
  130. lub_string_free(str);
  131. break;
  132. }
  133. fprintf(stdout, "%s\n", str);
  134. lub_string_free(str);
  135. }
  136. konf_buf_delete(buf);
  137. }
  138. res = 0;
  139. err:
  140. lub_string_free(line);
  141. konf_client_free(client);
  142. return res;
  143. }
  144. /*--------------------------------------------------------- */
  145. /* Print help message */
  146. static void help(int status, const char *argv0)
  147. {
  148. const char *name = NULL;
  149. if (!argv0)
  150. return;
  151. /* Find the basename */
  152. name = strrchr(argv0, '/');
  153. if (name)
  154. name++;
  155. else
  156. name = argv0;
  157. if (status != 0) {
  158. fprintf(stderr, "Try `%s -h' for more information.\n",
  159. name);
  160. } else {
  161. printf("Usage: %s [options] -- <command for konfd daemon>\n", name);
  162. printf("Utility for communication to the konfd "
  163. "configuration daemon.\n");
  164. printf("Options:\n");
  165. printf("\t-v, --version\tPrint utility version.\n");
  166. printf("\t-h, --help\tPrint this help.\n");
  167. printf("\t-s <path>, --socket=<path>\tSpecify listen socket "
  168. "of the konfd daemon.\n");
  169. }
  170. }