testsam.c 42 KB

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