runner.pm 48 KB

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