1 /* RCS $Id: runargv.c,v 1.3 2007-10-15 15:46:20 ihi Exp $
4 -- Invoke a sub process.
7 -- Use the standard methods of executing a sub process.
10 -- Dennis Vadura, dvadura@dmake.wticorp.com
13 -- http://dmake.wticorp.com/
16 -- Copyright (c) 1996,1997 by WTI Corp. All rights reserved.
18 -- This program is NOT free software; you can redistribute it and/or
19 -- modify it under the terms of the Software License Agreement Provided
20 -- in the file <distribution-root>/readme/license.txt.
23 -- Use cvs log to obtain detailed change logs.
50 static PR
*_procs
= NIL(PR
);
51 static int _proc_cnt
= 0;
52 static int _abort_flg
= FALSE
;
53 static int _use_i
= -1;
54 static int _do_upd
= 0;
56 static void _add_child
ANSI((int, CELLPTR
, int, int));
57 static void _attach_cmd
ANSI((char *, int, int, CELLPTR
, int, int));
58 static void _finished_child
ANSI((int, int));
59 static int _running
ANSI((CELLPTR
));
61 /* iz71422 changed the parameters for runargv but it (and the rest of
62 * qssl) got *NOT* fixed. */
63 iz81252 changed the parameters
for Pack_argv() and runargv() but
this file
66 runargv(target
, ignore
, group
, last
, shell
, cmd
)
78 if( _running(target
) /*&& Max_proc != 1*/ ) {
79 /* The command will be executed when the previous recipe
81 _attach_cmd( cmd
, group
, ignore
, target
, last
, shell
);
85 while( _proc_cnt
== Max_proc
)
86 if( Wait_for_child(FALSE
, -1) == -1 ) Fatal( "Lost a child %d", errno
);
88 argv
= Pack_argv( group
, shell
, cmd
);
94 case -1: /* fork failed */
95 Error("%s: %s", argv
[0], strerror(errno
));
96 Handle_result(-1, ignore
, _abort_flg
, target
);
100 execvp(argv
[0], argv
);
101 Continue
= TRUE
; /* survive error message */
102 Error("%s: %s", argv
[0], strerror(errno
));
103 kill(getpid(), SIGTERM
);
106 default: /* parent */
107 _add_child(pid
, target
, ignore
, last
);
115 Wait_for_child( abort_flg
, pid
)
123 waitchild
= (pid
== -1)? FALSE
: Wait_for_completion
;
126 if( (wid
= wait(&status
)) == -1 ) return(-1);
128 _abort_flg
= abort_flg
;
129 _finished_child(wid
, status
);
132 while( waitchild
&& pid
!= wid
);
143 if( _procs
!= NIL(PR
) ) {
144 for( i
=0; i
<Max_proc
; i
++ )
145 if( _procs
[i
].pr_valid
)
146 kill(_procs
[i
].pr_pid
, SIGTERM
);
148 while( Wait_for_child(TRUE
, -1) != -1 );
154 _add_child( pid
, target
, ignore
, last
)
163 if( _procs
== NIL(PR
) ) {
164 TALLOC( _procs
, Max_proc
, PR
);
167 if( (i
= _use_i
) == -1 )
168 for( i
=0; i
<Max_proc
; i
++ )
169 if( !_procs
[i
].pr_valid
)
176 pp
->pr_target
= target
;
177 pp
->pr_ignore
= ignore
;
179 pp
->pr_dir
= DmStrDup(Get_current_dir());
181 Current_target
= NIL(CELL
);
185 if( Wait_for_completion
) Wait_for_child( FALSE
, pid
);
190 _finished_child(pid
, status
)
198 for( i
=0; i
<Max_proc
; i
++ )
199 if( _procs
[i
].pr_valid
&& _procs
[i
].pr_pid
== pid
)
202 /* Some children we didn't make esp true if using /bin/sh to execute a
203 * a pipe and feed the output as a makefile into dmake. */
204 if( i
== Max_proc
) return;
205 _procs
[i
].pr_valid
= 0;
207 dir
= DmStrDup(Get_current_dir());
208 Set_dir( _procs
[i
].pr_dir
);
210 if( _procs
[i
].pr_recipe
!= NIL(RCP
) && !_abort_flg
) {
211 RCPPTR rp
= _procs
[i
].pr_recipe
;
214 Current_target
= _procs
[i
].pr_target
;
215 Handle_result( status
, _procs
[i
].pr_ignore
, FALSE
, _procs
[i
].pr_target
);
216 Current_target
= NIL(CELL
);
218 if ( _procs
[i
].pr_target
->ce_attr
& A_ERROR
) {
219 _procs
[i
].pr_last
= TRUE
;
220 goto ABORT_REMAINDER_OF_RECIPE
;
223 _procs
[i
].pr_recipe
= rp
->prp_next
;
226 runargv( _procs
[i
].pr_target
, rp
->prp_ignore
, rp
->prp_group
,
227 rp
->prp_last
, rp
->prp_shell
, rp
->prp_cmd
);
233 if( _proc_cnt
== Max_proc
) Wait_for_child( FALSE
, -1 );
236 Handle_result(status
,_procs
[i
].pr_ignore
,_abort_flg
,_procs
[i
].pr_target
);
238 ABORT_REMAINDER_OF_RECIPE
:
239 if( _procs
[i
].pr_last
) {
240 FREE(_procs
[i
].pr_dir
);
242 if( !Doing_bang
) Update_time_stamp( _procs
[i
].pr_target
);
257 if( !_procs
) return(FALSE
);
259 for( i
=0; i
<Max_proc
; i
++ )
260 if( _procs
[i
].pr_valid
&&
261 _procs
[i
].pr_target
== cp
)
264 return( i
!= Max_proc
);
269 _attach_cmd( cmd
, group
, ignore
, cp
, last
, shell
)
280 for( i
=0; i
<Max_proc
; i
++ )
281 if( _procs
[i
].pr_valid
&&
282 _procs
[i
].pr_target
== cp
)
285 TALLOC( rp
, 1, RCP
);
286 rp
->prp_cmd
= DmStrDup(cmd
);
287 rp
->prp_group
= group
;
288 rp
->prp_ignore
= ignore
;
290 rp
->prp_shell
= shell
;
292 if( _procs
[i
].pr_recipe
== NIL(RCP
) )
293 _procs
[i
].pr_recipe
= _procs
[i
].pr_recipe_end
= rp
;
295 _procs
[i
].pr_recipe_end
->prp_next
= rp
;
296 _procs
[i
].pr_recipe_end
= rp
;