1
0

callback_script.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * clish_script_callback.c
  3. *
  4. * Callback hook to action a shell script.
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <string.h>
  10. #include <assert.h>
  11. #include <signal.h>
  12. #include <sys/types.h>
  13. #include <sys/wait.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <fcntl.h>
  17. #include "lub/string.h"
  18. #include "konf/buf.h"
  19. #include "internal.h"
  20. /*--------------------------------------------------------- */
  21. int clish_script_callback(clish_context_t *context,
  22. clish_action_t *action, const char *script, char **out)
  23. {
  24. clish_shell_t *this = context->shell;
  25. const char * shebang = NULL;
  26. pid_t cpid = -1;
  27. int res;
  28. const char *fifo_name;
  29. FILE *rpipe, *wpipe;
  30. char *command = NULL;
  31. bool_t is_sh = BOOL_FALSE;
  32. /* Signal vars */
  33. struct sigaction sig_old_int;
  34. struct sigaction sig_old_quit;
  35. struct sigaction sig_new;
  36. sigset_t sig_set;
  37. assert(this);
  38. if (!script) /* Nothing to do */
  39. return BOOL_TRUE;
  40. /* Find out shebang */
  41. if (action)
  42. shebang = clish_action__get_shebang(action);
  43. if (!shebang)
  44. shebang = clish_shell__get_default_shebang(this);
  45. assert(shebang);
  46. if (0 == lub_string_nocasecmp(shebang, "/bin/sh"))
  47. is_sh = BOOL_TRUE;
  48. #ifdef DEBUG
  49. fprintf(stderr, "SHEBANG: #!%s\n", shebang);
  50. fprintf(stderr, "SCRIPT: %s\n", script);
  51. #endif /* DEBUG */
  52. /* If /bin/sh we don't need FIFO */
  53. if (!is_sh) {
  54. /* Get FIFO */
  55. fifo_name = clish_shell__get_fifo(this);
  56. if (!fifo_name) {
  57. fprintf(stderr, "System error. Can't create temporary FIFO.\n"
  58. "The ACTION will be not executed.\n");
  59. return BOOL_FALSE;
  60. }
  61. /* Create process to write to FIFO */
  62. cpid = fork();
  63. if (cpid == -1) {
  64. fprintf(stderr, "System error. Can't fork the write process.\n"
  65. "The ACTION will be not executed.\n");
  66. return BOOL_FALSE;
  67. }
  68. /* Child: write to FIFO */
  69. if (cpid == 0) {
  70. wpipe = fopen(fifo_name, "w");
  71. if (!wpipe)
  72. _exit(-1);
  73. fwrite(script, strlen(script) + 1, 1, wpipe);
  74. fclose(wpipe);
  75. _exit(0);
  76. }
  77. }
  78. /* Parent */
  79. /* Prepare command */
  80. if (!is_sh) {
  81. lub_string_cat(&command, shebang);
  82. lub_string_cat(&command, " ");
  83. lub_string_cat(&command, fifo_name);
  84. } else {
  85. lub_string_cat(&command, script);
  86. }
  87. /* If the stdout of script is needed */
  88. if (out) {
  89. konf_buf_t *buf;
  90. /* Ignore SIGINT and SIGQUIT */
  91. sigemptyset(&sig_set);
  92. sig_new.sa_flags = 0;
  93. sig_new.sa_mask = sig_set;
  94. sig_new.sa_handler = SIG_IGN;
  95. sigaction(SIGINT, &sig_new, &sig_old_int);
  96. sigaction(SIGQUIT, &sig_new, &sig_old_quit);
  97. /* Execute shebang with FIFO as argument */
  98. rpipe = popen(command, "r");
  99. if (!rpipe) {
  100. fprintf(stderr, "System error. Can't fork the script.\n"
  101. "The ACTION will be not executed.\n");
  102. lub_string_free(command);
  103. if (!is_sh) {
  104. kill(cpid, SIGTERM);
  105. waitpid(cpid, NULL, 0);
  106. }
  107. /* Restore SIGINT and SIGQUIT */
  108. sigaction(SIGINT, &sig_old_int, NULL);
  109. sigaction(SIGQUIT, &sig_old_quit, NULL);
  110. return BOOL_FALSE;
  111. }
  112. /* Read the result of script execution */
  113. buf = konf_buf_new(fileno(rpipe));
  114. while (konf_buf_read(buf) > 0);
  115. *out = konf_buf__dup_line(buf);
  116. konf_buf_delete(buf);
  117. /* Wait for the writing process */
  118. if (!is_sh) {
  119. kill(cpid, SIGTERM);
  120. waitpid(cpid, NULL, 0);
  121. }
  122. /* Wait for script */
  123. res = pclose(rpipe);
  124. /* Restore SIGINT and SIGQUIT */
  125. sigaction(SIGINT, &sig_old_int, NULL);
  126. sigaction(SIGQUIT, &sig_old_quit, NULL);
  127. } else {
  128. res = system(command);
  129. /* Wait for the writing process */
  130. if (!is_sh) {
  131. kill(cpid, SIGTERM);
  132. waitpid(cpid, NULL, 0);
  133. }
  134. }
  135. lub_string_free(command);
  136. #ifdef DEBUG
  137. fprintf(stderr, "RETCODE: %d\n", WEXITSTATUS(res));
  138. #endif /* DEBUG */
  139. return WEXITSTATUS(res);
  140. }
  141. /*--------------------------------------------------------- */
  142. int clish_dryrun_callback(clish_context_t *context,
  143. clish_action_t *action, const char *script, char ** out)
  144. {
  145. #ifdef DEBUG
  146. fprintf(stderr, "DRY-RUN: %s\n", script);
  147. #endif /* DEBUG */
  148. if (out)
  149. *out = NULL;
  150. context = context; /* Happy compiler */
  151. action = action; /* Happy compiler */
  152. script = script; /* Happy compiler */
  153. return 0;
  154. }
  155. /*--------------------------------------------------------- */