testamf1.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. * Copyright (c) 2002-2003 MontaVista Software, Inc.
  3. * Copyright (c) 2006 Ericsson AB.
  4. * Copyright (c) 2006 Sun Microsystems, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Steven Dake (sdake@mvista.com)
  9. * Hans Feldt
  10. *
  11. * This software licensed under BSD license, the text of which follows:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * - Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  22. * contributors may be used to endorse or promote products derived from this
  23. * software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  35. * THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <errno.h>
  41. #include <signal.h>
  42. #include <unistd.h>
  43. #include <sys/types.h>
  44. #include <sys/socket.h>
  45. #include <sys/un.h>
  46. #include <sched.h>
  47. #include <sys/stat.h>
  48. #include <fcntl.h>
  49. #include <stdarg.h>
  50. #include "saAis.h"
  51. #include "saAmf.h"
  52. SaAmfHandleT handle;
  53. SaAmfHealthcheckKeyT key0 = {
  54. .key = "key1",
  55. .keyLen = 4
  56. };
  57. SaNameT compNameGlobal;
  58. int good_health = 0;
  59. int good_health_limit = 0;
  60. enum {
  61. FINALIZE = 0,
  62. UNREGISTER,
  63. ERROR_REPORT
  64. };
  65. #define die(format, args...) _die (__FILE__, __LINE__, format, ##args)
  66. static void _die (char *file, int line, char *format, ...) __attribute__((format(printf, 3, 4)));
  67. static void _die (char *file, int line, char *format, ...)
  68. {
  69. char buf[1024];
  70. va_list ap;
  71. sprintf (buf, "%d - %s:#%d - Error: '%s', exiting...\n",
  72. getpid(), file, line, format);
  73. va_start (ap, format);
  74. vfprintf (stderr, buf, ap);
  75. va_end(ap);
  76. exit (-1);
  77. }
  78. static void response (
  79. SaAmfHandleT handle, SaInvocationT invocation, SaAisErrorT error)
  80. {
  81. SaAisErrorT result;
  82. do {
  83. result = saAmfResponse (handle, invocation, error);
  84. if (result == SA_AIS_ERR_TRY_AGAIN) {
  85. fprintf(stderr, "%d: TRY_AGAIN received\n", getpid());
  86. usleep (100000);
  87. }
  88. } while (result == SA_AIS_ERR_TRY_AGAIN);
  89. if (result != SA_AIS_OK) {
  90. die ("saAmfResponse failed %d", result);
  91. }
  92. }
  93. void printSaNameT (SaNameT *name)
  94. {
  95. int i;
  96. for (i = 0; i < name->length; i++) {
  97. printf ("%c", name->value[i]);
  98. }
  99. }
  100. void setSanameT (SaNameT *name, char *str) {
  101. name->length = strlen (str);
  102. memcpy (name->value, str, name->length);
  103. }
  104. static unsigned int healthcheck_no = 0;
  105. int stop = 0;
  106. void HealthcheckCallback (SaInvocationT invocation,
  107. const SaNameT *compName,
  108. SaAmfHealthcheckKeyT *healthcheckKey)
  109. {
  110. SaAisErrorT res;
  111. if( !good_health && healthcheck_no++);
  112. if (healthcheck_no == good_health_limit ) {
  113. response (handle, invocation, SA_AIS_OK);
  114. res = saAmfHealthcheckStop (handle,
  115. &compNameGlobal,
  116. &key0);
  117. printf ("healthcheck stop result %d (should be %d)\n", res, SA_AIS_OK);
  118. printf ("COMPONENT REPORTING ERROR %s\n", compNameGlobal.value);
  119. saAmfComponentErrorReport (handle, compName, 0, SA_AMF_COMPONENT_RESTART, 0);
  120. printf ("COMPONENT DONE REPORTING ERROR\n");
  121. } else {
  122. response (handle, invocation, SA_AIS_OK);
  123. }
  124. }
  125. void ComponentTerminateCallback (
  126. SaInvocationT invocation,
  127. const SaNameT *compName)
  128. {
  129. printf ("ComponentTerminateCallback\n");
  130. response (handle, invocation, SA_AIS_OK);
  131. exit (0);
  132. }
  133. #if 0
  134. #include <sys/time.h>
  135. #define TRU "%d"
  136. #define TRS "%s"
  137. #define TR(format,x) do { \
  138. struct timeval t;\
  139. gettimeofday(&t,NULL); \
  140. printf("%s:%d: %s : %d : %u: %u :%s : " format "\n",\
  141. __FILE__,__LINE__,__FUNCTION__, \
  142. (int)getpid(),(int)t.tv_sec, (int)t.tv_usec,#x,x); \
  143. }while(0)
  144. #else
  145. #define TRU "%d"
  146. #define TRS "%s"
  147. #define TR(format,x)
  148. #endif
  149. void CSISetCallback (
  150. SaInvocationT invocation,
  151. const SaNameT *compName,
  152. SaAmfHAStateT haState,
  153. SaAmfCSIDescriptorT *csiDescriptor)
  154. {
  155. SaAmfHAStateT state;
  156. int res;
  157. int i;
  158. switch (haState) {
  159. case SA_AMF_HA_ACTIVE:
  160. printf ("%d: Component '%s' requested to enter hastate SA_AMF_ACTIVE"
  161. " for \n\tCSI '%s'\n",
  162. (int)getpid(), compName->value, csiDescriptor->csiName.value);
  163. response (handle, invocation, SA_AIS_OK);
  164. res = saAmfHAStateGet (handle, compName, &csiDescriptor->csiName, &state);
  165. if (res != SA_AIS_OK || haState != state) {
  166. fprintf (stderr, "saAmfHAStateGet failed: %d\n", res);
  167. exit (-1);
  168. }
  169. TR(TRU, csiDescriptor->csiAttr.number);
  170. for(i=0; i<csiDescriptor->csiAttr.number; i++) {
  171. if( strcmp((char*)csiDescriptor->csiAttr.attr[i].attrName, "good_health_limit") == 0){
  172. good_health = strcmp((char*)csiDescriptor->csiAttr.attr[i].attrValue, "0") ? 0 : 1;
  173. good_health_limit = atoi((char*)csiDescriptor->csiAttr.attr[i].attrValue);
  174. }
  175. #if 0
  176. TR(TRS,csiDescriptor->csiAttr.attr[i].attrName);
  177. TR(TRS, csiDescriptor->csiAttr.attr[i].attrValue);
  178. #endif
  179. }
  180. TR(TRU, csiDescriptor->csiFlags);
  181. printSaNameT((SaNameT*) &csiDescriptor->csiStateDescriptor.activeDescriptor.activeCompName);
  182. TR(TRU, csiDescriptor->csiStateDescriptor.activeDescriptor.transitionDescriptor);
  183. break;
  184. case SA_AMF_HA_STANDBY:
  185. printf ("%d: Component '%s' requested to enter hastate SA_AMF_STANDBY "
  186. "for \n\tCSI '%s'\n",
  187. (int)getpid(), compName->value, csiDescriptor->csiName.value);
  188. response (handle, invocation, SA_AIS_OK);
  189. TR(TRU,csiDescriptor->csiAttr.number);
  190. for(i=0; i<csiDescriptor->csiAttr.number; i++) {
  191. if(!strcmp((char*)csiDescriptor->csiAttr.attr[i].attrName, "good_health") &&
  192. !strcmp((char*)csiDescriptor->csiAttr.attr[i].attrValue, "true")){
  193. good_health = 1;
  194. }
  195. TR(TRS,csiDescriptor->csiAttr.attr[i].attrName);
  196. TR(TRS,csiDescriptor->csiAttr.attr[i].attrValue);
  197. }
  198. TR(TRU,csiDescriptor->csiFlags);
  199. printSaNameT((SaNameT*) &csiDescriptor->csiStateDescriptor.standbyDescriptor.activeCompName);
  200. TR(TRU,csiDescriptor->csiStateDescriptor.standbyDescriptor.standbyRank);
  201. break;
  202. case SA_AMF_HA_QUIESCED:
  203. printf ("%d: Component '%s' requested to enter hastate SA_AMF_HA_QUIESCED "
  204. "for \n\tCSI '%s'\n",
  205. (int)getpid(), compName->value, csiDescriptor->csiName.value);
  206. response (handle, invocation, SA_AIS_OK);
  207. break;
  208. case SA_AMF_HA_QUIESCING:
  209. break;
  210. default:
  211. break;
  212. }
  213. }
  214. void CSIRemoveCallback (
  215. SaInvocationT invocation,
  216. const SaNameT *compName,
  217. const SaNameT *csiName,
  218. SaAmfCSIFlagsT csiFlags)
  219. {
  220. printf ("CSIRemoveCallback for component '");
  221. printSaNameT ((SaNameT *)compName);
  222. printf ("' in CSI '");
  223. printSaNameT ((SaNameT *)csiName);
  224. printf ("'\n");
  225. response (handle, invocation, SA_AIS_OK);
  226. }
  227. #ifdef COMPILE_OUT
  228. void ProtectionGroupTrackCallback (
  229. const SaNameT *csiName,
  230. SaAmfProtectionGroupNotificationT *notificationBuffer,
  231. SaUint32T numberOfItems,
  232. SaUint32T numberOfMembers,
  233. SaAisErrorT error)
  234. {
  235. int i;
  236. printf ("ProtectionGroupTrackCallback items %d members %d\n", (int)numberOfItems, (int)numberOfMembers);
  237. printf ("buffer is %p\n", notificationBuffer);
  238. for (i = 0; i < numberOfItems; i++) {
  239. printf ("component name");
  240. printSaNameT (&notificationBuffer[i].member.compName);
  241. printf ("\n");
  242. printf ("\treadiness state is %d\n", notificationBuffer[i].member.readinessState);
  243. printf ("\thastate %d\n", notificationBuffer[i].member.haState);
  244. printf ("\tchange is %d\n", notificationBuffer[i].change);
  245. }
  246. }
  247. #endif
  248. SaAmfCallbacksT amfCallbacks = {
  249. .saAmfHealthcheckCallback = HealthcheckCallback,
  250. .saAmfComponentTerminateCallback = ComponentTerminateCallback,
  251. .saAmfCSISetCallback = CSISetCallback,
  252. .saAmfCSIRemoveCallback = CSIRemoveCallback,
  253. };
  254. SaAmfCallbacksT amfCallbacks;
  255. SaVersionT version = { 'B', 1, 1 };
  256. #if ! defined(TS_CLASS) && (defined(OPENAIS_BSD) || defined(OPENAIS_LINUX) || defined(OPENAIS_SOLARIS))
  257. static struct sched_param sched_param;
  258. #endif
  259. void sigintr_handler (int signum) {
  260. stop = FINALIZE;
  261. }
  262. void sigusr1_handler (int signum) {
  263. stop = UNREGISTER;
  264. }
  265. void sigusr2_handler (int signum) {
  266. stop = ERROR_REPORT;
  267. }
  268. void write_pid (void) {
  269. char pid[256];
  270. char filename[256];
  271. int fd;
  272. int res;
  273. sprintf (filename, "/var/run/openais_cleanup_%s", compNameGlobal.value);
  274. fd = open (filename, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
  275. if (fd == -1) {
  276. printf("%d: Failed using /var/run for pid file, using /tmp\n", (int)getpid());
  277. sprintf (filename, "/tmp/openais_cleanup_%s", compNameGlobal.value);
  278. fd = open (filename, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
  279. }
  280. sprintf (pid, "%d", (int)getpid());
  281. res = write (fd, pid, strlen (pid));
  282. close (fd);
  283. }
  284. int main (int argc, char **argv)
  285. {
  286. int result;
  287. SaSelectionObjectT select_fd;
  288. fd_set read_fds;
  289. char *name;
  290. char *env;
  291. name = getenv ("SA_AMF_COMPONENT_NAME");
  292. if (name == NULL) {
  293. die ("SA_AMF_COMPONENT_NAME missing");
  294. }
  295. if (strstr (name, "safComp=") == NULL ||
  296. strstr (name, "safSu=") == NULL ||
  297. strstr (name, "safSg=") == NULL ||
  298. strstr (name, "safApp=") == NULL) {
  299. die ("SA_AMF_COMPONENT_NAME value wrong");
  300. }
  301. printf("%d: Hello world from %s\n", (int)getpid(), name);
  302. env = getenv ("var1");
  303. if (env == NULL) {
  304. die ("var1 missing");
  305. }
  306. if (strcmp (env, "val1") != 0) {
  307. die ("var1 value wrong");
  308. }
  309. env = getenv ("var2");
  310. if (env == NULL) {
  311. die ("var2 wrong");
  312. }
  313. if (strcmp (env, "val2") != 0) {
  314. die ("var2 value wrong");
  315. }
  316. signal (SIGINT, sigintr_handler);
  317. signal (SIGUSR1, sigusr1_handler);
  318. signal (SIGUSR2, sigusr2_handler);
  319. #if ! defined(TS_CLASS) && (defined(OPENAIS_BSD) || defined(OPENAIS_LINUX) || defined(OPENAIS_SOLARIS))
  320. sched_param.sched_priority = sched_get_priority_max(SCHED_RR);
  321. if (sched_param.sched_priority == -1) {
  322. fprintf (stderr, "%d: couldn't retrieve the maximum scheduling " \
  323. "priority supported by the Round-Robin class (%s)\n",
  324. (int)getpid(), strerror(errno));
  325. } else {
  326. result = sched_setscheduler (0, SCHED_RR, &sched_param);
  327. if (result == -1) {
  328. fprintf (stderr, "%d: couldn't set sched priority (%s)\n",
  329. (int)getpid(), strerror(errno));
  330. }
  331. }
  332. #endif
  333. do {
  334. result = saAmfInitialize (&handle, &amfCallbacks, &version);
  335. if (result == SA_AIS_ERR_TRY_AGAIN) {
  336. printf("%d: TRY_AGAIN received\n", getpid());
  337. usleep (100000);
  338. }
  339. } while (result == SA_AIS_ERR_TRY_AGAIN);
  340. if (result != SA_AIS_OK) {
  341. die ("saAmfInitialize result is %d", result);
  342. }
  343. FD_ZERO (&read_fds);
  344. do {
  345. result = saAmfSelectionObjectGet (handle, &select_fd);
  346. if (result == SA_AIS_ERR_TRY_AGAIN) {
  347. printf("%d: TRY_AGAIN received\n", getpid());
  348. usleep (100000);
  349. }
  350. } while (result == SA_AIS_ERR_TRY_AGAIN);
  351. if (result != SA_AIS_OK) {
  352. die ("saAmfSelectionObjectGet failed %d", result);
  353. }
  354. FD_SET (select_fd, &read_fds);
  355. do {
  356. result = saAmfComponentNameGet (handle, &compNameGlobal);
  357. if (result == SA_AIS_ERR_TRY_AGAIN) {
  358. printf("%d: TRY_AGAIN received\n", getpid());
  359. usleep (100000);
  360. }
  361. } while (result == SA_AIS_ERR_TRY_AGAIN);
  362. if (result != SA_AIS_OK) {
  363. die ("saAmfComponentNameGet failed %d", result);
  364. }
  365. write_pid ();
  366. do {
  367. result = saAmfHealthcheckStart (handle,
  368. &compNameGlobal,
  369. &key0,
  370. SA_AMF_HEALTHCHECK_AMF_INVOKED,
  371. SA_AMF_COMPONENT_FAILOVER);
  372. if (result == SA_AIS_ERR_TRY_AGAIN) {
  373. printf("%d: TRY_AGAIN received\n", getpid());
  374. usleep (100000);
  375. }
  376. } while (result == SA_AIS_ERR_TRY_AGAIN);
  377. if (result != SA_AIS_OK) {
  378. die ("saAmfHealthcheckStart failed %d", result);
  379. }
  380. {
  381. SaNameT badname;
  382. strcpy ((char*)badname.value, "badname");
  383. badname.length = 7;
  384. do {
  385. result = saAmfComponentRegister (handle, &badname, NULL);
  386. if (result == SA_AIS_ERR_TRY_AGAIN) {
  387. printf("%d: TRY_AGAIN received\n", getpid());
  388. usleep (100000);
  389. }
  390. } while (result == SA_AIS_ERR_TRY_AGAIN);
  391. if (result != SA_AIS_ERR_INVALID_PARAM) {
  392. die ("saAmfComponentRegister failed %d", result);
  393. }
  394. }
  395. do {
  396. result = saAmfComponentRegister (handle, &compNameGlobal, NULL);
  397. if (result == SA_AIS_ERR_TRY_AGAIN) {
  398. printf("%d: TRY_AGAIN received\n", getpid());
  399. usleep (100000);
  400. }
  401. } while (result == SA_AIS_ERR_TRY_AGAIN);
  402. if (result != SA_AIS_OK) {
  403. die ("saAmfComponentRegister failed %d", result);
  404. }
  405. /*
  406. * Test already started healthcheck
  407. */
  408. do {
  409. result = saAmfHealthcheckStart (handle,
  410. &compNameGlobal,
  411. &key0,
  412. SA_AMF_HEALTHCHECK_AMF_INVOKED,
  413. SA_AMF_COMPONENT_FAILOVER);
  414. if (result == SA_AIS_ERR_TRY_AGAIN) {
  415. printf("%d: TRY_AGAIN received\n", getpid());
  416. usleep (100000);
  417. }
  418. } while (result == SA_AIS_ERR_TRY_AGAIN);
  419. if (result != SA_AIS_ERR_EXIST) {
  420. die ("saAmfHealthcheckStart failed %d", result);
  421. }
  422. do {
  423. result = select (select_fd + 1, &read_fds, 0, 0, 0);
  424. if (result == -1 && errno == EINTR) {
  425. switch (stop) {
  426. case FINALIZE:
  427. result = saAmfFinalize (handle);
  428. if (result != SA_AIS_OK) {
  429. die ("saAmfFinalize failed %d", result);
  430. }
  431. fprintf(stderr, "%d: %s exiting\n",
  432. getpid(), compNameGlobal.value);
  433. exit (EXIT_SUCCESS);
  434. break;
  435. case UNREGISTER:
  436. fprintf(stderr, "%d: %s unregistering\n",
  437. getpid(), compNameGlobal.value);
  438. result = saAmfComponentUnregister (
  439. handle, &compNameGlobal, NULL);
  440. if (result != SA_AIS_OK) {
  441. die ("saAmfComponentUnregister failed %d", result);
  442. }
  443. fprintf(stderr, "%d: waiting after unregister\n", getpid());
  444. while (1) {
  445. sleep (100000000);
  446. }
  447. break;
  448. case ERROR_REPORT:
  449. fprintf(stderr, "%d: %s error reporting\n",
  450. getpid(), compNameGlobal.value);
  451. result = saAmfComponentErrorReport (
  452. handle, &compNameGlobal, 0, SA_AMF_COMPONENT_RESTART, 0);
  453. if (result != SA_AIS_OK) {
  454. die ("saAmfComponentErrorReport failed %d", result);
  455. }
  456. fprintf(stderr, "%d: waiting after error report\n", getpid());
  457. while (1) {
  458. sleep (100000000);
  459. }
  460. break;
  461. default:
  462. die ("unknown %d", stop);
  463. break;
  464. }
  465. } else if (result == -1) {
  466. die ("select failed - %s", strerror (errno));
  467. }
  468. if (result > 0) {
  469. do {
  470. result = saAmfDispatch (handle, SA_DISPATCH_ALL);
  471. if (result == SA_AIS_ERR_TRY_AGAIN) {
  472. fprintf(stderr, "%d: TRY_AGAIN received\n", getpid());
  473. usleep (100000);
  474. }
  475. } while (result == SA_AIS_ERR_TRY_AGAIN);
  476. if (result != SA_AIS_OK) {
  477. die ("saAmfDispatch failed %d", result);
  478. }
  479. }
  480. } while (result && stop == 0);
  481. fprintf(stderr, "%d: exiting...\n", getpid());
  482. exit (EXIT_SUCCESS);
  483. }