--- parser3/src/main/pa_exec.C 2003/09/25 09:15:03 1.52 +++ parser3/src/main/pa_exec.C 2007/11/15 19:44:39 1.71 @@ -1,13 +1,13 @@ /** @file Parser: program executing for different OS-es. - Copyright(c) 2000,2001-2003 ArtLebedev Group(http://www.artlebedev.com) + Copyright(c) 2000,2001-2005 ArtLebedev Group(http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) @todo setrlimit */ -static const char* IDENT_EXEC_C="$Date: 2003/09/25 09:15:03 $"; +static const char * const IDENT_EXEC_C="$Date: 2007/11/15 19:44:39 $"; #include "pa_config_includes.h" @@ -27,6 +27,7 @@ static const char* IDENT_EXEC_C="$Date: /// 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, @@ -72,10 +73,9 @@ static DWORD CreateHiddenConsoleProcess( // calculating script's directory char dir[MAX_STRING]; - strncpy(dir, szCmdLine, MAX_STRING-1); dir[MAX_STRING-1]=0; + strncpy(dir, szScriptFileSpec, MAX_STRING-1); dir[MAX_STRING-1]=0; lsplit(dir,' '); // trim arguments rsplit(dir,'/'); rsplit(dir,'\\'); // trim filename - chdir(dir); // Create a child process (suspended) fCreated=CreateProcess(NULL, @@ -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")) { @@ -132,9 +163,9 @@ static const char* buildCommand(const ch buf[size]=0; if(strncmp(buf, "#!", 2)==0) { const char* begin=buf+2; - if(*begin==' ') // alx: were an old magic for some linux-es + while(*begin==' ') // alx: were an old magic for some linux-es begin++; - if(char *end=strchr(begin, '\n')) { + if(const char *end=strchr(begin, '\n')) { String string(pa_strdup(begin, end-begin)); string << " " << file_spec_cstr; result=string.cstr(); @@ -271,7 +302,9 @@ static pid_t execve_piped(const char* fi static int get_exit_status(int pid) { int status; - if(!waitpid(pid, &status, 0)) + pid_t cid; + while ((cid=waitpid(pid, &status, WUNTRACED)) == -1 && errno == EINTR); + if(!cid) return -1; return WIFEXITED(status) ? WEXITSTATUS(status) : -2; @@ -288,13 +321,43 @@ 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 struct Append_env_pair_info { #ifdef WIN32 - String& string; - Append_env_pair_info(String& astring): string(astring) {} + String::Body& body; + Append_env_pair_info(String::Body& abody): body(abody) {} #else char **env_ref; #endif @@ -304,19 +367,22 @@ struct Append_env_pair_info { static void append_env_pair(HashStringString::key_type key, HashStringString::value_type value, Append_env_pair_info *info) { #ifdef WIN32 - info->string << key << "=" << value; - info->string.append_know_length("\1", 1, String::L_AS_IS); // placeholder for of zero byte + info->body << key << "=" << value; + info->body.append_know_length("\1", 1); // placeholder for of zero byte #else String::Body body; - body << key << "=" << value; + body << key << "=" << value.cstr(); *(info->env_ref++)=body.cstrm(); #endif } -/// @todonow unix part to smart_ptr PA_exec_result pa_exec( - bool forced_allow, + bool +#if defined(NO_PA_EXEC) || defined(PA_SAFE_MODE) + forced_allow +#endif + , const String& file_spec, const HashStringString* env, const ArrayString& argv, @@ -325,29 +391,28 @@ PA_exec_result pa_exec( #ifdef NO_PA_EXECS if(!forced_allow) - throw Exception("parser.runtime", + throw Exception(PARSER_RUNTIME, &file_spec, "parser execs are disabled [recompile parser without --disable-execs configure option]"); #endif - // execve needs non const - char* file_spec_cstr=file_spec.cstrm(String::L_FILE_SPEC); #ifdef WIN32 PROCESS_INFORMATION pi; HANDLE hInWrite, hOutRead, hErrRead; - const char* cmd=buildCommand(file_spec.cstr(String::L_FILE_SPEC), argv); - char* env_cstr; + const char* script_spec_cstr=file_spec.cstr(String::L_FILE_SPEC); + const char* cmd=buildCommand(script_spec_cstr, argv); + char* env_cstr=0; if(env) { - String string; - Append_env_pair_info info(string); - env->for_each(append_env_pair, &info); - env_cstr=info.string.cstrm(String::L_UNSPECIFIED); + String::Body body; + Append_env_pair_info info(body); + env->for_each(append_env_pair, &info); + env_cstr=info.body.cstrm(); for(char* replacer=env_cstr; *replacer; replacer++) if(*replacer=='\1') *replacer=0; } - if(DWORD error=CreateHiddenConsoleProcess(cmd, env_cstr, &pi, &hInWrite, &hOutRead, &hErrRead)) { + if(DWORD error=CreateHiddenConsoleProcess(cmd, script_spec_cstr, env_cstr, &pi, &hInWrite, &hOutRead, &hErrRead)) { char szErrorDesc[MAX_STRING]; const char* param="the file you tried to run"; size_t error_size=FormatMessage( @@ -359,8 +424,8 @@ PA_exec_result pa_exec( throw Exception(0, &file_spec, - "exec failed - %s (%ld). Consider adding shbang line (#!x:\\interpreter\\command line)", - error_size?szErrorDesc:"", (long)error); + "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; @@ -370,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); @@ -386,6 +451,9 @@ from http://www.apache.org/websrc/cvsweb #else + // execve needs non const + char* file_spec_cstr=file_spec.cstrm(String::L_FILE_SPEC); + int pipe_write, pipe_read, pipe_err; #ifdef PA_SAFE_MODE @@ -401,11 +469,11 @@ from http://www.apache.org/websrc/cvsweb } #endif - char* argv_cstrs[1+10+1]={file_spec_cstr, 0}; + char* argv_cstrs[1+50+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", + throw Exception(PARSER_RUNTIME, &file_spec, "too many arguments (%d > max %d)", argv_size, argv_max); for(int i=0; i", errno); + } #endif return result;