Annotation of parser3/src/main/pa_exec.C, revision 1.70
1.1 paf 1: /** @file
2: Parser: program executing for different OS-es.
3:
1.67 paf 4: Copyright(c) 2000,2001-2005 ArtLebedev Group(http://www.artlebedev.com)
1.25 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 paf 6:
1.18 paf 7: @todo setrlimit
1.1 paf 8: */
1.38 paf 9:
1.70 ! misha 10: static const char * const IDENT_EXEC_C="$Date: 2007/04/23 10:30:31 $";
1.1 paf 11:
12: #include "pa_config_includes.h"
13:
1.19 paf 14: #include "pa_exec.h"
15: #include "pa_exception.h"
16: #include "pa_common.h"
17:
1.1 paf 18: #ifdef WIN32
19: # include <windows.h>
20: #else
21: # include <signal.h>
22: # include <sys/types.h>
23: # include <sys/wait.h>
24: #endif
25:
26: #ifdef WIN32
27:
28: /// this func from http://www.ccas.ru/~posp/popov/spawn.htm
1.36 paf 29: static DWORD CreateHiddenConsoleProcess(LPCTSTR szCmdLine,
1.65 paf 30: LPCTSTR szScriptFileSpec,
1.51 paf 31: char *szEnv,
1.1 paf 32: PROCESS_INFORMATION* ppi,
33: LPHANDLE phInWrite,
34: LPHANDLE phOutRead,
1.9 parser 35: LPHANDLE phErrRead)
1.1 paf 36: {
1.51 paf 37: DWORD result=0;
1.36 paf 38: BOOL fCreated;
1.51 paf 39: STARTUPINFO si;
40: SECURITY_ATTRIBUTES sa={0};
41: HANDLE hInRead;
42: HANDLE hOutWrite;
43: HANDLE hErrWrite;
44:
45: // Create pipes
46: // initialize security attributes for handle inheritance (for WinNT)
47: sa.nLength=sizeof(sa);
48: sa.bInheritHandle=TRUE;
49: sa.lpSecurityDescriptor=NULL;
50:
51: // create STDIN pipe
52: if(!CreatePipe(&hInRead, phInWrite, &sa, 0))
53: goto error;
54:
55: // create STDOUT pipe
56: if(!CreatePipe(phOutRead, &hOutWrite, &sa, 0))
57: goto error;
58:
59: // create STDERR pipe
60: if(!CreatePipe(phErrRead, &hErrWrite, &sa, 0))
61: goto error;
62:
63: // process startup information
64: memset(&si, 0, sizeof(si));
65: si.cb=sizeof(si);
66: si.dwFlags=STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
67: // child process' console must be hidden for Win95 compatibility
68: si.wShowWindow=SW_HIDE;
69: // assign "other" sides of pipes
70: si.hStdInput=hInRead;
71: si.hStdOutput=hOutWrite;
72: si.hStdError=hErrWrite;
73:
1.31 paf 74: // calculating script's directory
75: char dir[MAX_STRING];
1.65 paf 76: strncpy(dir, szScriptFileSpec, MAX_STRING-1); dir[MAX_STRING-1]=0;
1.33 paf 77: lsplit(dir,' '); // trim arguments
1.31 paf 78: rsplit(dir,'/'); rsplit(dir,'\\'); // trim filename
1.51 paf 79:
80: // Create a child process (suspended)
81: fCreated=CreateProcess(NULL,
82: (LPTSTR)szCmdLine,
83: NULL,
84: NULL,
85: TRUE,
86: CREATE_NO_WINDOW,
87: szEnv,
88: dir,
89: &si,
90: ppi);
1.36 paf 91: if(!fCreated)
92: result=GetLastError();
1.51 paf 93:
94: CloseHandle(hInRead);
95: CloseHandle(hOutWrite);
96: CloseHandle(hErrWrite);
97:
98: if(!fCreated)
99: goto error;
100:
101: return result;
102:
1.1 paf 103: error:
1.36 paf 104: if(!result/*yet*/)
105: result=GetLastError(); // get it
1.51 paf 106:
107: CloseHandle(*phInWrite);
108: CloseHandle(*phOutRead);
109: CloseHandle(*phErrRead);
110:
111: return result;
112: }
1.36 paf 113:
1.51 paf 114: static void read_pipe(String& result, HANDLE hOutRead, String::Language lang){
115: while(true) {
116: char *buf=new(PointerFreeGC) char[MAX_STRING];
117: unsigned long size;
118: if(!ReadFile(hOutRead, buf, MAX_STRING-1, &size, NULL) || !size)
119: break;
120: buf[size]=0;
121: result.append_know_length(buf, size, lang);
122: }
1.1 paf 123: }
124:
1.70 ! misha 125: static void read_pipe(File_read_result& result, HANDLE hOutRead){
! 126: char *buf=new(PointerFreeGC) char[MAX_STRING];
! 127:
! 128: unsigned long size = 0;
! 129: unsigned long bufsize = 0;
! 130: unsigned long newsize = 0;
! 131:
! 132: result.headers = 0;
! 133: result.length = 0;
! 134: result.str = 0;
! 135: result.success = false;
! 136:
! 137: while(true) {
! 138: if(!ReadFile(hOutRead, buf, MAX_STRING, &size, NULL) || !size)
! 139: break;
! 140: newsize = (size + result.length);
! 141: if(newsize > bufsize){
! 142: bufsize = (size==MAX_STRING)?newsize*2:newsize;
! 143: char *tmp = new(PointerFreeGC) char[bufsize];
! 144: if(result.str)
! 145: memcpy(tmp, result.str, result.length);
! 146: memcpy(tmp+result.length, buf, size);
! 147: result.str = tmp;
! 148: }else{
! 149: memcpy(result.str+result.length, buf, size);
! 150: }
! 151: result.length = newsize;
! 152: }
! 153: }
! 154:
1.51 paf 155: static const char* buildCommand(const char* file_spec_cstr, const ArrayString& argv) {
156: const char* result=file_spec_cstr;
1.4 paf 157: if(FILE *f=fopen(file_spec_cstr, "r")) {
1.51 paf 158: try {
1.1 paf 159: char buf[MAX_STRING];
160: size_t size=fread(buf, 1, MAX_STRING-1, f);
161: if(size>2) {
162: buf[size]=0;
163: if(strncmp(buf, "#!", 2)==0) {
1.51 paf 164: const char* begin=buf+2;
1.65 paf 165: while(*begin==' ') // alx: were an old magic for some linux-es
1.4 paf 166: begin++;
1.68 paf 167: if(const char *end=strchr(begin, '\n')) {
1.51 paf 168: String string(pa_strdup(begin, end-begin));
1.1 paf 169: string << " " << file_spec_cstr;
1.8 parser 170: result=string.cstr();
1.1 paf 171: }
172: }
173: }
1.51 paf 174: } catch(...) {
175: fclose(f);
176: rethrow;
177: }
1.1 paf 178: fclose(f);
179: }
1.51 paf 180: { // appending argv
181: String string(result);
182: for(size_t i=0; i<argv.count(); i++) {
183: string << " ";
184: string << *argv[i];
1.8 parser 185: }
186:
1.51 paf 187: result=string.cstr();
1.8 parser 188: }
189:
190: return result;
1.1 paf 191: }
192:
1.51 paf 193: #else
1.1 paf 194:
1.51 paf 195: static pid_t execve_piped(const char* file_spec_cstr,
1.27 paf 196: char * const argv[], char * const env[],
197: int *pipe_in, int *pipe_out, int *pipe_err) {
1.46 paf 198: pid_t pid;
1.1 paf 199: int in_fds[2];
200: int out_fds[2];
201: int err_fds[2];
202: int save_errno;
203:
204: if(pipe_in && pipe(in_fds)<0) {
205: save_errno=errno;
206: errno=save_errno;
207: return 0;
208: }
209:
210: if(pipe_out && pipe(out_fds)<0) {
211: save_errno=errno;
212: if(pipe_in) {
213: close(in_fds[0]); close(in_fds[1]);
214: }
215: errno=save_errno;
216: return 0;
217: }
218:
219: if(pipe_err && pipe(err_fds)<0) {
220: save_errno=errno;
221: if(pipe_in) {
222: close(in_fds[0]); close(in_fds[1]);
223: }
224: if(pipe_out) {
225: close(out_fds[0]); close(out_fds[1]);
226: }
227: errno=save_errno;
228: return 0;
229: }
230:
231: if((pid=fork())<0) {
232: save_errno=errno;
233: if(pipe_in) {
234: close(in_fds[0]); close(in_fds[1]);
235: }
236: if(pipe_out) {
237: close(out_fds[0]); close(out_fds[1]);
238: }
239: if(pipe_err) {
240: close(err_fds[0]); close(err_fds[1]);
241: }
242: errno=save_errno;
1.46 paf 243: return -1;
1.1 paf 244: }
245:
246: if(!pid) {
247: /* Child process */
248:
249: if(pipe_out) {
250: close(out_fds[0]);
251: dup2(out_fds[1], STDOUT_FILENO);
252: close(out_fds[1]);
253: }
254:
255: if(pipe_in) {
256: close(in_fds[1]);
257: dup2(in_fds[0], STDIN_FILENO);
258: close(in_fds[0]);
259: }
260:
261: if(pipe_err) {
262: close(err_fds[0]);
263: dup2(err_fds[1], STDERR_FILENO);
264: close(err_fds[1]);
265: }
266:
1.42 paf 267: /* grabbed this from Apache source: */
268: /* HP-UX SIGCHLD fix goes here, if someone will remind me what it is... */
1.1 paf 269: signal(SIGCHLD, SIG_DFL); /* Was that it? */
270:
1.31 paf 271: // chdir to script's directory
272: char dir[MAX_STRING];
273: strncpy(dir, file_spec_cstr, MAX_STRING-1); dir[MAX_STRING-1]=0;
274: rsplit(dir,'/'); // trim filename
275: chdir(dir);
276:
277: // execute
278: execve(file_spec_cstr, argv, env);
1.1 paf 279: exit(-errno);
280: }
281:
282: /* Parent process */
283:
284: if(pipe_out) {
285: close(out_fds[1]);
286: *pipe_out=out_fds[0];
287: }
288:
289: if(pipe_in) {
290: close(in_fds[0]);
291: *pipe_in=in_fds[1];
292: }
293:
294: if(pipe_err) {
295: close(err_fds[1]);
296: *pipe_err=err_fds[0];
297: }
298:
299: return pid;
300: }
301:
302: static int get_exit_status(int pid) {
303: int status;
1.58 paf 304: pid_t cid;
305: while ((cid=waitpid(pid, &status, WUNTRACED)) == -1 && errno == EINTR);
306: if(!cid)
1.1 paf 307: return -1;
308: return WIFEXITED(status) ?
309: WEXITSTATUS(status) : -2;
310: }
311:
1.51 paf 312: static void read_pipe(String& result, int file, String::Language lang){
1.1 paf 313: while(true) {
1.51 paf 314: char *buf=new(PointerFreeGC) char[MAX_STRING];
315: ssize_t length=read(file, buf, MAX_STRING-1);
316: if(length<=0)
1.49 paf 317: break;
1.51 paf 318: buf[length]=0;
319: result.append_know_length(buf, length, lang);
320: }
1.1 paf 321: }
322:
1.70 ! misha 323: static void read_pipe(File_read_result& result, int file){
! 324: char *buf=new(PointerFreeGC) char[MAX_STRING];
! 325:
! 326: ssize_t size = 0;
! 327: unsigned long bufsize = 0;
! 328: unsigned long newsize = 0;
! 329:
! 330: result.headers = 0;
! 331: result.length = 0;
! 332: result.str = 0;
! 333: result.success = false;
! 334:
! 335: while(true) {
! 336: read(file, buf, MAX_STRING);
! 337: if(size <= 0)
! 338: break;
! 339: newsize = (size + result.length);
! 340: if(newsize > bufsize){
! 341: bufsize = (size==MAX_STRING)?newsize*2:newsize;
! 342: char *tmp = new(PointerFreeGC) char[bufsize];
! 343: if(result.str)
! 344: memcpy(tmp, result.str, result.length);
! 345: memcpy(tmp+result.length, buf, size);
! 346: result.str = tmp;
! 347: }else{
! 348: memcpy(result.str+result.length, buf, size);
! 349: }
! 350: result.length = newsize;
! 351: }
! 352: }
! 353:
1.51 paf 354: #endif
1.1 paf 355:
1.51 paf 356: #ifndef DOXYGEN
357: struct Append_env_pair_info {
1.1 paf 358: #ifdef WIN32
1.62 paf 359: String::Body& body;
360: Append_env_pair_info(String::Body& abody): body(abody) {}
1.51 paf 361: #else
362: char **env_ref;
363: #endif
364: };
365: #endif
366: ///@test maybe here and at argv construction --- cstr(String::L_UNSPECIFIED
367: static void append_env_pair(HashStringString::key_type key, HashStringString::value_type value,
368: Append_env_pair_info *info) {
369: #ifdef WIN32
1.62 paf 370: info->body << key << "=" << value;
371: info->body.append_know_length("\1", 1); // placeholder for of zero byte
1.1 paf 372: #else
1.52 paf 373: String::Body body;
1.63 paf 374: body << key << "=" << value.cstr();
1.1 paf 375:
1.61 paf 376: *(info->env_ref++)=body.cstrm();
1.1 paf 377: #endif
378: }
1.31 paf 379:
1.51 paf 380: PA_exec_result pa_exec(
1.53 paf 381: bool
1.55 paf 382: #if defined(NO_PA_EXEC) || defined(PA_SAFE_MODE)
1.53 paf 383: forced_allow
384: #endif
385: ,
1.32 paf 386: const String& file_spec,
1.51 paf 387: const HashStringString* env,
388: const ArrayString& argv,
389: String& in) {
390: PA_exec_result result;
1.1 paf 391:
1.28 paf 392: #ifdef NO_PA_EXECS
1.32 paf 393: if(!forced_allow)
1.69 misha 394: throw Exception(PARSER_RUNTIME,
1.32 paf 395: &file_spec,
396: "parser execs are disabled [recompile parser without --disable-execs configure option]");
397: #endif
1.28 paf 398:
1.1 paf 399: #ifdef WIN32
400:
401: PROCESS_INFORMATION pi;
402: HANDLE hInWrite, hOutRead, hErrRead;
1.65 paf 403: const char* script_spec_cstr=file_spec.cstr(String::L_FILE_SPEC);
404: const char* cmd=buildCommand(script_spec_cstr, argv);
1.53 paf 405: char* env_cstr=0;
1.1 paf 406: if(env) {
1.62 paf 407: String::Body body;
408: Append_env_pair_info info(body);
1.68 paf 409: env->for_each<Append_env_pair_info*>(append_env_pair, &info);
1.62 paf 410: env_cstr=info.body.cstrm();
1.51 paf 411: for(char* replacer=env_cstr; *replacer; replacer++)
412: if(*replacer=='\1')
413: *replacer=0;
1.1 paf 414: }
1.65 paf 415: if(DWORD error=CreateHiddenConsoleProcess(cmd, script_spec_cstr, env_cstr, &pi, &hInWrite, &hOutRead, &hErrRead)) {
1.36 paf 416: char szErrorDesc[MAX_STRING];
1.51 paf 417: const char* param="the file you tried to run";
1.36 paf 418: size_t error_size=FormatMessage(
419: FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_ARGUMENT_ARRAY , NULL, error,
420: MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
1.51 paf 421: szErrorDesc, sizeof(szErrorDesc), (va_list *)¶m);
1.36 paf 422: if(error_size>3) // ".\r\n"
423: szErrorDesc[error_size-3]=0;
424:
425: throw Exception(0,
426: &file_spec,
1.56 paf 427: "exec failed - %s (%u). Consider adding shbang line (#!x:\\interpreter\\command line)",
428: error_size?szErrorDesc:"<unknown>", error);
1.36 paf 429: } else {
1.51 paf 430: const char* in_cstr=in.cstr();
1.5 paf 431: DWORD written_size;
1.51 paf 432: WriteFile(hInWrite, in_cstr, in.length(), &written_size, NULL);
1.5 paf 433: // EOF for stupid text reads
434: // normally they should read CONTENT_LENGTH bytes,
435: // without this char
436: WriteFile(hInWrite, "\x1A", 1, &written_size, NULL);
1.9 parser 437: CloseHandle(hInWrite);
1.70 ! misha 438: read_pipe(result.out, hOutRead);
1.9 parser 439: CloseHandle(hOutRead);
1.51 paf 440: read_pipe(result.err, hErrRead, String::L_TAINTED);
1.9 parser 441: CloseHandle(hErrRead);
1.1 paf 442: /*
443: 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
444:
445: * We must close the handles to the new process and its main thread
446: * to prevent handle and memory leaks.
447: */
448: CloseHandle(pi.hProcess);
449: CloseHandle(pi.hThread);
450: }
451:
452: #else
1.53 paf 453:
454: // execve needs non const
455: char* file_spec_cstr=file_spec.cstrm(String::L_FILE_SPEC);
1.1 paf 456:
457: int pipe_write, pipe_read, pipe_err;
1.31 paf 458:
1.47 paf 459: #ifdef PA_SAFE_MODE
1.32 paf 460: if(!forced_allow) {
461: struct stat finfo;
462: if(stat(file_spec_cstr, &finfo)!=0)
1.34 paf 463: throw Exception("file.missing",
1.51 paf 464: &file_spec,
465: "stat failed: %s (%d), actual filename '%s'",
466: strerror(errno), errno, file_spec_cstr);
1.31 paf 467:
1.50 paf 468: check_safe_mode(finfo, file_spec, file_spec_cstr);
1.32 paf 469: }
1.31 paf 470: #endif
471:
1.66 paf 472: char* argv_cstrs[1+50+1]={file_spec_cstr, 0};
1.51 paf 473: const int argv_size=argv.count();
474: const int argv_max=sizeof(argv_cstrs)/sizeof(argv_cstrs[0])-1-1;
475: if(argv_size>argv_max)
1.69 misha 476: throw Exception(PARSER_RUNTIME,
1.51 paf 477: &file_spec,
478: "too many arguments (%d > max %d)", argv_size, argv_max);
479: for(int i=0; i<argv_size; i++)
480: argv_cstrs[1+i]=argv[i]->cstrm();
481: argv_cstrs[1+argv_size]=0;
482:
483: char **env_cstrs;
1.1 paf 484: if(env) {
1.51 paf 485: env_cstrs=new(PointerFreeGC) char *[env->count()+1/*0*/];
486: Append_env_pair_info info={env_cstrs};
487: env->for_each(append_env_pair, &info);
488: *info.env_ref=0;
489: } else
490: env_cstrs=0;
1.31 paf 491:
1.46 paf 492: pid_t pid=execve_piped(
1.1 paf 493: file_spec_cstr,
1.27 paf 494: argv_cstrs, env_cstrs,
1.22 paf 495: &pipe_write, &pipe_read, &pipe_err);
1.46 paf 496: if(pid>0) {
1.21 paf 497: // in child
1.51 paf 498: if(in.length()) {// there is some in data
499: const char* in_cstr=in.cstr();
500: write(pipe_write, in_cstr, in.length());
501: }
1.1 paf 502: close(pipe_write);
1.70 ! misha 503: read_pipe(result.out, pipe_read);
1.5 paf 504: close(pipe_read);
1.51 paf 505: read_pipe(result.err, pipe_err, String::L_TAINTED);
1.1 paf 506: close(pipe_err);
507:
1.51 paf 508: result.status=get_exit_status(pid); // negative may mean "-errno[execl()]"
1.59 paf 509: } else {
510: const char* str=strerror(errno);
1.34 paf 511: throw Exception(0,
1.2 paf 512: &file_spec,
1.59 paf 513: "%s error: %s (%d)", pid<0?"fork":"pipe", str?str:"<unknown>", errno);
514: }
1.1 paf 515: #endif
516:
1.51 paf 517: return result;
1.1 paf 518: }
E-mail: