Diff for /parser3/src/main/pa_exec.C between versions 1.48.2.18.2.12 and 1.71

version 1.48.2.18.2.12, 2003/03/27 14:51:28 version 1.71, 2007/11/15 19:44:39
Line 1 Line 1
 /** @file  /** @file
         Parser: program executing for different OS-es.          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 <paf@design.ru> (http://paf.design.ru)          Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
   
         @todo setrlimit          @todo setrlimit
 */  */
   
 static const char* IDENT_EXEC_C="$Date$";  static const char * const IDENT_EXEC_C="$Date$";
   
 #include "pa_config_includes.h"  #include "pa_config_includes.h"
   
Line 27  static const char* IDENT_EXEC_C="$Date$" Line 27  static const char* IDENT_EXEC_C="$Date$"
   
 /// 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;
     STARTUPINFO si;          STARTUPINFO si;
     SECURITY_ATTRIBUTES sa={0};          SECURITY_ATTRIBUTES sa={0};
     HANDLE hInRead;          HANDLE hInRead;
     HANDLE hOutWrite;          HANDLE hOutWrite;
     HANDLE hErrWrite;          HANDLE hErrWrite;
           
     // Create pipes          // Create pipes
     // initialize security attributes for handle inheritance (for WinNT)          // initialize security attributes for handle inheritance (for WinNT)
     sa.nLength=sizeof(sa);          sa.nLength=sizeof(sa);
     sa.bInheritHandle=TRUE;          sa.bInheritHandle=TRUE;
     sa.lpSecurityDescriptor=NULL;          sa.lpSecurityDescriptor=NULL;
           
     // create STDIN pipe          // create STDIN pipe
     if(!CreatePipe(&hInRead, phInWrite, &sa, 0))          if(!CreatePipe(&hInRead, phInWrite, &sa, 0))
         goto error;                  goto error;
           
     // create STDOUT pipe          // create STDOUT pipe
     if(!CreatePipe(phOutRead, &hOutWrite, &sa, 0))          if(!CreatePipe(phOutRead, &hOutWrite, &sa, 0))
         goto error;                  goto error;
           
     // create STDERR pipe          // create STDERR pipe
     if(!CreatePipe(phErrRead, &hErrWrite, &sa, 0))          if(!CreatePipe(phErrRead, &hErrWrite, &sa, 0))
         goto error;                  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); 
     si.dwFlags=STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;          si.dwFlags=STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
     // child process' console must be hidden for Win95 compatibility          // child process' console must be hidden for Win95 compatibility
     si.wShowWindow=SW_HIDE;          si.wShowWindow=SW_HIDE;
     // assign "other" sides of pipes          // assign "other" sides of pipes
     si.hStdInput=hInRead;          si.hStdInput=hInRead;
     si.hStdOutput=hOutWrite;          si.hStdOutput=hOutWrite;
     si.hStdError=hErrWrite;          si.hStdError=hErrWrite;
           
         // 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,                  (LPTSTR)szCmdLine,
                               (LPTSTR)szCmdLine,                  NULL,
                               NULL,                  NULL,
                               NULL,                  TRUE,
                               TRUE,                  CREATE_NO_WINDOW,
                               CREATE_NO_WINDOW,                  szEnv,
                               szEnv,                  dir,
                               dir,                  &si,
                               &si,                  ppi);
                               ppi);  
         if(!fCreated)          if(!fCreated)
                 result=GetLastError();                  result=GetLastError();
           
     CloseHandle(hInRead);          CloseHandle(hInRead);
     CloseHandle(hOutWrite);          CloseHandle(hOutWrite);
     CloseHandle(hErrWrite);          CloseHandle(hErrWrite);
           
     if(!fCreated)          if(!fCreated)
         goto error;                  goto error;
           
     return result;          return result;
           
 error:  error:
         if(!result/*yet*/)          if(!result/*yet*/)
                 result=GetLastError(); // get it                  result=GetLastError(); // get it
           
     CloseHandle(*phInWrite);          CloseHandle(*phInWrite);
     CloseHandle(*phOutRead);          CloseHandle(*phOutRead);
     CloseHandle(*phErrRead);          CloseHandle(*phErrRead);
           
     return result;          return result;
 }  }
   
 static void read_pipe(String& result, HANDLE hOutRead, String::Language lang){  static void read_pipe(String& result, HANDLE hOutRead, String::Language lang){
Line 118  static void read_pipe(String& result, HA Line 118  static void read_pipe(String& result, HA
                 if(!ReadFile(hOutRead, buf, MAX_STRING-1, &size, NULL) || !size)                   if(!ReadFile(hOutRead, buf, MAX_STRING-1, &size, NULL) || !size) 
                         break;                          break;
                 buf[size]=0;                  buf[size]=0;
                 result.append_help_length(buf, size, lang);                  result.append_know_length(buf, size, lang);
           }
   }
   
   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;
         }          }
 }  }
   
Line 132  static const char* buildCommand(const ch Line 163  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(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 271  static pid_t execve_piped(const char* fi Line 302  static pid_t execve_piped(const char* fi
   
 static int get_exit_status(int pid) {  static int get_exit_status(int pid) {
         int status;          int status;
         if(!waitpid(pid, &status, 0))          pid_t cid;
           while ((cid=waitpid(pid, &status, WUNTRACED)) == -1 && errno == EINTR); 
           if(!cid)
                 return -1;                  return -1;
         return WIFEXITED(status) ?           return WIFEXITED(status) ? 
                 WEXITSTATUS(status) : -2;                  WEXITSTATUS(status) : -2;
Line 280  static int get_exit_status(int pid) { Line 313  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];
                 ssize_t size=read(file, buf, MAX_STRING);                  ssize_t length=read(file, buf, MAX_STRING-1);
                 if(size<=0)                  if(length<=0)
                           break;
                   buf[length]=0;
                   result.append_know_length(buf, length, lang);
           }
   }
   
   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;                          break;
                 result.append(buf, size, lang);                  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;
         }          }
 }  }
   
Line 292  static void read_pipe(String& result, in Line 356  static void read_pipe(String& result, in
 #ifndef DOXYGEN  #ifndef DOXYGEN
 struct Append_env_pair_info {  struct Append_env_pair_info {
 #ifdef WIN32  #ifdef WIN32
         String& string;          String::Body& body;
         Append_env_pair_info(String& astring): string(astring) {}          Append_env_pair_info(String::Body& abody): body(abody) {}
 #else  #else
         char **env_ref;          char **env_ref;
 #endif  #endif
Line 303  struct Append_env_pair_info { Line 367  struct Append_env_pair_info {
 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 WIN32
         info->string << key << "=" << value;          info->body << key << "=" << value;
         info->string.append_help_length("", 1, String::L_AS_IS); // zero byte          info->body.append_know_length("\1", 1); // placeholder for of zero byte
 #else  #else
         StringBody body;          String::Body body;
         body << key << "=" << value;          body << key << "=" << value.cstr();
   
         *(info->env_ref++)=body.cstrm();          *(info->env_ref++)=body.cstrm();
 #endif  #endif
 }  }
   
 /// @todonow unix part to smart_ptr  
 PA_exec_result pa_exec(  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 String& file_spec, 
                         const HashStringString* env,                           const HashStringString* env, 
                         const ArrayString& argv,                           const ArrayString& argv, 
Line 324  PA_exec_result pa_exec( Line 391  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
   
         // execve needs non const  
         char* file_spec_cstr=file_spec.cstrm(String::L_FILE_SPEC);   
 #ifdef WIN32  #ifdef WIN32
   
         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.cstr(String::L_FILE_SPEC);
         char* env_cstr;          const char* cmd=buildCommand(script_spec_cstr, argv);
           char* env_cstr=0;
         if(env) {          if(env) {
                 String string;                  String::Body body;
                 Append_env_pair_info info(string);                  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.string.cstrm(String::L_UNSPECIFIED);                  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];                  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 355  PA_exec_result pa_exec( Line 424  PA_exec_result pa_exec(
                           
                 throw Exception(0,                  throw Exception(0,
                         &file_spec,                          &file_spec,
                         "exec failed - %s (%ld). Consider adding shbang line (#!x:\\interpreter\\command line)",                           "exec failed - %s (%u). Consider adding shbang line (#!x:\\interpreter\\command line)", 
                                 error_size?szErrorDesc:"<unknown>", (long)error);                                  error_size?szErrorDesc:"<unknown>", error);
         } else {          } else {
                 const char* in_cstr=in.cstr();                  const char* in_cstr=in.cstr();
                 DWORD written_size;                  DWORD written_size;
Line 366  PA_exec_result pa_exec( Line 435  PA_exec_result pa_exec(
                 // without this char                  // without this char
                 WriteFile(hInWrite, "\x1A", 1, &written_size, NULL);                  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);
Line 382  from http://www.apache.org/websrc/cvsweb Line 451  from http://www.apache.org/websrc/cvsweb
   
 #else  #else
   
           // execve needs non const
           char* file_spec_cstr=file_spec.cstrm(String::L_FILE_SPEC); 
   
         int pipe_write, pipe_read, pipe_err;          int pipe_write, pipe_read, pipe_err;
   
 #ifdef PA_SAFE_MODE  #ifdef PA_SAFE_MODE
Line 393  from http://www.apache.org/websrc/cvsweb Line 465  from http://www.apache.org/websrc/cvsweb
                                 "stat failed: %s (%d), actual filename '%s'",                                   "stat failed: %s (%d), actual filename '%s'", 
                                         strerror(errno), errno, file_spec_cstr);                                          strerror(errno), errno, file_spec_cstr);
   
                 if(finfo.st_uid/*foreign?*/!=geteuid()                  check_safe_mode(finfo, file_spec, file_spec_cstr);
                         && finfo.st_gid/*foreign?*/!=getegid())  
                         throw Exception("parser.runtime",   
                                 &file_spec,   
                                 "parser is in safe mode: executing files of foreign group and user disabled [recompile parser with --disable-safe-mode configure option], actual filename '%s'",   
                                         file_spec_cstr);  
         }          }
 #endif  #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_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 500  from http://www.apache.org/websrc/cvsweb
                         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);
   
                 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);
                 throw Exception(0,                  throw Exception(0,
                         &file_spec,                          &file_spec,
                         "%s error: %s (%d)", pid<0?"fork":"pipe", strerror(errno), errno);                           "%s error: %s (%d)", pid<0?"fork":"pipe", str?str:"<unknown>", errno); 
           }
 #endif  #endif
   
         return result;          return result;

Removed from v.1.48.2.18.2.12  
changed lines
  Added in v.1.71


E-mail: