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