clish_script_callback.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. /* 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. assert(cmd);
  38. if (!script) /* Nothing to do */
  39. return BOOL_TRUE;
  40. /* Find out shebang */
  41. shebang = clish_command__get_shebang(cmd);
  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, "System error. Can't create temporary FIFO.\n"
  53. "The ACTION will be not executed.\n");
  54. return BOOL_FALSE;
  55. }
  56. /* Create process to execute script */
  57. cpid = fork();
  58. if (cpid == -1) {
  59. fprintf(stderr, "System error. Can't fork the write process.\n"
  60. "The ACTION will be not executed.\n");
  61. return BOOL_FALSE;
  62. }
  63. /* Child */
  64. if (cpid == 0) {
  65. int retval;
  66. wpipe = fopen(fifo_name, "w");
  67. if (!wpipe)
  68. _exit(-1);
  69. fwrite(script, strlen(script) + 1, 1, wpipe);
  70. fclose(wpipe);
  71. _exit(0);
  72. }
  73. /* Parent */
  74. /* Ignore SIGINT and SIGQUIT */
  75. sigemptyset(&sig_set);
  76. sig_new.sa_flags = 0;
  77. sig_new.sa_mask = sig_set;
  78. sig_new.sa_handler = SIG_IGN;
  79. sigaction(SIGINT, &sig_new, &sig_old_int);
  80. sigaction(SIGQUIT, &sig_new, &sig_old_quit);
  81. /* Prepare command */
  82. lub_string_cat(&command, shebang);
  83. lub_string_cat(&command, " ");
  84. lub_string_cat(&command, fifo_name);
  85. /* Execute shebang with FIFO as argument */
  86. rpipe = popen(command, "r");
  87. lub_string_free(command);
  88. if (!rpipe) {
  89. fprintf(stderr, "System error. Can't fork the script.\n"
  90. "The ACTION will be not executed.\n");
  91. kill(cpid, SIGTERM);
  92. waitpid(cpid, NULL, 0);
  93. /* Restore SIGINT and SIGQUIT */
  94. sigaction(SIGINT, &sig_old_int, NULL);
  95. sigaction(SIGQUIT, &sig_old_quit, NULL);
  96. return BOOL_FALSE;
  97. }
  98. /* Read the result of script execution */
  99. while (read(fileno(rpipe), &buf, 1) > 0)
  100. write(fileno(clish_shell__get_ostream(this)), &buf, 1);
  101. /* Wait for the writing process */
  102. waitpid(cpid, NULL, 0);
  103. /* Wait for script */
  104. res = pclose(rpipe);
  105. /* Restore SIGINT and SIGQUIT */
  106. sigaction(SIGINT, &sig_old_int, NULL);
  107. sigaction(SIGQUIT, &sig_old_quit, NULL);
  108. #ifdef DEBUG
  109. fprintf(stderr, "RETCODE: %d\n", WEXITSTATUS(res));
  110. #endif /* DEBUG */
  111. return (0 == res) ? BOOL_TRUE : BOOL_FALSE;
  112. }
  113. /*--------------------------------------------------------- */
  114. bool_t clish_dryrun_callback(clish_shell_t * this,
  115. const clish_command_t * cmd, const char *script)
  116. {
  117. #ifdef DEBUG
  118. fprintf(stderr, "DRY-RUN: %s\n", script);
  119. #endif /* DEBUG */
  120. return BOOL_TRUE;
  121. }
  122. /*--------------------------------------------------------- */