From 60ee85bf172c43a4104203b6dfacb3353e606e48 Mon Sep 17 00:00:00 2001 From: Klaus Schmidinger Date: Sat, 20 Oct 2001 10:39:27 +0200 Subject: [PATCH] Closing all open file descriptors when calling external programs --- HISTORY | 1 + recording.c | 4 ++-- thread.c | 34 +++++++++++++++++++++++++++++++++- thread.h | 7 ++++++- vdr.c | 4 ++-- 5 files changed, 44 insertions(+), 6 deletions(-) diff --git a/HISTORY b/HISTORY index 4614a16f..01beb2ae 100644 --- a/HISTORY +++ b/HISTORY @@ -821,3 +821,4 @@ Video Disk Recorder Revision History - Removed the "system time seen..." message. - Fixed a bug in the replay mode display when pressing the Green or Yellow button while in trick mode (thanks to Stefan Huelswitt) +- Closing all open file descriptors when calling external programs. diff --git a/recording.c b/recording.c index 2f4e1d26..fe56a6e6 100644 --- a/recording.c +++ b/recording.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: recording.c 1.41 2001/10/19 13:12:17 kls Exp $ + * $Id: recording.c 1.42 2001/10/20 10:28:28 kls Exp $ */ #include "recording.h" @@ -575,7 +575,7 @@ void cRecordingUserCommand::InvokeCommand(const char *State, const char *Recordi char *cmd; asprintf(&cmd, "%s %s '%s'", command, State, RecordingFileName); isyslog(LOG_INFO, "executing '%s'", cmd); - system(cmd); + SystemExec(cmd); delete cmd; } } 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); + } +} + diff --git a/thread.h b/thread.h index 221f9ac7..89dcdf85 100644 --- a/thread.h +++ b/thread.h @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: thread.h 1.9 2001/09/15 12:46:52 kls Exp $ + * $Id: thread.h 1.10 2001/10/20 10:25:19 kls Exp $ */ #ifndef __THREAD_H @@ -104,4 +104,9 @@ public: int Close(void); }; +// SystemExec() implements a 'system()' call that closes all unnecessary file +// descriptors in the child process. + +int SystemExec(const char *Command); + #endif //__THREAD_H diff --git a/vdr.c b/vdr.c index efba4666..6c75967e 100644 --- a/vdr.c +++ b/vdr.c @@ -22,7 +22,7 @@ * * The project's page is at http://www.cadsoft.de/people/kls/vdr * - * $Id: vdr.c 1.84 2001/10/19 13:37:24 kls Exp $ + * $Id: vdr.c 1.85 2001/10/20 10:26:54 kls Exp $ */ #include @@ -509,7 +509,7 @@ int main(int argc, char *argv[]) char *cmd; asprintf(&cmd, "%s %ld %ld %d '%s'", Shutdown, Next, Delta, Channel, File); isyslog(LOG_INFO, "executing '%s'", cmd); - system(cmd); + SystemExec(cmd); delete cmd; } else if (WatchdogTimeout > 0) {