--- parser3/src/main/pa_exec.C 2023/09/26 20:49:10 1.97 +++ parser3/src/main/pa_exec.C 2026/10/28 20:22:22 1.108 @@ -1,7 +1,7 @@ /** @file Parser: program executing for different OS-es. - Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com) + Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com) Authors: Konstantin Morshnev , Alexandr Petrosian @todo setrlimit @@ -13,7 +13,9 @@ #include "pa_exception.h" #include "pa_common.h" -volatile const char * IDENT_PA_EXEC_C="$Id: pa_exec.C,v 1.97 2023/09/26 20:49:10 moko Exp $" IDENT_PA_EXEC_H; +volatile const char * IDENT_PA_EXEC_C="$Id: pa_exec.C,v 1.108 2026/10/28 20:22:22 moko Exp $" IDENT_PA_EXEC_H; + +THREAD_LOCAL int pa_exec_active_pid=0; #ifdef _MSC_VER @@ -81,7 +83,7 @@ static DWORD CreateHiddenConsoleProcess( char dir[MAX_STRING]; pa_strncpy(dir, szScriptFileSpec, MAX_STRING); lsplit(dir,' '); // trim arguments - rsplit(dir,'/'); rsplit(dir,'\\'); // trim filename + rsplit(dir,"/\\"); // trim filename // Create a child process (suspended) fCreated=CreateProcess(NULL, @@ -131,7 +133,7 @@ static int get_exit_status(HANDLE hProce 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) { char *buf=new(PointerFreeGC) char[MAX_STRING+1]; DWORD size=0; @@ -142,7 +144,7 @@ static void read_pipe(String& result, HA } } -static void read_pipe(File_read_result& result, HANDLE hOutRead){ +static void read_pipe(File_read_result& result, HANDLE hOutRead) { char *buf=(char*)pa_malloc(MAX_STRING+1); DWORD bufsize = MAX_STRING; @@ -165,6 +167,36 @@ static void read_pipe(File_read_result& } } +static const char* arg_quote(const char *arg) { + size_t length = strlen(arg); + if (length >= 1 && arg[0] == '-') + return arg; // avoid options qouting + if (length >= 2 && arg[0] == '"' && arg[length - 1] == '"') + return arg; // allready qouted + + size_t extra_length = 2; // opening and closing quotes + for(const char *src = arg; *src; src++){ + if(*src == '"') + extra_length++; + } + + char *result = (char *)pa_malloc(length + extra_length + 1); + char *dest = result; + + *dest++ = '"'; + + for(const char *src=arg; *src;){ + char c = *src++; + if(c == '"') + *dest++ = '"'; + *dest++ = c; + } + + *dest++ = '"'; + *dest = '\0'; + return result; +} + static const char* buildCommand(const char* file_spec_cstr, const ArrayString& argv) { const char* result=file_spec_cstr; if(FILE *f=pa_fopen(file_spec_cstr, "r")) { @@ -190,13 +222,13 @@ static const char* buildCommand(const ch } fclose(f); } + { // appending argv String string(result); for(size_t i=0; icstr()); } - result=string.cstr(); } @@ -285,7 +317,10 @@ static pid_t execve_piped(const char* fi char dir[MAX_STRING]; pa_strncpy(dir, file_spec_cstr, MAX_STRING); rsplit(dir,'/'); // trim filename - chdir(dir); + PA_UNUSED int ignore_result=chdir(dir); + + // put the child in its own process group so the response timeout can kill it and its children + setpgid(0, 0); // execute execve(file_spec_cstr, argv, env); @@ -293,6 +328,8 @@ static pid_t execve_piped(const char* fi } /* Parent process */ + + setpgid(pid, pid); // repeat in the parent in case the timeout fires before the child calls setpgid() if(pipe_out) { close(out_fds[1]); @@ -312,6 +349,12 @@ static pid_t execve_piped(const char* fi return pid; } +class Temp_exec_pid { +public: + Temp_exec_pid(pid_t pid) { pa_exec_active_pid=pid; } + ~Temp_exec_pid() { pa_exec_active_pid=0; } +}; + static int get_exit_status(int pid) { int status=0; pid_t cid; @@ -368,7 +411,6 @@ struct Append_env_pair_info { }; #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 _MSC_VER @@ -443,7 +485,7 @@ PA_exec_result pa_exec(bool forced_allow if(!forced_allow) { struct stat finfo; if(pa_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)%s", strerror(errno), errno, actual_filename(file_spec, file_spec_cstr)); check_safe_mode(finfo, file_spec, file_spec_cstr); } @@ -471,9 +513,10 @@ PA_exec_result pa_exec(bool forced_allow argv_cstrs, env_cstrs, &pipe_write, &pipe_read, &pipe_err); if(pid>0) { - // in child + // child running + Temp_exec_pid temp(pid); if(in.length>0) // there is some in data - write(pipe_write, in.str, in.length); + PA_UNUSED int ignore_result=write(pipe_write, in.str, in.length); close(pipe_write); read_pipe(result.out, pipe_read); close(pipe_read);