Annotation of parser3/src/main/pa_exec.C, revision 1.81
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.81 ! misha 10: static const char * const IDENT_EXEC_C="$Date: 2010-08-25 02:13:06 $";
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) {
1.77 misha 116: char *buf=new(PointerFreeGC) char[MAX_STRING+1];
1.78 misha 117: DWORD size=0;
1.77 misha 118: if(!ReadFile(hOutRead, buf, MAX_STRING, &size, NULL) || !size)
1.51 paf 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.77 misha 127: char *buf=(char*)pa_malloc(MAX_STRING+1);
128: DWORD bufsize = MAX_STRING;
1.70 misha 129:
130: result.headers = 0;
131: result.length = 0;
132: result.str = 0;
133: result.success = false;
134:
135: while(true) {
1.77 misha 136: DWORD size=0;
137: if(!ReadFile(hOutRead, buf + result.length, bufsize - result.length, &size, NULL) || !size)
1.70 misha 138: break;
1.77 misha 139: result.length += size;
140: if(result.length >= bufsize){
141: bufsize *= 2;
142: buf=(char*)pa_realloc(buf, bufsize+1);
1.70 misha 143: }
1.77 misha 144: result.str=buf;
1.70 misha 145: }
146: }
147:
1.51 paf 148: static const char* buildCommand(const char* file_spec_cstr, const ArrayString& argv) {
149: const char* result=file_spec_cstr;
1.4 paf 150: if(FILE *f=fopen(file_spec_cstr, "r")) {
1.51 paf 151: try {
1.1 paf 152: char buf[MAX_STRING];
153: size_t size=fread(buf, 1, MAX_STRING-1, f);
154: if(size>2) {
155: buf[size]=0;
156: if(strncmp(buf, "#!", 2)==0) {
1.51 paf 157: const char* begin=buf+2;
1.65 paf 158: while(*begin==' ') // alx: were an old magic for some linux-es
1.4 paf 159: begin++;
1.68 paf 160: if(const char *end=strchr(begin, '\n')) {
1.51 paf 161: String string(pa_strdup(begin, end-begin));
1.1 paf 162: string << " " << file_spec_cstr;
1.8 parser 163: result=string.cstr();
1.1 paf 164: }
165: }
166: }
1.51 paf 167: } catch(...) {
168: fclose(f);
169: rethrow;
170: }
1.1 paf 171: fclose(f);
172: }
1.51 paf 173: { // appending argv
174: String string(result);
175: for(size_t i=0; i<argv.count(); i++) {
176: string << " ";
177: string << *argv[i];
1.8 parser 178: }
179:
1.51 paf 180: result=string.cstr();
1.8 parser 181: }
182:
183: return result;
1.1 paf 184: }
185:
1.51 paf 186: #else
1.1 paf 187:
1.51 paf 188: static pid_t execve_piped(const char* file_spec_cstr,
1.27 paf 189: char * const argv[], char * const env[],
190: int *pipe_in, int *pipe_out, int *pipe_err) {
1.46 paf 191: pid_t pid;
1.1 paf 192: int in_fds[2];
193: int out_fds[2];
194: int err_fds[2];
195: int save_errno;
196:
197: if(pipe_in && pipe(in_fds)<0) {
198: save_errno=errno;
199: errno=save_errno;
200: return 0;
201: }
202:
203: if(pipe_out && pipe(out_fds)<0) {
204: save_errno=errno;
205: if(pipe_in) {
206: close(in_fds[0]); close(in_fds[1]);
207: }
208: errno=save_errno;
209: return 0;
210: }
211:
212: if(pipe_err && pipe(err_fds)<0) {
213: save_errno=errno;
214: if(pipe_in) {
215: close(in_fds[0]); close(in_fds[1]);
216: }
217: if(pipe_out) {
218: close(out_fds[0]); close(out_fds[1]);
219: }
220: errno=save_errno;
221: return 0;
222: }
223:
224: if((pid=fork())<0) {
225: save_errno=errno;
226: if(pipe_in) {
227: close(in_fds[0]); close(in_fds[1]);
228: }
229: if(pipe_out) {
230: close(out_fds[0]); close(out_fds[1]);
231: }
232: if(pipe_err) {
233: close(err_fds[0]); close(err_fds[1]);
234: }
235: errno=save_errno;
1.46 paf 236: return -1;
1.1 paf 237: }
238:
239: if(!pid) {
240: /* Child process */
241:
242: if(pipe_out) {
243: close(out_fds[0]);
244: dup2(out_fds[1], STDOUT_FILENO);
245: close(out_fds[1]);
246: }
247:
248: if(pipe_in) {
249: close(in_fds[1]);
250: dup2(in_fds[0], STDIN_FILENO);
251: close(in_fds[0]);
252: }
253:
254: if(pipe_err) {
255: close(err_fds[0]);
256: dup2(err_fds[1], STDERR_FILENO);
257: close(err_fds[1]);
258: }
259:
1.42 paf 260: /* grabbed this from Apache source: */
261: /* HP-UX SIGCHLD fix goes here, if someone will remind me what it is... */
1.1 paf 262: signal(SIGCHLD, SIG_DFL); /* Was that it? */
263:
1.31 paf 264: // chdir to script's directory
265: char dir[MAX_STRING];
266: strncpy(dir, file_spec_cstr, MAX_STRING-1); dir[MAX_STRING-1]=0;
267: rsplit(dir,'/'); // trim filename
268: chdir(dir);
269:
270: // execute
271: execve(file_spec_cstr, argv, env);
1.1 paf 272: exit(-errno);
273: }
274:
275: /* Parent process */
276:
277: if(pipe_out) {
278: close(out_fds[1]);
279: *pipe_out=out_fds[0];
280: }
281:
282: if(pipe_in) {
283: close(in_fds[0]);
284: *pipe_in=in_fds[1];
285: }
286:
287: if(pipe_err) {
288: close(err_fds[1]);
289: *pipe_err=err_fds[0];
290: }
291:
292: return pid;
293: }
294:
295: static int get_exit_status(int pid) {
296: int status;
1.58 paf 297: pid_t cid;
298: while ((cid=waitpid(pid, &status, WUNTRACED)) == -1 && errno == EINTR);
299: if(!cid)
1.1 paf 300: return -1;
301: return WIFEXITED(status) ?
302: WEXITSTATUS(status) : -2;
303: }
304:
1.51 paf 305: static void read_pipe(String& result, int file, String::Language lang){
1.1 paf 306: while(true) {
1.77 misha 307: char *buf=new(PointerFreeGC) char[MAX_STRING+1];
308: ssize_t length=read(file, buf, MAX_STRING);
1.51 paf 309: if(length<=0)
1.49 paf 310: break;
1.51 paf 311: buf[length]=0;
312: result.append_know_length(buf, length, lang);
313: }
1.1 paf 314: }
315:
1.70 misha 316: static void read_pipe(File_read_result& result, int file){
1.77 misha 317: char *buf=(char*)pa_malloc(MAX_STRING+1);
318: ssize_t bufsize = MAX_STRING;
1.70 misha 319:
320: result.headers = 0;
321: result.length = 0;
322: result.str = 0;
323: result.success = false;
324:
325: while(true) {
1.77 misha 326: ssize_t size=read(file, buf + result.length, bufsize - result.length);
1.70 misha 327: if(size <= 0)
328: break;
1.77 misha 329: result.length += size;
330: if(result.length >= bufsize){
331: bufsize *= 2;
332: buf=(char*)pa_realloc(buf, bufsize+1);
1.70 misha 333: }
1.77 misha 334: result.str=buf;
1.70 misha 335: }
336: }
337:
1.51 paf 338: #endif
1.1 paf 339:
1.51 paf 340: #ifndef DOXYGEN
341: struct Append_env_pair_info {
1.1 paf 342: #ifdef WIN32
1.62 paf 343: String::Body& body;
344: Append_env_pair_info(String::Body& abody): body(abody) {}
1.51 paf 345: #else
346: char **env_ref;
347: #endif
348: };
349: #endif
1.75 misha 350: ///@test maybe here and at argv construction --- untaint_cstr(String::L_AS_IS
1.51 paf 351: static void append_env_pair(HashStringString::key_type key, HashStringString::value_type value,
352: Append_env_pair_info *info) {
353: #ifdef WIN32
1.62 paf 354: info->body << key << "=" << value;
355: info->body.append_know_length("\1", 1); // placeholder for of zero byte
1.1 paf 356: #else
1.52 paf 357: String::Body body;
1.63 paf 358: body << key << "=" << value.cstr();
1.1 paf 359:
1.61 paf 360: *(info->env_ref++)=body.cstrm();
1.1 paf 361: #endif
362: }
1.31 paf 363:
1.51 paf 364: PA_exec_result pa_exec(
1.81 ! misha 365: bool forced_allow,
1.32 paf 366: const String& file_spec,
1.51 paf 367: const HashStringString* env,
368: const ArrayString& argv,
369: String& in) {
370: PA_exec_result result;
1.1 paf 371:
1.28 paf 372: #ifdef NO_PA_EXECS
1.32 paf 373: if(!forced_allow)
1.69 misha 374: throw Exception(PARSER_RUNTIME,
1.32 paf 375: &file_spec,
376: "parser execs are disabled [recompile parser without --disable-execs configure option]");
377: #endif
1.28 paf 378:
1.1 paf 379: #ifdef WIN32
380:
381: PROCESS_INFORMATION pi;
382: HANDLE hInWrite, hOutRead, hErrRead;
1.75 misha 383: const char* script_spec_cstr=file_spec.taint_cstr(String::L_FILE_SPEC);
1.65 paf 384: const char* cmd=buildCommand(script_spec_cstr, argv);
1.53 paf 385: char* env_cstr=0;
1.1 paf 386: if(env) {
1.62 paf 387: String::Body body;
388: Append_env_pair_info info(body);
1.68 paf 389: env->for_each<Append_env_pair_info*>(append_env_pair, &info);
1.62 paf 390: env_cstr=info.body.cstrm();
1.51 paf 391: for(char* replacer=env_cstr; *replacer; replacer++)
392: if(*replacer=='\1')
393: *replacer=0;
1.1 paf 394: }
1.65 paf 395: if(DWORD error=CreateHiddenConsoleProcess(cmd, script_spec_cstr, env_cstr, &pi, &hInWrite, &hOutRead, &hErrRead)) {
1.36 paf 396: char szErrorDesc[MAX_STRING];
1.51 paf 397: const char* param="the file you tried to run";
1.36 paf 398: size_t error_size=FormatMessage(
399: FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_ARGUMENT_ARRAY , NULL, error,
400: MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
1.51 paf 401: szErrorDesc, sizeof(szErrorDesc), (va_list *)¶m);
1.36 paf 402: if(error_size>3) // ".\r\n"
403: szErrorDesc[error_size-3]=0;
1.73 misha 404:
1.72 misha 405: throw Exception("file.execute",
1.36 paf 406: &file_spec,
1.56 paf 407: "exec failed - %s (%u). Consider adding shbang line (#!x:\\interpreter\\command line)",
408: error_size?szErrorDesc:"<unknown>", error);
1.36 paf 409: } else {
1.51 paf 410: const char* in_cstr=in.cstr();
1.5 paf 411: DWORD written_size;
1.51 paf 412: WriteFile(hInWrite, in_cstr, in.length(), &written_size, NULL);
1.5 paf 413: // EOF for stupid text reads
414: // normally they should read CONTENT_LENGTH bytes,
415: // without this char
416: WriteFile(hInWrite, "\x1A", 1, &written_size, NULL);
1.9 parser 417: CloseHandle(hInWrite);
1.70 misha 418: read_pipe(result.out, hOutRead);
1.9 parser 419: CloseHandle(hOutRead);
1.51 paf 420: read_pipe(result.err, hErrRead, String::L_TAINTED);
1.9 parser 421: CloseHandle(hErrRead);
1.1 paf 422: /*
423: 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
424:
425: * We must close the handles to the new process and its main thread
1.73 misha 426: * to prevent handle and memory leaks.
1.1 paf 427: */
428: CloseHandle(pi.hProcess);
1.73 misha 429: CloseHandle(pi.hThread);
1.1 paf 430: }
431:
432: #else
1.53 paf 433:
434: // execve needs non const
1.75 misha 435: char* file_spec_cstr=file_spec.taint_cstrm(String::L_FILE_SPEC);
1.1 paf 436:
437: int pipe_write, pipe_read, pipe_err;
1.31 paf 438:
1.32 paf 439: if(!forced_allow) {
440: struct stat finfo;
441: if(stat(file_spec_cstr, &finfo)!=0)
1.34 paf 442: throw Exception("file.missing",
1.51 paf 443: &file_spec,
444: "stat failed: %s (%d), actual filename '%s'",
445: strerror(errno), errno, file_spec_cstr);
1.31 paf 446:
1.50 paf 447: check_safe_mode(finfo, file_spec, file_spec_cstr);
1.32 paf 448: }
1.31 paf 449:
1.76 misha 450: char* argv_cstrs[1+100+1]={file_spec_cstr, 0};
1.51 paf 451: const int argv_size=argv.count();
452: const int argv_max=sizeof(argv_cstrs)/sizeof(argv_cstrs[0])-1-1;
453: if(argv_size>argv_max)
1.69 misha 454: throw Exception(PARSER_RUNTIME,
1.51 paf 455: &file_spec,
456: "too many arguments (%d > max %d)", argv_size, argv_max);
457: for(int i=0; i<argv_size; i++)
458: argv_cstrs[1+i]=argv[i]->cstrm();
459: argv_cstrs[1+argv_size]=0;
460:
461: char **env_cstrs;
1.1 paf 462: if(env) {
1.51 paf 463: env_cstrs=new(PointerFreeGC) char *[env->count()+1/*0*/];
464: Append_env_pair_info info={env_cstrs};
465: env->for_each(append_env_pair, &info);
466: *info.env_ref=0;
467: } else
468: env_cstrs=0;
1.31 paf 469:
1.46 paf 470: pid_t pid=execve_piped(
1.1 paf 471: file_spec_cstr,
1.27 paf 472: argv_cstrs, env_cstrs,
1.22 paf 473: &pipe_write, &pipe_read, &pipe_err);
1.46 paf 474: if(pid>0) {
1.21 paf 475: // in child
1.73 misha 476: if(!in.is_empty()) {// there is some in data
1.51 paf 477: const char* in_cstr=in.cstr();
478: write(pipe_write, in_cstr, in.length());
479: }
1.1 paf 480: close(pipe_write);
1.70 misha 481: read_pipe(result.out, pipe_read);
1.5 paf 482: close(pipe_read);
1.51 paf 483: read_pipe(result.err, pipe_err, String::L_TAINTED);
1.1 paf 484: close(pipe_err);
485:
1.51 paf 486: result.status=get_exit_status(pid); // negative may mean "-errno[execl()]"
1.59 paf 487: } else {
488: const char* str=strerror(errno);
1.72 misha 489: throw Exception("file.execute",
1.2 paf 490: &file_spec,
1.59 paf 491: "%s error: %s (%d)", pid<0?"fork":"pipe", str?str:"<unknown>", errno);
492: }
1.1 paf 493: #endif
494:
1.51 paf 495: return result;
1.1 paf 496: }
E-mail: