ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/unix_process.c
Revision: 3.14
Committed: Tue May 14 18:37:36 2019 UTC (5 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.13: +8 -2 lines
Log Message:
Call vfork() if supported with exec()

File Contents

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