1 /*
2  * Copyright 1993, 1995 Christopher Seiwald.
3  * Copyright 2007 Noel Belcourt.
4  *
5  * This file is part of Jam - see jam.c for Copyright information.
6  */
7 
8 #include "jam.h"
9 #include "execcmd.h"
10 
11 #include "lists.h"
12 #include "output.h"
13 #include "strings.h"
14 
15 #include <errno.h>
16 #include <signal.h>
17 #include <stdio.h>
18 #include <time.h>
19 #include <unistd.h>  /* vfork(), _exit(), STDOUT_FILENO and such */
20 #include <sys/resource.h>
21 #include <sys/times.h>
22 #include <sys/wait.h>
23 #include <poll.h>
24 
25 #if defined(sun) || defined(__sun)
26     #include <wait.h>
27 #endif
28 
29 #ifdef USE_EXECUNIX
30 
31 #include <sys/times.h>
32 
33 #if defined(__APPLE__)
34     #define NO_VFORK
35 #endif
36 
37 #ifdef NO_VFORK
38     #define vfork() fork()
39 #endif
40 
41 
42 /*
43  * execunix.c - execute a shell script on UNIX/OS2/AmigaOS
44  *
45  * If $(JAMSHELL) is defined, uses that to formulate execvp()/spawnvp(). The
46  * default is: /bin/sh -c
47  *
48  * In $(JAMSHELL), % expands to the command string and ! expands to the slot
49  * number (starting at 1) for multiprocess (-j) invocations. If $(JAMSHELL) does
50  * not include a %, it is tacked on as the last argument.
51  *
52  * Each word must be an individual element in a jam variable value.
53  *
54  * Do not just set JAMSHELL to /bin/sh - it will not work!
55  *
56  * External routines:
57  *  exec_check() - preprocess and validate the command.
58  *  exec_cmd() - launch an async command execution.
59  *  exec_wait() - wait for any of the async command processes to terminate.
60  */
61 
62 /* find a free slot in the running commands table */
63 static int get_free_cmdtab_slot();
64 
65 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
66 
67 static clock_t tps;
68 
69 /* We hold stdout & stderr child process information in two element arrays
70  * indexed as follows.
71  */
72 #define OUT 0
73 #define ERR 1
74 
75 static struct cmdtab_t
76 {
77     int          pid;            /* on win32, a real process handle */
78     int          fd[ 2 ];        /* file descriptors for stdout and stderr */
79     FILE      *  stream[ 2 ];    /* child's stdout and stderr file streams */
80     clock_t      start_time;     /* start time of child process */
81     int          exit_reason;    /* termination status */
82     char      *  buffer[ 2 ];    /* buffers to hold stdout and stderr, if any */
83     int          buf_size[ 2 ];  /* buffer sizes in bytes */
84     timestamp    start_dt;       /* start of command timestamp */
85 
86     int flags;
87 
88     /* Function called when the command completes. */
89     ExecCmdCallback func;
90 
91     /* Opaque data passed back to the 'func' callback. */
92     void * closure;
93 } * cmdtab = NULL;
94 static int cmdtab_size = 0;
95 
96 /* Contains both stdin and stdout of all processes.
97  * The length is either globs.jobs or globs.jobs * 2
98  * depending on globs.pipe_action.
99  */
100 struct pollfd * wait_fds = NULL;
101 #define WAIT_FDS_SIZE ( globs.jobs * ( globs.pipe_action ? 2 : 1 ) )
102 #define GET_WAIT_FD( job_idx ) ( wait_fds + ( ( job_idx * ( globs.pipe_action ? 2 : 1 ) ) ) )
103 
104 /*
105  * exec_init() - global initialization
106  */
exec_init(void)107 void exec_init( void )
108 {
109     int i;
110     if ( globs.jobs > cmdtab_size )
111     {
112         cmdtab = (cmdtab_t*)BJAM_REALLOC( cmdtab, globs.jobs * sizeof( *cmdtab ) );
113         memset( cmdtab + cmdtab_size, 0, ( globs.jobs - cmdtab_size ) * sizeof( *cmdtab ) );
114         wait_fds = (pollfd*)BJAM_REALLOC( wait_fds, WAIT_FDS_SIZE * sizeof ( *wait_fds ) );
115         for ( i = cmdtab_size; i < globs.jobs; ++i )
116         {
117             GET_WAIT_FD( i )[ OUT ].fd = -1;
118             GET_WAIT_FD( i )[ OUT ].events = POLLIN;
119             if ( globs.pipe_action )
120             {
121                 GET_WAIT_FD( i )[ ERR ].fd = -1;
122                 GET_WAIT_FD( i )[ ERR ].events = POLLIN;
123             }
124         }
125         cmdtab_size = globs.jobs;
126     }
127 }
128 
exec_done(void)129 void exec_done( void )
130 {
131     BJAM_FREE( cmdtab );
132     BJAM_FREE( wait_fds );
133 }
134 
135 /*
136  * exec_check() - preprocess and validate the command.
137  */
138 
exec_check(string const * command,LIST ** pShell,int * error_length,int * error_max_length)139 int exec_check
140 (
141     string const * command,
142     LIST * * pShell,
143     int * error_length,
144     int * error_max_length
145 )
146 {
147     int const is_raw_cmd = is_raw_command_request( *pShell );
148 
149     /* We allow empty commands for non-default shells since we do not really
150      * know what they are going to do with such commands.
151      */
152     if ( !command->size && ( is_raw_cmd || list_empty( *pShell ) ) )
153         return EXEC_CHECK_NOOP;
154 
155     return is_raw_cmd
156         ? EXEC_CHECK_OK
157         : check_cmd_for_too_long_lines( command->value, MAXLINE, error_length,
158             error_max_length );
159 }
160 
161 
162 /*
163  * exec_cmd() - launch an async command execution.
164  */
165 
166 /* We hold file descriptors for pipes used to communicate with child processes
167  * in two element arrays indexed as follows.
168  */
169 #define EXECCMD_PIPE_READ 0
170 #define EXECCMD_PIPE_WRITE 1
171 
exec_cmd(string const * command,int flags,ExecCmdCallback func,void * closure,LIST * shell)172 void exec_cmd
173 (
174     string const * command,
175     int flags,
176     ExecCmdCallback func,
177     void * closure,
178     LIST * shell
179 )
180 {
181     struct sigaction ignore, saveintr, savequit;
182     sigset_t chldmask, savemask;
183 
184     int const slot = get_free_cmdtab_slot();
185     int out[ 2 ];
186     int err[ 2 ];
187     int len;
188     char const * argv[ MAXARGC + 1 ];  /* +1 for NULL */
189 
190     /* Initialize default shell. */
191     static LIST * default_shell;
192     if ( !default_shell )
193         default_shell = list_push_back( list_new(
194             object_new( "/bin/sh" ) ),
195             object_new( "-c" ) );
196 
197     if ( list_empty( shell ) )
198         shell = default_shell;
199 
200     /* Forumulate argv. If shell was defined, be prepared for % and ! subs.
201      * Otherwise, use stock /bin/sh.
202      */
203     argv_from_shell( argv, shell, command->value, slot );
204 
205     if ( DEBUG_EXECCMD )
206     {
207         int i;
208         out_printf( "Using shell: " );
209         list_print( shell );
210         out_printf( "\n" );
211         for ( i = 0; argv[ i ]; ++i )
212             out_printf( "    argv[%d] = '%s'\n", i, argv[ i ] );
213     }
214 
215     /* Create pipes for collecting child output. */
216     if ( pipe( out ) < 0 || ( globs.pipe_action && pipe( err ) < 0 ) )
217     {
218         perror( "pipe" );
219         exit( EXITBAD );
220     }
221 
222     /* Start the command */
223 
224     timestamp_current( &cmdtab[ slot ].start_dt );
225 
226     if ( 0 < globs.timeout )
227     {
228         /* Handle hung processes by manually tracking elapsed time and signal
229          * process when time limit expires.
230          */
231         struct tms buf;
232         cmdtab[ slot ].start_time = times( &buf );
233 
234         /* Make a global, only do this once. */
235         if ( !tps ) tps = sysconf( _SC_CLK_TCK );
236     }
237 
238     /* Child does not need the read pipe ends used by the parent. */
239     fcntl( out[ EXECCMD_PIPE_READ ], F_SETFD, FD_CLOEXEC );
240     if ( globs.pipe_action )
241         fcntl( err[ EXECCMD_PIPE_READ ], F_SETFD, FD_CLOEXEC );
242 
243     /* ignore SIGINT and SIGQUIT */
244     ignore.sa_handler = SIG_IGN;
245     sigemptyset(&ignore.sa_mask);
246     ignore.sa_flags = 0;
247     if (sigaction(SIGINT, &ignore, &saveintr) < 0)
248         return;
249     if (sigaction(SIGQUIT, &ignore, &savequit) < 0)
250         return;
251 
252     /* block SIGCHLD */
253     sigemptyset(&chldmask);
254     sigaddset(&chldmask, SIGCHLD);
255     if (sigprocmask(SIG_BLOCK, &chldmask, &savemask) < 0)
256         return;
257 
258     if ( ( cmdtab[ slot ].pid = vfork() ) == -1 )
259     {
260         perror( "vfork" );
261         exit( EXITBAD );
262     }
263 
264     if ( cmdtab[ slot ].pid == 0 )
265     {
266         /*****************/
267         /* Child process */
268         /*****************/
269         int const pid = getpid();
270 
271         /* restore previous signals */
272         sigaction(SIGINT, &saveintr, NULL);
273         sigaction(SIGQUIT, &savequit, NULL);
274         sigprocmask(SIG_SETMASK, &savemask, NULL);
275 
276         /* Redirect stdout and stderr to pipes inherited from the parent. */
277         dup2( out[ EXECCMD_PIPE_WRITE ], STDOUT_FILENO );
278         dup2( globs.pipe_action ? err[ EXECCMD_PIPE_WRITE ] :
279             out[ EXECCMD_PIPE_WRITE ], STDERR_FILENO );
280         close( out[ EXECCMD_PIPE_WRITE ] );
281         if ( globs.pipe_action )
282             close( err[ EXECCMD_PIPE_WRITE ] );
283 
284         /* Make this process a process group leader so that when we kill it, all
285          * child processes of this process are terminated as well. We use
286          * killpg( pid, SIGKILL ) to kill the process group leader and all its
287          * children.
288          */
289         if ( 0 < globs.timeout )
290         {
291             struct rlimit r_limit;
292             r_limit.rlim_cur = globs.timeout;
293             r_limit.rlim_max = globs.timeout;
294             setrlimit( RLIMIT_CPU, &r_limit );
295         }
296         if (0 != setpgid( pid, pid )) {
297             perror("setpgid(child)");
298             /* exit( EXITBAD ); */
299         }
300         execvp( argv[ 0 ], (char * *)argv );
301         perror( "execvp" );
302         _exit( 127 );
303     }
304 
305     /******************/
306     /* Parent process */
307     /******************/
308 
309     /* redundant call, ignore return value */
310     setpgid(cmdtab[ slot ].pid, cmdtab[ slot ].pid);
311 
312     /* Parent not need the write pipe ends used by the child. */
313     close( out[ EXECCMD_PIPE_WRITE ] );
314     if ( globs.pipe_action )
315         close( err[ EXECCMD_PIPE_WRITE ] );
316 
317     /* Set both pipe read file descriptors to non-blocking. */
318     fcntl( out[ EXECCMD_PIPE_READ ], F_SETFL, O_NONBLOCK );
319     if ( globs.pipe_action )
320         fcntl( err[ EXECCMD_PIPE_READ ], F_SETFL, O_NONBLOCK );
321 
322     /* Parent reads from out[ EXECCMD_PIPE_READ ]. */
323     cmdtab[ slot ].fd[ OUT ] = out[ EXECCMD_PIPE_READ ];
324     cmdtab[ slot ].stream[ OUT ] = fdopen( cmdtab[ slot ].fd[ OUT ], "rb" );
325     if ( !cmdtab[ slot ].stream[ OUT ] )
326     {
327         perror( "fdopen" );
328         exit( EXITBAD );
329     }
330 
331     /* Parent reads from err[ EXECCMD_PIPE_READ ]. */
332     if ( globs.pipe_action )
333     {
334         cmdtab[ slot ].fd[ ERR ] = err[ EXECCMD_PIPE_READ ];
335         cmdtab[ slot ].stream[ ERR ] = fdopen( cmdtab[ slot ].fd[ ERR ], "rb" );
336         if ( !cmdtab[ slot ].stream[ ERR ] )
337         {
338             perror( "fdopen" );
339             exit( EXITBAD );
340         }
341     }
342 
343     GET_WAIT_FD( slot )[ OUT ].fd = out[ EXECCMD_PIPE_READ ];
344     if ( globs.pipe_action )
345         GET_WAIT_FD( slot )[ ERR ].fd = err[ EXECCMD_PIPE_READ ];
346 
347     cmdtab[ slot ].flags = flags;
348 
349     /* Save input data into the selected running commands table slot. */
350     cmdtab[ slot ].func = func;
351     cmdtab[ slot ].closure = closure;
352 
353     /* restore previous signals */
354     sigaction(SIGINT, &saveintr, NULL);
355     sigaction(SIGQUIT, &savequit, NULL);
356     sigprocmask(SIG_SETMASK, &savemask, NULL);
357 }
358 
359 #undef EXECCMD_PIPE_READ
360 #undef EXECCMD_PIPE_WRITE
361 
362 
363 /* Returns 1 if file descriptor is closed, or 0 if it is still alive.
364  *
365  * i is index into cmdtab
366  *
367  * s (stream) indexes:
368  *  - cmdtab[ i ].stream[ s ]
369  *  - cmdtab[ i ].buffer[ s ]
370  *  - cmdtab[ i ].fd    [ s ]
371  */
372 
read_descriptor(int i,int s)373 static int read_descriptor( int i, int s )
374 {
375     int ret;
376     char buffer[ BUFSIZ ];
377 
378     while ( 0 < ( ret = fread( buffer, sizeof( char ), BUFSIZ - 1,
379         cmdtab[ i ].stream[ s ] ) ) )
380     {
381         buffer[ ret ] = 0;
382 
383         /* Copy it to our output if appropriate */
384         if ( ! ( cmdtab[ i ].flags & EXEC_CMD_QUIET ) )
385         {
386             if ( s == OUT && ( globs.pipe_action != 2 ) )
387                 out_data( buffer );
388             else if ( s == ERR && ( globs.pipe_action & 2 ) )
389                 err_data( buffer );
390         }
391 
392         if ( !cmdtab[ i ].buffer[ s ] )
393         {
394             /* Never been allocated. */
395             if ( globs.max_buf && ret > globs.max_buf )
396             {
397                 ret = globs.max_buf;
398                 buffer[ ret ] = 0;
399             }
400             cmdtab[ i ].buf_size[ s ] = ret + 1;
401             cmdtab[ i ].buffer[ s ] = (char*)BJAM_MALLOC_ATOMIC( ret + 1 );
402             memcpy( cmdtab[ i ].buffer[ s ], buffer, ret + 1 );
403         }
404         else
405         {
406             /* Previously allocated. */
407             if ( cmdtab[ i ].buf_size[ s ] < globs.max_buf || !globs.max_buf )
408             {
409                 char * tmp = cmdtab[ i ].buffer[ s ];
410                 int const old_len = cmdtab[ i ].buf_size[ s ] - 1;
411                 int const new_len = old_len + ret + 1;
412                 cmdtab[ i ].buf_size[ s ] = new_len;
413                 cmdtab[ i ].buffer[ s ] = (char*)BJAM_MALLOC_ATOMIC( new_len );
414                 memcpy( cmdtab[ i ].buffer[ s ], tmp, old_len );
415                 memcpy( cmdtab[ i ].buffer[ s ] + old_len, buffer, ret + 1 );
416                 BJAM_FREE( tmp );
417             }
418         }
419     }
420 
421     /* If buffer full, ensure last buffer char is newline so that jam log
422      * contains the command status at beginning of it own line instead of
423      * appended to end of the previous output.
424      */
425     if ( globs.max_buf && globs.max_buf <= cmdtab[ i ].buf_size[ s ] )
426         cmdtab[ i ].buffer[ s ][ cmdtab[ i ].buf_size[ s ] - 2 ] = '\n';
427 
428     return feof( cmdtab[ i ].stream[ s ] );
429 }
430 
431 
432 /*
433  * close_streams() - Close the stream and pipe descriptor.
434  */
435 
close_streams(int const i,int const s)436 static void close_streams( int const i, int const s )
437 {
438     fclose( cmdtab[ i ].stream[ s ] );
439     cmdtab[ i ].stream[ s ] = 0;
440 
441     close( cmdtab[ i ].fd[ s ] );
442     cmdtab[ i ].fd[ s ] = 0;
443 
444     GET_WAIT_FD( i )[ s ].fd = -1;
445 }
446 
447 
448 /*
449  * exec_wait() - wait for any of the async command processes to terminate.
450  *
451  * May register more than one terminated child process but will exit as soon as
452  * at least one has been registered.
453  */
454 
exec_wait()455 void exec_wait()
456 {
457     int finished = 0;
458 
459     /* Process children that signaled. */
460     while ( !finished )
461     {
462         int i;
463         struct timeval tv;
464         struct timeval * ptv = NULL;
465         int select_timeout = globs.timeout;
466 
467         /* Check for timeouts:
468          *   - kill children that already timed out
469          *   - decide how long until the next one times out
470          */
471         if ( globs.timeout > 0 )
472         {
473             struct tms buf;
474             clock_t const current = times( &buf );
475             for ( i = 0; i < globs.jobs; ++i )
476                 if ( cmdtab[ i ].pid )
477                 {
478                     clock_t const consumed =
479                         ( current - cmdtab[ i ].start_time ) / tps;
480                     if ( consumed >= globs.timeout )
481                     {
482                         killpg( cmdtab[ i ].pid, SIGKILL );
483                         cmdtab[ i ].exit_reason = EXIT_TIMEOUT;
484                     }
485                     else if ( globs.timeout - consumed < select_timeout )
486                         select_timeout = globs.timeout - consumed;
487                 }
488 
489             /* If nothing else causes our select() call to exit, force it after
490              * however long it takes for the next one of our child processes to
491              * crossed its allotted processing time so we can terminate it.
492              */
493             tv.tv_sec = select_timeout;
494             tv.tv_usec = 0;
495             ptv = &tv;
496         }
497 
498         /* select() will wait for I/O on a descriptor, a signal, or timeout. */
499         {
500             /* disable child termination signals while in select */
501             int ret;
502             int timeout;
503             sigset_t sigmask;
504             sigemptyset(&sigmask);
505             sigaddset(&sigmask, SIGCHLD);
506             sigprocmask(SIG_BLOCK, &sigmask, NULL);
507 
508             /* If no timeout is specified, pass -1 (which means no timeout,
509              * wait indefinitely) to poll, to prevent busy-looping.
510              */
511             timeout = select_timeout? select_timeout * 1000 : -1;
512             while ( ( ret = poll( wait_fds, WAIT_FDS_SIZE, timeout ) ) == -1 )
513                 if ( errno != EINTR )
514                     break;
515             /* restore original signal mask by unblocking sigchld */
516             sigprocmask(SIG_UNBLOCK, &sigmask, NULL);
517             if ( ret <= 0 )
518                 continue;
519         }
520 
521         for ( i = 0; i < globs.jobs; ++i )
522         {
523             int out_done = 0;
524             int err_done = 0;
525             if ( GET_WAIT_FD( i )[ OUT ].revents )
526                 out_done = read_descriptor( i, OUT );
527 
528             if ( globs.pipe_action && ( GET_WAIT_FD( i )[ ERR ].revents ) )
529                 err_done = read_descriptor( i, ERR );
530 
531             /* If feof on either descriptor, we are done. */
532             if ( out_done || err_done )
533             {
534                 int pid;
535                 int status;
536                 int rstat;
537                 timing_info time_info;
538                 struct rusage cmd_usage;
539 
540                 /* We found a terminated child process - our search is done. */
541                 finished = 1;
542 
543                 /* Close the stream and pipe descriptors. */
544                 close_streams( i, OUT );
545                 if ( globs.pipe_action )
546                     close_streams( i, ERR );
547 
548                 /* Reap the child and release resources. */
549                 while ( ( pid = wait4( cmdtab[ i ].pid, &status, 0, &cmd_usage ) ) == -1 )
550                     if ( errno != EINTR )
551                         break;
552                 if ( pid != cmdtab[ i ].pid )
553                 {
554                     err_printf( "unknown pid %d with errno = %d\n", pid, errno );
555                     exit( EXITBAD );
556                 }
557 
558                 /* Set reason for exit if not timed out. */
559                 if ( WIFEXITED( status ) )
560                     cmdtab[ i ].exit_reason = WEXITSTATUS( status )
561                         ? EXIT_FAIL
562                         : EXIT_OK;
563 
564                 {
565                     time_info.system = ((double)(cmd_usage.ru_stime.tv_sec)*1000000.0+(double)(cmd_usage.ru_stime.tv_usec))/1000000.0;
566                     time_info.user   = ((double)(cmd_usage.ru_utime.tv_sec)*1000000.0+(double)(cmd_usage.ru_utime.tv_usec))/1000000.0;
567                     timestamp_copy( &time_info.start, &cmdtab[ i ].start_dt );
568                     timestamp_current( &time_info.end );
569                 }
570 
571                 /* Drive the completion. */
572                 if ( interrupted() )
573                     rstat = EXEC_CMD_INTR;
574                 else if ( status )
575                     rstat = EXEC_CMD_FAIL;
576                 else
577                     rstat = EXEC_CMD_OK;
578 
579                 /* Call the callback, may call back to jam rule land. */
580                 (*cmdtab[ i ].func)( cmdtab[ i ].closure, rstat, &time_info,
581                     cmdtab[ i ].buffer[ OUT ], cmdtab[ i ].buffer[ ERR ],
582                     cmdtab[ i ].exit_reason );
583 
584                 /* Clean up the command's running commands table slot. */
585                 BJAM_FREE( cmdtab[ i ].buffer[ OUT ] );
586                 cmdtab[ i ].buffer[ OUT ] = 0;
587                 cmdtab[ i ].buf_size[ OUT ] = 0;
588 
589                 BJAM_FREE( cmdtab[ i ].buffer[ ERR ] );
590                 cmdtab[ i ].buffer[ ERR ] = 0;
591                 cmdtab[ i ].buf_size[ ERR ] = 0;
592 
593                 cmdtab[ i ].pid = 0;
594                 cmdtab[ i ].func = 0;
595                 cmdtab[ i ].closure = 0;
596                 cmdtab[ i ].start_time = 0;
597             }
598         }
599     }
600 }
601 
602 
603 /*
604  * Find a free slot in the running commands table.
605  */
606 
get_free_cmdtab_slot()607 static int get_free_cmdtab_slot()
608 {
609     int slot;
610     for ( slot = 0; slot < globs.jobs; ++slot )
611         if ( !cmdtab[ slot ].pid )
612             return slot;
613     err_printf( "no slots for child!\n" );
614     exit( EXITBAD );
615 }
616 
617 # endif /* USE_EXECUNIX */
618