ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/unix_process.c
Revision: 3.15
Committed: Sat Dec 28 18:05:14 2019 UTC (4 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.14: +1 -3 lines
Log Message:
Removed redundant include files

File Contents

# User Rev Content
1 schorsch 3.1 #ifndef lint
2 greg 3.15 static const char RCSid[] = "$Id: unix_process.c,v 3.14 2019/05/14 18:37:36 greg Exp $";
3 schorsch 3.1 #endif
4     /*
5     * Routines to communicate with separate process via dual pipes
6     * Unix version
7     *
8     * External symbols declared in standard.h
9     */
10    
11     #include "copyright.h"
12 schorsch 3.2
13     #include <sys/wait.h>
14 greg 3.7 #include <stdlib.h>
15 schorsch 3.1
16     #include "rtprocess.h"
17 greg 3.7 #include "rtio.h"
18 schorsch 3.1
19    
20     int
21     open_process( /* open communication to separate process */
22     SUBPROC *pd,
23     char *av[]
24     )
25     {
26     char *compath;
27     int p0[2], p1[2];
28    
29 greg 3.12 pd->pid = -1;
30 greg 3.10 pd->running = 0; /* not going yet */
31    
32     if (av == NULL) /* cloning operation? */
33     compath = NULL;
34     else if ((compath = getpath(av[0], getenv("PATH"), X_OK)) == NULL)
35 schorsch 3.1 return(0);
36     if (pipe(p0) < 0 || pipe(p1) < 0)
37     return(-1);
38 greg 3.14 #ifdef BSD
39     if (compath != NULL)
40     pd->pid = vfork(); /* more efficient with exec() */
41     else
42     #endif
43     pd->pid = fork();
44     if (pd->pid == 0) { /* if child... */
45 schorsch 3.1 close(p0[1]);
46     close(p1[0]);
47     if (p0[0] != 0) { /* connect p0 to stdin */
48     dup2(p0[0], 0);
49     close(p0[0]);
50     }
51     if (p1[1] != 1) { /* connect p1 to stdout */
52     dup2(p1[1], 1);
53     close(p1[1]);
54     }
55 greg 3.10 if (compath == NULL) /* just cloning? */
56     return(0);
57     execv(compath, av); /* else exec command */
58 schorsch 3.1 perror(compath);
59     _exit(127);
60     }
61     if (pd->pid == -1)
62     return(-1);
63     close(p0[0]);
64     close(p1[1]);
65     pd->r = p1[0];
66     pd->w = p0[1];
67 greg 3.6 /*
68     * Close write stream on exec to avoid multiprocessing deadlock.
69     * No use in read stream without it, so set flag there as well.
70     * GW: This bug took me two days to figure out!!
71     */
72     fcntl(pd->r, F_SETFD, FD_CLOEXEC);
73     fcntl(pd->w, F_SETFD, FD_CLOEXEC);
74 schorsch 3.1 pd->running = 1;
75     return(PIPE_BUF);
76     }
77    
78    
79     int
80 greg 3.11 close_processes( /* close pipes and wait for processes to finish */
81     SUBPROC pd[],
82     int nproc
83 schorsch 3.1 )
84     {
85 greg 3.11 int togo = nproc;
86     int status, rtn_status = 0;
87     RT_PID pid;
88     int i;
89    
90     for (i = 0; i < nproc; i++) /* close pipes, first */
91     if (pd[i].running) {
92     close(pd[i].w);
93     close(pd[i].r);
94     pd[i].running = 0;
95 greg 3.13 } else
96     togo -= (pd[i].pid < 0);
97 greg 3.11 if (nproc == 1) { /* await specific process? */
98     if (waitpid(pd->pid, &status, 0) != pd->pid)
99     return(-1);
100 greg 3.12 pd->pid = -1;
101 greg 3.5 return(status>>8 & 0xff);
102 greg 3.11 }
103     /* else unordered wait */
104     while (togo > 0 && (pid = wait(&status)) >= 0) {
105     for (i = nproc; i-- > 0; )
106     if (pd[i].pid == pid) {
107 greg 3.12 pd[i].pid = -1;
108 greg 3.11 --togo;
109     break;
110     }
111     if (i < 0)
112     continue; /* child we don't know? */
113     status = status>>8 & 0xff;
114     if (status) /* record non-zero status */
115     rtn_status = status;
116     }
117     if (togo) /* child went missing? */
118     return(-1);
119     return(rtn_status);
120 schorsch 3.1 }