1
0

sym_script.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * sym_script.c
  3. *
  4. * Function to execute a shell script.
  5. */
  6. #include "private.h"
  7. #include "lub/string.h"
  8. #include "konf/buf.h"
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include <string.h>
  13. #include <assert.h>
  14. #include <signal.h>
  15. #include <sys/types.h>
  16. #include <sys/wait.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <fcntl.h>
  20. /*--------------------------------------------------------- */
  21. CLISH_PLUGIN_SYM(clish_script)
  22. {
  23. clish_context_t *context = (clish_context_t *)clish_context;
  24. clish_shell_t *this = context->shell;
  25. const clish_action_t *action = context->action;
  26. const char * shebang = NULL;
  27. pid_t cpid = -1;
  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 (action)
  43. shebang = clish_action__get_shebang(action);
  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. if (!is_sh) {
  105. kill(cpid, SIGTERM);
  106. waitpid(cpid, NULL, 0);
  107. }
  108. /* Restore SIGINT and SIGQUIT */
  109. sigaction(SIGINT, &sig_old_int, NULL);
  110. sigaction(SIGQUIT, &sig_old_quit, NULL);
  111. return BOOL_FALSE;
  112. }
  113. /* Read the result of script execution */
  114. buf = konf_buf_new(fileno(rpipe));
  115. while (konf_buf_read(buf) > 0);
  116. *out = konf_buf__dup_line(buf);
  117. konf_buf_delete(buf);
  118. /* Wait for the writing process */
  119. if (!is_sh) {
  120. kill(cpid, SIGTERM);
  121. waitpid(cpid, NULL, 0);
  122. }
  123. /* Wait for script */
  124. res = pclose(rpipe);
  125. /* Restore SIGINT and SIGQUIT */
  126. sigaction(SIGINT, &sig_old_int, NULL);
  127. sigaction(SIGQUIT, &sig_old_quit, NULL);
  128. } else {
  129. res = system(command);
  130. /* Wait for the writing process */
  131. if (!is_sh) {
  132. kill(cpid, SIGTERM);
  133. waitpid(cpid, NULL, 0);
  134. }
  135. }
  136. lub_string_free(command);
  137. #ifdef DEBUG
  138. fprintf(stderr, "RETCODE: %d\n", WEXITSTATUS(res));
  139. #endif /* DEBUG */
  140. return WEXITSTATUS(res);
  141. }
  142. /*--------------------------------------------------------- */