diff options
author | Klaus Schmidinger <vdr@tvdr.de> | 2001-10-20 10:39:27 +0200 |
---|---|---|
committer | Klaus Schmidinger <vdr@tvdr.de> | 2001-10-20 10:39:27 +0200 |
commit | 60ee85bf172c43a4104203b6dfacb3353e606e48 (patch) | |
tree | aaf52d69f89f4473cd47b0bb88283acfe7254242 /thread.c | |
parent | 312764a7ab5dd1add651a1edf186ed5b517f68f2 (diff) | |
download | vdr-60ee85bf172c43a4104203b6dfacb3353e606e48.tar.gz vdr-60ee85bf172c43a4104203b6dfacb3353e606e48.tar.bz2 |
Closing all open file descriptors when calling external programs
Diffstat (limited to 'thread.c')
-rw-r--r-- | thread.c | 34 |
1 files changed, 33 insertions, 1 deletions
@@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: thread.c 1.13 2001/09/23 14:04:35 kls Exp $ + * $Id: thread.c 1.14 2001/10/20 10:26:35 kls Exp $ */ #include "thread.h" @@ -342,3 +342,35 @@ int cPipe::Close(void) return ret; } + +// --- SystemExec ------------------------------------------------------------ + +int SystemExec(const char *Command) +{ + pid_t pid; + + if ((pid = fork()) < 0) { // fork failed + LOG_ERROR; + return -1; + } + + if (pid > 0) { // parent process + int status; + if (waitpid(pid, &status, 0) < 0) { + LOG_ERROR; + return -1; + } + return status; + } + else { // child process + int MaxPossibleFileDescriptors = getdtablesize(); + for (int i = STDERR_FILENO + 1; i < MaxPossibleFileDescriptors; i++) + close(i); //close all dup'ed filedescriptors + if (execl("/bin/sh", "sh", "-c", Command, NULL) == -1) { + LOG_ERROR_STR(Command); + _exit(-1); + } + _exit(0); + } +} + |