1 |
– |
/* Copyright (c) 1996 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 |
|
* Parallel network process handling routines |
6 |
|
*/ |
7 |
|
|
8 |
+ |
#include <stdlib.h> |
9 |
|
#include <stdio.h> |
10 |
+ |
#include <string.h> |
11 |
|
#include <signal.h> |
12 |
|
#include <fcntl.h> |
13 |
+ |
#include <unistd.h> |
14 |
+ |
#include <sys/wait.h> |
15 |
+ |
|
16 |
+ |
#include "rtmisc.h" |
17 |
|
#include "selcall.h" |
18 |
|
#include "netproc.h" |
19 |
|
#include "paths.h" |
17 |
– |
#include "vfork.h" |
20 |
|
|
21 |
|
PSERVER *pslist = NULL; /* global process server list */ |
22 |
|
|
23 |
< |
static PROC *pindex[FD_SETSIZE]; /* process index table */ |
23 |
> |
static NETPROC *pindex[FD_SETSIZE]; /* process index table */ |
24 |
|
|
25 |
|
static char ourhost[64]; /* this host name */ |
26 |
< |
static char ourdir[MAXPATH]; /* our working directory */ |
26 |
> |
static char ourdir[PATH_MAX]; /* our working directory */ |
27 |
|
static char ouruser[32]; /* our user name */ |
28 |
|
static char *ourshell; /* our user's shell */ |
29 |
|
|
32 |
|
|
33 |
|
extern char *remsh; /* externally defined remote shell program */ |
34 |
|
|
35 |
< |
extern char *malloc(), *realloc(); |
36 |
< |
extern char *getenv(); |
35 |
> |
static int readerrs(int fd); |
36 |
> |
static void wait4end(void); |
37 |
> |
static int finishjob(PSERVER *ps, int pn, int status); |
38 |
|
|
39 |
|
|
40 |
< |
PSERVER * |
41 |
< |
addpserver(host, dir, usr, np) /* add a new process server */ |
42 |
< |
char *host, *dir, *usr; |
43 |
< |
int np; |
40 |
> |
extern PSERVER * |
41 |
> |
addpserver( /* add a new process server */ |
42 |
> |
char *host, |
43 |
> |
char *dir, |
44 |
> |
char *usr, |
45 |
> |
int np |
46 |
> |
) |
47 |
|
{ |
48 |
|
register PSERVER *ps; |
49 |
|
/* allocate the struct */ |
50 |
|
if (np < 1) |
51 |
|
return(NULL); |
52 |
< |
ps = (PSERVER *)malloc(sizeof(PSERVER)+(np-1)*sizeof(PROC)); |
52 |
> |
ps = (PSERVER *)malloc(sizeof(PSERVER)+(np-1)*sizeof(NETPROC)); |
53 |
|
if (ps == NULL) |
54 |
|
return(NULL); |
55 |
|
if (!ourhost[0]) { /* initialize */ |
56 |
< |
char dirtmp[MAXPATH]; |
56 |
> |
char dirtmp[PATH_MAX]; |
57 |
|
register char *cp; |
58 |
|
register int len; |
59 |
|
|
60 |
|
strcpy(ourhost, myhostname()); |
61 |
< |
getwd(dirtmp); |
61 |
> |
getcwd(dirtmp, sizeof(dirtmp)); |
62 |
|
if ((cp = getenv("HOME")) != NULL) { |
63 |
|
if (!strcmp(cp, dirtmp)) |
64 |
|
ourdir[0] = '\0'; |
112 |
|
} |
113 |
|
|
114 |
|
|
115 |
< |
delpserver(ps) /* delete a process server */ |
116 |
< |
PSERVER *ps; |
115 |
> |
extern void |
116 |
> |
delpserver( /* delete a process server */ |
117 |
> |
PSERVER *ps |
118 |
> |
) |
119 |
|
{ |
120 |
|
PSERVER pstart; |
121 |
|
register PSERVER *psp; |
134 |
|
/* remove server from list */ |
135 |
|
psp->next = ps->next; |
136 |
|
pslist = pstart.next; |
137 |
< |
free((char *)ps); /* free associated memory */ |
137 |
> |
free((void *)ps); /* free associated memory */ |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
< |
PSERVER * |
142 |
< |
findjob(pnp) /* find out where process is running */ |
143 |
< |
register int *pnp; /* modified */ |
141 |
> |
extern PSERVER * |
142 |
> |
findjob( /* find out where process is running */ |
143 |
> |
register int *pnp /* modified */ |
144 |
> |
) |
145 |
|
{ |
146 |
|
register PSERVER *ps; |
147 |
|
register int i; |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
< |
int |
160 |
< |
startjob(ps, command, compf) /* start a job on a process server */ |
161 |
< |
register PSERVER *ps; |
162 |
< |
char *command; |
163 |
< |
int (*compf)(); |
159 |
> |
extern int |
160 |
> |
startjob( /* start a job on a process server */ |
161 |
> |
register PSERVER *ps, |
162 |
> |
char *command, |
163 |
> |
pscompfunc *compf |
164 |
> |
) |
165 |
|
{ |
166 |
< |
char udirt[MAXPATH]; |
166 |
> |
char udirt[PATH_MAX]; |
167 |
|
char *av[16]; |
168 |
|
int pfd[2], pid; |
169 |
|
register int i; |
185 |
|
exit(1); |
186 |
|
} |
187 |
|
/* start child process */ |
188 |
< |
if ((pid = vfork()) == 0) { |
188 |
> |
if ((pid = fork()) == 0) { |
189 |
|
close(pfd[0]); /* connect stderr to pipe */ |
190 |
|
if (pfd[1] != 2) { |
191 |
|
dup2(pfd[1], 2); |
229 |
|
ps->proc[i].pid = pid; |
230 |
|
close(pfd[1]); /* get piped stderr file descriptor */ |
231 |
|
ps->proc[i].efd = pfd[0]; |
232 |
< |
fcntl(pfd[0], F_SETFD, 1); /* set close on exec flag */ |
232 |
> |
fcntl(pfd[0], F_SETFD, FD_CLOEXEC); /* set close on exec flag */ |
233 |
|
pindex[pfd[0]] = ps->proc + i; /* assign error fd index */ |
234 |
|
FD_SET(pfd[0], &errdesc); /* add to select call parameter */ |
235 |
|
if (pfd[0] > maxfd) |
239 |
|
|
240 |
|
|
241 |
|
static int |
242 |
< |
readerrs(fd) /* read error output from fd */ |
243 |
< |
int fd; |
242 |
> |
readerrs( /* read error output from fd */ |
243 |
> |
int fd |
244 |
> |
) |
245 |
|
{ |
246 |
|
char errbuf[BUFSIZ]; |
247 |
|
int nr; |
248 |
< |
register PROC *pp; |
248 |
> |
register NETPROC *pp; |
249 |
|
/* look up associated process */ |
250 |
|
if ((pp = pindex[fd]) == NULL) |
251 |
|
abort(); /* serious consistency error */ |
260 |
|
if (pp->elen == 0) |
261 |
|
pp->errs = (char *)malloc(nr+1); |
262 |
|
else |
263 |
< |
pp->errs = (char *)realloc(pp->errs, pp->elen+nr+1); |
263 |
> |
pp->errs = (char *)realloc((void *)pp->errs, pp->elen+nr+1); |
264 |
|
if (pp->errs == NULL) { |
265 |
|
perror("malloc failed"); |
266 |
|
exit(1); |
271 |
|
} |
272 |
|
|
273 |
|
|
274 |
< |
static |
275 |
< |
wait4end() /* read error streams until someone is done */ |
274 |
> |
static void |
275 |
> |
wait4end(void) /* read error streams until someone is done */ |
276 |
|
{ |
277 |
|
fd_set readfds, excepfds; |
278 |
|
register int i; |
294 |
|
|
295 |
|
|
296 |
|
static int |
297 |
< |
finishjob(ps, pn, status) /* clean up finished process */ |
298 |
< |
PSERVER *ps; |
299 |
< |
int pn; |
300 |
< |
int status; |
297 |
> |
finishjob( /* clean up finished process */ |
298 |
> |
PSERVER *ps, |
299 |
> |
int pn, |
300 |
> |
int status |
301 |
> |
) |
302 |
|
{ |
303 |
< |
register PROC *pp; |
303 |
> |
register NETPROC *pp; |
304 |
|
|
305 |
|
pp = ps->proc + pn; |
306 |
|
if (pp->cf != NULL) /* client cleanup */ |
308 |
|
close(pp->efd); /* close error stream */ |
309 |
|
pindex[pp->efd] = NULL; |
310 |
|
FD_CLR(pp->efd, &errdesc); |
311 |
< |
free((char *)pp->errs); |
311 |
> |
free((void *)pp->errs); |
312 |
|
pp->com = NULL; /* clear settings */ |
313 |
|
pp->pid = -1; |
314 |
|
pp->efd = -1; |
319 |
|
} |
320 |
|
|
321 |
|
|
322 |
< |
int |
323 |
< |
wait4job(ps, pid) /* wait for process to finish */ |
324 |
< |
PSERVER *ps; |
325 |
< |
int pid; |
322 |
> |
extern int |
323 |
> |
wait4job( /* wait for process to finish */ |
324 |
> |
PSERVER *ps, |
325 |
> |
int pid |
326 |
> |
) |
327 |
|
{ |
328 |
|
int status, psn, psn2; |
329 |
|
PSERVER *ps2; |