--- parser3/src/main/pa_exec.C 2007/04/23 10:30:31 1.69 +++ parser3/src/main/pa_exec.C 2009/08/15 13:04:10 1.76 @@ -1,13 +1,13 @@ /** @file Parser: program executing for different OS-es. - Copyright(c) 2000,2001-2005 ArtLebedev Group(http://www.artlebedev.com) + Copyright(c) 2000-2009 ArtLebedev Group(http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) @todo setrlimit */ -static const char * const IDENT_EXEC_C="$Date: 2007/04/23 10:30:31 $"; +static const char * const IDENT_EXEC_C="$Date: 2009/08/15 13:04:10 $"; #include "pa_config_includes.h" @@ -29,10 +29,10 @@ static const char * const IDENT_EXEC_C=" static DWORD CreateHiddenConsoleProcess(LPCTSTR szCmdLine, LPCTSTR szScriptFileSpec, char *szEnv, - PROCESS_INFORMATION* ppi, - LPHANDLE phInWrite, - LPHANDLE phOutRead, - LPHANDLE phErrRead) + PROCESS_INFORMATION* ppi, + LPHANDLE phInWrite, + LPHANDLE phOutRead, + LPHANDLE phErrRead) { DWORD result=0; BOOL fCreated; @@ -122,6 +122,37 @@ static void read_pipe(String& result, HA } } +static void read_pipe(File_read_result& result, HANDLE hOutRead){ + + char *buf=new(PointerFreeGC) char[MAX_STRING]; + + unsigned long size = 0; + unsigned long bufsize = 0; + unsigned long newsize = 0; + + result.headers = 0; + result.length = 0; + result.str = 0; + result.success = false; + + while(true) { + if(!ReadFile(hOutRead, buf, MAX_STRING-1, &size, NULL) || !size) + break; + newsize = (size + result.length); + if(newsize > bufsize){ + bufsize = (size==MAX_STRING-1)?newsize*2:newsize; + char *tmp = new(PointerFreeGC) char[bufsize]; + if(result.str) + memcpy(tmp, result.str, result.length); + memcpy(tmp+result.length, buf, size); + result.str = tmp; + }else{ + memcpy(result.str+result.length, buf, size); + } + result.length = newsize; + } +} + static const char* buildCommand(const char* file_spec_cstr, const ArrayString& argv) { const char* result=file_spec_cstr; if(FILE *f=fopen(file_spec_cstr, "r")) { @@ -290,6 +321,36 @@ static void read_pipe(String& result, in } } +static void read_pipe(File_read_result& result, int file){ + char *buf=new(PointerFreeGC) char[MAX_STRING]; + + unsigned long bufsize = 0; + unsigned long newsize = 0; + + result.headers = 0; + result.length = 0; + result.str = 0; + result.success = false; + + while(true) { + ssize_t size=read(file, buf, MAX_STRING-1); + if(size <= 0) + break; + newsize = (size + result.length); + if(newsize > bufsize){ + bufsize = (size==MAX_STRING-1)?newsize*2:newsize; + char *tmp = new(PointerFreeGC) char[bufsize]; + if(result.str) + memcpy(tmp, result.str, result.length); + memcpy(tmp+result.length, buf, size); + result.str = tmp; + }else{ + memcpy(result.str+result.length, buf, size); + } + result.length = newsize; + } +} + #endif #ifndef DOXYGEN @@ -302,7 +363,7 @@ struct Append_env_pair_info { #endif }; #endif -///@test maybe here and at argv construction --- cstr(String::L_UNSPECIFIED +///@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 @@ -339,7 +400,7 @@ PA_exec_result pa_exec( PROCESS_INFORMATION pi; HANDLE hInWrite, hOutRead, hErrRead; - const char* script_spec_cstr=file_spec.cstr(String::L_FILE_SPEC); + const char* script_spec_cstr=file_spec.taint_cstr(String::L_FILE_SPEC); const char* cmd=buildCommand(script_spec_cstr, argv); char* env_cstr=0; if(env) { @@ -360,8 +421,8 @@ PA_exec_result pa_exec( szErrorDesc, sizeof(szErrorDesc), (va_list *)¶m); if(error_size>3) // ".\r\n" szErrorDesc[error_size-3]=0; - - throw Exception(0, + + throw Exception("file.execute", &file_spec, "exec failed - %s (%u). Consider adding shbang line (#!x:\\interpreter\\command line)", error_size?szErrorDesc:"", error); @@ -374,7 +435,7 @@ PA_exec_result pa_exec( // without this char WriteFile(hInWrite, "\x1A", 1, &written_size, NULL); CloseHandle(hInWrite); - read_pipe(result.out, hOutRead, String::L_AS_IS); + read_pipe(result.out, hOutRead); CloseHandle(hOutRead); read_pipe(result.err, hErrRead, String::L_TAINTED); CloseHandle(hErrRead); @@ -382,16 +443,16 @@ PA_exec_result pa_exec( 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. + * to prevent handle and memory leaks. */ CloseHandle(pi.hProcess); - CloseHandle(pi.hThread); + CloseHandle(pi.hThread); } #else // execve needs non const - char* file_spec_cstr=file_spec.cstrm(String::L_FILE_SPEC); + char* file_spec_cstr=file_spec.taint_cstrm(String::L_FILE_SPEC); int pipe_write, pipe_read, pipe_err; @@ -408,7 +469,7 @@ from http://www.apache.org/websrc/cvsweb } #endif - char* argv_cstrs[1+50+1]={file_spec_cstr, 0}; + 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) @@ -434,12 +495,12 @@ from http://www.apache.org/websrc/cvsweb &pipe_write, &pipe_read, &pipe_err); if(pid>0) { // in child - if(in.length()) {// there is some in data + if(!in.is_empty()) {// there is some in data const char* in_cstr=in.cstr(); write(pipe_write, in_cstr, in.length()); } close(pipe_write); - read_pipe(result.out, pipe_read, String::L_AS_IS); + read_pipe(result.out, pipe_read); close(pipe_read); read_pipe(result.err, pipe_err, String::L_TAINTED); close(pipe_err); @@ -447,7 +508,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(0, + throw Exception("file.execute", &file_spec, "%s error: %s (%d)", pid<0?"fork":"pipe", str?str:"", errno); }