From c32875509d3a78e124a0d59dac36f2dd8f44f343 Mon Sep 17 00:00:00 2001 From: Charl Wentzel Date: Mon, 17 Jul 2017 22:51:30 +0200 Subject: [PATCH] Minor updates: - JSONparseCore: - Set File mode on create - SelectableCore: - Check pipe() return value on create - Validate replacement of stdin with pipe --- JSONparseCore.cpp | 2 +- SelectableCore.cpp | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/JSONparseCore.cpp b/JSONparseCore.cpp index 5d0f4c0..9c27c66 100644 --- a/JSONparseCore.cpp +++ b/JSONparseCore.cpp @@ -105,7 +105,7 @@ bool CJSONparse::WriteToFile( const char * BasePath, const char * FilePath, cons } // Open file - if ((Handle = open( FilePath, O_CREAT|O_WRONLY|O_TRUNC )) < 0) { + if ((Handle = open( FilePath, O_CREAT|O_WRONLY|O_TRUNC, 660 )) < 0) { Error = true; sprintf( ErrorText, "Could not open file" ); return false; diff --git a/SelectableCore.cpp b/SelectableCore.cpp index 33bc3c2..820e842 100644 --- a/SelectableCore.cpp +++ b/SelectableCore.cpp @@ -458,6 +458,7 @@ int CSelectableCore::OpenPort( THandle * Handle ) int CSelectableCore::OpenForkPipe( THandle * Handle ) { int pipefd[2]; + int newfd; char * Args[50]; int Count = 0; @@ -477,8 +478,7 @@ int CSelectableCore::OpenForkPipe( THandle * Handle ) } // Create Pipe - pipe( pipefd ); - if ((pipefd[0] == -1) || (pipefd[1] == -1)) + if ((pipe( pipefd ) == -1) || (pipefd[0] == -1) || (pipefd[1] == -1)) { // Log event if (Log) Log->Message( LogLevel, dlMedium, "%s: Handle '%s' - Could not open Pipe", Name, Handle->Name ); @@ -512,15 +512,20 @@ int CSelectableCore::OpenForkPipe( THandle * Handle ) else { // Fork success - this is child - // Replace stdin with Read-end of pipe - close( 0 ); - dup( pipefd[0] ); - close( pipefd[0] ); - // Close Write-end of pipe close( pipefd[1] ); - // Replace Processing image + // Replace stdin with Read-end of pipe + close( 0 ); + newfd = dup2( pipefd[0], 0 ); + close( pipefd[0] ); + if (newfd != 0) { + // Replace failed, exit immediately + if (Log) Log->Message( LogLevel, dlMedium, "%s: Handle '%s' - Stdin redirect failed on forked process", Name, Handle->Name ); + exit( EXIT_FAILURE ); + } + + // Fork : Replace Processing image BuildArgs( Handle->Path, Count, Args ); // printf( "Execute params [%d]:\n", Count ); // int i = 0; @@ -532,7 +537,7 @@ int CSelectableCore::OpenForkPipe( THandle * Handle ) // Replace failed, exit immediately if (Log) Log->Message( LogLevel, dlMedium, "%s: Handle '%s' - Exec on forked process failed", Name, Handle->Name ); - exit(127); + exit( EXIT_FAILURE ); } // Add to Select Lists