--- parser3/src/main/pa_exec.C 2009/10/02 01:18:27 1.78 +++ parser3/src/main/pa_exec.C 2016/11/28 22:42:58 1.89 @@ -1,38 +1,32 @@ /** @file Parser: program executing for different OS-es. - Copyright(c) 2000-2009 ArtLebedev Group(http://www.artlebedev.com) + Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) @todo setrlimit */ -static const char * const IDENT_EXEC_C="$Date: 2009/10/02 01:18:27 $"; - #include "pa_config_includes.h" #include "pa_exec.h" #include "pa_exception.h" #include "pa_common.h" -#ifdef WIN32 -# include -#else -# include -# include -# include -#endif +volatile const char * IDENT_PA_EXEC_C="$Id: pa_exec.C,v 1.89 2016/11/28 22:42:58 moko Exp $" IDENT_PA_EXEC_H; -#ifdef WIN32 +#ifdef _MSC_VER + +#include /// this func from http://www.ccas.ru/~posp/popov/spawn.htm static DWORD CreateHiddenConsoleProcess(LPCTSTR szCmdLine, - LPCTSTR szScriptFileSpec, - char *szEnv, - PROCESS_INFORMATION* ppi, - LPHANDLE phInWrite, - LPHANDLE phOutRead, - LPHANDLE phErrRead) + LPCTSTR szScriptFileSpec, + char *szEnv, + PROCESS_INFORMATION* ppi, + LPHANDLE phInWrite, + LPHANDLE phOutRead, + LPHANDLE phErrRead) { DWORD result=0; BOOL fCreated; @@ -51,15 +45,27 @@ static DWORD CreateHiddenConsoleProcess( // create STDIN pipe if(!CreatePipe(&hInRead, phInWrite, &sa, 0)) goto error; - + + // Ensure the write handle to the pipe for STDIN is not inherited. + if (!SetHandleInformation(*phInWrite, HANDLE_FLAG_INHERIT, 0)) + goto error; + // create STDOUT pipe if(!CreatePipe(phOutRead, &hOutWrite, &sa, 0)) goto error; + // Ensure the read handle to the pipe for STDOUT is not inherited. + if (!SetHandleInformation(*phOutRead, HANDLE_FLAG_INHERIT, 0)) + goto error; + // create STDERR pipe if(!CreatePipe(phErrRead, &hErrWrite, &sa, 0)) goto error; + // Ensure the read handle to the pipe for STDERR is not inherited. + if (!SetHandleInformation(*phErrRead, HANDLE_FLAG_INHERIT, 0)) + goto error; + // process startup information memset(&si, 0, sizeof(si)); si.cb=sizeof(si); @@ -111,6 +117,20 @@ error: return result; } +static int get_exit_status(HANDLE hProcess) { + DWORD dwExitCode = 0; + if(!GetExitCodeProcess(hProcess, &dwExitCode)) + return -1; + if(dwExitCode != STILL_ACTIVE) + return dwExitCode; + // wait for 1 second for process to exit + if(WaitForSingleObject(hProcess, 1000) != WAIT_OBJECT_0) + return -2; + if(!GetExitCodeProcess(hProcess, &dwExitCode)) + return -1; + return dwExitCode; +} + static void read_pipe(String& result, HANDLE hOutRead, String::Language lang){ while(true) { char *buf=new(PointerFreeGC) char[MAX_STRING+1]; @@ -339,7 +359,7 @@ static void read_pipe(File_read_result& #ifndef DOXYGEN struct Append_env_pair_info { -#ifdef WIN32 +#ifdef _MSC_VER String::Body& body; Append_env_pair_info(String::Body& abody): body(abody) {} #else @@ -347,10 +367,11 @@ struct Append_env_pair_info { #endif }; #endif + ///@test maybe here and at argv construction --- untaint_cstr(String::L_AS_IS static void append_env_pair(HashStringString::key_type key, HashStringString::value_type value, Append_env_pair_info *info) { -#ifdef WIN32 +#ifdef _MSC_VER info->body << key << "=" << value; info->body.append_know_length("\1", 1); // placeholder for of zero byte #else @@ -361,26 +382,15 @@ static void append_env_pair(HashStringSt #endif } -PA_exec_result pa_exec( - bool -#if defined(NO_PA_EXEC) || defined(PA_SAFE_MODE) - forced_allow -#endif - , - const String& file_spec, - const HashStringString* env, - const ArrayString& argv, - String& in) { +PA_exec_result pa_exec(bool forced_allow, const String& file_spec, const HashStringString* env, const ArrayString& argv, String::C in) { PA_exec_result result; #ifdef NO_PA_EXECS if(!forced_allow) - throw Exception(PARSER_RUNTIME, - &file_spec, - "parser execs are disabled [recompile parser without --disable-execs configure option]"); + throw Exception(PARSER_RUNTIME, &file_spec, "parser execs are disabled [recompile parser without --disable-execs configure option]"); #endif -#ifdef WIN32 +#ifdef _MSC_VER PROCESS_INFORMATION pi; HANDLE hInWrite, hOutRead, hErrRead; @@ -406,29 +416,19 @@ PA_exec_result pa_exec( if(error_size>3) // ".\r\n" szErrorDesc[error_size-3]=0; - throw Exception("file.execute", - &file_spec, - "exec failed - %s (%u). Consider adding shbang line (#!x:\\interpreter\\command line)", - error_size?szErrorDesc:"", error); + throw Exception("file.execute", &file_spec, "exec failed - %s (%u). Consider adding shbang line (#!x:\\interpreter\\command line)", error_size ? szErrorDesc : "", error); } else { - const char* in_cstr=in.cstr(); DWORD written_size; - WriteFile(hInWrite, in_cstr, in.length(), &written_size, NULL); - // EOF for stupid text reads - // normally they should read CONTENT_LENGTH bytes, - // without this char - WriteFile(hInWrite, "\x1A", 1, &written_size, NULL); + if(in.length>0) + WriteFile(hInWrite, in.str, in.length, &written_size, NULL); CloseHandle(hInWrite); read_pipe(result.out, hOutRead); CloseHandle(hOutRead); - read_pipe(result.err, hErrRead, String::L_TAINTED); + read_pipe(result.err, hErrRead, String::L_TAINTED); CloseHandle(hErrRead); -/* -from http://www.apache.org/websrc/cvsweb.cgi/apache-1.3/src/main/util_script.c?rev=1.151&content-type=text/vnd.viewcvs-markup - - * We must close the handles to the new process and its main thread - * to prevent handle and memory leaks. -*/ + result.status=get_exit_status(pi.hProcess); + // We must close the handles to the new process and its main thread + // to prevent handle and memory leaks. CloseHandle(pi.hProcess); CloseHandle(pi.hThread); } @@ -440,26 +440,19 @@ from http://www.apache.org/websrc/cvsweb int pipe_write, pipe_read, pipe_err; -#ifdef PA_SAFE_MODE if(!forced_allow) { struct stat finfo; if(stat(file_spec_cstr, &finfo)!=0) - throw Exception("file.missing", - &file_spec, - "stat failed: %s (%d), actual filename '%s'", - strerror(errno), errno, file_spec_cstr); + throw Exception("file.missing", &file_spec, "stat failed: %s (%d), actual filename '%s'", strerror(errno), errno, file_spec_cstr); check_safe_mode(finfo, file_spec, file_spec_cstr); } -#endif char* argv_cstrs[1+100+1]={file_spec_cstr, 0}; const int argv_size=argv.count(); const int argv_max=sizeof(argv_cstrs)/sizeof(argv_cstrs[0])-1-1; if(argv_size>argv_max) - throw Exception(PARSER_RUNTIME, - &file_spec, - "too many arguments (%d > max %d)", argv_size, argv_max); + throw Exception(PARSER_RUNTIME, &file_spec, "too many arguments (%d > max %d)", argv_size, argv_max); for(int i=0; icstrm(); argv_cstrs[1+argv_size]=0; @@ -479,10 +472,8 @@ from http://www.apache.org/websrc/cvsweb &pipe_write, &pipe_read, &pipe_err); if(pid>0) { // in child - if(!in.is_empty()) {// there is some in data - const char* in_cstr=in.cstr(); - write(pipe_write, in_cstr, in.length()); - } + if(in.length>0) // there is some in data + write(pipe_write, in.str, in.length); close(pipe_write); read_pipe(result.out, pipe_read); close(pipe_read); @@ -492,9 +483,7 @@ from http://www.apache.org/websrc/cvsweb result.status=get_exit_status(pid); // negative may mean "-errno[execl()]" } else { const char* str=strerror(errno); - throw Exception("file.execute", - &file_spec, - "%s error: %s (%d)", pid<0?"fork":"pipe", str?str:"", errno); + throw Exception("file.execute", &file_spec, "%s error: %s (%d)", pid<0?"fork":"pipe", str?str:"", errno); } #endif