callback_script.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. const char *script, char **out)
  23. {
  24. clish_shell_t *this = context->shell;
  25. const clish_command_t *cmd = context->cmd;
  26. const char * shebang = NULL;
  27. pid_t cpid;
  28. int res;
  29. const char *fifo_name;
  30. FILE *rpipe, *wpipe;
  31. char *command = NULL;
  32. bool_t is_sh = BOOL_FALSE;
  33. /* Signal vars */
  34. struct sigaction sig_old_int;
  35. struct sigaction sig_old_quit;
  36. struct sigaction sig_new;
  37. sigset_t sig_set;
  38. assert(this);
  39. if (!script) /* Nothing to do */
  40. return BOOL_TRUE;
  41. /* Find out shebang */
  42. if (cmd)
  43. shebang = clish_action__get_shebang(clish_command__get_action(cmd));
  44. if (!shebang)
  45. shebang = clish_shell__get_default_shebang(this);
  46. assert(shebang);
  47. if (0 == lub_string_nocasecmp(shebang, "/bin/sh"))
  48. is_sh = BOOL_TRUE;
  49. #ifdef DEBUG
  50. fprintf(stderr, "SHEBANG: #!%s\n", shebang);
  51. fprintf(stderr, "SCRIPT: %s\n", script);
  52. #endif /* DEBUG */
  53. /* If /bin/sh we don't need FIFO */
  54. if (!is_sh) {
  55. /* Get FIFO */
  56. fifo_name = clish_shell__get_fifo(this);
  57. if (!fifo_name) {
  58. fprintf(stderr, "System error. Can't create temporary FIFO.\n"
  59. "The ACTION will be not executed.\n");
  60. return BOOL_FALSE;
  61. }
  62. /* Create process to write to FIFO */
  63. cpid = fork();
  64. if (cpid == -1) {
  65. fprintf(stderr, "System error. Can't fork the write process.\n"
  66. "The ACTION will be not executed.\n");
  67. return BOOL_FALSE;
  68. }
  69. /* Child: write to FIFO */
  70. if (cpid == 0) {
  71. wpipe = fopen(fifo_name, "w");
  72. if (!wpipe)
  73. _exit(-1);
  74. fwrite(script, strlen(script) + 1, 1, wpipe);
  75. fclose(wpipe);
  76. _exit(0);
  77. }
  78. }
  79. /* Parent */
  80. /* Prepare command */
  81. if (!is_sh) {
  82. lub_string_cat(&command, shebang);
  83. lub_string_cat(&command, " ");
  84. lub_string_cat(&command, fifo_name);
  85. } else {
  86. lub_string_cat(&command, script);
  87. }
  88. /* If the stdout of script is needed */
  89. if (out) {
  90. konf_buf_t *buf;
  91. /* Ignore SIGINT and SIGQUIT */
  92. sigemptyset(&sig_set);
  93. sig_new.sa_flags = 0;
  94. sig_new.sa_mask = sig_set;
  95. sig_new.sa_handler = SIG_IGN;
  96. sigaction(SIGINT, &sig_new, &sig_old_int);
  97. sigaction(SIGQUIT, &sig_new, &sig_old_quit);
  98. /* Execute shebang with FIFO as argument */
  99. rpipe = popen(command, "r");
  100. if (!rpipe) {
  101. fprintf(stderr, "System error. Can't fork the script.\n"
  102. "The ACTION will be not executed.\n");
  103. lub_string_free(command);
  104. kill(cpid, SIGTERM);
  105. if (!is_sh)
  106. waitpid(cpid, NULL, 0);
  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. waitpid(cpid, NULL, 0);
  120. /* Wait for script */
  121. res = pclose(rpipe);
  122. /* Restore SIGINT and SIGQUIT */
  123. sigaction(SIGINT, &sig_old_int, NULL);
  124. sigaction(SIGQUIT, &sig_old_quit, NULL);
  125. } else {
  126. res = system(command);
  127. }
  128. lub_string_free(command);
  129. #ifdef DEBUG
  130. fprintf(stderr, "RETCODE: %d\n", WEXITSTATUS(res));
  131. #endif /* DEBUG */
  132. return WEXITSTATUS(res);
  133. }
  134. /*--------------------------------------------------------- */
  135. int clish_dryrun_callback(clish_context_t *context,
  136. const char *script, char ** out)
  137. {
  138. #ifdef DEBUG
  139. fprintf(stderr, "DRY-RUN: %s\n", script);
  140. #endif /* DEBUG */
  141. if (out)
  142. *out = NULL;
  143. return 0;
  144. }
  145. /*--------------------------------------------------------- */