klish.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <getopt.h>
  12. #include <sys/socket.h>
  13. #include <sys/un.h>
  14. #ifdef HAVE_LOCALE_H
  15. #include <locale.h>
  16. #endif
  17. #ifdef HAVE_LANGINFO_CODESET
  18. #include <langinfo.h>
  19. #endif
  20. #include <faux/faux.h>
  21. #include <faux/str.h>
  22. #include <faux/msg.h>
  23. #include <faux/list.h>
  24. #include <faux/file.h>
  25. #include <faux/eloop.h>
  26. #include <klish/ktp.h>
  27. #include <klish/ktp_session.h>
  28. #include "private.h"
  29. static bool_t stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
  30. void *user_data);
  31. static bool_t stderr_cb(ktp_session_t *ktp, const char *line, size_t len,
  32. void *user_data);
  33. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  34. void *associated_data, void *user_data);
  35. static bool_t ktp_sync_cmd(ktp_session_t *ktp, const char *line,
  36. int *retcode, faux_error_t *error, bool_t dry_run);
  37. int main(int argc, char **argv)
  38. {
  39. int retval = -1;
  40. struct options *opts = NULL;
  41. int unix_sock = -1;
  42. ktp_session_t *ktp = NULL;
  43. int retcode = 0;
  44. faux_eloop_t *eloop = NULL;
  45. #ifdef HAVE_LOCALE_H
  46. // Set current locale
  47. setlocale(LC_ALL, "");
  48. #endif
  49. // Parse command line options
  50. opts = opts_init();
  51. if (opts_parse(argc, argv, opts)) {
  52. fprintf(stderr, "Error: Can't parse command line options\n");
  53. goto err;
  54. }
  55. // Parse config file
  56. if (!access(opts->cfgfile, R_OK)) {
  57. if (!config_parse(opts->cfgfile, opts))
  58. goto err;
  59. } else if (opts->cfgfile_userdefined) {
  60. // User defined config must be found
  61. fprintf(stderr, "Error: Can't find config file %s\n",
  62. opts->cfgfile);
  63. goto err;
  64. }
  65. // Connect to server
  66. unix_sock = ktp_connect_unix(opts->unix_socket_path);
  67. if (unix_sock < 0) {
  68. fprintf(stderr, "Error: Can't connect to server\n");
  69. goto err;
  70. }
  71. // Eloop object
  72. eloop = faux_eloop_new(NULL);
  73. faux_eloop_add_signal(eloop, SIGINT, stop_loop_ev, NULL);
  74. faux_eloop_add_signal(eloop, SIGTERM, stop_loop_ev, NULL);
  75. faux_eloop_add_signal(eloop, SIGQUIT, stop_loop_ev, NULL);
  76. // KTP session
  77. ktp = ktp_session_new(unix_sock, eloop);
  78. assert(ktp);
  79. if (!ktp) {
  80. fprintf(stderr, "Error: Can't create klish session\n");
  81. goto err;
  82. }
  83. ktp_session_set_cb(ktp, KTP_SESSION_CB_STDOUT, stdout_cb, NULL);
  84. ktp_session_set_cb(ktp, KTP_SESSION_CB_STDERR, stderr_cb, NULL);
  85. // Commands from cmdline
  86. if (faux_list_len(opts->commands) > 0) {
  87. const char *line = NULL;
  88. faux_list_node_t *iter = faux_list_head(opts->commands);
  89. while ((line = faux_list_each(&iter))) {
  90. faux_error_t *error = faux_error_new();
  91. bool_t rc = BOOL_FALSE;
  92. // Echo command
  93. if (!opts->quiet)
  94. fprintf(stderr, "%s\n", line);
  95. // Request to server
  96. rc = ktp_sync_cmd(ktp, line, &retcode,
  97. error, opts->dry_run);
  98. if (!rc)
  99. retcode = -1;
  100. if (faux_error_len(error) > 0) {
  101. fprintf(stderr, "Error:\n");
  102. faux_error_fshow(error, stderr);
  103. }
  104. faux_error_free(error);
  105. fprintf(stderr, "Retcode: %d\n", retcode);
  106. // Stop-on-error
  107. if (opts->stop_on_error && (!rc || retcode != 0))
  108. break;
  109. }
  110. // Commands from files
  111. } else if (faux_list_len(opts->files) > 0) {
  112. const char *filename = NULL;
  113. faux_list_node_t *iter = faux_list_head(opts->files);
  114. while ((filename = (const char *)faux_list_each(&iter))) {
  115. char *line = NULL;
  116. bool_t stop = BOOL_FALSE;
  117. faux_file_t *fd = faux_file_open(filename, O_RDONLY, 0);
  118. while ((line = faux_file_getline(fd))) {
  119. faux_error_t *error = faux_error_new();
  120. bool_t rc = BOOL_FALSE;
  121. // Echo command
  122. if (!opts->quiet)
  123. fprintf(stderr, "%s\n", line);
  124. // Request to server
  125. rc = ktp_sync_cmd(ktp, line, &retcode,
  126. error, opts->dry_run);
  127. if (!rc)
  128. retcode = -1;
  129. if (faux_error_len(error) > 0) {
  130. fprintf(stderr, "Error:\n");
  131. faux_error_fshow(error, stderr);
  132. }
  133. faux_error_free(error);
  134. fprintf(stderr, "Retcode: %d\n", retcode);
  135. faux_str_free(line);
  136. // Stop-on-error
  137. if (opts->stop_on_error && (!rc || retcode != 0)) {
  138. stop = BOOL_TRUE;
  139. break;
  140. }
  141. }
  142. faux_file_close(fd);
  143. if (stop)
  144. break;
  145. }
  146. // Interactive shell
  147. } else {
  148. // Interactive code is complex so move it to separate file
  149. retcode = klish_interactive_shell(ktp, opts);
  150. }
  151. retval = 0;
  152. err:
  153. ktp_session_free(ktp);
  154. faux_eloop_free(eloop);
  155. ktp_disconnect(unix_sock);
  156. opts_free(opts);
  157. if ((retval < 0) || (retcode < 0))
  158. return -1;
  159. return 0;
  160. }
  161. static bool_t ktp_sync_cmd(ktp_session_t *ktp, const char *line,
  162. int *retcode, faux_error_t *error, bool_t dry_run)
  163. {
  164. if (!ktp_session_cmd(ktp, line, error, dry_run))
  165. return BOOL_FALSE;
  166. faux_eloop_loop(ktp_session_eloop(ktp));
  167. return ktp_session_retcode(ktp, retcode);
  168. }
  169. static bool_t stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
  170. void *user_data)
  171. {
  172. if (write(STDOUT_FILENO, line, len) < 0)
  173. return BOOL_FALSE;
  174. ktp = ktp;
  175. user_data = user_data;
  176. return BOOL_TRUE;
  177. }
  178. static bool_t stderr_cb(ktp_session_t *ktp, const char *line, size_t len,
  179. void *user_data)
  180. {
  181. if (write(STDERR_FILENO, line, len) < 0)
  182. return BOOL_FALSE;
  183. ktp = ktp;
  184. user_data = user_data;
  185. return BOOL_TRUE;
  186. }
  187. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  188. void *associated_data, void *user_data)
  189. {
  190. // Happy compiler
  191. eloop = eloop;
  192. type = type;
  193. associated_data = associated_data;
  194. user_data = user_data;
  195. return BOOL_FALSE; // Stop Event Loop
  196. }