12 |
|
|
13 |
|
#include <sys/types.h> |
14 |
|
#include <sys/wait.h> |
15 |
+ |
#include <fcntl.h> |
16 |
+ |
#include <stdlib.h> |
17 |
|
|
18 |
|
#include "rtprocess.h" |
19 |
< |
#include "vfork.h" |
19 |
> |
#include "rtio.h" |
20 |
|
|
21 |
|
|
22 |
|
int |
25 |
|
char *av[] |
26 |
|
) |
27 |
|
{ |
26 |
– |
extern char *getpath(), *getenv(); |
28 |
|
char *compath; |
29 |
|
int p0[2], p1[2]; |
30 |
|
|
31 |
< |
pd->running = 0; /* not yet */ |
32 |
< |
/* find executable */ |
33 |
< |
compath = getpath(av[0], getenv("PATH"), 1); |
34 |
< |
if (compath == 0) |
31 |
> |
pd->running = 0; /* not going yet */ |
32 |
> |
|
33 |
> |
if (av == NULL) /* cloning operation? */ |
34 |
> |
compath = NULL; |
35 |
> |
else if ((compath = getpath(av[0], getenv("PATH"), X_OK)) == NULL) |
36 |
|
return(0); |
37 |
|
if (pipe(p0) < 0 || pipe(p1) < 0) |
38 |
|
return(-1); |
39 |
< |
if ((pd->pid = vfork()) == 0) { /* if child */ |
39 |
> |
if ((pd->pid = fork()) == 0) { /* if child... */ |
40 |
|
close(p0[1]); |
41 |
|
close(p1[0]); |
42 |
|
if (p0[0] != 0) { /* connect p0 to stdin */ |
47 |
|
dup2(p1[1], 1); |
48 |
|
close(p1[1]); |
49 |
|
} |
50 |
< |
execv(compath, av); /* exec command */ |
50 |
> |
if (compath == NULL) /* just cloning? */ |
51 |
> |
return(0); |
52 |
> |
execv(compath, av); /* else exec command */ |
53 |
|
perror(compath); |
54 |
|
_exit(127); |
55 |
|
} |
59 |
|
close(p1[1]); |
60 |
|
pd->r = p1[0]; |
61 |
|
pd->w = p0[1]; |
62 |
+ |
/* |
63 |
+ |
* Close write stream on exec to avoid multiprocessing deadlock. |
64 |
+ |
* No use in read stream without it, so set flag there as well. |
65 |
+ |
* GW: This bug took me two days to figure out!! |
66 |
+ |
*/ |
67 |
+ |
fcntl(pd->r, F_SETFD, FD_CLOEXEC); |
68 |
+ |
fcntl(pd->w, F_SETFD, FD_CLOEXEC); |
69 |
|
pd->running = 1; |
70 |
|
return(PIPE_BUF); |
71 |
|
} |
77 |
|
SUBPROC *pd |
78 |
|
) |
79 |
|
{ |
80 |
< |
int pid, status; |
80 |
> |
int status; |
81 |
|
|
82 |
< |
close(pd->r); |
82 |
> |
if (!pd->running) |
83 |
> |
return(0); |
84 |
|
close(pd->w); |
85 |
+ |
close(pd->r); |
86 |
|
pd->running = 0; |
87 |
< |
while ((pid = wait(&status)) != -1) |
88 |
< |
if (pid == pd->pid) |
89 |
< |
return(status>>8 & 0xff); |
87 |
> |
if (waitpid(pd->pid, &status, 0) == pd->pid) |
88 |
> |
return(status>>8 & 0xff); |
89 |
> |
|
90 |
|
return(-1); /* ? unknown status */ |
91 |
|
} |
92 |
|
|