1
0

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 "lub/string.h"
  19. #include "konf/buf.h"
  20. #include "internal.h"
  21. /*--------------------------------------------------------- */
  22. bool_t clish_script_callback(clish_shell_t * this,
  23. const clish_command_t * cmd, const char *script, char ** out)
  24. {
  25. const char * shebang = NULL;
  26. pid_t cpid;
  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. assert(cmd);
  39. if (!script) /* Nothing to do */
  40. return BOOL_TRUE;
  41. /* Find out shebang */
  42. shebang = clish_command__get_shebang(cmd);
  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. kill(cpid, SIGTERM);
  104. if (!is_sh)
  105. waitpid(cpid, NULL, 0);
  106. /* Restore SIGINT and SIGQUIT */
  107. sigaction(SIGINT, &sig_old_int, NULL);
  108. sigaction(SIGQUIT, &sig_old_quit, NULL);
  109. return BOOL_FALSE;
  110. }
  111. /* Read the result of script execution */
  112. buf = konf_buf_new(fileno(rpipe));
  113. while (konf_buf_read(buf) > 0);
  114. *out = konf_buf__dup_line(buf);
  115. konf_buf_delete(buf);
  116. /* Wait for the writing process */
  117. if (!is_sh)
  118. waitpid(cpid, NULL, 0);
  119. /* Wait for script */
  120. res = pclose(rpipe);
  121. /* Restore SIGINT and SIGQUIT */
  122. sigaction(SIGINT, &sig_old_int, NULL);
  123. sigaction(SIGQUIT, &sig_old_quit, NULL);
  124. } else {
  125. res = system(command);
  126. }
  127. lub_string_free(command);
  128. #ifdef DEBUG
  129. fprintf(stderr, "RETCODE: %d\n", WEXITSTATUS(res));
  130. #endif /* DEBUG */
  131. return (0 == res) ? BOOL_TRUE : BOOL_FALSE;
  132. }
  133. /*--------------------------------------------------------- */
  134. bool_t clish_dryrun_callback(clish_shell_t * this,
  135. const clish_command_t * cmd, const char *script, char ** out)
  136. {
  137. #ifdef DEBUG
  138. fprintf(stderr, "DRY-RUN: %s\n", script);
  139. #endif /* DEBUG */
  140. if (out)
  141. *out = NULL;
  142. return BOOL_TRUE;
  143. }
  144. /*--------------------------------------------------------- */