summaryrefslogtreecommitdiff
path: root/thread.c
diff options
context:
space:
mode:
authorKlaus Schmidinger <vdr@tvdr.de>2001-10-20 10:39:27 +0200
committerKlaus Schmidinger <vdr@tvdr.de>2001-10-20 10:39:27 +0200
commit60ee85bf172c43a4104203b6dfacb3353e606e48 (patch)
treeaaf52d69f89f4473cd47b0bb88283acfe7254242 /thread.c
parent312764a7ab5dd1add651a1edf186ed5b517f68f2 (diff)
downloadvdr-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.c34
1 files changed, 33 insertions, 1 deletions
diff --git a/thread.c b/thread.c
index f4ba76a1..878d769a 100644
--- a/thread.c
+++ b/thread.c
@@ -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);
+ }
+}
+