1
0

callback_script.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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_context_t *context,
  23. const char *script, char **out)
  24. {
  25. clish_shell_t *this = context->shell;
  26. const clish_command_t *cmd = context->cmd;
  27. const char * shebang = NULL;
  28. pid_t cpid;
  29. int res;
  30. const char *fifo_name;
  31. FILE *rpipe, *wpipe;
  32. char *command = NULL;
  33. bool_t is_sh = BOOL_FALSE;
  34. /* Signal vars */
  35. struct sigaction sig_old_int;
  36. struct sigaction sig_old_quit;
  37. struct sigaction sig_new;
  38. sigset_t sig_set;
  39. assert(this);
  40. assert(cmd);
  41. if (!script) /* Nothing to do */
  42. return BOOL_TRUE;
  43. /* Find out shebang */
  44. shebang = clish_action__get_shebang(clish_command__get_action(cmd));
  45. if (!shebang)
  46. shebang = clish_shell__get_default_shebang(this);
  47. assert(shebang);
  48. if (0 == lub_string_nocasecmp(shebang, "/bin/sh"))
  49. is_sh = BOOL_TRUE;
  50. #ifdef DEBUG
  51. fprintf(stderr, "SHEBANG: #!%s\n", shebang);
  52. fprintf(stderr, "SCRIPT: %s\n", script);
  53. #endif /* DEBUG */
  54. /* If /bin/sh we don't need FIFO */
  55. if (!is_sh) {
  56. /* Get FIFO */
  57. fifo_name = clish_shell__get_fifo(this);
  58. if (!fifo_name) {
  59. fprintf(stderr, "System error. Can't create temporary FIFO.\n"
  60. "The ACTION will be not executed.\n");
  61. return BOOL_FALSE;
  62. }
  63. /* Create process to write to FIFO */
  64. cpid = fork();
  65. if (cpid == -1) {
  66. fprintf(stderr, "System error. Can't fork the write process.\n"
  67. "The ACTION will be not executed.\n");
  68. return BOOL_FALSE;
  69. }
  70. /* Child: write to FIFO */
  71. if (cpid == 0) {
  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. /* If the stdout of script is needed */
  90. if (out) {
  91. konf_buf_t *buf;
  92. /* Ignore SIGINT and SIGQUIT */
  93. sigemptyset(&sig_set);
  94. sig_new.sa_flags = 0;
  95. sig_new.sa_mask = sig_set;
  96. sig_new.sa_handler = SIG_IGN;
  97. sigaction(SIGINT, &sig_new, &sig_old_int);
  98. sigaction(SIGQUIT, &sig_new, &sig_old_quit);
  99. /* Execute shebang with FIFO as argument */
  100. rpipe = popen(command, "r");
  101. if (!rpipe) {
  102. fprintf(stderr, "System error. Can't fork the script.\n"
  103. "The ACTION will be not executed.\n");
  104. lub_string_free(command);
  105. kill(cpid, SIGTERM);
  106. if (!is_sh)
  107. waitpid(cpid, NULL, 0);
  108. /* Restore SIGINT and SIGQUIT */
  109. sigaction(SIGINT, &sig_old_int, NULL);
  110. sigaction(SIGQUIT, &sig_old_quit, NULL);
  111. return BOOL_FALSE;
  112. }
  113. /* Read the result of script execution */
  114. buf = konf_buf_new(fileno(rpipe));
  115. while (konf_buf_read(buf) > 0);
  116. *out = konf_buf__dup_line(buf);
  117. konf_buf_delete(buf);
  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_context_t *context,
  137. const char *script, char ** out)
  138. {
  139. #ifdef DEBUG
  140. fprintf(stderr, "DRY-RUN: %s\n", script);
  141. #endif /* DEBUG */
  142. if (out)
  143. *out = NULL;
  144. return BOOL_TRUE;
  145. }
  146. /*--------------------------------------------------------- */