sym_script.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * sym_script.c
  3. *
  4. * Function to execute a shell script.
  5. */
  6. #include "private.h"
  7. #include "lub/string.h"
  8. #include "konf/buf.h"
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include <string.h>
  13. #include <assert.h>
  14. #include <signal.h>
  15. #include <sys/types.h>
  16. #include <sys/wait.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <fcntl.h>
  20. /*--------------------------------------------------------- */
  21. CLISH_PLUGIN_OSYM(clish_script)
  22. {
  23. clish_shell_t *this = clish_context__get_shell(clish_context);
  24. const clish_action_t *action = clish_context__get_action(clish_context);
  25. const char *shebang = NULL;
  26. pid_t cpid = -1;
  27. int res;
  28. const char *fifo_name;
  29. FILE *wpipe;
  30. char *command = NULL;
  31. struct sigaction sig_old_int;
  32. struct sigaction sig_old_quit;
  33. struct sigaction sig_new;
  34. sigset_t sig_set;
  35. assert(this);
  36. if (!script) /* Nothing to do */
  37. return 0;
  38. /* Find out shebang */
  39. if (action)
  40. shebang = clish_action__get_shebang(action);
  41. if (!shebang)
  42. shebang = clish_shell__get_default_shebang(this);
  43. assert(shebang);
  44. #ifdef DEBUG
  45. fprintf(stderr, "SHEBANG: #!%s\n", shebang);
  46. fprintf(stderr, "SCRIPT: %s\n", script);
  47. #endif /* DEBUG */
  48. /* Get FIFO */
  49. fifo_name = clish_shell__get_fifo(this);
  50. if (!fifo_name) {
  51. fprintf(stderr, "Error: Can't create temporary FIFO.\n"
  52. "Error: The ACTION will be not executed.\n");
  53. return -1;
  54. }
  55. /* Create process to write to FIFO */
  56. cpid = fork();
  57. if (cpid == -1) {
  58. fprintf(stderr, "Error: Can't fork the write process.\n"
  59. "Error: The ACTION will be not executed.\n");
  60. return -1;
  61. }
  62. /* Child: write to FIFO */
  63. if (cpid == 0) {
  64. wpipe = fopen(fifo_name, "w");
  65. if (!wpipe)
  66. _exit(-1);
  67. fwrite(script, strlen(script), 1, wpipe);
  68. fclose(wpipe);
  69. _exit(0);
  70. }
  71. /* Parent */
  72. /* Prepare command */
  73. lub_string_cat(&command, shebang);
  74. lub_string_cat(&command, " ");
  75. lub_string_cat(&command, fifo_name);
  76. /* Ignore SIGINT and SIGQUIT */
  77. /* Probably this code is necessary to don't get SIGINT
  78. * from executed script. Because the executed script
  79. * and klish have the same terminal.
  80. */
  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. res = system(command);
  88. /* Restore SIGINT and SIGQUIT */
  89. sigaction(SIGINT, &sig_old_int, NULL);
  90. sigaction(SIGQUIT, &sig_old_quit, NULL);
  91. /* Wait for the writing process */
  92. kill(cpid, SIGTERM);
  93. waitpid(cpid, NULL, 0);
  94. lub_string_free(command);
  95. #ifdef DEBUG
  96. fprintf(stderr, "RETCODE: %d\n", WEXITSTATUS(res));
  97. #endif /* DEBUG */
  98. return WEXITSTATUS(res);
  99. }