|
|
| version 1.63, 2004/07/07 11:29:06 | version 1.87, 2015/10/28 22:50:00 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: program executing for different OS-es. | Parser: program executing for different OS-es. |
| Copyright(c) 2000,2001-2004 ArtLebedev Group(http://www.artlebedev.com) | Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| @todo setrlimit | @todo setrlimit |
| */ | */ |
| static const char * const IDENT_EXEC_C="$Date$"; | |
| #include "pa_config_includes.h" | #include "pa_config_includes.h" |
| #include "pa_exec.h" | #include "pa_exec.h" |
| #include "pa_exception.h" | #include "pa_exception.h" |
| #include "pa_common.h" | #include "pa_common.h" |
| #ifdef WIN32 | volatile const char * IDENT_PA_EXEC_C="$Id$" IDENT_PA_EXEC_H; |
| # include <windows.h> | |
| #else | |
| # include <signal.h> | |
| # include <sys/types.h> | |
| # include <sys/wait.h> | |
| #endif | |
| #ifdef WIN32 | #ifdef _MSC_VER |
| #include <windows.h> | |
| /// this func from http://www.ccas.ru/~posp/popov/spawn.htm | /// this func from http://www.ccas.ru/~posp/popov/spawn.htm |
| static DWORD CreateHiddenConsoleProcess(LPCTSTR szCmdLine, | static DWORD CreateHiddenConsoleProcess(LPCTSTR szCmdLine, |
| LPCTSTR szScriptFileSpec, | |
| char *szEnv, | char *szEnv, |
| PROCESS_INFORMATION* ppi, | PROCESS_INFORMATION* ppi, |
| LPHANDLE phInWrite, | LPHANDLE phInWrite, |
| LPHANDLE phOutRead, | LPHANDLE phOutRead, |
| LPHANDLE phErrRead) | LPHANDLE phErrRead) |
| { | { |
| DWORD result=0; | DWORD result=0; |
| BOOL fCreated; | BOOL fCreated; |
| Line 50 static DWORD CreateHiddenConsoleProcess( | Line 45 static DWORD CreateHiddenConsoleProcess( |
| // create STDIN pipe | // create STDIN pipe |
| if(!CreatePipe(&hInRead, phInWrite, &sa, 0)) | if(!CreatePipe(&hInRead, phInWrite, &sa, 0)) |
| goto error; | 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 | // create STDOUT pipe |
| if(!CreatePipe(phOutRead, &hOutWrite, &sa, 0)) | if(!CreatePipe(phOutRead, &hOutWrite, &sa, 0)) |
| goto error; | 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 | // create STDERR pipe |
| if(!CreatePipe(phErrRead, &hErrWrite, &sa, 0)) | if(!CreatePipe(phErrRead, &hErrWrite, &sa, 0)) |
| goto error; | 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 | // process startup information |
| memset(&si, 0, sizeof(si)); | memset(&si, 0, sizeof(si)); |
| si.cb=sizeof(si); | si.cb=sizeof(si); |
| Line 72 static DWORD CreateHiddenConsoleProcess( | Line 79 static DWORD CreateHiddenConsoleProcess( |
| // calculating script's directory | // calculating script's directory |
| char dir[MAX_STRING]; | 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 | lsplit(dir,' '); // trim arguments |
| rsplit(dir,'/'); rsplit(dir,'\\'); // trim filename | rsplit(dir,'/'); rsplit(dir,'\\'); // trim filename |
| chdir(dir); | |
| // Create a child process (suspended) | // Create a child process (suspended) |
| fCreated=CreateProcess(NULL, | fCreated=CreateProcess(NULL, |
| Line 111 error: | Line 117 error: |
| return result; | 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){ | static void read_pipe(String& result, HANDLE hOutRead, String::Language lang){ |
| while(true) { | while(true) { |
| char *buf=new(PointerFreeGC) char[MAX_STRING]; | char *buf=new(PointerFreeGC) char[MAX_STRING+1]; |
| unsigned long size; | DWORD size=0; |
| if(!ReadFile(hOutRead, buf, MAX_STRING-1, &size, NULL) || !size) | if(!ReadFile(hOutRead, buf, MAX_STRING, &size, NULL) || !size) |
| break; | break; |
| buf[size]=0; | buf[size]=0; |
| result.append_know_length(buf, size, lang); | result.append_know_length(buf, size, lang); |
| } | } |
| } | } |
| static void read_pipe(File_read_result& result, HANDLE hOutRead){ | |
| char *buf=(char*)pa_malloc(MAX_STRING+1); | |
| DWORD bufsize = MAX_STRING; | |
| result.headers = 0; | |
| result.length = 0; | |
| result.str = 0; | |
| result.success = false; | |
| while(true) { | |
| DWORD size=0; | |
| if(!ReadFile(hOutRead, buf + result.length, bufsize - result.length, &size, NULL) || !size) | |
| break; | |
| result.length += size; | |
| if(result.length >= bufsize){ | |
| bufsize *= 2; | |
| buf=(char*)pa_realloc(buf, bufsize+1); | |
| } | |
| result.str=buf; | |
| } | |
| } | |
| static const char* buildCommand(const char* file_spec_cstr, const ArrayString& argv) { | static const char* buildCommand(const char* file_spec_cstr, const ArrayString& argv) { |
| const char* result=file_spec_cstr; | const char* result=file_spec_cstr; |
| if(FILE *f=fopen(file_spec_cstr, "r")) { | if(FILE *f=fopen(file_spec_cstr, "r")) { |
| Line 132 static const char* buildCommand(const ch | Line 175 static const char* buildCommand(const ch |
| buf[size]=0; | buf[size]=0; |
| if(strncmp(buf, "#!", 2)==0) { | if(strncmp(buf, "#!", 2)==0) { |
| const char* begin=buf+2; | 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++; | begin++; |
| if(char *end=strchr(begin, '\n')) { | if(const char *end=strchr(begin, '\n')) { |
| String string(pa_strdup(begin, end-begin)); | String string(pa_strdup(begin, end-begin)); |
| string << " " << file_spec_cstr; | string << " " << file_spec_cstr; |
| result=string.cstr(); | result=string.cstr(); |
| Line 281 static int get_exit_status(int pid) { | Line 324 static int get_exit_status(int pid) { |
| static void read_pipe(String& result, int file, String::Language lang){ | static void read_pipe(String& result, int file, String::Language lang){ |
| while(true) { | while(true) { |
| char *buf=new(PointerFreeGC) char[MAX_STRING]; | char *buf=new(PointerFreeGC) char[MAX_STRING+1]; |
| ssize_t length=read(file, buf, MAX_STRING-1); | ssize_t length=read(file, buf, MAX_STRING); |
| if(length<=0) | if(length<=0) |
| break; | break; |
| buf[length]=0; | buf[length]=0; |
| Line 290 static void read_pipe(String& result, in | Line 333 static void read_pipe(String& result, in |
| } | } |
| } | } |
| static void read_pipe(File_read_result& result, int file){ | |
| char *buf=(char*)pa_malloc(MAX_STRING+1); | |
| ssize_t bufsize = MAX_STRING; | |
| result.headers = 0; | |
| result.length = 0; | |
| result.str = 0; | |
| result.success = false; | |
| while(true) { | |
| ssize_t size=read(file, buf + result.length, bufsize - result.length); | |
| if(size <= 0) | |
| break; | |
| result.length += size; | |
| if(result.length >= bufsize){ | |
| bufsize *= 2; | |
| buf=(char*)pa_realloc(buf, bufsize+1); | |
| } | |
| result.str=buf; | |
| } | |
| } | |
| #endif | #endif |
| #ifndef DOXYGEN | #ifndef DOXYGEN |
| struct Append_env_pair_info { | struct Append_env_pair_info { |
| #ifdef WIN32 | #ifdef _MSC_VER |
| String::Body& body; | String::Body& body; |
| Append_env_pair_info(String::Body& abody): body(abody) {} | Append_env_pair_info(String::Body& abody): body(abody) {} |
| #else | #else |
| Line 302 struct Append_env_pair_info { | Line 367 struct Append_env_pair_info { |
| #endif | #endif |
| }; | }; |
| #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, | static void append_env_pair(HashStringString::key_type key, HashStringString::value_type value, |
| Append_env_pair_info *info) { | Append_env_pair_info *info) { |
| #ifdef WIN32 | #ifdef _MSC_VER |
| info->body << key << "=" << value; | info->body << key << "=" << value; |
| info->body.append_know_length("\1", 1); // placeholder for of zero byte | info->body.append_know_length("\1", 1); // placeholder for of zero byte |
| #else | #else |
| Line 317 static void append_env_pair(HashStringSt | Line 383 static void append_env_pair(HashStringSt |
| } | } |
| PA_exec_result pa_exec( | PA_exec_result pa_exec( |
| bool | bool forced_allow, |
| #if defined(NO_PA_EXEC) || defined(PA_SAFE_MODE) | |
| forced_allow | |
| #endif | |
| , | |
| const String& file_spec, | const String& file_spec, |
| const HashStringString* env, | const HashStringString* env, |
| const ArrayString& argv, | const ArrayString& argv, |
| Line 330 PA_exec_result pa_exec( | Line 392 PA_exec_result pa_exec( |
| #ifdef NO_PA_EXECS | #ifdef NO_PA_EXECS |
| if(!forced_allow) | if(!forced_allow) |
| throw Exception("parser.runtime", | throw Exception(PARSER_RUNTIME, |
| &file_spec, | &file_spec, |
| "parser execs are disabled [recompile parser without --disable-execs configure option]"); | "parser execs are disabled [recompile parser without --disable-execs configure option]"); |
| #endif | #endif |
| #ifdef WIN32 | #ifdef _MSC_VER |
| PROCESS_INFORMATION pi; | PROCESS_INFORMATION pi; |
| HANDLE hInWrite, hOutRead, hErrRead; | HANDLE hInWrite, hOutRead, hErrRead; |
| const char* cmd=buildCommand(file_spec.cstr(String::L_FILE_SPEC), argv); | 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; | char* env_cstr=0; |
| if(env) { | if(env) { |
| String::Body body; | String::Body body; |
| Append_env_pair_info info(body); | Append_env_pair_info info(body); |
| env->for_each(append_env_pair, &info); | env->for_each<Append_env_pair_info*>(append_env_pair, &info); |
| env_cstr=info.body.cstrm(); | env_cstr=info.body.cstrm(); |
| for(char* replacer=env_cstr; *replacer; replacer++) | for(char* replacer=env_cstr; *replacer; replacer++) |
| if(*replacer=='\1') | if(*replacer=='\1') |
| *replacer=0; | *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]; | char szErrorDesc[MAX_STRING]; |
| const char* param="the file you tried to run"; | const char* param="the file you tried to run"; |
| size_t error_size=FormatMessage( | size_t error_size=FormatMessage( |
| Line 359 PA_exec_result pa_exec( | Line 422 PA_exec_result pa_exec( |
| szErrorDesc, sizeof(szErrorDesc), (va_list *)¶m); | szErrorDesc, sizeof(szErrorDesc), (va_list *)¶m); |
| if(error_size>3) // ".\r\n" | if(error_size>3) // ".\r\n" |
| szErrorDesc[error_size-3]=0; | szErrorDesc[error_size-3]=0; |
| throw Exception(0, | throw Exception("file.execute", |
| &file_spec, | &file_spec, |
| "exec failed - %s (%u). Consider adding shbang line (#!x:\\interpreter\\command line)", | "exec failed - %s (%u). Consider adding shbang line (#!x:\\interpreter\\command line)", |
| error_size?szErrorDesc:"<unknown>", error); | error_size?szErrorDesc:"<unknown>", error); |
| Line 368 PA_exec_result pa_exec( | Line 431 PA_exec_result pa_exec( |
| const char* in_cstr=in.cstr(); | const char* in_cstr=in.cstr(); |
| DWORD written_size; | DWORD written_size; |
| WriteFile(hInWrite, in_cstr, in.length(), &written_size, NULL); | 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); | |
| CloseHandle(hInWrite); | CloseHandle(hInWrite); |
| read_pipe(result.out, hOutRead, String::L_AS_IS); | read_pipe(result.out, hOutRead); |
| CloseHandle(hOutRead); | CloseHandle(hOutRead); |
| read_pipe(result.err, hErrRead, String::L_TAINTED); | read_pipe(result.err, hErrRead, String::L_TAINTED); |
| CloseHandle(hErrRead); | CloseHandle(hErrRead); |
| /* | result.status=get_exit_status(pi.hProcess); |
| 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. | |
| * We must close the handles to the new process and its main thread | |
| * to prevent handle and memory leaks. | |
| */ | |
| CloseHandle(pi.hProcess); | CloseHandle(pi.hProcess); |
| CloseHandle(pi.hThread); | CloseHandle(pi.hThread); |
| } | } |
| #else | #else |
| // execve needs non const | // 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; | int pipe_write, pipe_read, pipe_err; |
| #ifdef PA_SAFE_MODE | |
| if(!forced_allow) { | if(!forced_allow) { |
| struct stat finfo; | struct stat finfo; |
| if(stat(file_spec_cstr, &finfo)!=0) | if(stat(file_spec_cstr, &finfo)!=0) |
| Line 405 from http://www.apache.org/websrc/cvsweb | Line 460 from http://www.apache.org/websrc/cvsweb |
| check_safe_mode(finfo, file_spec, file_spec_cstr); | check_safe_mode(finfo, file_spec, file_spec_cstr); |
| } | } |
| #endif | |
| char* argv_cstrs[1+10+1]={file_spec_cstr, 0}; | char* argv_cstrs[1+100+1]={file_spec_cstr, 0}; |
| const int argv_size=argv.count(); | const int argv_size=argv.count(); |
| const int argv_max=sizeof(argv_cstrs)/sizeof(argv_cstrs[0])-1-1; | const int argv_max=sizeof(argv_cstrs)/sizeof(argv_cstrs[0])-1-1; |
| if(argv_size>argv_max) | if(argv_size>argv_max) |
| throw Exception("parser.runtime", | throw Exception(PARSER_RUNTIME, |
| &file_spec, | &file_spec, |
| "too many arguments (%d > max %d)", argv_size, argv_max); | "too many arguments (%d > max %d)", argv_size, argv_max); |
| for(int i=0; i<argv_size; i++) | for(int i=0; i<argv_size; i++) |
| Line 433 from http://www.apache.org/websrc/cvsweb | Line 487 from http://www.apache.org/websrc/cvsweb |
| &pipe_write, &pipe_read, &pipe_err); | &pipe_write, &pipe_read, &pipe_err); |
| if(pid>0) { | if(pid>0) { |
| // in child | // 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(); | const char* in_cstr=in.cstr(); |
| write(pipe_write, in_cstr, in.length()); | write(pipe_write, in_cstr, in.length()); |
| } | } |
| close(pipe_write); | close(pipe_write); |
| read_pipe(result.out, pipe_read, String::L_AS_IS); | read_pipe(result.out, pipe_read); |
| close(pipe_read); | close(pipe_read); |
| read_pipe(result.err, pipe_err, String::L_TAINTED); | read_pipe(result.err, pipe_err, String::L_TAINTED); |
| close(pipe_err); | close(pipe_err); |
| Line 446 from http://www.apache.org/websrc/cvsweb | Line 500 from http://www.apache.org/websrc/cvsweb |
| result.status=get_exit_status(pid); // negative may mean "-errno[execl()]" | result.status=get_exit_status(pid); // negative may mean "-errno[execl()]" |
| } else { | } else { |
| const char* str=strerror(errno); | const char* str=strerror(errno); |
| throw Exception(0, | throw Exception("file.execute", |
| &file_spec, | &file_spec, |
| "%s error: %s (%d)", pid<0?"fork":"pipe", str?str:"<unknown>", errno); | "%s error: %s (%d)", pid<0?"fork":"pipe", str?str:"<unknown>", errno); |
| } | } |