testsam.c 42 KB

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