4 * An experimental VMS implementation of the same routine in [-.src]run.c
5 * <benjamin@cyclic.com>
7 * Derived in part from pipe.c, in this directory.
11 #include "vms-types.h"
26 #include <lib$routines.h>
31 /* Subprocess IO structure */
32 typedef struct _SUBIO {
40 struct IOSB read_iosb;
41 struct IOSB write_iosb;
44 unsigned long event_flag;
45 unsigned char event_flag_byte;
48 static SUBIO *siop_head = NULL, *siop_tail = NULL;
50 static int piped_child_exith(int);
52 static struct EXHCB piped_child_exit_handler_block =
53 {0, piped_child_exith, 1, &piped_child_exit_handler_block.exh$l_status, 0};
58 char body[NAM$C_MAXRSS+1];
61 /* Subprocess Completion AST */
62 void piped_child_done(siop)
68 if (siop->self != siop)
70 siop->read_iosb.status = SS$_NORMAL;
71 siop->write_iosb.status = SS$_NORMAL;
75 /* Exit handler, established by piped_child() */
77 piped_child_exith(status)
87 if (siop->self != siop)
93 /* Finish pending reads and shutdown */
94 if(!siop->read_iosb.status)
96 fflush (siop->read_fp);
97 fclose (siop->read_fp);
100 fclose (siop->read_fp);
101 sys$dassgn (siop->read_chan);
103 /* Finish pending writes and shutdown */
104 if(!siop->write_iosb.status)
106 fflush (siop->write_fp);
107 sys$qio (0, siop->write_chan, IO$_WRITEOF, &iosb,
108 0, 0, 0, 0, 0, 0, 0, 0);
109 fclose (siop->write_fp);
112 fclose (siop->write_fp);
113 sys$dassgn (siop->write_chan);
115 sys$synch (siop->event_flag, &siop->write_iosb);
122 int piped_child(command, tofdp, fromfdp)
124 int *tofdp, *fromfdp;
126 static int exit_handler = 0;
127 struct IOSB iosb1, iosb2;
129 unsigned long flags, vmspid, return_status;
131 struct dsc$descriptor_s cmddsc;
132 struct dsc$descriptor_s read_mbxdsc, write_mbxdsc;
134 static Vstring read_mbxname, write_mbxname;
135 static struct itm$list3 write_mbxlist[2] = {
136 {sizeof(write_mbxname.body)-1, DVI$_DEVNAM,
137 &write_mbxname.body, (size_t *) &write_mbxname.length},
139 static struct itm$list3 read_mbxlist[2] = {
140 {sizeof(read_mbxname.body)-1, DVI$_DEVNAM,
141 &read_mbxname.body, (size_t *) &read_mbxname.length},
144 read_mbxname.length = sizeof(read_mbxname.body);
145 write_mbxname.length = sizeof(write_mbxname.body);
147 siop = (SUBIO *) calloc(1, sizeof(SUBIO));
150 perror("piped_child: malloc failed\n");
156 /* Construct command line by concatenating argument list */
157 strcpy(cmd, command[0]);
158 for(i=1; command[i] != NULL; i++)
161 strcat(cmd, command[i]);
165 fprintf(stderr, "piped_child: running '%s'\n", cmd);
167 /* Allocate a pair of temporary mailboxes (2kB each) */
168 rs1 = sys$crembx (0, &siop->read_chan, 2048, 2048, 0, 0, 0, 0);
169 rs2 = sys$crembx (0, &siop->write_chan, 2048, 2048, 0, 0, 0, 0);
171 if (rs1 != SS$_NORMAL || rs2 != SS$_NORMAL)
173 vaxc$errno = rs1 | rs2;
176 perror ("piped_child: $CREMBX failure");
180 /* Get mailbox names, so we can fopen() them */
181 rs1 = sys$getdviw (0, siop->read_chan, 0, &read_mbxlist,
184 rs2 = sys$getdviw (0, siop->write_chan, 0, &write_mbxlist,
187 if ((rs1 != SS$_NORMAL && !(iosb1.status & STS$M_SUCCESS)) ||
188 (rs2 != SS$_NORMAL && !(iosb2.status & STS$M_SUCCESS)))
190 vaxc$errno = iosb1.status | iosb2.status;
192 sys$dassgn (siop->read_chan);
193 sys$dassgn (siop->write_chan);
195 perror ("piped_child: $GETDVIW failure, could not get mailbox names");
201 fprintf(stderr, "piped_child: $GETDVIW succeeded, got mailbox names\n");
202 fprintf(stderr, "piped_child: ReadMBX: %s, WriteMBX: %s\n",
203 read_mbxname.body, write_mbxname.body);
207 write_mbxname.body[write_mbxname.length] = '\0';
208 read_mbxname.body[read_mbxname.length] = '\0';
211 write_mbxdsc.dsc$w_length = write_mbxname.length;
212 write_mbxdsc.dsc$b_dtype = DSC$K_DTYPE_T;
213 write_mbxdsc.dsc$b_class = DSC$K_CLASS_S;
214 write_mbxdsc.dsc$a_pointer = write_mbxname.body;
216 read_mbxdsc.dsc$w_length = read_mbxname.length;
217 read_mbxdsc.dsc$b_dtype = DSC$K_DTYPE_T;
218 read_mbxdsc.dsc$b_class = DSC$K_CLASS_S;
219 read_mbxdsc.dsc$a_pointer = read_mbxname.body;
221 /* Build descriptor for command line */
222 cmddsc.dsc$w_length = strlen(cmd);
223 cmddsc.dsc$b_dtype = DSC$K_DTYPE_T;
224 cmddsc.dsc$b_class = DSC$K_CLASS_S;
225 cmddsc.dsc$a_pointer = (char *) cmd;
227 flags = CLI$M_NOWAIT;
229 /* Allocate an event flag to signal process termination */
230 rs1 = lib$get_ef(&siop->event_flag);
231 if (rs1 != SS$_NORMAL)
235 sys$dassgn(siop->read_chan);
236 sys$dassgn(siop->write_chan);
237 perror("piped_child: LIB$GET_EF failed");
241 /* Save the EFN as a byte for later calls to other routines */
242 siop->event_flag_byte = 0xff & siop->event_flag;
245 fprintf(stderr, "piped_child: Got an EFN: %d\n", siop->event_flag_byte);
247 rs1 = lib$spawn(&cmddsc, &write_mbxdsc, &read_mbxdsc, &flags, 0,
248 &siop->pid, &siop->return_status, &siop->event_flag_byte,
249 &piped_child_done, siop->self);
251 if (rs1 != SS$_NORMAL)
255 sys$dassgn(siop->read_chan);
256 sys$dassgn(siop->write_chan);
257 perror("piped_child: LIB$SPAWN failure");
262 fprintf(stderr, "piped_child: LIB$SPAWN succeeded, pid is %08x.\n",
265 /* Establish an exit handler so the process isn't prematurely terminated */
268 rs1 = sys$dclexh (&piped_child_exit_handler_block);
269 if (rs1 != SS$_NORMAL)
273 sys$dassgn (siop->read_chan);
274 sys$dassgn (siop->write_chan);
275 sys$delprc (siop->pid, 0);
277 perror("piped_child: $DCLEXH failure");
283 /* Let's open some files */
284 siop->read_fp = fopen (read_mbxname.body, "r");
285 siop->write_fp = fopen (write_mbxname.body, "w");
287 if (!siop->read_fp || !siop->write_fp)
289 sys$dassgn (siop->read_chan);
290 sys$dassgn (siop->write_chan);
291 sys$delprc (siop->pid);
293 perror("piped_child: fopen() failed");
297 *fromfdp = fileno(siop->read_fp);
298 *tofdp = fileno(siop->write_fp);
301 fprintf(stderr, "piped_child: file open successful: tofd=%d fromfd=%d\n",
304 /* Keep track of active subprocess I/O (SUBIO) structures */
307 siop_tail->next = siop;
308 siop->prev = siop_tail;
312 siop_head = siop_tail = siop;
319 * >0 VMS exit status of subprocess
320 * 0 success, subprocess was shutdown
321 * -1 pid not found in list of subprocesses
322 * -2 memory corruption detected
325 piped_child_shutdown(pid)
330 SUBIO *siop = siop_head;
332 while (siop && siop->self == siop && siop->pid != pid)
337 else if (siop->self != siop)
340 /* Finish reading and writing and shutdown */
341 if (siop->read_iosb.status)
343 fflush (siop->read_fp);
344 fclose (siop->read_fp);
347 fclose(siop->read_fp);
348 sys$dassgn (siop->read_chan);
350 if (siop->write_iosb.status)
352 fflush (siop->write_fp);
353 sys$qio (0, siop->write_chan, IO$_WRITEOF, &iosb,
354 0, 0, 0, 0, 0, 0, 0, 0);
355 fclose (siop->write_fp);
358 fclose(siop->write_fp);
359 sys$dassgn (siop->write_chan);
361 sys$synch (siop->event_flag, &siop->write_iosb);
362 lib$free_ef(&siop->event_flag);
364 /* Ditch SUBIO structure */
365 if (siop == siop_tail)
366 siop_tail = siop->prev;
367 if (siop == siop_head)
368 siop_head = siop->next;
370 siop->prev->next = siop->next;
372 siop->next->prev = siop->prev;
374 if (siop->return_status)
375 return_status = siop->return_status;
381 return return_status;