/Src/Dependencies/Boost/tools/build/v2/engine/execunix.c

http://hadesmem.googlecode.com/ · C · 569 lines · 382 code · 84 blank · 103 comment · 72 complexity · 554253a4eafc3c5e7de898c02ba044e2 MD5 · raw file

  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. #include "jam.h"
  8. #include "lists.h"
  9. #include "execcmd.h"
  10. #include "output.h"
  11. #include <errno.h>
  12. #include <signal.h>
  13. #include <stdio.h>
  14. #include <time.h>
  15. #include <unistd.h> /* needed for vfork(), _exit() prototypes */
  16. #include <sys/resource.h>
  17. #include <sys/times.h>
  18. #include <sys/wait.h>
  19. #if defined(sun) || defined(__sun) || defined(linux)
  20. #include <wait.h>
  21. #endif
  22. #ifdef USE_EXECUNIX
  23. #include <sys/times.h>
  24. #if defined(__APPLE__)
  25. #define NO_VFORK
  26. #endif
  27. #ifdef NO_VFORK
  28. #define vfork() fork()
  29. #endif
  30. /*
  31. * execunix.c - execute a shell script on UNIX/WinNT/OS2/AmigaOS
  32. *
  33. * If $(JAMSHELL) is defined, uses that to formulate execvp()/spawnvp().
  34. * The default is:
  35. *
  36. * /bin/sh -c % [ on UNIX/AmigaOS ]
  37. * cmd.exe /c % [ on OS2/WinNT ]
  38. *
  39. * Each word must be an individual element in a jam variable value.
  40. *
  41. * In $(JAMSHELL), % expands to the command string and ! expands to the slot
  42. * number (starting at 1) for multiprocess (-j) invocations. If $(JAMSHELL) does
  43. * not include a %, it is tacked on as the last argument.
  44. *
  45. * Do not just set JAMSHELL to /bin/sh or cmd.exe - it will not work!
  46. *
  47. * External routines:
  48. * exec_cmd() - launch an async command execution.
  49. * exec_wait() - wait and drive at most one execution completion.
  50. *
  51. * Internal routines:
  52. * onintr() - bump intr to note command interruption.
  53. *
  54. * 04/08/94 (seiwald) - Coherent/386 support added.
  55. * 05/04/94 (seiwald) - async multiprocess interface
  56. * 01/22/95 (seiwald) - $(JAMSHELL) support
  57. * 06/02/97 (gsar) - full async multiprocess support for Win32
  58. */
  59. static clock_t tps = 0;
  60. static struct timeval tv;
  61. static int select_timeout = 0;
  62. static int intr = 0;
  63. static int cmdsrunning = 0;
  64. static struct tms old_time;
  65. #define OUT 0
  66. #define ERR 1
  67. static struct
  68. {
  69. int pid; /* on win32, a real process handle */
  70. int fd[2]; /* file descriptors for stdout and stderr */
  71. FILE *stream[2]; /* child's stdout (0) and stderr (1) file stream */
  72. clock_t start_time; /* start time of child process */
  73. int exit_reason; /* termination status */
  74. int action_length; /* length of action string */
  75. int target_length; /* length of target string */
  76. char *action; /* buffer to hold action and target invoked */
  77. char *target; /* buffer to hold action and target invoked */
  78. char *command; /* buffer to hold command being invoked */
  79. char *buffer[2]; /* buffer to hold stdout and stderr, if any */
  80. void (*func)( void *closure, int status, timing_info*, char *, char * );
  81. void *closure;
  82. time_t start_dt; /* start of command timestamp */
  83. } cmdtab[ MAXJOBS ] = {{0}};
  84. /*
  85. * onintr() - bump intr to note command interruption
  86. */
  87. void onintr( int disp )
  88. {
  89. ++intr;
  90. printf( "...interrupted\n" );
  91. }
  92. /*
  93. * exec_cmd() - launch an async command execution.
  94. */
  95. void exec_cmd
  96. (
  97. char * string,
  98. void (*func)( void *closure, int status, timing_info*, char *, char * ),
  99. void * closure,
  100. LIST * shell,
  101. char * action,
  102. char * target
  103. )
  104. {
  105. static int initialized = 0;
  106. int out[2];
  107. int err[2];
  108. int slot;
  109. int len;
  110. char * argv[ MAXARGC + 1 ]; /* +1 for NULL */
  111. /* Find a slot in the running commands table for this one. */
  112. for ( slot = 0; slot < MAXJOBS; ++slot )
  113. if ( !cmdtab[ slot ].pid )
  114. break;
  115. if ( slot == MAXJOBS )
  116. {
  117. printf( "no slots for child!\n" );
  118. exit( EXITBAD );
  119. }
  120. /* Forumulate argv. If shell was defined, be prepared for % and ! subs.
  121. * Otherwise, use stock /bin/sh on unix or cmd.exe on NT.
  122. */
  123. if ( shell )
  124. {
  125. int i;
  126. char jobno[4];
  127. int gotpercent = 0;
  128. sprintf( jobno, "%d", slot + 1 );
  129. for ( i = 0; shell && i < MAXARGC; ++i, shell = list_next( shell ) )
  130. {
  131. switch ( shell->string[0] )
  132. {
  133. case '%': argv[ i ] = string; ++gotpercent; break;
  134. case '!': argv[ i ] = jobno; break;
  135. default : argv[ i ] = shell->string;
  136. }
  137. if ( DEBUG_EXECCMD )
  138. printf( "argv[%d] = '%s'\n", i, argv[ i ] );
  139. }
  140. if ( !gotpercent )
  141. argv[ i++ ] = string;
  142. argv[ i ] = 0;
  143. }
  144. else
  145. {
  146. argv[ 0 ] = "/bin/sh";
  147. argv[ 1 ] = "-c";
  148. argv[ 2 ] = string;
  149. argv[ 3 ] = 0;
  150. }
  151. /* Increment jobs running. */
  152. ++cmdsrunning;
  153. /* Save off actual command string. */
  154. cmdtab[ slot ].command = BJAM_MALLOC_ATOMIC( strlen( string ) + 1 );
  155. strcpy( cmdtab[ slot ].command, string );
  156. /* Initialize only once. */
  157. if ( !initialized )
  158. {
  159. times( &old_time );
  160. initialized = 1;
  161. }
  162. /* Create pipes from child to parent. */
  163. {
  164. if ( pipe( out ) < 0 )
  165. exit( EXITBAD );
  166. if ( pipe( err ) < 0 )
  167. exit( EXITBAD );
  168. }
  169. /* Start the command */
  170. cmdtab[ slot ].start_dt = time(0);
  171. if ( 0 < globs.timeout )
  172. {
  173. /*
  174. * Handle hung processes by manually tracking elapsed time and signal
  175. * process when time limit expires.
  176. */
  177. struct tms buf;
  178. cmdtab[ slot ].start_time = times( &buf );
  179. /* Make a global, only do this once. */
  180. if ( tps == 0 ) tps = sysconf( _SC_CLK_TCK );
  181. }
  182. if ( ( cmdtab[ slot ].pid = vfork() ) == 0 )
  183. {
  184. int pid = getpid();
  185. close( out[0] );
  186. close( err[0] );
  187. dup2( out[1], STDOUT_FILENO );
  188. if ( globs.pipe_action == 0 )
  189. dup2( out[1], STDERR_FILENO );
  190. else
  191. dup2( err[1], STDERR_FILENO );
  192. close( out[1] );
  193. close( err[1] );
  194. /* Make this process a process group leader so that when we kill it, all
  195. * child processes of this process are terminated as well. We use
  196. * killpg(pid, SIGKILL) to kill the process group leader and all its
  197. * children.
  198. */
  199. if ( 0 < globs.timeout )
  200. {
  201. struct rlimit r_limit;
  202. r_limit.rlim_cur = globs.timeout;
  203. r_limit.rlim_max = globs.timeout;
  204. setrlimit( RLIMIT_CPU, &r_limit );
  205. }
  206. setpgid( pid,pid );
  207. execvp( argv[0], argv );
  208. perror( "execvp" );
  209. _exit( 127 );
  210. }
  211. else if ( cmdtab[ slot ].pid == -1 )
  212. {
  213. perror( "vfork" );
  214. exit( EXITBAD );
  215. }
  216. setpgid( cmdtab[ slot ].pid, cmdtab[ slot ].pid );
  217. /* close write end of pipes */
  218. close( out[1] );
  219. close( err[1] );
  220. /* set both file descriptors to non-blocking */
  221. fcntl(out[0], F_SETFL, O_NONBLOCK);
  222. fcntl(err[0], F_SETFL, O_NONBLOCK);
  223. /* child writes stdout to out[1], parent reads from out[0] */
  224. cmdtab[ slot ].fd[ OUT ] = out[0];
  225. cmdtab[ slot ].stream[ OUT ] = fdopen( cmdtab[ slot ].fd[ OUT ], "rb" );
  226. if ( cmdtab[ slot ].stream[ OUT ] == NULL )
  227. {
  228. perror( "fdopen" );
  229. exit( EXITBAD );
  230. }
  231. /* child writes stderr to err[1], parent reads from err[0] */
  232. if (globs.pipe_action == 0)
  233. {
  234. close(err[0]);
  235. }
  236. else
  237. {
  238. cmdtab[ slot ].fd[ ERR ] = err[0];
  239. cmdtab[ slot ].stream[ ERR ] = fdopen( cmdtab[ slot ].fd[ ERR ], "rb" );
  240. if ( cmdtab[ slot ].stream[ ERR ] == NULL )
  241. {
  242. perror( "fdopen" );
  243. exit( EXITBAD );
  244. }
  245. }
  246. /* Ensure enough room for rule and target name. */
  247. if ( action && target )
  248. {
  249. len = strlen( action ) + 1;
  250. if ( cmdtab[ slot ].action_length < len )
  251. {
  252. BJAM_FREE( cmdtab[ slot ].action );
  253. cmdtab[ slot ].action = BJAM_MALLOC_ATOMIC( len );
  254. cmdtab[ slot ].action_length = len;
  255. }
  256. strcpy( cmdtab[ slot ].action, action );
  257. len = strlen( target ) + 1;
  258. if ( cmdtab[ slot ].target_length < len )
  259. {
  260. BJAM_FREE( cmdtab[ slot ].target );
  261. cmdtab[ slot ].target = BJAM_MALLOC_ATOMIC( len );
  262. cmdtab[ slot ].target_length = len;
  263. }
  264. strcpy( cmdtab[ slot ].target, target );
  265. }
  266. else
  267. {
  268. BJAM_FREE( cmdtab[ slot ].action );
  269. BJAM_FREE( cmdtab[ slot ].target );
  270. cmdtab[ slot ].action = 0;
  271. cmdtab[ slot ].target = 0;
  272. cmdtab[ slot ].action_length = 0;
  273. cmdtab[ slot ].target_length = 0;
  274. }
  275. /* Save the operation for exec_wait() to find. */
  276. cmdtab[ slot ].func = func;
  277. cmdtab[ slot ].closure = closure;
  278. /* Wait until we are under the limit of concurrent commands. Do not trust
  279. * globs.jobs alone.
  280. */
  281. while ( ( cmdsrunning >= MAXJOBS ) || ( cmdsrunning >= globs.jobs ) )
  282. if ( !exec_wait() )
  283. break;
  284. }
  285. /* Returns 1 if file is closed, 0 if descriptor is still live.
  286. *
  287. * i is index into cmdtab
  288. *
  289. * s (stream) indexes:
  290. * - cmdtab[ i ].stream[ s ]
  291. * - cmdtab[ i ].buffer[ s ]
  292. * - cmdtab[ i ].fd [ s ]
  293. */
  294. int read_descriptor( int i, int s )
  295. {
  296. int ret;
  297. int len;
  298. char buffer[BUFSIZ];
  299. while ( 0 < ( ret = fread( buffer, sizeof(char), BUFSIZ-1, cmdtab[ i ].stream[ s ] ) ) )
  300. {
  301. buffer[ret] = 0;
  302. if ( !cmdtab[ i ].buffer[ s ] )
  303. {
  304. /* Never been allocated. */
  305. cmdtab[ i ].buffer[ s ] = (char*)BJAM_MALLOC_ATOMIC( ret + 1 );
  306. memcpy( cmdtab[ i ].buffer[ s ], buffer, ret + 1 );
  307. }
  308. else
  309. {
  310. /* Previously allocated. */
  311. char * tmp = cmdtab[ i ].buffer[ s ];
  312. len = strlen( tmp );
  313. cmdtab[ i ].buffer[ s ] = (char*)BJAM_MALLOC_ATOMIC( len + ret + 1 );
  314. memcpy( cmdtab[ i ].buffer[ s ], tmp, len );
  315. memcpy( cmdtab[ i ].buffer[ s ] + len, buffer, ret + 1 );
  316. BJAM_FREE( tmp );
  317. }
  318. }
  319. return feof(cmdtab[ i ].stream[ s ]);
  320. }
  321. void close_streams( int i, int s )
  322. {
  323. /* Close the stream and pipe descriptor. */
  324. fclose(cmdtab[ i ].stream[ s ]);
  325. cmdtab[ i ].stream[ s ] = 0;
  326. close(cmdtab[ i ].fd[ s ]);
  327. cmdtab[ i ].fd[ s ] = 0;
  328. }
  329. void populate_file_descriptors( int * fmax, fd_set * fds)
  330. {
  331. int i, fd_max = 0;
  332. struct tms buf;
  333. clock_t current = times( &buf );
  334. select_timeout = globs.timeout;
  335. /* Compute max read file descriptor for use in select. */
  336. FD_ZERO(fds);
  337. for ( i = 0; i < globs.jobs; ++i )
  338. {
  339. if ( 0 < cmdtab[ i ].fd[ OUT ] )
  340. {
  341. fd_max = fd_max < cmdtab[ i ].fd[ OUT ] ? cmdtab[ i ].fd[ OUT ] : fd_max;
  342. FD_SET(cmdtab[ i ].fd[ OUT ], fds);
  343. }
  344. if ( globs.pipe_action != 0 )
  345. {
  346. if (0 < cmdtab[ i ].fd[ ERR ])
  347. {
  348. fd_max = fd_max < cmdtab[ i ].fd[ ERR ] ? cmdtab[ i ].fd[ ERR ] : fd_max;
  349. FD_SET(cmdtab[ i ].fd[ ERR ], fds);
  350. }
  351. }
  352. if (globs.timeout && cmdtab[ i ].pid) {
  353. clock_t consumed = (current - cmdtab[ i ].start_time) / tps;
  354. clock_t process_timesout = globs.timeout - consumed;
  355. if (0 < process_timesout && process_timesout < select_timeout) {
  356. select_timeout = process_timesout;
  357. }
  358. if ( globs.timeout <= consumed )
  359. {
  360. killpg( cmdtab[ i ].pid, SIGKILL );
  361. cmdtab[ i ].exit_reason = EXIT_TIMEOUT;
  362. }
  363. }
  364. }
  365. *fmax = fd_max;
  366. }
  367. /*
  368. * exec_wait() - wait and drive at most one execution completion.
  369. */
  370. int exec_wait()
  371. {
  372. int i;
  373. int ret;
  374. int fd_max;
  375. int pid;
  376. int status;
  377. int finished;
  378. int rstat;
  379. timing_info time_info;
  380. fd_set fds;
  381. struct tms new_time;
  382. /* Handle naive make1() which does not know if commands are running. */
  383. if ( !cmdsrunning )
  384. return 0;
  385. /* Process children that signaled. */
  386. finished = 0;
  387. while ( !finished && cmdsrunning )
  388. {
  389. /* Compute max read file descriptor for use in select(). */
  390. populate_file_descriptors( &fd_max, &fds );
  391. if ( 0 < globs.timeout )
  392. {
  393. /* Force select() to timeout so we can terminate expired processes.
  394. */
  395. tv.tv_sec = select_timeout;
  396. tv.tv_usec = 0;
  397. /* select() will wait until: i/o on a descriptor, a signal, or we
  398. * time out.
  399. */
  400. ret = select( fd_max + 1, &fds, 0, 0, &tv );
  401. }
  402. else
  403. {
  404. /* select() will wait until i/o on a descriptor or a signal. */
  405. ret = select( fd_max + 1, &fds, 0, 0, 0 );
  406. }
  407. if ( 0 < ret )
  408. {
  409. for ( i = 0; i < globs.jobs; ++i )
  410. {
  411. int out = 0;
  412. int err = 0;
  413. if ( FD_ISSET( cmdtab[ i ].fd[ OUT ], &fds ) )
  414. out = read_descriptor( i, OUT );
  415. if ( ( globs.pipe_action != 0 ) &&
  416. ( FD_ISSET( cmdtab[ i ].fd[ ERR ], &fds ) ) )
  417. err = read_descriptor( i, ERR );
  418. /* If feof on either descriptor, then we are done. */
  419. if ( out || err )
  420. {
  421. /* Close the stream and pipe descriptors. */
  422. close_streams( i, OUT );
  423. if ( globs.pipe_action != 0 )
  424. close_streams( i, ERR );
  425. /* Reap the child and release resources. */
  426. pid = waitpid( cmdtab[ i ].pid, &status, 0 );
  427. if ( pid == cmdtab[ i ].pid )
  428. {
  429. finished = 1;
  430. pid = 0;
  431. cmdtab[ i ].pid = 0;
  432. /* Set reason for exit if not timed out. */
  433. if ( WIFEXITED( status ) )
  434. {
  435. cmdtab[ i ].exit_reason = 0 == WEXITSTATUS( status )
  436. ? EXIT_OK
  437. : EXIT_FAIL;
  438. }
  439. /* Print out the rule and target name. */
  440. out_action( cmdtab[ i ].action, cmdtab[ i ].target,
  441. cmdtab[ i ].command, cmdtab[ i ].buffer[ OUT ],
  442. cmdtab[ i ].buffer[ ERR ], cmdtab[ i ].exit_reason
  443. );
  444. times( &new_time );
  445. time_info.system = (double)( new_time.tms_cstime - old_time.tms_cstime ) / CLOCKS_PER_SEC;
  446. time_info.user = (double)( new_time.tms_cutime - old_time.tms_cutime ) / CLOCKS_PER_SEC;
  447. time_info.start = cmdtab[ i ].start_dt;
  448. time_info.end = time( 0 );
  449. old_time = new_time;
  450. /* Drive the completion. */
  451. --cmdsrunning;
  452. if ( intr )
  453. rstat = EXEC_CMD_INTR;
  454. else if ( status != 0 )
  455. rstat = EXEC_CMD_FAIL;
  456. else
  457. rstat = EXEC_CMD_OK;
  458. /* Assume -p0 in effect so only pass buffer[ 0 ]
  459. * containing merged output.
  460. */
  461. (*cmdtab[ i ].func)( cmdtab[ i ].closure, rstat,
  462. &time_info, cmdtab[ i ].command,
  463. cmdtab[ i ].buffer[ 0 ] );
  464. BJAM_FREE( cmdtab[ i ].buffer[ OUT ] );
  465. cmdtab[ i ].buffer[ OUT ] = 0;
  466. BJAM_FREE( cmdtab[ i ].buffer[ ERR ] );
  467. cmdtab[ i ].buffer[ ERR ] = 0;
  468. BJAM_FREE( cmdtab[ i ].command );
  469. cmdtab[ i ].command = 0;
  470. cmdtab[ i ].func = 0;
  471. cmdtab[ i ].closure = 0;
  472. cmdtab[ i ].start_time = 0;
  473. }
  474. else
  475. {
  476. printf( "unknown pid %d with errno = %d\n", pid, errno );
  477. exit( EXITBAD );
  478. }
  479. }
  480. }
  481. }
  482. }
  483. return 1;
  484. }
  485. # endif /* USE_EXECUNIX */