proctests.cc 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531
  1. #include <cassert>
  2. #include <iostream>
  3. #include <list>
  4. #include <utility>
  5. #include <string>
  6. #include <sstream>
  7. #include "service.h"
  8. #include "proc-service.h"
  9. #include "dinit-util.h"
  10. #include "test_procservice.h"
  11. // Tests of process-service related functionality.
  12. //
  13. // These tests work mostly by completely mocking out the base_process_service class. The mock
  14. // implementations can be found in test-baseproc.cc.
  15. #ifdef NDEBUG
  16. #error "This file must be built with assertions ENABLED!"
  17. #endif
  18. extern eventloop_t event_loop;
  19. constexpr static auto REG = dependency_type::REGULAR;
  20. constexpr static auto WAITS = dependency_type::WAITS_FOR;
  21. // Regular service start
  22. void test_proc_service_start()
  23. {
  24. using namespace std;
  25. service_set sset;
  26. ha_string command = "test-command";
  27. list<pair<unsigned,unsigned>> command_offsets;
  28. command_offsets.emplace_back(0, command.length());
  29. std::list<prelim_dep> depends;
  30. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  31. init_service_defaults(p);
  32. sset.add_service(&p);
  33. p.start();
  34. sset.process_queues();
  35. assert(p.get_state() == service_state_t::STARTING);
  36. base_process_service_test::exec_succeeded(&p);
  37. sset.process_queues();
  38. assert(p.get_state() == service_state_t::STARTED);
  39. assert(event_loop.active_timers.size() == 0);
  40. sset.remove_service(&p);
  41. }
  42. // Test start with readiness notification
  43. void test_proc_notify_start()
  44. {
  45. using namespace std;
  46. service_set sset;
  47. ha_string command = "test-command";
  48. list<pair<unsigned,unsigned>> command_offsets;
  49. command_offsets.emplace_back(0, command.length());
  50. std::list<prelim_dep> depends;
  51. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  52. init_service_defaults(p);
  53. p.set_notification_fd(3);
  54. sset.add_service(&p);
  55. p.start();
  56. sset.process_queues();
  57. assert(p.get_state() == service_state_t::STARTING);
  58. base_process_service_test::exec_succeeded(&p);
  59. sset.process_queues();
  60. assert(p.get_state() == service_state_t::STARTING);
  61. int nfd = base_process_service_test::get_notification_fd(&p);
  62. assert(nfd > 0);
  63. char notifystr[] = "ok started\n";
  64. std::vector<char> rnotifystr;
  65. rnotifystr.insert(rnotifystr.end(), notifystr, notifystr + sizeof(notifystr));
  66. bp_sys::supply_read_data(nfd, std::move(rnotifystr));
  67. event_loop.regd_fd_watchers[nfd]->fd_event(event_loop, nfd, dasynq::IN_EVENTS);
  68. assert(p.get_state() == service_state_t::STARTED);
  69. assert(event_loop.active_timers.size() == 0);
  70. sset.remove_service(&p);
  71. }
  72. // Unexpected termination
  73. void test_proc_unexpected_term()
  74. {
  75. using namespace std;
  76. service_set sset;
  77. ha_string command = "test-command";
  78. list<pair<unsigned,unsigned>> command_offsets;
  79. command_offsets.emplace_back(0, command.length());
  80. std::list<prelim_dep> depends;
  81. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  82. init_service_defaults(p);
  83. sset.add_service(&p);
  84. service_record d1 {&sset, "test-service-2", service_type_t::INTERNAL, {{&p, REG}}};
  85. sset.add_service(&d1);
  86. d1.start();
  87. sset.process_queues();
  88. base_process_service_test::exec_succeeded(&p);
  89. sset.process_queues();
  90. assert(p.get_state() == service_state_t::STARTED);
  91. assert(p.get_target_state() == service_state_t::STARTED);
  92. assert(d1.get_state() == service_state_t::STARTED);
  93. assert(d1.get_target_state() == service_state_t::STARTED);
  94. base_process_service_test::handle_exit(&p, 0);
  95. assert(p.get_target_state() == service_state_t::STOPPED);
  96. assert(p.get_state() == service_state_t::STOPPED);
  97. assert(p.get_stop_reason() == stopped_reason_t::TERMINATED);
  98. assert(event_loop.active_timers.size() == 0);
  99. assert(d1.get_state() == service_state_t::STOPPED);
  100. assert(d1.get_target_state() == service_state_t::STOPPED);
  101. sset.remove_service(&d1);
  102. sset.remove_service(&p);
  103. }
  104. // Unexpected termination until restarts exhausted, followed by a normal start.
  105. void test_proc_term_start()
  106. {
  107. using namespace std;
  108. service_set sset;
  109. ha_string command = "test-command";
  110. list<pair<unsigned,unsigned>> command_offsets;
  111. command_offsets.emplace_back(0, command.length());
  112. std::list<prelim_dep> depends;
  113. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  114. init_service_defaults(p);
  115. // One restart per 1000 interval
  116. p.set_restart_interval({1000, 0}, 1);
  117. p.set_auto_restart(true);
  118. sset.add_service(&p);
  119. // Start the service
  120. p.start();
  121. sset.process_queues();
  122. base_process_service_test::exec_succeeded(&p);
  123. sset.process_queues();
  124. assert(p.get_state() == service_state_t::STARTED);
  125. assert(p.get_target_state() == service_state_t::STARTED);
  126. // Unexpected termination - should restart
  127. base_process_service_test::handle_exit(&p, 0);
  128. sset.process_queues();
  129. assert(p.get_target_state() == service_state_t::STARTED);
  130. assert(p.get_state() == service_state_t::STARTING);
  131. base_process_service_test::exec_succeeded(&p);
  132. sset.process_queues();
  133. assert(p.get_state() == service_state_t::STARTED);
  134. assert(p.get_target_state() == service_state_t::STARTED);
  135. // 2nd unexpected termination - should stop
  136. base_process_service_test::handle_exit(&p, 0);
  137. sset.process_queues();
  138. assert(p.get_target_state() == service_state_t::STOPPED);
  139. assert(p.get_state() == service_state_t::STOPPED);
  140. // explicit restart:
  141. p.start();
  142. sset.process_queues();
  143. base_process_service_test::exec_succeeded(&p);
  144. sset.process_queues();
  145. assert(p.get_state() == service_state_t::STARTED);
  146. assert(p.get_target_state() == service_state_t::STARTED);
  147. // Now, again, one automatic restart should go through if the process terminates
  148. // restart:
  149. base_process_service_test::handle_exit(&p, 0);
  150. sset.process_queues();
  151. assert(p.get_target_state() == service_state_t::STARTED);
  152. assert(p.get_state() == service_state_t::STARTING);
  153. base_process_service_test::exec_succeeded(&p);
  154. sset.process_queues();
  155. assert(p.get_state() == service_state_t::STARTED);
  156. assert(p.get_target_state() == service_state_t::STARTED);
  157. // and stop:
  158. base_process_service_test::handle_exit(&p, 0);
  159. sset.process_queues();
  160. assert(p.get_target_state() == service_state_t::STOPPED);
  161. assert(p.get_state() == service_state_t::STOPPED);
  162. sset.remove_service(&p);
  163. }
  164. // Unexpected termination with restart
  165. void test_proc_term_restart()
  166. {
  167. using namespace std;
  168. service_set sset;
  169. ha_string command = "test-command";
  170. list<pair<unsigned,unsigned>> command_offsets;
  171. command_offsets.emplace_back(0, command.length());
  172. std::list<prelim_dep> depends;
  173. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  174. init_service_defaults(p);
  175. p.set_auto_restart(true);
  176. sset.add_service(&p);
  177. p.start();
  178. sset.process_queues();
  179. pid_t first_pid = bp_sys::last_forked_pid;
  180. base_process_service_test::exec_succeeded(&p);
  181. sset.process_queues();
  182. assert(p.get_state() == service_state_t::STARTED);
  183. assert(event_loop.active_timers.size() == 0);
  184. base_process_service_test::handle_exit(&p, 0);
  185. sset.process_queues();
  186. // Starting, restart timer should be armed:
  187. assert(p.get_state() == service_state_t::STARTING);
  188. assert(event_loop.active_timers.size() == 1);
  189. assert(bp_sys::last_forked_pid == first_pid);
  190. event_loop.advance_time(default_restart_interval);
  191. // Startup timer now active:
  192. assert(event_loop.active_timers.size() == 1);
  193. assert(bp_sys::last_forked_pid == (first_pid + 1));
  194. base_process_service_test::exec_succeeded(&p);
  195. sset.process_queues();
  196. assert(p.get_state() == service_state_t::STARTED);
  197. assert(event_loop.active_timers.size() == 0);
  198. sset.remove_service(&p);
  199. }
  200. // Unexpected termination with restart, with dependent
  201. void test_proc_term_restart2()
  202. {
  203. using namespace std;
  204. service_set sset;
  205. ha_string command = "test-command";
  206. list<pair<unsigned,unsigned>> command_offsets;
  207. command_offsets.emplace_back(0, command.length());
  208. std::list<prelim_dep> depends;
  209. service_record b {&sset, "boot"};
  210. sset.add_service(&b);
  211. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  212. init_service_defaults(p);
  213. p.set_auto_restart(true);
  214. sset.add_service(&p);
  215. b.add_dep(&p, WAITS);
  216. pid_t first_pid = bp_sys::last_forked_pid;
  217. b.start();
  218. sset.process_queues();
  219. assert(p.get_state() == service_state_t::STARTING);
  220. assert(bp_sys::last_forked_pid == first_pid + 1);
  221. base_process_service_test::exec_succeeded(&p);
  222. sset.process_queues();
  223. assert(p.get_state() == service_state_t::STARTED);
  224. assert(event_loop.active_timers.size() == 0);
  225. assert(bp_sys::last_forked_pid == first_pid + 1);
  226. // simulate process terminating, should then be restarted:
  227. base_process_service_test::handle_exit(&p, 0);
  228. sset.process_queues();
  229. // Starting, restart timer should be armed:
  230. assert(p.get_state() == service_state_t::STARTING);
  231. assert(event_loop.active_timers.size() == 1);
  232. assert(bp_sys::last_forked_pid == first_pid + 1);
  233. event_loop.advance_time(time_val(0, 200000000));
  234. // startup timer will be active:
  235. assert(event_loop.active_timers.size() == 1);
  236. sset.process_queues();
  237. assert(bp_sys::last_forked_pid == first_pid + 2);
  238. base_process_service_test::exec_succeeded(&p);
  239. sset.process_queues();
  240. assert(p.get_state() == service_state_t::STARTED);
  241. assert(event_loop.active_timers.size() == 0);
  242. assert(sset.count_active_services() == 2);
  243. // Request stop, this time it should not restart:
  244. p.stop(true);
  245. sset.process_queues();
  246. base_process_service_test::handle_exit(&p, 0);
  247. sset.process_queues();
  248. assert(p.get_state() == service_state_t::STOPPED);
  249. assert(event_loop.active_timers.size() == 0);
  250. assert(sset.count_active_services() == 1);
  251. assert(bp_sys::last_forked_pid == first_pid + 2);
  252. // simulate terminate dinit
  253. sset.stop_all_services();
  254. sset.process_queues();
  255. assert(p.get_state() == service_state_t::STOPPED);
  256. assert(event_loop.active_timers.size() == 0);
  257. assert(sset.count_active_services() == 0);
  258. sset.remove_service(&p);
  259. sset.remove_service(&b);
  260. }
  261. // Restart due to dependent, after unexpected termination
  262. void test_proc_term_restart3()
  263. {
  264. using namespace std;
  265. service_set sset;
  266. ha_string command = "test-command";
  267. list<pair<unsigned,unsigned>> command_offsets;
  268. command_offsets.emplace_back(0, command.length());
  269. std::list<prelim_dep> depends;
  270. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  271. init_service_defaults(p);
  272. sset.add_service(&p);
  273. service_record d1 {&sset, "test-service-2", service_type_t::INTERNAL, {{&p, REG}}};
  274. d1.set_auto_restart(true);
  275. sset.add_service(&d1);
  276. d1.start();
  277. sset.process_queues();
  278. base_process_service_test::exec_succeeded(&p);
  279. sset.process_queues();
  280. assert(p.get_state() == service_state_t::STARTED);
  281. assert(p.get_target_state() == service_state_t::STARTED);
  282. assert(d1.get_state() == service_state_t::STARTED);
  283. assert(d1.get_target_state() == service_state_t::STARTED);
  284. base_process_service_test::handle_exit(&p, 0);
  285. sset.process_queues();
  286. assert(p.get_target_state() == service_state_t::STARTED);
  287. assert(p.get_state() == service_state_t::STARTING);
  288. assert(p.get_stop_reason() == stopped_reason_t::TERMINATED);
  289. assert(d1.get_state() == service_state_t::STARTING);
  290. assert(d1.get_target_state() == service_state_t::STARTED);
  291. sset.remove_service(&d1);
  292. sset.remove_service(&p);
  293. event_loop.active_timers.clear();
  294. }
  295. // Restart after unexpected termination, start times out
  296. void test_proc_term_restart4()
  297. {
  298. using namespace std;
  299. service_set sset;
  300. ha_string command = "test-command";
  301. list<pair<unsigned,unsigned>> command_offsets;
  302. command_offsets.emplace_back(0, command.length());
  303. std::list<prelim_dep> depends;
  304. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  305. init_service_defaults(p);
  306. p.set_auto_restart(true);
  307. time_val start_timeout {5, 0};
  308. p.set_start_timeout(start_timeout);
  309. sset.add_service(&p);
  310. p.start();
  311. sset.process_queues();
  312. pid_t first_pid = bp_sys::last_forked_pid;
  313. base_process_service_test::exec_succeeded(&p);
  314. sset.process_queues();
  315. assert(p.get_state() == service_state_t::STARTED);
  316. assert(event_loop.active_timers.size() == 0);
  317. base_process_service_test::handle_exit(&p, 0);
  318. sset.process_queues();
  319. // Starting, restart timer should be armed:
  320. assert(p.get_state() == service_state_t::STARTING);
  321. assert(event_loop.active_timers.size() == 1);
  322. assert(bp_sys::last_forked_pid == first_pid);
  323. event_loop.advance_time(default_restart_interval);
  324. // restart timer should have stopped, start timeout timer should have started
  325. assert(event_loop.active_timers.size() == 1);
  326. assert(bp_sys::last_forked_pid == (first_pid + 1));
  327. event_loop.advance_time(start_timeout);
  328. assert(p.get_state() == service_state_t::STOPPING);
  329. assert(event_loop.active_timers.size() == 1); // stop timeout should be set
  330. base_process_service_test::exec_succeeded(&p); // the exec must finally succeed...
  331. base_process_service_test::handle_exit(&p, 0);
  332. assert(p.get_state() == service_state_t::STOPPED);
  333. assert(event_loop.active_timers.size() == 0);
  334. sset.remove_service(&p);
  335. }
  336. // Failure to restart should propagate to dependent
  337. void test_proc_term_restart_fail()
  338. {
  339. using namespace std;
  340. service_set sset;
  341. ha_string command = "test-command";
  342. list<pair<unsigned,unsigned>> command_offsets;
  343. command_offsets.emplace_back(0, command.length());
  344. std::list<prelim_dep> depends;
  345. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  346. init_service_defaults(p);
  347. p.set_auto_restart(true);
  348. sset.add_service(&p);
  349. service_record p_dpt1 {&sset, "dpt1", service_type_t::INTERNAL, {{ &p, REG }}};
  350. p_dpt1.set_auto_restart(true);
  351. sset.add_service(&p_dpt1);
  352. service_record p_dpt2 {&sset, "dpt2", service_type_t::INTERNAL, {{ &p_dpt1, WAITS }}};
  353. sset.add_service(&p_dpt2);
  354. p_dpt2.start();
  355. sset.process_queues();
  356. for (int i = 0; i < 3; ++i) {
  357. assert(p.get_state() == service_state_t::STARTING);
  358. base_process_service_test::exec_succeeded(&p);
  359. sset.process_queues();
  360. assert(p.get_state() == service_state_t::STARTED);
  361. assert(event_loop.active_timers.size() == 0);
  362. base_process_service_test::handle_exit(&p, 0);
  363. sset.process_queues();
  364. // Starting, restart timer should be armed:
  365. assert(p.get_state() == service_state_t::STARTING);
  366. assert(event_loop.active_timers.size() == 1);
  367. event_loop.advance_time(default_restart_interval);
  368. }
  369. assert(p.get_state() == service_state_t::STARTING);
  370. base_process_service_test::exec_succeeded(&p);
  371. sset.process_queues();
  372. assert(p.get_state() == service_state_t::STARTED);
  373. assert(event_loop.active_timers.size() == 0);
  374. // There should be no attempt to restart this time:
  375. base_process_service_test::handle_exit(&p, 0);
  376. sset.process_queues();
  377. assert(event_loop.active_timers.size() == 0);
  378. assert(p.get_state() == service_state_t::STOPPED);
  379. assert(p_dpt1.get_state() == service_state_t::STOPPED);
  380. sset.remove_service(&p_dpt2);
  381. sset.remove_service(&p_dpt1);
  382. sset.remove_service(&p);
  383. }
  384. // Termination via stop request
  385. void test_term_via_stop()
  386. {
  387. using namespace std;
  388. service_set sset;
  389. ha_string command = "test-command";
  390. list<pair<unsigned,unsigned>> command_offsets;
  391. command_offsets.emplace_back(0, command.length());
  392. std::list<prelim_dep> depends;
  393. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  394. init_service_defaults(p);
  395. sset.add_service(&p);
  396. p.start();
  397. sset.process_queues();
  398. base_process_service_test::exec_succeeded(&p);
  399. sset.process_queues();
  400. assert(p.get_state() == service_state_t::STARTED);
  401. assert(event_loop.active_timers.size() == 0);
  402. p.stop(true);
  403. sset.process_queues();
  404. assert(p.get_state() == service_state_t::STOPPING);
  405. assert(event_loop.active_timers.size() == 1);
  406. base_process_service_test::handle_exit(&p, 0);
  407. sset.process_queues();
  408. assert(p.get_state() == service_state_t::STOPPED);
  409. assert(p.get_stop_reason() == stopped_reason_t::NORMAL);
  410. assert(event_loop.active_timers.size() == 0);
  411. sset.remove_service(&p);
  412. }
  413. // Termination via stop request, ensure reason is reset:
  414. void test_term_via_stop2()
  415. {
  416. using namespace std;
  417. service_set sset;
  418. ha_string command = "test-command";
  419. list<pair<unsigned,unsigned>> command_offsets;
  420. command_offsets.emplace_back(0, command.length());
  421. std::list<prelim_dep> depends;
  422. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  423. init_service_defaults(p);
  424. sset.add_service(&p);
  425. p.start();
  426. sset.process_queues();
  427. // first set it up with failure reason:
  428. base_process_service_test::exec_failed(&p, ENOENT);
  429. sset.process_queues();
  430. assert(p.get_state() == service_state_t::STOPPED);
  431. assert(p.get_stop_reason() == stopped_reason_t::EXECFAILED);
  432. // now restart clean:
  433. p.start();
  434. sset.process_queues();
  435. base_process_service_test::exec_succeeded(&p);
  436. sset.process_queues();
  437. assert(p.get_state() == service_state_t::STARTED);
  438. assert(event_loop.active_timers.size() == 0);
  439. p.stop(true);
  440. sset.process_queues();
  441. assert(p.get_state() == service_state_t::STOPPING);
  442. base_process_service_test::handle_exit(&p, 0);
  443. sset.process_queues();
  444. assert(p.get_state() == service_state_t::STOPPED);
  445. assert(p.get_stop_reason() == stopped_reason_t::NORMAL);
  446. assert(event_loop.active_timers.size() == 0);
  447. sset.remove_service(&p);
  448. }
  449. // stop twice
  450. void test_term_via_stop3()
  451. {
  452. using namespace std;
  453. service_set sset;
  454. ha_string command = "test-command";
  455. list<pair<unsigned,unsigned>> command_offsets;
  456. command_offsets.emplace_back(0, command.length());
  457. std::list<prelim_dep> depends;
  458. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  459. init_service_defaults(p);
  460. sset.add_service(&p);
  461. p.start();
  462. sset.process_queues();
  463. base_process_service_test::exec_succeeded(&p);
  464. sset.process_queues();
  465. assert(p.get_state() == service_state_t::STARTED);
  466. assert(event_loop.active_timers.size() == 0);
  467. p.stop(true);
  468. sset.process_queues();
  469. assert(p.get_state() == service_state_t::STOPPING);
  470. assert(event_loop.active_timers.size() == 1);
  471. base_process_service_test::handle_exit(&p, 0);
  472. sset.process_queues();
  473. assert(p.get_state() == service_state_t::STOPPED);
  474. assert(p.get_stop_reason() == stopped_reason_t::NORMAL);
  475. assert(event_loop.active_timers.size() == 0);
  476. p.start();
  477. sset.process_queues();
  478. base_process_service_test::exec_succeeded(&p);
  479. sset.process_queues();
  480. assert(p.get_state() == service_state_t::STARTED);
  481. assert(event_loop.active_timers.size() == 0);
  482. bp_sys::last_sig_sent = 0; // make sure signal is re-sent
  483. p.stop(true);
  484. sset.process_queues();
  485. assert(p.get_state() == service_state_t::STOPPING);
  486. assert(event_loop.active_timers.size() == 1);
  487. assert(bp_sys::last_sig_sent == SIGTERM);
  488. base_process_service_test::handle_exit(&p, 0);
  489. sset.process_queues();
  490. assert(p.get_state() == service_state_t::STOPPED);
  491. assert(p.get_stop_reason() == stopped_reason_t::NORMAL);
  492. assert(event_loop.active_timers.size() == 0);
  493. sset.remove_service(&p);
  494. }
  495. // Time-out during start
  496. void test_proc_start_timeout()
  497. {
  498. using namespace std;
  499. service_set sset;
  500. ha_string command = "test-command";
  501. list<pair<unsigned,unsigned>> command_offsets;
  502. command_offsets.emplace_back(0, command.length());
  503. std::list<prelim_dep> depends;
  504. scripted_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  505. init_service_defaults(p);
  506. p.set_start_timeout(time_val(10,0));
  507. sset.add_service(&p);
  508. p.start();
  509. sset.process_queues();
  510. assert(p.get_state() == service_state_t::STARTING);
  511. event_loop.advance_time(time_val(10,0));
  512. sset.process_queues();
  513. assert(p.get_state() == service_state_t::STOPPING);
  514. base_process_service_test::handle_signal_exit(&p, SIGTERM);
  515. sset.process_queues();
  516. // We set no stop script, so state should now be STOPPED with no timer set
  517. assert(p.get_state() == service_state_t::STOPPED);
  518. assert(p.get_stop_reason() == stopped_reason_t::TIMEDOUT);
  519. assert(event_loop.active_timers.size() == 0);
  520. sset.remove_service(&p);
  521. }
  522. // Test that a timeout doesn't stop a "waits for" dependent to fail to start
  523. void test_proc_start_timeout2()
  524. {
  525. using namespace std;
  526. service_set sset;
  527. ha_string command = "test-command";
  528. list<pair<unsigned,unsigned>> command_offsets;
  529. command_offsets.emplace_back(0, command.length());
  530. std::list<prelim_dep> depends;
  531. scripted_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  532. p.set_start_timeout(time_val {1,0});
  533. init_service_defaults(p);
  534. sset.add_service(&p);
  535. service_record ts {&sset, "test-service-1", service_type_t::INTERNAL,
  536. {{&p, dependency_type::WAITS_FOR}} };
  537. ts.start();
  538. sset.process_queues();
  539. assert(p.get_state() == service_state_t::STARTING);
  540. assert(ts.get_state() == service_state_t::STARTING);
  541. event_loop.advance_time(time_val {1,0}); // start timer should expire
  542. sset.process_queues();
  543. assert(p.get_state() == service_state_t::STOPPING);
  544. base_process_service_test::handle_exit(&p, 0);
  545. sset.process_queues();
  546. assert(p.get_state() == service_state_t::STOPPED);
  547. assert(p.get_stop_reason() == stopped_reason_t::TIMEDOUT);
  548. assert(ts.get_state() == service_state_t::STARTED);
  549. assert(event_loop.active_timers.size() == 0);
  550. sset.remove_service(&p);
  551. }
  552. // Test exec() failure for process service start.
  553. void test_proc_start_execfail()
  554. {
  555. using namespace std;
  556. service_set sset;
  557. ha_string command = "test-command";
  558. list<pair<unsigned,unsigned>> command_offsets;
  559. command_offsets.emplace_back(0, command.length());
  560. std::list<prelim_dep> depends;
  561. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  562. init_service_defaults(p);
  563. sset.add_service(&p);
  564. p.start();
  565. sset.process_queues();
  566. assert(p.get_state() == service_state_t::STARTING);
  567. base_process_service_test::exec_failed(&p, ENOENT);
  568. sset.process_queues();
  569. assert(p.get_state() == service_state_t::STOPPED);
  570. assert(p.get_stop_reason() == stopped_reason_t::EXECFAILED);
  571. assert(event_loop.active_timers.size() == 0);
  572. sset.remove_service(&p);
  573. }
  574. // Test no ready notification before process terminates
  575. void test_proc_notify_fail()
  576. {
  577. using namespace std;
  578. service_set sset;
  579. ha_string command = "test-command";
  580. list<pair<unsigned,unsigned>> command_offsets;
  581. command_offsets.emplace_back(0, command.length());
  582. std::list<prelim_dep> depends;
  583. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  584. init_service_defaults(p);
  585. p.set_notification_fd(3);
  586. sset.add_service(&p);
  587. p.start();
  588. sset.process_queues();
  589. assert(p.get_state() == service_state_t::STARTING);
  590. base_process_service_test::exec_succeeded(&p);
  591. sset.process_queues();
  592. assert(p.get_state() == service_state_t::STARTING);
  593. int nfd = base_process_service_test::get_notification_fd(&p);
  594. assert(nfd > 0);
  595. // Signal EOF on notify fd:
  596. event_loop.regd_fd_watchers[nfd]->fd_event(event_loop, nfd, dasynq::IN_EVENTS);
  597. assert(p.get_state() == service_state_t::STOPPING);
  598. base_process_service_test::handle_exit(&p, 0);
  599. sset.process_queues();
  600. assert(p.get_state() == service_state_t::STOPPED);
  601. assert(event_loop.active_timers.size() == 0);
  602. sset.remove_service(&p);
  603. }
  604. // Test stop timeout
  605. void test_proc_stop_timeout()
  606. {
  607. using namespace std;
  608. service_set sset;
  609. ha_string command = "test-command";
  610. list<pair<unsigned,unsigned>> command_offsets;
  611. command_offsets.emplace_back(0, command.length());
  612. std::list<prelim_dep> depends;
  613. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  614. init_service_defaults(p);
  615. p.set_stop_timeout(time_val {10, 0});
  616. sset.add_service(&p);
  617. p.start();
  618. sset.process_queues();
  619. assert(p.get_state() == service_state_t::STARTING);
  620. base_process_service_test::exec_succeeded(&p);
  621. sset.process_queues();
  622. assert(p.get_state() == service_state_t::STARTED);
  623. p.stop(true);
  624. sset.process_queues();
  625. assert(p.get_state() == service_state_t::STOPPING);
  626. assert(bp_sys::last_sig_sent == SIGTERM);
  627. event_loop.advance_time(time_val {10, 0}); // expire stop timer
  628. sset.process_queues();
  629. // kill signal (SIGKILL) should have been sent; process not dead until it's dead, however
  630. assert(p.get_state() == service_state_t::STOPPING);
  631. assert(bp_sys::last_sig_sent == SIGKILL);
  632. base_process_service_test::handle_exit(&p, 0);
  633. sset.process_queues();
  634. assert(p.get_state() == service_state_t::STOPPED);
  635. assert(p.get_stop_reason() == stopped_reason_t::NORMAL);
  636. assert(event_loop.active_timers.size() == 0);
  637. sset.remove_service(&p);
  638. }
  639. // Smooth recovery
  640. void test_proc_smooth_recovery1()
  641. {
  642. using namespace std;
  643. service_set sset;
  644. ha_string command = "test-command";
  645. list<pair<unsigned,unsigned>> command_offsets;
  646. command_offsets.emplace_back(0, command.length());
  647. std::list<prelim_dep> depends;
  648. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  649. init_service_defaults(p);
  650. p.set_smooth_recovery(true);
  651. p.set_restart_delay(time_val {0, 1000});
  652. sset.add_service(&p);
  653. p.start();
  654. sset.process_queues();
  655. base_process_service_test::exec_succeeded(&p);
  656. sset.process_queues();
  657. pid_t first_instance = bp_sys::last_forked_pid;
  658. assert(p.get_state() == service_state_t::STARTED);
  659. base_process_service_test::handle_exit(&p, 0);
  660. sset.process_queues();
  661. // since time hasn't been changed, we expect that the process has not yet been re-launched:
  662. assert(first_instance == bp_sys::last_forked_pid);
  663. assert(p.get_state() == service_state_t::STARTED);
  664. event_loop.advance_time(time_val {0, 1000});
  665. sset.process_queues();
  666. // Now a new process should've been launched:
  667. assert(first_instance + 1 == bp_sys::last_forked_pid);
  668. assert(p.get_state() == service_state_t::STARTED);
  669. base_process_service_test::exec_succeeded(&p);
  670. sset.process_queues();
  671. assert(event_loop.active_timers.size() == 0);
  672. sset.remove_service(&p);
  673. }
  674. // Smooth recovery without restart delay
  675. void test_proc_smooth_recovery2()
  676. {
  677. using namespace std;
  678. service_set sset;
  679. ha_string command = "test-command";
  680. list<pair<unsigned,unsigned>> command_offsets;
  681. command_offsets.emplace_back(0, command.length());
  682. std::list<prelim_dep> depends;
  683. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  684. init_service_defaults(p);
  685. p.set_smooth_recovery(true);
  686. p.set_restart_delay(time_val(0, 0));
  687. sset.add_service(&p);
  688. p.start();
  689. sset.process_queues();
  690. base_process_service_test::exec_succeeded(&p);
  691. sset.process_queues();
  692. pid_t first_instance = bp_sys::last_forked_pid;
  693. assert(p.get_state() == service_state_t::STARTED);
  694. assert(event_loop.active_timers.size() == 0);
  695. base_process_service_test::handle_exit(&p, 0);
  696. sset.process_queues();
  697. // no restart delay, process should restart immediately:
  698. assert(first_instance + 1 == bp_sys::last_forked_pid);
  699. assert(p.get_state() == service_state_t::STARTED);
  700. base_process_service_test::exec_succeeded(&p);
  701. sset.process_queues();
  702. assert(event_loop.active_timers.size() == 0);
  703. sset.remove_service(&p);
  704. }
  705. // failure during smooth recovery is non-recoverable
  706. void test_proc_smooth_recovery3()
  707. {
  708. using namespace std;
  709. service_set sset;
  710. ha_string command = "test-command";
  711. list<pair<unsigned,unsigned>> command_offsets;
  712. command_offsets.emplace_back(0, command.length());
  713. std::list<prelim_dep> depends;
  714. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  715. init_service_defaults(p);
  716. p.set_smooth_recovery(true);
  717. p.set_restart_delay(time_val(0, 0));
  718. sset.add_service(&p);
  719. service_record d1 {&sset, "test-service-2", service_type_t::INTERNAL, {{&p, REG}}};
  720. d1.set_auto_restart(true);
  721. sset.add_service(&d1);
  722. d1.start();
  723. //p.start();
  724. sset.process_queues();
  725. base_process_service_test::exec_succeeded(&p);
  726. sset.process_queues();
  727. pid_t first_instance = bp_sys::last_forked_pid;
  728. assert(p.get_state() == service_state_t::STARTED);
  729. assert(event_loop.active_timers.size() == 0);
  730. base_process_service_test::handle_exit(&p, 0);
  731. sset.process_queues();
  732. // no restart delay, process should attempt restart immediately:
  733. assert(first_instance + 1 == bp_sys::last_forked_pid);
  734. assert(p.get_state() == service_state_t::STARTED);
  735. assert(event_loop.active_timers.size() == 1); // (restart timer)
  736. base_process_service_test::exec_failed(&p, ENOENT);
  737. sset.process_queues();
  738. assert(p.get_state() == service_state_t::STOPPED);
  739. assert(p.get_target_state() == service_state_t::STOPPED);
  740. assert(d1.get_state() == service_state_t::STOPPED);
  741. assert(d1.get_target_state() == service_state_t::STOPPED);
  742. assert(event_loop.active_timers.size() == 0);
  743. sset.remove_service(&d1);
  744. sset.remove_service(&p);
  745. }
  746. // stop issued during smooth recovery (waiting for restart timer)
  747. void test_proc_smooth_recovery4()
  748. {
  749. using namespace std;
  750. service_set sset;
  751. ha_string command = "test-command";
  752. list<pair<unsigned,unsigned>> command_offsets;
  753. command_offsets.emplace_back(0, command.length());
  754. std::list<prelim_dep> depends;
  755. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  756. init_service_defaults(p);
  757. p.set_smooth_recovery(true);
  758. p.set_restart_delay(time_val(2, 0)); // 2 second restart delay
  759. sset.add_service(&p);
  760. p.start();
  761. sset.process_queues();
  762. base_process_service_test::exec_succeeded(&p);
  763. sset.process_queues();
  764. pid_t first_instance = bp_sys::last_forked_pid;
  765. assert(p.get_state() == service_state_t::STARTED);
  766. assert(event_loop.active_timers.size() == 0);
  767. base_process_service_test::handle_exit(&p, 0);
  768. sset.process_queues();
  769. // smooth recovery should have begun
  770. event_loop.advance_time(time_val(1, 0));
  771. assert(p.get_state() == service_state_t::STARTED);
  772. // If we stop now, timer should be cancelled, no signal should be sent
  773. bp_sys::last_sig_sent = -1;
  774. p.stop(true);
  775. sset.process_queues();
  776. assert(p.get_state() == service_state_t::STOPPED);
  777. assert(first_instance == bp_sys::last_forked_pid); // no more processes launched
  778. assert(bp_sys::last_sig_sent == -1);
  779. assert(event_loop.active_timers.size() == 0);
  780. sset.remove_service(&p);
  781. }
  782. // stop during smooth recovery (waiting for process startup)
  783. void test_proc_smooth_recovery5()
  784. {
  785. using namespace std;
  786. service_set sset;
  787. ha_string command = "test-command";
  788. list<pair<unsigned,unsigned>> command_offsets;
  789. command_offsets.emplace_back(0, command.length());
  790. std::list<prelim_dep> depends;
  791. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  792. init_service_defaults(p);
  793. p.set_smooth_recovery(true);
  794. p.set_restart_delay(time_val {0, 1000});
  795. sset.add_service(&p);
  796. p.start();
  797. sset.process_queues();
  798. base_process_service_test::exec_succeeded(&p);
  799. sset.process_queues();
  800. pid_t first_instance = bp_sys::last_forked_pid;
  801. assert(p.get_state() == service_state_t::STARTED);
  802. base_process_service_test::handle_exit(&p, 0);
  803. sset.process_queues();
  804. // since time hasn't been changed, we expect that the process has not yet been re-launched:
  805. assert(first_instance == bp_sys::last_forked_pid);
  806. assert(p.get_state() == service_state_t::STARTED);
  807. event_loop.advance_time(time_val {0, 1000});
  808. sset.process_queues();
  809. // Now a new process should've been launched:
  810. assert(first_instance + 1 == bp_sys::last_forked_pid);
  811. assert(p.get_state() == service_state_t::STARTED);
  812. // However, at this stage the exec has not succeeded. If we issue a stop, we shouldn't see a signal sent yet,
  813. // since it's not clear what signal to send (term signal might not be SIGTERM, but if it's something else, the
  814. // process before exec() may not respond correctly)
  815. bp_sys::last_sig_sent = -1;
  816. p.stop(true);
  817. assert(bp_sys::last_sig_sent == -1);
  818. assert(p.get_state() == service_state_t::STOPPING);
  819. // Once the exec succeeds, then we should:
  820. base_process_service_test::exec_succeeded(&p);
  821. sset.process_queues();
  822. assert(bp_sys::last_sig_sent == SIGTERM);
  823. base_process_service_test::handle_exit(&p, 0);
  824. sset.process_queues();
  825. assert(p.get_state() == service_state_t::STOPPED);
  826. assert(event_loop.active_timers.size() == 0);
  827. sset.remove_service(&p);
  828. }
  829. // smooth recovery: timeout waiting for readiness notification
  830. void test_proc_smooth_recovery6()
  831. {
  832. using namespace std;
  833. service_set sset;
  834. ha_string command = "test-command";
  835. list<pair<unsigned,unsigned>> command_offsets;
  836. command_offsets.emplace_back(0, command.length());
  837. std::list<prelim_dep> depends;
  838. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  839. init_service_defaults(p);
  840. p.set_smooth_recovery(true);
  841. p.set_restart_delay(time_val {0, 1000});
  842. p.set_start_timeout(time_val {1, 0});
  843. p.set_notification_fd(3);
  844. sset.add_service(&p);
  845. p.start();
  846. sset.process_queues();
  847. base_process_service_test::exec_succeeded(&p);
  848. sset.process_queues();
  849. // readiness notification from process:
  850. int nfd = base_process_service_test::get_notification_fd(&p);
  851. char notifystr[] = "ok started\n";
  852. std::vector<char> rnotifystr;
  853. rnotifystr.insert(rnotifystr.end(), notifystr, notifystr + sizeof(notifystr));
  854. bp_sys::supply_read_data(nfd, std::move(rnotifystr));
  855. event_loop.regd_fd_watchers[nfd]->fd_event(event_loop, nfd, dasynq::IN_EVENTS);
  856. assert(p.get_state() == service_state_t::STARTED);
  857. assert(event_loop.active_timers.size() == 0);
  858. pid_t first_instance = bp_sys::last_forked_pid;
  859. assert(p.get_state() == service_state_t::STARTED);
  860. base_process_service_test::handle_exit(&p, 0);
  861. sset.process_queues();
  862. event_loop.advance_time(time_val {0, 1000});
  863. // new process should've been forked:
  864. assert(first_instance != bp_sys::last_forked_pid);
  865. assert(p.get_state() == service_state_t::STARTED);
  866. assert(p.get_pid() == bp_sys::last_forked_pid);
  867. base_process_service_test::exec_succeeded(&p);
  868. // Now, timeout while waiting for readiness:
  869. bp_sys::last_sig_sent = -1;
  870. event_loop.advance_time(time_val {1, 0});
  871. // We should see the process has been signalled:
  872. assert(bp_sys::last_sig_sent == SIGINT);
  873. assert(p.get_state() == service_state_t::STOPPING);
  874. base_process_service_test::handle_exit(&p, 1);
  875. sset.process_queues();
  876. // The state should now be stopped:
  877. assert(p.get_state() == service_state_t::STOPPED);
  878. assert(event_loop.active_timers.size() == 0);
  879. sset.remove_service(&p);
  880. }
  881. // smooth recovery: termination while waiting for readiness notification
  882. void test_proc_smooth_recovery6a()
  883. {
  884. using namespace std;
  885. service_set sset;
  886. ha_string command = "test-command";
  887. list<pair<unsigned,unsigned>> command_offsets;
  888. command_offsets.emplace_back(0, command.length());
  889. std::list<prelim_dep> depends;
  890. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  891. init_service_defaults(p);
  892. p.set_smooth_recovery(true);
  893. p.set_restart_delay(time_val {0, 1000});
  894. p.set_start_timeout(time_val {1, 0});
  895. p.set_notification_fd(3);
  896. sset.add_service(&p);
  897. p.start();
  898. sset.process_queues();
  899. base_process_service_test::exec_succeeded(&p);
  900. sset.process_queues();
  901. // readiness notification from process:
  902. int nfd = base_process_service_test::get_notification_fd(&p);
  903. char notifystr[] = "ok started\n";
  904. std::vector<char> rnotifystr;
  905. rnotifystr.insert(rnotifystr.end(), notifystr, notifystr + sizeof(notifystr));
  906. bp_sys::supply_read_data(nfd, std::move(rnotifystr));
  907. event_loop.regd_fd_watchers[nfd]->fd_event(event_loop, nfd, dasynq::IN_EVENTS);
  908. assert(p.get_state() == service_state_t::STARTED);
  909. assert(event_loop.active_timers.size() == 0);
  910. pid_t first_instance = bp_sys::last_forked_pid;
  911. assert(p.get_state() == service_state_t::STARTED);
  912. base_process_service_test::handle_exit(&p, 0);
  913. sset.process_queues();
  914. event_loop.advance_time(time_val {0, 1000});
  915. // new process should've been forked:
  916. assert(first_instance != bp_sys::last_forked_pid);
  917. assert(p.get_state() == service_state_t::STARTED);
  918. assert(p.get_pid() == bp_sys::last_forked_pid);
  919. base_process_service_test::exec_succeeded(&p);
  920. sset.process_queues();
  921. base_process_service_test::handle_exit(&p, 1);
  922. sset.process_queues();
  923. // The state should now be stopped:
  924. assert(p.get_state() == service_state_t::STOPPED);
  925. assert(event_loop.active_timers.size() == 0);
  926. sset.remove_service(&p);
  927. }
  928. // simulate the launcher process forking a daemon process, and supply the process ID of that
  929. // daemon process in a pid file.
  930. static void supply_pid_contents(const char *pid_file, pid_t *daemon_instance_p = nullptr)
  931. {
  932. using namespace std;
  933. pid_t daemon_instance = ++bp_sys::last_forked_pid;
  934. // Set up the pid file content with the pid of the daemon
  935. stringstream str;
  936. str << daemon_instance << std::flush;
  937. string pid_file_content = str.str();
  938. vector<char> pid_file_content_v(pid_file_content.begin(), pid_file_content.end());
  939. bp_sys::supply_file_content(pid_file, std::move(pid_file_content_v));
  940. if (daemon_instance_p != nullptr) {
  941. *daemon_instance_p = daemon_instance;
  942. }
  943. }
  944. void test_bgproc_start()
  945. {
  946. using namespace std;
  947. service_set sset;
  948. ha_string command = "test-command";
  949. list<pair<unsigned,unsigned>> command_offsets;
  950. command_offsets.emplace_back(0, command.length());
  951. std::list<prelim_dep> depends;
  952. bgproc_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  953. init_service_defaults(p);
  954. p.set_pid_file("/run/daemon.pid");
  955. sset.add_service(&p);
  956. p.start();
  957. sset.process_queues();
  958. assert(p.get_state() == service_state_t::STARTING);
  959. base_process_service_test::exec_succeeded(&p);
  960. sset.process_queues();
  961. assert(p.get_state() == service_state_t::STARTING);
  962. supply_pid_contents("/run/daemon.pid");
  963. base_process_service_test::handle_exit(&p, 0);
  964. assert(p.get_state() == service_state_t::STARTED);
  965. assert(event_loop.active_timers.size() == 0);
  966. sset.remove_service(&p);
  967. }
  968. void test_bgproc_start_fail()
  969. {
  970. using namespace std;
  971. service_set sset;
  972. ha_string command = "test-command";
  973. list<pair<unsigned,unsigned>> command_offsets;
  974. command_offsets.emplace_back(0, command.length());
  975. std::list<prelim_dep> depends;
  976. bgproc_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  977. init_service_defaults(p);
  978. p.set_pid_file("/run/daemon.pid");
  979. sset.add_service(&p);
  980. p.start();
  981. sset.process_queues();
  982. assert(p.get_state() == service_state_t::STARTING);
  983. base_process_service_test::exec_succeeded(&p);
  984. sset.process_queues();
  985. assert(p.get_state() == service_state_t::STARTING);
  986. base_process_service_test::handle_exit(&p, 0x1); // fail status
  987. assert(p.get_state() == service_state_t::STOPPED);
  988. assert(event_loop.active_timers.size() == 0);
  989. sset.remove_service(&p);
  990. }
  991. void test_bgproc_start_fail_pid()
  992. {
  993. using namespace std;
  994. service_set sset;
  995. ha_string command = "test-command";
  996. list<pair<unsigned,unsigned>> command_offsets;
  997. command_offsets.emplace_back(0, command.length());
  998. std::list<prelim_dep> depends;
  999. bgproc_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  1000. init_service_defaults(p);
  1001. p.set_pid_file("/run/no-exist-daemon.pid");
  1002. sset.add_service(&p);
  1003. p.start();
  1004. sset.process_queues();
  1005. assert(p.get_state() == service_state_t::STARTING);
  1006. base_process_service_test::exec_succeeded(&p);
  1007. sset.process_queues();
  1008. assert(p.get_state() == service_state_t::STARTING);
  1009. // launcher returns success, but no pid file exists:
  1010. base_process_service_test::handle_exit(&p, 0x0);
  1011. assert(p.get_state() == service_state_t::STOPPED);
  1012. assert(event_loop.active_timers.size() == 0);
  1013. sset.remove_service(&p);
  1014. }
  1015. void test_bgproc_unexpected_term()
  1016. {
  1017. using namespace std;
  1018. service_set sset;
  1019. ha_string command = "test-command";
  1020. list<pair<unsigned,unsigned>> command_offsets;
  1021. command_offsets.emplace_back(0, command.length());
  1022. std::list<prelim_dep> depends;
  1023. bgproc_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  1024. init_service_defaults(p);
  1025. p.set_pid_file("/run/daemon.pid");
  1026. sset.add_service(&p);
  1027. service_record d1 {&sset, "test-service-2", service_type_t::INTERNAL, {{&p, REG}}};
  1028. sset.add_service(&d1);
  1029. d1.start();
  1030. sset.process_queues();
  1031. base_process_service_test::exec_succeeded(&p);
  1032. assert(p.get_state() == service_state_t::STARTING);
  1033. assert(d1.get_state() == service_state_t::STARTING);
  1034. supply_pid_contents("/run/daemon.pid");
  1035. base_process_service_test::handle_exit(&p, 0);
  1036. assert(p.get_target_state() == service_state_t::STARTED);
  1037. assert(p.get_state() == service_state_t::STARTED);
  1038. assert(d1.get_target_state() == service_state_t::STARTED);
  1039. assert(d1.get_state() == service_state_t::STARTED);
  1040. // Now the backgrounded daemon itself terminates:
  1041. base_process_service_test::handle_exit(&p, 0);
  1042. assert(p.get_state() == service_state_t::STOPPED);
  1043. assert(p.get_target_state() == service_state_t::STOPPED);
  1044. assert(p.get_stop_reason() == stopped_reason_t::TERMINATED);
  1045. assert(event_loop.active_timers.size() == 0);
  1046. assert(d1.get_state() == service_state_t::STOPPED);
  1047. assert(d1.get_target_state() == service_state_t::STOPPED);
  1048. sset.remove_service(&d1);
  1049. sset.remove_service(&p);
  1050. }
  1051. void test_bgproc_smooth_recover()
  1052. {
  1053. using namespace std;
  1054. service_set sset;
  1055. ha_string command = "test-command";
  1056. list<pair<unsigned,unsigned>> command_offsets;
  1057. command_offsets.emplace_back(0, command.length());
  1058. std::list<prelim_dep> depends;
  1059. bgproc_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  1060. init_service_defaults(p);
  1061. p.set_smooth_recovery(true);
  1062. p.set_restart_delay(time_val {0, 1000});
  1063. p.set_pid_file("/run/daemon.pid");
  1064. sset.add_service(&p);
  1065. p.start();
  1066. sset.process_queues();
  1067. base_process_service_test::exec_succeeded(&p);
  1068. sset.process_queues();
  1069. pid_t daemon_instance;
  1070. supply_pid_contents("/run/daemon.pid", &daemon_instance);
  1071. assert(p.get_state() == service_state_t::STARTING);
  1072. base_process_service_test::handle_exit(&p, 0); // exit the launch process
  1073. sset.process_queues();
  1074. // daemon process has been started now, state should be STARTED
  1075. assert(p.get_state() == service_state_t::STARTED);
  1076. assert(daemon_instance == bp_sys::last_forked_pid);
  1077. base_process_service_test::handle_exit(&p, 0); // exit the daemon process
  1078. // since time hasn't been changed, we expect that the process has not yet been re-launched:
  1079. assert(p.get_state() == service_state_t::STARTED);
  1080. assert(daemon_instance == bp_sys::last_forked_pid);
  1081. event_loop.advance_time(time_val {0, 1000});
  1082. sset.process_queues();
  1083. // Now a new process should've been launched:
  1084. assert(daemon_instance + 1 == bp_sys::last_forked_pid);
  1085. assert(p.get_state() == service_state_t::STARTED);
  1086. assert(event_loop.active_timers.size() == 1); // start timer
  1087. base_process_service_test::exec_succeeded(&p);
  1088. sset.process_queues();
  1089. assert(event_loop.active_timers.size() == 1);
  1090. supply_pid_contents("/run/daemon.pid", &daemon_instance);
  1091. base_process_service_test::handle_exit(&p, 0);
  1092. sset.process_queues();
  1093. assert(p.get_state() == service_state_t::STARTED);
  1094. assert(event_loop.active_timers.size() == 0);
  1095. // Now run through it again
  1096. base_process_service_test::handle_exit(&p, 0); // exit the daemon process
  1097. // since time hasn't been changed, we expect that the process has not yet been re-launched:
  1098. assert(p.get_state() == service_state_t::STARTED);
  1099. assert(p.get_pid() == -1);
  1100. assert(daemon_instance == bp_sys::last_forked_pid);
  1101. event_loop.advance_time(time_val {0, 1000});
  1102. sset.process_queues();
  1103. // Now a new process should've been launched:
  1104. assert(daemon_instance + 1 == bp_sys::last_forked_pid);
  1105. assert(p.get_state() == service_state_t::STARTED);
  1106. assert(event_loop.active_timers.size() == 1);
  1107. base_process_service_test::exec_succeeded(&p);
  1108. sset.process_queues();
  1109. assert(event_loop.active_timers.size() == 1);
  1110. supply_pid_contents("/run/daemon.pid", &daemon_instance);
  1111. base_process_service_test::handle_exit(&p, 0);
  1112. sset.process_queues();
  1113. assert(p.get_state() == service_state_t::STARTED);
  1114. assert(p.get_pid() == daemon_instance);
  1115. assert(event_loop.active_timers.size() == 0);
  1116. sset.remove_service(&p);
  1117. }
  1118. // stop issued during smooth recovery, with dependent
  1119. void test_bgproc_smooth_recove2()
  1120. {
  1121. using namespace std;
  1122. service_set sset;
  1123. ha_string command = "test-command";
  1124. list<pair<unsigned,unsigned>> command_offsets;
  1125. command_offsets.emplace_back(0, command.length());
  1126. bgproc_service p {&sset, "testproc", ha_string(command), command_offsets, {}};
  1127. init_service_defaults(p);
  1128. p.set_smooth_recovery(true);
  1129. p.set_restart_delay(time_val {0, 1000});
  1130. p.set_pid_file("/run/daemon.pid");
  1131. sset.add_service(&p);
  1132. process_service d1 {&sset, "testproc-d1", std::move(command), command_offsets, {{&p, REG}}};
  1133. init_service_defaults(d1);
  1134. sset.add_service(&d1);
  1135. d1.start();
  1136. sset.process_queues();
  1137. // process for p exec succeds, reads pid file, starts
  1138. base_process_service_test::exec_succeeded(&p);
  1139. sset.process_queues();
  1140. pid_t daemon_instance;
  1141. supply_pid_contents("/run/daemon.pid", &daemon_instance);
  1142. assert(p.get_state() == service_state_t::STARTING);
  1143. base_process_service_test::handle_exit(&p, 0); // exit the launch process
  1144. sset.process_queues();
  1145. assert(p.get_state() == service_state_t::STARTED);
  1146. // dependent has been forked already:
  1147. assert(bp_sys::last_forked_pid == daemon_instance + 1);
  1148. // dependent then starts
  1149. base_process_service_test::exec_succeeded(&d1);
  1150. assert(bp_sys::last_forked_pid == daemon_instance + 1);
  1151. // exit daemon process unexpectedly:
  1152. base_process_service_test::handle_exit(&p, 0);
  1153. // since time hasn't been changed, we expect that the process has not yet been re-launched:
  1154. assert(p.get_state() == service_state_t::STARTED);
  1155. assert(bp_sys::last_forked_pid == daemon_instance + 1);
  1156. event_loop.advance_time(time_val {0, 1000});
  1157. // Now a new process should've been launched:
  1158. assert(bp_sys::last_forked_pid == daemon_instance + 2);
  1159. assert(p.get_state() == service_state_t::STARTED);
  1160. assert(event_loop.active_timers.size() == 1); // start timer
  1161. // We tell the service to stop, before the smooth recovery completes (in fact
  1162. // before the exec even succeeds):
  1163. p.stop(true);
  1164. sset.process_queues();
  1165. assert(p.get_state() == service_state_t::STOPPING);
  1166. assert(d1.get_state() == service_state_t::STOPPING);
  1167. // Now the bgprocess launcher completes but with a bogus pid file contents:
  1168. string pid_file_content_str = "";
  1169. vector<char> pid_file_content_v(pid_file_content_str.begin(), pid_file_content_str.end());
  1170. bp_sys::supply_file_content("/run/daemon.pid", std::move(pid_file_content_v));
  1171. base_process_service_test::exec_succeeded(&p);
  1172. base_process_service_test::handle_exit(&p, 0); // exit the launch process
  1173. assert(p.get_state() == service_state_t::STOPPING);
  1174. assert(d1.get_state() == service_state_t::STOPPING);
  1175. // Now the dependent stops:
  1176. base_process_service_test::handle_exit(&d1, 0);
  1177. assert(d1.get_state() == service_state_t::STOPPED);
  1178. assert(p.get_state() == service_state_t::STOPPED);
  1179. assert(event_loop.active_timers.size() == 0);
  1180. sset.remove_service(&p);
  1181. sset.remove_service(&d1);
  1182. }
  1183. // bgproc smooth recovery failure
  1184. void test_bgproc_smooth_recove3()
  1185. {
  1186. using namespace std;
  1187. service_set sset;
  1188. ha_string command = "test-command";
  1189. list<pair<unsigned,unsigned>> command_offsets;
  1190. command_offsets.emplace_back(0, command.length());
  1191. bgproc_service p {&sset, "testproc", ha_string(command), command_offsets, {}};
  1192. init_service_defaults(p);
  1193. p.set_smooth_recovery(true);
  1194. p.set_restart_delay(time_val {0, 1000});
  1195. p.set_pid_file("/run/daemon.pid");
  1196. sset.add_service(&p);
  1197. p.start();
  1198. sset.process_queues();
  1199. // process for p exec succeds, reads pid file, starts
  1200. base_process_service_test::exec_succeeded(&p);
  1201. sset.process_queues();
  1202. pid_t daemon_instance;
  1203. supply_pid_contents("/run/daemon.pid", &daemon_instance);
  1204. assert(p.get_state() == service_state_t::STARTING);
  1205. base_process_service_test::handle_exit(&p, 0); // exit the launch process
  1206. sset.process_queues();
  1207. assert(p.get_state() == service_state_t::STARTED);
  1208. // exit daemon process unexpectedly:
  1209. base_process_service_test::handle_exit(&p, 0);
  1210. // since time hasn't been changed, we expect that the process has not yet been re-launched:
  1211. assert(p.get_state() == service_state_t::STARTED);
  1212. assert(bp_sys::last_forked_pid == daemon_instance);
  1213. event_loop.advance_time(time_val {0, 1000});
  1214. // Now a new process should've been launched:
  1215. assert(bp_sys::last_forked_pid == daemon_instance + 1);
  1216. assert(p.get_state() == service_state_t::STARTED);
  1217. assert(event_loop.active_timers.size() == 1); // start timer
  1218. // Let the new process fail to executable:
  1219. base_process_service_test::exec_failed(&p, ENOMEM);
  1220. sset.process_queues();
  1221. assert(p.get_state() == service_state_t::STOPPED);
  1222. assert(p.get_stop_reason() == stopped_reason_t::TERMINATED);
  1223. assert(event_loop.active_timers.size() == 0);
  1224. sset.remove_service(&p);
  1225. }
  1226. // stop while in smooth recovery - waiting for restart timer
  1227. void test_bgproc_smooth_recove4()
  1228. {
  1229. using namespace std;
  1230. service_set sset;
  1231. ha_string command = "test-command";
  1232. list<pair<unsigned,unsigned>> command_offsets;
  1233. command_offsets.emplace_back(0, command.length());
  1234. bgproc_service p {&sset, "testproc", ha_string(command), command_offsets, {}};
  1235. init_service_defaults(p);
  1236. p.set_smooth_recovery(true);
  1237. p.set_restart_delay(time_val {0, 1000});
  1238. p.set_pid_file("/run/daemon.pid");
  1239. sset.add_service(&p);
  1240. p.start();
  1241. sset.process_queues();
  1242. // process for p exec succeds, reads pid file, starts
  1243. base_process_service_test::exec_succeeded(&p);
  1244. sset.process_queues();
  1245. pid_t daemon_instance;
  1246. supply_pid_contents("/run/daemon.pid", &daemon_instance);
  1247. assert(p.get_state() == service_state_t::STARTING);
  1248. base_process_service_test::handle_exit(&p, 0); // exit the launch process
  1249. sset.process_queues();
  1250. assert(p.get_state() == service_state_t::STARTED);
  1251. // exit daemon process unexpectedly:
  1252. base_process_service_test::handle_exit(&p, 0);
  1253. // since time hasn't been changed, we expect that the process has not yet been re-launched:
  1254. assert(p.get_state() == service_state_t::STARTED);
  1255. assert(bp_sys::last_forked_pid == daemon_instance);
  1256. // Now issue stop:
  1257. p.stop();
  1258. sset.process_queues();
  1259. // since process was not running, shouldn't need to wait for it to end:
  1260. assert(p.get_state() == service_state_t::STOPPED);
  1261. assert(event_loop.active_timers.size() == 0);
  1262. assert(bp_sys::last_forked_pid == daemon_instance);
  1263. // now start again:
  1264. p.start();
  1265. sset.process_queues();
  1266. supply_pid_contents("/run/daemon.pid", &daemon_instance);
  1267. assert(p.get_state() == service_state_t::STARTING);
  1268. base_process_service_test::exec_succeeded(&p);
  1269. base_process_service_test::handle_exit(&p, 0); // exit the launch process
  1270. sset.process_queues();
  1271. assert(p.get_state() == service_state_t::STARTED);
  1272. // and terminate:
  1273. p.stop();
  1274. sset.process_queues();
  1275. base_process_service_test::handle_exit(&p, 0);
  1276. sset.process_queues();
  1277. assert(p.get_state() == service_state_t::STOPPED);
  1278. sset.remove_service(&p);
  1279. }
  1280. // Unexpected termination with restart
  1281. void test_bgproc_term_restart()
  1282. {
  1283. using namespace std;
  1284. service_set sset;
  1285. ha_string command = "test-command";
  1286. list<pair<unsigned,unsigned>> command_offsets;
  1287. command_offsets.emplace_back(0, command.length());
  1288. std::list<prelim_dep> depends;
  1289. bgproc_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  1290. init_service_defaults(p);
  1291. p.set_auto_restart(true);
  1292. p.set_restart_delay(time_val {0, 1000});
  1293. p.set_pid_file("/run/daemon.pid");
  1294. sset.add_service(&p);
  1295. p.start();
  1296. sset.process_queues();
  1297. base_process_service_test::exec_succeeded(&p);
  1298. sset.process_queues();
  1299. assert(p.get_state() == service_state_t::STARTING);
  1300. pid_t daemon_instance;
  1301. supply_pid_contents("/run/daemon.pid", &daemon_instance);
  1302. // Launch process completes:
  1303. base_process_service_test::handle_exit(&p, 0); // exit the launch process
  1304. assert(p.get_state() == service_state_t::STARTED);
  1305. assert(p.get_pid() == daemon_instance);
  1306. // Unexpected termination of daemon:
  1307. base_process_service_test::handle_exit(&p, 1);
  1308. // Should re-start:
  1309. assert(p.get_state() == service_state_t::STARTING);
  1310. // Starting, restart timer should be armed:
  1311. assert(p.get_state() == service_state_t::STARTING);
  1312. assert(p.get_pid() == -1);
  1313. assert(event_loop.active_timers.size() == 1);
  1314. event_loop.advance_time(time_val(0, 1000));
  1315. assert(event_loop.active_timers.size() == 1); // start timer
  1316. assert(p.get_pid() != -1);
  1317. // Supply new pid
  1318. supply_pid_contents("/run/daemon.pid", &daemon_instance);
  1319. // Launch process completes again
  1320. base_process_service_test::handle_exit(&p, 0);
  1321. assert(p.get_state() == service_state_t::STARTED);
  1322. assert(p.get_pid() == daemon_instance);
  1323. assert(event_loop.active_timers.size() == 0);
  1324. sset.remove_service(&p);
  1325. }
  1326. void test_bgproc_stop()
  1327. {
  1328. using namespace std;
  1329. service_set sset;
  1330. ha_string command = "test-command";
  1331. list<pair<unsigned,unsigned>> command_offsets;
  1332. command_offsets.emplace_back(0, command.length());
  1333. std::list<prelim_dep> depends;
  1334. bgproc_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  1335. init_service_defaults(p);
  1336. p.set_auto_restart(true);
  1337. p.set_pid_file("/run/daemon.pid");
  1338. sset.add_service(&p);
  1339. p.start();
  1340. sset.process_queues();
  1341. base_process_service_test::exec_succeeded(&p);
  1342. sset.process_queues();
  1343. assert(p.get_state() == service_state_t::STARTING);
  1344. pid_t daemon_instance;
  1345. supply_pid_contents("/run/daemon.pid", &daemon_instance);
  1346. // Launch process completes:
  1347. base_process_service_test::handle_exit(&p, 0); // exit the launch process
  1348. assert(p.get_state() == service_state_t::STARTED);
  1349. assert(p.get_pid() == daemon_instance);
  1350. // Issue stop:
  1351. p.stop();
  1352. sset.process_queues();
  1353. assert(p.get_state() == service_state_t::STOPPING);
  1354. base_process_service_test::handle_exit(&p, 1);
  1355. assert(p.get_state() == service_state_t::STOPPED);
  1356. assert(event_loop.active_timers.size() == 0);
  1357. sset.remove_service(&p);
  1358. }
  1359. // If launch process completes successfully (exit code 0) after "stop" issued during startup,
  1360. // make sure the pid file is read and the daemon process is signalled.
  1361. void test_bgproc_stop2()
  1362. {
  1363. using namespace std;
  1364. service_set sset;
  1365. ha_string command = "test-command";
  1366. list<pair<unsigned,unsigned>> command_offsets;
  1367. command_offsets.emplace_back(0, command.length());
  1368. std::list<prelim_dep> depends;
  1369. bgproc_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  1370. init_service_defaults(p);
  1371. p.set_auto_restart(true);
  1372. p.set_pid_file("/run/daemon.pid");
  1373. sset.add_service(&p);
  1374. p.start();
  1375. sset.process_queues();
  1376. base_process_service_test::exec_succeeded(&p);
  1377. sset.process_queues();
  1378. assert(p.get_state() == service_state_t::STARTING);
  1379. pid_t daemon_instance;
  1380. supply_pid_contents("/run/daemon.pid", &daemon_instance);
  1381. // Now issue stop
  1382. p.stop();
  1383. // doesn't really matter if state is STARTING or STOPPING
  1384. assert(p.get_state() == service_state_t::STARTING || p.get_state() == service_state_t::STOPPING);
  1385. // Launch process completes:
  1386. bp_sys::last_sig_sent = -1;
  1387. base_process_service_test::handle_exit(&p, 0); // exit the launch process
  1388. // What should happen now: read the pid file, immediately signal the daemon, and go STOPPING
  1389. assert(p.get_pid() == daemon_instance);
  1390. assert(bp_sys::last_sig_sent == SIGTERM);
  1391. assert(p.get_state() == service_state_t::STOPPING);
  1392. // daemon exits:
  1393. base_process_service_test::handle_exit(&p, 1);
  1394. assert(p.get_state() == service_state_t::STOPPED);
  1395. assert(event_loop.active_timers.size() == 0);
  1396. sset.remove_service(&p);
  1397. }
  1398. // Stop issued during smooth recovery
  1399. void test_bgproc_stop3()
  1400. {
  1401. using namespace std;
  1402. service_set sset;
  1403. ha_string command = "test-command";
  1404. list<pair<unsigned,unsigned>> command_offsets;
  1405. command_offsets.emplace_back(0, command.length());
  1406. std::list<prelim_dep> depends;
  1407. bgproc_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  1408. init_service_defaults(p);
  1409. p.set_smooth_recovery(true);
  1410. p.set_restart_delay(time_val {0, 1000});
  1411. p.set_pid_file("/run/daemon.pid");
  1412. sset.add_service(&p);
  1413. p.start();
  1414. sset.process_queues();
  1415. base_process_service_test::exec_succeeded(&p);
  1416. sset.process_queues();
  1417. pid_t daemon_instance;
  1418. supply_pid_contents("/run/daemon.pid", &daemon_instance);
  1419. assert(p.get_state() == service_state_t::STARTING);
  1420. base_process_service_test::handle_exit(&p, 0); // exit the launch process
  1421. sset.process_queues();
  1422. // daemon process has been started now, state should be STARTED
  1423. assert(p.get_state() == service_state_t::STARTED);
  1424. assert(daemon_instance == bp_sys::last_forked_pid);
  1425. base_process_service_test::handle_exit(&p, 0); // exit the daemon process
  1426. // since time hasn't been changed, we expect that the process has not yet been re-launched:
  1427. assert(p.get_state() == service_state_t::STARTED);
  1428. assert(daemon_instance == bp_sys::last_forked_pid);
  1429. event_loop.advance_time(time_val {0, 1000});
  1430. sset.process_queues();
  1431. // Now a new process should've been launched:
  1432. assert(daemon_instance + 1 == bp_sys::last_forked_pid);
  1433. assert(p.get_state() == service_state_t::STARTED);
  1434. assert(event_loop.active_timers.size() == 1); // start timer
  1435. base_process_service_test::exec_succeeded(&p);
  1436. supply_pid_contents("/run/daemon.pid", &daemon_instance);
  1437. // Now, we are in smooth recovery (state = STARTED), launch process is running.
  1438. p.stop();
  1439. assert(p.get_state() == service_state_t::STOPPING);
  1440. assert(event_loop.active_timers.size() == 1);
  1441. base_process_service_test::handle_exit(&p, 0);
  1442. // The launch process terminated, but *successfully*. So the PID file should be read and the
  1443. // daemon process killed also.
  1444. assert(p.get_state() == service_state_t::STOPPING);
  1445. assert(p.get_pid() == daemon_instance);
  1446. base_process_service_test::handle_exit(&p, 0); // exit the daemon process
  1447. assert(p.get_state() == service_state_t::STOPPED);
  1448. assert(daemon_instance == bp_sys::last_forked_pid);
  1449. assert(event_loop.active_timers.size() == 0);
  1450. sset.remove_service(&p);
  1451. }
  1452. // stop issued via command (service with stop-command set)
  1453. void test_bgproc_stop4()
  1454. {
  1455. using namespace std;
  1456. service_set sset;
  1457. ha_string command = "test-command";
  1458. list<pair<unsigned,unsigned>> command_offsets;
  1459. command_offsets.emplace_back(0, command.length());
  1460. std::list<prelim_dep> depends;
  1461. ha_string stop_command = "stop-command";
  1462. list<pair<unsigned,unsigned>> stop_command_offsets;
  1463. stop_command_offsets.emplace_back(0, stop_command.length());
  1464. bgproc_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  1465. init_service_defaults(p);
  1466. p.set_smooth_recovery(true);
  1467. p.set_restart_delay(time_val {0, 1000});
  1468. p.set_pid_file("/run/daemon.pid");
  1469. sset.add_service(&p);
  1470. p.set_stop_command(stop_command, stop_command_offsets);
  1471. p.start();
  1472. sset.process_queues();
  1473. base_process_service_test::exec_succeeded(&p);
  1474. sset.process_queues();
  1475. pid_t daemon_instance;
  1476. supply_pid_contents("/run/daemon.pid", &daemon_instance);
  1477. assert(p.get_state() == service_state_t::STARTING);
  1478. base_process_service_test::handle_exit(&p, 0); // exit the launch process
  1479. sset.process_queues();
  1480. // daemon process has been started now, state should be STARTED
  1481. assert(p.get_state() == service_state_t::STARTED);
  1482. assert(daemon_instance == bp_sys::last_forked_pid);
  1483. // so stop:
  1484. p.stop();
  1485. sset.process_queues();
  1486. base_process_service_test::handle_stop_exit(&p, 0); // exit the daemon process
  1487. sset.process_queues();
  1488. assert(p.get_state() == service_state_t::STOPPING);
  1489. base_process_service_test::handle_exit(&p, 0);
  1490. sset.process_queues();
  1491. assert(p.get_state() == service_state_t::STOPPED);
  1492. assert(event_loop.active_timers.size() == 0);
  1493. sset.remove_service(&p);
  1494. }
  1495. // stop issued via command (service with stop-command set); service process dies before stop command
  1496. void test_bgproc_stop5()
  1497. {
  1498. using namespace std;
  1499. service_set sset;
  1500. ha_string command = "test-command";
  1501. list<pair<unsigned,unsigned>> command_offsets;
  1502. command_offsets.emplace_back(0, command.length());
  1503. std::list<prelim_dep> depends;
  1504. ha_string stop_command = "stop-command";
  1505. list<pair<unsigned,unsigned>> stop_command_offsets;
  1506. stop_command_offsets.emplace_back(0, stop_command.length());
  1507. bgproc_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  1508. init_service_defaults(p);
  1509. p.set_smooth_recovery(true);
  1510. p.set_restart_delay(time_val {0, 1000});
  1511. p.set_pid_file("/run/daemon.pid");
  1512. sset.add_service(&p);
  1513. p.set_stop_command(stop_command, stop_command_offsets);
  1514. p.start();
  1515. sset.process_queues();
  1516. base_process_service_test::exec_succeeded(&p);
  1517. sset.process_queues();
  1518. pid_t daemon_instance;
  1519. supply_pid_contents("/run/daemon.pid", &daemon_instance);
  1520. assert(p.get_state() == service_state_t::STARTING);
  1521. base_process_service_test::handle_exit(&p, 0); // exit the launch process
  1522. sset.process_queues();
  1523. // daemon process has been started now, state should be STARTED
  1524. assert(p.get_state() == service_state_t::STARTED);
  1525. assert(daemon_instance == bp_sys::last_forked_pid);
  1526. // so stop:
  1527. p.stop();
  1528. sset.process_queues();
  1529. assert(p.get_state() == service_state_t::STOPPING);
  1530. base_process_service_test::handle_exit(&p, 0);
  1531. sset.process_queues();
  1532. assert(p.get_state() == service_state_t::STOPPING);
  1533. base_process_service_test::handle_stop_exit(&p, 0); // exit the daemon process
  1534. sset.process_queues();
  1535. assert(p.get_state() == service_state_t::STOPPED);
  1536. assert(event_loop.active_timers.size() == 0);
  1537. sset.remove_service(&p);
  1538. }
  1539. // Test stop timeout
  1540. void test_scripted_stop_timeout()
  1541. {
  1542. using namespace std;
  1543. service_set sset;
  1544. ha_string command = "test-command";
  1545. ha_string stopcommand = "stop-command";
  1546. list<pair<unsigned,unsigned>> command_offsets;
  1547. command_offsets.emplace_back(0, command.length());
  1548. std::list<prelim_dep> depends;
  1549. scripted_service p {&sset, "testscripted", std::move(command), command_offsets, depends};
  1550. init_service_defaults(p);
  1551. p.set_stop_command(stopcommand, command_offsets);
  1552. p.set_stop_timeout(time_val {10, 0});
  1553. sset.add_service(&p);
  1554. p.start();
  1555. sset.process_queues();
  1556. assert(p.get_state() == service_state_t::STARTING);
  1557. base_process_service_test::exec_succeeded(&p);
  1558. sset.process_queues();
  1559. base_process_service_test::handle_exit(&p, 0);
  1560. sset.process_queues();
  1561. assert(p.get_state() == service_state_t::STARTED);
  1562. p.stop(true);
  1563. sset.process_queues();
  1564. assert(p.get_state() == service_state_t::STOPPING);
  1565. base_process_service_test::exec_succeeded(&p);
  1566. sset.process_queues();
  1567. // should still be stopping:
  1568. assert(p.get_state() == service_state_t::STOPPING);
  1569. event_loop.advance_time(time_val {10, 0}); // expire stop timer
  1570. sset.process_queues();
  1571. // kill signal (SIGKILL) should have been sent; process not dead until it's dead, however
  1572. assert(p.get_state() == service_state_t::STOPPING);
  1573. assert(bp_sys::last_sig_sent == SIGKILL);
  1574. base_process_service_test::handle_exit(&p, SIGKILL);
  1575. sset.process_queues();
  1576. assert(p.get_state() == service_state_t::STOPPED);
  1577. assert(p.get_stop_reason() == stopped_reason_t::NORMAL);
  1578. assert(event_loop.active_timers.size() == 0);
  1579. sset.remove_service(&p);
  1580. }
  1581. void test_scripted_start_fail()
  1582. {
  1583. using namespace std;
  1584. service_set sset;
  1585. ha_string command = "test-command";
  1586. ha_string stopcommand = "stop-command";
  1587. list<pair<unsigned,unsigned>> command_offsets;
  1588. command_offsets.emplace_back(0, command.length());
  1589. std::list<prelim_dep> depends;
  1590. scripted_service p {&sset, "testscripted", std::move(command), command_offsets, depends};
  1591. init_service_defaults(p);
  1592. p.set_stop_command(stopcommand, command_offsets);
  1593. sset.add_service(&p);
  1594. service_record *s2 = new service_record(&sset, "test-service-2", service_type_t::INTERNAL, {{&p, REG}});
  1595. service_record *s3 = new service_record(&sset, "test-service-3",
  1596. service_type_t::INTERNAL, {{&p, REG}, {s2, REG}});
  1597. sset.add_service(s2);
  1598. sset.add_service(s3);
  1599. s3->start();
  1600. sset.process_queues();
  1601. assert(p.get_state() == service_state_t::STARTING);
  1602. base_process_service_test::exec_succeeded(&p);
  1603. sset.process_queues();
  1604. base_process_service_test::handle_exit(&p, 0x1); // exit fail
  1605. sset.process_queues();
  1606. // failed to start:
  1607. assert(p.get_state() == service_state_t::STOPPED);
  1608. assert(s2->get_state() == service_state_t::STOPPED);
  1609. assert(s3->get_state() == service_state_t::STOPPED);
  1610. assert(p.get_stop_reason() == stopped_reason_t::FAILED);
  1611. assert(s2->get_stop_reason() == stopped_reason_t::DEPFAILED);
  1612. assert(s3->get_stop_reason() == stopped_reason_t::DEPFAILED);
  1613. event_loop.active_timers.clear();
  1614. sset.remove_service(&p);
  1615. assert(sset.count_active_services() == 0);
  1616. }
  1617. void test_scripted_stop_fail()
  1618. {
  1619. using namespace std;
  1620. service_set sset;
  1621. ha_string command = "test-command";
  1622. ha_string stopcommand = "stop-command";
  1623. list<pair<unsigned,unsigned>> command_offsets;
  1624. command_offsets.emplace_back(0, command.length());
  1625. std::list<prelim_dep> depends;
  1626. scripted_service p {&sset, "testscripted", std::move(command), command_offsets, depends};
  1627. init_service_defaults(p);
  1628. p.set_stop_command(stopcommand, command_offsets);
  1629. sset.add_service(&p);
  1630. service_record *s2 = new service_record(&sset, "test-service-2", service_type_t::INTERNAL, {});
  1631. service_record *s3 = new service_record(&sset, "test-service-3", service_type_t::INTERNAL,
  1632. {{s2, REG}, {&p, REG}});
  1633. service_record *s4 = new service_record(&sset, "test-service-4", service_type_t::INTERNAL,
  1634. {{&p, REG}, {s3, REG}});
  1635. sset.add_service(s2);
  1636. sset.add_service(s3);
  1637. sset.add_service(s4);
  1638. s4->start();
  1639. sset.process_queues();
  1640. base_process_service_test::exec_succeeded(&p);
  1641. sset.process_queues();
  1642. base_process_service_test::handle_exit(&p, 0x0); // success
  1643. sset.process_queues();
  1644. assert(p.get_state() == service_state_t::STARTED);
  1645. assert(s2->get_state() == service_state_t::STARTED);
  1646. assert(s3->get_state() == service_state_t::STARTED);
  1647. assert(s4->get_state() == service_state_t::STARTED);
  1648. pid_t last_forked = bp_sys::last_forked_pid;
  1649. s4->stop(true);
  1650. sset.process_queues();
  1651. base_process_service_test::exec_succeeded(&p);
  1652. sset.process_queues();
  1653. base_process_service_test::handle_exit(&p, 0x1); // failure
  1654. sset.process_queues();
  1655. // The stop command should be executed once:
  1656. assert((bp_sys::last_forked_pid - last_forked) == 1);
  1657. assert(p.get_state() == service_state_t::STOPPED);
  1658. assert(s2->get_state() == service_state_t::STOPPED);
  1659. assert(s3->get_state() == service_state_t::STOPPED);
  1660. assert(s4->get_state() == service_state_t::STOPPED);
  1661. event_loop.active_timers.clear();
  1662. sset.remove_service(&p);
  1663. }
  1664. void test_scripted_start_skip()
  1665. {
  1666. using namespace std;
  1667. service_set sset;
  1668. ha_string command = "test-command";
  1669. list<pair<unsigned,unsigned>> command_offsets;
  1670. command_offsets.emplace_back(0, command.length());
  1671. std::list<prelim_dep> depends;
  1672. scripted_service p {&sset, "testscripted", std::move(command), command_offsets, depends};
  1673. init_service_defaults(p);
  1674. service_flags_t sflags;
  1675. sflags.skippable = true;
  1676. p.set_flags(sflags);
  1677. sset.add_service(&p);
  1678. service_record *s2 = new service_record(&sset, "test-service-2", service_type_t::INTERNAL, {{&p, REG}});
  1679. sset.add_service(s2);
  1680. s2->start();
  1681. sset.process_queues();
  1682. assert(p.get_state() == service_state_t::STARTING);
  1683. base_process_service_test::exec_succeeded(&p);
  1684. sset.process_queues();
  1685. assert(p.get_state() == service_state_t::STARTING);
  1686. base_process_service_test::handle_signal_exit(&p, SIGINT); // interrupted
  1687. sset.process_queues();
  1688. assert(p.get_state() == service_state_t::STARTED);
  1689. assert(s2->get_state() == service_state_t::STARTED);
  1690. assert(p.was_start_skipped());
  1691. assert(! s2->was_start_skipped());
  1692. assert(sset.count_active_services() == 2);
  1693. s2->stop(true);
  1694. sset.process_queues();
  1695. assert(p.get_state() == service_state_t::STOPPED);
  1696. assert(s2->get_state() == service_state_t::STOPPED);
  1697. assert(p.get_stop_reason() == stopped_reason_t::NORMAL);
  1698. assert(s2->get_stop_reason() == stopped_reason_t::NORMAL);
  1699. assert(sset.count_active_services() == 0);
  1700. event_loop.active_timers.clear();
  1701. sset.remove_service(&p);
  1702. }
  1703. // Test interrupting start of a service marked skippable
  1704. void test_scripted_start_skip2()
  1705. {
  1706. using namespace std;
  1707. service_set sset;
  1708. ha_string command = "test-command";
  1709. list<pair<unsigned,unsigned>> command_offsets;
  1710. command_offsets.emplace_back(0, command.length());
  1711. std::list<prelim_dep> depends;
  1712. scripted_service p {&sset, "testscripted", std::move(command), command_offsets, depends};
  1713. init_service_defaults(p);
  1714. service_flags_t sflags;
  1715. sflags.skippable = true;
  1716. sflags.start_interruptible = true;
  1717. p.set_flags(sflags);
  1718. sset.add_service(&p);
  1719. service_record *s2 = new service_record(&sset, "test-service-2", service_type_t::INTERNAL, {{&p, REG}});
  1720. sset.add_service(s2);
  1721. s2->start();
  1722. sset.process_queues();
  1723. assert(p.get_state() == service_state_t::STARTING);
  1724. base_process_service_test::exec_succeeded(&p);
  1725. sset.process_queues();
  1726. assert(p.get_state() == service_state_t::STARTING);
  1727. s2->stop(true); // abort startup; p should be cancelled
  1728. sset.process_queues();
  1729. assert(p.get_state() == service_state_t::STOPPING);
  1730. base_process_service_test::handle_signal_exit(&p, SIGINT); // interrupted
  1731. sset.process_queues();
  1732. assert(p.get_state() == service_state_t::STOPPED);
  1733. assert(s2->get_state() == service_state_t::STOPPED);
  1734. assert(p.get_stop_reason() == stopped_reason_t::NORMAL);
  1735. assert(s2->get_stop_reason() == stopped_reason_t::NORMAL);
  1736. assert(sset.count_active_services() == 0);
  1737. event_loop.active_timers.clear();
  1738. sset.remove_service(&p);
  1739. }
  1740. // Test that starting a service with a waits-for dependency on another - currently stopping - service,
  1741. // causes that service to re-start.
  1742. void test_waitsfor_restart()
  1743. {
  1744. using namespace std;
  1745. service_set sset;
  1746. ha_string command = "test-command";
  1747. list<pair<unsigned,unsigned>> command_offsets;
  1748. command_offsets.emplace_back(0, command.length());
  1749. std::list<prelim_dep> depends;
  1750. process_service p {&sset, "testproc", std::move(command), command_offsets, depends};
  1751. init_service_defaults(p);
  1752. sset.add_service(&p);
  1753. service_record tp {&sset, "test-service", service_type_t::INTERNAL, {{&p, WAITS}}};
  1754. sset.add_service(&tp);
  1755. // start p:
  1756. p.start();
  1757. sset.process_queues();
  1758. assert(p.get_state() == service_state_t::STARTING);
  1759. base_process_service_test::exec_succeeded(&p);
  1760. sset.process_queues();
  1761. assert(p.get_state() == service_state_t::STARTED);
  1762. assert(event_loop.active_timers.size() == 0);
  1763. // begin stopping p:
  1764. p.stop(true);
  1765. sset.process_queues();
  1766. assert(p.get_state() == service_state_t::STOPPING);
  1767. // start tp (which waits-for p):
  1768. tp.start();
  1769. sset.process_queues();
  1770. assert(tp.get_state() == service_state_t::STARTING);
  1771. assert(p.get_state() == service_state_t::STOPPING);
  1772. // p terminates (finishes stopping). Then it should re-start...
  1773. base_process_service_test::handle_signal_exit(&p, SIGTERM);
  1774. sset.process_queues();
  1775. assert(tp.get_state() == service_state_t::STARTING);
  1776. assert(p.get_state() == service_state_t::STARTING);
  1777. base_process_service_test::exec_succeeded(&p);
  1778. sset.process_queues();
  1779. assert(tp.get_state() == service_state_t::STARTED);
  1780. assert(p.get_state() == service_state_t::STARTED);
  1781. sset.remove_service(&tp);
  1782. sset.remove_service(&p);
  1783. }
  1784. #define RUN_TEST(name, spacing) \
  1785. std::cout << #name "..." spacing << std::flush; \
  1786. name(); \
  1787. std::cout << "PASSED" << std::endl;
  1788. int main(int argc, char **argv)
  1789. {
  1790. RUN_TEST(test_proc_service_start, " ");
  1791. RUN_TEST(test_proc_notify_start, " ");
  1792. RUN_TEST(test_proc_unexpected_term, " ");
  1793. RUN_TEST(test_proc_term_start, " ");
  1794. RUN_TEST(test_proc_term_restart, " ");
  1795. RUN_TEST(test_proc_term_restart2, " ");
  1796. RUN_TEST(test_proc_term_restart3, " ");
  1797. RUN_TEST(test_proc_term_restart4, " ");
  1798. RUN_TEST(test_proc_term_restart_fail, "");
  1799. RUN_TEST(test_term_via_stop, " ");
  1800. RUN_TEST(test_term_via_stop2, " ");
  1801. RUN_TEST(test_term_via_stop3, " ");
  1802. RUN_TEST(test_proc_start_timeout, " ");
  1803. RUN_TEST(test_proc_start_timeout2, " ");
  1804. RUN_TEST(test_proc_start_execfail, " ");
  1805. RUN_TEST(test_proc_notify_fail, " ");
  1806. RUN_TEST(test_proc_stop_timeout, " ");
  1807. RUN_TEST(test_proc_smooth_recovery1, " ");
  1808. RUN_TEST(test_proc_smooth_recovery2, " ");
  1809. RUN_TEST(test_proc_smooth_recovery3, " ");
  1810. RUN_TEST(test_proc_smooth_recovery4, " ");
  1811. RUN_TEST(test_proc_smooth_recovery5, " ");
  1812. RUN_TEST(test_proc_smooth_recovery6, " ");
  1813. RUN_TEST(test_proc_smooth_recovery6a, "");
  1814. RUN_TEST(test_bgproc_start, " ");
  1815. RUN_TEST(test_bgproc_start_fail, " ");
  1816. RUN_TEST(test_bgproc_start_fail_pid, " ");
  1817. RUN_TEST(test_bgproc_unexpected_term, "");
  1818. RUN_TEST(test_bgproc_smooth_recover, " ");
  1819. RUN_TEST(test_bgproc_smooth_recove2, " ");
  1820. RUN_TEST(test_bgproc_smooth_recove3, " ");
  1821. RUN_TEST(test_bgproc_smooth_recove4, " ");
  1822. RUN_TEST(test_bgproc_term_restart, " ");
  1823. RUN_TEST(test_bgproc_stop, " ");
  1824. RUN_TEST(test_bgproc_stop2, " ");
  1825. RUN_TEST(test_bgproc_stop3, " ");
  1826. RUN_TEST(test_bgproc_stop4, " ");
  1827. RUN_TEST(test_bgproc_stop5, " ");
  1828. RUN_TEST(test_scripted_stop_timeout, " ");
  1829. RUN_TEST(test_scripted_start_fail, " ");
  1830. RUN_TEST(test_scripted_stop_fail, " ");
  1831. RUN_TEST(test_scripted_start_skip, " ");
  1832. RUN_TEST(test_scripted_start_skip2, " ");
  1833. RUN_TEST(test_waitsfor_restart, " ");
  1834. }