2 * Copyright 1993, 1995 Christopher Seiwald.
3 * Copyright 2007 Noel Belcourt.
5 * This file is part of Jam - see jam.c for Copyright information.
16 #include <unistd.h> /* needed for vfork(), _exit() prototypes */
17 #include <sys/resource.h>
18 #include <sys/times.h>
21 #if defined(sun) || defined(__sun) || defined(linux)
27 #include <sys/times.h>
29 #if defined(__APPLE__)
34 #define vfork() fork()
39 * execunix.c - execute a shell script on UNIX/WinNT/OS2/AmigaOS
41 * If $(JAMSHELL) is defined, uses that to formulate execvp()/spawnvp().
44 * /bin/sh -c % [ on UNIX/AmigaOS ]
45 * cmd.exe /c % [ on OS2/WinNT ]
47 * Each word must be an individual element in a jam variable value.
49 * In $(JAMSHELL), % expands to the command string and ! expands to the slot
50 * number (starting at 1) for multiprocess (-j) invocations. If $(JAMSHELL) does
51 * not include a %, it is tacked on as the last argument.
53 * Do not just set JAMSHELL to /bin/sh or cmd.exe - it will not work!
56 * exec_cmd() - launch an async command execution.
57 * exec_wait() - wait and drive at most one execution completion.
60 * onintr() - bump intr to note command interruption.
62 * 04/08/94 (seiwald) - Coherent/386 support added.
63 * 05/04/94 (seiwald) - async multiprocess interface
64 * 01/22/95 (seiwald) - $(JAMSHELL) support
65 * 06/02/97 (gsar) - full async multiprocess support for Win32
68 static clock_t tps = 0;
69 static struct timeval tv;
70 static int select_timeout = 0;
72 static int cmdsrunning = 0;
73 static struct tms old_time;
80 int pid; /* on win32, a real process handle */
81 int fd[2]; /* file descriptors for stdout and stderr */
82 FILE *stream[2]; /* child's stdout (0) and stderr (1) file stream */
83 clock_t start_time; /* start time of child process */
84 int exit_reason; /* termination status */
85 int action_length; /* length of action string */
86 int target_length; /* length of target string */
87 char *action; /* buffer to hold action and target invoked */
88 char *target; /* buffer to hold action and target invoked */
89 char *command; /* buffer to hold command being invoked */
90 char *buffer[2]; /* buffer to hold stdout and stderr, if any */
91 void (*func)( void *closure, int status, timing_info*, char *, char * );
93 time_t start_dt; /* start of command timestamp */
94 } cmdtab[ MAXJOBS ] = {{0}};
97 * onintr() - bump intr to note command interruption
100 void onintr( int disp )
103 printf( "...interrupted\n" );
108 * exec_cmd() - launch an async command execution.
114 void (*func)( void *closure, int status, timing_info*, char *, char * ),
121 static int initialized = 0;
126 char * argv[ MAXARGC + 1 ]; /* +1 for NULL */
128 /* Find a slot in the running commands table for this one. */
129 for ( slot = 0; slot < MAXJOBS; ++slot )
130 if ( !cmdtab[ slot ].pid )
133 if ( slot == MAXJOBS )
135 printf( "no slots for child!\n" );
139 /* Forumulate argv. If shell was defined, be prepared for % and ! subs.
140 * Otherwise, use stock /bin/sh on unix or cmd.exe on NT.
148 sprintf( jobno, "%d", slot + 1 );
150 for ( i = 0; shell && i < MAXARGC; ++i, shell = list_next( shell ) )
152 switch ( shell->string[0] )
154 case '%': argv[ i ] = string; ++gotpercent; break;
155 case '!': argv[ i ] = jobno; break;
156 default : argv[ i ] = shell->string;
159 printf( "argv[%d] = '%s'\n", i, argv[ i ] );
163 argv[ i++ ] = string;
169 argv[ 0 ] = "/bin/sh";
175 /* Increment jobs running. */
178 /* Save off actual command string. */
179 cmdtab[ slot ].command = BJAM_MALLOC_ATOMIC( strlen( string ) + 1 );
180 strcpy( cmdtab[ slot ].command, string );
182 /* Initialize only once. */
189 /* Create pipes from child to parent. */
191 if ( pipe( out ) < 0 )
194 if ( pipe( err ) < 0 )
198 /* Start the command */
200 cmdtab[ slot ].start_dt = time(0);
202 if ( 0 < globs.timeout )
205 * Handle hung processes by manually tracking elapsed time and signal
206 * process when time limit expires.
209 cmdtab[ slot ].start_time = times( &buf );
211 /* Make a global, only do this once. */
212 if ( tps == 0 ) tps = sysconf( _SC_CLK_TCK );
215 if ( ( cmdtab[ slot ].pid = vfork() ) == 0 )
222 dup2( out[1], STDOUT_FILENO );
224 if ( globs.pipe_action == 0 )
225 dup2( out[1], STDERR_FILENO );
227 dup2( err[1], STDERR_FILENO );
232 /* Make this process a process group leader so that when we kill it, all
233 * child processes of this process are terminated as well. We use
234 * killpg(pid, SIGKILL) to kill the process group leader and all its
237 if ( 0 < globs.timeout )
239 struct rlimit r_limit;
240 r_limit.rlim_cur = globs.timeout;
241 r_limit.rlim_max = globs.timeout;
242 setrlimit( RLIMIT_CPU, &r_limit );
245 execvp( argv[0], argv );
249 else if ( cmdtab[ slot ].pid == -1 )
255 setpgid( cmdtab[ slot ].pid, cmdtab[ slot ].pid );
257 /* close write end of pipes */
261 /* set both file descriptors to non-blocking */
262 fcntl(out[0], F_SETFL, O_NONBLOCK);
263 fcntl(err[0], F_SETFL, O_NONBLOCK);
265 /* child writes stdout to out[1], parent reads from out[0] */
266 cmdtab[ slot ].fd[ OUT ] = out[0];
267 cmdtab[ slot ].stream[ OUT ] = fdopen( cmdtab[ slot ].fd[ OUT ], "rb" );
268 if ( cmdtab[ slot ].stream[ OUT ] == NULL )
274 /* child writes stderr to err[1], parent reads from err[0] */
275 if (globs.pipe_action == 0)
281 cmdtab[ slot ].fd[ ERR ] = err[0];
282 cmdtab[ slot ].stream[ ERR ] = fdopen( cmdtab[ slot ].fd[ ERR ], "rb" );
283 if ( cmdtab[ slot ].stream[ ERR ] == NULL )
290 /* Ensure enough room for rule and target name. */
291 if ( action && target )
293 len = strlen( action ) + 1;
294 if ( cmdtab[ slot ].action_length < len )
296 BJAM_FREE( cmdtab[ slot ].action );
297 cmdtab[ slot ].action = BJAM_MALLOC_ATOMIC( len );
298 cmdtab[ slot ].action_length = len;
300 strcpy( cmdtab[ slot ].action, action );
301 len = strlen( target ) + 1;
302 if ( cmdtab[ slot ].target_length < len )
304 BJAM_FREE( cmdtab[ slot ].target );
305 cmdtab[ slot ].target = BJAM_MALLOC_ATOMIC( len );
306 cmdtab[ slot ].target_length = len;
308 strcpy( cmdtab[ slot ].target, target );
312 BJAM_FREE( cmdtab[ slot ].action );
313 BJAM_FREE( cmdtab[ slot ].target );
314 cmdtab[ slot ].action = 0;
315 cmdtab[ slot ].target = 0;
316 cmdtab[ slot ].action_length = 0;
317 cmdtab[ slot ].target_length = 0;
320 /* Save the operation for exec_wait() to find. */
321 cmdtab[ slot ].func = func;
322 cmdtab[ slot ].closure = closure;
324 /* Wait until we are under the limit of concurrent commands. Do not trust
327 while ( ( cmdsrunning >= MAXJOBS ) || ( cmdsrunning >= globs.jobs ) )
333 /* Returns 1 if file is closed, 0 if descriptor is still live.
335 * i is index into cmdtab
337 * s (stream) indexes:
338 * - cmdtab[ i ].stream[ s ]
339 * - cmdtab[ i ].buffer[ s ]
340 * - cmdtab[ i ].fd [ s ]
343 int read_descriptor( int i, int s )
349 while ( 0 < ( ret = fread( buffer, sizeof(char), BUFSIZ-1, cmdtab[ i ].stream[ s ] ) ) )
352 if ( !cmdtab[ i ].buffer[ s ] )
354 /* Never been allocated. */
355 cmdtab[ i ].buffer[ s ] = (char*)BJAM_MALLOC_ATOMIC( ret + 1 );
356 memcpy( cmdtab[ i ].buffer[ s ], buffer, ret + 1 );
360 /* Previously allocated. */
361 char * tmp = cmdtab[ i ].buffer[ s ];
363 cmdtab[ i ].buffer[ s ] = (char*)BJAM_MALLOC_ATOMIC( len + ret + 1 );
364 memcpy( cmdtab[ i ].buffer[ s ], tmp, len );
365 memcpy( cmdtab[ i ].buffer[ s ] + len, buffer, ret + 1 );
370 return feof(cmdtab[ i ].stream[ s ]);
374 void close_streams( int i, int s )
376 /* Close the stream and pipe descriptor. */
377 fclose(cmdtab[ i ].stream[ s ]);
378 cmdtab[ i ].stream[ s ] = 0;
380 close(cmdtab[ i ].fd[ s ]);
381 cmdtab[ i ].fd[ s ] = 0;
385 void populate_file_descriptors( int * fmax, fd_set * fds)
389 clock_t current = times( &buf );
390 select_timeout = globs.timeout;
392 /* Compute max read file descriptor for use in select. */
394 for ( i = 0; i < globs.jobs; ++i )
396 if ( 0 < cmdtab[ i ].fd[ OUT ] )
398 fd_max = fd_max < cmdtab[ i ].fd[ OUT ] ? cmdtab[ i ].fd[ OUT ] : fd_max;
399 FD_SET(cmdtab[ i ].fd[ OUT ], fds);
401 if ( globs.pipe_action != 0 )
403 if (0 < cmdtab[ i ].fd[ ERR ])
405 fd_max = fd_max < cmdtab[ i ].fd[ ERR ] ? cmdtab[ i ].fd[ ERR ] : fd_max;
406 FD_SET(cmdtab[ i ].fd[ ERR ], fds);
410 if (globs.timeout && cmdtab[ i ].pid) {
411 clock_t consumed = (current - cmdtab[ i ].start_time) / tps;
412 clock_t process_timesout = globs.timeout - consumed;
413 if (0 < process_timesout && process_timesout < select_timeout) {
414 select_timeout = process_timesout;
416 if ( globs.timeout <= consumed )
418 killpg( cmdtab[ i ].pid, SIGKILL );
419 cmdtab[ i ].exit_reason = EXIT_TIMEOUT;
428 * exec_wait() - wait and drive at most one execution completion.
440 timing_info time_info;
444 /* Handle naive make1() which does not know if commands are running. */
448 /* Process children that signaled. */
450 while ( !finished && cmdsrunning )
452 /* Compute max read file descriptor for use in select(). */
453 populate_file_descriptors( &fd_max, &fds );
455 if ( 0 < globs.timeout )
457 /* Force select() to timeout so we can terminate expired processes.
459 tv.tv_sec = select_timeout;
462 /* select() will wait until: i/o on a descriptor, a signal, or we
465 ret = select( fd_max + 1, &fds, 0, 0, &tv );
469 /* select() will wait until i/o on a descriptor or a signal. */
470 ret = select( fd_max + 1, &fds, 0, 0, 0 );
475 for ( i = 0; i < globs.jobs; ++i )
479 if ( FD_ISSET( cmdtab[ i ].fd[ OUT ], &fds ) )
480 out = read_descriptor( i, OUT );
482 if ( ( globs.pipe_action != 0 ) &&
483 ( FD_ISSET( cmdtab[ i ].fd[ ERR ], &fds ) ) )
484 err = read_descriptor( i, ERR );
486 /* If feof on either descriptor, then we are done. */
489 /* Close the stream and pipe descriptors. */
490 close_streams( i, OUT );
491 if ( globs.pipe_action != 0 )
492 close_streams( i, ERR );
494 /* Reap the child and release resources. */
495 pid = waitpid( cmdtab[ i ].pid, &status, 0 );
497 if ( pid == cmdtab[ i ].pid )
503 /* Set reason for exit if not timed out. */
504 if ( WIFEXITED( status ) )
506 cmdtab[ i ].exit_reason = 0 == WEXITSTATUS( status )
511 /* Print out the rule and target name. */
512 out_action( cmdtab[ i ].action, cmdtab[ i ].target,
513 cmdtab[ i ].command, cmdtab[ i ].buffer[ OUT ],
514 cmdtab[ i ].buffer[ ERR ], cmdtab[ i ].exit_reason
519 time_info.system = (double)( new_time.tms_cstime - old_time.tms_cstime ) / CLOCKS_PER_SEC;
520 time_info.user = (double)( new_time.tms_cutime - old_time.tms_cutime ) / CLOCKS_PER_SEC;
521 time_info.start = cmdtab[ i ].start_dt;
522 time_info.end = time( 0 );
526 /* Drive the completion. */
530 rstat = EXEC_CMD_INTR;
531 else if ( status != 0 )
532 rstat = EXEC_CMD_FAIL;
536 /* Assume -p0 in effect so only pass buffer[ 0 ]
537 * containing merged output.
539 (*cmdtab[ i ].func)( cmdtab[ i ].closure, rstat,
540 &time_info, cmdtab[ i ].command,
541 cmdtab[ i ].buffer[ 0 ] );
543 BJAM_FREE( cmdtab[ i ].buffer[ OUT ] );
544 cmdtab[ i ].buffer[ OUT ] = 0;
546 BJAM_FREE( cmdtab[ i ].buffer[ ERR ] );
547 cmdtab[ i ].buffer[ ERR ] = 0;
549 BJAM_FREE( cmdtab[ i ].command );
550 cmdtab[ i ].command = 0;
552 cmdtab[ i ].func = 0;
553 cmdtab[ i ].closure = 0;
554 cmdtab[ i ].start_time = 0;
558 printf( "unknown pid %d with errno = %d\n", pid, errno );
569 # endif /* USE_EXECUNIX */