testamf1.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. sched_priority: 99
  259. };
  260. #endif
  261. void sigintr_handler (int signum) {
  262. stop = FINALIZE;
  263. }
  264. void sigusr1_handler (int signum) {
  265. stop = UNREGISTER;
  266. }
  267. void sigusr2_handler (int signum) {
  268. stop = ERROR_REPORT;
  269. }
  270. void write_pid (void) {
  271. char pid[256];
  272. char filename[256];
  273. int fd;
  274. int res;
  275. sprintf (filename, "/var/run/openais_cleanup_%s", compNameGlobal.value);
  276. fd = open (filename, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
  277. if (fd == -1) {
  278. printf("%d: Failed using /var/run for pid file, using /tmp\n", (int)getpid());
  279. sprintf (filename, "/tmp/openais_cleanup_%s", compNameGlobal.value);
  280. fd = open (filename, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
  281. }
  282. sprintf (pid, "%d", (int)getpid());
  283. res = write (fd, pid, strlen (pid));
  284. close (fd);
  285. }
  286. int main (int argc, char **argv)
  287. {
  288. int result;
  289. SaSelectionObjectT select_fd;
  290. fd_set read_fds;
  291. char *name;
  292. char *env;
  293. name = getenv ("SA_AMF_COMPONENT_NAME");
  294. if (name == NULL) {
  295. die ("SA_AMF_COMPONENT_NAME missing");
  296. }
  297. if (strstr (name, "safComp=") == NULL ||
  298. strstr (name, "safSu=") == NULL ||
  299. strstr (name, "safSg=") == NULL ||
  300. strstr (name, "safApp=") == NULL) {
  301. die ("SA_AMF_COMPONENT_NAME value wrong");
  302. }
  303. printf("%d: Hello world from %s\n", (int)getpid(), name);
  304. env = getenv ("var1");
  305. if (env == NULL) {
  306. die ("var1 missing");
  307. }
  308. if (strcmp (env, "val1") != 0) {
  309. die ("var1 value wrong");
  310. }
  311. env = getenv ("var2");
  312. if (env == NULL) {
  313. die ("var2 wrong");
  314. }
  315. if (strcmp (env, "val2") != 0) {
  316. die ("var2 value wrong");
  317. }
  318. signal (SIGINT, sigintr_handler);
  319. signal (SIGUSR1, sigusr1_handler);
  320. signal (SIGUSR2, sigusr2_handler);
  321. #if ! defined(TS_CLASS) && (defined(OPENAIS_BSD) || defined(OPENAIS_LINUX) || defined(OPENAIS_SOLARIS))
  322. result = sched_setscheduler (0, SCHED_RR, &sched_param);
  323. if (result == -1) {
  324. fprintf (stderr, "%d: couldn't set sched priority\n", (int)getpid());
  325. }
  326. #endif
  327. do {
  328. result = saAmfInitialize (&handle, &amfCallbacks, &version);
  329. if (result == SA_AIS_ERR_TRY_AGAIN) {
  330. printf("%d: TRY_AGAIN received\n", getpid());
  331. usleep (100000);
  332. }
  333. } while (result == SA_AIS_ERR_TRY_AGAIN);
  334. if (result != SA_AIS_OK) {
  335. die ("saAmfInitialize result is %d", result);
  336. }
  337. FD_ZERO (&read_fds);
  338. do {
  339. result = saAmfSelectionObjectGet (handle, &select_fd);
  340. if (result == SA_AIS_ERR_TRY_AGAIN) {
  341. printf("%d: TRY_AGAIN received\n", getpid());
  342. usleep (100000);
  343. }
  344. } while (result == SA_AIS_ERR_TRY_AGAIN);
  345. if (result != SA_AIS_OK) {
  346. die ("saAmfSelectionObjectGet failed %d", result);
  347. }
  348. FD_SET (select_fd, &read_fds);
  349. do {
  350. result = saAmfComponentNameGet (handle, &compNameGlobal);
  351. if (result == SA_AIS_ERR_TRY_AGAIN) {
  352. printf("%d: TRY_AGAIN received\n", getpid());
  353. usleep (100000);
  354. }
  355. } while (result == SA_AIS_ERR_TRY_AGAIN);
  356. if (result != SA_AIS_OK) {
  357. die ("saAmfComponentNameGet failed %d", result);
  358. }
  359. write_pid ();
  360. do {
  361. result = saAmfHealthcheckStart (handle,
  362. &compNameGlobal,
  363. &key0,
  364. SA_AMF_HEALTHCHECK_AMF_INVOKED,
  365. SA_AMF_COMPONENT_FAILOVER);
  366. if (result == SA_AIS_ERR_TRY_AGAIN) {
  367. printf("%d: TRY_AGAIN received\n", getpid());
  368. usleep (100000);
  369. }
  370. } while (result == SA_AIS_ERR_TRY_AGAIN);
  371. if (result != SA_AIS_OK) {
  372. die ("saAmfHealthcheckStart failed %d", result);
  373. }
  374. {
  375. SaNameT badname;
  376. strcpy ((char*)badname.value, "badname");
  377. badname.length = 7;
  378. do {
  379. result = saAmfComponentRegister (handle, &badname, NULL);
  380. if (result == SA_AIS_ERR_TRY_AGAIN) {
  381. printf("%d: TRY_AGAIN received\n", getpid());
  382. usleep (100000);
  383. }
  384. } while (result == SA_AIS_ERR_TRY_AGAIN);
  385. if (result != SA_AIS_ERR_INVALID_PARAM) {
  386. die ("saAmfComponentRegister failed %d", result);
  387. }
  388. }
  389. do {
  390. result = saAmfComponentRegister (handle, &compNameGlobal, NULL);
  391. if (result == SA_AIS_ERR_TRY_AGAIN) {
  392. printf("%d: TRY_AGAIN received\n", getpid());
  393. usleep (100000);
  394. }
  395. } while (result == SA_AIS_ERR_TRY_AGAIN);
  396. if (result != SA_AIS_OK) {
  397. die ("saAmfComponentRegister failed %d", result);
  398. }
  399. /*
  400. * Test already started healthcheck
  401. */
  402. do {
  403. result = saAmfHealthcheckStart (handle,
  404. &compNameGlobal,
  405. &key0,
  406. SA_AMF_HEALTHCHECK_AMF_INVOKED,
  407. SA_AMF_COMPONENT_FAILOVER);
  408. if (result == SA_AIS_ERR_TRY_AGAIN) {
  409. printf("%d: TRY_AGAIN received\n", getpid());
  410. usleep (100000);
  411. }
  412. } while (result == SA_AIS_ERR_TRY_AGAIN);
  413. if (result != SA_AIS_ERR_EXIST) {
  414. die ("saAmfHealthcheckStart failed %d", result);
  415. }
  416. do {
  417. result = select (select_fd + 1, &read_fds, 0, 0, 0);
  418. if (result == -1 && errno == EINTR) {
  419. switch (stop) {
  420. case FINALIZE:
  421. result = saAmfFinalize (handle);
  422. if (result != SA_AIS_OK) {
  423. die ("saAmfFinalize failed %d", result);
  424. }
  425. fprintf(stderr, "%d: %s exiting\n",
  426. getpid(), compNameGlobal.value);
  427. exit (EXIT_SUCCESS);
  428. break;
  429. case UNREGISTER:
  430. fprintf(stderr, "%d: %s unregistering\n",
  431. getpid(), compNameGlobal.value);
  432. result = saAmfComponentUnregister (
  433. handle, &compNameGlobal, NULL);
  434. if (result != SA_AIS_OK) {
  435. die ("saAmfComponentUnregister failed %d", result);
  436. }
  437. fprintf(stderr, "%d: waiting after unregister\n", getpid());
  438. while (1) {
  439. sleep (100000000);
  440. }
  441. break;
  442. case ERROR_REPORT:
  443. fprintf(stderr, "%d: %s error reporting\n",
  444. getpid(), compNameGlobal.value);
  445. result = saAmfComponentErrorReport (
  446. handle, &compNameGlobal, 0, SA_AMF_COMPONENT_RESTART, 0);
  447. if (result != SA_AIS_OK) {
  448. die ("saAmfComponentErrorReport failed %d", result);
  449. }
  450. fprintf(stderr, "%d: waiting after error report\n", getpid());
  451. while (1) {
  452. sleep (100000000);
  453. }
  454. break;
  455. default:
  456. die ("unknown %d", stop);
  457. break;
  458. }
  459. } else if (result == -1) {
  460. die ("select failed - %s", strerror (errno));
  461. }
  462. if (result > 0) {
  463. do {
  464. result = saAmfDispatch (handle, SA_DISPATCH_ALL);
  465. if (result == SA_AIS_ERR_TRY_AGAIN) {
  466. fprintf(stderr, "%d: TRY_AGAIN received\n", getpid());
  467. usleep (100000);
  468. }
  469. } while (result == SA_AIS_ERR_TRY_AGAIN);
  470. if (result != SA_AIS_OK) {
  471. die ("saAmfDispatch failed %d", result);
  472. }
  473. }
  474. } while (result && stop == 0);
  475. fprintf(stderr, "%d: exiting...\n", getpid());
  476. exit (EXIT_SUCCESS);
  477. }