1
0

callback_script.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * clish_script_callback.c
  3. *
  4. * Callback hook to action a shell script.
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <string.h>
  10. #include <assert.h>
  11. #include <signal.h>
  12. #include <sys/types.h>
  13. #include <sys/wait.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <fcntl.h>
  17. #include "lub/string.h"
  18. #include "konf/buf.h"
  19. #include "internal.h"
  20. /*--------------------------------------------------------- */
  21. int clish_script_callback(clish_context_t *context,
  22. clish_action_t *action, const char *script, char **out)
  23. {
  24. clish_shell_t *this = context->shell;
  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 BOOL_TRUE;
  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, "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 write to FIFO */
  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: 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, "System error. Can't fork the script.\n"
  91. "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 BOOL_FALSE;
  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. /*--------------------------------------------------------- */
  126. int clish_dryrun_callback(clish_context_t *context,
  127. clish_action_t *action, const char *script, char ** out)
  128. {
  129. #ifdef DEBUG
  130. fprintf(stderr, "DRY-RUN: %s\n", script);
  131. #endif /* DEBUG */
  132. if (out)
  133. *out = NULL;
  134. context = context; /* Happy compiler */
  135. action = action; /* Happy compiler */
  136. script = script; /* Happy compiler */
  137. return 0;
  138. }
  139. /*--------------------------------------------------------- */