testamf1.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright (c) 2002-2003 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.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 MontaVista Software, 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. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <errno.h>
  37. #include <signal.h>
  38. #include <unistd.h>
  39. #include <sys/types.h>
  40. #include <sys/socket.h>
  41. #include <sys/un.h>
  42. #include <sched.h>
  43. #include <sys/stat.h>
  44. #include <fcntl.h>
  45. #include "saAis.h"
  46. #include "saAmf.h"
  47. SaAmfHandleT handle;
  48. SaAmfHealthcheckKeyT key0 = {
  49. .key = "key1",
  50. .keyLen = 4
  51. };
  52. SaNameT compNameGlobal;
  53. void printSaNameT (SaNameT *name)
  54. {
  55. int i;
  56. for (i = 0; i < name->length; i++) {
  57. printf ("%c", name->value[i]);
  58. }
  59. }
  60. void setSanameT (SaNameT *name, char *str) {
  61. name->length = strlen (str);
  62. memcpy (name->value, str, name->length);
  63. }
  64. static int health_flag = -1;
  65. static unsigned int healthcheck_count = 0;
  66. static unsigned int healthcheck_no = 0;
  67. int stop = 0;
  68. void HealthcheckCallback (SaInvocationT invocation,
  69. const SaNameT *compName,
  70. SaAmfHealthcheckKeyT *healthcheckKey)
  71. {
  72. SaAisErrorT res;
  73. healthcheck_no++;
  74. /*
  75. printf ("Healthcheck %u for key '%s' for component ",
  76. healthcheck_no, healthcheckKey->key);
  77. printSaNameT ((SaNameT *)compName);
  78. printf ("\n");
  79. */
  80. if (healthcheck_no == 10) {
  81. printf ("COMPONENT REPORTING ERROR %s\n", compNameGlobal.value);
  82. saAmfComponentErrorReport (handle, compName, 0, SA_AMF_COMPONENT_RESTART, 0);
  83. res = saAmfResponse (handle, invocation, SA_AIS_OK);
  84. printf ("COMPONENT DONE REPORTING ERROR\n");
  85. } else {
  86. res = saAmfResponse (handle, invocation, SA_AIS_OK);
  87. }
  88. /*
  89. if (healthcheck_no < 10) {
  90. res = saAmfResponse (handle, invocation, SA_AIS_OK);
  91. }
  92. */
  93. /*
  94. if (healthcheck_no == 3) {
  95. res = saAmfHealthcheckStop (handle, &compNameGlobal, &key0);
  96. stop = 1;
  97. }
  98. */
  99. }
  100. void ComponentTerminateCallback (
  101. SaInvocationT invocation,
  102. const SaNameT *compName)
  103. {
  104. printf ("ComponentTerminateCallback\n");
  105. saAmfResponse (handle, invocation, SA_AIS_OK);
  106. exit (0);
  107. }
  108. void CSISetCallback (
  109. SaInvocationT invocation,
  110. const SaNameT *compName,
  111. SaAmfHAStateT haState,
  112. SaAmfCSIDescriptorT *csiDescriptor)
  113. {
  114. int res;
  115. switch (haState) {
  116. case SA_AMF_HA_ACTIVE:
  117. printf ("CSISetCallback:");
  118. printf ("for CSI '");
  119. printSaNameT ((SaNameT *)&csiDescriptor->csiName);
  120. printf ("' for component ");
  121. printSaNameT ((SaNameT *)compName);
  122. printf ("'");
  123. printf (" requested to enter hastate SA_AMF_ACTIVE.\n");
  124. res = saAmfResponse (handle, invocation, SA_AIS_OK);
  125. break;
  126. case SA_AMF_HA_STANDBY:
  127. printf ("CSISetCallback:");
  128. printf ("for CSI '");
  129. printSaNameT ((SaNameT *)compName);
  130. printf ("' for component ");
  131. printSaNameT ((SaNameT *)compName);
  132. printf ("'");
  133. printf (" requested to enter hastate SA_AMF_STANDBY.\n");
  134. saAmfResponse (handle, invocation, SA_AIS_OK);
  135. break;
  136. }
  137. }
  138. void CSIRemoveCallback (
  139. SaInvocationT invocation,
  140. const SaNameT *compName,
  141. const SaNameT *csiName,
  142. const SaAmfCSIFlagsT *csiFlags)
  143. {
  144. int res;
  145. printf ("CSIRemoveCallback for component '");
  146. printSaNameT ((SaNameT *)compName);
  147. printf ("' in CSI '");
  148. printSaNameT ((SaNameT *)csiName);
  149. printf ("'\n");
  150. res = saAmfResponse (handle, invocation, SA_AIS_OK);
  151. }
  152. #ifdef COMPILE_OUT
  153. void ProtectionGroupTrackCallback (
  154. const SaNameT *csiName,
  155. SaAmfProtectionGroupNotificationT *notificationBuffer,
  156. SaUint32T numberOfItems,
  157. SaUint32T numberOfMembers,
  158. SaAisErrorT error)
  159. {
  160. int i;
  161. printf ("ProtectionGroupTrackCallback items %d members %d\n", (int)numberOfItems, (int)numberOfMembers);
  162. printf ("buffer is %p\n", notificationBuffer);
  163. for (i = 0; i < numberOfItems; i++) {
  164. printf ("component name");
  165. printSaNameT (&notificationBuffer[i].member.compName);
  166. printf ("\n");
  167. printf ("\treadiness state is %d\n", notificationBuffer[i].member.readinessState);
  168. printf ("\thastate %d\n", notificationBuffer[i].member.haState);
  169. printf ("\tchange is %d\n", notificationBuffer[i].change);
  170. }
  171. }
  172. #endif
  173. SaAmfCallbacksT amfCallbacks = {
  174. .saAmfHealthcheckCallback = HealthcheckCallback,
  175. .saAmfComponentTerminateCallback = ComponentTerminateCallback,
  176. .saAmfCSISetCallback = CSISetCallback,
  177. .saAmfCSIRemoveCallback = CSIRemoveCallback,
  178. };
  179. SaAmfCallbacksT amfCallbacks;
  180. SaVersionT version = { 'B', 1, 1 };
  181. static struct sched_param sched_param = {
  182. sched_priority: 99
  183. };
  184. void sigintr_handler (int signum) {
  185. exit (0);
  186. }
  187. void write_pid (void) {
  188. char pid[256];
  189. char filename[256];
  190. int fd;
  191. int res;
  192. sprintf (filename, "/var/run/openais_cleanup_%s", compNameGlobal.value);
  193. fd = open (filename, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
  194. sprintf (pid, "%d", getpid());
  195. res = write (fd, pid, strlen (pid));
  196. close (fd);
  197. }
  198. int main (int argc, char **argv) {
  199. int result;
  200. SaSelectionObjectT select_fd;
  201. fd_set read_fds;
  202. extern char *optarg;
  203. extern int optind;
  204. signal (SIGINT, sigintr_handler);
  205. result = sched_setscheduler (0, SCHED_RR, &sched_param);
  206. if (result == -1) {
  207. printf ("couldn't set sched priority\n");
  208. }
  209. result = saAmfInitialize (&handle, &amfCallbacks, &version);
  210. if (result != SA_AIS_OK) {
  211. printf ("initialize result is %d\n", result);
  212. exit (1);
  213. }
  214. FD_ZERO (&read_fds);
  215. saAmfSelectionObjectGet (handle, &select_fd);
  216. FD_SET (select_fd, &read_fds);
  217. saAmfComponentNameGet (handle, &compNameGlobal);
  218. write_pid ();
  219. result = saAmfHealthcheckStart (handle,
  220. &compNameGlobal,
  221. &key0,
  222. SA_AMF_HEALTHCHECK_AMF_INVOKED,
  223. SA_AMF_COMPONENT_FAILOVER);
  224. result = saAmfHealthcheckStart (handle,
  225. &compNameGlobal,
  226. &key0,
  227. SA_AMF_HEALTHCHECK_AMF_INVOKED,
  228. SA_AMF_COMPONENT_FAILOVER);
  229. result = saAmfComponentRegister (handle, &compNameGlobal, NULL);
  230. printf ("register result is %d (should be 1)\n", result);
  231. do {
  232. select (select_fd + 1, &read_fds, 0, 0, 0);
  233. saAmfDispatch (handle, SA_DISPATCH_ALL);
  234. } while (result && stop == 0);
  235. printf ("healthchecks stopped for 5 seconds\n");
  236. sleep (5);
  237. result = saAmfHealthcheckStart (handle,
  238. &compNameGlobal,
  239. &key0,
  240. SA_AMF_HEALTHCHECK_AMF_INVOKED,
  241. SA_AMF_COMPONENT_FAILOVER);
  242. do {
  243. select (select_fd + 1, &read_fds, 0, 0, 0);
  244. saAmfDispatch (handle, SA_DISPATCH_ALL);
  245. } while (result);
  246. saAmfFinalize (handle);
  247. exit (0);
  248. }