ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/process.c
Revision: 2.4
Committed: Fri Apr 16 10:26:45 1993 UTC (31 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +18 -4 lines
Log Message:
made readbuf() and writebuf() retry after interrupted system call

File Contents

# Content
1 /* Copyright (c) 1991 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * Routines to communicate with separate process via dual pipes
9 */
10
11 /* find pipe buffer limit */
12 #include <sys/param.h>
13
14 #ifndef PIPE_BUF
15 #ifdef PIPSIZ
16 #define PIPE_BUF PIPSIZ
17 #else
18 #ifdef PIPE_MAX
19 #define PIPE_BUF PIPE_MAX
20 #else
21 #define PIPE_BUF 512 /* hyperconservative */
22 #endif
23 #endif
24 #endif
25
26 #include "vfork.h"
27
28 #ifndef BSD
29 #include <errno.h>
30 #endif
31
32
33 int
34 open_process(pd, av) /* open communication to separate process */
35 int pd[3];
36 char *av[];
37 {
38 extern char *getpath(), *getenv();
39 char *compath;
40 int p0[2], p1[2];
41 /* find executable */
42 compath = getpath(av[0], getenv("PATH"), 1);
43 if (compath == 0)
44 return(0);
45 if (pipe(p0) < 0 || pipe(p1) < 0)
46 return(-1);
47 if ((pd[2] = vfork()) == 0) { /* if child */
48 close(p0[1]);
49 close(p1[0]);
50 if (p0[0] != 0) { /* connect p0 to stdin */
51 dup2(p0[0], 0);
52 close(p0[0]);
53 }
54 if (p1[1] != 1) { /* connect p1 to stdout */
55 dup2(p1[1], 1);
56 close(p1[1]);
57 }
58 execv(compath, av); /* exec command */
59 perror(compath);
60 _exit(127);
61 }
62 if (pd[2] == -1)
63 return(-1);
64 close(p0[0]);
65 close(p1[1]);
66 pd[0] = p1[0];
67 pd[1] = p0[1];
68 return(PIPE_BUF);
69 }
70
71
72 int
73 process(pd, recvbuf, sendbuf, nbr, nbs) /* process data through pd */
74 int pd[3];
75 char *recvbuf, *sendbuf;
76 int nbr, nbs;
77 {
78 if (nbs > PIPE_BUF)
79 return(-1);
80 if (writebuf(pd[1], sendbuf, nbs) < nbs)
81 return(-1);
82 return(readbuf(pd[0], recvbuf, nbr));
83 }
84
85
86 int
87 close_process(pd) /* close pipes and wait for process */
88 int pd[3];
89 {
90 int pid, status;
91
92 close(pd[1]);
93 close(pd[0]);
94 while ((pid = wait(&status)) != -1)
95 if (pid == pd[2])
96 return(status>>8 & 0xff);
97 return(-1); /* ? unknown status */
98 }
99
100
101 int
102 readbuf(fd, bpos, siz) /* read all of requested buffer */
103 int fd;
104 char *bpos;
105 int siz;
106 {
107 register int cc = 0, nrem = siz;
108 retry:
109 while (nrem > 0 && (cc = read(fd, bpos, nrem)) > 0) {
110 bpos += cc;
111 nrem -= cc;
112 }
113 if (cc < 0) {
114 #ifndef BSD
115 if (errno == EINTR) /* we were interrupted! */
116 goto retry;
117 #endif
118 return(cc);
119 }
120 return(siz-nrem);
121 }
122
123
124 int
125 writebuf(fd, bpos, siz) /* write all of requested buffer */
126 int fd;
127 char *bpos;
128 int siz;
129 {
130 register int cc = 0, nrem = siz;
131 retry:
132 while (nrem > 0 && (cc = write(fd, bpos, nrem)) > 0) {
133 bpos += cc;
134 nrem -= cc;
135 }
136 if (cc < 0) {
137 #ifndef BSD
138 if (errno == EINTR) /* we were interrupted! */
139 goto retry;
140 #endif
141 return(cc);
142 }
143 return(siz-nrem);
144 }