sym_script.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #include <limits.h>
  21. /*--------------------------------------------------------- */
  22. CLISH_PLUGIN_SYM(clish_script)
  23. {
  24. clish_shell_t *this = clish_context__get_shell(clish_context);
  25. const clish_action_t *action = clish_context__get_action(clish_context);
  26. const char *shebang = NULL;
  27. pid_t cpid = -1;
  28. int res;
  29. char fifo_name[PATH_MAX];
  30. FILE *rpipe, *wpipe;
  31. char *command = NULL;
  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 0;
  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. #ifdef DEBUG
  47. fprintf(stderr, "SHEBANG: #!%s\n", shebang);
  48. fprintf(stderr, "SCRIPT: %s\n", script);
  49. #endif /* DEBUG */
  50. /* Get FIFO */
  51. if (! clish_shell_mkfifo(this, fifo_name, sizeof(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. clish_shell_rmfifo(this, fifo_name);
  62. return -1;
  63. }
  64. /* Child: write to FIFO */
  65. if (cpid == 0) {
  66. wpipe = fopen(fifo_name, "w");
  67. if (!wpipe)
  68. _exit(-1);
  69. fwrite(script, strlen(script), 1, wpipe);
  70. fclose(wpipe);
  71. _exit(0);
  72. }
  73. /* Parent */
  74. /* Prepare command */
  75. lub_string_cat(&command, shebang);
  76. lub_string_cat(&command, " ");
  77. lub_string_cat(&command, fifo_name);
  78. /* If the stdout of script is needed */
  79. if (out) {
  80. konf_buf_t *buf;
  81. /* Ignore SIGINT and SIGQUIT */
  82. sigemptyset(&sig_set);
  83. sig_new.sa_flags = 0;
  84. sig_new.sa_mask = sig_set;
  85. sig_new.sa_handler = SIG_IGN;
  86. sigaction(SIGINT, &sig_new, &sig_old_int);
  87. sigaction(SIGQUIT, &sig_new, &sig_old_quit);
  88. /* Execute shebang with FIFO as argument */
  89. rpipe = popen(command, "r");
  90. if (!rpipe) {
  91. fprintf(stderr, "Error: Can't fork the script.\n"
  92. "Error: The ACTION will be not executed.\n");
  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. lub_string_free(command);
  99. clish_shell_rmfifo(this, fifo_name);
  100. return -1;
  101. }
  102. /* Read the result of script execution */
  103. buf = konf_buf_new(fileno(rpipe));
  104. while (konf_buf_read(buf) > 0);
  105. *out = konf_buf__dup_line(buf);
  106. konf_buf_delete(buf);
  107. /* Wait for the writing process */
  108. kill(cpid, SIGTERM);
  109. waitpid(cpid, NULL, 0);
  110. /* Wait for script */
  111. res = pclose(rpipe);
  112. /* Restore SIGINT and SIGQUIT */
  113. sigaction(SIGINT, &sig_old_int, NULL);
  114. sigaction(SIGQUIT, &sig_old_quit, NULL);
  115. } else {
  116. res = system(command);
  117. /* Wait for the writing process */
  118. kill(cpid, SIGTERM);
  119. waitpid(cpid, NULL, 0);
  120. }
  121. /* Clean up */
  122. lub_string_free(command);
  123. clish_shell_rmfifo(this, fifo_name);
  124. #ifdef DEBUG
  125. fprintf(stderr, "RETCODE: %d\n", WEXITSTATUS(res));
  126. #endif /* DEBUG */
  127. return WEXITSTATUS(res);
  128. }
  129. /*--------------------------------------------------------- */