clish_script_callback.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * clish_script_callback.c
  3. *
  4. *
  5. * Callback hook to action a shell script.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include <assert.h>
  12. #include <signal.h>
  13. #include <sys/types.h>
  14. #include <sys/wait.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <fcntl.h>
  18. #include "private.h"
  19. #define KLISH_FIFO "/tmp/klish.fifo"
  20. /*--------------------------------------------------------- */
  21. bool_t clish_script_callback(clish_shell_t * this,
  22. const clish_command_t * cmd, const char *script)
  23. {
  24. const char * shebang = NULL;
  25. pid_t cpid;
  26. char buf;
  27. int res;
  28. const char *fifo_name;
  29. FILE *rpipe, *wpipe;
  30. char *command = NULL;
  31. bool_t is_sh = BOOL_FALSE;
  32. char **out = NULL;
  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. assert(cmd);
  40. if (!script) /* Nothing to do */
  41. return BOOL_TRUE;
  42. /* Find out shebang */
  43. shebang = clish_command__get_shebang(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. int retval;
  72. wpipe = fopen(fifo_name, "w");
  73. if (!wpipe)
  74. _exit(-1);
  75. fwrite(script, strlen(script) + 1, 1, wpipe);
  76. fclose(wpipe);
  77. _exit(0);
  78. }
  79. }
  80. /* Parent */
  81. /* Prepare command */
  82. if (!is_sh) {
  83. lub_string_cat(&command, shebang);
  84. lub_string_cat(&command, " ");
  85. lub_string_cat(&command, fifo_name);
  86. } else {
  87. lub_string_cat(&command, script);
  88. }
  89. #ifdef DEBUG
  90. fprintf(stderr, "COMMAND: %s\n", command);
  91. #endif /* DEBUG */
  92. /* If the stdout of script is needed */
  93. if (out) {
  94. /* Ignore SIGINT and SIGQUIT */
  95. sigemptyset(&sig_set);
  96. sig_new.sa_flags = 0;
  97. sig_new.sa_mask = sig_set;
  98. sig_new.sa_handler = SIG_IGN;
  99. sigaction(SIGINT, &sig_new, &sig_old_int);
  100. sigaction(SIGQUIT, &sig_new, &sig_old_quit);
  101. /* Execute shebang with FIFO as argument */
  102. rpipe = popen(command, "r");
  103. if (!rpipe) {
  104. fprintf(stderr, "System error. Can't fork the script.\n"
  105. "The ACTION will be not executed.\n");
  106. lub_string_free(command);
  107. kill(cpid, SIGTERM);
  108. if (!is_sh)
  109. waitpid(cpid, NULL, 0);
  110. /* Restore SIGINT and SIGQUIT */
  111. sigaction(SIGINT, &sig_old_int, NULL);
  112. sigaction(SIGQUIT, &sig_old_quit, NULL);
  113. return BOOL_FALSE;
  114. }
  115. /* Read the result of script execution */
  116. while (read(fileno(rpipe), &buf, 1) > 0)
  117. write(fileno(clish_shell__get_ostream(this)), &buf, 1);
  118. /* Wait for the writing process */
  119. if (!is_sh)
  120. waitpid(cpid, NULL, 0);
  121. /* Wait for script */
  122. res = pclose(rpipe);
  123. /* Restore SIGINT and SIGQUIT */
  124. sigaction(SIGINT, &sig_old_int, NULL);
  125. sigaction(SIGQUIT, &sig_old_quit, NULL);
  126. } else {
  127. res = system(command);
  128. }
  129. lub_string_free(command);
  130. #ifdef DEBUG
  131. fprintf(stderr, "RETCODE: %d\n", WEXITSTATUS(res));
  132. #endif /* DEBUG */
  133. return (0 == res) ? BOOL_TRUE : BOOL_FALSE;
  134. }
  135. /*--------------------------------------------------------- */
  136. bool_t clish_dryrun_callback(clish_shell_t * this,
  137. const clish_command_t * cmd, const char *script)
  138. {
  139. #ifdef DEBUG
  140. fprintf(stderr, "DRY-RUN: %s\n", script);
  141. #endif /* DEBUG */
  142. return BOOL_TRUE;
  143. }
  144. /*--------------------------------------------------------- */