X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=pipex_posix.c;h=f53d74f7a8eacace0b4af8670a5074dc1088a331;hb=60900035e6d0309705f2326ee50edc52386305e9;hp=34b32cc8e39cd03e883643c55fc41d5c714aa3f8;hpb=a0f731f21d6aa26dbf7246039a1c66c2ade0533f;p=polyglot.git diff --git a/pipex_posix.c b/pipex_posix.c index 34b32cc..f53d74f 100644 --- a/pipex_posix.c +++ b/pipex_posix.c @@ -3,7 +3,10 @@ // includes #include +#include #include +#include +#include #include "pipex.h" // constants @@ -28,9 +31,13 @@ void pipex_open(pipex_t *pipex, char * ptr; char * argv[256]; int from_child[2], to_child[2]; + wordexp_t p; + int i,ret; pipex->pid=-1; pipex->io->name=name; + pipex->quit_pending=FALSE; + pipex->command=command; if(command==NULL){ pipex->io->in_fd = STDIN_FILENO; @@ -43,7 +50,7 @@ void pipex_open(pipex_t *pipex, }else{ // parse the command line and create the argument list - +#if 0 if (strlen(command) >= StringSize) my_fatal("pipex_open(): buffer overflow\n"); strcpy(string,command); argc = 0; @@ -53,7 +60,27 @@ void pipex_open(pipex_t *pipex, } argv[argc] = NULL; - +#else + //printf("command=[%s]\n",command); + //Buffer overflow alert + ret=wordexp(command, &p, 0); + if(ret!=0){ + my_fatal("pipex_open(): %s: Unable to parse command.\n",command); + } + argc = p.we_wordc; + if(argc>=256-2){ + my_fatal("pipex_open(): %s: Too many arguments.\n",command); + } + for(i=0;ipid = fork(); @@ -94,11 +121,14 @@ void pipex_open(pipex_t *pipex, // attach standard error to standard output // commenting this out gives error messages on the console - /* my_dup2(STDOUT_FILENO,STDERR_FILENO); */ + my_dup2(STDOUT_FILENO,STDERR_FILENO); if(chdir(working_dir)){ - my_fatal("pipex_open(): cannot change directory: %s\n", - strerror(errno)); + printf("%s pipex_open(): %s: %s\n", + PIPEX_MAGIC, + working_dir, + strerror(errno)); + goto wait_for_eof; } // launch the new executable file @@ -106,8 +136,14 @@ void pipex_open(pipex_t *pipex, execvp(argv[0],&argv[0]); // execvp() only returns when an error has occured - - my_fatal("engine_open(): execvp(): %s\n",strerror(errno)); + + printf("%s pipex_open(): execvp(): %s: %s\n", + PIPEX_MAGIC, + argv[0], + strerror(errno)); + wait_for_eof: + while(fgets(string,StringSize,stdin)); + exit(EXIT_SUCCESS); } else { // pid > 0 @@ -203,7 +239,26 @@ void pipex_send_eof(pipex_t *pipex){ // pipex_exit() void pipex_exit(pipex_t *pipex){ - // NOOP for now + int status; + my_log("POLYGLOT Waiting for child process to exit.\n"); + waitpid(pipex->pid,&status,0); + if(WIFEXITED(status)){ + if(pipex->quit_pending){ + my_log("POLYGLOT Child exited with status %d.\n",WEXITSTATUS(status)); + }else{ + // Suppress further messages. + pipex->quit_pending=TRUE; + my_fatal("pipex_exit(): %s: child exited with status %d.\n",pipex->command,WEXITSTATUS(status)); + } + }else if(WIFSIGNALED(status)){ + if(pipex->quit_pending){ + my_log("POLYGLOT pipex_exit(): %s: child terminated with signal %d.\n",pipex->command,WTERMSIG(status)); + }else{ + // Suppress further messages. + pipex->quit_pending=TRUE; + my_fatal("pipex_exit(): %s: child terminated with signal %d.\n",pipex->command,WTERMSIG(status)); + } + } return; } @@ -218,7 +273,9 @@ bool pipex_readln(pipex_t *pipex, char *string){ pipex->state|=PIPEX_EOF; return FALSE; } - + if(strncmp(PIPEX_MAGIC,string,strlen(PIPEX_MAGIC))==0){ + my_fatal("%s\n",string+strlen(PIPEX_MAGIC)+1); + } return TRUE; }