version 1.4.56b
[polyglot.git] / pipex_posix.c
1 #ifndef _WIN32
2
3 // includes
4
5 #include <string.h>
6 #include <stdlib.h>
7 #include <errno.h>
8 #include <wordexp.h>
9 #include <sys/wait.h>
10 #include "pipex.h"
11
12 // constants
13
14 static const unsigned int StringSize = 4096;
15
16 // prototypes
17
18 static void my_close(int fd);
19 static void my_dup2(int old_fd, int new_fd) ;
20
21 // functions
22
23 // pipex_open()
24
25 void pipex_open(pipex_t *pipex,
26                 const char *name,
27                 const char *working_dir,
28                 const char *command){
29     char string[StringSize];
30     int argc;
31     char * ptr;
32     char * argv[256];
33     int from_child[2], to_child[2];
34     wordexp_t p;
35     int i,ret;
36
37     pipex->pid=-1;
38     pipex->io->name=name;
39     pipex->quit_pending=FALSE;
40     pipex->command=command;
41     
42     if(command==NULL){
43         pipex->io->in_fd = STDIN_FILENO;
44         pipex->io->out_fd = STDOUT_FILENO;
45
46             // attach standard error to standard output
47         
48         my_dup2(STDOUT_FILENO,STDERR_FILENO);
49         io_init(pipex->io);
50     }else{
51     
52         // parse the command line and create the argument list
53 #if 0    
54         if (strlen(command) >= StringSize) my_fatal("pipex_open(): buffer overflow\n");
55         strcpy(string,command);
56         argc = 0;
57         
58         for (ptr = strtok(string," "); ptr != NULL; ptr = strtok(NULL," ")) {
59             argv[argc++] = ptr;
60         }
61
62         argv[argc] = NULL;
63 #else
64         //printf("command=[%s]\n",command);
65         //Buffer overflow alert
66         ret=wordexp(command, &p, 0);
67         if(ret!=0){
68           my_fatal("pipex_open(): %s: Unable to parse command.\n",command);
69         }
70         argc = p.we_wordc;
71         if(argc>=256-2){
72           my_fatal("pipex_open(): %s: Too many arguments.\n",command);
73         }
74         for(i=0;i<argc;i++){
75           argv[i] = p.we_wordv[i];
76         }
77         //      int i;
78         //for(i=0;i<argc;i++){
79         //  printf("[%s]",argv[i]);
80         //}
81         //printf("\n");
82         argv[argc] = NULL;
83 #endif        
84       // create the pipes
85         
86         if (pipe(from_child) == -1) {
87             my_fatal("pipex_open(): pipe(): %s\n",strerror(errno));
88         }
89         
90         if (pipe(to_child) == -1) {
91             my_fatal("pipex_open(): pipe(): %s\n",strerror(errno));
92         }
93         
94             // create the child process 
95         
96         pipex->pid = fork();
97         
98         if (pipex->pid == -1) {
99             
100             my_fatal("pipex_open(): fork(): %s\n",strerror(errno));
101             
102         } else if (pipex->pid == 0) {
103             
104                 // child 
105             
106                 // close unused pipe descriptors to avoid deadlocks
107             
108             my_close(from_child[0]);
109             my_close(to_child[1]);
110             
111                 // attach the pipe to standard input
112             
113             my_dup2(to_child[0],STDIN_FILENO);
114             my_close(to_child[0]);
115             
116                 // attach the pipe to standard output
117             
118             my_dup2(from_child[1],STDOUT_FILENO);
119             my_close(from_child[1]);
120             
121                 // attach standard error to standard output
122                 // commenting this out gives error messages on the console
123             
124             my_dup2(STDOUT_FILENO,STDERR_FILENO); 
125             
126             if(chdir(working_dir)){
127                 printf("%s pipex_open(): %s: %s\n",
128                        PIPEX_MAGIC,
129                        working_dir,
130                        strerror(errno));
131                 goto wait_for_eof;
132             }
133             
134             // launch the new executable file
135
136             execvp(argv[0],&argv[0]);
137             
138                 // execvp() only returns when an error has occured
139
140             printf("%s pipex_open(): execvp(): %s: %s\n",
141                    PIPEX_MAGIC,
142                    argv[0],
143                    strerror(errno));
144         wait_for_eof:
145             while(fgets(string,StringSize,stdin));
146             exit(EXIT_SUCCESS);
147             
148         } else { // pid > 0
149             
150             ASSERT(pipex->pid>0);
151             
152                 // parent 
153             
154                 // close unused pipe descriptors to avoid deadlocks
155             
156             my_close(from_child[1]);
157             my_close(to_child[0]);
158             
159                 // fill in the pipex struct
160             
161             pipex->io->in_fd = from_child[0];
162             pipex->io->out_fd = to_child[1];
163             pipex->state|=PIPEX_ACTIVE; // can we test if this really TRUE?
164             
165             io_init(pipex->io);
166         } 
167     }
168 }
169
170 void pipex_wait_event(pipex_t *pipex[]){
171
172     fd_set set[1];
173     pipex_t *p;
174     int fd_max;
175     int val;
176     pipex_t **q;
177
178     q=pipex;
179
180         // init
181
182    FD_ZERO(set);
183    fd_max = -1; // HACK
184    while((p=*(q++))!=NULL){
185        ASSERT(p->io->in_fd>=0);
186        FD_SET(p->io->in_fd,set);
187        if (p->io->in_fd > fd_max){
188            fd_max = p->io->in_fd;
189        }
190    }
191    
192    // wait for something to read (no timeout)
193
194    ASSERT(fd_max>=0);
195    val = select(fd_max+1,set,NULL,NULL,NULL);
196    if (val == -1 && errno != EINTR) my_fatal("pipex_wait_event(): select(): %s\n",strerror(errno));
197
198    q=pipex;
199    if (val > 0) {
200        while((p=*(q++))!=NULL){
201            if (FD_ISSET(p->io->in_fd,set)) io_get_update(p->io); 
202        }
203    }    
204 }
205
206 // pipex_active()
207
208 bool pipex_active(pipex_t *pipex){
209     return (pipex->state&PIPEX_ACTIVE)!=0;
210 }
211
212 // pipex_eof()
213
214 bool pipex_eof(pipex_t *pipex){
215     return (pipex->state&PIPEX_EOF)!=0;
216 }
217
218
219 // pipex_set_priority()
220
221 void pipex_set_priority(pipex_t *pipex, int value){
222     if(pipex->pid!=-1){
223         setpriority(PRIO_PROCESS,pipex->pid,value);
224     }
225 }
226
227 // pipex_set_affinity()
228
229 void pipex_set_affinity(pipex_t *pipex, int value){
230     my_log("POLYGLOT Setting affinity is not yet implemented on posix\n");
231 }
232
233 // pipex_send_eof()
234
235 void pipex_send_eof(pipex_t *pipex){
236     io_close(pipex->io);
237 }
238
239 // pipex_exit()
240
241 void pipex_exit(pipex_t *pipex){
242     int status;
243     my_log("POLYGLOT Waiting for child process to exit.\n");
244     waitpid(pipex->pid,&status,0);
245     if(WIFEXITED(status)){
246       if(pipex->quit_pending){
247         my_log("POLYGLOT Child exited with status %d.\n",WEXITSTATUS(status));
248       }else{
249         // Suppress further messages.
250         pipex->quit_pending=TRUE;
251         my_fatal("pipex_exit(): %s: child exited with status %d.\n",pipex->command,WEXITSTATUS(status));
252       }
253     }else if(WIFSIGNALED(status)){
254       if(pipex->quit_pending){
255         my_log("POLYGLOT pipex_exit(): %s: child terminated with signal %d.\n",pipex->command,WTERMSIG(status));
256       }else{
257         // Suppress further messages.
258         pipex->quit_pending=TRUE;
259           my_fatal("pipex_exit(): %s: child terminated with signal %d.\n",pipex->command,WTERMSIG(status));
260       }
261     }
262     return;
263 }
264
265 // pipex_readln()
266
267 bool pipex_readln(pipex_t *pipex, char *string){
268     while (!io_line_ready(pipex->io)) {
269       io_get_update(pipex->io);
270    }
271    if (!io_get_line(pipex->io,string,StringSize)) { // EOF
272        string[0]='\0';
273        pipex->state|=PIPEX_EOF;
274        return FALSE;
275    }
276    if(strncmp(PIPEX_MAGIC,string,strlen(PIPEX_MAGIC))==0){
277      my_fatal("%s\n",string+strlen(PIPEX_MAGIC)+1);
278    }
279
280    return TRUE;
281 }
282
283 // pipex_readln_nb()
284
285 bool pipex_readln_nb(pipex_t *pipex, char *string){
286     if(io_line_ready(pipex->io)){
287         return pipex_readln(pipex,string);
288     }else{  
289         string[0]='\0';
290         return FALSE;
291     }
292 }
293
294 // pipex_write()
295
296 void pipex_write(pipex_t *pipex, const char *string){
297        io_send_queue(pipex->io,"%s",string);
298 }
299
300
301 // pipex_writeln()
302
303 void pipex_writeln(pipex_t *pipex, const char *string){
304        io_send(pipex->io,"%s",string);
305 }
306
307 // my_close()
308
309 static void my_close(int fd) {
310
311    ASSERT(fd>=0);
312
313    if (close(fd) == -1) my_fatal("my_close(): close(): %s\n",strerror(errno));
314 }
315
316 // my_dup2()
317
318 static void my_dup2(int old_fd, int new_fd) {
319
320    ASSERT(old_fd>=0);
321    ASSERT(new_fd>=0);
322
323    if (dup2(old_fd,new_fd) == -1) my_fatal("my_dup2(): dup2(): %s\n",strerror(errno));
324 }
325
326
327 #endif