X-Git-Url: http://git.tuebingen.mpg.de/?p=dss.git;a=blobdiff_plain;f=exec.c;h=920ea0e2e9086bee1b5d82e72a8f051ad32efd3a;hp=13a0e4bf43cd53ab7464c3fb177490ce3c7ca166;hb=f38f86c1d4695a7016d5002e979c4f68ed12fb69;hpb=c418d2188c9c2c542270023d6fc3bc6cf34f8d29 diff --git a/exec.c b/exec.c index 13a0e4b..920ea0e 100644 --- a/exec.c +++ b/exec.c @@ -1,111 +1,67 @@ +/* + * Copyright (C) 2003-2011 Andre Noll + * + * Licensed under the GPL v2. For licencing details see COPYING. + */ + /** \file exec.c Helper functions for spawning new processes. */ #include -#include -#include -#include #include #include #include - +#include +#include #include "gcc-compat.h" -#include "error.h" -#include "string.h" - +#include "log.h" +#include "err.h" +#include "str.h" +#include "exec.h" /** - * Spawn a new process and redirect fd 0, 1, and 2. + * Spawn a new process using execvp(). * * \param pid Will hold the pid of the created process upon return. * \param file Path of the executable to execute. * \param args The argument array for the command. - * \param fds a Pointer to a value-result array. * * \return Standard. * - * \sa null(4), pipe(2), dup2(2), fork(2), exec(3). + * \sa fork(2), exec(3). */ -int dss_exec(pid_t *pid, const char *file, char *const *const args, int *fds) +void dss_exec(pid_t *pid, const char *file, char *const *const args) { - int ret, in[2] = {-1, -1}, out[2] = {-1, -1}, err[2] = {-1, -1}, - null = -1; /* ;) */ - - ret = -E_DUP_PIPE; - if (fds[0] > 0 && pipe(in) < 0) - goto err_out; - if (fds[1] > 0 && pipe(out) < 0) - goto err_out; - if (fds[2] > 0 && pipe(err) < 0) - goto err_out; - if (!fds[0] || !fds[1] || !fds[2]) { - ret = -E_NULL_OPEN; - null = open("/dev/null", O_RDONLY); - if (null < 0) - goto err_out; - } - if ((*pid = fork()) < 0) + if ((*pid = fork()) < 0) { + DSS_EMERG_LOG(("fork error: %s\n", strerror(errno))); exit(EXIT_FAILURE); - if (!(*pid)) { /* child */ - if (fds[0] >= 0) { - if (fds[0]) { - close(in[1]); - if (in[0] != STDIN_FILENO) - dup2(in[0], STDIN_FILENO); - } else - dup2(null, STDIN_FILENO); - } - if (fds[1] >= 0) { - if (fds[1]) { - close(out[0]); - if (out[1] != STDOUT_FILENO) - dup2(out[1], STDOUT_FILENO); - } else - dup2(null, STDOUT_FILENO); - } - if (fds[2] >= 0) { - if (fds[2]) { - close(err[0]); - if (err[1] != STDERR_FILENO) - dup2(err[1], STDERR_FILENO); - } else - dup2(null, STDERR_FILENO); - } - if (null >= 0) - close(null); - execvp(file, args); - _exit(EXIT_FAILURE); - } - if (fds[0] > 0) { - close(in[0]); - *fds = in[1]; - } - if (fds[1] > 0) { - close(out[1]); - *(fds + 1) = out[0]; } - if (fds[2] > 0) { - close(err[1]); - *(fds + 2) = err[0]; - } - if (null >= 0) - close(null); - return 1; -err_out: - make_err_msg("failed to exec %s", file); - if (err[0] >= 0) - close(err[0]); - if (err[1] >= 0) - close(err[1]); - if (out[0] >= 0) - close(out[0]); - if (out[1] >= 0) - close(out[1]); - if (in[0] >= 0) - close(in[0]); - if (in[1] >= 0) - close(in[1]); - if (null >= 0) - close(null); - return ret; + if (*pid) /* parent */ + return; + signal(SIGINT, SIG_DFL); + signal(SIGTERM, SIG_DFL); + signal(SIGCHLD, SIG_DFL); + execvp(file, args); + DSS_EMERG_LOG(("execvp error: %s\n", strerror(errno))); + _exit(EXIT_FAILURE); +} + +/** + * Exec the command given as a command line. + * + * \param pid Will hold the pid of the created process upon return. + * \param cmdline Holds the command and its arguments, separated by spaces. + * + * This function uses fork/exec to create a new process. + * + * \return Standard. + */ +void dss_exec_cmdline_pid(pid_t *pid, const char *cmdline) +{ + char **argv, *tmp = dss_strdup(cmdline); + + split_args(tmp, &argv, " \t"); + dss_exec(pid, argv[0], argv); + free(argv); + free(tmp); }