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