ktest.pl 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698
  1. #!/usr/bin/perl -w
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. #
  4. # Copyright 2010 - Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
  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. use FindBin;
  13. use IO::Handle;
  14. my $VERSION = "0.2";
  15. $| = 1;
  16. my %opt;
  17. my %repeat_tests;
  18. my %repeats;
  19. my %evals;
  20. my @command_vars;
  21. my %command_tmp_vars;
  22. #default opts
  23. my %default = (
  24. "MAILER" => "sendmail", # default mailer
  25. "EMAIL_ON_ERROR" => 1,
  26. "EMAIL_WHEN_FINISHED" => 1,
  27. "EMAIL_WHEN_CANCELED" => 0,
  28. "EMAIL_WHEN_STARTED" => 0,
  29. "NUM_TESTS" => 1,
  30. "TEST_TYPE" => "build",
  31. "BUILD_TYPE" => "oldconfig",
  32. "MAKE_CMD" => "make",
  33. "CLOSE_CONSOLE_SIGNAL" => "INT",
  34. "TIMEOUT" => 120,
  35. "TMP_DIR" => "/tmp/ktest/\${MACHINE}",
  36. "SLEEP_TIME" => 60, # sleep time between tests
  37. "BUILD_NOCLEAN" => 0,
  38. "REBOOT_ON_ERROR" => 0,
  39. "POWEROFF_ON_ERROR" => 0,
  40. "REBOOT_ON_SUCCESS" => 1,
  41. "POWEROFF_ON_SUCCESS" => 0,
  42. "BUILD_OPTIONS" => "",
  43. "BISECT_SLEEP_TIME" => 60, # sleep time between bisects
  44. "PATCHCHECK_SLEEP_TIME" => 60, # sleep time between patch checks
  45. "CLEAR_LOG" => 0,
  46. "BISECT_MANUAL" => 0,
  47. "BISECT_SKIP" => 1,
  48. "BISECT_TRIES" => 1,
  49. "MIN_CONFIG_TYPE" => "boot",
  50. "SUCCESS_LINE" => "login:",
  51. "DETECT_TRIPLE_FAULT" => 1,
  52. "NO_INSTALL" => 0,
  53. "BOOTED_TIMEOUT" => 1,
  54. "DIE_ON_FAILURE" => 1,
  55. "SSH_EXEC" => "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND",
  56. "SCP_TO_TARGET" => "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE",
  57. "SCP_TO_TARGET_INSTALL" => "\${SCP_TO_TARGET}",
  58. "REBOOT" => "ssh \$SSH_USER\@\$MACHINE reboot",
  59. "REBOOT_RETURN_CODE" => 255,
  60. "STOP_AFTER_SUCCESS" => 10,
  61. "STOP_AFTER_FAILURE" => 60,
  62. "STOP_TEST_AFTER" => 600,
  63. "MAX_MONITOR_WAIT" => 1800,
  64. "GRUB_REBOOT" => "grub2-reboot",
  65. "GRUB_BLS_GET" => "grubby --info=ALL",
  66. "SYSLINUX" => "extlinux",
  67. "SYSLINUX_PATH" => "/boot/extlinux",
  68. "CONNECT_TIMEOUT" => 25,
  69. # required, and we will ask users if they don't have them but we keep the default
  70. # value something that is common.
  71. "REBOOT_TYPE" => "grub",
  72. "LOCALVERSION" => "-test",
  73. "SSH_USER" => "root",
  74. "BUILD_TARGET" => "arch/x86/boot/bzImage",
  75. "TARGET_IMAGE" => "/boot/vmlinuz-test",
  76. "LOG_FILE" => undef,
  77. "IGNORE_UNUSED" => 0,
  78. );
  79. my $test_log_start = 0;
  80. my $ktest_config = "ktest.conf";
  81. my $version;
  82. my $have_version = 0;
  83. my $machine;
  84. my $last_machine;
  85. my $ssh_user;
  86. my $tmpdir;
  87. my $builddir;
  88. my $outputdir;
  89. my $output_config;
  90. my $test_type;
  91. my $build_type;
  92. my $build_options;
  93. my $final_post_ktest;
  94. my $pre_ktest;
  95. my $post_ktest;
  96. my $pre_test;
  97. my $pre_test_die;
  98. my $post_test;
  99. my $pre_build;
  100. my $post_build;
  101. my $pre_build_die;
  102. my $post_build_die;
  103. my $reboot_type;
  104. my $reboot_script;
  105. my $power_cycle;
  106. my $reboot;
  107. my $reboot_return_code;
  108. my $reboot_on_error;
  109. my $switch_to_good;
  110. my $switch_to_test;
  111. my $poweroff_on_error;
  112. my $reboot_on_success;
  113. my $die_on_failure;
  114. my $powercycle_after_reboot;
  115. my $poweroff_after_halt;
  116. my $max_monitor_wait;
  117. my $ssh_exec;
  118. my $scp_to_target;
  119. my $scp_to_target_install;
  120. my $power_off;
  121. my $grub_menu;
  122. my $last_grub_menu;
  123. my $grub_file;
  124. my $grub_number;
  125. my $grub_reboot;
  126. my $grub_bls_get;
  127. my $syslinux;
  128. my $syslinux_path;
  129. my $syslinux_label;
  130. my $target;
  131. my $make;
  132. my $pre_install;
  133. my $post_install;
  134. my $no_install;
  135. my $noclean;
  136. my $minconfig;
  137. my $start_minconfig;
  138. my $start_minconfig_defined;
  139. my $output_minconfig;
  140. my $minconfig_type;
  141. my $use_output_minconfig;
  142. my $warnings_file;
  143. my $ignore_config;
  144. my $ignore_errors;
  145. my $addconfig;
  146. my $in_bisect = 0;
  147. my $bisect_bad_commit = "";
  148. my $reverse_bisect;
  149. my $bisect_manual;
  150. my $bisect_skip;
  151. my $bisect_tries;
  152. my $config_bisect_good;
  153. my $bisect_ret_good;
  154. my $bisect_ret_bad;
  155. my $bisect_ret_skip;
  156. my $bisect_ret_abort;
  157. my $bisect_ret_default;
  158. my $in_patchcheck = 0;
  159. my $run_test;
  160. my $buildlog;
  161. my $testlog;
  162. my $dmesg;
  163. my $monitor_fp;
  164. my $monitor_pid;
  165. my $monitor_cnt = 0;
  166. my $sleep_time;
  167. my $bisect_sleep_time;
  168. my $patchcheck_sleep_time;
  169. my $ignore_warnings;
  170. my $store_failures;
  171. my $store_successes;
  172. my $test_name;
  173. my $timeout;
  174. my $run_timeout;
  175. my $connect_timeout;
  176. my $config_bisect_exec;
  177. my $booted_timeout;
  178. my $detect_triplefault;
  179. my $console;
  180. my $close_console_signal;
  181. my $reboot_success_line;
  182. my $success_line;
  183. my $stop_after_success;
  184. my $stop_after_failure;
  185. my $stop_test_after;
  186. my $build_target;
  187. my $target_image;
  188. my $checkout;
  189. my $localversion;
  190. my $iteration = 0;
  191. my $successes = 0;
  192. my $stty_orig;
  193. my $run_command_status = 0;
  194. my $bisect_good;
  195. my $bisect_bad;
  196. my $bisect_type;
  197. my $bisect_start;
  198. my $bisect_replay;
  199. my $bisect_files;
  200. my $bisect_reverse;
  201. my $bisect_check;
  202. my $config_bisect;
  203. my $config_bisect_type;
  204. my $config_bisect_check;
  205. my $patchcheck_type;
  206. my $patchcheck_start;
  207. my $patchcheck_cherry;
  208. my $patchcheck_end;
  209. my $patchcheck_skip;
  210. my $build_time;
  211. my $install_time;
  212. my $reboot_time;
  213. my $test_time;
  214. my $warning_found = 0;
  215. my $pwd;
  216. my $dirname = $FindBin::Bin;
  217. my $mailto;
  218. my $mailer;
  219. my $mail_path;
  220. my $mail_max_size;
  221. my $mail_command;
  222. my $email_on_error;
  223. my $email_when_finished;
  224. my $email_when_started;
  225. my $email_when_canceled;
  226. my $script_start_time = localtime();
  227. # set when a test is something other that just building or install
  228. # which would require more options.
  229. my $buildonly = 1;
  230. # tell build not to worry about warnings, even when WARNINGS_FILE is set
  231. my $warnings_ok = 0;
  232. # set when creating a new config
  233. my $newconfig = 0;
  234. my %entered_configs;
  235. my %config_help;
  236. my %variable;
  237. # force_config is the list of configs that we force enabled (or disabled)
  238. # in a .config file. The MIN_CONFIG and ADD_CONFIG configs.
  239. my %force_config;
  240. # do not force reboots on config problems
  241. my $no_reboot = 1;
  242. # reboot on success
  243. my $reboot_success = 0;
  244. my %option_map = (
  245. "MAILTO" => \$mailto,
  246. "MAILER" => \$mailer,
  247. "MAIL_PATH" => \$mail_path,
  248. "MAIL_MAX_SIZE" => \$mail_max_size,
  249. "MAIL_COMMAND" => \$mail_command,
  250. "EMAIL_ON_ERROR" => \$email_on_error,
  251. "EMAIL_WHEN_FINISHED" => \$email_when_finished,
  252. "EMAIL_WHEN_STARTED" => \$email_when_started,
  253. "EMAIL_WHEN_CANCELED" => \$email_when_canceled,
  254. "MACHINE" => \$machine,
  255. "SSH_USER" => \$ssh_user,
  256. "TMP_DIR" => \$tmpdir,
  257. "OUTPUT_DIR" => \$outputdir,
  258. "BUILD_DIR" => \$builddir,
  259. "TEST_TYPE" => \$test_type,
  260. "PRE_KTEST" => \$pre_ktest,
  261. "POST_KTEST" => \$post_ktest,
  262. "PRE_TEST" => \$pre_test,
  263. "PRE_TEST_DIE" => \$pre_test_die,
  264. "POST_TEST" => \$post_test,
  265. "BUILD_TYPE" => \$build_type,
  266. "BUILD_OPTIONS" => \$build_options,
  267. "PRE_BUILD" => \$pre_build,
  268. "POST_BUILD" => \$post_build,
  269. "PRE_BUILD_DIE" => \$pre_build_die,
  270. "POST_BUILD_DIE" => \$post_build_die,
  271. "POWER_CYCLE" => \$power_cycle,
  272. "REBOOT" => \$reboot,
  273. "REBOOT_RETURN_CODE" => \$reboot_return_code,
  274. "BUILD_NOCLEAN" => \$noclean,
  275. "MIN_CONFIG" => \$minconfig,
  276. "OUTPUT_MIN_CONFIG" => \$output_minconfig,
  277. "START_MIN_CONFIG" => \$start_minconfig,
  278. "MIN_CONFIG_TYPE" => \$minconfig_type,
  279. "USE_OUTPUT_MIN_CONFIG" => \$use_output_minconfig,
  280. "WARNINGS_FILE" => \$warnings_file,
  281. "IGNORE_CONFIG" => \$ignore_config,
  282. "TEST" => \$run_test,
  283. "ADD_CONFIG" => \$addconfig,
  284. "REBOOT_TYPE" => \$reboot_type,
  285. "GRUB_MENU" => \$grub_menu,
  286. "GRUB_FILE" => \$grub_file,
  287. "GRUB_REBOOT" => \$grub_reboot,
  288. "GRUB_BLS_GET" => \$grub_bls_get,
  289. "SYSLINUX" => \$syslinux,
  290. "SYSLINUX_PATH" => \$syslinux_path,
  291. "SYSLINUX_LABEL" => \$syslinux_label,
  292. "PRE_INSTALL" => \$pre_install,
  293. "POST_INSTALL" => \$post_install,
  294. "NO_INSTALL" => \$no_install,
  295. "REBOOT_SCRIPT" => \$reboot_script,
  296. "REBOOT_ON_ERROR" => \$reboot_on_error,
  297. "SWITCH_TO_GOOD" => \$switch_to_good,
  298. "SWITCH_TO_TEST" => \$switch_to_test,
  299. "POWEROFF_ON_ERROR" => \$poweroff_on_error,
  300. "REBOOT_ON_SUCCESS" => \$reboot_on_success,
  301. "DIE_ON_FAILURE" => \$die_on_failure,
  302. "POWER_OFF" => \$power_off,
  303. "POWERCYCLE_AFTER_REBOOT" => \$powercycle_after_reboot,
  304. "POWEROFF_AFTER_HALT" => \$poweroff_after_halt,
  305. "MAX_MONITOR_WAIT" => \$max_monitor_wait,
  306. "SLEEP_TIME" => \$sleep_time,
  307. "BISECT_SLEEP_TIME" => \$bisect_sleep_time,
  308. "PATCHCHECK_SLEEP_TIME" => \$patchcheck_sleep_time,
  309. "IGNORE_WARNINGS" => \$ignore_warnings,
  310. "IGNORE_ERRORS" => \$ignore_errors,
  311. "BISECT_MANUAL" => \$bisect_manual,
  312. "BISECT_SKIP" => \$bisect_skip,
  313. "BISECT_TRIES" => \$bisect_tries,
  314. "CONFIG_BISECT_GOOD" => \$config_bisect_good,
  315. "BISECT_RET_GOOD" => \$bisect_ret_good,
  316. "BISECT_RET_BAD" => \$bisect_ret_bad,
  317. "BISECT_RET_SKIP" => \$bisect_ret_skip,
  318. "BISECT_RET_ABORT" => \$bisect_ret_abort,
  319. "BISECT_RET_DEFAULT" => \$bisect_ret_default,
  320. "STORE_FAILURES" => \$store_failures,
  321. "STORE_SUCCESSES" => \$store_successes,
  322. "TEST_NAME" => \$test_name,
  323. "TIMEOUT" => \$timeout,
  324. "RUN_TIMEOUT" => \$run_timeout,
  325. "CONNECT_TIMEOUT" => \$connect_timeout,
  326. "CONFIG_BISECT_EXEC" => \$config_bisect_exec,
  327. "BOOTED_TIMEOUT" => \$booted_timeout,
  328. "CONSOLE" => \$console,
  329. "CLOSE_CONSOLE_SIGNAL" => \$close_console_signal,
  330. "DETECT_TRIPLE_FAULT" => \$detect_triplefault,
  331. "SUCCESS_LINE" => \$success_line,
  332. "REBOOT_SUCCESS_LINE" => \$reboot_success_line,
  333. "STOP_AFTER_SUCCESS" => \$stop_after_success,
  334. "STOP_AFTER_FAILURE" => \$stop_after_failure,
  335. "STOP_TEST_AFTER" => \$stop_test_after,
  336. "BUILD_TARGET" => \$build_target,
  337. "SSH_EXEC" => \$ssh_exec,
  338. "SCP_TO_TARGET" => \$scp_to_target,
  339. "SCP_TO_TARGET_INSTALL" => \$scp_to_target_install,
  340. "CHECKOUT" => \$checkout,
  341. "TARGET_IMAGE" => \$target_image,
  342. "LOCALVERSION" => \$localversion,
  343. "BISECT_GOOD" => \$bisect_good,
  344. "BISECT_BAD" => \$bisect_bad,
  345. "BISECT_TYPE" => \$bisect_type,
  346. "BISECT_START" => \$bisect_start,
  347. "BISECT_REPLAY" => \$bisect_replay,
  348. "BISECT_FILES" => \$bisect_files,
  349. "BISECT_REVERSE" => \$bisect_reverse,
  350. "BISECT_CHECK" => \$bisect_check,
  351. "CONFIG_BISECT" => \$config_bisect,
  352. "CONFIG_BISECT_TYPE" => \$config_bisect_type,
  353. "CONFIG_BISECT_CHECK" => \$config_bisect_check,
  354. "PATCHCHECK_TYPE" => \$patchcheck_type,
  355. "PATCHCHECK_START" => \$patchcheck_start,
  356. "PATCHCHECK_CHERRY" => \$patchcheck_cherry,
  357. "PATCHCHECK_END" => \$patchcheck_end,
  358. "PATCHCHECK_SKIP" => \$patchcheck_skip,
  359. );
  360. # Options may be used by other options, record them.
  361. my %used_options;
  362. # default variables that can be used
  363. chomp ($variable{"PWD"} = `pwd`);
  364. $pwd = $variable{"PWD"};
  365. $config_help{"MACHINE"} = << "EOF"
  366. The machine hostname that you will test.
  367. For build only tests, it is still needed to differentiate log files.
  368. EOF
  369. ;
  370. $config_help{"SSH_USER"} = << "EOF"
  371. The box is expected to have ssh on normal bootup, provide the user
  372. (most likely root, since you need privileged operations)
  373. EOF
  374. ;
  375. $config_help{"BUILD_DIR"} = << "EOF"
  376. The directory that contains the Linux source code (full path).
  377. You can use \${PWD} that will be the path where ktest.pl is run, or use
  378. \${THIS_DIR} which is assigned \${PWD} but may be changed later.
  379. EOF
  380. ;
  381. $config_help{"OUTPUT_DIR"} = << "EOF"
  382. The directory that the objects will be built (full path).
  383. (can not be same as BUILD_DIR)
  384. You can use \${PWD} that will be the path where ktest.pl is run, or use
  385. \${THIS_DIR} which is assigned \${PWD} but may be changed later.
  386. EOF
  387. ;
  388. $config_help{"BUILD_TARGET"} = << "EOF"
  389. The location of the compiled file to copy to the target.
  390. (relative to OUTPUT_DIR)
  391. EOF
  392. ;
  393. $config_help{"BUILD_OPTIONS"} = << "EOF"
  394. Options to add to \"make\" when building.
  395. i.e. -j20
  396. EOF
  397. ;
  398. $config_help{"TARGET_IMAGE"} = << "EOF"
  399. The place to put your image on the test machine.
  400. EOF
  401. ;
  402. $config_help{"POWER_CYCLE"} = << "EOF"
  403. A script or command to reboot the box.
  404. Here is a digital loggers power switch example
  405. POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
  406. Here is an example to reboot a virtual box on the current host
  407. with the name "Guest".
  408. POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
  409. EOF
  410. ;
  411. $config_help{"CONSOLE"} = << "EOF"
  412. The script or command that reads the console
  413. If you use ttywatch server, something like the following would work.
  414. CONSOLE = nc -d localhost 3001
  415. For a virtual machine with guest name "Guest".
  416. CONSOLE = virsh console Guest
  417. EOF
  418. ;
  419. $config_help{"LOCALVERSION"} = << "EOF"
  420. Required version ending to differentiate the test
  421. from other linux builds on the system.
  422. EOF
  423. ;
  424. $config_help{"REBOOT_TYPE"} = << "EOF"
  425. Way to reboot the box to the test kernel.
  426. Only valid options so far are "grub", "grub2", "grub2bls", "syslinux", and "script".
  427. If you specify grub, it will assume grub version 1
  428. and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
  429. and select that target to reboot to the kernel. If this is not
  430. your setup, then specify "script" and have a command or script
  431. specified in REBOOT_SCRIPT to boot to the target.
  432. The entry in /boot/grub/menu.lst must be entered in manually.
  433. The test will not modify that file.
  434. If you specify grub2, then you also need to specify both \$GRUB_MENU
  435. and \$GRUB_FILE.
  436. If you specify grub2bls, then you also need to specify \$GRUB_MENU.
  437. If you specify syslinux, then you may use SYSLINUX to define the syslinux
  438. command (defaults to extlinux), and SYSLINUX_PATH to specify the path to
  439. the syslinux install (defaults to /boot/extlinux). But you have to specify
  440. SYSLINUX_LABEL to define the label to boot to for the test kernel.
  441. EOF
  442. ;
  443. $config_help{"GRUB_MENU"} = << "EOF"
  444. The grub title name for the test kernel to boot
  445. (Only mandatory if REBOOT_TYPE = grub or grub2)
  446. Note, ktest.pl will not update the grub menu.lst, you need to
  447. manually add an option for the test. ktest.pl will search
  448. the grub menu.lst for this option to find what kernel to
  449. reboot into.
  450. For example, if in the /boot/grub/menu.lst the test kernel title has:
  451. title Test Kernel
  452. kernel vmlinuz-test
  453. GRUB_MENU = Test Kernel
  454. For grub2, a search of \$GRUB_FILE is performed for the lines
  455. that begin with "menuentry". It will not detect submenus. The
  456. menu must be a non-nested menu. Add the quotes used in the menu
  457. to guarantee your selection, as the first menuentry with the content
  458. of \$GRUB_MENU that is found will be used.
  459. For grub2bls, \$GRUB_MENU is searched on the result of \$GRUB_BLS_GET
  460. command for the lines that begin with "title".
  461. EOF
  462. ;
  463. $config_help{"GRUB_FILE"} = << "EOF"
  464. If grub2 is used, the full path for the grub.cfg file is placed
  465. here. Use something like /boot/grub2/grub.cfg to search.
  466. EOF
  467. ;
  468. $config_help{"SYSLINUX_LABEL"} = << "EOF"
  469. If syslinux is used, the label that boots the target kernel must
  470. be specified with SYSLINUX_LABEL.
  471. EOF
  472. ;
  473. $config_help{"REBOOT_SCRIPT"} = << "EOF"
  474. A script to reboot the target into the test kernel
  475. (Only mandatory if REBOOT_TYPE = script)
  476. EOF
  477. ;
  478. # used with process_expression()
  479. my $d = 0;
  480. # defined before get_test_name()
  481. my $in_die = 0;
  482. # defined before process_warning_line()
  483. my $check_build_re = ".*:.*(warning|error|Error):.*";
  484. my $utf8_quote = "\\x{e2}\\x{80}(\\x{98}|\\x{99})";
  485. # defined before child_finished()
  486. my $child_done;
  487. # config_ignore holds the configs that were set (or unset) for
  488. # a good config and we will ignore these configs for the rest
  489. # of a config bisect. These configs stay as they were.
  490. my %config_ignore;
  491. # config_set holds what all configs were set as.
  492. my %config_set;
  493. # config_off holds the set of configs that the bad config had disabled.
  494. # We need to record them and set them in the .config when running
  495. # olddefconfig, because olddefconfig keeps the defaults.
  496. my %config_off;
  497. # config_off_tmp holds a set of configs to turn off for now
  498. my @config_off_tmp;
  499. # config_list is the set of configs that are being tested
  500. my %config_list;
  501. my %null_config;
  502. my %dependency;
  503. # found above run_config_bisect()
  504. my $pass = 1;
  505. # found above add_dep()
  506. my %depends;
  507. my %depcount;
  508. my $iflevel = 0;
  509. my @ifdeps;
  510. # prevent recursion
  511. my %read_kconfigs;
  512. # found above test_this_config()
  513. my %min_configs;
  514. my %keep_configs;
  515. my %save_configs;
  516. my %processed_configs;
  517. my %nochange_config;
  518. #
  519. # These are first defined here, main function later on
  520. #
  521. sub run_command;
  522. sub start_monitor;
  523. sub end_monitor;
  524. sub wait_for_monitor;
  525. sub _logit {
  526. if (defined($opt{"LOG_FILE"})) {
  527. print LOG @_;
  528. }
  529. }
  530. sub logit {
  531. if (defined($opt{"LOG_FILE"})) {
  532. _logit @_;
  533. } else {
  534. print @_;
  535. }
  536. }
  537. sub doprint {
  538. print @_;
  539. _logit @_;
  540. }
  541. sub read_prompt {
  542. my ($cancel, $prompt) = @_;
  543. my $ans;
  544. for (;;) {
  545. if ($cancel) {
  546. print "$prompt [y/n/C] ";
  547. } else {
  548. print "$prompt [Y/n] ";
  549. }
  550. $ans = <STDIN>;
  551. chomp $ans;
  552. if ($ans =~ /^\s*$/) {
  553. if ($cancel) {
  554. $ans = "c";
  555. } else {
  556. $ans = "y";
  557. }
  558. }
  559. last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
  560. if ($cancel) {
  561. last if ($ans =~ /^c$/i);
  562. print "Please answer either 'y', 'n' or 'c'.\n";
  563. } else {
  564. print "Please answer either 'y' or 'n'.\n";
  565. }
  566. }
  567. if ($ans =~ /^c/i) {
  568. exit;
  569. }
  570. if ($ans !~ /^y$/i) {
  571. return 0;
  572. }
  573. return 1;
  574. }
  575. sub read_yn {
  576. my ($prompt) = @_;
  577. return read_prompt 0, $prompt;
  578. }
  579. sub read_ync {
  580. my ($prompt) = @_;
  581. return read_prompt 1, $prompt;
  582. }
  583. sub get_mandatory_config {
  584. my ($config) = @_;
  585. my $ans;
  586. return if (defined($opt{$config}));
  587. if (defined($config_help{$config})) {
  588. print "\n";
  589. print $config_help{$config};
  590. }
  591. for (;;) {
  592. print "$config = ";
  593. if (defined($default{$config}) && length($default{$config})) {
  594. print "\[$default{$config}\] ";
  595. }
  596. $ans = <STDIN>;
  597. $ans =~ s/^\s*(.*\S)\s*$/$1/;
  598. if ($ans =~ /^\s*$/) {
  599. if ($default{$config}) {
  600. $ans = $default{$config};
  601. } else {
  602. print "Your answer can not be blank\n";
  603. next;
  604. }
  605. }
  606. $entered_configs{$config} = ${ans};
  607. last;
  608. }
  609. }
  610. sub show_time {
  611. my ($time) = @_;
  612. my $hours = 0;
  613. my $minutes = 0;
  614. if ($time > 3600) {
  615. $hours = int($time / 3600);
  616. $time -= $hours * 3600;
  617. }
  618. if ($time > 60) {
  619. $minutes = int($time / 60);
  620. $time -= $minutes * 60;
  621. }
  622. if ($hours > 0) {
  623. doprint "$hours hour";
  624. doprint "s" if ($hours > 1);
  625. doprint " ";
  626. }
  627. if ($minutes > 0) {
  628. doprint "$minutes minute";
  629. doprint "s" if ($minutes > 1);
  630. doprint " ";
  631. }
  632. doprint "$time second";
  633. doprint "s" if ($time != 1);
  634. }
  635. sub print_times {
  636. doprint "\n";
  637. if ($build_time) {
  638. doprint "Build time: ";
  639. show_time($build_time);
  640. doprint "\n";
  641. }
  642. if ($install_time) {
  643. doprint "Install time: ";
  644. show_time($install_time);
  645. doprint "\n";
  646. }
  647. if ($reboot_time) {
  648. doprint "Reboot time: ";
  649. show_time($reboot_time);
  650. doprint "\n";
  651. }
  652. if ($test_time) {
  653. doprint "Test time: ";
  654. show_time($test_time);
  655. doprint "\n";
  656. }
  657. if ($warning_found) {
  658. doprint "\n*** WARNING";
  659. doprint "S" if ($warning_found > 1);
  660. doprint " found in build: $warning_found ***\n\n";
  661. }
  662. # reset for iterations like bisect
  663. $build_time = 0;
  664. $install_time = 0;
  665. $reboot_time = 0;
  666. $test_time = 0;
  667. $warning_found = 0;
  668. }
  669. sub get_mandatory_configs {
  670. get_mandatory_config("MACHINE");
  671. get_mandatory_config("BUILD_DIR");
  672. get_mandatory_config("OUTPUT_DIR");
  673. if ($newconfig) {
  674. get_mandatory_config("BUILD_OPTIONS");
  675. }
  676. # options required for other than just building a kernel
  677. if (!$buildonly) {
  678. get_mandatory_config("POWER_CYCLE");
  679. get_mandatory_config("CONSOLE");
  680. }
  681. # options required for install and more
  682. if ($buildonly != 1) {
  683. get_mandatory_config("SSH_USER");
  684. get_mandatory_config("BUILD_TARGET");
  685. get_mandatory_config("TARGET_IMAGE");
  686. }
  687. get_mandatory_config("LOCALVERSION");
  688. return if ($buildonly);
  689. my $rtype = $opt{"REBOOT_TYPE"};
  690. if (!defined($rtype)) {
  691. if (!defined($opt{"GRUB_MENU"})) {
  692. get_mandatory_config("REBOOT_TYPE");
  693. $rtype = $entered_configs{"REBOOT_TYPE"};
  694. } else {
  695. $rtype = "grub";
  696. }
  697. }
  698. if (($rtype eq "grub") or ($rtype eq "grub2bls")) {
  699. get_mandatory_config("GRUB_MENU");
  700. }
  701. if ($rtype eq "grub2") {
  702. get_mandatory_config("GRUB_MENU");
  703. get_mandatory_config("GRUB_FILE");
  704. }
  705. if ($rtype eq "syslinux") {
  706. get_mandatory_config("SYSLINUX_LABEL");
  707. }
  708. }
  709. sub process_variables {
  710. my ($value, $remove_undef) = @_;
  711. my $retval = "";
  712. # We want to check for '\', and it is just easier
  713. # to check the previous character of '$' and not need
  714. # to worry if '$' is the first character. By adding
  715. # a space to $value, we can just check [^\\]\$ and
  716. # it will still work.
  717. $value = " $value";
  718. while ($value =~ /(.*?[^\\])\$\{([^\{]*?)\}(.*)/) {
  719. my $begin = $1;
  720. my $var = $2;
  721. my $end = $3;
  722. # append beginning of value to retval
  723. $retval = "$retval$begin";
  724. if ($var =~ s/^shell\s+//) {
  725. $retval = `$var`;
  726. if ($?) {
  727. doprint "WARNING: $var returned an error\n";
  728. } else {
  729. chomp $retval;
  730. }
  731. } elsif (defined($variable{$var})) {
  732. $retval = "$retval$variable{$var}";
  733. } elsif (defined($remove_undef) && $remove_undef) {
  734. # for if statements, any variable that is not defined,
  735. # we simple convert to 0
  736. $retval = "${retval}0";
  737. } else {
  738. # put back the origin piece, but with $#### to not reprocess it
  739. $retval = "$retval\$####\{$var\}";
  740. # This could be an option that is used later, save
  741. # it so we don't warn if this option is not one of
  742. # ktests options.
  743. $used_options{$var} = 1;
  744. }
  745. $value = "$retval$end";
  746. $retval = "";
  747. }
  748. $retval = $value;
  749. # Convert the saved variables with $####{var} back to ${var}
  750. $retval =~ s/\$####/\$/g;
  751. # remove the space added in the beginning
  752. $retval =~ s/ //;
  753. return "$retval";
  754. }
  755. sub set_value {
  756. my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
  757. my $prvalue = process_variables($rvalue);
  758. if ($lvalue =~ /^(TEST|BISECT|CONFIG_BISECT)_TYPE(\[.*\])?$/ &&
  759. $prvalue !~ /^(config_|)bisect$/ &&
  760. $prvalue !~ /^build$/ &&
  761. $prvalue !~ /^make_warnings_file$/ &&
  762. $buildonly) {
  763. # Note if a test is something other than build, then we
  764. # will need other mandatory options.
  765. if ($prvalue ne "install") {
  766. $buildonly = 0;
  767. } else {
  768. # install still limits some mandatory options.
  769. $buildonly = 2;
  770. }
  771. }
  772. if (defined($opt{$lvalue})) {
  773. if (!$override || defined(${$overrides}{$lvalue})) {
  774. my $extra = "";
  775. if ($override) {
  776. $extra = "In the same override section!\n";
  777. }
  778. die "$name: $.: Option $lvalue defined more than once!\n$extra";
  779. }
  780. ${$overrides}{$lvalue} = $prvalue;
  781. }
  782. $opt{$lvalue} = $prvalue;
  783. }
  784. sub set_eval {
  785. my ($lvalue, $rvalue, $name) = @_;
  786. my $prvalue = process_variables($rvalue);
  787. my $arr;
  788. if (defined($evals{$lvalue})) {
  789. $arr = $evals{$lvalue};
  790. } else {
  791. $arr = [];
  792. $evals{$lvalue} = $arr;
  793. }
  794. push @{$arr}, $rvalue;
  795. }
  796. sub set_variable {
  797. my ($lvalue, $rvalue, $command) = @_;
  798. # Command line variables override all others
  799. if (defined($command_tmp_vars{$lvalue})) {
  800. return;
  801. }
  802. if ($rvalue =~ /^\s*$/) {
  803. delete $variable{$lvalue};
  804. } else {
  805. $rvalue = process_variables($rvalue);
  806. $variable{$lvalue} = $rvalue;
  807. }
  808. if (defined($command)) {
  809. $command_tmp_vars{$lvalue} = 1;
  810. }
  811. }
  812. sub process_compare {
  813. my ($lval, $cmp, $rval) = @_;
  814. # remove whitespace
  815. $lval =~ s/^\s*//;
  816. $lval =~ s/\s*$//;
  817. $rval =~ s/^\s*//;
  818. $rval =~ s/\s*$//;
  819. if ($cmp eq "==") {
  820. return $lval eq $rval;
  821. } elsif ($cmp eq "!=") {
  822. return $lval ne $rval;
  823. } elsif ($cmp eq "=~") {
  824. return $lval =~ m/$rval/;
  825. } elsif ($cmp eq "!~") {
  826. return $lval !~ m/$rval/;
  827. }
  828. my $statement = "$lval $cmp $rval";
  829. my $ret = eval $statement;
  830. # $@ stores error of eval
  831. if ($@) {
  832. return -1;
  833. }
  834. return $ret;
  835. }
  836. sub value_defined {
  837. my ($val) = @_;
  838. return defined($variable{$2}) ||
  839. defined($opt{$2});
  840. }
  841. sub process_expression {
  842. my ($name, $val) = @_;
  843. my $c = $d++;
  844. while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
  845. my $express = $1;
  846. if (process_expression($name, $express)) {
  847. $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
  848. } else {
  849. $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
  850. }
  851. }
  852. $d--;
  853. my $OR = "\\|\\|";
  854. my $AND = "\\&\\&";
  855. while ($val =~ s/^(.*?)($OR|$AND)//) {
  856. my $express = $1;
  857. my $op = $2;
  858. if (process_expression($name, $express)) {
  859. if ($op eq "||") {
  860. return 1;
  861. }
  862. } else {
  863. if ($op eq "&&") {
  864. return 0;
  865. }
  866. }
  867. }
  868. if ($val =~ /(.*)(==|\!=|>=|<=|>|<|=~|\!~)(.*)/) {
  869. my $ret = process_compare($1, $2, $3);
  870. if ($ret < 0) {
  871. die "$name: $.: Unable to process comparison\n";
  872. }
  873. return $ret;
  874. }
  875. if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
  876. if (defined $1) {
  877. return !value_defined($2);
  878. } else {
  879. return value_defined($2);
  880. }
  881. }
  882. if ($val =~ s/^\s*NOT\s+(.*)//) {
  883. my $express = $1;
  884. my $ret = process_expression($name, $express);
  885. return !$ret;
  886. }
  887. if ($val =~ /^\s*0\s*$/) {
  888. return 0;
  889. } elsif ($val =~ /^\s*\d+\s*$/) {
  890. return 1;
  891. }
  892. die ("$name: $.: Undefined content $val in if statement\n");
  893. }
  894. sub process_if {
  895. my ($name, $value) = @_;
  896. # Convert variables and replace undefined ones with 0
  897. my $val = process_variables($value, 1);
  898. my $ret = process_expression $name, $val;
  899. return $ret;
  900. }
  901. sub __read_config {
  902. my ($config, $current_test_num) = @_;
  903. my $in;
  904. open($in, $config) || die "can't read file $config";
  905. my $name = $config;
  906. $name =~ s,.*/(.*),$1,;
  907. my $test_num = $$current_test_num;
  908. my $default = 1;
  909. my $repeat = 1;
  910. my $num_tests_set = 0;
  911. my $skip = 0;
  912. my $rest;
  913. my $line;
  914. my $test_case = 0;
  915. my $if = 0;
  916. my $if_set = 0;
  917. my $override = 0;
  918. my %overrides;
  919. while (<$in>) {
  920. # ignore blank lines and comments
  921. next if (/^\s*$/ || /\s*\#/);
  922. if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
  923. my $type = $1;
  924. $rest = $2;
  925. $line = $2;
  926. my $old_test_num;
  927. my $old_repeat;
  928. $override = 0;
  929. if ($type eq "TEST_START") {
  930. if ($num_tests_set) {
  931. die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
  932. }
  933. $old_test_num = $test_num;
  934. $old_repeat = $repeat;
  935. $test_num += $repeat;
  936. $default = 0;
  937. $repeat = 1;
  938. } else {
  939. $default = 1;
  940. }
  941. # If SKIP is anywhere in the line, the command will be skipped
  942. if ($rest =~ s/\s+SKIP\b//) {
  943. $skip = 1;
  944. } else {
  945. $test_case = 1;
  946. $skip = 0;
  947. }
  948. if ($rest =~ s/\sELSE\b//) {
  949. if (!$if) {
  950. die "$name: $.: ELSE found with out matching IF section\n$_";
  951. }
  952. $if = 0;
  953. if ($if_set) {
  954. $skip = 1;
  955. } else {
  956. $skip = 0;
  957. }
  958. }
  959. if ($rest =~ s/\sIF\s+(.*)//) {
  960. if (process_if($name, $1)) {
  961. $if_set = 1;
  962. } else {
  963. $skip = 1;
  964. }
  965. $if = 1;
  966. } else {
  967. $if = 0;
  968. $if_set = 0;
  969. }
  970. if (!$skip) {
  971. if ($type eq "TEST_START") {
  972. if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
  973. $repeat = $1;
  974. $repeat_tests{"$test_num"} = $repeat;
  975. }
  976. } elsif ($rest =~ s/\sOVERRIDE\b//) {
  977. # DEFAULT only
  978. $override = 1;
  979. # Clear previous overrides
  980. %overrides = ();
  981. }
  982. }
  983. if (!$skip && $rest !~ /^\s*$/) {
  984. die "$name: $.: Garbage found after $type\n$_";
  985. }
  986. if ($skip && $type eq "TEST_START") {
  987. $test_num = $old_test_num;
  988. $repeat = $old_repeat;
  989. }
  990. } elsif (/^\s*ELSE\b(.*)$/) {
  991. if (!$if) {
  992. die "$name: $.: ELSE found with out matching IF section\n$_";
  993. }
  994. $rest = $1;
  995. if ($if_set) {
  996. $skip = 1;
  997. $rest = "";
  998. } else {
  999. $skip = 0;
  1000. if ($rest =~ /\sIF\s+(.*)/) {
  1001. # May be a ELSE IF section.
  1002. if (process_if($name, $1)) {
  1003. $if_set = 1;
  1004. } else {
  1005. $skip = 1;
  1006. }
  1007. $rest = "";
  1008. } else {
  1009. $if = 0;
  1010. }
  1011. }
  1012. if ($rest !~ /^\s*$/) {
  1013. die "$name: $.: Garbage found after DEFAULTS\n$_";
  1014. }
  1015. } elsif (/^\s*INCLUDE\s+(\S+)/) {
  1016. next if ($skip);
  1017. if (!$default) {
  1018. die "$name: $.: INCLUDE can only be done in default sections\n$_";
  1019. }
  1020. my $file = process_variables($1);
  1021. if ($file !~ m,^/,) {
  1022. # check the path of the config file first
  1023. if ($config =~ m,(.*)/,) {
  1024. if (-f "$1/$file") {
  1025. $file = "$1/$file";
  1026. }
  1027. }
  1028. }
  1029. if ( ! -r $file ) {
  1030. die "$name: $.: Can't read file $file\n$_";
  1031. }
  1032. if (__read_config($file, \$test_num)) {
  1033. $test_case = 1;
  1034. }
  1035. } elsif (/^\s*([A-Z_\[\]\d]+)\s*=~\s*(.*?)\s*$/) {
  1036. next if ($skip);
  1037. my $lvalue = $1;
  1038. my $rvalue = $2;
  1039. if ($default || $lvalue =~ /\[\d+\]$/) {
  1040. set_eval($lvalue, $rvalue, $name);
  1041. } else {
  1042. my $val = "$lvalue\[$test_num\]";
  1043. set_eval($val, $rvalue, $name);
  1044. }
  1045. } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
  1046. next if ($skip);
  1047. my $lvalue = $1;
  1048. my $rvalue = $2;
  1049. if (!$default &&
  1050. ($lvalue eq "NUM_TESTS" ||
  1051. $lvalue eq "LOG_FILE" ||
  1052. $lvalue eq "CLEAR_LOG")) {
  1053. die "$name: $.: $lvalue must be set in DEFAULTS section\n";
  1054. }
  1055. if ($lvalue eq "NUM_TESTS") {
  1056. if ($test_num) {
  1057. die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
  1058. }
  1059. if (!$default) {
  1060. die "$name: $.: NUM_TESTS must be set in default section\n";
  1061. }
  1062. $num_tests_set = 1;
  1063. }
  1064. if ($default || $lvalue =~ /\[\d+\]$/) {
  1065. set_value($lvalue, $rvalue, $override, \%overrides, $name);
  1066. } else {
  1067. my $val = "$lvalue\[$test_num\]";
  1068. set_value($val, $rvalue, $override, \%overrides, $name);
  1069. if ($repeat > 1) {
  1070. $repeats{$val} = $repeat;
  1071. }
  1072. }
  1073. } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
  1074. next if ($skip);
  1075. my $lvalue = $1;
  1076. my $rvalue = $2;
  1077. # process config variables.
  1078. # Config variables are only active while reading the
  1079. # config and can be defined anywhere. They also ignore
  1080. # TEST_START and DEFAULTS, but are skipped if they are in
  1081. # one of these sections that have SKIP defined.
  1082. # The save variable can be
  1083. # defined multiple times and the new one simply overrides
  1084. # the previous one.
  1085. set_variable($lvalue, $rvalue);
  1086. } else {
  1087. die "$name: $.: Garbage found in config\n$_";
  1088. }
  1089. }
  1090. if ($test_num) {
  1091. $test_num += $repeat - 1;
  1092. $opt{"NUM_TESTS"} = $test_num;
  1093. }
  1094. close($in);
  1095. $$current_test_num = $test_num;
  1096. return $test_case;
  1097. }
  1098. sub get_test_case {
  1099. print "What test case would you like to run?\n";
  1100. print " (build, install or boot)\n";
  1101. print " Other tests are available but require editing ktest.conf\n";
  1102. print " (see tools/testing/ktest/sample.conf)\n";
  1103. my $ans = <STDIN>;
  1104. chomp $ans;
  1105. $default{"TEST_TYPE"} = $ans;
  1106. }
  1107. sub read_config {
  1108. my ($config) = @_;
  1109. my $test_case;
  1110. my $test_num = 0;
  1111. $test_case = __read_config $config, \$test_num;
  1112. foreach my $val (@command_vars) {
  1113. chomp $val;
  1114. my %command_overrides;
  1115. if ($val =~ m/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
  1116. my $lvalue = $1;
  1117. my $rvalue = $2;
  1118. set_value($lvalue, $rvalue, 1, \%command_overrides, "COMMAND LINE");
  1119. } else {
  1120. die "Invalid option definition '$val'\n";
  1121. }
  1122. }
  1123. # make sure we have all mandatory configs
  1124. get_mandatory_configs;
  1125. # was a test specified?
  1126. if (!$test_case) {
  1127. print "No test case specified.\n";
  1128. get_test_case;
  1129. }
  1130. # set any defaults
  1131. foreach my $default (keys %default) {
  1132. if (!defined($opt{$default})) {
  1133. $opt{$default} = $default{$default};
  1134. }
  1135. }
  1136. if ($opt{"IGNORE_UNUSED"} == 1) {
  1137. return;
  1138. }
  1139. my %not_used;
  1140. # check if there are any stragglers (typos?)
  1141. foreach my $option (keys %opt) {
  1142. my $op = $option;
  1143. # remove per test labels.
  1144. $op =~ s/\[.*\]//;
  1145. if (!exists($option_map{$op}) &&
  1146. !exists($default{$op}) &&
  1147. !exists($used_options{$op})) {
  1148. $not_used{$op} = 1;
  1149. }
  1150. }
  1151. if (%not_used) {
  1152. my $s = "s are";
  1153. $s = " is" if (keys %not_used == 1);
  1154. print "The following option$s not used; could be a typo:\n";
  1155. foreach my $option (keys %not_used) {
  1156. print "$option\n";
  1157. }
  1158. print "Set IGNORE_UNUSED = 1 to have ktest ignore unused variables\n";
  1159. if (!read_yn "Do you want to continue?") {
  1160. exit -1;
  1161. }
  1162. }
  1163. }
  1164. sub __eval_option {
  1165. my ($name, $option, $i) = @_;
  1166. # Add space to evaluate the character before $
  1167. $option = " $option";
  1168. my $retval = "";
  1169. my $repeated = 0;
  1170. my $parent = 0;
  1171. foreach my $test (keys %repeat_tests) {
  1172. if ($i >= $test &&
  1173. $i < $test + $repeat_tests{$test}) {
  1174. $repeated = 1;
  1175. $parent = $test;
  1176. last;
  1177. }
  1178. }
  1179. while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
  1180. my $start = $1;
  1181. my $var = $2;
  1182. my $end = $3;
  1183. # Append beginning of line
  1184. $retval = "$retval$start";
  1185. # If the iteration option OPT[$i] exists, then use that.
  1186. # otherwise see if the default OPT (without [$i]) exists.
  1187. my $o = "$var\[$i\]";
  1188. my $parento = "$var\[$parent\]";
  1189. # If a variable contains itself, use the default var
  1190. if (($var eq $name) && defined($opt{$var})) {
  1191. $o = $opt{$var};
  1192. # Only append if the default doesn't contain itself
  1193. if ($o !~ m/\$\{$var\}/) {
  1194. $retval = "$retval$o";
  1195. }
  1196. } elsif (defined($opt{$o})) {
  1197. $o = $opt{$o};
  1198. $retval = "$retval$o";
  1199. } elsif ($repeated && defined($opt{$parento})) {
  1200. $o = $opt{$parento};
  1201. $retval = "$retval$o";
  1202. } elsif (defined($opt{$var})) {
  1203. $o = $opt{$var};
  1204. $retval = "$retval$o";
  1205. } elsif ($var eq "KERNEL_VERSION" && defined($make)) {
  1206. # special option KERNEL_VERSION uses kernel version
  1207. get_version();
  1208. $retval = "$retval$version";
  1209. } else {
  1210. $retval = "$retval\$\{$var\}";
  1211. }
  1212. $option = $end;
  1213. }
  1214. $retval = "$retval$option";
  1215. $retval =~ s/^ //;
  1216. return $retval;
  1217. }
  1218. sub process_evals {
  1219. my ($name, $option, $i) = @_;
  1220. my $option_name = "$name\[$i\]";
  1221. my $ev;
  1222. my $old_option = $option;
  1223. if (defined($evals{$option_name})) {
  1224. $ev = $evals{$option_name};
  1225. } elsif (defined($evals{$name})) {
  1226. $ev = $evals{$name};
  1227. } else {
  1228. return $option;
  1229. }
  1230. for my $e (@{$ev}) {
  1231. eval "\$option =~ $e";
  1232. }
  1233. if ($option ne $old_option) {
  1234. doprint("$name changed from '$old_option' to '$option'\n");
  1235. }
  1236. return $option;
  1237. }
  1238. sub eval_option {
  1239. my ($name, $option, $i) = @_;
  1240. my $prev = "";
  1241. # Since an option can evaluate to another option,
  1242. # keep iterating until we do not evaluate any more
  1243. # options.
  1244. my $r = 0;
  1245. while ($prev ne $option) {
  1246. # Check for recursive evaluations.
  1247. # 100 deep should be more than enough.
  1248. if ($r++ > 100) {
  1249. die "Over 100 evaluations occurred with $option\n" .
  1250. "Check for recursive variables\n";
  1251. }
  1252. $prev = $option;
  1253. $option = __eval_option($name, $option, $i);
  1254. }
  1255. $option = process_evals($name, $option, $i);
  1256. return $option;
  1257. }
  1258. sub reboot {
  1259. my ($time) = @_;
  1260. my $powercycle = 0;
  1261. # test if the machine can be connected to within a few seconds
  1262. my $stat = run_ssh("echo check machine status", $connect_timeout);
  1263. if (!$stat) {
  1264. doprint("power cycle\n");
  1265. $powercycle = 1;
  1266. }
  1267. if ($powercycle) {
  1268. run_command "$power_cycle";
  1269. start_monitor;
  1270. # flush out current monitor
  1271. # May contain the reboot success line
  1272. wait_for_monitor 1;
  1273. } else {
  1274. # Make sure everything has been written to disk
  1275. run_ssh("sync", 10);
  1276. if (defined($time)) {
  1277. start_monitor;
  1278. # flush out current monitor
  1279. # May contain the reboot success line
  1280. wait_for_monitor 1;
  1281. }
  1282. # try to reboot normally
  1283. if (run_command $reboot) {
  1284. if (defined($powercycle_after_reboot)) {
  1285. sleep $powercycle_after_reboot;
  1286. run_command "$power_cycle";
  1287. }
  1288. } else {
  1289. # nope? power cycle it.
  1290. run_command "$power_cycle";
  1291. }
  1292. }
  1293. if (defined($time)) {
  1294. # We only want to get to the new kernel, don't fail
  1295. # if we stumble over a call trace.
  1296. my $save_ignore_errors = $ignore_errors;
  1297. $ignore_errors = 1;
  1298. # Look for the good kernel to boot
  1299. if (wait_for_monitor($time, "Linux version")) {
  1300. # reboot got stuck?
  1301. doprint "Reboot did not finish. Forcing power cycle\n";
  1302. run_command "$power_cycle";
  1303. }
  1304. $ignore_errors = $save_ignore_errors;
  1305. # Still need to wait for the reboot to finish
  1306. wait_for_monitor($time, $reboot_success_line);
  1307. }
  1308. if ($powercycle || $time) {
  1309. end_monitor;
  1310. }
  1311. }
  1312. sub reboot_to_good {
  1313. my ($time) = @_;
  1314. if (defined($switch_to_good)) {
  1315. run_command $switch_to_good;
  1316. }
  1317. reboot $time;
  1318. }
  1319. sub do_not_reboot {
  1320. my $i = $iteration;
  1321. return $test_type eq "build" || $no_reboot ||
  1322. ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
  1323. ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build") ||
  1324. ($test_type eq "config_bisect" && $opt{"CONFIG_BISECT_TYPE[$i]"} eq "build");
  1325. }
  1326. sub get_test_name() {
  1327. my $name;
  1328. if (defined($test_name)) {
  1329. $name = "$test_name:$test_type";
  1330. } else {
  1331. $name = $test_type;
  1332. }
  1333. return $name;
  1334. }
  1335. sub dodie {
  1336. # avoid recursion
  1337. return if ($in_die);
  1338. $in_die = 1;
  1339. if ($monitor_cnt) {
  1340. # restore terminal settings
  1341. system("stty $stty_orig");
  1342. }
  1343. my $i = $iteration;
  1344. doprint "CRITICAL FAILURE... [TEST $i] ", @_, "\n";
  1345. if ($reboot_on_error && !do_not_reboot) {
  1346. doprint "REBOOTING\n";
  1347. reboot_to_good;
  1348. } elsif ($poweroff_on_error && defined($power_off)) {
  1349. doprint "POWERING OFF\n";
  1350. `$power_off`;
  1351. }
  1352. if (defined($opt{"LOG_FILE"})) {
  1353. print " See $opt{LOG_FILE} for more info.\n";
  1354. }
  1355. if ($email_on_error) {
  1356. my $name = get_test_name;
  1357. my $log_file;
  1358. if (defined($opt{"LOG_FILE"})) {
  1359. my $whence = 2; # End of file
  1360. my $log_size = tell LOG;
  1361. my $size = $log_size - $test_log_start;
  1362. if (defined($mail_max_size)) {
  1363. if ($size > $mail_max_size) {
  1364. $size = $mail_max_size;
  1365. }
  1366. }
  1367. my $pos = - $size;
  1368. $log_file = "$tmpdir/log";
  1369. open (L, "$opt{LOG_FILE}") or die "Can't open $opt{LOG_FILE} to read)";
  1370. open (O, "> $tmpdir/log") or die "Can't open $tmpdir/log\n";
  1371. seek(L, $pos, $whence);
  1372. while (<L>) {
  1373. print O;
  1374. }
  1375. close O;
  1376. close L;
  1377. }
  1378. send_email("KTEST: critical failure for test $i [$name]",
  1379. "Your test started at $script_start_time has failed with:\n@_\n", $log_file);
  1380. }
  1381. if (defined($post_test)) {
  1382. run_command $post_test;
  1383. }
  1384. die @_, "\n";
  1385. }
  1386. sub create_pty {
  1387. my ($ptm, $pts) = @_;
  1388. my $tmp;
  1389. my $TIOCSPTLCK = 0x40045431;
  1390. my $TIOCGPTN = 0x80045430;
  1391. sysopen($ptm, "/dev/ptmx", O_RDWR | O_NONBLOCK) or
  1392. dodie "Can't open /dev/ptmx";
  1393. # unlockpt()
  1394. $tmp = pack("i", 0);
  1395. ioctl($ptm, $TIOCSPTLCK, $tmp) or
  1396. dodie "ioctl TIOCSPTLCK for /dev/ptmx failed";
  1397. # ptsname()
  1398. ioctl($ptm, $TIOCGPTN, $tmp) or
  1399. dodie "ioctl TIOCGPTN for /dev/ptmx failed";
  1400. $tmp = unpack("i", $tmp);
  1401. sysopen($pts, "/dev/pts/$tmp", O_RDWR | O_NONBLOCK) or
  1402. dodie "Can't open /dev/pts/$tmp";
  1403. }
  1404. sub exec_console {
  1405. my ($ptm, $pts) = @_;
  1406. close($ptm);
  1407. close(\*STDIN);
  1408. close(\*STDOUT);
  1409. close(\*STDERR);
  1410. open(\*STDIN, '<&', $pts);
  1411. open(\*STDOUT, '>&', $pts);
  1412. open(\*STDERR, '>&', $pts);
  1413. close($pts);
  1414. exec $console or
  1415. dodie "Can't open console $console";
  1416. }
  1417. sub open_console {
  1418. my ($ptm) = @_;
  1419. my $pts = \*PTSFD;
  1420. my $pid;
  1421. # save terminal settings
  1422. $stty_orig = `stty -g`;
  1423. # place terminal in cbreak mode so that stdin can be read one character at
  1424. # a time without having to wait for a newline
  1425. system("stty -icanon -echo -icrnl");
  1426. create_pty($ptm, $pts);
  1427. $pid = fork;
  1428. if (!$pid) {
  1429. # child
  1430. exec_console($ptm, $pts)
  1431. }
  1432. # parent
  1433. close($pts);
  1434. return $pid;
  1435. open(PTSFD, "Stop perl from warning about single use of PTSFD");
  1436. }
  1437. sub close_console {
  1438. my ($fp, $pid) = @_;
  1439. doprint "kill child process $pid\n";
  1440. kill $close_console_signal, $pid;
  1441. doprint "wait for child process $pid to exit\n";
  1442. waitpid($pid, 0);
  1443. print "closing!\n";
  1444. close($fp);
  1445. # restore terminal settings
  1446. system("stty $stty_orig");
  1447. }
  1448. sub start_monitor {
  1449. if ($monitor_cnt++) {
  1450. return;
  1451. }
  1452. $monitor_fp = \*MONFD;
  1453. $monitor_pid = open_console $monitor_fp;
  1454. return;
  1455. open(MONFD, "Stop perl from warning about single use of MONFD");
  1456. }
  1457. sub end_monitor {
  1458. return if (!defined $console);
  1459. if (--$monitor_cnt) {
  1460. return;
  1461. }
  1462. close_console($monitor_fp, $monitor_pid);
  1463. }
  1464. sub wait_for_monitor {
  1465. my ($time, $stop) = @_;
  1466. my $full_line = "";
  1467. my $line;
  1468. my $booted = 0;
  1469. my $start_time = time;
  1470. my $skip_call_trace = 0;
  1471. my $bug = 0;
  1472. my $bug_ignored = 0;
  1473. my $now;
  1474. doprint "** Wait for monitor to settle down **\n";
  1475. # read the monitor and wait for the system to calm down
  1476. while (!$booted) {
  1477. $line = wait_for_input($monitor_fp, $time);
  1478. last if (!defined($line));
  1479. print "$line";
  1480. $full_line .= $line;
  1481. if (defined($stop) && $full_line =~ /$stop/) {
  1482. doprint "wait for monitor detected $stop\n";
  1483. $booted = 1;
  1484. }
  1485. if ($full_line =~ /\[ backtrace testing \]/) {
  1486. $skip_call_trace = 1;
  1487. }
  1488. if ($full_line =~ /call trace:/i) {
  1489. if (!$bug && !$skip_call_trace) {
  1490. if ($ignore_errors) {
  1491. $bug_ignored = 1;
  1492. } else {
  1493. $bug = 1;
  1494. }
  1495. }
  1496. }
  1497. if ($full_line =~ /\[ end of backtrace testing \]/) {
  1498. $skip_call_trace = 0;
  1499. }
  1500. if ($full_line =~ /Kernel panic -/) {
  1501. $bug = 1;
  1502. }
  1503. if ($line =~ /\n/) {
  1504. $full_line = "";
  1505. }
  1506. $now = time;
  1507. if ($now - $start_time >= $max_monitor_wait) {
  1508. doprint "Exiting monitor flush due to hitting MAX_MONITOR_WAIT\n";
  1509. return 1;
  1510. }
  1511. }
  1512. print "** Monitor flushed **\n";
  1513. # if stop is defined but wasn't hit, return error
  1514. # used by reboot (which wants to see a reboot)
  1515. if (defined($stop) && !$booted) {
  1516. $bug = 1;
  1517. }
  1518. return $bug;
  1519. }
  1520. sub save_logs {
  1521. my ($result, $basedir) = @_;
  1522. my @t = localtime;
  1523. my $date = sprintf "%04d%02d%02d%02d%02d%02d",
  1524. 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
  1525. my $type = $build_type;
  1526. if ($type =~ /useconfig/) {
  1527. $type = "useconfig";
  1528. }
  1529. my $dir = "$machine-$test_type-$type-$result-$date";
  1530. $dir = "$basedir/$dir";
  1531. if (!-d $dir) {
  1532. mkpath($dir) or
  1533. dodie "can't create $dir";
  1534. }
  1535. my %files = (
  1536. "config" => $output_config,
  1537. "buildlog" => $buildlog,
  1538. "dmesg" => $dmesg,
  1539. "testlog" => $testlog,
  1540. );
  1541. while (my ($name, $source) = each(%files)) {
  1542. if (-f "$source") {
  1543. cp "$source", "$dir/$name" or
  1544. dodie "failed to copy $source";
  1545. }
  1546. }
  1547. doprint "*** Saved info to $dir ***\n";
  1548. }
  1549. sub fail {
  1550. if ($die_on_failure) {
  1551. dodie @_;
  1552. }
  1553. doprint "FAILED\n";
  1554. my $i = $iteration;
  1555. # no need to reboot for just building.
  1556. if (!do_not_reboot) {
  1557. doprint "REBOOTING\n";
  1558. reboot_to_good $sleep_time;
  1559. }
  1560. my $name = "";
  1561. if (defined($test_name)) {
  1562. $name = " ($test_name)";
  1563. }
  1564. print_times;
  1565. doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  1566. doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  1567. doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
  1568. doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  1569. doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  1570. if (defined($store_failures)) {
  1571. save_logs "fail", $store_failures;
  1572. }
  1573. if (defined($post_test)) {
  1574. run_command $post_test;
  1575. }
  1576. return 1;
  1577. }
  1578. sub run_command {
  1579. my ($command, $redirect, $timeout) = @_;
  1580. my $start_time;
  1581. my $end_time;
  1582. my $dolog = 0;
  1583. my $dord = 0;
  1584. my $dostdout = 0;
  1585. my $pid;
  1586. my $command_orig = $command;
  1587. $command =~ s/\$SSH_USER/$ssh_user/g;
  1588. $command =~ s/\$MACHINE/$machine/g;
  1589. if (!defined($timeout)) {
  1590. $timeout = $run_timeout;
  1591. }
  1592. if (!defined($timeout)) {
  1593. $timeout = -1; # tell wait_for_input to wait indefinitely
  1594. }
  1595. doprint("$command ... ");
  1596. $start_time = time;
  1597. $pid = open(CMD, "$command 2>&1 |") or
  1598. (fail "unable to exec $command" and return 0);
  1599. if (defined($opt{"LOG_FILE"})) {
  1600. $dolog = 1;
  1601. }
  1602. if (defined($redirect)) {
  1603. if ($redirect eq 1) {
  1604. $dostdout = 1;
  1605. # Have the output of the command on its own line
  1606. doprint "\n";
  1607. } else {
  1608. open (RD, ">$redirect") or
  1609. dodie "failed to write to redirect $redirect";
  1610. $dord = 1;
  1611. }
  1612. }
  1613. my $hit_timeout = 0;
  1614. while (1) {
  1615. my $fp = \*CMD;
  1616. my $line = wait_for_input($fp, $timeout);
  1617. if (!defined($line)) {
  1618. my $now = time;
  1619. if ($timeout >= 0 && (($now - $start_time) >= $timeout)) {
  1620. doprint "Hit timeout of $timeout, killing process\n";
  1621. $hit_timeout = 1;
  1622. kill 9, $pid;
  1623. }
  1624. last;
  1625. }
  1626. print LOG $line if ($dolog);
  1627. print RD $line if ($dord);
  1628. print $line if ($dostdout);
  1629. }
  1630. waitpid($pid, 0);
  1631. # shift 8 for real exit status
  1632. $run_command_status = $? >> 8;
  1633. if ($command_orig eq $default{REBOOT} &&
  1634. $run_command_status == $reboot_return_code) {
  1635. $run_command_status = 0;
  1636. }
  1637. close(CMD);
  1638. close(RD) if ($dord);
  1639. $end_time = time;
  1640. my $delta = $end_time - $start_time;
  1641. if ($delta == 1) {
  1642. doprint "[1 second] ";
  1643. } else {
  1644. doprint "[$delta seconds] ";
  1645. }
  1646. if ($hit_timeout) {
  1647. $run_command_status = 1;
  1648. }
  1649. if ($run_command_status) {
  1650. doprint "FAILED!\n";
  1651. } else {
  1652. doprint "SUCCESS\n";
  1653. }
  1654. return !$run_command_status;
  1655. }
  1656. sub run_ssh {
  1657. my ($cmd, $timeout) = @_;
  1658. my $cp_exec = $ssh_exec;
  1659. $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
  1660. return run_command "$cp_exec", undef , $timeout;
  1661. }
  1662. sub run_scp {
  1663. my ($src, $dst, $cp_scp) = @_;
  1664. $cp_scp =~ s/\$SRC_FILE/$src/g;
  1665. $cp_scp =~ s/\$DST_FILE/$dst/g;
  1666. return run_command "$cp_scp";
  1667. }
  1668. sub run_scp_install {
  1669. my ($src, $dst) = @_;
  1670. my $cp_scp = $scp_to_target_install;
  1671. return run_scp($src, $dst, $cp_scp);
  1672. }
  1673. sub run_scp_mod {
  1674. my ($src, $dst) = @_;
  1675. my $cp_scp = $scp_to_target;
  1676. return run_scp($src, $dst, $cp_scp);
  1677. }
  1678. sub _get_grub_index {
  1679. my ($command, $target, $skip, $submenu) = @_;
  1680. return if (defined($grub_number) && defined($last_grub_menu) &&
  1681. $last_grub_menu eq $grub_menu && defined($last_machine) &&
  1682. $last_machine eq $machine);
  1683. doprint "Find $reboot_type menu ... ";
  1684. $grub_number = -1;
  1685. my $ssh_grub = $ssh_exec;
  1686. $ssh_grub =~ s,\$SSH_COMMAND,$command,g;
  1687. open(IN, "$ssh_grub |") or
  1688. dodie "unable to execute $command";
  1689. my $found = 0;
  1690. my $submenu_number = 0;
  1691. while (<IN>) {
  1692. if (/$target/) {
  1693. $grub_number++;
  1694. $found = 1;
  1695. last;
  1696. } elsif (defined($submenu) && /$submenu/) {
  1697. $submenu_number++;
  1698. $grub_number = -1;
  1699. } elsif (/$skip/) {
  1700. $grub_number++;
  1701. }
  1702. }
  1703. close(IN);
  1704. dodie "Could not find '$grub_menu' through $command on $machine"
  1705. if (!$found);
  1706. if ($submenu_number > 0) {
  1707. $grub_number = "$submenu_number>$grub_number";
  1708. }
  1709. doprint "$grub_number\n";
  1710. $last_grub_menu = $grub_menu;
  1711. $last_machine = $machine;
  1712. }
  1713. sub get_grub_index {
  1714. my $command;
  1715. my $target;
  1716. my $skip;
  1717. my $submenu;
  1718. my $grub_menu_qt;
  1719. if ($reboot_type !~ /^grub/) {
  1720. return;
  1721. }
  1722. $grub_menu_qt = quotemeta($grub_menu);
  1723. if ($reboot_type eq "grub") {
  1724. $command = "cat /boot/grub/menu.lst";
  1725. $target = '^\s*title\s+' . $grub_menu_qt . '\s*$';
  1726. $skip = '^\s*title\s';
  1727. } elsif ($reboot_type eq "grub2") {
  1728. $command = "cat $grub_file";
  1729. $target = '^\s*menuentry.*' . $grub_menu_qt;
  1730. $skip = '^\s*menuentry\s';
  1731. $submenu = '^\s*submenu\s';
  1732. } elsif ($reboot_type eq "grub2bls") {
  1733. $command = $grub_bls_get;
  1734. $target = '^title=.*' . $grub_menu_qt;
  1735. $skip = '^title=';
  1736. } else {
  1737. return;
  1738. }
  1739. _get_grub_index($command, $target, $skip, $submenu);
  1740. }
  1741. sub wait_for_input {
  1742. my ($fp, $time) = @_;
  1743. my $start_time;
  1744. my $rin;
  1745. my $rout;
  1746. my $nr;
  1747. my $buf;
  1748. my $line;
  1749. my $ch;
  1750. if (!defined($time)) {
  1751. $time = $timeout;
  1752. }
  1753. if ($time < 0) {
  1754. # Negative number means wait indefinitely
  1755. undef $time;
  1756. }
  1757. $rin = '';
  1758. vec($rin, fileno($fp), 1) = 1;
  1759. vec($rin, fileno(\*STDIN), 1) = 1;
  1760. $start_time = time;
  1761. while (1) {
  1762. $nr = select($rout=$rin, undef, undef, $time);
  1763. last if ($nr <= 0);
  1764. # copy data from stdin to the console
  1765. if (vec($rout, fileno(\*STDIN), 1) == 1) {
  1766. $nr = sysread(\*STDIN, $buf, 1000);
  1767. syswrite($fp, $buf, $nr) if ($nr > 0);
  1768. }
  1769. # The timeout is based on time waiting for the fp data
  1770. if (vec($rout, fileno($fp), 1) != 1) {
  1771. last if (defined($time) && (time - $start_time > $time));
  1772. next;
  1773. }
  1774. $line = "";
  1775. # try to read one char at a time
  1776. while (sysread $fp, $ch, 1) {
  1777. $line .= $ch;
  1778. last if ($ch eq "\n");
  1779. }
  1780. last if (!length($line));
  1781. return $line;
  1782. }
  1783. return undef;
  1784. }
  1785. sub reboot_to {
  1786. if (defined($switch_to_test)) {
  1787. run_command $switch_to_test;
  1788. }
  1789. if ($reboot_type eq "grub") {
  1790. run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
  1791. } elsif (($reboot_type eq "grub2") or ($reboot_type eq "grub2bls")) {
  1792. run_ssh "$grub_reboot \"'$grub_number'\"";
  1793. } elsif ($reboot_type eq "syslinux") {
  1794. run_ssh "$syslinux --once \\\"$syslinux_label\\\" $syslinux_path";
  1795. } elsif (defined $reboot_script) {
  1796. run_command "$reboot_script";
  1797. }
  1798. reboot;
  1799. }
  1800. sub get_sha1 {
  1801. my ($commit) = @_;
  1802. doprint "git rev-list --max-count=1 $commit ... ";
  1803. my $sha1 = `git rev-list --max-count=1 $commit`;
  1804. my $ret = $?;
  1805. logit $sha1;
  1806. if ($ret) {
  1807. doprint "FAILED\n";
  1808. dodie "Failed to get git $commit";
  1809. }
  1810. print "SUCCESS\n";
  1811. chomp $sha1;
  1812. return $sha1;
  1813. }
  1814. sub monitor {
  1815. my $booted = 0;
  1816. my $bug = 0;
  1817. my $bug_ignored = 0;
  1818. my $skip_call_trace = 0;
  1819. my $loops;
  1820. my $start_time = time;
  1821. wait_for_monitor 5;
  1822. my $line;
  1823. my $full_line = "";
  1824. open(DMESG, "> $dmesg") or
  1825. dodie "unable to write to $dmesg";
  1826. reboot_to;
  1827. my $success_start;
  1828. my $failure_start;
  1829. my $monitor_start = time;
  1830. my $done = 0;
  1831. my $version_found = 0;
  1832. while (!$done) {
  1833. if ($bug && defined($stop_after_failure) &&
  1834. $stop_after_failure >= 0) {
  1835. my $time = $stop_after_failure - (time - $failure_start);
  1836. $line = wait_for_input($monitor_fp, $time);
  1837. if (!defined($line)) {
  1838. doprint "bug timed out after $booted_timeout seconds\n";
  1839. doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
  1840. last;
  1841. }
  1842. } elsif ($booted) {
  1843. $line = wait_for_input($monitor_fp, $booted_timeout);
  1844. if (!defined($line)) {
  1845. my $s = $booted_timeout == 1 ? "" : "s";
  1846. doprint "Successful boot found: break after $booted_timeout second$s\n";
  1847. last;
  1848. }
  1849. } else {
  1850. $line = wait_for_input($monitor_fp);
  1851. if (!defined($line)) {
  1852. my $s = $timeout == 1 ? "" : "s";
  1853. doprint "Timed out after $timeout second$s\n";
  1854. last;
  1855. }
  1856. }
  1857. doprint $line;
  1858. print DMESG $line;
  1859. # we are not guaranteed to get a full line
  1860. $full_line .= $line;
  1861. if ($full_line =~ /$success_line/) {
  1862. $booted = 1;
  1863. $success_start = time;
  1864. }
  1865. if ($booted && defined($stop_after_success) &&
  1866. $stop_after_success >= 0) {
  1867. my $now = time;
  1868. if ($now - $success_start >= $stop_after_success) {
  1869. doprint "Test forced to stop after $stop_after_success seconds after success\n";
  1870. last;
  1871. }
  1872. }
  1873. if ($full_line =~ /\[ backtrace testing \]/) {
  1874. $skip_call_trace = 1;
  1875. }
  1876. if ($full_line =~ /call trace:/i) {
  1877. if (!$bug && !$skip_call_trace) {
  1878. if ($ignore_errors) {
  1879. $bug_ignored = 1;
  1880. } else {
  1881. $bug = 1;
  1882. $failure_start = time;
  1883. }
  1884. }
  1885. }
  1886. if ($bug && defined($stop_after_failure) &&
  1887. $stop_after_failure >= 0) {
  1888. my $now = time;
  1889. if ($now - $failure_start >= $stop_after_failure) {
  1890. doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
  1891. last;
  1892. }
  1893. }
  1894. if ($full_line =~ /\[ end of backtrace testing \]/) {
  1895. $skip_call_trace = 0;
  1896. }
  1897. if ($full_line =~ /Kernel panic -/) {
  1898. $failure_start = time;
  1899. $bug = 1;
  1900. }
  1901. # Detect triple faults by testing the banner
  1902. if ($full_line =~ /\bLinux version (\S+).*\n/) {
  1903. if ($1 eq $version) {
  1904. $version_found = 1;
  1905. } elsif ($version_found && $detect_triplefault) {
  1906. # We already booted into the kernel we are testing,
  1907. # but now we booted into another kernel?
  1908. # Consider this a triple fault.
  1909. doprint "Already booted in Linux kernel $version, but now\n";
  1910. doprint "we booted into Linux kernel $1.\n";
  1911. doprint "Assuming that this is a triple fault.\n";
  1912. doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
  1913. last;
  1914. }
  1915. }
  1916. if ($line =~ /\n/) {
  1917. $full_line = "";
  1918. }
  1919. if ($stop_test_after > 0 && !$booted && !$bug) {
  1920. if (time - $monitor_start > $stop_test_after) {
  1921. doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
  1922. $done = 1;
  1923. }
  1924. }
  1925. }
  1926. my $end_time = time;
  1927. $reboot_time = $end_time - $start_time;
  1928. close(DMESG);
  1929. if ($bug) {
  1930. return 0 if ($in_bisect);
  1931. fail "failed - got a bug report" and return 0;
  1932. }
  1933. if (!$booted) {
  1934. return 0 if ($in_bisect);
  1935. fail "failed - never got a boot prompt." and return 0;
  1936. }
  1937. if ($bug_ignored) {
  1938. doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
  1939. }
  1940. return 1;
  1941. }
  1942. sub eval_kernel_version {
  1943. my ($option) = @_;
  1944. $option =~ s/\$KERNEL_VERSION/$version/g;
  1945. return $option;
  1946. }
  1947. sub do_post_install {
  1948. return if (!defined($post_install));
  1949. my $cp_post_install = eval_kernel_version $post_install;
  1950. run_command "$cp_post_install" or
  1951. dodie "Failed to run post install";
  1952. }
  1953. # Sometimes the reboot fails, and will hang. We try to ssh to the box
  1954. # and if we fail, we force another reboot, that should powercycle it.
  1955. sub test_booted {
  1956. if (!run_ssh "echo testing connection") {
  1957. reboot $sleep_time;
  1958. }
  1959. }
  1960. sub install {
  1961. return if ($no_install);
  1962. my $start_time = time;
  1963. if (defined($pre_install)) {
  1964. my $cp_pre_install = eval_kernel_version $pre_install;
  1965. run_command "$cp_pre_install" or
  1966. dodie "Failed to run pre install";
  1967. }
  1968. my $cp_target = eval_kernel_version $target_image;
  1969. test_booted;
  1970. run_scp_install "$outputdir/$build_target", "$cp_target" or
  1971. dodie "failed to copy image";
  1972. my $install_mods = 0;
  1973. # should we process modules?
  1974. $install_mods = 0;
  1975. open(IN, "$output_config") or dodie("Can't read config file");
  1976. while (<IN>) {
  1977. if (/CONFIG_MODULES(=y)?/) {
  1978. if (defined($1)) {
  1979. $install_mods = 1;
  1980. last;
  1981. }
  1982. }
  1983. }
  1984. close(IN);
  1985. if (!$install_mods) {
  1986. do_post_install;
  1987. doprint "No modules needed\n";
  1988. my $end_time = time;
  1989. $install_time = $end_time - $start_time;
  1990. return;
  1991. }
  1992. run_command "$make INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=$tmpdir modules_install" or
  1993. dodie "Failed to install modules";
  1994. my $modlib = "/lib/modules/$version";
  1995. my $modtar = "ktest-mods.tar.bz2";
  1996. run_ssh "rm -rf $modlib" or
  1997. dodie "failed to remove old mods: $modlib";
  1998. # would be nice if scp -r did not follow symbolic links
  1999. run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
  2000. dodie "making tarball";
  2001. run_scp_mod "$tmpdir/$modtar", "/tmp" or
  2002. dodie "failed to copy modules";
  2003. unlink "$tmpdir/$modtar";
  2004. run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
  2005. dodie "failed to tar modules";
  2006. run_ssh "rm -f /tmp/$modtar";
  2007. do_post_install;
  2008. my $end_time = time;
  2009. $install_time = $end_time - $start_time;
  2010. }
  2011. sub get_version {
  2012. # get the release name
  2013. return if ($have_version);
  2014. doprint "$make kernelrelease ... ";
  2015. $version = `$make -s kernelrelease | tail -1`;
  2016. if (!length($version)) {
  2017. run_command "$make allnoconfig" or return 0;
  2018. doprint "$make kernelrelease ... ";
  2019. $version = `$make -s kernelrelease | tail -1`;
  2020. }
  2021. chomp($version);
  2022. doprint "$version\n";
  2023. $have_version = 1;
  2024. }
  2025. sub start_monitor_and_install {
  2026. # Make sure the stable kernel has finished booting
  2027. # Install bisects, don't need console
  2028. if (defined $console) {
  2029. start_monitor;
  2030. wait_for_monitor 5;
  2031. end_monitor;
  2032. }
  2033. get_grub_index;
  2034. get_version;
  2035. install;
  2036. start_monitor if (defined $console);
  2037. return monitor;
  2038. }
  2039. sub process_warning_line {
  2040. my ($line) = @_;
  2041. chomp $line;
  2042. # for distcc heterogeneous systems, some compilers
  2043. # do things differently causing warning lines
  2044. # to be slightly different. This makes an attempt
  2045. # to fixe those issues.
  2046. # chop off the index into the line
  2047. # using distcc, some compilers give different indexes
  2048. # depending on white space
  2049. $line =~ s/^(\s*\S+:\d+:)\d+/$1/;
  2050. # Some compilers use UTF-8 extended for quotes and some don't.
  2051. $line =~ s/$utf8_quote/'/g;
  2052. return $line;
  2053. }
  2054. # Read buildlog and check against warnings file for any
  2055. # new warnings.
  2056. #
  2057. # Returns 1 if OK
  2058. # 0 otherwise
  2059. sub check_buildlog {
  2060. my %warnings_list;
  2061. # Failed builds should not reboot the target
  2062. my $save_no_reboot = $no_reboot;
  2063. $no_reboot = 1;
  2064. if (-f $warnings_file) {
  2065. open(IN, $warnings_file) or
  2066. dodie "Error opening $warnings_file";
  2067. while (<IN>) {
  2068. if (/$check_build_re/) {
  2069. my $warning = process_warning_line $_;
  2070. $warnings_list{$warning} = 1;
  2071. }
  2072. }
  2073. close(IN);
  2074. }
  2075. open(IN, $buildlog) or dodie "Can't open $buildlog";
  2076. while (<IN>) {
  2077. if (/$check_build_re/) {
  2078. my $warning = process_warning_line $_;
  2079. if (!defined $warnings_list{$warning}) {
  2080. $warning_found++;
  2081. # If warnings file didn't exist, and WARNINGS_FILE exist,
  2082. # then we fail on any warning!
  2083. if (defined $warnings_file) {
  2084. fail "New warning found (not in $warnings_file)\n$_\n";
  2085. $no_reboot = $save_no_reboot;
  2086. return 0;
  2087. }
  2088. }
  2089. }
  2090. }
  2091. $no_reboot = $save_no_reboot;
  2092. close(IN);
  2093. }
  2094. sub check_patch_buildlog {
  2095. my ($patch) = @_;
  2096. my @files = `git show $patch | diffstat -l`;
  2097. foreach my $file (@files) {
  2098. chomp $file;
  2099. }
  2100. open(IN, "git show $patch |") or
  2101. dodie "failed to show $patch";
  2102. while (<IN>) {
  2103. if (m,^--- a/(.*),) {
  2104. chomp $1;
  2105. $files[$#files] = $1;
  2106. }
  2107. }
  2108. close(IN);
  2109. open(IN, $buildlog) or dodie "Can't open $buildlog";
  2110. while (<IN>) {
  2111. if (/^\s*(.*?):.*(warning|error)/) {
  2112. my $err = $1;
  2113. foreach my $file (@files) {
  2114. my $fullpath = "$builddir/$file";
  2115. if ($file eq $err || $fullpath eq $err) {
  2116. fail "$file built with warnings" and return 0;
  2117. }
  2118. }
  2119. }
  2120. }
  2121. close(IN);
  2122. return 1;
  2123. }
  2124. sub apply_min_config {
  2125. my $outconfig = "$output_config.new";
  2126. # Read the config file and remove anything that
  2127. # is in the force_config hash (from minconfig and others)
  2128. # then add the force config back.
  2129. doprint "Applying minimum configurations into $output_config.new\n";
  2130. open (OUT, ">$outconfig") or
  2131. dodie "Can't create $outconfig";
  2132. if (-f $output_config) {
  2133. open (IN, $output_config) or
  2134. dodie "Failed to open $output_config";
  2135. while (<IN>) {
  2136. if (/^(# )?(CONFIG_[^\s=]*)/) {
  2137. next if (defined($force_config{$2}));
  2138. }
  2139. print OUT;
  2140. }
  2141. close IN;
  2142. }
  2143. foreach my $config (keys %force_config) {
  2144. print OUT "$force_config{$config}\n";
  2145. }
  2146. close OUT;
  2147. run_command "mv $outconfig $output_config";
  2148. }
  2149. sub make_oldconfig {
  2150. my @force_list = keys %force_config;
  2151. if ($#force_list >= 0) {
  2152. apply_min_config;
  2153. }
  2154. if (!run_command "$make olddefconfig") {
  2155. # Perhaps olddefconfig doesn't exist in this version of the kernel
  2156. # try oldnoconfig
  2157. doprint "olddefconfig failed, trying make oldnoconfig\n";
  2158. if (!run_command "$make oldnoconfig") {
  2159. doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
  2160. # try a yes '' | oldconfig
  2161. run_command "yes '' | $make oldconfig" or
  2162. dodie "failed make config oldconfig";
  2163. }
  2164. }
  2165. }
  2166. # read a config file and use this to force new configs.
  2167. sub load_force_config {
  2168. my ($config) = @_;
  2169. doprint "Loading force configs from $config\n";
  2170. open(IN, $config) or
  2171. dodie "failed to read $config";
  2172. while (<IN>) {
  2173. chomp;
  2174. if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
  2175. $force_config{$1} = $_;
  2176. } elsif (/^# (CONFIG_\S*) is not set/) {
  2177. $force_config{$1} = $_;
  2178. }
  2179. }
  2180. close IN;
  2181. }
  2182. sub build {
  2183. my ($type) = @_;
  2184. unlink $buildlog;
  2185. my $start_time = time;
  2186. # Failed builds should not reboot the target
  2187. my $save_no_reboot = $no_reboot;
  2188. $no_reboot = 1;
  2189. # Calculate a new version from here.
  2190. $have_version = 0;
  2191. if (defined($pre_build)) {
  2192. my $ret = run_command $pre_build;
  2193. if (!$ret && defined($pre_build_die) &&
  2194. $pre_build_die) {
  2195. dodie "failed to pre_build\n";
  2196. }
  2197. }
  2198. if ($type =~ /^useconfig:(.*)/) {
  2199. run_command "cp $1 $output_config" or
  2200. dodie "could not copy $1 to .config";
  2201. $type = "oldconfig";
  2202. }
  2203. # old config can ask questions
  2204. if ($type eq "oldconfig") {
  2205. $type = "olddefconfig";
  2206. # allow for empty configs
  2207. run_command "touch $output_config";
  2208. if (!$noclean) {
  2209. run_command "mv $output_config $outputdir/config_temp" or
  2210. dodie "moving .config";
  2211. run_command "$make mrproper" or dodie "make mrproper";
  2212. run_command "mv $outputdir/config_temp $output_config" or
  2213. dodie "moving config_temp";
  2214. }
  2215. } elsif (!$noclean) {
  2216. unlink "$output_config";
  2217. run_command "$make mrproper" or
  2218. dodie "make mrproper";
  2219. }
  2220. # add something to distinguish this build
  2221. open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
  2222. print OUT "$localversion\n";
  2223. close(OUT);
  2224. if (defined($minconfig)) {
  2225. load_force_config($minconfig);
  2226. }
  2227. if ($type ne "olddefconfig") {
  2228. run_command "$make $type" or
  2229. dodie "failed make config";
  2230. }
  2231. # Run old config regardless, to enforce min configurations
  2232. make_oldconfig;
  2233. if (not defined($build_options)){
  2234. $build_options = "";
  2235. }
  2236. my $build_ret = run_command "$make $build_options", $buildlog;
  2237. if (defined($post_build)) {
  2238. # Because a post build may change the kernel version
  2239. # do it now.
  2240. get_version;
  2241. my $ret = run_command $post_build;
  2242. if (!$ret && defined($post_build_die) &&
  2243. $post_build_die) {
  2244. dodie "failed to post_build\n";
  2245. }
  2246. }
  2247. if (!$build_ret) {
  2248. # bisect may need this to pass
  2249. if ($in_bisect) {
  2250. $no_reboot = $save_no_reboot;
  2251. return 0;
  2252. }
  2253. fail "failed build" and return 0;
  2254. }
  2255. $no_reboot = $save_no_reboot;
  2256. my $end_time = time;
  2257. $build_time = $end_time - $start_time;
  2258. return 1;
  2259. }
  2260. sub halt {
  2261. if (!run_ssh "halt" or defined($power_off)) {
  2262. if (defined($poweroff_after_halt)) {
  2263. sleep $poweroff_after_halt;
  2264. run_command "$power_off";
  2265. }
  2266. } else {
  2267. # nope? the zap it!
  2268. run_command "$power_off";
  2269. }
  2270. }
  2271. sub success {
  2272. my ($i) = @_;
  2273. $successes++;
  2274. my $name = "";
  2275. if (defined($test_name)) {
  2276. $name = " ($test_name)";
  2277. }
  2278. print_times;
  2279. doprint "\n\n";
  2280. doprint "*******************************************\n";
  2281. doprint "*******************************************\n";
  2282. doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
  2283. doprint "*******************************************\n";
  2284. doprint "*******************************************\n";
  2285. if (defined($store_successes)) {
  2286. save_logs "success", $store_successes;
  2287. }
  2288. if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
  2289. doprint "Reboot and wait $sleep_time seconds\n";
  2290. reboot_to_good $sleep_time;
  2291. }
  2292. if (defined($post_test)) {
  2293. run_command $post_test;
  2294. }
  2295. }
  2296. sub answer_bisect {
  2297. for (;;) {
  2298. doprint "Pass, fail, or skip? [p/f/s]";
  2299. my $ans = <STDIN>;
  2300. chomp $ans;
  2301. if ($ans eq "p" || $ans eq "P") {
  2302. return 1;
  2303. } elsif ($ans eq "f" || $ans eq "F") {
  2304. return 0;
  2305. } elsif ($ans eq "s" || $ans eq "S") {
  2306. return -1;
  2307. } else {
  2308. print "Please answer 'p', 'f', or 's'\n";
  2309. }
  2310. }
  2311. }
  2312. sub child_run_test {
  2313. # child should have no power
  2314. $reboot_on_error = 0;
  2315. $poweroff_on_error = 0;
  2316. $die_on_failure = 1;
  2317. run_command $run_test, $testlog;
  2318. exit $run_command_status;
  2319. }
  2320. sub child_finished {
  2321. $child_done = 1;
  2322. }
  2323. sub do_run_test {
  2324. my $child_pid;
  2325. my $child_exit;
  2326. my $line;
  2327. my $full_line;
  2328. my $bug = 0;
  2329. my $bug_ignored = 0;
  2330. my $start_time = time;
  2331. wait_for_monitor 1;
  2332. doprint "run test $run_test\n";
  2333. $child_done = 0;
  2334. $SIG{CHLD} = qw(child_finished);
  2335. $child_pid = fork;
  2336. child_run_test if (!$child_pid);
  2337. $full_line = "";
  2338. do {
  2339. $line = wait_for_input($monitor_fp, 1);
  2340. if (defined($line)) {
  2341. # we are not guaranteed to get a full line
  2342. $full_line .= $line;
  2343. doprint $line;
  2344. if ($full_line =~ /call trace:/i) {
  2345. if ($ignore_errors) {
  2346. $bug_ignored = 1;
  2347. } else {
  2348. $bug = 1;
  2349. }
  2350. }
  2351. if ($full_line =~ /Kernel panic -/) {
  2352. $bug = 1;
  2353. }
  2354. if ($line =~ /\n/) {
  2355. $full_line = "";
  2356. }
  2357. }
  2358. } while (!$child_done && !$bug);
  2359. if (!$bug && $bug_ignored) {
  2360. doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
  2361. }
  2362. if ($bug) {
  2363. my $failure_start = time;
  2364. my $now;
  2365. do {
  2366. $line = wait_for_input($monitor_fp, 1);
  2367. if (defined($line)) {
  2368. doprint $line;
  2369. }
  2370. $now = time;
  2371. if ($now - $failure_start >= $stop_after_failure) {
  2372. last;
  2373. }
  2374. } while (defined($line));
  2375. doprint "Detected kernel crash!\n";
  2376. # kill the child with extreme prejudice
  2377. kill 9, $child_pid;
  2378. }
  2379. waitpid $child_pid, 0;
  2380. $child_exit = $? >> 8;
  2381. my $end_time = time;
  2382. $test_time = $end_time - $start_time;
  2383. if (!$bug && $in_bisect) {
  2384. if (defined($bisect_ret_good)) {
  2385. if ($child_exit == $bisect_ret_good) {
  2386. return 1;
  2387. }
  2388. }
  2389. if (defined($bisect_ret_skip)) {
  2390. if ($child_exit == $bisect_ret_skip) {
  2391. return -1;
  2392. }
  2393. }
  2394. if (defined($bisect_ret_abort)) {
  2395. if ($child_exit == $bisect_ret_abort) {
  2396. fail "test abort" and return -2;
  2397. }
  2398. }
  2399. if (defined($bisect_ret_bad)) {
  2400. if ($child_exit == $bisect_ret_skip) {
  2401. return 0;
  2402. }
  2403. }
  2404. if (defined($bisect_ret_default)) {
  2405. if ($bisect_ret_default eq "good") {
  2406. return 1;
  2407. } elsif ($bisect_ret_default eq "bad") {
  2408. return 0;
  2409. } elsif ($bisect_ret_default eq "skip") {
  2410. return -1;
  2411. } elsif ($bisect_ret_default eq "abort") {
  2412. return -2;
  2413. } else {
  2414. fail "unknown default action: $bisect_ret_default"
  2415. and return -2;
  2416. }
  2417. }
  2418. }
  2419. if ($bug || $child_exit) {
  2420. return 0 if $in_bisect;
  2421. fail "test failed" and return 0;
  2422. }
  2423. return 1;
  2424. }
  2425. sub run_git_bisect {
  2426. my ($command) = @_;
  2427. doprint "$command ... ";
  2428. my $output = `$command 2>&1`;
  2429. my $ret = $?;
  2430. logit $output;
  2431. if ($ret) {
  2432. doprint "FAILED\n";
  2433. dodie "Failed to git bisect";
  2434. }
  2435. doprint "SUCCESS\n";
  2436. if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
  2437. doprint "$1 [$2]\n";
  2438. } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
  2439. $bisect_bad_commit = $1;
  2440. doprint "Found bad commit... $1\n";
  2441. return 0;
  2442. } else {
  2443. # we already logged it, just print it now.
  2444. print $output;
  2445. }
  2446. return 1;
  2447. }
  2448. sub bisect_reboot {
  2449. doprint "Reboot and sleep $bisect_sleep_time seconds\n";
  2450. reboot_to_good $bisect_sleep_time;
  2451. }
  2452. # returns 1 on success, 0 on failure, -1 on skip
  2453. sub run_bisect_test {
  2454. my ($type, $buildtype) = @_;
  2455. my $failed = 0;
  2456. my $result;
  2457. $in_bisect = 1;
  2458. build $buildtype or $failed = 1;
  2459. if ($type ne "build") {
  2460. if ($failed && $bisect_skip) {
  2461. $in_bisect = 0;
  2462. return -1;
  2463. }
  2464. dodie "Failed on build" if $failed;
  2465. # Now boot the box
  2466. start_monitor_and_install or $failed = 1;
  2467. if ($type ne "boot") {
  2468. if ($failed && $bisect_skip) {
  2469. end_monitor;
  2470. bisect_reboot;
  2471. $in_bisect = 0;
  2472. return -1;
  2473. }
  2474. dodie "Failed on boot" if $failed;
  2475. do_run_test or $failed = 1;
  2476. }
  2477. end_monitor;
  2478. }
  2479. if ($failed) {
  2480. $result = 0;
  2481. } else {
  2482. $result = 1;
  2483. }
  2484. # reboot the box to a kernel we can ssh to
  2485. if ($type ne "build") {
  2486. bisect_reboot;
  2487. }
  2488. $in_bisect = 0;
  2489. return $result;
  2490. }
  2491. sub run_bisect {
  2492. my ($type) = @_;
  2493. my $buildtype = "oldconfig";
  2494. # We should have a minconfig to use?
  2495. if (defined($minconfig)) {
  2496. $buildtype = "useconfig:$minconfig";
  2497. }
  2498. # If the user sets bisect_tries to less than 1, then no tries
  2499. # is a success.
  2500. my $ret = 1;
  2501. # Still let the user manually decide that though.
  2502. if ($bisect_tries < 1 && $bisect_manual) {
  2503. $ret = answer_bisect;
  2504. }
  2505. for (my $i = 0; $i < $bisect_tries; $i++) {
  2506. if ($bisect_tries > 1) {
  2507. my $t = $i + 1;
  2508. doprint("Running bisect trial $t of $bisect_tries:\n");
  2509. }
  2510. $ret = run_bisect_test $type, $buildtype;
  2511. if ($bisect_manual) {
  2512. $ret = answer_bisect;
  2513. }
  2514. last if (!$ret);
  2515. }
  2516. # Are we looking for where it worked, not failed?
  2517. if ($reverse_bisect && $ret >= 0) {
  2518. $ret = !$ret;
  2519. }
  2520. if ($ret > 0) {
  2521. return "good";
  2522. } elsif ($ret == 0) {
  2523. return "bad";
  2524. } elsif ($bisect_skip) {
  2525. doprint "HIT A BAD COMMIT ... SKIPPING\n";
  2526. return "skip";
  2527. }
  2528. }
  2529. sub update_bisect_replay {
  2530. my $tmp_log = "$tmpdir/ktest_bisect_log";
  2531. run_command "git bisect log > $tmp_log" or
  2532. dodie "can't create bisect log";
  2533. return $tmp_log;
  2534. }
  2535. sub bisect {
  2536. my ($i) = @_;
  2537. my $result;
  2538. dodie "BISECT_GOOD[$i] not defined\n" if (!defined($bisect_good));
  2539. dodie "BISECT_BAD[$i] not defined\n" if (!defined($bisect_bad));
  2540. dodie "BISECT_TYPE[$i] not defined\n" if (!defined($bisect_type));
  2541. my $good = $bisect_good;
  2542. my $bad = $bisect_bad;
  2543. my $type = $bisect_type;
  2544. my $start = $bisect_start;
  2545. my $replay = $bisect_replay;
  2546. my $start_files = $bisect_files;
  2547. if (defined($start_files)) {
  2548. $start_files = " -- " . $start_files;
  2549. } else {
  2550. $start_files = "";
  2551. }
  2552. # convert to true sha1's
  2553. $good = get_sha1($good);
  2554. $bad = get_sha1($bad);
  2555. if (defined($bisect_reverse) && $bisect_reverse == 1) {
  2556. doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
  2557. $reverse_bisect = 1;
  2558. } else {
  2559. $reverse_bisect = 0;
  2560. }
  2561. # Can't have a test without having a test to run
  2562. if ($type eq "test" && !defined($run_test)) {
  2563. $type = "boot";
  2564. }
  2565. # Check if a bisect was running
  2566. my $bisect_start_file = "$builddir/.git/BISECT_START";
  2567. my $check = $bisect_check;
  2568. my $do_check = defined($check) && $check ne "0";
  2569. if ( -f $bisect_start_file ) {
  2570. print "Bisect in progress found\n";
  2571. if ($do_check) {
  2572. print " If you say yes, then no checks of good or bad will be done\n";
  2573. }
  2574. if (defined($replay)) {
  2575. print "** BISECT_REPLAY is defined in config file **";
  2576. print " Ignore config option and perform new git bisect log?\n";
  2577. if (read_ync " (yes, no, or cancel) ") {
  2578. $replay = update_bisect_replay;
  2579. $do_check = 0;
  2580. }
  2581. } elsif (read_yn "read git log and continue?") {
  2582. $replay = update_bisect_replay;
  2583. $do_check = 0;
  2584. }
  2585. }
  2586. if ($do_check) {
  2587. # get current HEAD
  2588. my $head = get_sha1("HEAD");
  2589. if ($check ne "good") {
  2590. doprint "TESTING BISECT BAD [$bad]\n";
  2591. run_command "git checkout $bad" or
  2592. dodie "Failed to checkout $bad";
  2593. $result = run_bisect $type;
  2594. if ($result ne "bad") {
  2595. fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
  2596. }
  2597. }
  2598. if ($check ne "bad") {
  2599. doprint "TESTING BISECT GOOD [$good]\n";
  2600. run_command "git checkout $good" or
  2601. dodie "Failed to checkout $good";
  2602. $result = run_bisect $type;
  2603. if ($result ne "good") {
  2604. fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
  2605. }
  2606. }
  2607. # checkout where we started
  2608. run_command "git checkout $head" or
  2609. dodie "Failed to checkout $head";
  2610. }
  2611. run_command "git bisect start$start_files" or
  2612. dodie "could not start bisect";
  2613. if (defined($replay)) {
  2614. run_command "git bisect replay $replay" or
  2615. dodie "failed to run replay";
  2616. } else {
  2617. run_command "git bisect good $good" or
  2618. dodie "could not set bisect good to $good";
  2619. run_git_bisect "git bisect bad $bad" or
  2620. dodie "could not set bisect bad to $bad";
  2621. }
  2622. if (defined($start)) {
  2623. run_command "git checkout $start" or
  2624. dodie "failed to checkout $start";
  2625. }
  2626. my $test;
  2627. do {
  2628. $result = run_bisect $type;
  2629. $test = run_git_bisect "git bisect $result";
  2630. print_times;
  2631. } while ($test);
  2632. run_command "git bisect log" or
  2633. dodie "could not capture git bisect log";
  2634. run_command "git bisect reset" or
  2635. dodie "could not reset git bisect";
  2636. doprint "Bad commit was [$bisect_bad_commit]\n";
  2637. success $i;
  2638. }
  2639. sub assign_configs {
  2640. my ($hash, $config) = @_;
  2641. doprint "Reading configs from $config\n";
  2642. open (IN, $config) or
  2643. dodie "Failed to read $config";
  2644. while (<IN>) {
  2645. chomp;
  2646. if (/^((CONFIG\S*)=.*)/) {
  2647. ${$hash}{$2} = $1;
  2648. } elsif (/^(# (CONFIG\S*) is not set)/) {
  2649. ${$hash}{$2} = $1;
  2650. }
  2651. }
  2652. close(IN);
  2653. }
  2654. sub process_config_ignore {
  2655. my ($config) = @_;
  2656. assign_configs \%config_ignore, $config;
  2657. }
  2658. sub get_dependencies {
  2659. my ($config) = @_;
  2660. my $arr = $dependency{$config};
  2661. if (!defined($arr)) {
  2662. return ();
  2663. }
  2664. my @deps = @{$arr};
  2665. foreach my $dep (@{$arr}) {
  2666. print "ADD DEP $dep\n";
  2667. @deps = (@deps, get_dependencies $dep);
  2668. }
  2669. return @deps;
  2670. }
  2671. sub save_config {
  2672. my ($pc, $file) = @_;
  2673. my %configs = %{$pc};
  2674. doprint "Saving configs into $file\n";
  2675. open(OUT, ">$file") or dodie "Can not write to $file";
  2676. foreach my $config (keys %configs) {
  2677. print OUT "$configs{$config}\n";
  2678. }
  2679. close(OUT);
  2680. }
  2681. sub create_config {
  2682. my ($name, $pc) = @_;
  2683. doprint "Creating old config from $name configs\n";
  2684. save_config $pc, $output_config;
  2685. make_oldconfig;
  2686. }
  2687. sub run_config_bisect_test {
  2688. my ($type) = @_;
  2689. my $ret = run_bisect_test $type, "oldconfig";
  2690. if ($bisect_manual) {
  2691. $ret = answer_bisect;
  2692. }
  2693. return $ret;
  2694. }
  2695. sub config_bisect_end {
  2696. my ($good, $bad) = @_;
  2697. my $diffexec = "diff -u";
  2698. if (-f "$builddir/scripts/diffconfig") {
  2699. $diffexec = "$builddir/scripts/diffconfig";
  2700. }
  2701. doprint "\n\n***************************************\n";
  2702. doprint "No more config bisecting possible.\n";
  2703. run_command "$diffexec $good $bad", 1;
  2704. doprint "***************************************\n\n";
  2705. }
  2706. sub run_config_bisect {
  2707. my ($good, $bad, $last_result) = @_;
  2708. my $reset = "";
  2709. my $cmd;
  2710. my $ret;
  2711. if (!length($last_result)) {
  2712. $reset = "-r";
  2713. }
  2714. run_command "$config_bisect_exec $reset -b $outputdir $good $bad $last_result", 1;
  2715. # config-bisect returns:
  2716. # 0 if there is more to bisect
  2717. # 1 for finding a good config
  2718. # 2 if it can not find any more configs
  2719. # -1 (255) on error
  2720. if ($run_command_status) {
  2721. return $run_command_status;
  2722. }
  2723. $ret = run_config_bisect_test $config_bisect_type;
  2724. if ($ret) {
  2725. doprint "NEW GOOD CONFIG ($pass)\n";
  2726. system("cp $output_config $tmpdir/good_config.tmp.$pass");
  2727. $pass++;
  2728. # Return 3 for good config
  2729. return 3;
  2730. } else {
  2731. doprint "NEW BAD CONFIG ($pass)\n";
  2732. system("cp $output_config $tmpdir/bad_config.tmp.$pass");
  2733. $pass++;
  2734. # Return 4 for bad config
  2735. return 4;
  2736. }
  2737. }
  2738. sub config_bisect {
  2739. my ($i) = @_;
  2740. my $good_config;
  2741. my $bad_config;
  2742. my $type = $config_bisect_type;
  2743. my $ret;
  2744. $bad_config = $config_bisect;
  2745. if (defined($config_bisect_good)) {
  2746. $good_config = $config_bisect_good;
  2747. } elsif (defined($minconfig)) {
  2748. $good_config = $minconfig;
  2749. } else {
  2750. doprint "No config specified, checking if defconfig works";
  2751. $ret = run_bisect_test $type, "defconfig";
  2752. if (!$ret) {
  2753. fail "Have no good config to compare with, please set CONFIG_BISECT_GOOD";
  2754. return 1;
  2755. }
  2756. $good_config = $output_config;
  2757. }
  2758. if (!defined($config_bisect_exec)) {
  2759. # First check the location that ktest.pl ran
  2760. my @locations = (
  2761. "$pwd/config-bisect.pl",
  2762. "$dirname/config-bisect.pl",
  2763. "$builddir/tools/testing/ktest/config-bisect.pl",
  2764. undef );
  2765. foreach my $loc (@locations) {
  2766. doprint "loc = $loc\n";
  2767. $config_bisect_exec = $loc;
  2768. last if (defined($config_bisect_exec && -x $config_bisect_exec));
  2769. }
  2770. if (!defined($config_bisect_exec)) {
  2771. fail "Could not find an executable config-bisect.pl\n",
  2772. " Set CONFIG_BISECT_EXEC to point to config-bisect.pl";
  2773. return 1;
  2774. }
  2775. }
  2776. # we don't want min configs to cause issues here.
  2777. doprint "Disabling 'MIN_CONFIG' for this test\n";
  2778. undef $minconfig;
  2779. my %good_configs;
  2780. my %bad_configs;
  2781. my %tmp_configs;
  2782. if (-f "$tmpdir/good_config.tmp" || -f "$tmpdir/bad_config.tmp") {
  2783. if (read_yn "Interrupted config-bisect. Continue (n - will start new)?") {
  2784. if (-f "$tmpdir/good_config.tmp") {
  2785. $good_config = "$tmpdir/good_config.tmp";
  2786. } else {
  2787. $good_config = "$tmpdir/good_config";
  2788. }
  2789. if (-f "$tmpdir/bad_config.tmp") {
  2790. $bad_config = "$tmpdir/bad_config.tmp";
  2791. } else {
  2792. $bad_config = "$tmpdir/bad_config";
  2793. }
  2794. }
  2795. }
  2796. doprint "Run good configs through make oldconfig\n";
  2797. assign_configs \%tmp_configs, $good_config;
  2798. create_config "$good_config", \%tmp_configs;
  2799. $good_config = "$tmpdir/good_config";
  2800. system("cp $output_config $good_config") == 0 or dodie "cp good config";
  2801. doprint "Run bad configs through make oldconfig\n";
  2802. assign_configs \%tmp_configs, $bad_config;
  2803. create_config "$bad_config", \%tmp_configs;
  2804. $bad_config = "$tmpdir/bad_config";
  2805. system("cp $output_config $bad_config") == 0 or dodie "cp bad config";
  2806. if (defined($config_bisect_check) && $config_bisect_check ne "0") {
  2807. if ($config_bisect_check ne "good") {
  2808. doprint "Testing bad config\n";
  2809. $ret = run_bisect_test $type, "useconfig:$bad_config";
  2810. if ($ret) {
  2811. fail "Bad config succeeded when expected to fail!";
  2812. return 0;
  2813. }
  2814. }
  2815. if ($config_bisect_check ne "bad") {
  2816. doprint "Testing good config\n";
  2817. $ret = run_bisect_test $type, "useconfig:$good_config";
  2818. if (!$ret) {
  2819. fail "Good config failed when expected to succeed!";
  2820. return 0;
  2821. }
  2822. }
  2823. }
  2824. my $last_run = "";
  2825. do {
  2826. $ret = run_config_bisect $good_config, $bad_config, $last_run;
  2827. if ($ret == 3) {
  2828. $last_run = "good";
  2829. } elsif ($ret == 4) {
  2830. $last_run = "bad";
  2831. }
  2832. print_times;
  2833. } while ($ret == 3 || $ret == 4);
  2834. if ($ret == 2) {
  2835. config_bisect_end "$good_config.tmp", "$bad_config.tmp";
  2836. }
  2837. return $ret if ($ret < 0);
  2838. success $i;
  2839. }
  2840. sub patchcheck_reboot {
  2841. doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
  2842. reboot_to_good $patchcheck_sleep_time;
  2843. }
  2844. sub patchcheck {
  2845. my ($i) = @_;
  2846. dodie "PATCHCHECK_START[$i] not defined\n"
  2847. if (!defined($patchcheck_start));
  2848. dodie "PATCHCHECK_TYPE[$i] not defined\n"
  2849. if (!defined($patchcheck_type));
  2850. my $start = $patchcheck_start;
  2851. my $cherry = $patchcheck_cherry;
  2852. if (!defined($cherry)) {
  2853. $cherry = 0;
  2854. }
  2855. my $end = "HEAD";
  2856. if (defined($patchcheck_end)) {
  2857. $end = $patchcheck_end;
  2858. } elsif ($cherry) {
  2859. dodie "PATCHCHECK_END must be defined with PATCHCHECK_CHERRY\n";
  2860. }
  2861. # Get the true sha1's since we can use things like HEAD~3
  2862. $start = get_sha1($start);
  2863. $end = get_sha1($end);
  2864. my $type = $patchcheck_type;
  2865. # Can't have a test without having a test to run
  2866. if ($type eq "test" && !defined($run_test)) {
  2867. $type = "boot";
  2868. }
  2869. if ($cherry) {
  2870. open (IN, "git cherry -v $start $end|") or
  2871. dodie "could not get git list";
  2872. } else {
  2873. open (IN, "git log --pretty=oneline $end|") or
  2874. dodie "could not get git list";
  2875. }
  2876. my @list;
  2877. while (<IN>) {
  2878. chomp;
  2879. # git cherry adds a '+' we want to remove
  2880. s/^\+ //;
  2881. $list[$#list+1] = $_;
  2882. last if (/^$start/);
  2883. }
  2884. close(IN);
  2885. if (!$cherry) {
  2886. if ($list[$#list] !~ /^$start/) {
  2887. fail "SHA1 $start not found";
  2888. }
  2889. # go backwards in the list
  2890. @list = reverse @list;
  2891. }
  2892. my %skip_list;
  2893. my $will_skip = 0;
  2894. if (defined($patchcheck_skip)) {
  2895. foreach my $s (split /\s+/, $patchcheck_skip) {
  2896. $s = `git log --pretty=oneline $s~1..$s`;
  2897. $s =~ s/^(\S+).*/$1/;
  2898. chomp $s;
  2899. $skip_list{$s} = 1;
  2900. $will_skip++;
  2901. }
  2902. }
  2903. doprint("Going to test the following commits:\n");
  2904. foreach my $l (@list) {
  2905. my $sha1 = $l;
  2906. $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
  2907. next if (defined($skip_list{$sha1}));
  2908. doprint "$l\n";
  2909. }
  2910. if ($will_skip) {
  2911. doprint("\nSkipping the following commits:\n");
  2912. foreach my $l (@list) {
  2913. my $sha1 = $l;
  2914. $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
  2915. next if (!defined($skip_list{$sha1}));
  2916. doprint "$l\n";
  2917. }
  2918. }
  2919. my $save_clean = $noclean;
  2920. my %ignored_warnings;
  2921. if (defined($ignore_warnings)) {
  2922. foreach my $sha1 (split /\s+/, $ignore_warnings) {
  2923. $ignored_warnings{$sha1} = 1;
  2924. }
  2925. }
  2926. $in_patchcheck = 1;
  2927. foreach my $item (@list) {
  2928. my $sha1 = $item;
  2929. $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
  2930. if (defined($skip_list{$sha1})) {
  2931. doprint "\nSkipping \"$item\"\n\n";
  2932. next;
  2933. }
  2934. doprint "\nProcessing commit \"$item\"\n\n";
  2935. run_command "git checkout $sha1" or
  2936. dodie "Failed to checkout $sha1";
  2937. # only clean on the first and last patch
  2938. if ($item eq $list[0] ||
  2939. $item eq $list[$#list]) {
  2940. $noclean = $save_clean;
  2941. } else {
  2942. $noclean = 1;
  2943. }
  2944. if (defined($minconfig)) {
  2945. build "useconfig:$minconfig" or return 0;
  2946. } else {
  2947. # ?? no config to use?
  2948. build "oldconfig" or return 0;
  2949. }
  2950. # No need to do per patch checking if warnings file exists
  2951. if (!defined($warnings_file) && !defined($ignored_warnings{$sha1})) {
  2952. check_patch_buildlog $sha1 or return 0;
  2953. }
  2954. check_buildlog or return 0;
  2955. next if ($type eq "build");
  2956. my $failed = 0;
  2957. start_monitor_and_install or $failed = 1;
  2958. if (!$failed && $type ne "boot"){
  2959. do_run_test or $failed = 1;
  2960. }
  2961. end_monitor;
  2962. if ($failed) {
  2963. print_times;
  2964. return 0;
  2965. }
  2966. patchcheck_reboot;
  2967. print_times;
  2968. }
  2969. $in_patchcheck = 0;
  2970. success $i;
  2971. return 1;
  2972. }
  2973. sub add_dep {
  2974. # $config depends on $dep
  2975. my ($config, $dep) = @_;
  2976. if (defined($depends{$config})) {
  2977. $depends{$config} .= " " . $dep;
  2978. } else {
  2979. $depends{$config} = $dep;
  2980. }
  2981. # record the number of configs depending on $dep
  2982. if (defined $depcount{$dep}) {
  2983. $depcount{$dep}++;
  2984. } else {
  2985. $depcount{$dep} = 1;
  2986. }
  2987. }
  2988. # taken from streamline_config.pl
  2989. sub read_kconfig {
  2990. my ($kconfig) = @_;
  2991. my $state = "NONE";
  2992. my $config;
  2993. my @kconfigs;
  2994. my $cont = 0;
  2995. my $line;
  2996. if (! -f $kconfig) {
  2997. doprint "file $kconfig does not exist, skipping\n";
  2998. return;
  2999. }
  3000. open(KIN, "$kconfig")
  3001. or dodie "Can't open $kconfig";
  3002. while (<KIN>) {
  3003. chomp;
  3004. # Make sure that lines ending with \ continue
  3005. if ($cont) {
  3006. $_ = $line . " " . $_;
  3007. }
  3008. if (s/\\$//) {
  3009. $cont = 1;
  3010. $line = $_;
  3011. next;
  3012. }
  3013. $cont = 0;
  3014. # collect any Kconfig sources
  3015. if (/^source\s*"(.*)"/) {
  3016. $kconfigs[$#kconfigs+1] = $1;
  3017. }
  3018. # configs found
  3019. if (/^\s*(menu)?config\s+(\S+)\s*$/) {
  3020. $state = "NEW";
  3021. $config = $2;
  3022. for (my $i = 0; $i < $iflevel; $i++) {
  3023. add_dep $config, $ifdeps[$i];
  3024. }
  3025. # collect the depends for the config
  3026. } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
  3027. add_dep $config, $1;
  3028. # Get the configs that select this config
  3029. } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
  3030. # selected by depends on config
  3031. add_dep $1, $config;
  3032. # Check for if statements
  3033. } elsif (/^if\s+(.*\S)\s*$/) {
  3034. my $deps = $1;
  3035. # remove beginning and ending non text
  3036. $deps =~ s/^[^a-zA-Z0-9_]*//;
  3037. $deps =~ s/[^a-zA-Z0-9_]*$//;
  3038. my @deps = split /[^a-zA-Z0-9_]+/, $deps;
  3039. $ifdeps[$iflevel++] = join ':', @deps;
  3040. } elsif (/^endif/) {
  3041. $iflevel-- if ($iflevel);
  3042. # stop on "help"
  3043. } elsif (/^\s*help\s*$/) {
  3044. $state = "NONE";
  3045. }
  3046. }
  3047. close(KIN);
  3048. # read in any configs that were found.
  3049. foreach $kconfig (@kconfigs) {
  3050. if (!defined($read_kconfigs{$kconfig})) {
  3051. $read_kconfigs{$kconfig} = 1;
  3052. read_kconfig("$builddir/$kconfig");
  3053. }
  3054. }
  3055. }
  3056. sub read_depends {
  3057. # find out which arch this is by the kconfig file
  3058. open (IN, $output_config) or
  3059. dodie "Failed to read $output_config";
  3060. my $arch;
  3061. while (<IN>) {
  3062. if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
  3063. $arch = $1;
  3064. last;
  3065. }
  3066. }
  3067. close IN;
  3068. if (!defined($arch)) {
  3069. doprint "Could not find arch from config file\n";
  3070. doprint "no dependencies used\n";
  3071. return;
  3072. }
  3073. # arch is really the subarch, we need to know
  3074. # what directory to look at.
  3075. if ($arch eq "i386" || $arch eq "x86_64") {
  3076. $arch = "x86";
  3077. }
  3078. my $kconfig = "$builddir/arch/$arch/Kconfig";
  3079. if (! -f $kconfig && $arch =~ /\d$/) {
  3080. my $orig = $arch;
  3081. # some subarchs have numbers, truncate them
  3082. $arch =~ s/\d*$//;
  3083. $kconfig = "$builddir/arch/$arch/Kconfig";
  3084. if (! -f $kconfig) {
  3085. doprint "No idea what arch dir $orig is for\n";
  3086. doprint "no dependencies used\n";
  3087. return;
  3088. }
  3089. }
  3090. read_kconfig($kconfig);
  3091. }
  3092. sub make_new_config {
  3093. my @configs = @_;
  3094. open (OUT, ">$output_config")
  3095. or dodie "Failed to write $output_config";
  3096. foreach my $config (@configs) {
  3097. print OUT "$config\n";
  3098. }
  3099. close OUT;
  3100. }
  3101. sub chomp_config {
  3102. my ($config) = @_;
  3103. $config =~ s/CONFIG_//;
  3104. return $config;
  3105. }
  3106. sub get_depends {
  3107. my ($dep) = @_;
  3108. my $kconfig = chomp_config $dep;
  3109. $dep = $depends{"$kconfig"};
  3110. # the dep string we have saves the dependencies as they
  3111. # were found, including expressions like ! && ||. We
  3112. # want to split this out into just an array of configs.
  3113. my $valid = "A-Za-z_0-9";
  3114. my @configs;
  3115. while ($dep =~ /[$valid]/) {
  3116. if ($dep =~ /^[^$valid]*([$valid]+)/) {
  3117. my $conf = "CONFIG_" . $1;
  3118. $configs[$#configs + 1] = $conf;
  3119. $dep =~ s/^[^$valid]*[$valid]+//;
  3120. } else {
  3121. dodie "this should never happen";
  3122. }
  3123. }
  3124. return @configs;
  3125. }
  3126. sub test_this_config {
  3127. my ($config) = @_;
  3128. my $found;
  3129. # if we already processed this config, skip it
  3130. if (defined($processed_configs{$config})) {
  3131. return undef;
  3132. }
  3133. $processed_configs{$config} = 1;
  3134. # if this config failed during this round, skip it
  3135. if (defined($nochange_config{$config})) {
  3136. return undef;
  3137. }
  3138. my $kconfig = chomp_config $config;
  3139. # Test dependencies first
  3140. if (defined($depends{"$kconfig"})) {
  3141. my @parents = get_depends $config;
  3142. foreach my $parent (@parents) {
  3143. # if the parent is in the min config, check it first
  3144. next if (!defined($min_configs{$parent}));
  3145. $found = test_this_config($parent);
  3146. if (defined($found)) {
  3147. return $found;
  3148. }
  3149. }
  3150. }
  3151. # Remove this config from the list of configs
  3152. # do a make olddefconfig and then read the resulting
  3153. # .config to make sure it is missing the config that
  3154. # we had before
  3155. my %configs = %min_configs;
  3156. $configs{$config} = "# $config is not set";
  3157. make_new_config ((values %configs), (values %keep_configs));
  3158. make_oldconfig;
  3159. delete $configs{$config};
  3160. undef %configs;
  3161. assign_configs \%configs, $output_config;
  3162. if (!defined($configs{$config}) || $configs{$config} =~ /^#/) {
  3163. return $config;
  3164. }
  3165. doprint "disabling config $config did not change .config\n";
  3166. $nochange_config{$config} = 1;
  3167. return undef;
  3168. }
  3169. sub make_min_config {
  3170. my ($i) = @_;
  3171. my $type = $minconfig_type;
  3172. if ($type ne "boot" && $type ne "test") {
  3173. fail "Invalid MIN_CONFIG_TYPE '$minconfig_type'\n" .
  3174. " make_min_config works only with 'boot' and 'test'\n" and return;
  3175. }
  3176. if (!defined($output_minconfig)) {
  3177. fail "OUTPUT_MIN_CONFIG not defined" and return;
  3178. }
  3179. # If output_minconfig exists, and the start_minconfig
  3180. # came from min_config, than ask if we should use
  3181. # that instead.
  3182. if (-f $output_minconfig && !$start_minconfig_defined) {
  3183. print "$output_minconfig exists\n";
  3184. if (!defined($use_output_minconfig)) {
  3185. if (read_yn " Use it as minconfig?") {
  3186. $start_minconfig = $output_minconfig;
  3187. }
  3188. } elsif ($use_output_minconfig > 0) {
  3189. doprint "Using $output_minconfig as MIN_CONFIG\n";
  3190. $start_minconfig = $output_minconfig;
  3191. } else {
  3192. doprint "Set to still use MIN_CONFIG as starting point\n";
  3193. }
  3194. }
  3195. if (!defined($start_minconfig)) {
  3196. fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
  3197. }
  3198. my $temp_config = "$tmpdir/temp_config";
  3199. # First things first. We build an allnoconfig to find
  3200. # out what the defaults are that we can't touch.
  3201. # Some are selections, but we really can't handle selections.
  3202. my $save_minconfig = $minconfig;
  3203. undef $minconfig;
  3204. run_command "$make allnoconfig" or return 0;
  3205. read_depends;
  3206. process_config_ignore $output_config;
  3207. undef %save_configs;
  3208. undef %min_configs;
  3209. if (defined($ignore_config)) {
  3210. # make sure the file exists
  3211. `touch $ignore_config`;
  3212. assign_configs \%save_configs, $ignore_config;
  3213. }
  3214. %keep_configs = %save_configs;
  3215. doprint "Load initial configs from $start_minconfig\n";
  3216. # Look at the current min configs, and save off all the
  3217. # ones that were set via the allnoconfig
  3218. assign_configs \%min_configs, $start_minconfig;
  3219. my @config_keys = keys %min_configs;
  3220. # All configs need a depcount
  3221. foreach my $config (@config_keys) {
  3222. my $kconfig = chomp_config $config;
  3223. if (!defined $depcount{$kconfig}) {
  3224. $depcount{$kconfig} = 0;
  3225. }
  3226. }
  3227. # Remove anything that was set by the make allnoconfig
  3228. # we shouldn't need them as they get set for us anyway.
  3229. foreach my $config (@config_keys) {
  3230. # Remove anything in the ignore_config
  3231. if (defined($keep_configs{$config})) {
  3232. my $file = $ignore_config;
  3233. $file =~ s,.*/(.*?)$,$1,;
  3234. doprint "$config set by $file ... ignored\n";
  3235. delete $min_configs{$config};
  3236. next;
  3237. }
  3238. # But make sure the settings are the same. If a min config
  3239. # sets a selection, we do not want to get rid of it if
  3240. # it is not the same as what we have. Just move it into
  3241. # the keep configs.
  3242. if (defined($config_ignore{$config})) {
  3243. if ($config_ignore{$config} ne $min_configs{$config}) {
  3244. doprint "$config is in allnoconfig as '$config_ignore{$config}'";
  3245. doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
  3246. $keep_configs{$config} = $min_configs{$config};
  3247. } else {
  3248. doprint "$config set by allnoconfig ... ignored\n";
  3249. }
  3250. delete $min_configs{$config};
  3251. }
  3252. }
  3253. my $done = 0;
  3254. my $take_two = 0;
  3255. while (!$done) {
  3256. my $config;
  3257. my $found;
  3258. # Now disable each config one by one and do a make oldconfig
  3259. # till we find a config that changes our list.
  3260. my @test_configs = keys %min_configs;
  3261. # Sort keys by who is most dependent on
  3262. @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
  3263. @test_configs ;
  3264. # Put configs that did not modify the config at the end.
  3265. my $reset = 1;
  3266. for (my $i = 0; $i < $#test_configs; $i++) {
  3267. if (!defined($nochange_config{$test_configs[0]})) {
  3268. $reset = 0;
  3269. last;
  3270. }
  3271. # This config didn't change the .config last time.
  3272. # Place it at the end
  3273. my $config = shift @test_configs;
  3274. push @test_configs, $config;
  3275. }
  3276. # if every test config has failed to modify the .config file
  3277. # in the past, then reset and start over.
  3278. if ($reset) {
  3279. undef %nochange_config;
  3280. }
  3281. undef %processed_configs;
  3282. foreach my $config (@test_configs) {
  3283. $found = test_this_config $config;
  3284. last if (defined($found));
  3285. # oh well, try another config
  3286. }
  3287. if (!defined($found)) {
  3288. # we could have failed due to the nochange_config hash
  3289. # reset and try again
  3290. if (!$take_two) {
  3291. undef %nochange_config;
  3292. $take_two = 1;
  3293. next;
  3294. }
  3295. doprint "No more configs found that we can disable\n";
  3296. $done = 1;
  3297. last;
  3298. }
  3299. $take_two = 0;
  3300. $config = $found;
  3301. doprint "Test with $config disabled\n";
  3302. # set in_bisect to keep build and monitor from dieing
  3303. $in_bisect = 1;
  3304. my $failed = 0;
  3305. build "oldconfig" or $failed = 1;
  3306. if (!$failed) {
  3307. start_monitor_and_install or $failed = 1;
  3308. if ($type eq "test" && !$failed) {
  3309. do_run_test or $failed = 1;
  3310. }
  3311. end_monitor;
  3312. }
  3313. $in_bisect = 0;
  3314. if ($failed) {
  3315. doprint "$min_configs{$config} is needed to boot the box... keeping\n";
  3316. # this config is needed, add it to the ignore list.
  3317. $keep_configs{$config} = $min_configs{$config};
  3318. $save_configs{$config} = $min_configs{$config};
  3319. delete $min_configs{$config};
  3320. # update new ignore configs
  3321. if (defined($ignore_config)) {
  3322. open (OUT, ">$temp_config") or
  3323. dodie "Can't write to $temp_config";
  3324. foreach my $config (keys %save_configs) {
  3325. print OUT "$save_configs{$config}\n";
  3326. }
  3327. close OUT;
  3328. run_command "mv $temp_config $ignore_config" or
  3329. dodie "failed to copy update to $ignore_config";
  3330. }
  3331. } else {
  3332. # We booted without this config, remove it from the minconfigs.
  3333. doprint "$config is not needed, disabling\n";
  3334. delete $min_configs{$config};
  3335. # Also disable anything that is not enabled in this config
  3336. my %configs;
  3337. assign_configs \%configs, $output_config;
  3338. my @config_keys = keys %min_configs;
  3339. foreach my $config (@config_keys) {
  3340. if (!defined($configs{$config})) {
  3341. doprint "$config is not set, disabling\n";
  3342. delete $min_configs{$config};
  3343. }
  3344. }
  3345. # Save off all the current mandatory configs
  3346. open (OUT, ">$temp_config") or
  3347. dodie "Can't write to $temp_config";
  3348. foreach my $config (keys %keep_configs) {
  3349. print OUT "$keep_configs{$config}\n";
  3350. }
  3351. foreach my $config (keys %min_configs) {
  3352. print OUT "$min_configs{$config}\n";
  3353. }
  3354. close OUT;
  3355. run_command "mv $temp_config $output_minconfig" or
  3356. dodie "failed to copy update to $output_minconfig";
  3357. }
  3358. doprint "Reboot and wait $sleep_time seconds\n";
  3359. reboot_to_good $sleep_time;
  3360. }
  3361. success $i;
  3362. return 1;
  3363. }
  3364. sub make_warnings_file {
  3365. my ($i) = @_;
  3366. if (!defined($warnings_file)) {
  3367. dodie "Must define WARNINGS_FILE for make_warnings_file test";
  3368. }
  3369. if ($build_type eq "nobuild") {
  3370. dodie "BUILD_TYPE can not be 'nobuild' for make_warnings_file test";
  3371. }
  3372. build $build_type or dodie "Failed to build";
  3373. open(OUT, ">$warnings_file") or dodie "Can't create $warnings_file";
  3374. open(IN, $buildlog) or dodie "Can't open $buildlog";
  3375. while (<IN>) {
  3376. # Some compilers use UTF-8 extended for quotes
  3377. # for distcc heterogeneous systems, this causes issues
  3378. s/$utf8_quote/'/g;
  3379. if (/$check_build_re/) {
  3380. print OUT;
  3381. }
  3382. }
  3383. close(IN);
  3384. close(OUT);
  3385. success $i;
  3386. }
  3387. sub option_defined {
  3388. my ($option) = @_;
  3389. if (defined($opt{$option}) && $opt{$option} !~ /^\s*$/) {
  3390. return 1;
  3391. }
  3392. return 0;
  3393. }
  3394. sub __set_test_option {
  3395. my ($name, $i) = @_;
  3396. my $option = "$name\[$i\]";
  3397. if (option_defined($option)) {
  3398. return $opt{$option};
  3399. }
  3400. foreach my $test (keys %repeat_tests) {
  3401. if ($i >= $test &&
  3402. $i < $test + $repeat_tests{$test}) {
  3403. $option = "$name\[$test\]";
  3404. if (option_defined($option)) {
  3405. return $opt{$option};
  3406. }
  3407. }
  3408. }
  3409. if (option_defined($name)) {
  3410. return $opt{$name};
  3411. }
  3412. return undef;
  3413. }
  3414. sub set_test_option {
  3415. my ($name, $i) = @_;
  3416. my $option = __set_test_option($name, $i);
  3417. return $option if (!defined($option));
  3418. return eval_option($name, $option, $i);
  3419. }
  3420. sub find_mailer {
  3421. my ($mailer) = @_;
  3422. my @paths = split /:/, $ENV{PATH};
  3423. # sendmail is usually in /usr/sbin
  3424. $paths[$#paths + 1] = "/usr/sbin";
  3425. foreach my $path (@paths) {
  3426. if (-x "$path/$mailer") {
  3427. return $path;
  3428. }
  3429. }
  3430. return undef;
  3431. }
  3432. sub do_send_mail {
  3433. my ($subject, $message, $file) = @_;
  3434. if (!defined($mail_path)) {
  3435. # find the mailer
  3436. $mail_path = find_mailer $mailer;
  3437. if (!defined($mail_path)) {
  3438. die "\nCan not find $mailer in PATH\n";
  3439. }
  3440. }
  3441. my $header_file = "$tmpdir/header";
  3442. open (HEAD, ">$header_file") or die "Can not create $header_file\n";
  3443. print HEAD "To: $mailto\n";
  3444. print HEAD "Subject: $subject\n\n";
  3445. print HEAD "$message\n";
  3446. close HEAD;
  3447. if (!defined($mail_command)) {
  3448. if ($mailer eq "mail" || $mailer eq "mailx") {
  3449. $mail_command = "cat \$HEADER_FILE \$BODY_FILE | \$MAIL_PATH/\$MAILER -s \'\$SUBJECT\' \$MAILTO";
  3450. } elsif ($mailer eq "sendmail" ) {
  3451. $mail_command = "cat \$HEADER_FILE \$BODY_FILE | \$MAIL_PATH/\$MAILER -t \$MAILTO";
  3452. } else {
  3453. die "\nYour mailer: $mailer is not supported.\n";
  3454. }
  3455. }
  3456. if (defined($file)) {
  3457. $mail_command =~ s/\$BODY_FILE/$file/g;
  3458. } else {
  3459. $mail_command =~ s/\$BODY_FILE//g;
  3460. }
  3461. $mail_command =~ s/\$HEADER_FILE/$header_file/g;
  3462. $mail_command =~ s/\$MAILER/$mailer/g;
  3463. $mail_command =~ s/\$MAIL_PATH/$mail_path/g;
  3464. $mail_command =~ s/\$MAILTO/$mailto/g;
  3465. $mail_command =~ s/\$SUBJECT/$subject/g;
  3466. $mail_command =~ s/\$MESSAGE/$message/g;
  3467. my $ret = run_command $mail_command;
  3468. if (!$ret && defined($file)) {
  3469. # try again without the file
  3470. $message .= "\n\n*** FAILED TO SEND LOG ***\n\n";
  3471. do_send_email($subject, $message);
  3472. }
  3473. }
  3474. sub send_email {
  3475. if (defined($mailto)) {
  3476. if (!defined($mailer)) {
  3477. doprint "No email sent: email or mailer not specified in config.\n";
  3478. return;
  3479. }
  3480. do_send_mail @_;
  3481. }
  3482. }
  3483. sub cancel_test {
  3484. if ($monitor_cnt) {
  3485. end_monitor;
  3486. }
  3487. if ($email_when_canceled) {
  3488. my $name = get_test_name;
  3489. send_email("KTEST: Your [$name] test was cancelled",
  3490. "Your test started at $script_start_time was cancelled: sig int");
  3491. }
  3492. die "\nCaught Sig Int, test interrupted: $!\n"
  3493. }
  3494. sub die_usage {
  3495. die << "EOF"
  3496. ktest.pl version: $VERSION
  3497. usage: ktest.pl [options] [config-file]
  3498. [options]:
  3499. -D value: Where value can act as an option override.
  3500. -D BUILD_NOCLEAN=1
  3501. Sets global BUILD_NOCLEAN to 1
  3502. -D TEST_TYPE[2]=build
  3503. Sets TEST_TYPE of test 2 to "build"
  3504. It can also override all temp variables.
  3505. -D USE_TEMP_DIR:=1
  3506. Will override all variables that use
  3507. "USE_TEMP_DIR="
  3508. EOF
  3509. ;
  3510. }
  3511. while ( $#ARGV >= 0 ) {
  3512. if ( $ARGV[0] eq "-D" ) {
  3513. shift;
  3514. die_usage if ($#ARGV < 1);
  3515. my $val = shift;
  3516. if ($val =~ m/(.*?):=(.*)$/) {
  3517. set_variable($1, $2, 1);
  3518. } else {
  3519. $command_vars[$#command_vars + 1] = $val;
  3520. }
  3521. } elsif ( $ARGV[0] =~ m/^-D(.*)/) {
  3522. my $val = $1;
  3523. shift;
  3524. if ($val =~ m/(.*?):=(.*)$/) {
  3525. set_variable($1, $2, 1);
  3526. } else {
  3527. $command_vars[$#command_vars + 1] = $val;
  3528. }
  3529. } elsif ( $ARGV[0] eq "-h" ) {
  3530. die_usage;
  3531. } else {
  3532. last;
  3533. }
  3534. }
  3535. $#ARGV < 1 or die_usage;
  3536. if ($#ARGV == 0) {
  3537. $ktest_config = $ARGV[0];
  3538. if (! -f $ktest_config) {
  3539. print "$ktest_config does not exist.\n";
  3540. if (!read_yn "Create it?") {
  3541. exit 0;
  3542. }
  3543. }
  3544. }
  3545. if (! -f $ktest_config) {
  3546. $newconfig = 1;
  3547. get_test_case;
  3548. open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
  3549. print OUT << "EOF"
  3550. # Generated by ktest.pl
  3551. #
  3552. # PWD is a ktest.pl variable that will result in the process working
  3553. # directory that ktest.pl is executed in.
  3554. # THIS_DIR is automatically assigned the PWD of the path that generated
  3555. # the config file. It is best to use this variable when assigning other
  3556. # directory paths within this directory. This allows you to easily
  3557. # move the test cases to other locations or to other machines.
  3558. #
  3559. THIS_DIR := $variable{"PWD"}
  3560. # Define each test with TEST_START
  3561. # The config options below it will override the defaults
  3562. TEST_START
  3563. TEST_TYPE = $default{"TEST_TYPE"}
  3564. DEFAULTS
  3565. EOF
  3566. ;
  3567. close(OUT);
  3568. }
  3569. read_config $ktest_config;
  3570. if (defined($opt{"LOG_FILE"})) {
  3571. $opt{"LOG_FILE"} = eval_option("LOG_FILE", $opt{"LOG_FILE"}, -1);
  3572. }
  3573. # Append any configs entered in manually to the config file.
  3574. my @new_configs = keys %entered_configs;
  3575. if ($#new_configs >= 0) {
  3576. print "\nAppending entered in configs to $ktest_config\n";
  3577. open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
  3578. foreach my $config (@new_configs) {
  3579. print OUT "$config = $entered_configs{$config}\n";
  3580. $opt{$config} = process_variables($entered_configs{$config});
  3581. }
  3582. }
  3583. if (defined($opt{"LOG_FILE"})) {
  3584. if ($opt{"CLEAR_LOG"}) {
  3585. unlink $opt{"LOG_FILE"};
  3586. }
  3587. if (! -e $opt{"LOG_FILE"} && $opt{"LOG_FILE"} =~ m,^(.*/),) {
  3588. my $dir = $1;
  3589. if (! -d $dir) {
  3590. mkpath($dir) or die "Failed to create directories '$dir': $!";
  3591. print "\nThe log directory $dir did not exist, so it was created.\n";
  3592. }
  3593. }
  3594. open(LOG, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
  3595. LOG->autoflush(1);
  3596. }
  3597. doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
  3598. for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
  3599. if (!$i) {
  3600. doprint "DEFAULT OPTIONS:\n";
  3601. } else {
  3602. doprint "\nTEST $i OPTIONS";
  3603. if (defined($repeat_tests{$i})) {
  3604. $repeat = $repeat_tests{$i};
  3605. doprint " ITERATE $repeat";
  3606. }
  3607. doprint "\n";
  3608. }
  3609. foreach my $option (sort keys %opt) {
  3610. if ($option =~ /\[(\d+)\]$/) {
  3611. next if ($i != $1);
  3612. } else {
  3613. next if ($i);
  3614. }
  3615. doprint "$option = $opt{$option}\n";
  3616. }
  3617. }
  3618. $SIG{INT} = qw(cancel_test);
  3619. # First we need to do is the builds
  3620. for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
  3621. # Do not reboot on failing test options
  3622. $no_reboot = 1;
  3623. $reboot_success = 0;
  3624. $have_version = 0;
  3625. $iteration = $i;
  3626. $build_time = 0;
  3627. $install_time = 0;
  3628. $reboot_time = 0;
  3629. $test_time = 0;
  3630. undef %force_config;
  3631. my $makecmd = set_test_option("MAKE_CMD", $i);
  3632. $outputdir = set_test_option("OUTPUT_DIR", $i);
  3633. $builddir = set_test_option("BUILD_DIR", $i);
  3634. chdir $builddir || dodie "can't change directory to $builddir";
  3635. if (!-d $outputdir) {
  3636. mkpath($outputdir) or
  3637. dodie "can't create $outputdir";
  3638. }
  3639. $make = "$makecmd O=$outputdir";
  3640. # Load all the options into their mapped variable names
  3641. foreach my $opt (keys %option_map) {
  3642. ${$option_map{$opt}} = set_test_option($opt, $i);
  3643. }
  3644. $start_minconfig_defined = 1;
  3645. # The first test may override the PRE_KTEST option
  3646. if ($i == 1) {
  3647. if (defined($pre_ktest)) {
  3648. doprint "\n";
  3649. run_command $pre_ktest;
  3650. }
  3651. if ($email_when_started) {
  3652. my $name = get_test_name;
  3653. send_email("KTEST: Your [$name] test was started",
  3654. "Your test was started on $script_start_time");
  3655. }
  3656. }
  3657. # Any test can override the POST_KTEST option
  3658. # The last test takes precedence.
  3659. if (defined($post_ktest)) {
  3660. $final_post_ktest = $post_ktest;
  3661. }
  3662. if (!defined($start_minconfig)) {
  3663. $start_minconfig_defined = 0;
  3664. $start_minconfig = $minconfig;
  3665. }
  3666. if (!-d $tmpdir) {
  3667. mkpath($tmpdir) or
  3668. dodie "can't create $tmpdir";
  3669. }
  3670. $ENV{"SSH_USER"} = $ssh_user;
  3671. $ENV{"MACHINE"} = $machine;
  3672. $buildlog = "$tmpdir/buildlog-$machine";
  3673. $testlog = "$tmpdir/testlog-$machine";
  3674. $dmesg = "$tmpdir/dmesg-$machine";
  3675. $output_config = "$outputdir/.config";
  3676. if (!$buildonly) {
  3677. $target = "$ssh_user\@$machine";
  3678. if (($reboot_type eq "grub") or ($reboot_type eq "grub2bls")) {
  3679. dodie "GRUB_MENU not defined" if (!defined($grub_menu));
  3680. } elsif ($reboot_type eq "grub2") {
  3681. dodie "GRUB_MENU not defined" if (!defined($grub_menu));
  3682. dodie "GRUB_FILE not defined" if (!defined($grub_file));
  3683. } elsif ($reboot_type eq "syslinux") {
  3684. dodie "SYSLINUX_LABEL not defined" if (!defined($syslinux_label));
  3685. }
  3686. }
  3687. my $run_type = $build_type;
  3688. if ($test_type eq "patchcheck") {
  3689. $run_type = $patchcheck_type;
  3690. } elsif ($test_type eq "bisect") {
  3691. $run_type = $bisect_type;
  3692. } elsif ($test_type eq "config_bisect") {
  3693. $run_type = $config_bisect_type;
  3694. } elsif ($test_type eq "make_min_config") {
  3695. $run_type = "";
  3696. } elsif ($test_type eq "make_warnings_file") {
  3697. $run_type = "";
  3698. }
  3699. # mistake in config file?
  3700. if (!defined($run_type)) {
  3701. $run_type = "ERROR";
  3702. }
  3703. my $installme = "";
  3704. $installme = " no_install" if ($no_install);
  3705. my $name = "";
  3706. if (defined($test_name)) {
  3707. $name = " ($test_name)";
  3708. }
  3709. doprint "\n\n";
  3710. if (defined($opt{"LOG_FILE"})) {
  3711. $test_log_start = tell(LOG);
  3712. }
  3713. doprint "RUNNING TEST $i of $opt{NUM_TESTS}$name with option $test_type $run_type$installme\n\n";
  3714. # Always show which build directory and output directory is being used
  3715. doprint "BUILD_DIR=$builddir\n";
  3716. doprint "OUTPUT_DIR=$outputdir\n\n";
  3717. if (defined($pre_test)) {
  3718. my $ret = run_command $pre_test;
  3719. if (!$ret && defined($pre_test_die) &&
  3720. $pre_test_die) {
  3721. dodie "failed to pre_test\n";
  3722. }
  3723. }
  3724. unlink $dmesg;
  3725. unlink $buildlog;
  3726. unlink $testlog;
  3727. if (defined($addconfig)) {
  3728. my $min = $minconfig;
  3729. if (!defined($minconfig)) {
  3730. $min = "";
  3731. }
  3732. run_command "cat $addconfig $min > $tmpdir/add_config" or
  3733. dodie "Failed to create temp config";
  3734. $minconfig = "$tmpdir/add_config";
  3735. }
  3736. if (defined($checkout)) {
  3737. run_command "git checkout $checkout" or
  3738. dodie "failed to checkout $checkout";
  3739. }
  3740. $no_reboot = 0;
  3741. # A test may opt to not reboot the box
  3742. if ($reboot_on_success) {
  3743. $reboot_success = 1;
  3744. }
  3745. if ($test_type eq "bisect") {
  3746. bisect $i;
  3747. next;
  3748. } elsif ($test_type eq "config_bisect") {
  3749. config_bisect $i;
  3750. next;
  3751. } elsif ($test_type eq "patchcheck") {
  3752. patchcheck $i;
  3753. next;
  3754. } elsif ($test_type eq "make_min_config") {
  3755. make_min_config $i;
  3756. next;
  3757. } elsif ($test_type eq "make_warnings_file") {
  3758. $no_reboot = 1;
  3759. make_warnings_file $i;
  3760. next;
  3761. }
  3762. if ($build_type ne "nobuild") {
  3763. build $build_type or next;
  3764. check_buildlog or next;
  3765. }
  3766. if ($test_type eq "install") {
  3767. get_version;
  3768. install;
  3769. success $i;
  3770. next;
  3771. }
  3772. if ($test_type ne "build") {
  3773. my $failed = 0;
  3774. start_monitor_and_install or $failed = 1;
  3775. if (!$failed && $test_type ne "boot" && defined($run_test)) {
  3776. do_run_test or $failed = 1;
  3777. }
  3778. end_monitor;
  3779. if ($failed) {
  3780. print_times;
  3781. next;
  3782. }
  3783. }
  3784. print_times;
  3785. success $i;
  3786. }
  3787. if (defined($final_post_ktest)) {
  3788. my $cp_final_post_ktest = eval_kernel_version $final_post_ktest;
  3789. run_command $cp_final_post_ktest;
  3790. }
  3791. if ($opt{"POWEROFF_ON_SUCCESS"}) {
  3792. halt;
  3793. } elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot && $reboot_success) {
  3794. reboot_to_good;
  3795. } elsif (defined($switch_to_good)) {
  3796. # still need to get to the good kernel
  3797. run_command $switch_to_good;
  3798. }
  3799. doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
  3800. if ($email_when_finished) {
  3801. send_email("KTEST: Your test has finished!",
  3802. "$successes of $opt{NUM_TESTS} tests started at $script_start_time were successful!");
  3803. }
  3804. if (defined($opt{"LOG_FILE"})) {
  3805. print "\n See $opt{LOG_FILE} for the record of results.\n\n";
  3806. close LOG;
  3807. }
  3808. exit 0;
  3809. ##
  3810. # The following are here to standardize tabs/spaces/etc across the most likely editors
  3811. ###
  3812. # Local Variables:
  3813. # mode: perl
  3814. # End:
  3815. # vim: softtabstop=4