testamf1.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. int good_health = 0;
  55. int good_health_limit = 0;
  56. void printSaNameT (SaNameT *name)
  57. {
  58. int i;
  59. for (i = 0; i < name->length; i++) {
  60. printf ("%c", name->value[i]);
  61. }
  62. }
  63. void setSanameT (SaNameT *name, char *str) {
  64. name->length = strlen (str);
  65. memcpy (name->value, str, name->length);
  66. }
  67. static unsigned int healthcheck_no = 0;
  68. int stop = 0;
  69. void HealthcheckCallback (SaInvocationT invocation,
  70. const SaNameT *compName,
  71. SaAmfHealthcheckKeyT *healthcheckKey)
  72. {
  73. SaAisErrorT res;
  74. if( !good_health && healthcheck_no++);
  75. if (healthcheck_no == good_health_limit ) {
  76. res = saAmfResponse (handle, invocation, SA_AIS_OK);
  77. res = saAmfHealthcheckStop (handle,
  78. &compNameGlobal,
  79. &key0);
  80. printf ("healthcheck stop result %d (should be %d)\n", res, SA_AIS_OK);
  81. printf ("COMPONENT REPORTING ERROR %s\n", compNameGlobal.value);
  82. saAmfComponentErrorReport (handle, compName, 0, SA_AMF_COMPONENT_RESTART, 0);
  83. printf ("COMPONENT DONE REPORTING ERROR\n");
  84. } else {
  85. res = saAmfResponse (handle, invocation, SA_AIS_OK);
  86. }
  87. }
  88. void ComponentTerminateCallback (
  89. SaInvocationT invocation,
  90. const SaNameT *compName)
  91. {
  92. printf ("ComponentTerminateCallback\n");
  93. saAmfResponse (handle, invocation, SA_AIS_OK);
  94. exit (0);
  95. }
  96. #if 0
  97. #include <sys/time.h>
  98. #define TRU "%d"
  99. #define TRS "%s"
  100. #define TR(format,x) do { \
  101. struct timeval t;\
  102. gettimeofday(&t,NULL); \
  103. printf("%s:%d: %s : %d : %u: %u :%s : " format "\n",\
  104. __FILE__,__LINE__,__FUNCTION__, \
  105. getpid(),(int)t.tv_sec, (int)t.tv_usec,#x,x); \
  106. }while(0)
  107. #else
  108. #define TRU "%d"
  109. #define TRS "%s"
  110. #define TR(format,x)
  111. #endif
  112. void CSISetCallback (
  113. SaInvocationT invocation,
  114. const SaNameT *compName,
  115. SaAmfHAStateT haState,
  116. SaAmfCSIDescriptorT *csiDescriptor)
  117. {
  118. SaAmfHAStateT state;
  119. int res;
  120. switch (haState) {
  121. case SA_AMF_HA_ACTIVE:
  122. printf ("Component '%s' requested to enter hastate SA_AMF_ACTIVE for \n\tCSI '%s'\n",
  123. compName->value, csiDescriptor->csiName.value);
  124. res = saAmfResponse (handle, invocation, SA_AIS_OK);
  125. if (res != SA_AIS_OK) {
  126. fprintf (stderr, "saAmfResponse failed: %d\n", res);
  127. exit (-1);
  128. }
  129. res = saAmfHAStateGet (handle, compName, &csiDescriptor->csiName, &state);
  130. if (res != SA_AIS_OK || haState != state) {
  131. fprintf (stderr, "saAmfHAStateGet failed: %d\n", res);
  132. exit (-1);
  133. }
  134. int i;
  135. TR(TRU, csiDescriptor->csiAttr.number);
  136. for(i=0; i<csiDescriptor->csiAttr.number; i++) {
  137. if( strcmp((char*)csiDescriptor->csiAttr.attr[i].attrName, "good_health_limit") == 0){
  138. good_health = strcmp((char*)csiDescriptor->csiAttr.attr[i].attrValue, "0") ? 0 : 1;
  139. good_health_limit = atoi((char*)csiDescriptor->csiAttr.attr[i].attrValue);
  140. }
  141. #if 0
  142. TR(TRS,csiDescriptor->csiAttr.attr[i].attrName);
  143. TR(TRS, csiDescriptor->csiAttr.attr[i].attrValue);
  144. #endif
  145. }
  146. TR(TRU, csiDescriptor->csiFlags);
  147. printSaNameT((SaNameT*) &csiDescriptor->csiStateDescriptor.activeDescriptor.activeCompName);
  148. TR(TRU, csiDescriptor->csiStateDescriptor.activeDescriptor.transitionDescriptor);
  149. break;
  150. case SA_AMF_HA_STANDBY:
  151. printf ("Component '%s' requested to enter hastate SA_AMF_STANDBY for \n\tCSI '%s'\n",
  152. compName->value, csiDescriptor->csiName.value);
  153. res = saAmfResponse (handle, invocation, SA_AIS_OK);
  154. TR(TRU,csiDescriptor->csiAttr.number);
  155. for(i=0; i<csiDescriptor->csiAttr.number; i++) {
  156. if(!strcmp((char*)csiDescriptor->csiAttr.attr[i].attrName, "good_health") &&
  157. !strcmp((char*)csiDescriptor->csiAttr.attr[i].attrValue, "true")){
  158. good_health = 1;
  159. }
  160. TR(TRS,csiDescriptor->csiAttr.attr[i].attrName);
  161. TR(TRS,csiDescriptor->csiAttr.attr[i].attrValue);
  162. }
  163. printf("%s:%d:%s:%d\n",__FILE__,__LINE__,__FUNCTION__,res);
  164. TR(TRU,csiDescriptor->csiFlags);
  165. printSaNameT((SaNameT*) &csiDescriptor->csiStateDescriptor.standbyDescriptor.activeCompName);
  166. TR(TRU,csiDescriptor->csiStateDescriptor.standbyDescriptor.standbyRank);
  167. break;
  168. case SA_AMF_HA_QUIESCED:
  169. break;
  170. case SA_AMF_HA_QUIESCING:
  171. break;
  172. }
  173. }
  174. void CSIRemoveCallback (
  175. SaInvocationT invocation,
  176. const SaNameT *compName,
  177. const SaNameT *csiName,
  178. SaAmfCSIFlagsT csiFlags)
  179. {
  180. int res;
  181. printf ("CSIRemoveCallback for component '");
  182. printSaNameT ((SaNameT *)compName);
  183. printf ("' in CSI '");
  184. printSaNameT ((SaNameT *)csiName);
  185. printf ("'\n");
  186. res = saAmfResponse (handle, invocation, SA_AIS_OK);
  187. }
  188. #ifdef COMPILE_OUT
  189. void ProtectionGroupTrackCallback (
  190. const SaNameT *csiName,
  191. SaAmfProtectionGroupNotificationT *notificationBuffer,
  192. SaUint32T numberOfItems,
  193. SaUint32T numberOfMembers,
  194. SaAisErrorT error)
  195. {
  196. int i;
  197. printf ("ProtectionGroupTrackCallback items %d members %d\n", (int)numberOfItems, (int)numberOfMembers);
  198. printf ("buffer is %p\n", notificationBuffer);
  199. for (i = 0; i < numberOfItems; i++) {
  200. printf ("component name");
  201. printSaNameT (&notificationBuffer[i].member.compName);
  202. printf ("\n");
  203. printf ("\treadiness state is %d\n", notificationBuffer[i].member.readinessState);
  204. printf ("\thastate %d\n", notificationBuffer[i].member.haState);
  205. printf ("\tchange is %d\n", notificationBuffer[i].change);
  206. }
  207. }
  208. #endif
  209. SaAmfCallbacksT amfCallbacks = {
  210. .saAmfHealthcheckCallback = HealthcheckCallback,
  211. .saAmfComponentTerminateCallback = ComponentTerminateCallback,
  212. .saAmfCSISetCallback = CSISetCallback,
  213. .saAmfCSIRemoveCallback = CSIRemoveCallback,
  214. };
  215. SaAmfCallbacksT amfCallbacks;
  216. SaVersionT version = { 'B', 1, 1 };
  217. #if defined(OPENAIS_BSD) || defined(OPENAIS_LINUX)
  218. static struct sched_param sched_param = {
  219. sched_priority: 99
  220. };
  221. #endif
  222. void sigintr_handler (int signum) {
  223. exit (0);
  224. }
  225. void write_pid (void) {
  226. char pid[256];
  227. char filename[256];
  228. int fd;
  229. int res;
  230. sprintf (filename, "/var/run/openais_cleanup_%s", compNameGlobal.value);
  231. fd = open (filename, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
  232. if (fd == -1) {
  233. printf("Failed using /var/run for pid file, using /tmp\n");
  234. sprintf (filename, "/tmp/openais_cleanup_%s", compNameGlobal.value);
  235. fd = open (filename, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
  236. }
  237. sprintf (pid, "%d", getpid());
  238. res = write (fd, pid, strlen (pid));
  239. close (fd);
  240. }
  241. int main (int argc, char **argv) {
  242. int result;
  243. SaSelectionObjectT select_fd;
  244. fd_set read_fds;
  245. extern char *optarg;
  246. extern int optind;
  247. char *name = getenv ("SA_AMF_COMPONENT_NAME");
  248. /* test that it exist */
  249. if (name == NULL) {
  250. fprintf(stderr, "SA_AMF_COMPONENT_NAME missing\n");
  251. exit (-1);
  252. }
  253. /* test for correct value */
  254. if (strstr (name, "safComp=A,safSu=SERVICE_X_") == NULL) {
  255. fprintf(stderr, "SA_AMF_COMPONENT_NAME value wrong\n");
  256. exit (-2);
  257. }
  258. printf("Hello world from %s\n", name);
  259. signal (SIGINT, sigintr_handler);
  260. #if defined(OPENAIS_BSD) || defined(OPENAIS_LINUX)
  261. result = sched_setscheduler (0, SCHED_RR, &sched_param);
  262. if (result == -1) {
  263. printf ("couldn't set sched priority\n");
  264. }
  265. #endif
  266. result = saAmfInitialize (&handle, &amfCallbacks, &version);
  267. if (result != SA_AIS_OK) {
  268. printf ("initialize result is %d\n", result);
  269. exit (1);
  270. }
  271. FD_ZERO (&read_fds);
  272. saAmfSelectionObjectGet (handle, &select_fd);
  273. FD_SET (select_fd, &read_fds);
  274. saAmfComponentNameGet (handle, &compNameGlobal);
  275. write_pid ();
  276. result = saAmfHealthcheckStart (handle,
  277. &compNameGlobal,
  278. &key0,
  279. SA_AMF_HEALTHCHECK_AMF_INVOKED,
  280. SA_AMF_COMPONENT_FAILOVER);
  281. printf ("healthcheck start result %d (should be %d)\n", result, SA_AIS_OK);
  282. {
  283. SaNameT badname;
  284. strcpy ((char*)badname.value, "badname");
  285. badname.length = 7;
  286. result = saAmfComponentRegister (handle, &badname, NULL);
  287. printf ("register result is %d (should be %d)\n", result, SA_AIS_ERR_INVALID_PARAM);
  288. }
  289. result = saAmfComponentRegister (handle, &compNameGlobal, NULL);
  290. printf ("register result is %d (should be %d)\n", result, SA_AIS_OK);
  291. /*
  292. * Test already started healthcheck
  293. */
  294. result = saAmfHealthcheckStart (handle,
  295. &compNameGlobal,
  296. &key0,
  297. SA_AMF_HEALTHCHECK_AMF_INVOKED,
  298. SA_AMF_COMPONENT_FAILOVER);
  299. printf ("healthcheck start result %d (should be %d)\n", result, SA_AIS_ERR_EXIST);
  300. do {
  301. select (select_fd + 1, &read_fds, 0, 0, 0);
  302. saAmfDispatch (handle, SA_DISPATCH_ALL);
  303. } while (result && stop == 0);
  304. printf ("healthchecks stopped for 5 seconds\n");
  305. sleep (5);
  306. result = saAmfHealthcheckStart (handle,
  307. &compNameGlobal,
  308. &key0,
  309. SA_AMF_HEALTHCHECK_AMF_INVOKED,
  310. SA_AMF_COMPONENT_FAILOVER);
  311. do {
  312. select (select_fd + 1, &read_fds, 0, 0, 0);
  313. saAmfDispatch (handle, SA_DISPATCH_ALL);
  314. } while (result);
  315. saAmfFinalize (handle);
  316. exit (0);
  317. }