testsam.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  1. /*
  2. * Copyright (c) 2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. /*
  35. * Provides test of SAM API
  36. */
  37. #include <config.h>
  38. #include <limits.h>
  39. #include <pthread.h>
  40. #include <sys/types.h>
  41. #include <stdio.h>
  42. #include <stdint.h>
  43. #include <stdlib.h>
  44. #include <unistd.h>
  45. #include <corosync/corotypes.h>
  46. #include <corosync/confdb.h>
  47. #include <corosync/sam.h>
  48. #include <signal.h>
  49. #include <string.h>
  50. #include <sys/wait.h>
  51. extern const char *__progname;
  52. static int test2_sig_delivered = 0;
  53. static int test5_hc_cb_count = 0;
  54. static int test6_sig_delivered = 0;
  55. /*
  56. * First test will just register SAM, with policy restart. First instance will
  57. * sleep one second, send hc and sleep another 3 seconds. This should force restart.
  58. * Second instance will sleep one second, send hc, stop hc and sleep 3 seconds.
  59. * Then start hc again and sleep 3 seconds. This should force restart again.
  60. * Last instance just calls initialize again. This should end with error.
  61. * Then call start, followed by stop and start again. Finally, we will call finalize
  62. * twice. One should succeed, second should fail. After this, we will call every function
  63. * (none should succeed).
  64. */
  65. static int test1 (void)
  66. {
  67. cs_error_t error;
  68. unsigned int instance_id;
  69. int i;
  70. printf ("%s: initialize\n", __FUNCTION__);
  71. error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART);
  72. if (error != CS_OK) {
  73. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  74. return 1;
  75. }
  76. printf ("%s: register\n", __FUNCTION__);
  77. error = sam_register (&instance_id);
  78. if (error != CS_OK) {
  79. fprintf (stderr, "Can't register. Error %d\n", error);
  80. return 1;
  81. }
  82. if (instance_id == 1 || instance_id == 2) {
  83. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  84. error = sam_start ();
  85. if (error != CS_OK) {
  86. fprintf (stderr, "Can't start hc. Error %d\n", error);
  87. return 1;
  88. }
  89. for (i = 0; i < 10; i++) {
  90. printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  91. sleep (1);
  92. printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id);
  93. error = sam_hc_send ();
  94. if (error != CS_OK) {
  95. fprintf (stderr, "Can't send hc. Error %d\n", error);
  96. return 1;
  97. }
  98. }
  99. if (instance_id == 2) {
  100. printf ("%s iid %d: stop\n", __FUNCTION__, instance_id);
  101. error = sam_stop ();
  102. if (error != CS_OK) {
  103. fprintf (stderr, "Can't send hc. Error %d\n", error);
  104. return 1;
  105. }
  106. }
  107. printf ("%s iid %d: sleep 3\n", __FUNCTION__, instance_id);
  108. sleep (3);
  109. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  110. error = sam_start ();
  111. if (error != CS_OK) {
  112. fprintf (stderr, "Can't start hc. Error %d\n", error);
  113. return 1;
  114. }
  115. printf ("%s iid %d: sleep 3\n", __FUNCTION__, instance_id);
  116. sleep (3);
  117. return 0;
  118. }
  119. if (instance_id == 3) {
  120. error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART);
  121. if (error == CS_OK) {
  122. fprintf (stderr, "Can initialize SAM API after initialization");
  123. return 1;
  124. }
  125. error = sam_start ();
  126. if (error != CS_OK) {
  127. fprintf (stderr, "Can't start hc. Error %d\n", error);
  128. return 1;
  129. }
  130. error = sam_stop ();
  131. if (error != CS_OK) {
  132. fprintf (stderr, "Can't stop hc. Error %d\n", error);
  133. return 1;
  134. }
  135. error = sam_finalize ();
  136. if (error != CS_OK) {
  137. fprintf (stderr, "Can't finalize sam. Error %d\n", error);
  138. return 1;
  139. }
  140. error = sam_finalize ();
  141. if (error == CS_OK) {
  142. fprintf (stderr, "Can finalize sam after finalization!\n");
  143. return 1;
  144. }
  145. if (sam_initialize (2, SAM_RECOVERY_POLICY_RESTART) == CS_OK ||
  146. sam_start () == CS_OK || sam_stop () == CS_OK ||
  147. sam_register (NULL) == CS_OK || sam_hc_send () == CS_OK ||
  148. sam_hc_callback_register (NULL) == CS_OK) {
  149. fprintf (stderr, "Can call one of function after finalization!\n");
  150. return 1;
  151. }
  152. return 0;
  153. }
  154. return 1;
  155. }
  156. static void test2_signal (int sig) {
  157. printf ("%s\n", __FUNCTION__);
  158. test2_sig_delivered = 1;
  159. }
  160. /*
  161. * This tests recovery policy quit and callback.
  162. */
  163. static int test2 (void) {
  164. cs_error_t error;
  165. unsigned int instance_id;
  166. printf ("%s: initialize\n", __FUNCTION__);
  167. error = sam_initialize (2000, SAM_RECOVERY_POLICY_QUIT);
  168. if (error != CS_OK) {
  169. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  170. return 1;
  171. }
  172. printf ("%s: register\n", __FUNCTION__);
  173. error = sam_register (&instance_id);
  174. if (error != CS_OK) {
  175. fprintf (stderr, "Can't register. Error %d\n", error);
  176. return 1;
  177. }
  178. if (instance_id == 1) {
  179. signal (SIGTERM, test2_signal);
  180. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  181. error = sam_start ();
  182. if (error != CS_OK) {
  183. fprintf (stderr, "Can't start hc. Error %d\n", error);
  184. return 1;
  185. }
  186. printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  187. sleep (1);
  188. printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id);
  189. error = sam_hc_send ();
  190. if (error != CS_OK) {
  191. fprintf (stderr, "Can't send hc. Error %d\n", error);
  192. return 1;
  193. }
  194. printf ("%s iid %d: wait for delivery of signal\n", __FUNCTION__, instance_id);
  195. while (!test2_sig_delivered) {
  196. sleep (1);
  197. }
  198. printf ("%s iid %d: wait for real kill\n", __FUNCTION__, instance_id);
  199. sleep (3);
  200. }
  201. return 1;
  202. }
  203. /*
  204. * Smoke test. Better to turn off coredump ;) This has no time limit, just restart process
  205. * when it dies.
  206. */
  207. static int test3 (void) {
  208. cs_error_t error;
  209. unsigned int instance_id;
  210. int tmp1, tmp2, tmp3;
  211. printf ("%s: initialize\n", __FUNCTION__);
  212. error = sam_initialize (0, SAM_RECOVERY_POLICY_RESTART);
  213. if (error != CS_OK) {
  214. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  215. return 1;
  216. }
  217. printf ("%s: register\n", __FUNCTION__);
  218. error = sam_register (&instance_id);
  219. if (error != CS_OK) {
  220. fprintf (stderr, "Can't register. Error %d\n", error);
  221. return 1;
  222. }
  223. if (instance_id < 100) {
  224. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  225. error = sam_start ();
  226. if (error != CS_OK) {
  227. fprintf (stderr, "Can't start hc. Error %d\n", error);
  228. return 1;
  229. }
  230. printf ("%s iid %d: divide by zero\n", __FUNCTION__, instance_id);
  231. tmp2 = rand ();
  232. tmp3 = 0;
  233. tmp1 = tmp2 / tmp3;
  234. tmp3 = tmp1;
  235. return 1;
  236. }
  237. return 0;
  238. }
  239. /*
  240. * Test sam_data_store, sam_data_restore and sam_data_getsize
  241. */
  242. static int test4 (void)
  243. {
  244. size_t size;
  245. cs_error_t err;
  246. int i;
  247. unsigned int instance_id;
  248. char saved_data[128];
  249. char saved_data2[128];
  250. printf ("%s: sam_data_getsize 1\n", __FUNCTION__);
  251. err = sam_data_getsize (&size);
  252. if (err != CS_ERR_BAD_HANDLE) {
  253. fprintf (stderr, "Test should return CS_ERR_BAD_HANDLE. Error returned %d\n", err);
  254. return 1;
  255. }
  256. printf ("%s: sam_data_getsize 2\n", __FUNCTION__);
  257. err = sam_data_getsize (NULL);
  258. if (err != CS_ERR_INVALID_PARAM) {
  259. fprintf (stderr, "Test should return CS_ERR_INVALID_PARAM. Error returned %d\n", err);
  260. return 1;
  261. }
  262. printf ("%s: sam_data_store 1\n", __FUNCTION__);
  263. err = sam_data_store (NULL, 0);
  264. if (err != CS_ERR_BAD_HANDLE) {
  265. fprintf (stderr, "Test should return CS_ERR_BAD_HANDLE. Error returned %d\n", err);
  266. return 1;
  267. }
  268. printf ("%s: sam_data_restore 1\n", __FUNCTION__);
  269. err = sam_data_restore (saved_data, sizeof (saved_data));
  270. if (err != CS_ERR_BAD_HANDLE) {
  271. fprintf (stderr, "Test should return CS_ERR_BAD_HANDLE. Error returned %d\n", err);
  272. return 1;
  273. }
  274. printf ("%s: sam_initialize\n", __FUNCTION__);
  275. err = sam_initialize (0, SAM_RECOVERY_POLICY_RESTART);
  276. if (err != CS_OK) {
  277. fprintf (stderr, "Can't initialize SAM API. Error %d\n", err);
  278. return 1;
  279. }
  280. printf ("%s: sam_data_getsize 3\n", __FUNCTION__);
  281. err = sam_data_getsize (&size);
  282. if (err != CS_OK) {
  283. fprintf (stderr, "Test should return CS_ERR_BAD_HANDLE. Error returned %d\n", err);
  284. return 1;
  285. }
  286. if (size != 0) {
  287. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  288. return 1;
  289. }
  290. printf ("%s: sam_data_restore 2\n", __FUNCTION__);
  291. err = sam_data_restore (NULL, sizeof (saved_data));
  292. if (err != CS_ERR_INVALID_PARAM) {
  293. fprintf (stderr, "Test should return CS_ERR_INVALID_PARAM. Error returned %d\n", err);
  294. return 1;
  295. }
  296. /*
  297. * Store some real data
  298. */
  299. for (i = 0; i < sizeof (saved_data); i++) {
  300. saved_data[i] = (char)(i + 5);
  301. }
  302. printf ("%s: sam_data_store 2\n", __FUNCTION__);
  303. err = sam_data_store (saved_data, sizeof (saved_data));
  304. if (err != CS_OK) {
  305. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  306. return 1;
  307. }
  308. printf ("%s: sam_data_getsize 4\n", __FUNCTION__);
  309. err = sam_data_getsize (&size);
  310. if (err != CS_OK) {
  311. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  312. return 1;
  313. }
  314. if (size != sizeof (saved_data)) {
  315. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  316. return 1;
  317. }
  318. printf ("%s: sam_data_restore 3\n", __FUNCTION__);
  319. err = sam_data_restore (saved_data2, sizeof (saved_data2) - 1);
  320. if (err != CS_ERR_INVALID_PARAM) {
  321. fprintf (stderr, "Test should return CS_ERR_INVALID_PARAM. Error returned %d\n", err);
  322. return 1;
  323. }
  324. printf ("%s: sam_data_restore 4\n", __FUNCTION__);
  325. err = sam_data_restore (saved_data2, sizeof (saved_data2));
  326. if (err != CS_OK) {
  327. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  328. return 1;
  329. }
  330. if (memcmp (saved_data, saved_data2, sizeof (saved_data2)) != 0) {
  331. fprintf (stderr, "Retored data are not same\n");
  332. return 1;
  333. }
  334. memset (saved_data2, 0, sizeof (saved_data2));
  335. printf ("%s: sam_data_store 3\n", __FUNCTION__);
  336. err = sam_data_store (NULL, 1);
  337. if (err != CS_OK) {
  338. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  339. return 1;
  340. }
  341. printf ("%s: sam_data_getsize 5\n", __FUNCTION__);
  342. err = sam_data_getsize (&size);
  343. if (err != CS_OK) {
  344. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  345. return 1;
  346. }
  347. if (size != 0) {
  348. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  349. return 1;
  350. }
  351. printf ("%s: sam_data_store 4\n", __FUNCTION__);
  352. err = sam_data_store (saved_data, sizeof (saved_data));
  353. if (err != CS_OK) {
  354. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  355. return 1;
  356. }
  357. printf ("%s: register\n", __FUNCTION__);
  358. err = sam_register (&instance_id);
  359. if (err != CS_OK) {
  360. fprintf (stderr, "Can't register. Error %d\n", err);
  361. return 1;
  362. }
  363. if (instance_id == 1) {
  364. printf ("%s iid %d: sam_start\n", __FUNCTION__, instance_id);
  365. err = sam_start ();
  366. if (err != CS_OK) {
  367. fprintf (stderr, "Can't start hc. Error %d\n", err);
  368. return 1;
  369. }
  370. printf ("%s iid %d: sam_data_getsize 6\n", __FUNCTION__, instance_id);
  371. err = sam_data_getsize (&size);
  372. if (err != CS_OK) {
  373. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  374. return 1;
  375. }
  376. if (size != sizeof (saved_data2)) {
  377. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  378. return 1;
  379. }
  380. printf ("%s iid %d: sam_data_restore 5\n", __FUNCTION__, instance_id);
  381. err = sam_data_restore (saved_data2, sizeof (saved_data2));
  382. if (err != CS_OK) {
  383. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  384. return 1;
  385. }
  386. if (memcmp (saved_data, saved_data2, sizeof (saved_data2)) != 0) {
  387. fprintf (stderr, "Retored data are not same\n");
  388. return 1;
  389. }
  390. for (i = 0; i < sizeof (saved_data); i++) {
  391. saved_data[i] = (char)(i - 5);
  392. }
  393. printf ("%s iid %d: sam_data_store 5\n", __FUNCTION__, instance_id);
  394. err = sam_data_store (saved_data, sizeof (saved_data) - 7);
  395. if (err != CS_OK) {
  396. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  397. return 1;
  398. }
  399. exit (1);
  400. }
  401. if (instance_id == 2) {
  402. printf ("%s iid %d: sam_start\n", __FUNCTION__, instance_id);
  403. err = sam_start ();
  404. if (err != CS_OK) {
  405. fprintf (stderr, "Can't start hc. Error %d\n", err);
  406. return 1;
  407. }
  408. printf ("%s iid %d: sam_data_getsize 7\n", __FUNCTION__, instance_id);
  409. err = sam_data_getsize (&size);
  410. if (err != CS_OK) {
  411. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  412. return 1;
  413. }
  414. if (size != sizeof (saved_data2) - 7) {
  415. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  416. return 1;
  417. }
  418. printf ("%s iid %d: sam_data_restore 6\n", __FUNCTION__, instance_id);
  419. err = sam_data_restore (saved_data2, sizeof (saved_data2));
  420. if (err != CS_OK) {
  421. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  422. return 1;
  423. }
  424. for (i = 0; i < sizeof (saved_data); i++) {
  425. saved_data[i] = (char)(i - 5);
  426. }
  427. if (memcmp (saved_data, saved_data2, sizeof (saved_data2) - 7) != 0) {
  428. fprintf (stderr, "Retored data are not same\n");
  429. return 1;
  430. }
  431. printf ("%s iid %d: sam_data_store 6\n", __FUNCTION__, instance_id);
  432. err = sam_data_store (NULL, 0);
  433. if (err != CS_OK) {
  434. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  435. return 1;
  436. }
  437. exit (1);
  438. }
  439. if (instance_id == 3) {
  440. printf ("%s iid %d: sam_data_getsize 8\n", __FUNCTION__, instance_id);
  441. err = sam_data_getsize (&size);
  442. if (err != CS_OK) {
  443. fprintf (stderr, "Test should return CS_OK. Error returned %d\n", err);
  444. return 1;
  445. }
  446. if (size != 0) {
  447. fprintf (stderr, "Test should return size of 0. Returned %zx\n", size);
  448. return 1;
  449. }
  450. }
  451. return (0);
  452. }
  453. static int test5_hc_cb (void)
  454. {
  455. cs_error_t res;
  456. printf ("%s %d\n", __FUNCTION__, ++test5_hc_cb_count);
  457. res = sam_data_store (&test5_hc_cb_count, sizeof (test5_hc_cb_count));
  458. if (res != CS_OK)
  459. return 1;
  460. if (test5_hc_cb_count > 10)
  461. return 1;
  462. return 0;
  463. }
  464. /*
  465. * Test event driven healtchecking.
  466. */
  467. static int test5 (void)
  468. {
  469. cs_error_t error;
  470. unsigned int instance_id;
  471. int hc_cb_count;
  472. printf ("%s: initialize\n", __FUNCTION__);
  473. error = sam_initialize (100, SAM_RECOVERY_POLICY_RESTART);
  474. if (error != CS_OK) {
  475. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  476. return 1;
  477. }
  478. printf ("%s: register\n", __FUNCTION__);
  479. error = sam_register (&instance_id);
  480. if (error != CS_OK) {
  481. fprintf (stderr, "Can't register. Error %d\n", error);
  482. return 1;
  483. }
  484. if (instance_id == 1) {
  485. printf ("%s iid %d: hc callback register\n", __FUNCTION__, instance_id);
  486. error = sam_hc_callback_register (test5_hc_cb);
  487. if (error != CS_OK) {
  488. fprintf (stderr, "Can't register hc cb. Error %d\n", error);
  489. return 1;
  490. }
  491. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  492. error = sam_start ();
  493. if (error != CS_OK) {
  494. fprintf (stderr, "Can't start hc. Error %d\n", error);
  495. return 1;
  496. }
  497. sleep (2);
  498. printf ("%s iid %d: Failed. Wasn't killed.\n", __FUNCTION__, instance_id);
  499. return 1;
  500. }
  501. if (instance_id == 2) {
  502. error = sam_data_restore (&hc_cb_count, sizeof (hc_cb_count));
  503. if (error != CS_OK) {
  504. fprintf (stderr, "sam_data_restore should return CS_OK. Error returned %d\n", error);
  505. return 1;
  506. }
  507. if (hc_cb_count != 11) {
  508. fprintf (stderr, "%s iid %d: Premature killed. hc_cb_count should be 11 and it is %d\n",
  509. __FUNCTION__, instance_id - 1, hc_cb_count);
  510. return 1;
  511. }
  512. return 0;
  513. }
  514. return 1;
  515. }
  516. static void test6_signal (int sig) {
  517. cs_error_t error;
  518. printf ("%s\n", __FUNCTION__);
  519. test6_sig_delivered++;
  520. if ((error = sam_data_store (&test6_sig_delivered, sizeof (test6_sig_delivered))) != CS_OK) {
  521. fprintf (stderr, "Can't store data! Error : %d\n", error);
  522. }
  523. }
  524. /*
  525. * Test warn signal set.
  526. */
  527. static int test6 (void) {
  528. cs_error_t error;
  529. unsigned int instance_id;
  530. int test6_sig_del;
  531. printf ("%s: initialize\n", __FUNCTION__);
  532. error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART);
  533. if (error != CS_OK) {
  534. fprintf (stderr, "Can't initialize SAM API. Error %d\n", error);
  535. return 1;
  536. }
  537. printf ("%s: register\n", __FUNCTION__);
  538. error = sam_register (&instance_id);
  539. if (error != CS_OK) {
  540. fprintf (stderr, "Can't register. Error %d\n", error);
  541. return 1;
  542. }
  543. if (instance_id == 1) {
  544. error = sam_warn_signal_set (SIGUSR1);
  545. if (error != CS_OK) {
  546. fprintf (stderr, "Can't set warn signal. Error %d\n", error);
  547. return 1;
  548. }
  549. signal (SIGUSR1, test6_signal);
  550. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  551. error = sam_start ();
  552. if (error != CS_OK) {
  553. fprintf (stderr, "Can't start hc. Error %d\n", error);
  554. return 1;
  555. }
  556. printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  557. sleep (1);
  558. printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id);
  559. error = sam_hc_send ();
  560. if (error != CS_OK) {
  561. fprintf (stderr, "Can't send hc. Error %d\n", error);
  562. return 1;
  563. }
  564. printf ("%s iid %d: wait for delivery of signal\n", __FUNCTION__, instance_id);
  565. while (!test6_sig_delivered) {
  566. sleep (1);
  567. }
  568. printf ("%s iid %d: wait for real kill\n", __FUNCTION__, instance_id);
  569. sleep (3);
  570. printf ("%s iid %d: wasn't killed\n", __FUNCTION__, instance_id);
  571. return (1);
  572. }
  573. if (instance_id == 2) {
  574. error = sam_data_restore (&test6_sig_del, sizeof (test6_sig_del));
  575. if (error != CS_OK) {
  576. fprintf (stderr, "Can't restore data. Error %d\n", error);
  577. return 1;
  578. }
  579. if (test6_sig_del != 1) {
  580. fprintf (stderr, "Previous test failed. Signal was not delivered\n");
  581. return 1;
  582. }
  583. error = sam_warn_signal_set (SIGKILL);
  584. if (error != CS_OK) {
  585. fprintf (stderr, "Can't set warn signal. Error %d\n", error);
  586. return 1;
  587. }
  588. signal (SIGUSR1, test6_signal);
  589. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  590. error = sam_start ();
  591. if (error != CS_OK) {
  592. fprintf (stderr, "Can't start hc. Error %d\n", error);
  593. return 1;
  594. }
  595. printf ("%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
  596. sleep (1);
  597. printf ("%s iid %d: hc send\n", __FUNCTION__, instance_id);
  598. error = sam_hc_send ();
  599. if (error != CS_OK) {
  600. fprintf (stderr, "Can't send hc. Error %d\n", error);
  601. return 1;
  602. }
  603. printf ("%s iid %d: wait for delivery of signal\n", __FUNCTION__, instance_id);
  604. while (!test6_sig_delivered) {
  605. sleep (1);
  606. }
  607. printf ("%s iid %d: wasn't killed\n", __FUNCTION__, instance_id);
  608. return (1);
  609. }
  610. if (instance_id == 3) {
  611. error = sam_data_restore (&test6_sig_del, sizeof (test6_sig_del));
  612. if (error != CS_OK) {
  613. fprintf (stderr, "Can't restore data. Error %d\n", error);
  614. return 1;
  615. }
  616. if (test6_sig_del != 1) {
  617. fprintf (stderr, "Previous test failed. Signal WAS delivered\n");
  618. return 1;
  619. }
  620. return (0);
  621. }
  622. return 1;
  623. }
  624. static void *test7_thread (void *arg)
  625. {
  626. /* Wait 5s */
  627. sleep (5);
  628. exit (0);
  629. }
  630. /*
  631. * Test quorum
  632. */
  633. static int test7 (void) {
  634. confdb_handle_t cdb_handle;
  635. cs_error_t err;
  636. hdb_handle_t quorum_handle;
  637. size_t value_len;
  638. char key_value[256];
  639. unsigned int instance_id;
  640. pthread_t kill_thread;
  641. err = confdb_initialize (&cdb_handle, NULL);
  642. if (err != CS_OK) {
  643. printf ("Could not initialize Cluster Configuration Database API instance error %d. Test skipped\n", err);
  644. return (1);
  645. }
  646. err = confdb_object_find_start(cdb_handle, OBJECT_PARENT_HANDLE);
  647. if (err != CS_OK) {
  648. printf ("Could not start object_find %d. Test skipped\n", err);
  649. return (1);
  650. }
  651. err = confdb_object_find(cdb_handle, OBJECT_PARENT_HANDLE, "quorum", strlen("quorum"), &quorum_handle);
  652. if (err != CS_OK) {
  653. printf ("Could not object_find \"quorum\": %d. Test skipped\n", err);
  654. return (1);
  655. }
  656. err = confdb_key_get(cdb_handle, quorum_handle, "provider", strlen("provider"), key_value, &value_len);
  657. if (err != CS_OK) {
  658. printf ("Could not get \"provider\" key: %d. Test skipped\n", err);
  659. return (1);
  660. }
  661. if (!(value_len - 1 == strlen ("testquorum") && memcmp (key_value, "testquorum", value_len - 1) == 0)) {
  662. printf ("Provider is not testquorum. Test skipped\n");
  663. return (1);
  664. }
  665. /*
  666. * Set to not quorate
  667. */
  668. err = confdb_key_create(cdb_handle, quorum_handle, "quorate", strlen("quorate"), "0", strlen("0"));
  669. if (err != CS_OK) {
  670. printf ("Can't create confdb key. Error %d\n", err);
  671. return (2);
  672. }
  673. printf ("%s: initialize\n", __FUNCTION__);
  674. err = sam_initialize (2000, SAM_RECOVERY_POLICY_QUORUM_RESTART);
  675. if (err != CS_OK) {
  676. fprintf (stderr, "Can't initialize SAM API. Error %d\n", err);
  677. return 2;
  678. }
  679. printf ("%s: register\n", __FUNCTION__);
  680. err = sam_register (&instance_id);
  681. if (err != CS_OK) {
  682. fprintf (stderr, "Can't register. Error %d\n", err);
  683. return 2;
  684. }
  685. if (instance_id == 1) {
  686. /*
  687. * Sam start should block forever, but 10s for us should be enough
  688. */
  689. pthread_create (&kill_thread, NULL, test7_thread, NULL);
  690. printf ("%s iid %d: start - should block forever (waiting 5s)\n", __FUNCTION__, instance_id);
  691. err = sam_start ();
  692. if (err != CS_OK) {
  693. fprintf (stderr, "Can't start hc. Error %d\n", err);
  694. return 2;
  695. }
  696. printf ("%s iid %d: wasn't killed\n", __FUNCTION__, instance_id);
  697. return (2);
  698. }
  699. if (instance_id == 2) {
  700. /*
  701. * Set to quorate
  702. */
  703. err = confdb_key_create(cdb_handle, quorum_handle, "quorate", strlen("quorate"), "1", strlen("1"));
  704. if (err != CS_OK) {
  705. printf ("Can't create confdb key. Error %d\n", err);
  706. return (2);
  707. }
  708. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  709. err = sam_start ();
  710. if (err != CS_OK) {
  711. fprintf (stderr, "Can't start hc. Error %d\n", err);
  712. return 2;
  713. }
  714. /*
  715. * Set corosync unquorate
  716. */
  717. err = confdb_key_create(cdb_handle, quorum_handle, "quorate", strlen("quorate"), "0", strlen("0"));
  718. if (err != CS_OK) {
  719. printf ("Can't create confdb key. Error %d\n", err);
  720. return (2);
  721. }
  722. printf ("%s iid %d: sleep 3\n", __FUNCTION__, instance_id);
  723. sleep (3);
  724. printf ("%s iid %d: wasn't killed\n", __FUNCTION__, instance_id);
  725. return (2);
  726. }
  727. if (instance_id == 3) {
  728. return (0);
  729. }
  730. return (2);
  731. }
  732. /*
  733. * Test confdb integration + quit policy
  734. */
  735. static int test8 (pid_t pid, pid_t old_pid, int test_n) {
  736. confdb_handle_t cdb_handle;
  737. cs_error_t err;
  738. hdb_handle_t res_handle, proc_handle, pid_handle;
  739. size_t value_len;
  740. uint64_t tstamp1, tstamp2;
  741. int32_t msec_diff;
  742. char key_value[256];
  743. unsigned int instance_id;
  744. char tmp_obj[PATH_MAX];
  745. confdb_value_types_t cdbtype;
  746. err = confdb_initialize (&cdb_handle, NULL);
  747. if (err != CS_OK) {
  748. printf ("Could not initialize Cluster Configuration Database API instance error %d. Test skipped\n", err);
  749. return (1);
  750. }
  751. printf ("%s test %d\n", __FUNCTION__, test_n);
  752. if (test_n == 2) {
  753. /*
  754. * Object should not exist
  755. */
  756. printf ("%s Testing if object exists (it shouldn't)\n", __FUNCTION__);
  757. err = confdb_object_find_start(cdb_handle, OBJECT_PARENT_HANDLE);
  758. if (err != CS_OK) {
  759. printf ("Could not start object_find %d.\n", err);
  760. return (2);
  761. }
  762. err = confdb_object_find(cdb_handle, OBJECT_PARENT_HANDLE, "resources", strlen("resources"), &res_handle);
  763. if (err != CS_OK) {
  764. printf ("Could not object_find \"resources\": %d.\n", err);
  765. return (2);
  766. }
  767. err = confdb_object_find_start(cdb_handle, res_handle);
  768. if (err != CS_OK) {
  769. printf ("Could not start object_find %d.\n", err);
  770. return (2);
  771. }
  772. err = confdb_object_find(cdb_handle, res_handle, "process", strlen("process"), &proc_handle);
  773. if (err != CS_OK) {
  774. printf ("Could not object_find \"process\": %d.\n", err);
  775. return (2);
  776. }
  777. if (snprintf (tmp_obj, sizeof (tmp_obj), "%s:%d", __progname, pid) >= sizeof (tmp_obj)) {
  778. snprintf (tmp_obj, sizeof (tmp_obj), "%d", pid);
  779. }
  780. err = confdb_object_find_start(cdb_handle, proc_handle);
  781. if (err != CS_OK) {
  782. printf ("Could not start object_find %d.\n", err);
  783. return (2);
  784. }
  785. err = confdb_object_find(cdb_handle, proc_handle, tmp_obj, strlen(tmp_obj), &pid_handle);
  786. if (err == CS_OK) {
  787. printf ("Could find object \"%s\": %d.\n", tmp_obj, err);
  788. return (2);
  789. }
  790. }
  791. if (test_n == 1 || test_n == 2) {
  792. printf ("%s: initialize\n", __FUNCTION__);
  793. err = sam_initialize (2000, SAM_RECOVERY_POLICY_QUIT | SAM_RECOVERY_POLICY_CONFDB);
  794. if (err != CS_OK) {
  795. fprintf (stderr, "Can't initialize SAM API. Error %d\n", err);
  796. return 2;
  797. }
  798. printf ("%s: register\n", __FUNCTION__);
  799. err = sam_register (&instance_id);
  800. if (err != CS_OK) {
  801. fprintf (stderr, "Can't register. Error %d\n", err);
  802. return 2;
  803. }
  804. err = confdb_object_find_start(cdb_handle, OBJECT_PARENT_HANDLE);
  805. if (err != CS_OK) {
  806. printf ("Could not start object_find %d.\n", err);
  807. return (2);
  808. }
  809. err = confdb_object_find(cdb_handle, OBJECT_PARENT_HANDLE, "resources", strlen("resources"), &res_handle);
  810. if (err != CS_OK) {
  811. printf ("Could not object_find \"resources\": %d.\n", err);
  812. return (2);
  813. }
  814. err = confdb_object_find_start(cdb_handle, res_handle);
  815. if (err != CS_OK) {
  816. printf ("Could not start object_find %d.\n", err);
  817. return (2);
  818. }
  819. err = confdb_object_find(cdb_handle, res_handle, "process", strlen("process"), &proc_handle);
  820. if (err != CS_OK) {
  821. printf ("Could not object_find \"process\": %d.\n", err);
  822. return (2);
  823. }
  824. if (snprintf (tmp_obj, sizeof (tmp_obj), "%s:%d", __progname, pid) >= sizeof (tmp_obj)) {
  825. snprintf (tmp_obj, sizeof (tmp_obj), "%d", pid);
  826. }
  827. err = confdb_object_find_start(cdb_handle, proc_handle);
  828. if (err != CS_OK) {
  829. printf ("Could not start object_find %d.\n", err);
  830. return (2);
  831. }
  832. err = confdb_object_find(cdb_handle, proc_handle, tmp_obj, strlen(tmp_obj), &pid_handle);
  833. if (err != CS_OK) {
  834. printf ("Could not object_find \"%s\": %d.\n", tmp_obj, err);
  835. return (2);
  836. }
  837. err = confdb_key_get(cdb_handle, pid_handle, "recovery", strlen("recovery"), key_value, &value_len);
  838. if (err != CS_OK) {
  839. printf ("Could not get \"recovery\" key: %d.\n", err);
  840. return (2);
  841. }
  842. if (value_len != strlen ("quit") || memcmp (key_value, "quit", value_len) != 0) {
  843. printf ("Recovery key \"%s\" is not \"watchdog\".\n", key_value);
  844. return (2);
  845. }
  846. err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len);
  847. if (err != CS_OK) {
  848. printf ("Could not get \"state\" key: %d.\n", err);
  849. return (2);
  850. }
  851. if (value_len != strlen ("stopped") || memcmp (key_value, "stopped", value_len) != 0) {
  852. printf ("State key is not \"stopped\".\n");
  853. return (2);
  854. }
  855. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  856. err = sam_start ();
  857. if (err != CS_OK) {
  858. fprintf (stderr, "Can't start hc. Error %d\n", err);
  859. return 2;
  860. }
  861. err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len);
  862. if (err != CS_OK) {
  863. printf ("Could not get \"state\" key: %d.\n", err);
  864. return (2);
  865. }
  866. if (value_len != strlen ("running") || memcmp (key_value, "running", value_len) != 0) {
  867. printf ("State key is not \"running\".\n");
  868. return (2);
  869. }
  870. printf ("%s iid %d: stop\n", __FUNCTION__, instance_id);
  871. err = sam_stop ();
  872. if (err != CS_OK) {
  873. fprintf (stderr, "Can't stop hc. Error %d\n", err);
  874. return 2;
  875. }
  876. err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len);
  877. if (err != CS_OK) {
  878. printf ("Could not get \"state\" key: %d.\n", err);
  879. return (2);
  880. }
  881. if (value_len != strlen ("stopped") || memcmp (key_value, "stopped", value_len) != 0) {
  882. printf ("State key is not \"stopped\".\n");
  883. return (2);
  884. }
  885. printf ("%s iid %d: sleeping 5\n", __FUNCTION__, instance_id);
  886. sleep (5);
  887. err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len);
  888. if (err != CS_OK) {
  889. printf ("Could not get \"state\" key: %d.\n", err);
  890. return (2);
  891. }
  892. if (value_len != strlen ("stopped") || memcmp (key_value, "stopped", value_len) != 0) {
  893. printf ("State key is not \"stopped\".\n");
  894. return (2);
  895. }
  896. printf ("%s iid %d: start 2\n", __FUNCTION__, instance_id);
  897. err = sam_start ();
  898. if (err != CS_OK) {
  899. fprintf (stderr, "Can't start hc. Error %d\n", err);
  900. return 2;
  901. }
  902. err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len);
  903. if (err != CS_OK) {
  904. printf ("Could not get \"state\" key: %d.\n", err);
  905. return (2);
  906. }
  907. if (value_len != strlen ("running") || memcmp (key_value, "running", value_len) != 0) {
  908. printf ("State key is not \"running\".\n");
  909. return (2);
  910. }
  911. if (test_n == 2) {
  912. printf ("%s iid %d: sleeping 5. Should be killed\n", __FUNCTION__, instance_id);
  913. sleep (5);
  914. return (2);
  915. } else {
  916. printf ("%s iid %d: Test HC\n", __FUNCTION__, instance_id);
  917. err = sam_hc_send ();
  918. if (err != CS_OK) {
  919. fprintf (stderr, "Can't send hc. Error %d\n", err);
  920. return 2;
  921. }
  922. err = confdb_key_get_typed (cdb_handle, pid_handle, "last_updated", &tstamp1, &value_len, &cdbtype);
  923. if (err != CS_OK) {
  924. printf ("Could not get \"state\" key: %d.\n", err);
  925. return (2);
  926. }
  927. printf ("%s iid %d: Sleep 1\n", __FUNCTION__, instance_id);
  928. sleep (1);
  929. err = sam_hc_send ();
  930. if (err != CS_OK) {
  931. fprintf (stderr, "Can't send hc. Error %d\n", err);
  932. return 2;
  933. }
  934. sleep (1);
  935. err = confdb_key_get_typed (cdb_handle, pid_handle, "last_updated", &tstamp2, &value_len, &cdbtype);
  936. if (err != CS_OK) {
  937. printf ("Could not get \"state\" key: %d.\n", err);
  938. return (2);
  939. }
  940. msec_diff = (tstamp2 - tstamp1)/CS_TIME_NS_IN_MSEC;
  941. if (msec_diff < 500 || msec_diff > 2000) {
  942. printf ("Difference %d is not within <500, 2000> interval.\n", msec_diff);
  943. return (2);
  944. }
  945. printf ("%s iid %d: stop 2\n", __FUNCTION__, instance_id);
  946. err = sam_stop ();
  947. if (err != CS_OK) {
  948. fprintf (stderr, "Can't stop hc. Error %d\n", err);
  949. return 2;
  950. }
  951. err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len);
  952. if (err != CS_OK) {
  953. printf ("Could not get \"state\" key: %d.\n", err);
  954. return (2);
  955. }
  956. if (value_len != strlen ("stopped") || memcmp (key_value, "stopped", value_len) != 0) {
  957. printf ("State key is not \"stopped\".\n");
  958. return (2);
  959. }
  960. printf ("%s iid %d: exiting\n", __FUNCTION__, instance_id);
  961. return (0);
  962. }
  963. }
  964. if (test_n == 3) {
  965. printf ("%s Testing if status is failed\n", __FUNCTION__);
  966. /*
  967. * Previous should be FAILED
  968. */
  969. err = confdb_object_find_start(cdb_handle, OBJECT_PARENT_HANDLE);
  970. if (err != CS_OK) {
  971. printf ("Could not start object_find %d.\n", err);
  972. return (2);
  973. }
  974. err = confdb_object_find(cdb_handle, OBJECT_PARENT_HANDLE, "resources", strlen("resources"), &res_handle);
  975. if (err != CS_OK) {
  976. printf ("Could not object_find \"resources\": %d.\n", err);
  977. return (2);
  978. }
  979. err = confdb_object_find_start(cdb_handle, res_handle);
  980. if (err != CS_OK) {
  981. printf ("Could not start object_find %d.\n", err);
  982. return (2);
  983. }
  984. err = confdb_object_find(cdb_handle, res_handle, "process", strlen("process"), &proc_handle);
  985. if (err != CS_OK) {
  986. printf ("Could not object_find \"process\": %d.\n", err);
  987. return (2);
  988. }
  989. if (snprintf (tmp_obj, sizeof (tmp_obj), "%s:%d", __progname, pid) >= sizeof (tmp_obj)) {
  990. snprintf (tmp_obj, sizeof (tmp_obj), "%d", pid);
  991. }
  992. err = confdb_object_find_start(cdb_handle, proc_handle);
  993. if (err != CS_OK) {
  994. printf ("Could not start object_find %d.\n", err);
  995. return (2);
  996. }
  997. err = confdb_object_find(cdb_handle, proc_handle, tmp_obj, strlen(tmp_obj), &pid_handle);
  998. if (err != CS_OK) {
  999. printf ("Could not object_find \"%s\": %d.\n", tmp_obj, err);
  1000. return (2);
  1001. }
  1002. err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len);
  1003. if (err != CS_OK) {
  1004. printf ("Could not get \"state\" key: %d.\n", err);
  1005. return (2);
  1006. }
  1007. if (value_len != strlen ("failed") || memcmp (key_value, "failed", value_len) != 0) {
  1008. printf ("State key is not \"failed\".\n");
  1009. return (2);
  1010. }
  1011. return (0);
  1012. }
  1013. return (2);
  1014. }
  1015. /*
  1016. * Test confdb integration + restart policy
  1017. */
  1018. static int test9 (pid_t pid, pid_t old_pid, int test_n) {
  1019. confdb_handle_t cdb_handle;
  1020. cs_error_t err;
  1021. hdb_handle_t res_handle, proc_handle, pid_handle;
  1022. size_t value_len;
  1023. char key_value[256];
  1024. unsigned int instance_id;
  1025. char tmp_obj[PATH_MAX];
  1026. err = confdb_initialize (&cdb_handle, NULL);
  1027. if (err != CS_OK) {
  1028. printf ("Could not initialize Cluster Configuration Database API instance error %d. Test skipped\n", err);
  1029. return (1);
  1030. }
  1031. printf ("%s test %d\n", __FUNCTION__, test_n);
  1032. if (test_n == 1) {
  1033. printf ("%s: initialize\n", __FUNCTION__);
  1034. err = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART | SAM_RECOVERY_POLICY_CONFDB);
  1035. if (err != CS_OK) {
  1036. fprintf (stderr, "Can't initialize SAM API. Error %d\n", err);
  1037. return 2;
  1038. }
  1039. printf ("%s: register\n", __FUNCTION__);
  1040. err = sam_register (&instance_id);
  1041. if (err != CS_OK) {
  1042. fprintf (stderr, "Can't register. Error %d\n", err);
  1043. return 2;
  1044. }
  1045. printf ("%s: iid %d\n", __FUNCTION__, instance_id);
  1046. if (instance_id < 3) {
  1047. err = confdb_object_find_start(cdb_handle, OBJECT_PARENT_HANDLE);
  1048. if (err != CS_OK) {
  1049. printf ("Could not start object_find %d.\n", err);
  1050. return (2);
  1051. }
  1052. err = confdb_object_find(cdb_handle, OBJECT_PARENT_HANDLE, "resources", strlen("resources"),
  1053. &res_handle);
  1054. if (err != CS_OK) {
  1055. printf ("Could not object_find \"resources\": %d.\n", err);
  1056. return (2);
  1057. }
  1058. err = confdb_object_find_start(cdb_handle, res_handle);
  1059. if (err != CS_OK) {
  1060. printf ("Could not start object_find %d.\n", err);
  1061. return (2);
  1062. }
  1063. err = confdb_object_find(cdb_handle, res_handle, "process", strlen("process"), &proc_handle);
  1064. if (err != CS_OK) {
  1065. printf ("Could not object_find \"process\": %d.\n", err);
  1066. return (2);
  1067. }
  1068. if (snprintf (tmp_obj, sizeof (tmp_obj), "%s:%d", __progname, pid) >= sizeof (tmp_obj)) {
  1069. snprintf (tmp_obj, sizeof (tmp_obj), "%d", pid);
  1070. }
  1071. err = confdb_object_find_start(cdb_handle, proc_handle);
  1072. if (err != CS_OK) {
  1073. printf ("Could not start object_find %d.\n", err);
  1074. return (2);
  1075. }
  1076. err = confdb_object_find(cdb_handle, proc_handle, tmp_obj, strlen(tmp_obj), &pid_handle);
  1077. if (err != CS_OK) {
  1078. printf ("Could not object_find \"%s\": %d.\n", tmp_obj, err);
  1079. return (2);
  1080. }
  1081. err = confdb_key_get(cdb_handle, pid_handle, "recovery", strlen("recovery"), key_value, &value_len);
  1082. if (err != CS_OK) {
  1083. printf ("Could not get \"recovery\" key: %d.\n", err);
  1084. return (2);
  1085. }
  1086. if (value_len != strlen ("restart") || memcmp (key_value, "restart", value_len) != 0) {
  1087. printf ("Recovery key \"%s\" is not \"restart\".\n", key_value);
  1088. return (2);
  1089. }
  1090. err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len);
  1091. if (err != CS_OK) {
  1092. printf ("Could not get \"state\" key: %d.\n", err);
  1093. return (2);
  1094. }
  1095. if (value_len != strlen ("stopped") || memcmp (key_value, "stopped", value_len) != 0) {
  1096. printf ("State key is not \"stopped\".\n");
  1097. return (2);
  1098. }
  1099. printf ("%s iid %d: start\n", __FUNCTION__, instance_id);
  1100. err = sam_start ();
  1101. if (err != CS_OK) {
  1102. fprintf (stderr, "Can't start hc. Error %d\n", err);
  1103. return 2;
  1104. }
  1105. err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len);
  1106. if (err != CS_OK) {
  1107. printf ("Could not get \"state\" key: %d.\n", err);
  1108. return (2);
  1109. }
  1110. if (value_len != strlen ("running") || memcmp (key_value, "running", value_len) != 0) {
  1111. printf ("State key is not \"running\".\n");
  1112. return (2);
  1113. }
  1114. printf ("%s iid %d: waiting for kill\n", __FUNCTION__, instance_id);
  1115. sleep (10);
  1116. return (2);
  1117. }
  1118. if (instance_id == 3) {
  1119. printf ("%s iid %d: mark failed\n", __FUNCTION__, instance_id);
  1120. if (err != CS_OK) {
  1121. fprintf (stderr, "Can't start hc. Error %d\n", err);
  1122. return 2;
  1123. }
  1124. err = sam_mark_failed ();
  1125. if (err != CS_OK) {
  1126. fprintf (stderr, "Can't mark failed. Error %d\n", err);
  1127. return 2;
  1128. }
  1129. sleep (10);
  1130. return (2);
  1131. }
  1132. return (2);
  1133. }
  1134. if (test_n == 2) {
  1135. printf ("%s Testing if status is failed\n", __FUNCTION__);
  1136. /*
  1137. * Previous should be FAILED
  1138. */
  1139. err = confdb_object_find_start(cdb_handle, OBJECT_PARENT_HANDLE);
  1140. if (err != CS_OK) {
  1141. printf ("Could not start object_find %d.\n", err);
  1142. return (2);
  1143. }
  1144. err = confdb_object_find(cdb_handle, OBJECT_PARENT_HANDLE, "resources", strlen("resources"), &res_handle);
  1145. if (err != CS_OK) {
  1146. printf ("Could not object_find \"resources\": %d.\n", err);
  1147. return (2);
  1148. }
  1149. err = confdb_object_find_start(cdb_handle, res_handle);
  1150. if (err != CS_OK) {
  1151. printf ("Could not start object_find %d.\n", err);
  1152. return (2);
  1153. }
  1154. err = confdb_object_find(cdb_handle, res_handle, "process", strlen("process"), &proc_handle);
  1155. if (err != CS_OK) {
  1156. printf ("Could not object_find \"process\": %d.\n", err);
  1157. return (2);
  1158. }
  1159. if (snprintf (tmp_obj, sizeof (tmp_obj), "%s:%d", __progname, pid) >= sizeof (tmp_obj)) {
  1160. snprintf (tmp_obj, sizeof (tmp_obj), "%d", pid);
  1161. }
  1162. err = confdb_object_find_start(cdb_handle, proc_handle);
  1163. if (err != CS_OK) {
  1164. printf ("Could not start object_find %d.\n", err);
  1165. return (2);
  1166. }
  1167. err = confdb_object_find(cdb_handle, proc_handle, tmp_obj, strlen(tmp_obj), &pid_handle);
  1168. if (err != CS_OK) {
  1169. printf ("Could not object_find \"%s\": %d.\n", tmp_obj, err);
  1170. return (2);
  1171. }
  1172. err = confdb_key_get(cdb_handle, pid_handle, "state", strlen("state"), key_value, &value_len);
  1173. if (err != CS_OK) {
  1174. printf ("Could not get \"state\" key: %d.\n", err);
  1175. return (2);
  1176. }
  1177. if (value_len != strlen ("failed") || memcmp (key_value, "failed", value_len) != 0) {
  1178. printf ("State key is not \"failed\".\n");
  1179. return (2);
  1180. }
  1181. return (0);
  1182. }
  1183. return (2);
  1184. }
  1185. int main(int argc, char *argv[])
  1186. {
  1187. pid_t pid, old_pid;
  1188. int err;
  1189. int stat;
  1190. int all_passed = 1;
  1191. int no_skipped = 0;
  1192. pid = fork ();
  1193. if (pid == -1) {
  1194. fprintf (stderr, "Can't fork\n");
  1195. return 1;
  1196. }
  1197. if (pid == 0) {
  1198. err = test1 ();
  1199. sam_finalize ();
  1200. return err;
  1201. }
  1202. waitpid (pid, &stat, 0);
  1203. fprintf (stderr, "test1 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  1204. if (WEXITSTATUS (stat) != 0)
  1205. all_passed = 0;
  1206. pid = fork ();
  1207. if (pid == -1) {
  1208. fprintf (stderr, "Can't fork\n");
  1209. return 1;
  1210. }
  1211. if (pid == 0) {
  1212. err = test2 ();
  1213. sam_finalize ();
  1214. return (err);
  1215. }
  1216. waitpid (pid, &stat, 0);
  1217. fprintf (stderr, "test2 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  1218. if (WEXITSTATUS (stat) != 0)
  1219. all_passed = 0;
  1220. pid = fork ();
  1221. if (pid == -1) {
  1222. fprintf (stderr, "Can't fork\n");
  1223. return 1;
  1224. }
  1225. if (pid == 0) {
  1226. err = test3 ();
  1227. sam_finalize ();
  1228. return (err);
  1229. }
  1230. waitpid (pid, &stat, 0);
  1231. fprintf (stderr, "test3 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  1232. if (WEXITSTATUS (stat) != 0)
  1233. all_passed = 0;
  1234. pid = fork ();
  1235. if (pid == -1) {
  1236. fprintf (stderr, "Can't fork\n");
  1237. return 1;
  1238. }
  1239. if (pid == 0) {
  1240. err = test4 ();
  1241. sam_finalize ();
  1242. return (err);
  1243. }
  1244. waitpid (pid, &stat, 0);
  1245. fprintf (stderr, "test4 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  1246. if (WEXITSTATUS (stat) != 0)
  1247. all_passed = 0;
  1248. pid = fork ();
  1249. if (pid == -1) {
  1250. fprintf (stderr, "Can't fork\n");
  1251. return 1;
  1252. }
  1253. if (pid == 0) {
  1254. err = test5 ();
  1255. sam_finalize ();
  1256. return (err);
  1257. }
  1258. waitpid (pid, &stat, 0);
  1259. fprintf (stderr, "test5 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  1260. if (WEXITSTATUS (stat) != 0)
  1261. all_passed = 0;
  1262. pid = fork ();
  1263. if (pid == -1) {
  1264. fprintf (stderr, "Can't fork\n");
  1265. return 1;
  1266. }
  1267. if (pid == 0) {
  1268. err = test6 ();
  1269. sam_finalize ();
  1270. return (err);
  1271. }
  1272. waitpid (pid, &stat, 0);
  1273. fprintf (stderr, "test6 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : "failed"));
  1274. if (WEXITSTATUS (stat) != 0)
  1275. all_passed = 0;
  1276. pid = fork ();
  1277. if (pid == -1) {
  1278. fprintf (stderr, "Can't fork\n");
  1279. return 2;
  1280. }
  1281. if (pid == 0) {
  1282. err = test7 ();
  1283. sam_finalize ();
  1284. return (err);
  1285. }
  1286. waitpid (pid, &stat, 0);
  1287. fprintf (stderr, "test7 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : (WEXITSTATUS (stat) == 1 ? "skipped" : "failed")));
  1288. if (WEXITSTATUS (stat) == 1)
  1289. no_skipped++;
  1290. if (WEXITSTATUS (stat) > 1)
  1291. all_passed = 0;
  1292. pid = fork ();
  1293. if (pid == -1) {
  1294. fprintf (stderr, "Can't fork\n");
  1295. return 2;
  1296. }
  1297. if (pid == 0) {
  1298. err = test8 (getpid (), 0, 1);
  1299. sam_finalize ();
  1300. return (err);
  1301. }
  1302. waitpid (pid, &stat, 0);
  1303. old_pid = pid;
  1304. if (WEXITSTATUS (stat) == 0) {
  1305. pid = fork ();
  1306. if (pid == -1) {
  1307. fprintf (stderr, "Can't fork\n");
  1308. return 2;
  1309. }
  1310. if (pid == 0) {
  1311. err = test8 (getpid (), old_pid, 2);
  1312. sam_finalize ();
  1313. return (err);
  1314. }
  1315. waitpid (pid, &stat, 0);
  1316. old_pid = pid;
  1317. if (WEXITSTATUS (stat) == 0) {
  1318. pid = fork ();
  1319. if (pid == -1) {
  1320. fprintf (stderr, "Can't fork\n");
  1321. return 2;
  1322. }
  1323. if (pid == 0) {
  1324. err = test8 (old_pid, 0, 3);
  1325. sam_finalize ();
  1326. return (err);
  1327. }
  1328. waitpid (pid, &stat, 0);
  1329. }
  1330. }
  1331. if (WEXITSTATUS (stat) == 1)
  1332. no_skipped++;
  1333. if (WEXITSTATUS (stat) > 1)
  1334. all_passed = 0;
  1335. pid = fork ();
  1336. if (pid == -1) {
  1337. fprintf (stderr, "Can't fork\n");
  1338. return 2;
  1339. }
  1340. if (pid == 0) {
  1341. err = test9 (getpid (), 0, 1);
  1342. sam_finalize ();
  1343. return (err);
  1344. }
  1345. waitpid (pid, &stat, 0);
  1346. old_pid = pid;
  1347. if (WEXITSTATUS (stat) == 0) {
  1348. pid = fork ();
  1349. if (pid == -1) {
  1350. fprintf (stderr, "Can't fork\n");
  1351. return 2;
  1352. }
  1353. if (pid == 0) {
  1354. err = test9 (old_pid, 0, 2);
  1355. sam_finalize ();
  1356. return (err);
  1357. }
  1358. waitpid (pid, &stat, 0);
  1359. }
  1360. fprintf (stderr, "test9 %s\n", (WEXITSTATUS (stat) == 0 ? "passed" : (WEXITSTATUS (stat) == 1 ? "skipped" : "failed")));
  1361. if (WEXITSTATUS (stat) == 1)
  1362. no_skipped++;
  1363. if (WEXITSTATUS (stat) > 1)
  1364. all_passed = 0;
  1365. if (all_passed)
  1366. fprintf (stderr, "All tests passed (%d skipped)\n", no_skipped);
  1367. return (all_passed ? 0 : 1);
  1368. }