1
0

sym_script.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. /* Signal vars */
  32. struct sigaction sig_old_int;
  33. struct sigaction sig_old_quit;
  34. struct sigaction sig_new;
  35. sigset_t sig_set;
  36. assert(this);
  37. if (!script) /* Nothing to do */
  38. return 0;
  39. /* Find out shebang */
  40. if (action)
  41. shebang = clish_action__get_shebang(action);
  42. if (!shebang)
  43. shebang = clish_shell__get_default_shebang(this);
  44. assert(shebang);
  45. #ifdef DEBUG
  46. fprintf(stderr, "SHEBANG: #!%s\n", shebang);
  47. fprintf(stderr, "SCRIPT: %s\n", script);
  48. #endif /* DEBUG */
  49. /* Get FIFO */
  50. fifo_name = clish_shell__get_fifo(this);
  51. if (!fifo_name) {
  52. fprintf(stderr, "Error: Can't create temporary FIFO.\n"
  53. "Error: The ACTION will be not executed.\n");
  54. return -1;
  55. }
  56. /* Create process to write to FIFO */
  57. cpid = fork();
  58. if (cpid == -1) {
  59. fprintf(stderr, "Error: Can't fork the write process.\n"
  60. "Error: The ACTION will be not executed.\n");
  61. return -1;
  62. }
  63. /* Child: write to FIFO */
  64. if (cpid == 0) {
  65. wpipe = fopen(fifo_name, "w");
  66. if (!wpipe)
  67. _exit(-1);
  68. fwrite(script, strlen(script) + 1, 1, wpipe);
  69. fclose(wpipe);
  70. _exit(0);
  71. }
  72. /* Parent */
  73. /* Prepare command */
  74. lub_string_cat(&command, shebang);
  75. lub_string_cat(&command, " ");
  76. lub_string_cat(&command, fifo_name);
  77. /* If the stdout of script is needed */
  78. if (out) {
  79. konf_buf_t *buf;
  80. /* Ignore SIGINT and SIGQUIT */
  81. sigemptyset(&sig_set);
  82. sig_new.sa_flags = 0;
  83. sig_new.sa_mask = sig_set;
  84. sig_new.sa_handler = SIG_IGN;
  85. sigaction(SIGINT, &sig_new, &sig_old_int);
  86. sigaction(SIGQUIT, &sig_new, &sig_old_quit);
  87. /* Execute shebang with FIFO as argument */
  88. rpipe = popen(command, "r");
  89. if (!rpipe) {
  90. fprintf(stderr, "Error: Can't fork the script.\n"
  91. "Error: The ACTION will be not executed.\n");
  92. lub_string_free(command);
  93. kill(cpid, SIGTERM);
  94. waitpid(cpid, NULL, 0);
  95. /* Restore SIGINT and SIGQUIT */
  96. sigaction(SIGINT, &sig_old_int, NULL);
  97. sigaction(SIGQUIT, &sig_old_quit, NULL);
  98. return -1;
  99. }
  100. /* Read the result of script execution */
  101. buf = konf_buf_new(fileno(rpipe));
  102. while (konf_buf_read(buf) > 0);
  103. *out = konf_buf__dup_line(buf);
  104. konf_buf_delete(buf);
  105. /* Wait for the writing process */
  106. kill(cpid, SIGTERM);
  107. waitpid(cpid, NULL, 0);
  108. /* Wait for script */
  109. res = pclose(rpipe);
  110. /* Restore SIGINT and SIGQUIT */
  111. sigaction(SIGINT, &sig_old_int, NULL);
  112. sigaction(SIGQUIT, &sig_old_quit, NULL);
  113. } else {
  114. res = system(command);
  115. /* Wait for the writing process */
  116. kill(cpid, SIGTERM);
  117. waitpid(cpid, NULL, 0);
  118. }
  119. lub_string_free(command);
  120. #ifdef DEBUG
  121. fprintf(stderr, "RETCODE: %d\n", WEXITSTATUS(res));
  122. #endif /* DEBUG */
  123. return WEXITSTATUS(res);
  124. }
  125. /*--------------------------------------------------------- */