X-Git-Url: http://git.tuebingen.mpg.de/?p=dss.git;a=blobdiff_plain;f=exec.c;h=2887cb29972b949f67bc4b4197652c8c8592b261;hp=13a0e4bf43cd53ab7464c3fb177490ce3c7ca166;hb=17eea857c175123d68e7dffc28134befa4c0aa5f;hpb=c418d2188c9c2c542270023d6fc3bc6cf34f8d29 diff --git a/exec.c b/exec.c index 13a0e4b..2887cb2 100644 --- a/exec.c +++ b/exec.c @@ -1,3 +1,9 @@ +/* + * Copyright (C) 2003-2010 Andre Noll + * + * Licensed under the GPL v2. For licencing details see COPYING. + */ + /** \file exec.c Helper functions for spawning new processes. */ #include @@ -7,9 +13,11 @@ #include #include #include +#include #include "gcc-compat.h" +#include "log.h" #include "error.h" #include "string.h" @@ -40,7 +48,7 @@ int dss_exec(pid_t *pid, const char *file, char *const *const args, int *fds) goto err_out; if (!fds[0] || !fds[1] || !fds[2]) { ret = -E_NULL_OPEN; - null = open("/dev/null", O_RDONLY); + null = open("/dev/null", O_RDWR); if (null < 0) goto err_out; } @@ -73,6 +81,9 @@ int dss_exec(pid_t *pid, const char *file, char *const *const args, int *fds) } if (null >= 0) close(null); + signal(SIGINT, SIG_DFL); + signal(SIGTERM, SIG_DFL); + signal(SIGCHLD, SIG_DFL); execvp(file, args); _exit(EXIT_FAILURE); } @@ -92,7 +103,7 @@ int dss_exec(pid_t *pid, const char *file, char *const *const args, int *fds) close(null); return 1; err_out: - make_err_msg("failed to exec %s", file); + DSS_ERROR_LOG("failed to exec %s\n", file); if (err[0] >= 0) close(err[0]); if (err[1] >= 0) @@ -109,3 +120,42 @@ err_out: close(null); return ret; } + + +/** + * Exec the given command. + * + * \param pid Will hold the pid of the created process upon return. + * \param cmdline Holds the command and its arguments, seperated by spaces. + * \param fds A pointer to a value-result array. + * + * This function uses fork/exec to create a new process. \a fds must be a + * pointer to three integers, corresponding to stdin, stdout and stderr + * respectively. It specifies how to deal with fd 0, 1, 2 in the child. The + * contents of \a fds are interpreted as follows: + * + * - fd[i] < 0: leave fd \a i alone. + * - fd[i] = 0: dup fd \a i to \p /dev/null. + * - fd[i] > 0: create a pipe and dup i to one end of that pipe. + * Upon return, fd[i] contains the file descriptor of the pipe. + * + * In any case, all unneeded filedescriptors are closed. + * + * \return Standard. + */ +int dss_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds) +{ + int argc, ret; + char **argv; + char *tmp = dss_strdup(cmdline); + + if (!tmp) + exit(EXIT_FAILURE); + argc = split_args(tmp, &argv, " \t"); + ret = dss_exec(pid, argv[0], argv, fds); + free(argv); + free(tmp); + return ret; +} + +