1
0

sym_script.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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_shell_t *this = clish_context__get_shell(clish_context);
  24. const clish_action_t *action = clish_context__get_action(clish_context);
  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. /*--------------------------------------------------------- */