123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- /*
- * clish_script_callback.c
- *
- *
- * Callback hook to action a shell script.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <assert.h>
- #include <signal.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include "private.h"
- #define KLISH_FIFO "/tmp/klish.fifo"
- /*--------------------------------------------------------- */
- bool_t clish_script_callback(clish_shell_t * this,
- const clish_command_t * cmd, const char *script)
- {
- const char * shebang = NULL;
- pid_t cpid;
- char buf;
- int res;
- const char *fifo_name;
- FILE *rpipe, *wpipe;
- char *command = NULL;
- bool_t is_sh = BOOL_FALSE;
- char **out = NULL;
- /* Signal vars */
- struct sigaction sig_old_int;
- struct sigaction sig_old_quit;
- struct sigaction sig_new;
- sigset_t sig_set;
- assert(this);
- assert(cmd);
- if (!script) /* Nothing to do */
- return BOOL_TRUE;
- /* Find out shebang */
- shebang = clish_command__get_shebang(cmd);
- if (!shebang)
- shebang = clish_shell__get_default_shebang(this);
- assert(shebang);
- if (0 == lub_string_nocasecmp(shebang, "/bin/sh"))
- is_sh = BOOL_TRUE;
- #ifdef DEBUG
- fprintf(stderr, "SHEBANG: #!%s\n", shebang);
- fprintf(stderr, "SCRIPT: %s\n", script);
- #endif /* DEBUG */
- /* If /bin/sh we don't need FIFO */
- if (!is_sh) {
- /* Get FIFO */
- fifo_name = clish_shell__get_fifo(this);
- if (!fifo_name) {
- fprintf(stderr, "System error. Can't create temporary FIFO.\n"
- "The ACTION will be not executed.\n");
- return BOOL_FALSE;
- }
- /* Create process to write to FIFO */
- cpid = fork();
- if (cpid == -1) {
- fprintf(stderr, "System error. Can't fork the write process.\n"
- "The ACTION will be not executed.\n");
- return BOOL_FALSE;
- }
- /* Child: write to FIFO */
- if (cpid == 0) {
- int retval;
- wpipe = fopen(fifo_name, "w");
- if (!wpipe)
- _exit(-1);
- fwrite(script, strlen(script) + 1, 1, wpipe);
- fclose(wpipe);
- _exit(0);
- }
- }
- /* Parent */
- /* Prepare command */
- if (!is_sh) {
- lub_string_cat(&command, shebang);
- lub_string_cat(&command, " ");
- lub_string_cat(&command, fifo_name);
- } else {
- lub_string_cat(&command, script);
- }
- #ifdef DEBUG
- fprintf(stderr, "COMMAND: %s\n", command);
- #endif /* DEBUG */
- /* If the stdout of script is needed */
- if (out) {
- /* Ignore SIGINT and SIGQUIT */
- sigemptyset(&sig_set);
- sig_new.sa_flags = 0;
- sig_new.sa_mask = sig_set;
- sig_new.sa_handler = SIG_IGN;
- sigaction(SIGINT, &sig_new, &sig_old_int);
- sigaction(SIGQUIT, &sig_new, &sig_old_quit);
- /* Execute shebang with FIFO as argument */
- rpipe = popen(command, "r");
- if (!rpipe) {
- fprintf(stderr, "System error. Can't fork the script.\n"
- "The ACTION will be not executed.\n");
- lub_string_free(command);
- kill(cpid, SIGTERM);
- if (!is_sh)
- waitpid(cpid, NULL, 0);
- /* Restore SIGINT and SIGQUIT */
- sigaction(SIGINT, &sig_old_int, NULL);
- sigaction(SIGQUIT, &sig_old_quit, NULL);
- return BOOL_FALSE;
- }
- /* Read the result of script execution */
- while (read(fileno(rpipe), &buf, 1) > 0)
- write(fileno(clish_shell__get_ostream(this)), &buf, 1);
- /* Wait for the writing process */
- if (!is_sh)
- waitpid(cpid, NULL, 0);
- /* Wait for script */
- res = pclose(rpipe);
- /* Restore SIGINT and SIGQUIT */
- sigaction(SIGINT, &sig_old_int, NULL);
- sigaction(SIGQUIT, &sig_old_quit, NULL);
- } else {
- res = system(command);
- }
- lub_string_free(command);
- #ifdef DEBUG
- fprintf(stderr, "RETCODE: %d\n", WEXITSTATUS(res));
- #endif /* DEBUG */
- return (0 == res) ? BOOL_TRUE : BOOL_FALSE;
- }
- /*--------------------------------------------------------- */
- bool_t clish_dryrun_callback(clish_shell_t * this,
- const clish_command_t * cmd, const char *script)
- {
- #ifdef DEBUG
- fprintf(stderr, "DRY-RUN: %s\n", script);
- #endif /* DEBUG */
- return BOOL_TRUE;
- }
- /*--------------------------------------------------------- */
|