14 static void my_close(int fd);
15 static void my_dup2(int old_fd, int new_fd) ;
21 void pipex_open(pipex_t *pipex,
23 const char *working_dir,
25 char string[StringSize];
29 int from_child[2], to_child[2];
35 pipex->quit_pending=FALSE;
36 pipex->command=command;
39 pipex->io->in_fd = STDIN_FILENO;
40 pipex->io->out_fd = STDOUT_FILENO;
42 // attach standard error to standard output
44 my_dup2(STDOUT_FILENO,STDERR_FILENO);
48 // parse the command line and create the argument list
50 if (strlen(command) >= StringSize) my_fatal("pipex_open(): buffer overflow\n");
51 strcpy(string,command);
54 for (ptr = strtok(string," "); ptr != NULL; ptr = strtok(NULL," ")) {
60 //printf("command=[%s]\n",command);
61 //Buffer overflow alert
62 ret=wordexp(command, &p, 0);
64 my_fatal("pipex_open(): %s: Unable to parse command.\n",command);
68 my_fatal("pipex_open(): %s: Too many arguments.\n",command);
71 argv[i] = p.we_wordv[i];
74 //for(i=0;i<argc;i++){
75 // printf("[%s]",argv[i]);
82 if (pipe(from_child) == -1) {
83 my_fatal("pipex_open(): pipe(): %s\n",strerror(errno));
86 if (pipe(to_child) == -1) {
87 my_fatal("pipex_open(): pipe(): %s\n",strerror(errno));
90 // create the child process
94 if (pipex->pid == -1) {
96 my_fatal("pipex_open(): fork(): %s\n",strerror(errno));
98 } else if (pipex->pid == 0) {
102 // close unused pipe descriptors to avoid deadlocks
104 my_close(from_child[0]);
105 my_close(to_child[1]);
107 // attach the pipe to standard input
109 my_dup2(to_child[0],STDIN_FILENO);
110 my_close(to_child[0]);
112 // attach the pipe to standard output
114 my_dup2(from_child[1],STDOUT_FILENO);
115 my_close(from_child[1]);
117 // attach standard error to standard output
118 // commenting this out gives error messages on the console
120 my_dup2(STDOUT_FILENO,STDERR_FILENO);
122 if(chdir(working_dir)){
123 printf("%s pipex_open(): %s: %s\n",
130 // launch the new executable file
132 execvp(argv[0],&argv[0]);
134 // execvp() only returns when an error has occured
136 printf("%s pipex_open(): execvp(): %s: %s\n",
141 while(fgets(string,StringSize,stdin));
146 ASSERT(pipex->pid>0);
150 // close unused pipe descriptors to avoid deadlocks
152 my_close(from_child[1]);
153 my_close(to_child[0]);
155 // fill in the pipex struct
157 pipex->io->in_fd = from_child[0];
158 pipex->io->out_fd = to_child[1];
159 pipex->state|=PIPEX_ACTIVE; // can we test if this really TRUE?
166 void pipex_wait_event(pipex_t *pipex[]){
180 while((p=*(q++))!=NULL){
181 ASSERT(p->io->in_fd>=0);
182 FD_SET(p->io->in_fd,set);
183 if (p->io->in_fd > fd_max){
184 fd_max = p->io->in_fd;
188 // wait for something to read (no timeout)
191 val = select(fd_max+1,set,NULL,NULL,NULL);
192 if (val == -1 && errno != EINTR) my_fatal("pipex_wait_event(): select(): %s\n",strerror(errno));
196 while((p=*(q++))!=NULL){
197 if (FD_ISSET(p->io->in_fd,set) /*&& !io_line_ready(p->io)*/){
198 io_get_update(p->io);
206 bool pipex_active(pipex_t *pipex){
207 return (pipex->state&PIPEX_ACTIVE)!=0;
212 bool pipex_eof(pipex_t *pipex){
213 return (pipex->state&PIPEX_EOF)!=0;
217 // pipex_set_priority()
219 void pipex_set_priority(pipex_t *pipex, int value){
221 setpriority(PRIO_PROCESS,pipex->pid,value);
225 // pipex_set_affinity()
227 void pipex_set_affinity(pipex_t *pipex, int value){
228 my_log("POLYGLOT Setting affinity is not yet implemented on posix\n");
233 void pipex_send_eof(pipex_t *pipex){
239 /* This routine waits for kill_timeout milliseconds for
240 * the process to exit by itself. If that doesn't
241 * happen it will kill the process.
246 void pipex_exit(pipex_t *pipex, int kill_timeout){
252 my_log("POLYGLOT Waiting for child process to exit.\n");
257 while(elapsed_time<kill_timeout){
258 ret=waitpid(pipex->pid,&status,WNOHANG);
260 my_log("POLYGLOT Child has not exited yet. Sleeping %dms.\n", WAIT_GRANULARITY);
261 my_sleep(WAIT_GRANULARITY);
262 elapsed_time+=WAIT_GRANULARITY;
269 my_log("POLYGLOT Child wouldn't exit by itself. Terminating it.\n");
270 kill(pipex->pid,SIGKILL);
271 waitpid(pipex->pid,&status,0);
273 if(WIFEXITED(status)){
274 if(pipex->quit_pending){
275 my_log("POLYGLOT Child exited with status %d.\n",WEXITSTATUS(status));
277 // Suppress further messages.
278 pipex->quit_pending=TRUE;
279 my_fatal("pipex_exit(): %s: child exited with status %d.\n",pipex->command,WEXITSTATUS(status));
281 }else if(WIFSIGNALED(status)){
282 if(pipex->quit_pending){
283 my_log("POLYGLOT pipex_exit(): %s: child terminated with signal %d.\n",pipex->command,WTERMSIG(status));
285 // Suppress further messages.
286 pipex->quit_pending=TRUE;
287 my_fatal("pipex_exit(): %s: child terminated with signal %d.\n",pipex->command,WTERMSIG(status));
293 // pipex_get_buffer()
295 char * pipex_get_buffer(pipex_t *pipex){
296 return pipex->io->in_buffer;
301 bool pipex_readln(pipex_t *pipex, char *string){
302 while (!io_line_ready(pipex->io)) {
303 io_get_update(pipex->io);
305 if (!io_get_line(pipex->io,string,StringSize)) { // EOF
307 pipex->state|=PIPEX_EOF;
310 if(strncmp(PIPEX_MAGIC,string,strlen(PIPEX_MAGIC))==0){
311 my_fatal("%s\n",string+strlen(PIPEX_MAGIC)+1);
319 bool pipex_readln_nb(pipex_t *pipex, char *string){
321 while(!pipex->io->in_eof && !io_line_ready(pipex->io) && io_peek(pipex->io)){
322 io_get_update(pipex->io);
325 if(io_line_ready(pipex->io)){
326 return pipex_readln(pipex,string);
327 }else if(pipex->io->in_eof){
329 pipex->state|=PIPEX_EOF;
339 void pipex_write(pipex_t *pipex, const char *string){
340 io_send_queue(pipex->io,"%s",string);
346 void pipex_writeln(pipex_t *pipex, const char *string){
347 io_send(pipex->io,"%s",string);
352 static void my_close(int fd) {
356 if (close(fd) == -1) my_fatal("my_close(): close(): %s\n",strerror(errno));
361 static void my_dup2(int old_fd, int new_fd) {
366 if (dup2(old_fd,new_fd) == -1) my_fatal("my_dup2(): dup2(): %s\n",strerror(errno));