1 |
– |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Routines to communicate with separate process via dual pipes |
6 |
+ |
* |
7 |
+ |
* External symbols declared in rtprocess.h |
8 |
|
*/ |
9 |
|
|
10 |
< |
/* find pipe buffer limit */ |
12 |
< |
#include <sys/param.h> |
10 |
> |
#include "copyright.h" |
11 |
|
|
12 |
< |
#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 |
12 |
> |
#include "rtprocess.h" |
13 |
|
|
14 |
< |
#ifndef BSD |
27 |
< |
#define vfork fork |
28 |
< |
#endif |
14 |
> |
/* |
15 |
|
|
16 |
+ |
The functions open_process() and close_process() exist in |
17 |
+ |
(currently) two versions, which are found in the files: |
18 |
|
|
19 |
< |
int |
20 |
< |
open_process(pd, av) /* open communication to separate process */ |
33 |
< |
int pd[3]; |
34 |
< |
char *av[]; |
35 |
< |
{ |
36 |
< |
extern char *getpath(), *getenv(); |
37 |
< |
char *compath; |
38 |
< |
int p0[2], p1[2]; |
39 |
< |
/* find executable */ |
40 |
< |
compath = getpath(av[0], getenv("PATH"), 1); |
41 |
< |
if (compath == 0) |
42 |
< |
return(0); |
43 |
< |
if (pipe(p0) < 0 || pipe(p1) < 0) |
44 |
< |
return(-1); |
45 |
< |
if ((pd[2] = vfork()) == 0) { /* if child */ |
46 |
< |
close(p0[1]); |
47 |
< |
close(p1[0]); |
48 |
< |
if (p0[0] != 0) { /* connect p0 to stdin */ |
49 |
< |
dup2(p0[0], 0); |
50 |
< |
close(p0[0]); |
51 |
< |
} |
52 |
< |
if (p1[1] != 1) { /* connect p1 to stdout */ |
53 |
< |
dup2(p1[1], 1); |
54 |
< |
close(p1[1]); |
55 |
< |
} |
56 |
< |
execv(compath, av); /* exec command */ |
57 |
< |
perror(compath); |
58 |
< |
_exit(127); |
59 |
< |
} |
60 |
< |
if (pd[2] == -1) |
61 |
< |
return(-1); |
62 |
< |
close(p0[0]); |
63 |
< |
close(p1[1]); |
64 |
< |
pd[0] = p1[0]; |
65 |
< |
pd[1] = p0[1]; |
66 |
< |
return(PIPE_BUF); |
67 |
< |
} |
19 |
> |
win_process.c |
20 |
> |
unix_process.c |
21 |
|
|
22 |
+ |
*/ |
23 |
|
|
24 |
|
int |
25 |
< |
process(pd, recvbuf, sendbuf, nbr, nbs) /* process data through pd */ |
26 |
< |
int pd[3]; |
27 |
< |
char *recvbuf, *sendbuf; |
28 |
< |
int nbr, nbs; |
25 |
> |
process( /* process data through pd */ |
26 |
> |
SUBPROC *pd, |
27 |
> |
void *recvbuf, void *sendbuf, |
28 |
> |
int nbr, int nbs |
29 |
> |
) |
30 |
|
{ |
31 |
< |
if (nbs > PIPE_BUF) |
31 |
> |
if (!(pd->flags & PF_RUNNING)) |
32 |
|
return(-1); |
33 |
< |
if (writebuf(pd[1], sendbuf, nbs) < nbs) |
33 |
> |
if (writebuf(pd->w, sendbuf, nbs) < nbs) |
34 |
|
return(-1); |
35 |
< |
return(readbuf(pd[0], recvbuf, nbr)); |
35 |
> |
return(readbuf(pd->r, recvbuf, nbr)); |
36 |
|
} |
37 |
|
|
38 |
|
|
84 |
– |
int |
85 |
– |
close_process(pd) /* close pipes and wait for process */ |
86 |
– |
int pd[3]; |
87 |
– |
{ |
88 |
– |
int pid, status; |
39 |
|
|
40 |
< |
close(pd[1]); |
41 |
< |
close(pd[0]); |
42 |
< |
while ((pid = wait(&status)) != -1) |
43 |
< |
if (pid == pd[2]) |
44 |
< |
return(status>>8 & 0xff); |
45 |
< |
return(-1); /* ? unknown status */ |
96 |
< |
} |
97 |
< |
|
98 |
< |
|
99 |
< |
int |
100 |
< |
readbuf(fd, bpos, siz) /* read all of requested buffer */ |
101 |
< |
int fd; |
102 |
< |
char *bpos; |
103 |
< |
int siz; |
40 |
> |
ssize_t |
41 |
> |
readbuf( /* read all of requested buffer */ |
42 |
> |
int fd, |
43 |
> |
void *buf, |
44 |
> |
ssize_t siz |
45 |
> |
) |
46 |
|
{ |
47 |
< |
register int cc, nrem = siz; |
48 |
< |
|
47 |
> |
char *bpos = (char *)buf; |
48 |
> |
ssize_t cc = 0, nrem = siz; |
49 |
> |
retry: |
50 |
|
while (nrem > 0 && (cc = read(fd, bpos, nrem)) > 0) { |
51 |
|
bpos += cc; |
52 |
|
nrem -= cc; |
53 |
|
} |
54 |
< |
if (cc < 0) |
54 |
> |
if (cc < 0) { |
55 |
> |
#ifndef BSD |
56 |
> |
if (errno == EINTR) /* we were interrupted! */ |
57 |
> |
goto retry; |
58 |
> |
#endif |
59 |
|
return(cc); |
60 |
+ |
} |
61 |
|
return(siz-nrem); |
62 |
|
} |
63 |
|
|
64 |
|
|
65 |
< |
int |
66 |
< |
writebuf(fd, bpos, siz) /* write all of requested buffer */ |
67 |
< |
int fd; |
68 |
< |
char *bpos; |
69 |
< |
int siz; |
65 |
> |
ssize_t |
66 |
> |
writebuf( /* write all of requested buffer */ |
67 |
> |
int fd, |
68 |
> |
void *buf, |
69 |
> |
ssize_t siz |
70 |
> |
) |
71 |
|
{ |
72 |
< |
register int cc, nrem = siz; |
73 |
< |
|
72 |
> |
char *bpos = (char *)buf; |
73 |
> |
ssize_t cc = 0, nrem = siz; |
74 |
> |
retry: |
75 |
|
while (nrem > 0 && (cc = write(fd, bpos, nrem)) > 0) { |
76 |
|
bpos += cc; |
77 |
|
nrem -= cc; |
78 |
|
} |
79 |
< |
if (cc < 0) |
79 |
> |
if (cc < 0) { |
80 |
> |
#ifndef BSD |
81 |
> |
if (errno == EINTR) /* we were interrupted! */ |
82 |
> |
goto retry; |
83 |
> |
#endif |
84 |
|
return(cc); |
85 |
+ |
} |
86 |
|
return(siz-nrem); |
87 |
|
} |