ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/unix_process.c
(Generate patch)

Comparing ray/src/common/unix_process.c (file contents):
Revision 3.5 by greg, Fri Sep 17 21:43:49 2004 UTC vs.
Revision 3.17 by greg, Mon Mar 2 19:06:48 2020 UTC

# Line 10 | Line 10 | static const char      RCSid[] = "$Id$";
10  
11   #include "copyright.h"
12  
13 #include <sys/types.h>
13   #include <sys/wait.h>
14 + #include <stdlib.h>
15  
16   #include "rtprocess.h"
17 + #include "rtio.h"
18  
19  
20 + SUBPROC         sp_inactive = SP_INACTIVE;
21 +
22 +
23   int
24   open_process(           /* open communication to separate process */
25 < SUBPROC *pd,
26 < char    *av[]
25 >        SUBPROC *pd,
26 >        char    *av[]
27   )
28   {
25        extern char     *getpath(), *getenv();
29          char    *compath;
30          int     p0[2], p1[2];
31  
32 <        pd->running = 0; /* not yet */
33 <                                        /* find executable */
34 <        compath = getpath(av[0], getenv("PATH"), 1);
35 <        if (compath == 0)
32 >        if (pd->pid > 0)
33 >                return(-1);             /* need to close, first */
34 >        
35 >        if ((pd->flags&(PF_FILT_INP|PF_FILT_OUT)) == (PF_FILT_INP|PF_FILT_OUT))
36 >                return(-1);             /* circular process not supported */
37 >
38 >        pd->flags &= ~PF_RUNNING;       /* not running for sure */
39 >        pd->pid = -1;
40 >
41 >        if (av == NULL)                 /* cloning operation? */
42 >                compath = NULL;
43 >        else if ((compath = getpath(av[0], getenv("PATH"), X_OK)) == NULL)
44                  return(0);
45 <        if (pipe(p0) < 0 || pipe(p1) < 0)
45 >
46 >        if (pd->flags & PF_FILT_INP) {  /* filterning input stream? */
47 >                if ((pd->r < 0) | (pd->r == 1))
48 >                        return(-1);
49 >                p0[0] = pd->r;
50 >                p0[1] = -1;
51 >        } else if (pipe(p0) < 0)
52                  return(-1);
53 <        if ((pd->pid = fork()) == 0) {          /* if child */
53 >
54 >        if (pd->flags & PF_FILT_OUT) {  /* filtering output stream? */
55 >                if (pd->w < 1)
56 >                        return(-1);
57 >                p1[0] = -1;
58 >                p1[1] = pd->w;
59 >        } else if (pipe(p1) < 0)
60 >                return(-1);
61 > #ifdef BSD
62 >        if (compath != NULL)
63 >                pd->pid = vfork();      /* more efficient with exec() */
64 >        else
65 > #endif
66 >        pd->pid = fork();
67 >        if (pd->pid == 0) {             /* if child... */
68                  close(p0[1]);
69                  close(p1[0]);
70                  if (p0[0] != 0) {       /* connect p0 to stdin */
71 <                        dup2(p0[0], 0);
71 >                        if (dup2(p0[0], 0) < 0)
72 >                                return(-1);
73                          close(p0[0]);
74                  }
75                  if (p1[1] != 1) {       /* connect p1 to stdout */
76 <                        dup2(p1[1], 1);
76 >                        if (dup2(p1[1], 1) < 0)
77 >                                return(-1);
78                          close(p1[1]);
79                  }
80 +                if (compath == NULL)    /* just cloning? */
81 +                        return(0);
82 +                                        /* else clear streams' FD_CLOEXEC */
83 +                if (p0[0] == 0)
84 +                        fcntl(0, F_SETFD, 0);
85 +                if (p1[1] == 1)
86 +                        fcntl(1, F_SETFD, 0);
87                  execv(compath, av);     /* exec command */
88                  perror(compath);
89                  _exit(127);
90          }
91          if (pd->pid == -1)
92                  return(-1);
93 <        close(p0[0]);
94 <        close(p1[1]);
95 <        pd->r = p1[0];
96 <        pd->w = p0[1];
97 <        pd->running = 1;
93 >                                        /* connect parent's streams */
94 >        if (!(pd->flags & PF_FILT_INP)) {
95 >                close(p0[0]);
96 >                pd->r = p1[0];
97 >        } else if (p1[0] != pd->r) {
98 >                if (dup2(p1[0], pd->r) < 0)
99 >                        return(-1);
100 >                close(p1[0]);
101 >        }
102 >        if (!(pd->flags & PF_FILT_OUT)) {
103 >                close(p1[1]);
104 >                pd->w = p0[1];
105 >        } else if (p0[1] != pd->w) {
106 >                if (dup2(p0[1], pd->w) < 0)
107 >                        return(-1);
108 >                close(p0[1]);
109 >        }
110 >        /*
111 >         * Close write stream on exec to avoid multiprocessing deadlock.
112 >         * No use in read stream without it, so set flag there as well.
113 >         * GW: This bug took me two days to figure out!!
114 >         */
115 >        if (pd->r > 0)
116 >                fcntl(pd->r, F_SETFD, FD_CLOEXEC);
117 >        if (pd->w > 1)
118 >                fcntl(pd->w, F_SETFD, FD_CLOEXEC);
119 >        pd->flags |= PF_RUNNING;
120          return(PIPE_BUF);
121   }
122  
123  
62
124   int
125 < close_process(          /* close pipes and wait for process */
126 < SUBPROC *pd
125 > close_processes(        /* close pipes and wait for processes to finish */
126 >        SUBPROC pd[],
127 >        int nproc
128   )
129   {
130 <        int     pid, status;
130 >        int     togo = nproc;
131 >        int     status, rtn_status = 0;
132 >        RT_PID  pid;
133 >        int     i;
134  
135 <        if (!pd->running)
136 <                return(0);
137 <        close(pd->r);
138 <        close(pd->w);
139 <        pd->running = 0;
140 <        if (waitpid(pd->pid, &status, 0) == pd->pid)
135 >        for (i = 0; i < nproc; i++)             /* close pipes, first */
136 >                if (pd[i].flags & PF_RUNNING) {
137 >                        close(pd[i].w);
138 >                        close(pd[i].r);
139 >                        pd[i].flags &= ~PF_RUNNING;
140 >                } else
141 >                        togo -= (pd[i].pid < 0);
142 >        if (nproc == 1) {                       /* await specific process? */
143 >                status = 0;
144 >                if (waitpid(pd->pid, &status, 0) != pd->pid)
145 >                        return(-1);
146 >                *pd = sp_inactive;
147                  return(status>>8 & 0xff);
148 <        return(-1);             /* ? unknown status */
148 >        }
149 >                                                /* else unordered wait */
150 >        while (togo > 0 && (pid = wait(&status)) >= 0) {
151 >                for (i = nproc; i-- > 0; )
152 >                        if (pd[i].pid == pid) {
153 >                                pd[i] = sp_inactive;
154 >                                --togo;
155 >                                break;
156 >                        }
157 >                if (i < 0)
158 >                        continue;               /* child we don't know? */
159 >                status = status>>8 & 0xff;
160 >                if (status)                     /* record non-zero status */
161 >                        rtn_status = status;
162 >        }
163 >        if (togo)                               /* child went missing? */
164 >                return(-1);
165 >        return(rtn_status);
166   }
79
80

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines