testamf1.c 8.0 KB

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