ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/unix_process.c
Revision: 3.3
Committed: Mon Nov 10 16:41:52 2003 UTC (20 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.2: +1 -2 lines
Log Message:
Removed redeclaration of fork() call + vfork.h header, using rtprocess.h instead

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: unix_process.c,v 3.2 2003/07/17 09:21:29 schorsch 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
16 #include "rtprocess.h"
17
18
19 int
20 open_process( /* open communication to separate process */
21 SUBPROC *pd,
22 char *av[]
23 )
24 {
25 extern char *getpath(), *getenv();
26 char *compath;
27 int p0[2], p1[2];
28
29 pd->running = 0; /* not yet */
30 /* find executable */
31 compath = getpath(av[0], getenv("PATH"), 1);
32 if (compath == 0)
33 return(0);
34 if (pipe(p0) < 0 || pipe(p1) < 0)
35 return(-1);
36 if ((pd->pid = vfork()) == 0) { /* if child */
37 close(p0[1]);
38 close(p1[0]);
39 if (p0[0] != 0) { /* connect p0 to stdin */
40 dup2(p0[0], 0);
41 close(p0[0]);
42 }
43 if (p1[1] != 1) { /* connect p1 to stdout */
44 dup2(p1[1], 1);
45 close(p1[1]);
46 }
47 execv(compath, av); /* exec command */
48 perror(compath);
49 _exit(127);
50 }
51 if (pd->pid == -1)
52 return(-1);
53 close(p0[0]);
54 close(p1[1]);
55 pd->r = p1[0];
56 pd->w = p0[1];
57 pd->running = 1;
58 return(PIPE_BUF);
59 }
60
61
62
63 int
64 close_process( /* close pipes and wait for process */
65 SUBPROC *pd
66 )
67 {
68 int pid, status;
69
70 close(pd->r);
71 close(pd->w);
72 pd->running = 0;
73 while ((pid = wait(&status)) != -1)
74 if (pid == pd->pid)
75 return(status>>8 & 0xff);
76 return(-1); /* ? unknown status */
77 }
78
79