runner.pm 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  1. #***************************************************************************
  2. # _ _ ____ _
  3. # Project ___| | | | _ \| |
  4. # / __| | | | |_) | |
  5. # | (__| |_| | _ <| |___
  6. # \___|\___/|_| \_\_____|
  7. #
  8. # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. #
  10. # This software is licensed as described in the file COPYING, which
  11. # you should have received as part of this distribution. The terms
  12. # are also available at https://curl.se/docs/copyright.html.
  13. #
  14. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. # copies of the Software, and permit persons to whom the Software is
  16. # furnished to do so, under the terms of the COPYING file.
  17. #
  18. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. # KIND, either express or implied.
  20. #
  21. # SPDX-License-Identifier: curl
  22. #
  23. ###########################################################################
  24. # This module contains entry points to run a single test. runner_init
  25. # determines whether they will run in a separate process or in the process of
  26. # the caller. The relevant interface is asynchronous so it will work in either
  27. # case. Program arguments are marshalled and then written to the end of a pipe
  28. # (in controlleripccall) which is later read from and the arguments
  29. # unmarshalled (in ipcrecv) before the desired function is called normally.
  30. # The function return values are then marshalled and written into another pipe
  31. # (again in ipcrecv) when is later read from and unmarshalled (in runnerar)
  32. # before being returned to the caller.
  33. package runner;
  34. use strict;
  35. use warnings;
  36. use 5.006;
  37. BEGIN {
  38. use base qw(Exporter);
  39. our @EXPORT = qw(
  40. checktestcmd
  41. prepro
  42. readtestkeywords
  43. restore_test_env
  44. runner_init
  45. runnerac_clearlocks
  46. runnerac_shutdown
  47. runnerac_stopservers
  48. runnerac_test_preprocess
  49. runnerac_test_run
  50. runnerar
  51. runnerar_ready
  52. stderrfilename
  53. stdoutfilename
  54. $DBGCURL
  55. $gdb
  56. $gdbthis
  57. $gdbxwin
  58. $shallow
  59. $tortalloc
  60. $valgrind_logfile
  61. $valgrind_tool
  62. );
  63. # these are for debugging only
  64. our @EXPORT_OK = qw(
  65. singletest_preprocess
  66. );
  67. }
  68. use B qw(
  69. svref_2object
  70. );
  71. use Storable qw(
  72. freeze
  73. thaw
  74. );
  75. use pathhelp qw(
  76. exe_ext
  77. );
  78. use processhelp qw(
  79. portable_sleep
  80. );
  81. use servers qw(
  82. checkcmd
  83. clearlocks
  84. initserverconfig
  85. serverfortest
  86. stopserver
  87. stopservers
  88. subvariables
  89. );
  90. use getpart;
  91. use globalconfig;
  92. use testutil qw(
  93. clearlogs
  94. logmsg
  95. runclient
  96. shell_quote
  97. subbase64
  98. subnewlines
  99. );
  100. use valgrind;
  101. #######################################################################
  102. # Global variables set elsewhere but used only by this package
  103. # These may only be set *before* runner_init is called
  104. our $DBGCURL=$CURL; #"../src/.libs/curl"; # alternative for debugging
  105. our $valgrind_logfile="--log-file"; # the option name for valgrind >=3
  106. our $valgrind_tool="--tool=memcheck";
  107. our $gdb = checktestcmd("gdb");
  108. our $gdbthis; # run test case with gdb debugger
  109. our $gdbxwin; # use windowed gdb when using gdb
  110. # torture test variables
  111. our $shallow;
  112. our $tortalloc;
  113. # local variables
  114. my %oldenv; # environment variables before test is started
  115. my $UNITDIR="./unit";
  116. my $CURLLOG="$LOGDIR/commands.log"; # all command lines run
  117. my $defserverlogslocktimeout = 2; # timeout to await server logs lock removal
  118. my $defpostcommanddelay = 0; # delay between command and postcheck sections
  119. my $multiprocess; # nonzero with a separate test runner process
  120. # pipes
  121. my $runnerr; # pipe that runner reads from
  122. my $runnerw; # pipe that runner writes to
  123. # per-runner variables, indexed by runner ID; these are used by controller only
  124. my %controllerr; # pipe that controller reads from
  125. my %controllerw; # pipe that controller writes to
  126. # redirected stdout/stderr to these files
  127. sub stdoutfilename {
  128. my ($logdir, $testnum)=@_;
  129. return "$logdir/stdout$testnum";
  130. }
  131. sub stderrfilename {
  132. my ($logdir, $testnum)=@_;
  133. return "$logdir/stderr$testnum";
  134. }
  135. #######################################################################
  136. # Initialize the runner and prepare it to run tests
  137. # The runner ID returned by this function must be passed into the other
  138. # runnerac_* functions
  139. # Called by controller
  140. sub runner_init {
  141. my ($logdir, $jobs)=@_;
  142. $multiprocess = !!$jobs;
  143. # enable memory debugging if curl is compiled with it
  144. $ENV{'CURL_MEMDEBUG'} = "$logdir/$MEMDUMP";
  145. $ENV{'CURL_ENTROPY'}="12345678";
  146. $ENV{'CURL_FORCETIME'}=1; # for debug NTLM magic
  147. $ENV{'CURL_GLOBAL_INIT'}=1; # debug curl_global_init/cleanup use
  148. $ENV{'HOME'}=$pwd;
  149. $ENV{'CURL_HOME'}=$ENV{'HOME'};
  150. $ENV{'XDG_CONFIG_HOME'}=$ENV{'HOME'};
  151. $ENV{'COLUMNS'}=79; # screen width!
  152. # Incorporate the $logdir into the random seed and re-seed the PRNG.
  153. # This gives each runner a unique yet consistent seed which provides
  154. # more unique port number selection in each runner, yet is deterministic
  155. # across runs.
  156. $randseed += unpack('%16C*', $logdir);
  157. srand $randseed;
  158. # create pipes for communication with runner
  159. my ($thisrunnerr, $thiscontrollerw, $thiscontrollerr, $thisrunnerw);
  160. pipe $thisrunnerr, $thiscontrollerw;
  161. pipe $thiscontrollerr, $thisrunnerw;
  162. my $thisrunnerid;
  163. if($multiprocess) {
  164. # Create a separate process in multiprocess mode
  165. my $child = fork();
  166. if(0 == $child) {
  167. # TODO: set up better signal handlers
  168. $SIG{INT} = 'IGNORE';
  169. $SIG{TERM} = 'IGNORE';
  170. eval {
  171. # some msys2 perl versions don't define SIGUSR1
  172. $SIG{USR1} = 'IGNORE';
  173. };
  174. $thisrunnerid = $$;
  175. print "Runner $thisrunnerid starting\n" if($verbose);
  176. # Here we are the child (runner).
  177. close($thiscontrollerw);
  178. close($thiscontrollerr);
  179. $runnerr = $thisrunnerr;
  180. $runnerw = $thisrunnerw;
  181. # Set this directory as ours
  182. $LOGDIR = $logdir;
  183. mkdir("$LOGDIR/$PIDDIR", 0777);
  184. mkdir("$LOGDIR/$LOCKDIR", 0777);
  185. # Initialize various server variables
  186. initserverconfig();
  187. # handle IPC calls
  188. event_loop();
  189. # Can't rely on logmsg here in case it's buffered
  190. print "Runner $thisrunnerid exiting\n" if($verbose);
  191. # To reach this point, either the controller has sent
  192. # runnerac_stopservers() and runnerac_shutdown() or we have called
  193. # runnerabort(). In both cases, there are no more of our servers
  194. # running and we can safely exit.
  195. exit 0;
  196. }
  197. # Here we are the parent (controller).
  198. close($thisrunnerw);
  199. close($thisrunnerr);
  200. $thisrunnerid = $child;
  201. } else {
  202. # Create our pid directory
  203. mkdir("$LOGDIR/$PIDDIR", 0777);
  204. # Don't create a separate process
  205. $thisrunnerid = "integrated";
  206. }
  207. $controllerw{$thisrunnerid} = $thiscontrollerw;
  208. $runnerr = $thisrunnerr;
  209. $runnerw = $thisrunnerw;
  210. $controllerr{$thisrunnerid} = $thiscontrollerr;
  211. return $thisrunnerid;
  212. }
  213. #######################################################################
  214. # Loop to execute incoming IPC calls until the shutdown call
  215. sub event_loop {
  216. while () {
  217. if(ipcrecv()) {
  218. last;
  219. }
  220. }
  221. }
  222. #######################################################################
  223. # Check for a command in the PATH of the machine running curl.
  224. #
  225. sub checktestcmd {
  226. my ($cmd)=@_;
  227. my @testpaths=("$LIBDIR/.libs", "$LIBDIR");
  228. return checkcmd($cmd, @testpaths);
  229. }
  230. # See if Valgrind should actually be used
  231. sub use_valgrind {
  232. if($valgrind) {
  233. my @valgrindoption = getpart("verify", "valgrind");
  234. if((!@valgrindoption) || ($valgrindoption[0] !~ /disable/)) {
  235. return 1;
  236. }
  237. }
  238. return 0;
  239. }
  240. # Massage the command result code into a useful form
  241. sub normalize_cmdres {
  242. my $cmdres = $_[0];
  243. my $signal_num = $cmdres & 127;
  244. my $dumped_core = $cmdres & 128;
  245. if(!$anyway && ($signal_num || $dumped_core)) {
  246. $cmdres = 1000;
  247. }
  248. else {
  249. $cmdres >>= 8;
  250. $cmdres = (2000 + $signal_num) if($signal_num && !$cmdres);
  251. }
  252. return ($cmdres, $dumped_core);
  253. }
  254. # 'prepro' processes the input array and replaces %-variables in the array
  255. # etc. Returns the processed version of the array
  256. sub prepro {
  257. my $testnum = shift;
  258. my (@entiretest) = @_;
  259. my $show = 1;
  260. my @out;
  261. my $data_crlf;
  262. my @pshow;
  263. my @altshow;
  264. my $plvl;
  265. my $line;
  266. for my $s (@entiretest) {
  267. my $f = $s;
  268. $line++;
  269. if($s =~ /^ *%if (.*)/) {
  270. my $cond = $1;
  271. my $rev = 0;
  272. if($cond =~ /^!(.*)/) {
  273. $cond = $1;
  274. $rev = 1;
  275. }
  276. $rev ^= $feature{$cond} ? 1 : 0;
  277. push @pshow, $show; # push the previous state
  278. $plvl++;
  279. if($show) {
  280. # only if this was showing before we can allow the alternative
  281. # to go showing as well
  282. push @altshow, $rev ^ 1; # push the reversed show state
  283. }
  284. else {
  285. push @altshow, 0; # the alt should still hide
  286. }
  287. if($show) {
  288. # we only allow show if already showing
  289. $show = $rev;
  290. }
  291. next;
  292. }
  293. elsif($s =~ /^ *%else/) {
  294. if(!$plvl) {
  295. print STDERR "error: test$testnum:$line: %else no %if\n";
  296. last;
  297. }
  298. $show = pop @altshow;
  299. push @altshow, $show; # put it back for consistency
  300. next;
  301. }
  302. elsif($s =~ /^ *%endif/) {
  303. if(!$plvl--) {
  304. print STDERR "error: test$testnum:$line: %endif had no %if\n";
  305. last;
  306. }
  307. $show = pop @pshow;
  308. pop @altshow; # not used here but we must pop it
  309. next;
  310. }
  311. if($show) {
  312. # The processor does CRLF replacements in the <data*> sections if
  313. # necessary since those parts might be read by separate servers.
  314. if($s =~ /^ *<data(.*)\>/) {
  315. if($1 =~ /crlf="yes"/ ||
  316. ($feature{"hyper"} && ($keywords{"HTTP"} || $keywords{"HTTPS"}))) {
  317. $data_crlf = 1;
  318. }
  319. }
  320. elsif(($s =~ /^ *<\/data/) && $data_crlf) {
  321. $data_crlf = 0;
  322. }
  323. subvariables(\$s, $testnum, "%");
  324. subbase64(\$s);
  325. subnewlines(0, \$s) if($data_crlf);
  326. push @out, $s;
  327. }
  328. }
  329. return @out;
  330. }
  331. #######################################################################
  332. # Load test keywords into %keywords hash
  333. #
  334. sub readtestkeywords {
  335. my @info_keywords = getpart("info", "keywords");
  336. # Clear the list of keywords from the last test
  337. %keywords = ();
  338. for my $k (@info_keywords) {
  339. chomp $k;
  340. $keywords{$k} = 1;
  341. }
  342. }
  343. #######################################################################
  344. # Return a list of log locks that still exist
  345. #
  346. sub logslocked {
  347. opendir(my $lockdir, "$LOGDIR/$LOCKDIR");
  348. my @locks;
  349. foreach (readdir $lockdir) {
  350. if(/^(.*)\.lock$/) {
  351. push @locks, $1;
  352. }
  353. }
  354. return @locks;
  355. }
  356. #######################################################################
  357. # Memory allocation test and failure torture testing.
  358. #
  359. sub torture {
  360. my ($testcmd, $testnum, $gdbline) = @_;
  361. # remove memdump first to be sure we get a new nice and clean one
  362. unlink("$LOGDIR/$MEMDUMP");
  363. # First get URL from test server, ignore the output/result
  364. runclient($testcmd);
  365. logmsg " CMD: $testcmd\n" if($verbose);
  366. # memanalyze -v is our friend, get the number of allocations made
  367. my $count=0;
  368. my @out = `$memanalyze -v "$LOGDIR/$MEMDUMP"`;
  369. for(@out) {
  370. if(/^Operations: (\d+)/) {
  371. $count = $1;
  372. last;
  373. }
  374. }
  375. if(!$count) {
  376. logmsg " found no functions to make fail\n";
  377. return 0;
  378. }
  379. my @ttests = (1 .. $count);
  380. if($shallow && ($shallow < $count)) {
  381. my $discard = scalar(@ttests) - $shallow;
  382. my $percent = sprintf("%.2f%%", $shallow * 100 / scalar(@ttests));
  383. logmsg " $count functions found, but only fail $shallow ($percent)\n";
  384. while($discard) {
  385. my $rm;
  386. do {
  387. # find a test to discard
  388. $rm = rand(scalar(@ttests));
  389. } while(!$ttests[$rm]);
  390. $ttests[$rm] = undef;
  391. $discard--;
  392. }
  393. }
  394. else {
  395. logmsg " $count functions to make fail\n";
  396. }
  397. for (@ttests) {
  398. my $limit = $_;
  399. my $fail;
  400. my $dumped_core;
  401. if(!defined($limit)) {
  402. # --shallow can undefine them
  403. next;
  404. }
  405. if($tortalloc && ($tortalloc != $limit)) {
  406. next;
  407. }
  408. if($verbose) {
  409. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
  410. localtime(time());
  411. my $now = sprintf("%02d:%02d:%02d ", $hour, $min, $sec);
  412. logmsg "Fail function no: $limit at $now\r";
  413. }
  414. # make the memory allocation function number $limit return failure
  415. $ENV{'CURL_MEMLIMIT'} = $limit;
  416. # remove memdump first to be sure we get a new nice and clean one
  417. unlink("$LOGDIR/$MEMDUMP");
  418. my $cmd = $testcmd;
  419. if($valgrind && !$gdbthis) {
  420. my @valgrindoption = getpart("verify", "valgrind");
  421. if((!@valgrindoption) || ($valgrindoption[0] !~ /disable/)) {
  422. my $valgrindcmd = "$valgrind ";
  423. $valgrindcmd .= "$valgrind_tool " if($valgrind_tool);
  424. $valgrindcmd .= "--quiet --leak-check=yes ";
  425. $valgrindcmd .= "--suppressions=$srcdir/valgrind.supp ";
  426. # $valgrindcmd .= "--gen-suppressions=all ";
  427. $valgrindcmd .= "--num-callers=16 ";
  428. $valgrindcmd .= "${valgrind_logfile}=$LOGDIR/valgrind$testnum";
  429. $cmd = "$valgrindcmd $testcmd";
  430. }
  431. }
  432. logmsg "*** Function number $limit is now set to fail ***\n" if($gdbthis);
  433. my $ret = 0;
  434. if($gdbthis) {
  435. runclient($gdbline);
  436. }
  437. else {
  438. $ret = runclient($cmd);
  439. }
  440. #logmsg "$_ Returned " . ($ret >> 8) . "\n";
  441. # Now clear the variable again
  442. delete $ENV{'CURL_MEMLIMIT'} if($ENV{'CURL_MEMLIMIT'});
  443. if(-r "core") {
  444. # there's core file present now!
  445. logmsg " core dumped\n";
  446. $dumped_core = 1;
  447. $fail = 2;
  448. }
  449. if($valgrind) {
  450. my @e = valgrindparse("$LOGDIR/valgrind$testnum");
  451. if(@e && $e[0]) {
  452. if($automakestyle) {
  453. logmsg "FAIL: torture $testnum - valgrind\n";
  454. }
  455. else {
  456. logmsg " valgrind ERROR ";
  457. logmsg @e;
  458. }
  459. $fail = 1;
  460. }
  461. }
  462. # verify that it returns a proper error code, doesn't leak memory
  463. # and doesn't core dump
  464. if(($ret & 255) || ($ret >> 8) >= 128) {
  465. logmsg " system() returned $ret\n";
  466. $fail=1;
  467. }
  468. else {
  469. my @memdata=`$memanalyze "$LOGDIR/$MEMDUMP"`;
  470. my $leak=0;
  471. for(@memdata) {
  472. if($_ ne "") {
  473. # well it could be other memory problems as well, but
  474. # we call it leak for short here
  475. $leak=1;
  476. }
  477. }
  478. if($leak) {
  479. logmsg "** MEMORY FAILURE\n";
  480. logmsg @memdata;
  481. logmsg `$memanalyze -l "$LOGDIR/$MEMDUMP"`;
  482. $fail = 1;
  483. }
  484. }
  485. if($fail) {
  486. logmsg " $testnum: torture FAILED: function number $limit in test.\n",
  487. " invoke with \"-t$limit\" to repeat this single case.\n";
  488. stopservers($verbose);
  489. return 1;
  490. }
  491. }
  492. logmsg "\n" if($verbose);
  493. logmsg "torture OK\n";
  494. return 0;
  495. }
  496. #######################################################################
  497. # restore environment variables that were modified in test
  498. sub restore_test_env {
  499. my $deleteoldenv = $_[0]; # 1 to delete the saved contents after restore
  500. foreach my $var (keys %oldenv) {
  501. if($oldenv{$var} eq 'notset') {
  502. delete $ENV{$var} if($ENV{$var});
  503. }
  504. else {
  505. $ENV{$var} = $oldenv{$var};
  506. }
  507. if($deleteoldenv) {
  508. delete $oldenv{$var};
  509. }
  510. }
  511. }
  512. #######################################################################
  513. # Start the servers needed to run this test case
  514. sub singletest_startservers {
  515. my ($testnum, $testtimings) = @_;
  516. # remove old test server files before servers are started/verified
  517. unlink("$LOGDIR/$SERVERCMD");
  518. unlink("$LOGDIR/$SERVERIN");
  519. unlink("$LOGDIR/$PROXYIN");
  520. # timestamp required servers verification start
  521. $$testtimings{"timesrvrini"} = Time::HiRes::time();
  522. my $why;
  523. my $error;
  524. if (!$listonly) {
  525. my @what = getpart("client", "server");
  526. if(!$what[0]) {
  527. warn "Test case $testnum has no server(s) specified";
  528. $why = "no server specified";
  529. $error = -1;
  530. } else {
  531. my $err;
  532. ($why, $err) = serverfortest(@what);
  533. if($err == 1) {
  534. # Error indicates an actual problem starting the server
  535. $error = -2;
  536. } else {
  537. $error = -1;
  538. }
  539. }
  540. }
  541. # timestamp required servers verification end
  542. $$testtimings{"timesrvrend"} = Time::HiRes::time();
  543. return ($why, $error);
  544. }
  545. #######################################################################
  546. # Generate preprocessed test file
  547. sub singletest_preprocess {
  548. my $testnum = $_[0];
  549. # Save a preprocessed version of the entire test file. This allows more
  550. # "basic" test case readers to enjoy variable replacements.
  551. my @entiretest = fulltest();
  552. my $otest = "$LOGDIR/test$testnum";
  553. @entiretest = prepro($testnum, @entiretest);
  554. # save the new version
  555. open(my $fulltesth, ">", "$otest") || die "Failure writing test file";
  556. foreach my $bytes (@entiretest) {
  557. print $fulltesth pack('a*', $bytes) or die "Failed to print '$bytes': $!";
  558. }
  559. close($fulltesth) || die "Failure writing test file";
  560. # in case the process changed the file, reload it
  561. loadtest("$LOGDIR/test${testnum}");
  562. }
  563. #######################################################################
  564. # Set up the test environment to run this test case
  565. sub singletest_setenv {
  566. my @setenv = getpart("client", "setenv");
  567. foreach my $s (@setenv) {
  568. chomp $s;
  569. if($s =~ /([^=]*)=(.*)/) {
  570. my ($var, $content) = ($1, $2);
  571. # remember current setting, to restore it once test runs
  572. $oldenv{$var} = ($ENV{$var})?"$ENV{$var}":'notset';
  573. # set new value
  574. if(!$content) {
  575. delete $ENV{$var} if($ENV{$var});
  576. }
  577. else {
  578. if($var =~ /^LD_PRELOAD/) {
  579. if(exe_ext('TOOL') && (exe_ext('TOOL') eq '.exe')) {
  580. logmsg "Skipping LD_PRELOAD due to lack of OS support\n" if($verbose);
  581. next;
  582. }
  583. if($feature{"debug"} || !$has_shared) {
  584. logmsg "Skipping LD_PRELOAD due to no release shared build\n" if($verbose);
  585. next;
  586. }
  587. }
  588. $ENV{$var} = "$content";
  589. logmsg "setenv $var = $content\n" if($verbose);
  590. }
  591. }
  592. }
  593. if($proxy_address) {
  594. $ENV{http_proxy} = $proxy_address;
  595. $ENV{HTTPS_PROXY} = $proxy_address;
  596. }
  597. }
  598. #######################################################################
  599. # Check that test environment is fine to run this test case
  600. sub singletest_precheck {
  601. my $testnum = $_[0];
  602. my $why;
  603. my @precheck = getpart("client", "precheck");
  604. if(@precheck) {
  605. my $cmd = $precheck[0];
  606. chomp $cmd;
  607. if($cmd) {
  608. my @p = split(/ /, $cmd);
  609. if($p[0] !~ /\//) {
  610. # the first word, the command, does not contain a slash so
  611. # we will scan the "improved" PATH to find the command to
  612. # be able to run it
  613. my $fullp = checktestcmd($p[0]);
  614. if($fullp) {
  615. $p[0] = $fullp;
  616. }
  617. $cmd = join(" ", @p);
  618. }
  619. my @o = `$cmd 2> $LOGDIR/precheck-$testnum`;
  620. if($o[0]) {
  621. $why = $o[0];
  622. $why =~ s/[\r\n]//g;
  623. }
  624. elsif($?) {
  625. $why = "precheck command error";
  626. }
  627. logmsg "prechecked $cmd\n" if($verbose);
  628. }
  629. }
  630. return $why;
  631. }
  632. #######################################################################
  633. # Prepare the test environment to run this test case
  634. sub singletest_prepare {
  635. my ($testnum) = @_;
  636. if($feature{"TrackMemory"}) {
  637. unlink("$LOGDIR/$MEMDUMP");
  638. }
  639. unlink("core");
  640. # remove server output logfiles after servers are started/verified
  641. unlink("$LOGDIR/$SERVERIN");
  642. unlink("$LOGDIR/$PROXYIN");
  643. # if this section exists, it might be FTP server instructions:
  644. my @ftpservercmd = getpart("reply", "servercmd");
  645. push @ftpservercmd, "Testnum $testnum\n";
  646. # write the instructions to file
  647. writearray("$LOGDIR/$SERVERCMD", \@ftpservercmd);
  648. # create (possibly-empty) files before starting the test
  649. for my $partsuffix (('', '1', '2', '3', '4')) {
  650. my @inputfile=getpart("client", "file".$partsuffix);
  651. my %fileattr = getpartattr("client", "file".$partsuffix);
  652. my $filename=$fileattr{'name'};
  653. if(@inputfile || $filename) {
  654. if(!$filename) {
  655. logmsg " $testnum: IGNORED: section client=>file has no name attribute\n";
  656. return -1;
  657. }
  658. my $fileContent = join('', @inputfile);
  659. # make directories if needed
  660. my $path = $filename;
  661. # cut off the file name part
  662. $path =~ s/^(.*)\/[^\/]*/$1/;
  663. my $nparts = scalar(split(/\//, $LOGDIR));
  664. my @parts = split(/\//, $path);
  665. if(join("/", @parts[0..$nparts-1]) eq $LOGDIR) {
  666. # the file is in $LOGDIR/
  667. my $d = shift @parts;
  668. for(@parts) {
  669. $d .= "/$_";
  670. mkdir $d; # 0777
  671. }
  672. }
  673. if (open(my $outfile, ">", "$filename")) {
  674. binmode $outfile; # for crapage systems, use binary
  675. if($fileattr{'nonewline'}) {
  676. # cut off the final newline
  677. chomp($fileContent);
  678. }
  679. print $outfile $fileContent;
  680. close($outfile);
  681. } else {
  682. logmsg "ERROR: cannot write $filename\n";
  683. }
  684. }
  685. }
  686. return 0;
  687. }
  688. #######################################################################
  689. # Run the test command
  690. sub singletest_run {
  691. my ($testnum, $testtimings) = @_;
  692. # get the command line options to use
  693. my ($cmd, @blaha)= getpart("client", "command");
  694. if($cmd) {
  695. # make some nice replace operations
  696. $cmd =~ s/\n//g; # no newlines please
  697. # substitute variables in the command line
  698. }
  699. else {
  700. # there was no command given, use something silly
  701. $cmd="-";
  702. }
  703. my $CURLOUT="$LOGDIR/curl$testnum.out"; # curl output if not stdout
  704. # if stdout section exists, we verify that the stdout contained this:
  705. my $out="";
  706. my %cmdhash = getpartattr("client", "command");
  707. if((!$cmdhash{'option'}) || ($cmdhash{'option'} !~ /no-output/)) {
  708. #We may slap on --output!
  709. if (!partexists("verify", "stdout") ||
  710. ($cmdhash{'option'} && $cmdhash{'option'} =~ /force-output/)) {
  711. $out=" --output $CURLOUT ";
  712. }
  713. }
  714. my @codepieces = getpart("client", "tool");
  715. my $tool="";
  716. if(@codepieces) {
  717. $tool = $codepieces[0];
  718. chomp $tool;
  719. $tool .= exe_ext('TOOL');
  720. }
  721. my $disablevalgrind;
  722. my $CMDLINE="";
  723. my $cmdargs;
  724. my $cmdtype = $cmdhash{'type'} || "default";
  725. my $fail_due_event_based = $run_event_based;
  726. if($cmdtype eq "perl") {
  727. # run the command line prepended with "perl"
  728. $cmdargs ="$cmd";
  729. $CMDLINE = "$perl ";
  730. $tool=$CMDLINE;
  731. $disablevalgrind=1;
  732. }
  733. elsif($cmdtype eq "shell") {
  734. # run the command line prepended with "/bin/sh"
  735. $cmdargs ="$cmd";
  736. $CMDLINE = "/bin/sh ";
  737. $tool=$CMDLINE;
  738. $disablevalgrind=1;
  739. }
  740. elsif(!$tool && !$keywords{"unittest"}) {
  741. # run curl, add suitable command line options
  742. my $inc="";
  743. if((!$cmdhash{'option'}) || ($cmdhash{'option'} !~ /no-include/)) {
  744. $inc = " --include";
  745. }
  746. $cmdargs = "$out$inc ";
  747. if($cmdhash{'option'} && ($cmdhash{'option'} =~ /binary-trace/)) {
  748. $cmdargs .= "--trace $LOGDIR/trace$testnum ";
  749. }
  750. else {
  751. $cmdargs .= "--trace-ascii $LOGDIR/trace$testnum ";
  752. }
  753. $cmdargs .= "--trace-time ";
  754. if($run_event_based) {
  755. $cmdargs .= "--test-event ";
  756. $fail_due_event_based--;
  757. }
  758. $cmdargs .= $cmd;
  759. if ($proxy_address) {
  760. $cmdargs .= " --proxy $proxy_address ";
  761. }
  762. }
  763. else {
  764. $cmdargs = " $cmd"; # $cmd is the command line for the test file
  765. $CURLOUT = stdoutfilename($LOGDIR, $testnum); # sends received data to stdout
  766. # Default the tool to a unit test with the same name as the test spec
  767. if($keywords{"unittest"} && !$tool) {
  768. $tool="unit$testnum";
  769. }
  770. if($tool =~ /^lib/) {
  771. $CMDLINE="$LIBDIR/$tool";
  772. }
  773. elsif($tool =~ /^unit/) {
  774. $CMDLINE="$UNITDIR/$tool";
  775. }
  776. if(! -f $CMDLINE) {
  777. logmsg " $testnum: IGNORED: The tool set in the test case for this: '$tool' does not exist\n";
  778. return (-1, 0, 0, "", "", 0);
  779. }
  780. $DBGCURL=$CMDLINE;
  781. }
  782. if($fail_due_event_based) {
  783. logmsg " $testnum: IGNORED: This test cannot run event based\n";
  784. return (-1, 0, 0, "", "", 0);
  785. }
  786. if($gdbthis) {
  787. # gdb is incompatible with valgrind, so disable it when debugging
  788. # Perhaps a better approach would be to run it under valgrind anyway
  789. # with --db-attach=yes or --vgdb=yes.
  790. $disablevalgrind=1;
  791. }
  792. my @stdintest = getpart("client", "stdin");
  793. if(@stdintest) {
  794. my $stdinfile="$LOGDIR/stdin-for-$testnum";
  795. my %hash = getpartattr("client", "stdin");
  796. if($hash{'nonewline'}) {
  797. # cut off the final newline from the final line of the stdin data
  798. chomp($stdintest[-1]);
  799. }
  800. writearray($stdinfile, \@stdintest);
  801. $cmdargs .= " <$stdinfile";
  802. }
  803. if(!$tool) {
  804. $CMDLINE=shell_quote($CURL);
  805. }
  806. if(use_valgrind() && !$disablevalgrind) {
  807. my $valgrindcmd = "$valgrind ";
  808. $valgrindcmd .= "$valgrind_tool " if($valgrind_tool);
  809. $valgrindcmd .= "--quiet --leak-check=yes ";
  810. $valgrindcmd .= "--suppressions=$srcdir/valgrind.supp ";
  811. # $valgrindcmd .= "--gen-suppressions=all ";
  812. $valgrindcmd .= "--num-callers=16 ";
  813. $valgrindcmd .= "${valgrind_logfile}=$LOGDIR/valgrind$testnum";
  814. $CMDLINE = "$valgrindcmd $CMDLINE";
  815. }
  816. $CMDLINE .= "$cmdargs > " . stdoutfilename($LOGDIR, $testnum) .
  817. " 2> " . stderrfilename($LOGDIR, $testnum);
  818. if($verbose) {
  819. logmsg "$CMDLINE\n";
  820. }
  821. open(my $cmdlog, ">", $CURLLOG) || die "Failure writing log file";
  822. print $cmdlog "$CMDLINE\n";
  823. close($cmdlog) || die "Failure writing log file";
  824. my $dumped_core;
  825. my $cmdres;
  826. if($gdbthis) {
  827. my $gdbinit = "$TESTDIR/gdbinit$testnum";
  828. open(my $gdbcmd, ">", "$LOGDIR/gdbcmd") || die "Failure writing gdb file";
  829. print $gdbcmd "set args $cmdargs\n";
  830. print $gdbcmd "show args\n";
  831. print $gdbcmd "source $gdbinit\n" if -e $gdbinit;
  832. close($gdbcmd) || die "Failure writing gdb file";
  833. }
  834. # Flush output.
  835. $| = 1;
  836. # timestamp starting of test command
  837. $$testtimings{"timetoolini"} = Time::HiRes::time();
  838. # run the command line we built
  839. if ($torture) {
  840. $cmdres = torture($CMDLINE,
  841. $testnum,
  842. "$gdb --directory $LIBDIR " . shell_quote($DBGCURL) . " -x $LOGDIR/gdbcmd");
  843. }
  844. elsif($gdbthis) {
  845. my $GDBW = ($gdbxwin) ? "-w" : "";
  846. runclient("$gdb --directory $LIBDIR " . shell_quote($DBGCURL) . " $GDBW -x $LOGDIR/gdbcmd");
  847. $cmdres=0; # makes it always continue after a debugged run
  848. }
  849. else {
  850. # Convert the raw result code into a more useful one
  851. ($cmdres, $dumped_core) = normalize_cmdres(runclient("$CMDLINE"));
  852. }
  853. # timestamp finishing of test command
  854. $$testtimings{"timetoolend"} = Time::HiRes::time();
  855. return (0, $cmdres, $dumped_core, $CURLOUT, $tool, use_valgrind() && !$disablevalgrind);
  856. }
  857. #######################################################################
  858. # Clean up after test command
  859. sub singletest_clean {
  860. my ($testnum, $dumped_core, $testtimings)=@_;
  861. if(!$dumped_core) {
  862. if(-r "core") {
  863. # there's core file present now!
  864. $dumped_core = 1;
  865. }
  866. }
  867. if($dumped_core) {
  868. logmsg "core dumped\n";
  869. if(0 && $gdb) {
  870. logmsg "running gdb for post-mortem analysis:\n";
  871. open(my $gdbcmd, ">", "$LOGDIR/gdbcmd2") || die "Failure writing gdb file";
  872. print $gdbcmd "bt\n";
  873. close($gdbcmd) || die "Failure writing gdb file";
  874. runclient("$gdb --directory libtest -x $LOGDIR/gdbcmd2 -batch " . shell_quote($DBGCURL) . " core ");
  875. # unlink("$LOGDIR/gdbcmd2");
  876. }
  877. }
  878. # If a server logs advisor read lock file exists, it is an indication
  879. # that the server has not yet finished writing out all its log files,
  880. # including server request log files used for protocol verification.
  881. # So, if the lock file exists the script waits here a certain amount
  882. # of time until the server removes it, or the given time expires.
  883. my $serverlogslocktimeout = $defserverlogslocktimeout;
  884. my %cmdhash = getpartattr("client", "command");
  885. if($cmdhash{'timeout'}) {
  886. # test is allowed to override default server logs lock timeout
  887. if($cmdhash{'timeout'} =~ /(\d+)/) {
  888. $serverlogslocktimeout = $1 if($1 >= 0);
  889. }
  890. }
  891. if($serverlogslocktimeout) {
  892. my $lockretry = $serverlogslocktimeout * 20;
  893. my @locks;
  894. while((@locks = logslocked()) && $lockretry--) {
  895. portable_sleep(0.05);
  896. }
  897. if(($lockretry < 0) &&
  898. ($serverlogslocktimeout >= $defserverlogslocktimeout)) {
  899. logmsg "Warning: server logs lock timeout ",
  900. "($serverlogslocktimeout seconds) expired (locks: " .
  901. join(", ", @locks) . ")\n";
  902. }
  903. }
  904. # Test harness ssh server does not have this synchronization mechanism,
  905. # this implies that some ssh server based tests might need a small delay
  906. # once that the client command has run to avoid false test failures.
  907. #
  908. # gnutls-serv also lacks this synchronization mechanism, so gnutls-serv
  909. # based tests might need a small delay once that the client command has
  910. # run to avoid false test failures.
  911. my $postcommanddelay = $defpostcommanddelay;
  912. if($cmdhash{'delay'}) {
  913. # test is allowed to specify a delay after command is executed
  914. if($cmdhash{'delay'} =~ /(\d+)/) {
  915. $postcommanddelay = $1 if($1 > 0);
  916. }
  917. }
  918. portable_sleep($postcommanddelay) if($postcommanddelay);
  919. # timestamp removal of server logs advisor read lock
  920. $$testtimings{"timesrvrlog"} = Time::HiRes::time();
  921. # test definition might instruct to stop some servers
  922. # stop also all servers relative to the given one
  923. my @killtestservers = getpart("client", "killserver");
  924. if(@killtestservers) {
  925. foreach my $server (@killtestservers) {
  926. chomp $server;
  927. if(stopserver($server)) {
  928. logmsg " $testnum: killserver FAILED\n";
  929. return 1; # normal error if asked to fail on unexpected alive
  930. }
  931. }
  932. }
  933. return 0;
  934. }
  935. #######################################################################
  936. # Verify that the postcheck succeeded
  937. sub singletest_postcheck {
  938. my ($testnum)=@_;
  939. # run the postcheck command
  940. my @postcheck= getpart("client", "postcheck");
  941. if(@postcheck) {
  942. my $cmd = join("", @postcheck);
  943. chomp $cmd;
  944. if($cmd) {
  945. logmsg "postcheck $cmd\n" if($verbose);
  946. my $rc = runclient("$cmd");
  947. # Must run the postcheck command in torture mode in order
  948. # to clean up, but the result can't be relied upon.
  949. if($rc != 0 && !$torture) {
  950. logmsg " $testnum: postcheck FAILED\n";
  951. return -1;
  952. }
  953. }
  954. }
  955. return 0;
  956. }
  957. ###################################################################
  958. # Get ready to run a single test case
  959. sub runner_test_preprocess {
  960. my ($testnum)=@_;
  961. my %testtimings;
  962. if(clearlogs()) {
  963. logmsg "Warning: log messages were lost\n";
  964. }
  965. # timestamp test preparation start
  966. # TODO: this metric now shows only a portion of the prep time; better would
  967. # be to time singletest_preprocess below instead
  968. $testtimings{"timeprepini"} = Time::HiRes::time();
  969. ###################################################################
  970. # Load test metadata
  971. # ignore any error here--if there were one, it would have been
  972. # caught during the selection phase and this test would not be
  973. # running now
  974. loadtest("${TESTDIR}/test${testnum}");
  975. readtestkeywords();
  976. ###################################################################
  977. # Restore environment variables that were modified in a previous run.
  978. # Test definition may instruct to (un)set environment vars.
  979. restore_test_env(1);
  980. ###################################################################
  981. # Start the servers needed to run this test case
  982. my ($why, $error) = singletest_startservers($testnum, \%testtimings);
  983. if(!$why) {
  984. ###############################################################
  985. # Generate preprocessed test file
  986. # This must be done after the servers are started so server
  987. # variables are available for substitution.
  988. singletest_preprocess($testnum);
  989. ###############################################################
  990. # Set up the test environment to run this test case
  991. singletest_setenv();
  992. ###############################################################
  993. # Check that the test environment is fine to run this test case
  994. if (!$listonly) {
  995. $why = singletest_precheck($testnum);
  996. $error = -1;
  997. }
  998. }
  999. return ($why, $error, clearlogs(), \%testtimings);
  1000. }
  1001. ###################################################################
  1002. # Run a single test case with an environment that already been prepared
  1003. # Returns 0=success, -1=skippable failure, -2=permanent error,
  1004. # 1=unskippable test failure, as first integer, plus any log messages,
  1005. # plus more return values when error is 0
  1006. sub runner_test_run {
  1007. my ($testnum)=@_;
  1008. if(clearlogs()) {
  1009. logmsg "Warning: log messages were lost\n";
  1010. }
  1011. #######################################################################
  1012. # Prepare the test environment to run this test case
  1013. my $error = singletest_prepare($testnum);
  1014. if($error) {
  1015. return (-2, clearlogs());
  1016. }
  1017. #######################################################################
  1018. # Run the test command
  1019. my %testtimings;
  1020. my $cmdres;
  1021. my $dumped_core;
  1022. my $CURLOUT;
  1023. my $tool;
  1024. my $usedvalgrind;
  1025. ($error, $cmdres, $dumped_core, $CURLOUT, $tool, $usedvalgrind) = singletest_run($testnum, \%testtimings);
  1026. if($error) {
  1027. return (-2, clearlogs(), \%testtimings);
  1028. }
  1029. #######################################################################
  1030. # Clean up after test command
  1031. $error = singletest_clean($testnum, $dumped_core, \%testtimings);
  1032. if($error) {
  1033. return ($error, clearlogs(), \%testtimings);
  1034. }
  1035. #######################################################################
  1036. # Verify that the postcheck succeeded
  1037. $error = singletest_postcheck($testnum);
  1038. if($error) {
  1039. return ($error, clearlogs(), \%testtimings);
  1040. }
  1041. #######################################################################
  1042. # restore environment variables that were modified
  1043. restore_test_env(0);
  1044. return (0, clearlogs(), \%testtimings, $cmdres, $CURLOUT, $tool, $usedvalgrind);
  1045. }
  1046. # Async call runner_clearlocks
  1047. # Called by controller
  1048. sub runnerac_clearlocks {
  1049. return controlleripccall(\&runner_clearlocks, @_);
  1050. }
  1051. # Async call runner_shutdown
  1052. # This call does NOT generate an IPC response and must be the last IPC call
  1053. # received.
  1054. # Called by controller
  1055. sub runnerac_shutdown {
  1056. my ($runnerid)=$_[0];
  1057. my $err = controlleripccall(\&runner_shutdown, @_);
  1058. # These have no more use
  1059. close($controllerw{$runnerid});
  1060. undef $controllerw{$runnerid};
  1061. close($controllerr{$runnerid});
  1062. undef $controllerr{$runnerid};
  1063. return $err;
  1064. }
  1065. # Async call of runner_stopservers
  1066. # Called by controller
  1067. sub runnerac_stopservers {
  1068. return controlleripccall(\&runner_stopservers, @_);
  1069. }
  1070. # Async call of runner_test_preprocess
  1071. # Called by controller
  1072. sub runnerac_test_preprocess {
  1073. return controlleripccall(\&runner_test_preprocess, @_);
  1074. }
  1075. # Async call of runner_test_run
  1076. # Called by controller
  1077. sub runnerac_test_run {
  1078. return controlleripccall(\&runner_test_run, @_);
  1079. }
  1080. ###################################################################
  1081. # Call an arbitrary function via IPC
  1082. # The first argument is the function reference, the second is the runner ID
  1083. # Returns 0 on success, -1 on error writing to runner
  1084. # Called by controller (indirectly, via a more specific function)
  1085. sub controlleripccall {
  1086. my $funcref = shift @_;
  1087. my $runnerid = shift @_;
  1088. # Get the name of the function from the reference
  1089. my $cv = svref_2object($funcref);
  1090. my $gv = $cv->GV;
  1091. # Prepend the name to the function arguments so it's marshalled along with them
  1092. unshift @_, $gv->NAME;
  1093. # Marshall the arguments into a flat string
  1094. my $margs = freeze \@_;
  1095. # Send IPC call via pipe
  1096. my $err;
  1097. while(! defined ($err = syswrite($controllerw{$runnerid}, (pack "L", length($margs)) . $margs)) || $err <= 0) {
  1098. if((!defined $err && ! $!{EINTR}) || (defined $err && $err == 0)) {
  1099. # Runner has likely died
  1100. return -1;
  1101. }
  1102. # system call was interrupted, probably by ^C; restart it so we stay in sync
  1103. }
  1104. if(!$multiprocess) {
  1105. # Call the remote function here in single process mode
  1106. ipcrecv();
  1107. }
  1108. return 0;
  1109. }
  1110. ###################################################################
  1111. # Receive async response of a previous call via IPC
  1112. # The first return value is the runner ID or undef on error
  1113. # Called by controller
  1114. sub runnerar {
  1115. my ($runnerid) = @_;
  1116. my $err;
  1117. my $datalen;
  1118. while(! defined ($err = sysread($controllerr{$runnerid}, $datalen, 4)) || $err <= 0) {
  1119. if((!defined $err && ! $!{EINTR}) || (defined $err && $err == 0)) {
  1120. # Runner is likely dead and closed the pipe
  1121. return undef;
  1122. }
  1123. # system call was interrupted, probably by ^C; restart it so we stay in sync
  1124. }
  1125. my $len=unpack("L", $datalen);
  1126. my $buf;
  1127. while(! defined ($err = sysread($controllerr{$runnerid}, $buf, $len)) || $err <= 0) {
  1128. if((!defined $err && ! $!{EINTR}) || (defined $err && $err == 0)) {
  1129. # Runner is likely dead and closed the pipe
  1130. return undef;
  1131. }
  1132. # system call was interrupted, probably by ^C; restart it so we stay in sync
  1133. }
  1134. # Decode response values
  1135. my $resarrayref = thaw $buf;
  1136. # First argument is runner ID
  1137. # TODO: remove this; it's unneeded since it's passed in
  1138. unshift @$resarrayref, $runnerid;
  1139. return @$resarrayref;
  1140. }
  1141. ###################################################################
  1142. # Returns runner ID if a response from an async call is ready or error
  1143. # First value is ready, second is error, however an error case shows up
  1144. # as ready in Linux, so you can't trust it.
  1145. # argument is 0 for nonblocking, undef for blocking, anything else for timeout
  1146. # Called by controller
  1147. sub runnerar_ready {
  1148. my ($blocking) = @_;
  1149. my $rin = "";
  1150. my %idbyfileno;
  1151. my $maxfileno=0;
  1152. foreach my $p (keys(%controllerr)) {
  1153. my $fd = fileno($controllerr{$p});
  1154. vec($rin, $fd, 1) = 1;
  1155. $idbyfileno{$fd} = $p; # save the runner ID for each pipe fd
  1156. if($fd > $maxfileno) {
  1157. $maxfileno = $fd;
  1158. }
  1159. }
  1160. $maxfileno || die "Internal error: no runners are available to wait on\n";
  1161. # Wait for any pipe from any runner to be ready
  1162. # This may be interrupted and return EINTR, but this is ignored and the
  1163. # caller will need to later call this function again.
  1164. # TODO: this is relatively slow with hundreds of fds
  1165. my $ein = $rin;
  1166. if(select(my $rout=$rin, undef, my $eout=$ein, $blocking) >= 1) {
  1167. for my $fd (0..$maxfileno) {
  1168. # Return an error condition first in case it's both
  1169. if(vec($eout, $fd, 1)) {
  1170. return (undef, $idbyfileno{$fd});
  1171. }
  1172. if(vec($rout, $fd, 1)) {
  1173. return ($idbyfileno{$fd}, undef);
  1174. }
  1175. }
  1176. die "Internal pipe readiness inconsistency\n";
  1177. }
  1178. return (undef, undef);
  1179. }
  1180. ###################################################################
  1181. # Cleanly abort and exit the runner
  1182. # This uses print since there is no longer any controller to write logs.
  1183. sub runnerabort{
  1184. print "Controller is gone: runner $$ for $LOGDIR exiting\n";
  1185. my ($error, $logs) = runner_stopservers();
  1186. print $logs;
  1187. runner_shutdown();
  1188. }
  1189. ###################################################################
  1190. # Receive an IPC call in the runner and execute it
  1191. # The IPC is read from the $runnerr pipe and the response is
  1192. # written to the $runnerw pipe
  1193. # Returns 0 if more IPC calls are expected or 1 if the runner should exit
  1194. sub ipcrecv {
  1195. my $err;
  1196. my $datalen;
  1197. while(! defined ($err = sysread($runnerr, $datalen, 4)) || $err <= 0) {
  1198. if((!defined $err && ! $!{EINTR}) || (defined $err && $err == 0)) {
  1199. # pipe has closed; controller is gone and we must exit
  1200. runnerabort();
  1201. # Special case: no response will be forthcoming
  1202. return 1;
  1203. }
  1204. # system call was interrupted, probably by ^C; restart it so we stay in sync
  1205. }
  1206. my $len=unpack("L", $datalen);
  1207. my $buf;
  1208. while(! defined ($err = sysread($runnerr, $buf, $len)) || $err <= 0) {
  1209. if((!defined $err && ! $!{EINTR}) || (defined $err && $err == 0)) {
  1210. # pipe has closed; controller is gone and we must exit
  1211. runnerabort();
  1212. # Special case: no response will be forthcoming
  1213. return 1;
  1214. }
  1215. # system call was interrupted, probably by ^C; restart it so we stay in sync
  1216. }
  1217. # Decode the function name and arguments
  1218. my $argsarrayref = thaw $buf;
  1219. # The name of the function to call is the first argument
  1220. my $funcname = shift @$argsarrayref;
  1221. # print "ipcrecv $funcname\n";
  1222. # Synchronously call the desired function
  1223. my @res;
  1224. if($funcname eq "runner_clearlocks") {
  1225. @res = runner_clearlocks(@$argsarrayref);
  1226. }
  1227. elsif($funcname eq "runner_shutdown") {
  1228. runner_shutdown(@$argsarrayref);
  1229. # Special case: no response will be forthcoming
  1230. return 1;
  1231. }
  1232. elsif($funcname eq "runner_stopservers") {
  1233. @res = runner_stopservers(@$argsarrayref);
  1234. }
  1235. elsif($funcname eq "runner_test_preprocess") {
  1236. @res = runner_test_preprocess(@$argsarrayref);
  1237. }
  1238. elsif($funcname eq "runner_test_run") {
  1239. @res = runner_test_run(@$argsarrayref);
  1240. } else {
  1241. die "Unknown IPC function $funcname\n";
  1242. }
  1243. # print "ipcrecv results\n";
  1244. # Marshall the results to return
  1245. $buf = freeze \@res;
  1246. while(! defined ($err = syswrite($runnerw, (pack "L", length($buf)) . $buf)) || $err <= 0) {
  1247. if((!defined $err && ! $!{EINTR}) || (defined $err && $err == 0)) {
  1248. # pipe has closed; controller is gone and we must exit
  1249. runnerabort();
  1250. # Special case: no response will be forthcoming
  1251. return 1;
  1252. }
  1253. # system call was interrupted, probably by ^C; restart it so we stay in sync
  1254. }
  1255. return 0;
  1256. }
  1257. ###################################################################
  1258. # Kill the server processes that still have lock files in a directory
  1259. sub runner_clearlocks {
  1260. my ($lockdir)=@_;
  1261. if(clearlogs()) {
  1262. logmsg "Warning: log messages were lost\n";
  1263. }
  1264. clearlocks($lockdir);
  1265. return clearlogs();
  1266. }
  1267. ###################################################################
  1268. # Kill all server processes
  1269. sub runner_stopservers {
  1270. my $error = stopservers($verbose);
  1271. my $logs = clearlogs();
  1272. return ($error, $logs);
  1273. }
  1274. ###################################################################
  1275. # Shut down this runner
  1276. sub runner_shutdown {
  1277. close($runnerr);
  1278. undef $runnerr;
  1279. close($runnerw);
  1280. undef $runnerw;
  1281. }
  1282. 1;