ktest.pl 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812
  1. #!/usr/bin/perl -w
  2. #
  3. # Copyright 2010 - Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
  4. # Licensed under the terms of the GNU GPL License version 2
  5. #
  6. use strict;
  7. use IPC::Open2;
  8. use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
  9. use File::Path qw(mkpath);
  10. use File::Copy qw(cp);
  11. use FileHandle;
  12. my $VERSION = "0.2";
  13. $| = 1;
  14. my %opt;
  15. my %repeat_tests;
  16. my %repeats;
  17. #default opts
  18. my %default = (
  19. "NUM_TESTS" => 1,
  20. "TEST_TYPE" => "build",
  21. "BUILD_TYPE" => "randconfig",
  22. "MAKE_CMD" => "make",
  23. "TIMEOUT" => 120,
  24. "TMP_DIR" => "/tmp/ktest/\${MACHINE}",
  25. "SLEEP_TIME" => 60, # sleep time between tests
  26. "BUILD_NOCLEAN" => 0,
  27. "REBOOT_ON_ERROR" => 0,
  28. "POWEROFF_ON_ERROR" => 0,
  29. "REBOOT_ON_SUCCESS" => 1,
  30. "POWEROFF_ON_SUCCESS" => 0,
  31. "BUILD_OPTIONS" => "",
  32. "BISECT_SLEEP_TIME" => 60, # sleep time between bisects
  33. "PATCHCHECK_SLEEP_TIME" => 60, # sleep time between patch checks
  34. "CLEAR_LOG" => 0,
  35. "BISECT_MANUAL" => 0,
  36. "BISECT_SKIP" => 1,
  37. "MIN_CONFIG_TYPE" => "boot",
  38. "SUCCESS_LINE" => "login:",
  39. "DETECT_TRIPLE_FAULT" => 1,
  40. "NO_INSTALL" => 0,
  41. "BOOTED_TIMEOUT" => 1,
  42. "DIE_ON_FAILURE" => 1,
  43. "SSH_EXEC" => "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND",
  44. "SCP_TO_TARGET" => "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE",
  45. "SCP_TO_TARGET_INSTALL" => "\${SCP_TO_TARGET}",
  46. "REBOOT" => "ssh \$SSH_USER\@\$MACHINE reboot",
  47. "STOP_AFTER_SUCCESS" => 10,
  48. "STOP_AFTER_FAILURE" => 60,
  49. "STOP_TEST_AFTER" => 600,
  50. "MAX_MONITOR_WAIT" => 1800,
  51. # required, and we will ask users if they don't have them but we keep the default
  52. # value something that is common.
  53. "REBOOT_TYPE" => "grub",
  54. "LOCALVERSION" => "-test",
  55. "SSH_USER" => "root",
  56. "BUILD_TARGET" => "arch/x86/boot/bzImage",
  57. "TARGET_IMAGE" => "/boot/vmlinuz-test",
  58. "LOG_FILE" => undef,
  59. "IGNORE_UNUSED" => 0,
  60. );
  61. my $ktest_config;
  62. my $version;
  63. my $have_version = 0;
  64. my $machine;
  65. my $ssh_user;
  66. my $tmpdir;
  67. my $builddir;
  68. my $outputdir;
  69. my $output_config;
  70. my $test_type;
  71. my $build_type;
  72. my $build_options;
  73. my $final_post_ktest;
  74. my $pre_ktest;
  75. my $post_ktest;
  76. my $pre_test;
  77. my $post_test;
  78. my $pre_build;
  79. my $post_build;
  80. my $pre_build_die;
  81. my $post_build_die;
  82. my $reboot_type;
  83. my $reboot_script;
  84. my $power_cycle;
  85. my $reboot;
  86. my $reboot_on_error;
  87. my $switch_to_good;
  88. my $switch_to_test;
  89. my $poweroff_on_error;
  90. my $reboot_on_success;
  91. my $die_on_failure;
  92. my $powercycle_after_reboot;
  93. my $poweroff_after_halt;
  94. my $max_monitor_wait;
  95. my $ssh_exec;
  96. my $scp_to_target;
  97. my $scp_to_target_install;
  98. my $power_off;
  99. my $grub_menu;
  100. my $grub_number;
  101. my $target;
  102. my $make;
  103. my $pre_install;
  104. my $post_install;
  105. my $no_install;
  106. my $noclean;
  107. my $minconfig;
  108. my $start_minconfig;
  109. my $start_minconfig_defined;
  110. my $output_minconfig;
  111. my $minconfig_type;
  112. my $use_output_minconfig;
  113. my $ignore_config;
  114. my $ignore_errors;
  115. my $addconfig;
  116. my $in_bisect = 0;
  117. my $bisect_bad_commit = "";
  118. my $reverse_bisect;
  119. my $bisect_manual;
  120. my $bisect_skip;
  121. my $config_bisect_good;
  122. my $bisect_ret_good;
  123. my $bisect_ret_bad;
  124. my $bisect_ret_skip;
  125. my $bisect_ret_abort;
  126. my $bisect_ret_default;
  127. my $in_patchcheck = 0;
  128. my $run_test;
  129. my $redirect;
  130. my $buildlog;
  131. my $testlog;
  132. my $dmesg;
  133. my $monitor_fp;
  134. my $monitor_pid;
  135. my $monitor_cnt = 0;
  136. my $sleep_time;
  137. my $bisect_sleep_time;
  138. my $patchcheck_sleep_time;
  139. my $ignore_warnings;
  140. my $store_failures;
  141. my $store_successes;
  142. my $test_name;
  143. my $timeout;
  144. my $booted_timeout;
  145. my $detect_triplefault;
  146. my $console;
  147. my $reboot_success_line;
  148. my $success_line;
  149. my $stop_after_success;
  150. my $stop_after_failure;
  151. my $stop_test_after;
  152. my $build_target;
  153. my $target_image;
  154. my $checkout;
  155. my $localversion;
  156. my $iteration = 0;
  157. my $successes = 0;
  158. my $bisect_good;
  159. my $bisect_bad;
  160. my $bisect_type;
  161. my $bisect_start;
  162. my $bisect_replay;
  163. my $bisect_files;
  164. my $bisect_reverse;
  165. my $bisect_check;
  166. my $config_bisect;
  167. my $config_bisect_type;
  168. my $config_bisect_check;
  169. my $patchcheck_type;
  170. my $patchcheck_start;
  171. my $patchcheck_end;
  172. # set when a test is something other that just building or install
  173. # which would require more options.
  174. my $buildonly = 1;
  175. # set when creating a new config
  176. my $newconfig = 0;
  177. my %entered_configs;
  178. my %config_help;
  179. my %variable;
  180. # force_config is the list of configs that we force enabled (or disabled)
  181. # in a .config file. The MIN_CONFIG and ADD_CONFIG configs.
  182. my %force_config;
  183. # do not force reboots on config problems
  184. my $no_reboot = 1;
  185. # reboot on success
  186. my $reboot_success = 0;
  187. my %option_map = (
  188. "MACHINE" => \$machine,
  189. "SSH_USER" => \$ssh_user,
  190. "TMP_DIR" => \$tmpdir,
  191. "OUTPUT_DIR" => \$outputdir,
  192. "BUILD_DIR" => \$builddir,
  193. "TEST_TYPE" => \$test_type,
  194. "PRE_KTEST" => \$pre_ktest,
  195. "POST_KTEST" => \$post_ktest,
  196. "PRE_TEST" => \$pre_test,
  197. "POST_TEST" => \$post_test,
  198. "BUILD_TYPE" => \$build_type,
  199. "BUILD_OPTIONS" => \$build_options,
  200. "PRE_BUILD" => \$pre_build,
  201. "POST_BUILD" => \$post_build,
  202. "PRE_BUILD_DIE" => \$pre_build_die,
  203. "POST_BUILD_DIE" => \$post_build_die,
  204. "POWER_CYCLE" => \$power_cycle,
  205. "REBOOT" => \$reboot,
  206. "BUILD_NOCLEAN" => \$noclean,
  207. "MIN_CONFIG" => \$minconfig,
  208. "OUTPUT_MIN_CONFIG" => \$output_minconfig,
  209. "START_MIN_CONFIG" => \$start_minconfig,
  210. "MIN_CONFIG_TYPE" => \$minconfig_type,
  211. "USE_OUTPUT_MIN_CONFIG" => \$use_output_minconfig,
  212. "IGNORE_CONFIG" => \$ignore_config,
  213. "TEST" => \$run_test,
  214. "ADD_CONFIG" => \$addconfig,
  215. "REBOOT_TYPE" => \$reboot_type,
  216. "GRUB_MENU" => \$grub_menu,
  217. "PRE_INSTALL" => \$pre_install,
  218. "POST_INSTALL" => \$post_install,
  219. "NO_INSTALL" => \$no_install,
  220. "REBOOT_SCRIPT" => \$reboot_script,
  221. "REBOOT_ON_ERROR" => \$reboot_on_error,
  222. "SWITCH_TO_GOOD" => \$switch_to_good,
  223. "SWITCH_TO_TEST" => \$switch_to_test,
  224. "POWEROFF_ON_ERROR" => \$poweroff_on_error,
  225. "REBOOT_ON_SUCCESS" => \$reboot_on_success,
  226. "DIE_ON_FAILURE" => \$die_on_failure,
  227. "POWER_OFF" => \$power_off,
  228. "POWERCYCLE_AFTER_REBOOT" => \$powercycle_after_reboot,
  229. "POWEROFF_AFTER_HALT" => \$poweroff_after_halt,
  230. "MAX_MONITOR_WAIT" => \$max_monitor_wait,
  231. "SLEEP_TIME" => \$sleep_time,
  232. "BISECT_SLEEP_TIME" => \$bisect_sleep_time,
  233. "PATCHCHECK_SLEEP_TIME" => \$patchcheck_sleep_time,
  234. "IGNORE_WARNINGS" => \$ignore_warnings,
  235. "IGNORE_ERRORS" => \$ignore_errors,
  236. "BISECT_MANUAL" => \$bisect_manual,
  237. "BISECT_SKIP" => \$bisect_skip,
  238. "CONFIG_BISECT_GOOD" => \$config_bisect_good,
  239. "BISECT_RET_GOOD" => \$bisect_ret_good,
  240. "BISECT_RET_BAD" => \$bisect_ret_bad,
  241. "BISECT_RET_SKIP" => \$bisect_ret_skip,
  242. "BISECT_RET_ABORT" => \$bisect_ret_abort,
  243. "BISECT_RET_DEFAULT" => \$bisect_ret_default,
  244. "STORE_FAILURES" => \$store_failures,
  245. "STORE_SUCCESSES" => \$store_successes,
  246. "TEST_NAME" => \$test_name,
  247. "TIMEOUT" => \$timeout,
  248. "BOOTED_TIMEOUT" => \$booted_timeout,
  249. "CONSOLE" => \$console,
  250. "DETECT_TRIPLE_FAULT" => \$detect_triplefault,
  251. "SUCCESS_LINE" => \$success_line,
  252. "REBOOT_SUCCESS_LINE" => \$reboot_success_line,
  253. "STOP_AFTER_SUCCESS" => \$stop_after_success,
  254. "STOP_AFTER_FAILURE" => \$stop_after_failure,
  255. "STOP_TEST_AFTER" => \$stop_test_after,
  256. "BUILD_TARGET" => \$build_target,
  257. "SSH_EXEC" => \$ssh_exec,
  258. "SCP_TO_TARGET" => \$scp_to_target,
  259. "SCP_TO_TARGET_INSTALL" => \$scp_to_target_install,
  260. "CHECKOUT" => \$checkout,
  261. "TARGET_IMAGE" => \$target_image,
  262. "LOCALVERSION" => \$localversion,
  263. "BISECT_GOOD" => \$bisect_good,
  264. "BISECT_BAD" => \$bisect_bad,
  265. "BISECT_TYPE" => \$bisect_type,
  266. "BISECT_START" => \$bisect_start,
  267. "BISECT_REPLAY" => \$bisect_replay,
  268. "BISECT_FILES" => \$bisect_files,
  269. "BISECT_REVERSE" => \$bisect_reverse,
  270. "BISECT_CHECK" => \$bisect_check,
  271. "CONFIG_BISECT" => \$config_bisect,
  272. "CONFIG_BISECT_TYPE" => \$config_bisect_type,
  273. "CONFIG_BISECT_CHECK" => \$config_bisect_check,
  274. "PATCHCHECK_TYPE" => \$patchcheck_type,
  275. "PATCHCHECK_START" => \$patchcheck_start,
  276. "PATCHCHECK_END" => \$patchcheck_end,
  277. );
  278. # Options may be used by other options, record them.
  279. my %used_options;
  280. # default variables that can be used
  281. chomp ($variable{"PWD"} = `pwd`);
  282. $config_help{"MACHINE"} = << "EOF"
  283. The machine hostname that you will test.
  284. For build only tests, it is still needed to differentiate log files.
  285. EOF
  286. ;
  287. $config_help{"SSH_USER"} = << "EOF"
  288. The box is expected to have ssh on normal bootup, provide the user
  289. (most likely root, since you need privileged operations)
  290. EOF
  291. ;
  292. $config_help{"BUILD_DIR"} = << "EOF"
  293. The directory that contains the Linux source code (full path).
  294. You can use \${PWD} that will be the path where ktest.pl is run, or use
  295. \${THIS_DIR} which is assigned \${PWD} but may be changed later.
  296. EOF
  297. ;
  298. $config_help{"OUTPUT_DIR"} = << "EOF"
  299. The directory that the objects will be built (full path).
  300. (can not be same as BUILD_DIR)
  301. You can use \${PWD} that will be the path where ktest.pl is run, or use
  302. \${THIS_DIR} which is assigned \${PWD} but may be changed later.
  303. EOF
  304. ;
  305. $config_help{"BUILD_TARGET"} = << "EOF"
  306. The location of the compiled file to copy to the target.
  307. (relative to OUTPUT_DIR)
  308. EOF
  309. ;
  310. $config_help{"BUILD_OPTIONS"} = << "EOF"
  311. Options to add to \"make\" when building.
  312. i.e. -j20
  313. EOF
  314. ;
  315. $config_help{"TARGET_IMAGE"} = << "EOF"
  316. The place to put your image on the test machine.
  317. EOF
  318. ;
  319. $config_help{"POWER_CYCLE"} = << "EOF"
  320. A script or command to reboot the box.
  321. Here is a digital loggers power switch example
  322. POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
  323. Here is an example to reboot a virtual box on the current host
  324. with the name "Guest".
  325. POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
  326. EOF
  327. ;
  328. $config_help{"CONSOLE"} = << "EOF"
  329. The script or command that reads the console
  330. If you use ttywatch server, something like the following would work.
  331. CONSOLE = nc -d localhost 3001
  332. For a virtual machine with guest name "Guest".
  333. CONSOLE = virsh console Guest
  334. EOF
  335. ;
  336. $config_help{"LOCALVERSION"} = << "EOF"
  337. Required version ending to differentiate the test
  338. from other linux builds on the system.
  339. EOF
  340. ;
  341. $config_help{"REBOOT_TYPE"} = << "EOF"
  342. Way to reboot the box to the test kernel.
  343. Only valid options so far are "grub" and "script".
  344. If you specify grub, it will assume grub version 1
  345. and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
  346. and select that target to reboot to the kernel. If this is not
  347. your setup, then specify "script" and have a command or script
  348. specified in REBOOT_SCRIPT to boot to the target.
  349. The entry in /boot/grub/menu.lst must be entered in manually.
  350. The test will not modify that file.
  351. EOF
  352. ;
  353. $config_help{"GRUB_MENU"} = << "EOF"
  354. The grub title name for the test kernel to boot
  355. (Only mandatory if REBOOT_TYPE = grub)
  356. Note, ktest.pl will not update the grub menu.lst, you need to
  357. manually add an option for the test. ktest.pl will search
  358. the grub menu.lst for this option to find what kernel to
  359. reboot into.
  360. For example, if in the /boot/grub/menu.lst the test kernel title has:
  361. title Test Kernel
  362. kernel vmlinuz-test
  363. GRUB_MENU = Test Kernel
  364. EOF
  365. ;
  366. $config_help{"REBOOT_SCRIPT"} = << "EOF"
  367. A script to reboot the target into the test kernel
  368. (Only mandatory if REBOOT_TYPE = script)
  369. EOF
  370. ;
  371. sub read_prompt {
  372. my ($cancel, $prompt) = @_;
  373. my $ans;
  374. for (;;) {
  375. if ($cancel) {
  376. print "$prompt [y/n/C] ";
  377. } else {
  378. print "$prompt [Y/n] ";
  379. }
  380. $ans = <STDIN>;
  381. chomp $ans;
  382. if ($ans =~ /^\s*$/) {
  383. if ($cancel) {
  384. $ans = "c";
  385. } else {
  386. $ans = "y";
  387. }
  388. }
  389. last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
  390. if ($cancel) {
  391. last if ($ans =~ /^c$/i);
  392. print "Please answer either 'y', 'n' or 'c'.\n";
  393. } else {
  394. print "Please answer either 'y' or 'n'.\n";
  395. }
  396. }
  397. if ($ans =~ /^c/i) {
  398. exit;
  399. }
  400. if ($ans !~ /^y$/i) {
  401. return 0;
  402. }
  403. return 1;
  404. }
  405. sub read_yn {
  406. my ($prompt) = @_;
  407. return read_prompt 0, $prompt;
  408. }
  409. sub read_ync {
  410. my ($prompt) = @_;
  411. return read_prompt 1, $prompt;
  412. }
  413. sub get_ktest_config {
  414. my ($config) = @_;
  415. my $ans;
  416. return if (defined($opt{$config}));
  417. if (defined($config_help{$config})) {
  418. print "\n";
  419. print $config_help{$config};
  420. }
  421. for (;;) {
  422. print "$config = ";
  423. if (defined($default{$config}) && length($default{$config})) {
  424. print "\[$default{$config}\] ";
  425. }
  426. $ans = <STDIN>;
  427. $ans =~ s/^\s*(.*\S)\s*$/$1/;
  428. if ($ans =~ /^\s*$/) {
  429. if ($default{$config}) {
  430. $ans = $default{$config};
  431. } else {
  432. print "Your answer can not be blank\n";
  433. next;
  434. }
  435. }
  436. $entered_configs{$config} = ${ans};
  437. last;
  438. }
  439. }
  440. sub get_ktest_configs {
  441. get_ktest_config("MACHINE");
  442. get_ktest_config("BUILD_DIR");
  443. get_ktest_config("OUTPUT_DIR");
  444. if ($newconfig) {
  445. get_ktest_config("BUILD_OPTIONS");
  446. }
  447. # options required for other than just building a kernel
  448. if (!$buildonly) {
  449. get_ktest_config("POWER_CYCLE");
  450. get_ktest_config("CONSOLE");
  451. }
  452. # options required for install and more
  453. if ($buildonly != 1) {
  454. get_ktest_config("SSH_USER");
  455. get_ktest_config("BUILD_TARGET");
  456. get_ktest_config("TARGET_IMAGE");
  457. }
  458. get_ktest_config("LOCALVERSION");
  459. return if ($buildonly);
  460. my $rtype = $opt{"REBOOT_TYPE"};
  461. if (!defined($rtype)) {
  462. if (!defined($opt{"GRUB_MENU"})) {
  463. get_ktest_config("REBOOT_TYPE");
  464. $rtype = $entered_configs{"REBOOT_TYPE"};
  465. } else {
  466. $rtype = "grub";
  467. }
  468. }
  469. if ($rtype eq "grub") {
  470. get_ktest_config("GRUB_MENU");
  471. }
  472. }
  473. sub process_variables {
  474. my ($value, $remove_undef) = @_;
  475. my $retval = "";
  476. # We want to check for '\', and it is just easier
  477. # to check the previous characet of '$' and not need
  478. # to worry if '$' is the first character. By adding
  479. # a space to $value, we can just check [^\\]\$ and
  480. # it will still work.
  481. $value = " $value";
  482. while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
  483. my $begin = $1;
  484. my $var = $2;
  485. my $end = $3;
  486. # append beginning of value to retval
  487. $retval = "$retval$begin";
  488. if (defined($variable{$var})) {
  489. $retval = "$retval$variable{$var}";
  490. } elsif (defined($remove_undef) && $remove_undef) {
  491. # for if statements, any variable that is not defined,
  492. # we simple convert to 0
  493. $retval = "${retval}0";
  494. } else {
  495. # put back the origin piece.
  496. $retval = "$retval\$\{$var\}";
  497. # This could be an option that is used later, save
  498. # it so we don't warn if this option is not one of
  499. # ktests options.
  500. $used_options{$var} = 1;
  501. }
  502. $value = $end;
  503. }
  504. $retval = "$retval$value";
  505. # remove the space added in the beginning
  506. $retval =~ s/ //;
  507. return "$retval"
  508. }
  509. sub set_value {
  510. my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
  511. my $prvalue = process_variables($rvalue);
  512. if ($buildonly && $lvalue =~ /^TEST_TYPE(\[.*\])?$/ && $prvalue ne "build") {
  513. # Note if a test is something other than build, then we
  514. # will need other manditory options.
  515. if ($prvalue ne "install") {
  516. $buildonly = 0;
  517. } else {
  518. # install still limits some manditory options.
  519. $buildonly = 2;
  520. }
  521. }
  522. if (defined($opt{$lvalue})) {
  523. if (!$override || defined(${$overrides}{$lvalue})) {
  524. my $extra = "";
  525. if ($override) {
  526. $extra = "In the same override section!\n";
  527. }
  528. die "$name: $.: Option $lvalue defined more than once!\n$extra";
  529. }
  530. ${$overrides}{$lvalue} = $prvalue;
  531. }
  532. if ($rvalue =~ /^\s*$/) {
  533. delete $opt{$lvalue};
  534. } else {
  535. $opt{$lvalue} = $prvalue;
  536. }
  537. }
  538. sub set_variable {
  539. my ($lvalue, $rvalue) = @_;
  540. if ($rvalue =~ /^\s*$/) {
  541. delete $variable{$lvalue};
  542. } else {
  543. $rvalue = process_variables($rvalue);
  544. $variable{$lvalue} = $rvalue;
  545. }
  546. }
  547. sub process_compare {
  548. my ($lval, $cmp, $rval) = @_;
  549. # remove whitespace
  550. $lval =~ s/^\s*//;
  551. $lval =~ s/\s*$//;
  552. $rval =~ s/^\s*//;
  553. $rval =~ s/\s*$//;
  554. if ($cmp eq "==") {
  555. return $lval eq $rval;
  556. } elsif ($cmp eq "!=") {
  557. return $lval ne $rval;
  558. } elsif ($cmp eq "=~") {
  559. return $lval =~ m/$rval/;
  560. } elsif ($cmp eq "!~") {
  561. return $lval !~ m/$rval/;
  562. }
  563. my $statement = "$lval $cmp $rval";
  564. my $ret = eval $statement;
  565. # $@ stores error of eval
  566. if ($@) {
  567. return -1;
  568. }
  569. return $ret;
  570. }
  571. sub value_defined {
  572. my ($val) = @_;
  573. return defined($variable{$2}) ||
  574. defined($opt{$2});
  575. }
  576. my $d = 0;
  577. sub process_expression {
  578. my ($name, $val) = @_;
  579. my $c = $d++;
  580. while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
  581. my $express = $1;
  582. if (process_expression($name, $express)) {
  583. $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
  584. } else {
  585. $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
  586. }
  587. }
  588. $d--;
  589. my $OR = "\\|\\|";
  590. my $AND = "\\&\\&";
  591. while ($val =~ s/^(.*?)($OR|$AND)//) {
  592. my $express = $1;
  593. my $op = $2;
  594. if (process_expression($name, $express)) {
  595. if ($op eq "||") {
  596. return 1;
  597. }
  598. } else {
  599. if ($op eq "&&") {
  600. return 0;
  601. }
  602. }
  603. }
  604. if ($val =~ /(.*)(==|\!=|>=|<=|>|<|=~|\!~)(.*)/) {
  605. my $ret = process_compare($1, $2, $3);
  606. if ($ret < 0) {
  607. die "$name: $.: Unable to process comparison\n";
  608. }
  609. return $ret;
  610. }
  611. if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
  612. if (defined $1) {
  613. return !value_defined($2);
  614. } else {
  615. return value_defined($2);
  616. }
  617. }
  618. if ($val =~ /^\s*0\s*$/) {
  619. return 0;
  620. } elsif ($val =~ /^\s*\d+\s*$/) {
  621. return 1;
  622. }
  623. die ("$name: $.: Undefined content $val in if statement\n");
  624. }
  625. sub process_if {
  626. my ($name, $value) = @_;
  627. # Convert variables and replace undefined ones with 0
  628. my $val = process_variables($value, 1);
  629. my $ret = process_expression $name, $val;
  630. return $ret;
  631. }
  632. sub __read_config {
  633. my ($config, $current_test_num) = @_;
  634. my $in;
  635. open($in, $config) || die "can't read file $config";
  636. my $name = $config;
  637. $name =~ s,.*/(.*),$1,;
  638. my $test_num = $$current_test_num;
  639. my $default = 1;
  640. my $repeat = 1;
  641. my $num_tests_set = 0;
  642. my $skip = 0;
  643. my $rest;
  644. my $line;
  645. my $test_case = 0;
  646. my $if = 0;
  647. my $if_set = 0;
  648. my $override = 0;
  649. my %overrides;
  650. while (<$in>) {
  651. # ignore blank lines and comments
  652. next if (/^\s*$/ || /\s*\#/);
  653. if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
  654. my $type = $1;
  655. $rest = $2;
  656. $line = $2;
  657. my $old_test_num;
  658. my $old_repeat;
  659. $override = 0;
  660. if ($type eq "TEST_START") {
  661. if ($num_tests_set) {
  662. die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
  663. }
  664. $old_test_num = $test_num;
  665. $old_repeat = $repeat;
  666. $test_num += $repeat;
  667. $default = 0;
  668. $repeat = 1;
  669. } else {
  670. $default = 1;
  671. }
  672. # If SKIP is anywhere in the line, the command will be skipped
  673. if ($rest =~ s/\s+SKIP\b//) {
  674. $skip = 1;
  675. } else {
  676. $test_case = 1;
  677. $skip = 0;
  678. }
  679. if ($rest =~ s/\sELSE\b//) {
  680. if (!$if) {
  681. die "$name: $.: ELSE found with out matching IF section\n$_";
  682. }
  683. $if = 0;
  684. if ($if_set) {
  685. $skip = 1;
  686. } else {
  687. $skip = 0;
  688. }
  689. }
  690. if ($rest =~ s/\sIF\s+(.*)//) {
  691. if (process_if($name, $1)) {
  692. $if_set = 1;
  693. } else {
  694. $skip = 1;
  695. }
  696. $if = 1;
  697. } else {
  698. $if = 0;
  699. $if_set = 0;
  700. }
  701. if (!$skip) {
  702. if ($type eq "TEST_START") {
  703. if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
  704. $repeat = $1;
  705. $repeat_tests{"$test_num"} = $repeat;
  706. }
  707. } elsif ($rest =~ s/\sOVERRIDE\b//) {
  708. # DEFAULT only
  709. $override = 1;
  710. # Clear previous overrides
  711. %overrides = ();
  712. }
  713. }
  714. if (!$skip && $rest !~ /^\s*$/) {
  715. die "$name: $.: Gargbage found after $type\n$_";
  716. }
  717. if ($skip && $type eq "TEST_START") {
  718. $test_num = $old_test_num;
  719. $repeat = $old_repeat;
  720. }
  721. } elsif (/^\s*ELSE\b(.*)$/) {
  722. if (!$if) {
  723. die "$name: $.: ELSE found with out matching IF section\n$_";
  724. }
  725. $rest = $1;
  726. if ($if_set) {
  727. $skip = 1;
  728. $rest = "";
  729. } else {
  730. $skip = 0;
  731. if ($rest =~ /\sIF\s+(.*)/) {
  732. # May be a ELSE IF section.
  733. if (!process_if($name, $1)) {
  734. $skip = 1;
  735. }
  736. $rest = "";
  737. } else {
  738. $if = 0;
  739. }
  740. }
  741. if ($rest !~ /^\s*$/) {
  742. die "$name: $.: Gargbage found after DEFAULTS\n$_";
  743. }
  744. } elsif (/^\s*INCLUDE\s+(\S+)/) {
  745. next if ($skip);
  746. if (!$default) {
  747. die "$name: $.: INCLUDE can only be done in default sections\n$_";
  748. }
  749. my $file = process_variables($1);
  750. if ($file !~ m,^/,) {
  751. # check the path of the config file first
  752. if ($config =~ m,(.*)/,) {
  753. if (-f "$1/$file") {
  754. $file = "$1/$file";
  755. }
  756. }
  757. }
  758. if ( ! -r $file ) {
  759. die "$name: $.: Can't read file $file\n$_";
  760. }
  761. if (__read_config($file, \$test_num)) {
  762. $test_case = 1;
  763. }
  764. } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
  765. next if ($skip);
  766. my $lvalue = $1;
  767. my $rvalue = $2;
  768. if (!$default &&
  769. ($lvalue eq "NUM_TESTS" ||
  770. $lvalue eq "LOG_FILE" ||
  771. $lvalue eq "CLEAR_LOG")) {
  772. die "$name: $.: $lvalue must be set in DEFAULTS section\n";
  773. }
  774. if ($lvalue eq "NUM_TESTS") {
  775. if ($test_num) {
  776. die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
  777. }
  778. if (!$default) {
  779. die "$name: $.: NUM_TESTS must be set in default section\n";
  780. }
  781. $num_tests_set = 1;
  782. }
  783. if ($default || $lvalue =~ /\[\d+\]$/) {
  784. set_value($lvalue, $rvalue, $override, \%overrides, $name);
  785. } else {
  786. my $val = "$lvalue\[$test_num\]";
  787. set_value($val, $rvalue, $override, \%overrides, $name);
  788. if ($repeat > 1) {
  789. $repeats{$val} = $repeat;
  790. }
  791. }
  792. } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
  793. next if ($skip);
  794. my $lvalue = $1;
  795. my $rvalue = $2;
  796. # process config variables.
  797. # Config variables are only active while reading the
  798. # config and can be defined anywhere. They also ignore
  799. # TEST_START and DEFAULTS, but are skipped if they are in
  800. # on of these sections that have SKIP defined.
  801. # The save variable can be
  802. # defined multiple times and the new one simply overrides
  803. # the prevous one.
  804. set_variable($lvalue, $rvalue);
  805. } else {
  806. die "$name: $.: Garbage found in config\n$_";
  807. }
  808. }
  809. if ($test_num) {
  810. $test_num += $repeat - 1;
  811. $opt{"NUM_TESTS"} = $test_num;
  812. }
  813. close($in);
  814. $$current_test_num = $test_num;
  815. return $test_case;
  816. }
  817. sub get_test_case {
  818. print "What test case would you like to run?\n";
  819. print " (build, install or boot)\n";
  820. print " Other tests are available but require editing the config file\n";
  821. my $ans = <STDIN>;
  822. chomp $ans;
  823. $default{"TEST_TYPE"} = $ans;
  824. }
  825. sub read_config {
  826. my ($config) = @_;
  827. my $test_case;
  828. my $test_num = 0;
  829. $test_case = __read_config $config, \$test_num;
  830. # make sure we have all mandatory configs
  831. get_ktest_configs;
  832. # was a test specified?
  833. if (!$test_case) {
  834. print "No test case specified.\n";
  835. get_test_case;
  836. }
  837. # set any defaults
  838. foreach my $default (keys %default) {
  839. if (!defined($opt{$default})) {
  840. $opt{$default} = $default{$default};
  841. }
  842. }
  843. if ($opt{"IGNORE_UNUSED"} == 1) {
  844. return;
  845. }
  846. my %not_used;
  847. # check if there are any stragglers (typos?)
  848. foreach my $option (keys %opt) {
  849. my $op = $option;
  850. # remove per test labels.
  851. $op =~ s/\[.*\]//;
  852. if (!exists($option_map{$op}) &&
  853. !exists($default{$op}) &&
  854. !exists($used_options{$op})) {
  855. $not_used{$op} = 1;
  856. }
  857. }
  858. if (%not_used) {
  859. my $s = "s are";
  860. $s = " is" if (keys %not_used == 1);
  861. print "The following option$s not used; could be a typo:\n";
  862. foreach my $option (keys %not_used) {
  863. print "$option\n";
  864. }
  865. print "Set IGRNORE_UNUSED = 1 to have ktest ignore unused variables\n";
  866. if (!read_yn "Do you want to continue?") {
  867. exit -1;
  868. }
  869. }
  870. }
  871. sub __eval_option {
  872. my ($option, $i) = @_;
  873. # Add space to evaluate the character before $
  874. $option = " $option";
  875. my $retval = "";
  876. my $repeated = 0;
  877. my $parent = 0;
  878. foreach my $test (keys %repeat_tests) {
  879. if ($i >= $test &&
  880. $i < $test + $repeat_tests{$test}) {
  881. $repeated = 1;
  882. $parent = $test;
  883. last;
  884. }
  885. }
  886. while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
  887. my $start = $1;
  888. my $var = $2;
  889. my $end = $3;
  890. # Append beginning of line
  891. $retval = "$retval$start";
  892. # If the iteration option OPT[$i] exists, then use that.
  893. # otherwise see if the default OPT (without [$i]) exists.
  894. my $o = "$var\[$i\]";
  895. my $parento = "$var\[$parent\]";
  896. if (defined($opt{$o})) {
  897. $o = $opt{$o};
  898. $retval = "$retval$o";
  899. } elsif ($repeated && defined($opt{$parento})) {
  900. $o = $opt{$parento};
  901. $retval = "$retval$o";
  902. } elsif (defined($opt{$var})) {
  903. $o = $opt{$var};
  904. $retval = "$retval$o";
  905. } else {
  906. $retval = "$retval\$\{$var\}";
  907. }
  908. $option = $end;
  909. }
  910. $retval = "$retval$option";
  911. $retval =~ s/^ //;
  912. return $retval;
  913. }
  914. sub eval_option {
  915. my ($option, $i) = @_;
  916. my $prev = "";
  917. # Since an option can evaluate to another option,
  918. # keep iterating until we do not evaluate any more
  919. # options.
  920. my $r = 0;
  921. while ($prev ne $option) {
  922. # Check for recursive evaluations.
  923. # 100 deep should be more than enough.
  924. if ($r++ > 100) {
  925. die "Over 100 evaluations accurred with $option\n" .
  926. "Check for recursive variables\n";
  927. }
  928. $prev = $option;
  929. $option = __eval_option($option, $i);
  930. }
  931. return $option;
  932. }
  933. sub _logit {
  934. if (defined($opt{"LOG_FILE"})) {
  935. open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
  936. print OUT @_;
  937. close(OUT);
  938. }
  939. }
  940. sub logit {
  941. if (defined($opt{"LOG_FILE"})) {
  942. _logit @_;
  943. } else {
  944. print @_;
  945. }
  946. }
  947. sub doprint {
  948. print @_;
  949. _logit @_;
  950. }
  951. sub run_command;
  952. sub start_monitor;
  953. sub end_monitor;
  954. sub wait_for_monitor;
  955. sub reboot {
  956. my ($time) = @_;
  957. if (defined($time)) {
  958. start_monitor;
  959. # flush out current monitor
  960. # May contain the reboot success line
  961. wait_for_monitor 1;
  962. }
  963. # try to reboot normally
  964. if (run_command $reboot) {
  965. if (defined($powercycle_after_reboot)) {
  966. sleep $powercycle_after_reboot;
  967. run_command "$power_cycle";
  968. }
  969. } else {
  970. # nope? power cycle it.
  971. run_command "$power_cycle";
  972. }
  973. if (defined($time)) {
  974. if (wait_for_monitor($time, $reboot_success_line)) {
  975. # reboot got stuck?
  976. doprint "Reboot did not finish. Forcing power cycle\n";
  977. run_command "$power_cycle";
  978. }
  979. end_monitor;
  980. }
  981. }
  982. sub reboot_to_good {
  983. my ($time) = @_;
  984. if (defined($switch_to_good)) {
  985. run_command $switch_to_good;
  986. }
  987. reboot $time;
  988. }
  989. sub do_not_reboot {
  990. my $i = $iteration;
  991. return $test_type eq "build" || $no_reboot ||
  992. ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
  993. ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
  994. }
  995. sub dodie {
  996. doprint "CRITICAL FAILURE... ", @_, "\n";
  997. my $i = $iteration;
  998. if ($reboot_on_error && !do_not_reboot) {
  999. doprint "REBOOTING\n";
  1000. reboot_to_good;
  1001. } elsif ($poweroff_on_error && defined($power_off)) {
  1002. doprint "POWERING OFF\n";
  1003. `$power_off`;
  1004. }
  1005. if (defined($opt{"LOG_FILE"})) {
  1006. print " See $opt{LOG_FILE} for more info.\n";
  1007. }
  1008. die @_, "\n";
  1009. }
  1010. sub open_console {
  1011. my ($fp) = @_;
  1012. my $flags;
  1013. my $pid = open($fp, "$console|") or
  1014. dodie "Can't open console $console";
  1015. $flags = fcntl($fp, F_GETFL, 0) or
  1016. dodie "Can't get flags for the socket: $!";
  1017. $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
  1018. dodie "Can't set flags for the socket: $!";
  1019. return $pid;
  1020. }
  1021. sub close_console {
  1022. my ($fp, $pid) = @_;
  1023. doprint "kill child process $pid\n";
  1024. kill 2, $pid;
  1025. print "closing!\n";
  1026. close($fp);
  1027. }
  1028. sub start_monitor {
  1029. if ($monitor_cnt++) {
  1030. return;
  1031. }
  1032. $monitor_fp = \*MONFD;
  1033. $monitor_pid = open_console $monitor_fp;
  1034. return;
  1035. open(MONFD, "Stop perl from warning about single use of MONFD");
  1036. }
  1037. sub end_monitor {
  1038. if (--$monitor_cnt) {
  1039. return;
  1040. }
  1041. close_console($monitor_fp, $monitor_pid);
  1042. }
  1043. sub wait_for_monitor {
  1044. my ($time, $stop) = @_;
  1045. my $full_line = "";
  1046. my $line;
  1047. my $booted = 0;
  1048. my $start_time = time;
  1049. my $skip_call_trace = 0;
  1050. my $bug = 0;
  1051. my $bug_ignored = 0;
  1052. my $now;
  1053. doprint "** Wait for monitor to settle down **\n";
  1054. # read the monitor and wait for the system to calm down
  1055. while (!$booted) {
  1056. $line = wait_for_input($monitor_fp, $time);
  1057. last if (!defined($line));
  1058. print "$line";
  1059. $full_line .= $line;
  1060. if (defined($stop) && $full_line =~ /$stop/) {
  1061. doprint "wait for monitor detected $stop\n";
  1062. $booted = 1;
  1063. }
  1064. if ($full_line =~ /\[ backtrace testing \]/) {
  1065. $skip_call_trace = 1;
  1066. }
  1067. if ($full_line =~ /call trace:/i) {
  1068. if (!$bug && !$skip_call_trace) {
  1069. if ($ignore_errors) {
  1070. $bug_ignored = 1;
  1071. } else {
  1072. $bug = 1;
  1073. }
  1074. }
  1075. }
  1076. if ($full_line =~ /\[ end of backtrace testing \]/) {
  1077. $skip_call_trace = 0;
  1078. }
  1079. if ($full_line =~ /Kernel panic -/) {
  1080. $bug = 1;
  1081. }
  1082. if ($line =~ /\n/) {
  1083. $full_line = "";
  1084. }
  1085. $now = time;
  1086. if ($now - $start_time >= $max_monitor_wait) {
  1087. doprint "Exiting monitor flush due to hitting MAX_MONITOR_WAIT\n";
  1088. return 1;
  1089. }
  1090. }
  1091. print "** Monitor flushed **\n";
  1092. return $bug;
  1093. }
  1094. sub save_logs {
  1095. my ($result, $basedir) = @_;
  1096. my @t = localtime;
  1097. my $date = sprintf "%04d%02d%02d%02d%02d%02d",
  1098. 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
  1099. my $type = $build_type;
  1100. if ($type =~ /useconfig/) {
  1101. $type = "useconfig";
  1102. }
  1103. my $dir = "$machine-$test_type-$type-$result-$date";
  1104. $dir = "$basedir/$dir";
  1105. if (!-d $dir) {
  1106. mkpath($dir) or
  1107. die "can't create $dir";
  1108. }
  1109. my %files = (
  1110. "config" => $output_config,
  1111. "buildlog" => $buildlog,
  1112. "dmesg" => $dmesg,
  1113. "testlog" => $testlog,
  1114. );
  1115. while (my ($name, $source) = each(%files)) {
  1116. if (-f "$source") {
  1117. cp "$source", "$dir/$name" or
  1118. die "failed to copy $source";
  1119. }
  1120. }
  1121. doprint "*** Saved info to $dir ***\n";
  1122. }
  1123. sub fail {
  1124. if (defined($post_test)) {
  1125. run_command $post_test;
  1126. }
  1127. if ($die_on_failure) {
  1128. dodie @_;
  1129. }
  1130. doprint "FAILED\n";
  1131. my $i = $iteration;
  1132. # no need to reboot for just building.
  1133. if (!do_not_reboot) {
  1134. doprint "REBOOTING\n";
  1135. reboot_to_good $sleep_time;
  1136. }
  1137. my $name = "";
  1138. if (defined($test_name)) {
  1139. $name = " ($test_name)";
  1140. }
  1141. doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  1142. doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  1143. doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
  1144. doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  1145. doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  1146. if (defined($store_failures)) {
  1147. save_logs "fail", $store_failures;
  1148. }
  1149. return 1;
  1150. }
  1151. sub run_command {
  1152. my ($command) = @_;
  1153. my $dolog = 0;
  1154. my $dord = 0;
  1155. my $pid;
  1156. $command =~ s/\$SSH_USER/$ssh_user/g;
  1157. $command =~ s/\$MACHINE/$machine/g;
  1158. doprint("$command ... ");
  1159. $pid = open(CMD, "$command 2>&1 |") or
  1160. (fail "unable to exec $command" and return 0);
  1161. if (defined($opt{"LOG_FILE"})) {
  1162. open(LOG, ">>$opt{LOG_FILE}") or
  1163. dodie "failed to write to log";
  1164. $dolog = 1;
  1165. }
  1166. if (defined($redirect)) {
  1167. open (RD, ">$redirect") or
  1168. dodie "failed to write to redirect $redirect";
  1169. $dord = 1;
  1170. }
  1171. while (<CMD>) {
  1172. print LOG if ($dolog);
  1173. print RD if ($dord);
  1174. }
  1175. waitpid($pid, 0);
  1176. my $failed = $?;
  1177. close(CMD);
  1178. close(LOG) if ($dolog);
  1179. close(RD) if ($dord);
  1180. if ($failed) {
  1181. doprint "FAILED!\n";
  1182. } else {
  1183. doprint "SUCCESS\n";
  1184. }
  1185. return !$failed;
  1186. }
  1187. sub run_ssh {
  1188. my ($cmd) = @_;
  1189. my $cp_exec = $ssh_exec;
  1190. $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
  1191. return run_command "$cp_exec";
  1192. }
  1193. sub run_scp {
  1194. my ($src, $dst, $cp_scp) = @_;
  1195. $cp_scp =~ s/\$SRC_FILE/$src/g;
  1196. $cp_scp =~ s/\$DST_FILE/$dst/g;
  1197. return run_command "$cp_scp";
  1198. }
  1199. sub run_scp_install {
  1200. my ($src, $dst) = @_;
  1201. my $cp_scp = $scp_to_target_install;
  1202. return run_scp($src, $dst, $cp_scp);
  1203. }
  1204. sub run_scp_mod {
  1205. my ($src, $dst) = @_;
  1206. my $cp_scp = $scp_to_target;
  1207. return run_scp($src, $dst, $cp_scp);
  1208. }
  1209. sub get_grub_index {
  1210. if ($reboot_type ne "grub") {
  1211. return;
  1212. }
  1213. return if (defined($grub_number));
  1214. doprint "Find grub menu ... ";
  1215. $grub_number = -1;
  1216. my $ssh_grub = $ssh_exec;
  1217. $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
  1218. open(IN, "$ssh_grub |")
  1219. or die "unable to get menu.lst";
  1220. my $found = 0;
  1221. while (<IN>) {
  1222. if (/^\s*title\s+$grub_menu\s*$/) {
  1223. $grub_number++;
  1224. $found = 1;
  1225. last;
  1226. } elsif (/^\s*title\s/) {
  1227. $grub_number++;
  1228. }
  1229. }
  1230. close(IN);
  1231. die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
  1232. if (!$found);
  1233. doprint "$grub_number\n";
  1234. }
  1235. sub wait_for_input
  1236. {
  1237. my ($fp, $time) = @_;
  1238. my $rin;
  1239. my $ready;
  1240. my $line;
  1241. my $ch;
  1242. if (!defined($time)) {
  1243. $time = $timeout;
  1244. }
  1245. $rin = '';
  1246. vec($rin, fileno($fp), 1) = 1;
  1247. $ready = select($rin, undef, undef, $time);
  1248. $line = "";
  1249. # try to read one char at a time
  1250. while (sysread $fp, $ch, 1) {
  1251. $line .= $ch;
  1252. last if ($ch eq "\n");
  1253. }
  1254. if (!length($line)) {
  1255. return undef;
  1256. }
  1257. return $line;
  1258. }
  1259. sub reboot_to {
  1260. if (defined($switch_to_test)) {
  1261. run_command $switch_to_test;
  1262. }
  1263. if ($reboot_type eq "grub") {
  1264. run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
  1265. } elsif (defined $reboot_script) {
  1266. run_command "$reboot_script";
  1267. }
  1268. reboot;
  1269. }
  1270. sub get_sha1 {
  1271. my ($commit) = @_;
  1272. doprint "git rev-list --max-count=1 $commit ... ";
  1273. my $sha1 = `git rev-list --max-count=1 $commit`;
  1274. my $ret = $?;
  1275. logit $sha1;
  1276. if ($ret) {
  1277. doprint "FAILED\n";
  1278. dodie "Failed to get git $commit";
  1279. }
  1280. print "SUCCESS\n";
  1281. chomp $sha1;
  1282. return $sha1;
  1283. }
  1284. sub monitor {
  1285. my $booted = 0;
  1286. my $bug = 0;
  1287. my $bug_ignored = 0;
  1288. my $skip_call_trace = 0;
  1289. my $loops;
  1290. wait_for_monitor 5;
  1291. my $line;
  1292. my $full_line = "";
  1293. open(DMESG, "> $dmesg") or
  1294. die "unable to write to $dmesg";
  1295. reboot_to;
  1296. my $success_start;
  1297. my $failure_start;
  1298. my $monitor_start = time;
  1299. my $done = 0;
  1300. my $version_found = 0;
  1301. while (!$done) {
  1302. if ($bug && defined($stop_after_failure) &&
  1303. $stop_after_failure >= 0) {
  1304. my $time = $stop_after_failure - (time - $failure_start);
  1305. $line = wait_for_input($monitor_fp, $time);
  1306. if (!defined($line)) {
  1307. doprint "bug timed out after $booted_timeout seconds\n";
  1308. doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
  1309. last;
  1310. }
  1311. } elsif ($booted) {
  1312. $line = wait_for_input($monitor_fp, $booted_timeout);
  1313. if (!defined($line)) {
  1314. my $s = $booted_timeout == 1 ? "" : "s";
  1315. doprint "Successful boot found: break after $booted_timeout second$s\n";
  1316. last;
  1317. }
  1318. } else {
  1319. $line = wait_for_input($monitor_fp);
  1320. if (!defined($line)) {
  1321. my $s = $timeout == 1 ? "" : "s";
  1322. doprint "Timed out after $timeout second$s\n";
  1323. last;
  1324. }
  1325. }
  1326. doprint $line;
  1327. print DMESG $line;
  1328. # we are not guaranteed to get a full line
  1329. $full_line .= $line;
  1330. if ($full_line =~ /$success_line/) {
  1331. $booted = 1;
  1332. $success_start = time;
  1333. }
  1334. if ($booted && defined($stop_after_success) &&
  1335. $stop_after_success >= 0) {
  1336. my $now = time;
  1337. if ($now - $success_start >= $stop_after_success) {
  1338. doprint "Test forced to stop after $stop_after_success seconds after success\n";
  1339. last;
  1340. }
  1341. }
  1342. if ($full_line =~ /\[ backtrace testing \]/) {
  1343. $skip_call_trace = 1;
  1344. }
  1345. if ($full_line =~ /call trace:/i) {
  1346. if (!$bug && !$skip_call_trace) {
  1347. if ($ignore_errors) {
  1348. $bug_ignored = 1;
  1349. } else {
  1350. $bug = 1;
  1351. $failure_start = time;
  1352. }
  1353. }
  1354. }
  1355. if ($bug && defined($stop_after_failure) &&
  1356. $stop_after_failure >= 0) {
  1357. my $now = time;
  1358. if ($now - $failure_start >= $stop_after_failure) {
  1359. doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
  1360. last;
  1361. }
  1362. }
  1363. if ($full_line =~ /\[ end of backtrace testing \]/) {
  1364. $skip_call_trace = 0;
  1365. }
  1366. if ($full_line =~ /Kernel panic -/) {
  1367. $failure_start = time;
  1368. $bug = 1;
  1369. }
  1370. # Detect triple faults by testing the banner
  1371. if ($full_line =~ /\bLinux version (\S+).*\n/) {
  1372. if ($1 eq $version) {
  1373. $version_found = 1;
  1374. } elsif ($version_found && $detect_triplefault) {
  1375. # We already booted into the kernel we are testing,
  1376. # but now we booted into another kernel?
  1377. # Consider this a triple fault.
  1378. doprint "Aleady booted in Linux kernel $version, but now\n";
  1379. doprint "we booted into Linux kernel $1.\n";
  1380. doprint "Assuming that this is a triple fault.\n";
  1381. doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
  1382. last;
  1383. }
  1384. }
  1385. if ($line =~ /\n/) {
  1386. $full_line = "";
  1387. }
  1388. if ($stop_test_after > 0 && !$booted && !$bug) {
  1389. if (time - $monitor_start > $stop_test_after) {
  1390. doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
  1391. $done = 1;
  1392. }
  1393. }
  1394. }
  1395. close(DMESG);
  1396. if ($bug) {
  1397. return 0 if ($in_bisect);
  1398. fail "failed - got a bug report" and return 0;
  1399. }
  1400. if (!$booted) {
  1401. return 0 if ($in_bisect);
  1402. fail "failed - never got a boot prompt." and return 0;
  1403. }
  1404. if ($bug_ignored) {
  1405. doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
  1406. }
  1407. return 1;
  1408. }
  1409. sub eval_kernel_version {
  1410. my ($option) = @_;
  1411. $option =~ s/\$KERNEL_VERSION/$version/g;
  1412. return $option;
  1413. }
  1414. sub do_post_install {
  1415. return if (!defined($post_install));
  1416. my $cp_post_install = eval_kernel_version $post_install;
  1417. run_command "$cp_post_install" or
  1418. dodie "Failed to run post install";
  1419. }
  1420. sub install {
  1421. return if ($no_install);
  1422. if (defined($pre_install)) {
  1423. my $cp_pre_install = eval_kernel_version $pre_install;
  1424. run_command "$cp_pre_install" or
  1425. dodie "Failed to run pre install";
  1426. }
  1427. my $cp_target = eval_kernel_version $target_image;
  1428. run_scp_install "$outputdir/$build_target", "$cp_target" or
  1429. dodie "failed to copy image";
  1430. my $install_mods = 0;
  1431. # should we process modules?
  1432. $install_mods = 0;
  1433. open(IN, "$output_config") or dodie("Can't read config file");
  1434. while (<IN>) {
  1435. if (/CONFIG_MODULES(=y)?/) {
  1436. $install_mods = 1 if (defined($1));
  1437. last;
  1438. }
  1439. }
  1440. close(IN);
  1441. if (!$install_mods) {
  1442. do_post_install;
  1443. doprint "No modules needed\n";
  1444. return;
  1445. }
  1446. run_command "$make INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=$tmpdir modules_install" or
  1447. dodie "Failed to install modules";
  1448. my $modlib = "/lib/modules/$version";
  1449. my $modtar = "ktest-mods.tar.bz2";
  1450. run_ssh "rm -rf $modlib" or
  1451. dodie "failed to remove old mods: $modlib";
  1452. # would be nice if scp -r did not follow symbolic links
  1453. run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
  1454. dodie "making tarball";
  1455. run_scp_mod "$tmpdir/$modtar", "/tmp" or
  1456. dodie "failed to copy modules";
  1457. unlink "$tmpdir/$modtar";
  1458. run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
  1459. dodie "failed to tar modules";
  1460. run_ssh "rm -f /tmp/$modtar";
  1461. do_post_install;
  1462. }
  1463. sub get_version {
  1464. # get the release name
  1465. return if ($have_version);
  1466. doprint "$make kernelrelease ... ";
  1467. $version = `$make kernelrelease | tail -1`;
  1468. chomp($version);
  1469. doprint "$version\n";
  1470. $have_version = 1;
  1471. }
  1472. sub start_monitor_and_boot {
  1473. # Make sure the stable kernel has finished booting
  1474. start_monitor;
  1475. wait_for_monitor 5;
  1476. end_monitor;
  1477. get_grub_index;
  1478. get_version;
  1479. install;
  1480. start_monitor;
  1481. return monitor;
  1482. }
  1483. sub check_buildlog {
  1484. my ($patch) = @_;
  1485. my @files = `git show $patch | diffstat -l`;
  1486. open(IN, "git show $patch |") or
  1487. dodie "failed to show $patch";
  1488. while (<IN>) {
  1489. if (m,^--- a/(.*),) {
  1490. chomp $1;
  1491. $files[$#files] = $1;
  1492. }
  1493. }
  1494. close(IN);
  1495. open(IN, $buildlog) or dodie "Can't open $buildlog";
  1496. while (<IN>) {
  1497. if (/^\s*(.*?):.*(warning|error)/) {
  1498. my $err = $1;
  1499. foreach my $file (@files) {
  1500. my $fullpath = "$builddir/$file";
  1501. if ($file eq $err || $fullpath eq $err) {
  1502. fail "$file built with warnings" and return 0;
  1503. }
  1504. }
  1505. }
  1506. }
  1507. close(IN);
  1508. return 1;
  1509. }
  1510. sub apply_min_config {
  1511. my $outconfig = "$output_config.new";
  1512. # Read the config file and remove anything that
  1513. # is in the force_config hash (from minconfig and others)
  1514. # then add the force config back.
  1515. doprint "Applying minimum configurations into $output_config.new\n";
  1516. open (OUT, ">$outconfig") or
  1517. dodie "Can't create $outconfig";
  1518. if (-f $output_config) {
  1519. open (IN, $output_config) or
  1520. dodie "Failed to open $output_config";
  1521. while (<IN>) {
  1522. if (/^(# )?(CONFIG_[^\s=]*)/) {
  1523. next if (defined($force_config{$2}));
  1524. }
  1525. print OUT;
  1526. }
  1527. close IN;
  1528. }
  1529. foreach my $config (keys %force_config) {
  1530. print OUT "$force_config{$config}\n";
  1531. }
  1532. close OUT;
  1533. run_command "mv $outconfig $output_config";
  1534. }
  1535. sub make_oldconfig {
  1536. my @force_list = keys %force_config;
  1537. if ($#force_list >= 0) {
  1538. apply_min_config;
  1539. }
  1540. if (!run_command "$make oldnoconfig") {
  1541. # Perhaps oldnoconfig doesn't exist in this version of the kernel
  1542. # try a yes '' | oldconfig
  1543. doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
  1544. run_command "yes '' | $make oldconfig" or
  1545. dodie "failed make config oldconfig";
  1546. }
  1547. }
  1548. # read a config file and use this to force new configs.
  1549. sub load_force_config {
  1550. my ($config) = @_;
  1551. doprint "Loading force configs from $config\n";
  1552. open(IN, $config) or
  1553. dodie "failed to read $config";
  1554. while (<IN>) {
  1555. chomp;
  1556. if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
  1557. $force_config{$1} = $_;
  1558. } elsif (/^# (CONFIG_\S*) is not set/) {
  1559. $force_config{$1} = $_;
  1560. }
  1561. }
  1562. close IN;
  1563. }
  1564. sub build {
  1565. my ($type) = @_;
  1566. unlink $buildlog;
  1567. # Failed builds should not reboot the target
  1568. my $save_no_reboot = $no_reboot;
  1569. $no_reboot = 1;
  1570. # Calculate a new version from here.
  1571. $have_version = 0;
  1572. if (defined($pre_build)) {
  1573. my $ret = run_command $pre_build;
  1574. if (!$ret && defined($pre_build_die) &&
  1575. $pre_build_die) {
  1576. dodie "failed to pre_build\n";
  1577. }
  1578. }
  1579. if ($type =~ /^useconfig:(.*)/) {
  1580. run_command "cp $1 $output_config" or
  1581. dodie "could not copy $1 to .config";
  1582. $type = "oldconfig";
  1583. }
  1584. # old config can ask questions
  1585. if ($type eq "oldconfig") {
  1586. $type = "oldnoconfig";
  1587. # allow for empty configs
  1588. run_command "touch $output_config";
  1589. if (!$noclean) {
  1590. run_command "mv $output_config $outputdir/config_temp" or
  1591. dodie "moving .config";
  1592. run_command "$make mrproper" or dodie "make mrproper";
  1593. run_command "mv $outputdir/config_temp $output_config" or
  1594. dodie "moving config_temp";
  1595. }
  1596. } elsif (!$noclean) {
  1597. unlink "$output_config";
  1598. run_command "$make mrproper" or
  1599. dodie "make mrproper";
  1600. }
  1601. # add something to distinguish this build
  1602. open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
  1603. print OUT "$localversion\n";
  1604. close(OUT);
  1605. if (defined($minconfig)) {
  1606. load_force_config($minconfig);
  1607. }
  1608. if ($type ne "oldnoconfig") {
  1609. run_command "$make $type" or
  1610. dodie "failed make config";
  1611. }
  1612. # Run old config regardless, to enforce min configurations
  1613. make_oldconfig;
  1614. $redirect = "$buildlog";
  1615. my $build_ret = run_command "$make $build_options";
  1616. undef $redirect;
  1617. if (defined($post_build)) {
  1618. # Because a post build may change the kernel version
  1619. # do it now.
  1620. get_version;
  1621. my $ret = run_command $post_build;
  1622. if (!$ret && defined($post_build_die) &&
  1623. $post_build_die) {
  1624. dodie "failed to post_build\n";
  1625. }
  1626. }
  1627. if (!$build_ret) {
  1628. # bisect may need this to pass
  1629. if ($in_bisect) {
  1630. $no_reboot = $save_no_reboot;
  1631. return 0;
  1632. }
  1633. fail "failed build" and return 0;
  1634. }
  1635. $no_reboot = $save_no_reboot;
  1636. return 1;
  1637. }
  1638. sub halt {
  1639. if (!run_ssh "halt" or defined($power_off)) {
  1640. if (defined($poweroff_after_halt)) {
  1641. sleep $poweroff_after_halt;
  1642. run_command "$power_off";
  1643. }
  1644. } else {
  1645. # nope? the zap it!
  1646. run_command "$power_off";
  1647. }
  1648. }
  1649. sub success {
  1650. my ($i) = @_;
  1651. if (defined($post_test)) {
  1652. run_command $post_test;
  1653. }
  1654. $successes++;
  1655. my $name = "";
  1656. if (defined($test_name)) {
  1657. $name = " ($test_name)";
  1658. }
  1659. doprint "\n\n*******************************************\n";
  1660. doprint "*******************************************\n";
  1661. doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
  1662. doprint "*******************************************\n";
  1663. doprint "*******************************************\n";
  1664. if (defined($store_successes)) {
  1665. save_logs "success", $store_successes;
  1666. }
  1667. if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
  1668. doprint "Reboot and wait $sleep_time seconds\n";
  1669. reboot_to_good $sleep_time;
  1670. }
  1671. }
  1672. sub answer_bisect {
  1673. for (;;) {
  1674. doprint "Pass or fail? [p/f]";
  1675. my $ans = <STDIN>;
  1676. chomp $ans;
  1677. if ($ans eq "p" || $ans eq "P") {
  1678. return 1;
  1679. } elsif ($ans eq "f" || $ans eq "F") {
  1680. return 0;
  1681. } else {
  1682. print "Please answer 'P' or 'F'\n";
  1683. }
  1684. }
  1685. }
  1686. sub child_run_test {
  1687. my $failed = 0;
  1688. # child should have no power
  1689. $reboot_on_error = 0;
  1690. $poweroff_on_error = 0;
  1691. $die_on_failure = 1;
  1692. $redirect = "$testlog";
  1693. run_command $run_test or $failed = 1;
  1694. undef $redirect;
  1695. exit $failed;
  1696. }
  1697. my $child_done;
  1698. sub child_finished {
  1699. $child_done = 1;
  1700. }
  1701. sub do_run_test {
  1702. my $child_pid;
  1703. my $child_exit;
  1704. my $line;
  1705. my $full_line;
  1706. my $bug = 0;
  1707. my $bug_ignored = 0;
  1708. wait_for_monitor 1;
  1709. doprint "run test $run_test\n";
  1710. $child_done = 0;
  1711. $SIG{CHLD} = qw(child_finished);
  1712. $child_pid = fork;
  1713. child_run_test if (!$child_pid);
  1714. $full_line = "";
  1715. do {
  1716. $line = wait_for_input($monitor_fp, 1);
  1717. if (defined($line)) {
  1718. # we are not guaranteed to get a full line
  1719. $full_line .= $line;
  1720. doprint $line;
  1721. if ($full_line =~ /call trace:/i) {
  1722. if ($ignore_errors) {
  1723. $bug_ignored = 1;
  1724. } else {
  1725. $bug = 1;
  1726. }
  1727. }
  1728. if ($full_line =~ /Kernel panic -/) {
  1729. $bug = 1;
  1730. }
  1731. if ($line =~ /\n/) {
  1732. $full_line = "";
  1733. }
  1734. }
  1735. } while (!$child_done && !$bug);
  1736. if (!$bug && $bug_ignored) {
  1737. doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
  1738. }
  1739. if ($bug) {
  1740. my $failure_start = time;
  1741. my $now;
  1742. do {
  1743. $line = wait_for_input($monitor_fp, 1);
  1744. if (defined($line)) {
  1745. doprint $line;
  1746. }
  1747. $now = time;
  1748. if ($now - $failure_start >= $stop_after_failure) {
  1749. last;
  1750. }
  1751. } while (defined($line));
  1752. doprint "Detected kernel crash!\n";
  1753. # kill the child with extreme prejudice
  1754. kill 9, $child_pid;
  1755. }
  1756. waitpid $child_pid, 0;
  1757. $child_exit = $?;
  1758. if (!$bug && $in_bisect) {
  1759. if (defined($bisect_ret_good)) {
  1760. if ($child_exit == $bisect_ret_good) {
  1761. return 1;
  1762. }
  1763. }
  1764. if (defined($bisect_ret_skip)) {
  1765. if ($child_exit == $bisect_ret_skip) {
  1766. return -1;
  1767. }
  1768. }
  1769. if (defined($bisect_ret_abort)) {
  1770. if ($child_exit == $bisect_ret_abort) {
  1771. fail "test abort" and return -2;
  1772. }
  1773. }
  1774. if (defined($bisect_ret_bad)) {
  1775. if ($child_exit == $bisect_ret_skip) {
  1776. return 0;
  1777. }
  1778. }
  1779. if (defined($bisect_ret_default)) {
  1780. if ($bisect_ret_default eq "good") {
  1781. return 1;
  1782. } elsif ($bisect_ret_default eq "bad") {
  1783. return 0;
  1784. } elsif ($bisect_ret_default eq "skip") {
  1785. return -1;
  1786. } elsif ($bisect_ret_default eq "abort") {
  1787. return -2;
  1788. } else {
  1789. fail "unknown default action: $bisect_ret_default"
  1790. and return -2;
  1791. }
  1792. }
  1793. }
  1794. if ($bug || $child_exit) {
  1795. return 0 if $in_bisect;
  1796. fail "test failed" and return 0;
  1797. }
  1798. return 1;
  1799. }
  1800. sub run_git_bisect {
  1801. my ($command) = @_;
  1802. doprint "$command ... ";
  1803. my $output = `$command 2>&1`;
  1804. my $ret = $?;
  1805. logit $output;
  1806. if ($ret) {
  1807. doprint "FAILED\n";
  1808. dodie "Failed to git bisect";
  1809. }
  1810. doprint "SUCCESS\n";
  1811. if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
  1812. doprint "$1 [$2]\n";
  1813. } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
  1814. $bisect_bad_commit = $1;
  1815. doprint "Found bad commit... $1\n";
  1816. return 0;
  1817. } else {
  1818. # we already logged it, just print it now.
  1819. print $output;
  1820. }
  1821. return 1;
  1822. }
  1823. sub bisect_reboot {
  1824. doprint "Reboot and sleep $bisect_sleep_time seconds\n";
  1825. reboot_to_good $bisect_sleep_time;
  1826. }
  1827. # returns 1 on success, 0 on failure, -1 on skip
  1828. sub run_bisect_test {
  1829. my ($type, $buildtype) = @_;
  1830. my $failed = 0;
  1831. my $result;
  1832. my $output;
  1833. my $ret;
  1834. $in_bisect = 1;
  1835. build $buildtype or $failed = 1;
  1836. if ($type ne "build") {
  1837. if ($failed && $bisect_skip) {
  1838. $in_bisect = 0;
  1839. return -1;
  1840. }
  1841. dodie "Failed on build" if $failed;
  1842. # Now boot the box
  1843. start_monitor_and_boot or $failed = 1;
  1844. if ($type ne "boot") {
  1845. if ($failed && $bisect_skip) {
  1846. end_monitor;
  1847. bisect_reboot;
  1848. $in_bisect = 0;
  1849. return -1;
  1850. }
  1851. dodie "Failed on boot" if $failed;
  1852. do_run_test or $failed = 1;
  1853. }
  1854. end_monitor;
  1855. }
  1856. if ($failed) {
  1857. $result = 0;
  1858. } else {
  1859. $result = 1;
  1860. }
  1861. # reboot the box to a kernel we can ssh to
  1862. if ($type ne "build") {
  1863. bisect_reboot;
  1864. }
  1865. $in_bisect = 0;
  1866. return $result;
  1867. }
  1868. sub run_bisect {
  1869. my ($type) = @_;
  1870. my $buildtype = "oldconfig";
  1871. # We should have a minconfig to use?
  1872. if (defined($minconfig)) {
  1873. $buildtype = "useconfig:$minconfig";
  1874. }
  1875. my $ret = run_bisect_test $type, $buildtype;
  1876. if ($bisect_manual) {
  1877. $ret = answer_bisect;
  1878. }
  1879. # Are we looking for where it worked, not failed?
  1880. if ($reverse_bisect && $ret >= 0) {
  1881. $ret = !$ret;
  1882. }
  1883. if ($ret > 0) {
  1884. return "good";
  1885. } elsif ($ret == 0) {
  1886. return "bad";
  1887. } elsif ($bisect_skip) {
  1888. doprint "HIT A BAD COMMIT ... SKIPPING\n";
  1889. return "skip";
  1890. }
  1891. }
  1892. sub update_bisect_replay {
  1893. my $tmp_log = "$tmpdir/ktest_bisect_log";
  1894. run_command "git bisect log > $tmp_log" or
  1895. die "can't create bisect log";
  1896. return $tmp_log;
  1897. }
  1898. sub bisect {
  1899. my ($i) = @_;
  1900. my $result;
  1901. die "BISECT_GOOD[$i] not defined\n" if (!defined($bisect_good));
  1902. die "BISECT_BAD[$i] not defined\n" if (!defined($bisect_bad));
  1903. die "BISECT_TYPE[$i] not defined\n" if (!defined($bisect_type));
  1904. my $good = $bisect_good;
  1905. my $bad = $bisect_bad;
  1906. my $type = $bisect_type;
  1907. my $start = $bisect_start;
  1908. my $replay = $bisect_replay;
  1909. my $start_files = $bisect_files;
  1910. if (defined($start_files)) {
  1911. $start_files = " -- " . $start_files;
  1912. } else {
  1913. $start_files = "";
  1914. }
  1915. # convert to true sha1's
  1916. $good = get_sha1($good);
  1917. $bad = get_sha1($bad);
  1918. if (defined($bisect_reverse) && $bisect_reverse == 1) {
  1919. doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
  1920. $reverse_bisect = 1;
  1921. } else {
  1922. $reverse_bisect = 0;
  1923. }
  1924. # Can't have a test without having a test to run
  1925. if ($type eq "test" && !defined($run_test)) {
  1926. $type = "boot";
  1927. }
  1928. # Check if a bisect was running
  1929. my $bisect_start_file = "$builddir/.git/BISECT_START";
  1930. my $check = $bisect_check;
  1931. my $do_check = defined($check) && $check ne "0";
  1932. if ( -f $bisect_start_file ) {
  1933. print "Bisect in progress found\n";
  1934. if ($do_check) {
  1935. print " If you say yes, then no checks of good or bad will be done\n";
  1936. }
  1937. if (defined($replay)) {
  1938. print "** BISECT_REPLAY is defined in config file **";
  1939. print " Ignore config option and perform new git bisect log?\n";
  1940. if (read_ync " (yes, no, or cancel) ") {
  1941. $replay = update_bisect_replay;
  1942. $do_check = 0;
  1943. }
  1944. } elsif (read_yn "read git log and continue?") {
  1945. $replay = update_bisect_replay;
  1946. $do_check = 0;
  1947. }
  1948. }
  1949. if ($do_check) {
  1950. # get current HEAD
  1951. my $head = get_sha1("HEAD");
  1952. if ($check ne "good") {
  1953. doprint "TESTING BISECT BAD [$bad]\n";
  1954. run_command "git checkout $bad" or
  1955. die "Failed to checkout $bad";
  1956. $result = run_bisect $type;
  1957. if ($result ne "bad") {
  1958. fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
  1959. }
  1960. }
  1961. if ($check ne "bad") {
  1962. doprint "TESTING BISECT GOOD [$good]\n";
  1963. run_command "git checkout $good" or
  1964. die "Failed to checkout $good";
  1965. $result = run_bisect $type;
  1966. if ($result ne "good") {
  1967. fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
  1968. }
  1969. }
  1970. # checkout where we started
  1971. run_command "git checkout $head" or
  1972. die "Failed to checkout $head";
  1973. }
  1974. run_command "git bisect start$start_files" or
  1975. dodie "could not start bisect";
  1976. run_command "git bisect good $good" or
  1977. dodie "could not set bisect good to $good";
  1978. run_git_bisect "git bisect bad $bad" or
  1979. dodie "could not set bisect bad to $bad";
  1980. if (defined($replay)) {
  1981. run_command "git bisect replay $replay" or
  1982. dodie "failed to run replay";
  1983. }
  1984. if (defined($start)) {
  1985. run_command "git checkout $start" or
  1986. dodie "failed to checkout $start";
  1987. }
  1988. my $test;
  1989. do {
  1990. $result = run_bisect $type;
  1991. $test = run_git_bisect "git bisect $result";
  1992. } while ($test);
  1993. run_command "git bisect log" or
  1994. dodie "could not capture git bisect log";
  1995. run_command "git bisect reset" or
  1996. dodie "could not reset git bisect";
  1997. doprint "Bad commit was [$bisect_bad_commit]\n";
  1998. success $i;
  1999. }
  2000. # config_ignore holds the configs that were set (or unset) for
  2001. # a good config and we will ignore these configs for the rest
  2002. # of a config bisect. These configs stay as they were.
  2003. my %config_ignore;
  2004. # config_set holds what all configs were set as.
  2005. my %config_set;
  2006. # config_off holds the set of configs that the bad config had disabled.
  2007. # We need to record them and set them in the .config when running
  2008. # oldnoconfig, because oldnoconfig does not turn off new symbols, but
  2009. # instead just keeps the defaults.
  2010. my %config_off;
  2011. # config_off_tmp holds a set of configs to turn off for now
  2012. my @config_off_tmp;
  2013. # config_list is the set of configs that are being tested
  2014. my %config_list;
  2015. my %null_config;
  2016. my %dependency;
  2017. sub assign_configs {
  2018. my ($hash, $config) = @_;
  2019. open (IN, $config)
  2020. or dodie "Failed to read $config";
  2021. while (<IN>) {
  2022. if (/^((CONFIG\S*)=.*)/) {
  2023. ${$hash}{$2} = $1;
  2024. }
  2025. }
  2026. close(IN);
  2027. }
  2028. sub process_config_ignore {
  2029. my ($config) = @_;
  2030. assign_configs \%config_ignore, $config;
  2031. }
  2032. sub read_current_config {
  2033. my ($config_ref) = @_;
  2034. %{$config_ref} = ();
  2035. undef %{$config_ref};
  2036. my @key = keys %{$config_ref};
  2037. if ($#key >= 0) {
  2038. print "did not delete!\n";
  2039. exit;
  2040. }
  2041. open (IN, "$output_config");
  2042. while (<IN>) {
  2043. if (/^(CONFIG\S+)=(.*)/) {
  2044. ${$config_ref}{$1} = $2;
  2045. }
  2046. }
  2047. close(IN);
  2048. }
  2049. sub get_dependencies {
  2050. my ($config) = @_;
  2051. my $arr = $dependency{$config};
  2052. if (!defined($arr)) {
  2053. return ();
  2054. }
  2055. my @deps = @{$arr};
  2056. foreach my $dep (@{$arr}) {
  2057. print "ADD DEP $dep\n";
  2058. @deps = (@deps, get_dependencies $dep);
  2059. }
  2060. return @deps;
  2061. }
  2062. sub create_config {
  2063. my @configs = @_;
  2064. open(OUT, ">$output_config") or dodie "Can not write to $output_config";
  2065. foreach my $config (@configs) {
  2066. print OUT "$config_set{$config}\n";
  2067. my @deps = get_dependencies $config;
  2068. foreach my $dep (@deps) {
  2069. print OUT "$config_set{$dep}\n";
  2070. }
  2071. }
  2072. # turn off configs to keep off
  2073. foreach my $config (keys %config_off) {
  2074. print OUT "# $config is not set\n";
  2075. }
  2076. # turn off configs that should be off for now
  2077. foreach my $config (@config_off_tmp) {
  2078. print OUT "# $config is not set\n";
  2079. }
  2080. foreach my $config (keys %config_ignore) {
  2081. print OUT "$config_ignore{$config}\n";
  2082. }
  2083. close(OUT);
  2084. make_oldconfig;
  2085. }
  2086. sub compare_configs {
  2087. my (%a, %b) = @_;
  2088. foreach my $item (keys %a) {
  2089. if (!defined($b{$item})) {
  2090. print "diff $item\n";
  2091. return 1;
  2092. }
  2093. delete $b{$item};
  2094. }
  2095. my @keys = keys %b;
  2096. if ($#keys) {
  2097. print "diff2 $keys[0]\n";
  2098. }
  2099. return -1 if ($#keys >= 0);
  2100. return 0;
  2101. }
  2102. sub run_config_bisect_test {
  2103. my ($type) = @_;
  2104. return run_bisect_test $type, "oldconfig";
  2105. }
  2106. sub process_passed {
  2107. my (%configs) = @_;
  2108. doprint "These configs had no failure: (Enabling them for further compiles)\n";
  2109. # Passed! All these configs are part of a good compile.
  2110. # Add them to the min options.
  2111. foreach my $config (keys %configs) {
  2112. if (defined($config_list{$config})) {
  2113. doprint " removing $config\n";
  2114. $config_ignore{$config} = $config_list{$config};
  2115. delete $config_list{$config};
  2116. }
  2117. }
  2118. doprint "config copied to $outputdir/config_good\n";
  2119. run_command "cp -f $output_config $outputdir/config_good";
  2120. }
  2121. sub process_failed {
  2122. my ($config) = @_;
  2123. doprint "\n\n***************************************\n";
  2124. doprint "Found bad config: $config\n";
  2125. doprint "***************************************\n\n";
  2126. }
  2127. sub run_config_bisect {
  2128. my @start_list = keys %config_list;
  2129. if ($#start_list < 0) {
  2130. doprint "No more configs to test!!!\n";
  2131. return -1;
  2132. }
  2133. doprint "***** RUN TEST ***\n";
  2134. my $type = $config_bisect_type;
  2135. my $ret;
  2136. my %current_config;
  2137. my $count = $#start_list + 1;
  2138. doprint " $count configs to test\n";
  2139. my $half = int($#start_list / 2);
  2140. do {
  2141. my @tophalf = @start_list[0 .. $half];
  2142. # keep the bottom half off
  2143. if ($half < $#start_list) {
  2144. @config_off_tmp = @start_list[$half + 1 .. $#start_list];
  2145. } else {
  2146. @config_off_tmp = ();
  2147. }
  2148. create_config @tophalf;
  2149. read_current_config \%current_config;
  2150. $count = $#tophalf + 1;
  2151. doprint "Testing $count configs\n";
  2152. my $found = 0;
  2153. # make sure we test something
  2154. foreach my $config (@tophalf) {
  2155. if (defined($current_config{$config})) {
  2156. logit " $config\n";
  2157. $found = 1;
  2158. }
  2159. }
  2160. if (!$found) {
  2161. # try the other half
  2162. doprint "Top half produced no set configs, trying bottom half\n";
  2163. # keep the top half off
  2164. @config_off_tmp = @tophalf;
  2165. @tophalf = @start_list[$half + 1 .. $#start_list];
  2166. create_config @tophalf;
  2167. read_current_config \%current_config;
  2168. foreach my $config (@tophalf) {
  2169. if (defined($current_config{$config})) {
  2170. logit " $config\n";
  2171. $found = 1;
  2172. }
  2173. }
  2174. if (!$found) {
  2175. doprint "Failed: Can't make new config with current configs\n";
  2176. foreach my $config (@start_list) {
  2177. doprint " CONFIG: $config\n";
  2178. }
  2179. return -1;
  2180. }
  2181. $count = $#tophalf + 1;
  2182. doprint "Testing $count configs\n";
  2183. }
  2184. $ret = run_config_bisect_test $type;
  2185. if ($bisect_manual) {
  2186. $ret = answer_bisect;
  2187. }
  2188. if ($ret) {
  2189. process_passed %current_config;
  2190. return 0;
  2191. }
  2192. doprint "This config had a failure.\n";
  2193. doprint "Removing these configs that were not set in this config:\n";
  2194. doprint "config copied to $outputdir/config_bad\n";
  2195. run_command "cp -f $output_config $outputdir/config_bad";
  2196. # A config exists in this group that was bad.
  2197. foreach my $config (keys %config_list) {
  2198. if (!defined($current_config{$config})) {
  2199. doprint " removing $config\n";
  2200. delete $config_list{$config};
  2201. }
  2202. }
  2203. @start_list = @tophalf;
  2204. if ($#start_list == 0) {
  2205. process_failed $start_list[0];
  2206. return 1;
  2207. }
  2208. # remove half the configs we are looking at and see if
  2209. # they are good.
  2210. $half = int($#start_list / 2);
  2211. } while ($#start_list > 0);
  2212. # we found a single config, try it again unless we are running manually
  2213. if ($bisect_manual) {
  2214. process_failed $start_list[0];
  2215. return 1;
  2216. }
  2217. my @tophalf = @start_list[0 .. 0];
  2218. $ret = run_config_bisect_test $type;
  2219. if ($ret) {
  2220. process_passed %current_config;
  2221. return 0;
  2222. }
  2223. process_failed $start_list[0];
  2224. return 1;
  2225. }
  2226. sub config_bisect {
  2227. my ($i) = @_;
  2228. my $start_config = $config_bisect;
  2229. my $tmpconfig = "$tmpdir/use_config";
  2230. if (defined($config_bisect_good)) {
  2231. process_config_ignore $config_bisect_good;
  2232. }
  2233. # Make the file with the bad config and the min config
  2234. if (defined($minconfig)) {
  2235. # read the min config for things to ignore
  2236. run_command "cp $minconfig $tmpconfig" or
  2237. dodie "failed to copy $minconfig to $tmpconfig";
  2238. } else {
  2239. unlink $tmpconfig;
  2240. }
  2241. if (-f $tmpconfig) {
  2242. load_force_config($tmpconfig);
  2243. process_config_ignore $tmpconfig;
  2244. }
  2245. # now process the start config
  2246. run_command "cp $start_config $output_config" or
  2247. dodie "failed to copy $start_config to $output_config";
  2248. # read directly what we want to check
  2249. my %config_check;
  2250. open (IN, $output_config)
  2251. or dodie "failed to open $output_config";
  2252. while (<IN>) {
  2253. if (/^((CONFIG\S*)=.*)/) {
  2254. $config_check{$2} = $1;
  2255. }
  2256. }
  2257. close(IN);
  2258. # Now run oldconfig with the minconfig
  2259. make_oldconfig;
  2260. # check to see what we lost (or gained)
  2261. open (IN, $output_config)
  2262. or dodie "Failed to read $start_config";
  2263. my %removed_configs;
  2264. my %added_configs;
  2265. while (<IN>) {
  2266. if (/^((CONFIG\S*)=.*)/) {
  2267. # save off all options
  2268. $config_set{$2} = $1;
  2269. if (defined($config_check{$2})) {
  2270. if (defined($config_ignore{$2})) {
  2271. $removed_configs{$2} = $1;
  2272. } else {
  2273. $config_list{$2} = $1;
  2274. }
  2275. } elsif (!defined($config_ignore{$2})) {
  2276. $added_configs{$2} = $1;
  2277. $config_list{$2} = $1;
  2278. }
  2279. } elsif (/^# ((CONFIG\S*).*)/) {
  2280. # Keep these configs disabled
  2281. $config_set{$2} = $1;
  2282. $config_off{$2} = $1;
  2283. }
  2284. }
  2285. close(IN);
  2286. my @confs = keys %removed_configs;
  2287. if ($#confs >= 0) {
  2288. doprint "Configs overridden by default configs and removed from check:\n";
  2289. foreach my $config (@confs) {
  2290. doprint " $config\n";
  2291. }
  2292. }
  2293. @confs = keys %added_configs;
  2294. if ($#confs >= 0) {
  2295. doprint "Configs appearing in make oldconfig and added:\n";
  2296. foreach my $config (@confs) {
  2297. doprint " $config\n";
  2298. }
  2299. }
  2300. my %config_test;
  2301. my $once = 0;
  2302. @config_off_tmp = ();
  2303. # Sometimes kconfig does weird things. We must make sure
  2304. # that the config we autocreate has everything we need
  2305. # to test, otherwise we may miss testing configs, or
  2306. # may not be able to create a new config.
  2307. # Here we create a config with everything set.
  2308. create_config (keys %config_list);
  2309. read_current_config \%config_test;
  2310. foreach my $config (keys %config_list) {
  2311. if (!defined($config_test{$config})) {
  2312. if (!$once) {
  2313. $once = 1;
  2314. doprint "Configs not produced by kconfig (will not be checked):\n";
  2315. }
  2316. doprint " $config\n";
  2317. delete $config_list{$config};
  2318. }
  2319. }
  2320. my $ret;
  2321. if (defined($config_bisect_check) && $config_bisect_check) {
  2322. doprint " Checking to make sure bad config with min config fails\n";
  2323. create_config keys %config_list;
  2324. $ret = run_config_bisect_test $config_bisect_type;
  2325. if ($ret) {
  2326. doprint " FAILED! Bad config with min config boots fine\n";
  2327. return -1;
  2328. }
  2329. doprint " Bad config with min config fails as expected\n";
  2330. }
  2331. do {
  2332. $ret = run_config_bisect;
  2333. } while (!$ret);
  2334. return $ret if ($ret < 0);
  2335. success $i;
  2336. }
  2337. sub patchcheck_reboot {
  2338. doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
  2339. reboot_to_good $patchcheck_sleep_time;
  2340. }
  2341. sub patchcheck {
  2342. my ($i) = @_;
  2343. die "PATCHCHECK_START[$i] not defined\n"
  2344. if (!defined($patchcheck_start));
  2345. die "PATCHCHECK_TYPE[$i] not defined\n"
  2346. if (!defined($patchcheck_type));
  2347. my $start = $patchcheck_start;
  2348. my $end = "HEAD";
  2349. if (defined($patchcheck_end)) {
  2350. $end = $patchcheck_end;
  2351. }
  2352. # Get the true sha1's since we can use things like HEAD~3
  2353. $start = get_sha1($start);
  2354. $end = get_sha1($end);
  2355. my $type = $patchcheck_type;
  2356. # Can't have a test without having a test to run
  2357. if ($type eq "test" && !defined($run_test)) {
  2358. $type = "boot";
  2359. }
  2360. open (IN, "git log --pretty=oneline $end|") or
  2361. dodie "could not get git list";
  2362. my @list;
  2363. while (<IN>) {
  2364. chomp;
  2365. $list[$#list+1] = $_;
  2366. last if (/^$start/);
  2367. }
  2368. close(IN);
  2369. if ($list[$#list] !~ /^$start/) {
  2370. fail "SHA1 $start not found";
  2371. }
  2372. # go backwards in the list
  2373. @list = reverse @list;
  2374. my $save_clean = $noclean;
  2375. my %ignored_warnings;
  2376. if (defined($ignore_warnings)) {
  2377. foreach my $sha1 (split /\s+/, $ignore_warnings) {
  2378. $ignored_warnings{$sha1} = 1;
  2379. }
  2380. }
  2381. $in_patchcheck = 1;
  2382. foreach my $item (@list) {
  2383. my $sha1 = $item;
  2384. $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
  2385. doprint "\nProcessing commit $item\n\n";
  2386. run_command "git checkout $sha1" or
  2387. die "Failed to checkout $sha1";
  2388. # only clean on the first and last patch
  2389. if ($item eq $list[0] ||
  2390. $item eq $list[$#list]) {
  2391. $noclean = $save_clean;
  2392. } else {
  2393. $noclean = 1;
  2394. }
  2395. if (defined($minconfig)) {
  2396. build "useconfig:$minconfig" or return 0;
  2397. } else {
  2398. # ?? no config to use?
  2399. build "oldconfig" or return 0;
  2400. }
  2401. if (!defined($ignored_warnings{$sha1})) {
  2402. check_buildlog $sha1 or return 0;
  2403. }
  2404. next if ($type eq "build");
  2405. my $failed = 0;
  2406. start_monitor_and_boot or $failed = 1;
  2407. if (!$failed && $type ne "boot"){
  2408. do_run_test or $failed = 1;
  2409. }
  2410. end_monitor;
  2411. return 0 if ($failed);
  2412. patchcheck_reboot;
  2413. }
  2414. $in_patchcheck = 0;
  2415. success $i;
  2416. return 1;
  2417. }
  2418. my %depends;
  2419. my %depcount;
  2420. my $iflevel = 0;
  2421. my @ifdeps;
  2422. # prevent recursion
  2423. my %read_kconfigs;
  2424. sub add_dep {
  2425. # $config depends on $dep
  2426. my ($config, $dep) = @_;
  2427. if (defined($depends{$config})) {
  2428. $depends{$config} .= " " . $dep;
  2429. } else {
  2430. $depends{$config} = $dep;
  2431. }
  2432. # record the number of configs depending on $dep
  2433. if (defined $depcount{$dep}) {
  2434. $depcount{$dep}++;
  2435. } else {
  2436. $depcount{$dep} = 1;
  2437. }
  2438. }
  2439. # taken from streamline_config.pl
  2440. sub read_kconfig {
  2441. my ($kconfig) = @_;
  2442. my $state = "NONE";
  2443. my $config;
  2444. my @kconfigs;
  2445. my $cont = 0;
  2446. my $line;
  2447. if (! -f $kconfig) {
  2448. doprint "file $kconfig does not exist, skipping\n";
  2449. return;
  2450. }
  2451. open(KIN, "$kconfig")
  2452. or die "Can't open $kconfig";
  2453. while (<KIN>) {
  2454. chomp;
  2455. # Make sure that lines ending with \ continue
  2456. if ($cont) {
  2457. $_ = $line . " " . $_;
  2458. }
  2459. if (s/\\$//) {
  2460. $cont = 1;
  2461. $line = $_;
  2462. next;
  2463. }
  2464. $cont = 0;
  2465. # collect any Kconfig sources
  2466. if (/^source\s*"(.*)"/) {
  2467. $kconfigs[$#kconfigs+1] = $1;
  2468. }
  2469. # configs found
  2470. if (/^\s*(menu)?config\s+(\S+)\s*$/) {
  2471. $state = "NEW";
  2472. $config = $2;
  2473. for (my $i = 0; $i < $iflevel; $i++) {
  2474. add_dep $config, $ifdeps[$i];
  2475. }
  2476. # collect the depends for the config
  2477. } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
  2478. add_dep $config, $1;
  2479. # Get the configs that select this config
  2480. } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
  2481. # selected by depends on config
  2482. add_dep $1, $config;
  2483. # Check for if statements
  2484. } elsif (/^if\s+(.*\S)\s*$/) {
  2485. my $deps = $1;
  2486. # remove beginning and ending non text
  2487. $deps =~ s/^[^a-zA-Z0-9_]*//;
  2488. $deps =~ s/[^a-zA-Z0-9_]*$//;
  2489. my @deps = split /[^a-zA-Z0-9_]+/, $deps;
  2490. $ifdeps[$iflevel++] = join ':', @deps;
  2491. } elsif (/^endif/) {
  2492. $iflevel-- if ($iflevel);
  2493. # stop on "help"
  2494. } elsif (/^\s*help\s*$/) {
  2495. $state = "NONE";
  2496. }
  2497. }
  2498. close(KIN);
  2499. # read in any configs that were found.
  2500. foreach $kconfig (@kconfigs) {
  2501. if (!defined($read_kconfigs{$kconfig})) {
  2502. $read_kconfigs{$kconfig} = 1;
  2503. read_kconfig("$builddir/$kconfig");
  2504. }
  2505. }
  2506. }
  2507. sub read_depends {
  2508. # find out which arch this is by the kconfig file
  2509. open (IN, $output_config)
  2510. or dodie "Failed to read $output_config";
  2511. my $arch;
  2512. while (<IN>) {
  2513. if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
  2514. $arch = $1;
  2515. last;
  2516. }
  2517. }
  2518. close IN;
  2519. if (!defined($arch)) {
  2520. doprint "Could not find arch from config file\n";
  2521. doprint "no dependencies used\n";
  2522. return;
  2523. }
  2524. # arch is really the subarch, we need to know
  2525. # what directory to look at.
  2526. if ($arch eq "i386" || $arch eq "x86_64") {
  2527. $arch = "x86";
  2528. } elsif ($arch =~ /^tile/) {
  2529. $arch = "tile";
  2530. }
  2531. my $kconfig = "$builddir/arch/$arch/Kconfig";
  2532. if (! -f $kconfig && $arch =~ /\d$/) {
  2533. my $orig = $arch;
  2534. # some subarchs have numbers, truncate them
  2535. $arch =~ s/\d*$//;
  2536. $kconfig = "$builddir/arch/$arch/Kconfig";
  2537. if (! -f $kconfig) {
  2538. doprint "No idea what arch dir $orig is for\n";
  2539. doprint "no dependencies used\n";
  2540. return;
  2541. }
  2542. }
  2543. read_kconfig($kconfig);
  2544. }
  2545. sub read_config_list {
  2546. my ($config) = @_;
  2547. open (IN, $config)
  2548. or dodie "Failed to read $config";
  2549. while (<IN>) {
  2550. if (/^((CONFIG\S*)=.*)/) {
  2551. if (!defined($config_ignore{$2})) {
  2552. $config_list{$2} = $1;
  2553. }
  2554. }
  2555. }
  2556. close(IN);
  2557. }
  2558. sub read_output_config {
  2559. my ($config) = @_;
  2560. assign_configs \%config_ignore, $config;
  2561. }
  2562. sub make_new_config {
  2563. my @configs = @_;
  2564. open (OUT, ">$output_config")
  2565. or dodie "Failed to write $output_config";
  2566. foreach my $config (@configs) {
  2567. print OUT "$config\n";
  2568. }
  2569. close OUT;
  2570. }
  2571. sub chomp_config {
  2572. my ($config) = @_;
  2573. $config =~ s/CONFIG_//;
  2574. return $config;
  2575. }
  2576. sub get_depends {
  2577. my ($dep) = @_;
  2578. my $kconfig = chomp_config $dep;
  2579. $dep = $depends{"$kconfig"};
  2580. # the dep string we have saves the dependencies as they
  2581. # were found, including expressions like ! && ||. We
  2582. # want to split this out into just an array of configs.
  2583. my $valid = "A-Za-z_0-9";
  2584. my @configs;
  2585. while ($dep =~ /[$valid]/) {
  2586. if ($dep =~ /^[^$valid]*([$valid]+)/) {
  2587. my $conf = "CONFIG_" . $1;
  2588. $configs[$#configs + 1] = $conf;
  2589. $dep =~ s/^[^$valid]*[$valid]+//;
  2590. } else {
  2591. die "this should never happen";
  2592. }
  2593. }
  2594. return @configs;
  2595. }
  2596. my %min_configs;
  2597. my %keep_configs;
  2598. my %save_configs;
  2599. my %processed_configs;
  2600. my %nochange_config;
  2601. sub test_this_config {
  2602. my ($config) = @_;
  2603. my $found;
  2604. # if we already processed this config, skip it
  2605. if (defined($processed_configs{$config})) {
  2606. return undef;
  2607. }
  2608. $processed_configs{$config} = 1;
  2609. # if this config failed during this round, skip it
  2610. if (defined($nochange_config{$config})) {
  2611. return undef;
  2612. }
  2613. my $kconfig = chomp_config $config;
  2614. # Test dependencies first
  2615. if (defined($depends{"$kconfig"})) {
  2616. my @parents = get_depends $config;
  2617. foreach my $parent (@parents) {
  2618. # if the parent is in the min config, check it first
  2619. next if (!defined($min_configs{$parent}));
  2620. $found = test_this_config($parent);
  2621. if (defined($found)) {
  2622. return $found;
  2623. }
  2624. }
  2625. }
  2626. # Remove this config from the list of configs
  2627. # do a make oldnoconfig and then read the resulting
  2628. # .config to make sure it is missing the config that
  2629. # we had before
  2630. my %configs = %min_configs;
  2631. delete $configs{$config};
  2632. make_new_config ((values %configs), (values %keep_configs));
  2633. make_oldconfig;
  2634. undef %configs;
  2635. assign_configs \%configs, $output_config;
  2636. return $config if (!defined($configs{$config}));
  2637. doprint "disabling config $config did not change .config\n";
  2638. $nochange_config{$config} = 1;
  2639. return undef;
  2640. }
  2641. sub make_min_config {
  2642. my ($i) = @_;
  2643. my $type = $minconfig_type;
  2644. if ($type ne "boot" && $type ne "test") {
  2645. fail "Invalid MIN_CONFIG_TYPE '$minconfig_type'\n" .
  2646. " make_min_config works only with 'boot' and 'test'\n" and return;
  2647. }
  2648. if (!defined($output_minconfig)) {
  2649. fail "OUTPUT_MIN_CONFIG not defined" and return;
  2650. }
  2651. # If output_minconfig exists, and the start_minconfig
  2652. # came from min_config, than ask if we should use
  2653. # that instead.
  2654. if (-f $output_minconfig && !$start_minconfig_defined) {
  2655. print "$output_minconfig exists\n";
  2656. if (!defined($use_output_minconfig)) {
  2657. if (read_yn " Use it as minconfig?") {
  2658. $start_minconfig = $output_minconfig;
  2659. }
  2660. } elsif ($use_output_minconfig > 0) {
  2661. doprint "Using $output_minconfig as MIN_CONFIG\n";
  2662. $start_minconfig = $output_minconfig;
  2663. } else {
  2664. doprint "Set to still use MIN_CONFIG as starting point\n";
  2665. }
  2666. }
  2667. if (!defined($start_minconfig)) {
  2668. fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
  2669. }
  2670. my $temp_config = "$tmpdir/temp_config";
  2671. # First things first. We build an allnoconfig to find
  2672. # out what the defaults are that we can't touch.
  2673. # Some are selections, but we really can't handle selections.
  2674. my $save_minconfig = $minconfig;
  2675. undef $minconfig;
  2676. run_command "$make allnoconfig" or return 0;
  2677. read_depends;
  2678. process_config_ignore $output_config;
  2679. undef %save_configs;
  2680. undef %min_configs;
  2681. if (defined($ignore_config)) {
  2682. # make sure the file exists
  2683. `touch $ignore_config`;
  2684. assign_configs \%save_configs, $ignore_config;
  2685. }
  2686. %keep_configs = %save_configs;
  2687. doprint "Load initial configs from $start_minconfig\n";
  2688. # Look at the current min configs, and save off all the
  2689. # ones that were set via the allnoconfig
  2690. assign_configs \%min_configs, $start_minconfig;
  2691. my @config_keys = keys %min_configs;
  2692. # All configs need a depcount
  2693. foreach my $config (@config_keys) {
  2694. my $kconfig = chomp_config $config;
  2695. if (!defined $depcount{$kconfig}) {
  2696. $depcount{$kconfig} = 0;
  2697. }
  2698. }
  2699. # Remove anything that was set by the make allnoconfig
  2700. # we shouldn't need them as they get set for us anyway.
  2701. foreach my $config (@config_keys) {
  2702. # Remove anything in the ignore_config
  2703. if (defined($keep_configs{$config})) {
  2704. my $file = $ignore_config;
  2705. $file =~ s,.*/(.*?)$,$1,;
  2706. doprint "$config set by $file ... ignored\n";
  2707. delete $min_configs{$config};
  2708. next;
  2709. }
  2710. # But make sure the settings are the same. If a min config
  2711. # sets a selection, we do not want to get rid of it if
  2712. # it is not the same as what we have. Just move it into
  2713. # the keep configs.
  2714. if (defined($config_ignore{$config})) {
  2715. if ($config_ignore{$config} ne $min_configs{$config}) {
  2716. doprint "$config is in allnoconfig as '$config_ignore{$config}'";
  2717. doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
  2718. $keep_configs{$config} = $min_configs{$config};
  2719. } else {
  2720. doprint "$config set by allnoconfig ... ignored\n";
  2721. }
  2722. delete $min_configs{$config};
  2723. }
  2724. }
  2725. my $done = 0;
  2726. my $take_two = 0;
  2727. while (!$done) {
  2728. my $config;
  2729. my $found;
  2730. # Now disable each config one by one and do a make oldconfig
  2731. # till we find a config that changes our list.
  2732. my @test_configs = keys %min_configs;
  2733. # Sort keys by who is most dependent on
  2734. @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
  2735. @test_configs ;
  2736. # Put configs that did not modify the config at the end.
  2737. my $reset = 1;
  2738. for (my $i = 0; $i < $#test_configs; $i++) {
  2739. if (!defined($nochange_config{$test_configs[0]})) {
  2740. $reset = 0;
  2741. last;
  2742. }
  2743. # This config didn't change the .config last time.
  2744. # Place it at the end
  2745. my $config = shift @test_configs;
  2746. push @test_configs, $config;
  2747. }
  2748. # if every test config has failed to modify the .config file
  2749. # in the past, then reset and start over.
  2750. if ($reset) {
  2751. undef %nochange_config;
  2752. }
  2753. undef %processed_configs;
  2754. foreach my $config (@test_configs) {
  2755. $found = test_this_config $config;
  2756. last if (defined($found));
  2757. # oh well, try another config
  2758. }
  2759. if (!defined($found)) {
  2760. # we could have failed due to the nochange_config hash
  2761. # reset and try again
  2762. if (!$take_two) {
  2763. undef %nochange_config;
  2764. $take_two = 1;
  2765. next;
  2766. }
  2767. doprint "No more configs found that we can disable\n";
  2768. $done = 1;
  2769. last;
  2770. }
  2771. $take_two = 0;
  2772. $config = $found;
  2773. doprint "Test with $config disabled\n";
  2774. # set in_bisect to keep build and monitor from dieing
  2775. $in_bisect = 1;
  2776. my $failed = 0;
  2777. build "oldconfig" or $failed = 1;
  2778. if (!$failed) {
  2779. start_monitor_and_boot or $failed = 1;
  2780. if ($type eq "test" && !$failed) {
  2781. do_run_test or $failed = 1;
  2782. }
  2783. end_monitor;
  2784. }
  2785. $in_bisect = 0;
  2786. if ($failed) {
  2787. doprint "$min_configs{$config} is needed to boot the box... keeping\n";
  2788. # this config is needed, add it to the ignore list.
  2789. $keep_configs{$config} = $min_configs{$config};
  2790. $save_configs{$config} = $min_configs{$config};
  2791. delete $min_configs{$config};
  2792. # update new ignore configs
  2793. if (defined($ignore_config)) {
  2794. open (OUT, ">$temp_config")
  2795. or die "Can't write to $temp_config";
  2796. foreach my $config (keys %save_configs) {
  2797. print OUT "$save_configs{$config}\n";
  2798. }
  2799. close OUT;
  2800. run_command "mv $temp_config $ignore_config" or
  2801. dodie "failed to copy update to $ignore_config";
  2802. }
  2803. } else {
  2804. # We booted without this config, remove it from the minconfigs.
  2805. doprint "$config is not needed, disabling\n";
  2806. delete $min_configs{$config};
  2807. # Also disable anything that is not enabled in this config
  2808. my %configs;
  2809. assign_configs \%configs, $output_config;
  2810. my @config_keys = keys %min_configs;
  2811. foreach my $config (@config_keys) {
  2812. if (!defined($configs{$config})) {
  2813. doprint "$config is not set, disabling\n";
  2814. delete $min_configs{$config};
  2815. }
  2816. }
  2817. # Save off all the current mandidory configs
  2818. open (OUT, ">$temp_config")
  2819. or die "Can't write to $temp_config";
  2820. foreach my $config (keys %keep_configs) {
  2821. print OUT "$keep_configs{$config}\n";
  2822. }
  2823. foreach my $config (keys %min_configs) {
  2824. print OUT "$min_configs{$config}\n";
  2825. }
  2826. close OUT;
  2827. run_command "mv $temp_config $output_minconfig" or
  2828. dodie "failed to copy update to $output_minconfig";
  2829. }
  2830. doprint "Reboot and wait $sleep_time seconds\n";
  2831. reboot_to_good $sleep_time;
  2832. }
  2833. success $i;
  2834. return 1;
  2835. }
  2836. $#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
  2837. if ($#ARGV == 0) {
  2838. $ktest_config = $ARGV[0];
  2839. if (! -f $ktest_config) {
  2840. print "$ktest_config does not exist.\n";
  2841. if (!read_yn "Create it?") {
  2842. exit 0;
  2843. }
  2844. }
  2845. } else {
  2846. $ktest_config = "ktest.conf";
  2847. }
  2848. if (! -f $ktest_config) {
  2849. $newconfig = 1;
  2850. get_test_case;
  2851. open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
  2852. print OUT << "EOF"
  2853. # Generated by ktest.pl
  2854. #
  2855. # PWD is a ktest.pl variable that will result in the process working
  2856. # directory that ktest.pl is executed in.
  2857. # THIS_DIR is automatically assigned the PWD of the path that generated
  2858. # the config file. It is best to use this variable when assigning other
  2859. # directory paths within this directory. This allows you to easily
  2860. # move the test cases to other locations or to other machines.
  2861. #
  2862. THIS_DIR := $variable{"PWD"}
  2863. # Define each test with TEST_START
  2864. # The config options below it will override the defaults
  2865. TEST_START
  2866. TEST_TYPE = $default{"TEST_TYPE"}
  2867. DEFAULTS
  2868. EOF
  2869. ;
  2870. close(OUT);
  2871. }
  2872. read_config $ktest_config;
  2873. if (defined($opt{"LOG_FILE"})) {
  2874. $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
  2875. }
  2876. # Append any configs entered in manually to the config file.
  2877. my @new_configs = keys %entered_configs;
  2878. if ($#new_configs >= 0) {
  2879. print "\nAppending entered in configs to $ktest_config\n";
  2880. open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
  2881. foreach my $config (@new_configs) {
  2882. print OUT "$config = $entered_configs{$config}\n";
  2883. $opt{$config} = process_variables($entered_configs{$config});
  2884. }
  2885. }
  2886. if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
  2887. unlink $opt{"LOG_FILE"};
  2888. }
  2889. doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
  2890. for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
  2891. if (!$i) {
  2892. doprint "DEFAULT OPTIONS:\n";
  2893. } else {
  2894. doprint "\nTEST $i OPTIONS";
  2895. if (defined($repeat_tests{$i})) {
  2896. $repeat = $repeat_tests{$i};
  2897. doprint " ITERATE $repeat";
  2898. }
  2899. doprint "\n";
  2900. }
  2901. foreach my $option (sort keys %opt) {
  2902. if ($option =~ /\[(\d+)\]$/) {
  2903. next if ($i != $1);
  2904. } else {
  2905. next if ($i);
  2906. }
  2907. doprint "$option = $opt{$option}\n";
  2908. }
  2909. }
  2910. sub __set_test_option {
  2911. my ($name, $i) = @_;
  2912. my $option = "$name\[$i\]";
  2913. if (defined($opt{$option})) {
  2914. return $opt{$option};
  2915. }
  2916. foreach my $test (keys %repeat_tests) {
  2917. if ($i >= $test &&
  2918. $i < $test + $repeat_tests{$test}) {
  2919. $option = "$name\[$test\]";
  2920. if (defined($opt{$option})) {
  2921. return $opt{$option};
  2922. }
  2923. }
  2924. }
  2925. if (defined($opt{$name})) {
  2926. return $opt{$name};
  2927. }
  2928. return undef;
  2929. }
  2930. sub set_test_option {
  2931. my ($name, $i) = @_;
  2932. my $option = __set_test_option($name, $i);
  2933. return $option if (!defined($option));
  2934. return eval_option($option, $i);
  2935. }
  2936. # First we need to do is the builds
  2937. for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
  2938. # Do not reboot on failing test options
  2939. $no_reboot = 1;
  2940. $reboot_success = 0;
  2941. $have_version = 0;
  2942. $iteration = $i;
  2943. undef %force_config;
  2944. my $makecmd = set_test_option("MAKE_CMD", $i);
  2945. # Load all the options into their mapped variable names
  2946. foreach my $opt (keys %option_map) {
  2947. ${$option_map{$opt}} = set_test_option($opt, $i);
  2948. }
  2949. $start_minconfig_defined = 1;
  2950. # The first test may override the PRE_KTEST option
  2951. if (defined($pre_ktest) && $i == 1) {
  2952. doprint "\n";
  2953. run_command $pre_ktest;
  2954. }
  2955. # Any test can override the POST_KTEST option
  2956. # The last test takes precedence.
  2957. if (defined($post_ktest)) {
  2958. $final_post_ktest = $post_ktest;
  2959. }
  2960. if (!defined($start_minconfig)) {
  2961. $start_minconfig_defined = 0;
  2962. $start_minconfig = $minconfig;
  2963. }
  2964. chdir $builddir || die "can't change directory to $builddir";
  2965. foreach my $dir ($tmpdir, $outputdir) {
  2966. if (!-d $dir) {
  2967. mkpath($dir) or
  2968. die "can't create $dir";
  2969. }
  2970. }
  2971. $ENV{"SSH_USER"} = $ssh_user;
  2972. $ENV{"MACHINE"} = $machine;
  2973. $buildlog = "$tmpdir/buildlog-$machine";
  2974. $testlog = "$tmpdir/testlog-$machine";
  2975. $dmesg = "$tmpdir/dmesg-$machine";
  2976. $make = "$makecmd O=$outputdir";
  2977. $output_config = "$outputdir/.config";
  2978. if (!$buildonly) {
  2979. $target = "$ssh_user\@$machine";
  2980. if ($reboot_type eq "grub") {
  2981. dodie "GRUB_MENU not defined" if (!defined($grub_menu));
  2982. }
  2983. }
  2984. my $run_type = $build_type;
  2985. if ($test_type eq "patchcheck") {
  2986. $run_type = $patchcheck_type;
  2987. } elsif ($test_type eq "bisect") {
  2988. $run_type = $bisect_type;
  2989. } elsif ($test_type eq "config_bisect") {
  2990. $run_type = $config_bisect_type;
  2991. }
  2992. if ($test_type eq "make_min_config") {
  2993. $run_type = "";
  2994. }
  2995. # mistake in config file?
  2996. if (!defined($run_type)) {
  2997. $run_type = "ERROR";
  2998. }
  2999. my $installme = "";
  3000. $installme = " no_install" if ($no_install);
  3001. doprint "\n\n";
  3002. doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
  3003. if (defined($pre_test)) {
  3004. run_command $pre_test;
  3005. }
  3006. unlink $dmesg;
  3007. unlink $buildlog;
  3008. unlink $testlog;
  3009. if (defined($addconfig)) {
  3010. my $min = $minconfig;
  3011. if (!defined($minconfig)) {
  3012. $min = "";
  3013. }
  3014. run_command "cat $addconfig $min > $tmpdir/add_config" or
  3015. dodie "Failed to create temp config";
  3016. $minconfig = "$tmpdir/add_config";
  3017. }
  3018. if (defined($checkout)) {
  3019. run_command "git checkout $checkout" or
  3020. die "failed to checkout $checkout";
  3021. }
  3022. $no_reboot = 0;
  3023. # A test may opt to not reboot the box
  3024. if ($reboot_on_success) {
  3025. $reboot_success = 1;
  3026. }
  3027. if ($test_type eq "bisect") {
  3028. bisect $i;
  3029. next;
  3030. } elsif ($test_type eq "config_bisect") {
  3031. config_bisect $i;
  3032. next;
  3033. } elsif ($test_type eq "patchcheck") {
  3034. patchcheck $i;
  3035. next;
  3036. } elsif ($test_type eq "make_min_config") {
  3037. make_min_config $i;
  3038. next;
  3039. }
  3040. if ($build_type ne "nobuild") {
  3041. build $build_type or next;
  3042. }
  3043. if ($test_type eq "install") {
  3044. get_version;
  3045. install;
  3046. success $i;
  3047. next;
  3048. }
  3049. if ($test_type ne "build") {
  3050. my $failed = 0;
  3051. start_monitor_and_boot or $failed = 1;
  3052. if (!$failed && $test_type ne "boot" && defined($run_test)) {
  3053. do_run_test or $failed = 1;
  3054. }
  3055. end_monitor;
  3056. next if ($failed);
  3057. }
  3058. success $i;
  3059. }
  3060. if (defined($final_post_ktest)) {
  3061. run_command $final_post_ktest;
  3062. }
  3063. if ($opt{"POWEROFF_ON_SUCCESS"}) {
  3064. halt;
  3065. } elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot && $reboot_success) {
  3066. reboot_to_good;
  3067. } elsif (defined($switch_to_good)) {
  3068. # still need to get to the good kernel
  3069. run_command $switch_to_good;
  3070. }
  3071. doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
  3072. exit 0;