Annotation of parser3/src/main/pa_exec.C, revision 1.73
1.1 paf 1: /** @file
2: Parser: program executing for different OS-es.
3:
1.73 ! misha 4: Copyright(c) 2000-2009 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.73 ! misha 10: static const char * const IDENT_EXEC_C="$Date: 2008-09-04 09:37:48 $";
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.73 ! misha 32: PROCESS_INFORMATION* ppi,
! 33: LPHANDLE phInWrite,
! 34: LPHANDLE phOutRead,
! 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){
1.71 misha 126:
1.70 misha 127: char *buf=new(PointerFreeGC) char[MAX_STRING];
128:
129: unsigned long size = 0;
130: unsigned long bufsize = 0;
131: unsigned long newsize = 0;
132:
133: result.headers = 0;
134: result.length = 0;
135: result.str = 0;
136: result.success = false;
137:
138: while(true) {
1.71 misha 139: if(!ReadFile(hOutRead, buf, MAX_STRING-1, &size, NULL) || !size)
1.70 misha 140: break;
141: newsize = (size + result.length);
142: if(newsize > bufsize){
1.71 misha 143: bufsize = (size==MAX_STRING-1)?newsize*2:newsize;
1.70 misha 144: char *tmp = new(PointerFreeGC) char[bufsize];
145: if(result.str)
146: memcpy(tmp, result.str, result.length);
147: memcpy(tmp+result.length, buf, size);
148: result.str = tmp;
149: }else{
150: memcpy(result.str+result.length, buf, size);
151: }
152: result.length = newsize;
153: }
154: }
155:
1.51 paf 156: static const char* buildCommand(const char* file_spec_cstr, const ArrayString& argv) {
157: const char* result=file_spec_cstr;
1.4 paf 158: if(FILE *f=fopen(file_spec_cstr, "r")) {
1.51 paf 159: try {
1.1 paf 160: char buf[MAX_STRING];
161: size_t size=fread(buf, 1, MAX_STRING-1, f);
162: if(size>2) {
163: buf[size]=0;
164: if(strncmp(buf, "#!", 2)==0) {
1.51 paf 165: const char* begin=buf+2;
1.65 paf 166: while(*begin==' ') // alx: were an old magic for some linux-es
1.4 paf 167: begin++;
1.68 paf 168: if(const char *end=strchr(begin, '\n')) {
1.51 paf 169: String string(pa_strdup(begin, end-begin));
1.1 paf 170: string << " " << file_spec_cstr;
1.8 parser 171: result=string.cstr();
1.1 paf 172: }
173: }
174: }
1.51 paf 175: } catch(...) {
176: fclose(f);
177: rethrow;
178: }
1.1 paf 179: fclose(f);
180: }
1.51 paf 181: { // appending argv
182: String string(result);
183: for(size_t i=0; i<argv.count(); i++) {
184: string << " ";
185: string << *argv[i];
1.8 parser 186: }
187:
1.51 paf 188: result=string.cstr();
1.8 parser 189: }
190:
191: return result;
1.1 paf 192: }
193:
1.51 paf 194: #else
1.1 paf 195:
1.51 paf 196: static pid_t execve_piped(const char* file_spec_cstr,
1.27 paf 197: char * const argv[], char * const env[],
198: int *pipe_in, int *pipe_out, int *pipe_err) {
1.46 paf 199: pid_t pid;
1.1 paf 200: int in_fds[2];
201: int out_fds[2];
202: int err_fds[2];
203: int save_errno;
204:
205: if(pipe_in && pipe(in_fds)<0) {
206: save_errno=errno;
207: errno=save_errno;
208: return 0;
209: }
210:
211: if(pipe_out && pipe(out_fds)<0) {
212: save_errno=errno;
213: if(pipe_in) {
214: close(in_fds[0]); close(in_fds[1]);
215: }
216: errno=save_errno;
217: return 0;
218: }
219:
220: if(pipe_err && pipe(err_fds)<0) {
221: save_errno=errno;
222: if(pipe_in) {
223: close(in_fds[0]); close(in_fds[1]);
224: }
225: if(pipe_out) {
226: close(out_fds[0]); close(out_fds[1]);
227: }
228: errno=save_errno;
229: return 0;
230: }
231:
232: if((pid=fork())<0) {
233: save_errno=errno;
234: if(pipe_in) {
235: close(in_fds[0]); close(in_fds[1]);
236: }
237: if(pipe_out) {
238: close(out_fds[0]); close(out_fds[1]);
239: }
240: if(pipe_err) {
241: close(err_fds[0]); close(err_fds[1]);
242: }
243: errno=save_errno;
1.46 paf 244: return -1;
1.1 paf 245: }
246:
247: if(!pid) {
248: /* Child process */
249:
250: if(pipe_out) {
251: close(out_fds[0]);
252: dup2(out_fds[1], STDOUT_FILENO);
253: close(out_fds[1]);
254: }
255:
256: if(pipe_in) {
257: close(in_fds[1]);
258: dup2(in_fds[0], STDIN_FILENO);
259: close(in_fds[0]);
260: }
261:
262: if(pipe_err) {
263: close(err_fds[0]);
264: dup2(err_fds[1], STDERR_FILENO);
265: close(err_fds[1]);
266: }
267:
1.42 paf 268: /* grabbed this from Apache source: */
269: /* HP-UX SIGCHLD fix goes here, if someone will remind me what it is... */
1.1 paf 270: signal(SIGCHLD, SIG_DFL); /* Was that it? */
271:
1.31 paf 272: // chdir to script's directory
273: char dir[MAX_STRING];
274: strncpy(dir, file_spec_cstr, MAX_STRING-1); dir[MAX_STRING-1]=0;
275: rsplit(dir,'/'); // trim filename
276: chdir(dir);
277:
278: // execute
279: execve(file_spec_cstr, argv, env);
1.1 paf 280: exit(-errno);
281: }
282:
283: /* Parent process */
284:
285: if(pipe_out) {
286: close(out_fds[1]);
287: *pipe_out=out_fds[0];
288: }
289:
290: if(pipe_in) {
291: close(in_fds[0]);
292: *pipe_in=in_fds[1];
293: }
294:
295: if(pipe_err) {
296: close(err_fds[1]);
297: *pipe_err=err_fds[0];
298: }
299:
300: return pid;
301: }
302:
303: static int get_exit_status(int pid) {
304: int status;
1.58 paf 305: pid_t cid;
306: while ((cid=waitpid(pid, &status, WUNTRACED)) == -1 && errno == EINTR);
307: if(!cid)
1.1 paf 308: return -1;
309: return WIFEXITED(status) ?
310: WEXITSTATUS(status) : -2;
311: }
312:
1.51 paf 313: static void read_pipe(String& result, int file, String::Language lang){
1.1 paf 314: while(true) {
1.51 paf 315: char *buf=new(PointerFreeGC) char[MAX_STRING];
316: ssize_t length=read(file, buf, MAX_STRING-1);
317: if(length<=0)
1.49 paf 318: break;
1.51 paf 319: buf[length]=0;
320: result.append_know_length(buf, length, lang);
321: }
1.1 paf 322: }
323:
1.70 misha 324: static void read_pipe(File_read_result& result, int file){
325: char *buf=new(PointerFreeGC) char[MAX_STRING];
326:
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) {
1.71 misha 336: ssize_t size=read(file, buf, MAX_STRING-1);
1.70 misha 337: if(size <= 0)
338: break;
339: newsize = (size + result.length);
340: if(newsize > bufsize){
1.71 misha 341: bufsize = (size==MAX_STRING-1)?newsize*2:newsize;
1.70 misha 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;
1.73 ! misha 424:
1.72 misha 425: throw Exception("file.execute",
1.36 paf 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
1.73 ! misha 446: * to prevent handle and memory leaks.
1.1 paf 447: */
448: CloseHandle(pi.hProcess);
1.73 ! misha 449: CloseHandle(pi.hThread);
1.1 paf 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.73 ! misha 498: if(!in.is_empty()) {// there is some in data
1.51 paf 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.72 misha 511: throw Exception("file.execute",
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: