amfcomp.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207
  1. /** @file amfcomp.c
  2. *
  3. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  4. * Copyright (c) 2006 Sun Microsystems, Inc.
  5. * Copyright (c) 2006 Ericsson AB.
  6. *
  7. * All rights reserved.
  8. *
  9. * Author: Steven Dake (sdake@mvista.com)
  10. *
  11. * Author: Hans Feldt, Anders Eriksson, Lars Holm
  12. * - Introduced AMF B.02 information model
  13. * - Use DN in API and multicast messages
  14. * - (Re-)Introduction of event based multicast messages
  15. * - Refactoring of code into several AMF files
  16. * - Component/SU restart, SU failover
  17. * - Constructors/destructors
  18. * - Serializers/deserializers
  19. *
  20. * This software licensed under BSD license, the text of which follows:
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions are met:
  24. *
  25. * - Redistributions of source code must retain the above copyright notice,
  26. * this list of conditions and the following disclaimer.
  27. * - Redistributions in binary form must reproduce the above copyright notice,
  28. * this list of conditions and the following disclaimer in the documentation
  29. * and/or other materials provided with the distribution.
  30. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  31. * contributors may be used to endorse or promote products derived from this
  32. * software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  35. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  36. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  37. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  38. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  39. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  40. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  41. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  42. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  44. * THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. * AMF Component Class Implementation
  47. *
  48. * This file contains functions for handling AMF-components. It can be
  49. * viewed as the implementation of the AMF Component class (called comp)
  50. * as described in SAI-Overview-B.02.01. The SA Forum specification
  51. * SAI-AIS-AMF-B.02.01 has been used as specification of the behaviour
  52. * and is referred to as 'the spec' below.
  53. *
  54. * The functions in this file are responsible for handling the following
  55. * types of components:
  56. * - sa-aware components
  57. * (proxy or non-proxy)
  58. * - non-sa-aware components
  59. * (non-proxied non-pre-instantiable and
  60. * proxied pre-instantiable or not pre-instantiable)
  61. *
  62. * The functions of this file are also responsible for:
  63. * - handling all communication with the AMF API library supported by the
  64. * AMF main function, see below
  65. * - instantiating and terminating components upon request
  66. * - updating the ha-state of the CSI-assignment related to the component
  67. * - initiating an error report to the parent SU
  68. * - handling all run time attributes of the AMF Component; all cached
  69. * attributes are stored as variables and sent to the IMM service
  70. * upon the changes described in the specification.
  71. *
  72. * Incoming events from the AMF library is primarily handled by the AMF
  73. * main function which:
  74. * <1> transforms the incoming event to an event that is multicast
  75. * to all AMF service instances in the cluster
  76. * <2> the event received from multicast is tranformed to a function
  77. * call of the external interface of comp
  78. *
  79. * Outgoing events to the AMF library is handled by static functions called
  80. * lib_<api callback function name>_request which creates an invocation handle
  81. * unique to this call and stores any variables comp want to associate to the
  82. * call back so it is possible to pick them up when the component responses
  83. * through the API. Finally, a timer is started to supervise that a response
  84. * really is received.
  85. *
  86. * Comp initiates error reports to its parent SU in the cases described in
  87. * paragraph 3.3.2.2 in the spec. Comp delegates all actions to SU except
  88. * - it stores the received or pre-configured recommended recovery
  89. * action
  90. * - sets the operational state to DISABLED unless the
  91. * recommended recovery action was SA_AMF_COMP_RESTART. (In this case
  92. * SU or node may set operational state of the component later on
  93. * when it has been fully investigated that no escallation to a
  94. * more powerful recovery action shall be made.)
  95. *
  96. * Comp contains the following state machines:
  97. * - presence state machine (PRSM)
  98. * - operational state machine (OPSM)
  99. * - readiness state machine (RESM)
  100. * - ha state per component service instance (CSI)
  101. *
  102. * The behaviour of comp is mainly controlled by the presence state machine,
  103. * while the operational and readiness state machines are used only to report
  104. * information to its parent (service unit SU) and management (IMM). Comp does
  105. * not control the logic to assign a CSI to itself and neither to decide the
  106. * value of the ha-state but only to faciltate the communication of the CSI
  107. * set (or remove) order and to evaluate the response from the library.
  108. *
  109. * The presence state machine implements all the states described in the
  110. * specification.
  111. * The '-ING' states of PRSM are designed as composite states (UML terminology).
  112. * Being a composite state means that the state contains substates.
  113. * PRSM composite states are:
  114. * - TERMINATING (TERMINATE and CLEANUP)
  115. * - INSTANTIATING (INSTANTIATE, INSTANTIATEDELAY and CLEANUP)
  116. * - RESTARTING (TERMINATE, INSTANTIATE, INSTANTIATEDELAY and CLEANUP)
  117. *
  118. * The reason for introducing these composite states is to make it easier to
  119. * understand the implementation of the behaviour described in paragraphs
  120. * 4.1 - 4.6 in the spec. The comp PRSM implements all the logic described
  121. * except for node reboot, which is handled by the AMF Node class.
  122. * Also PRSM reports all changes of state to its parent SU.
  123. *
  124. */
  125. #include <sys/types.h>
  126. #include <unistd.h>
  127. #include <sys/wait.h>
  128. #include <stdio.h>
  129. #include <string.h>
  130. #include <stdlib.h>
  131. #include <errno.h>
  132. #include <assert.h>
  133. #include "../include/saAis.h"
  134. #include "../include/saAmf.h"
  135. #include "../include/ipc_gen.h"
  136. #include "../include/ipc_amf.h"
  137. #include "totempg.h"
  138. #include "timer.h"
  139. #include "ipc.h"
  140. #include "service.h"
  141. #include "util.h"
  142. #include "amf.h"
  143. #include "print.h"
  144. #include "main.h"
  145. enum clc_command_run_operation_type {
  146. CLC_COMMAND_RUN_OPERATION_TYPE_INSTANTIATE = 1,
  147. CLC_COMMAND_RUN_OPERATION_TYPE_TERMINATE = 2,
  148. CLC_COMMAND_RUN_OPERATION_TYPE_CLEANUP = 3
  149. };
  150. struct clc_command_run_data {
  151. struct amf_comp *comp;
  152. enum clc_command_run_operation_type type;
  153. void (*completion_callback) (void *context);
  154. };
  155. struct clc_interface {
  156. int (*instantiate) (struct amf_comp *comp);
  157. int (*terminate) (struct amf_comp *comp);
  158. int (*cleanup) (struct amf_comp *comp);
  159. };
  160. struct csi_remove_callback_data {
  161. struct amf_csi *csi;
  162. };
  163. struct component_terminate_callback_data {
  164. struct amf_comp *comp;
  165. };
  166. static void comp_presence_state_set (
  167. struct amf_comp *comp,
  168. SaAmfPresenceStateT presence_state);
  169. static int clc_cli_instantiate (struct amf_comp *comp);
  170. static int clc_instantiate_callback (struct amf_comp *comp);
  171. static int clc_csi_set_callback (struct amf_comp *comp);
  172. static int clc_cli_terminate (struct amf_comp *comp);
  173. static int lib_comp_terminate_request (struct amf_comp *comp);
  174. static int clc_csi_remove_callback (struct amf_comp *comp);
  175. static int clc_cli_cleanup (struct amf_comp *comp);
  176. static int clc_cli_cleanup_local (struct amf_comp *comp);
  177. static void healthcheck_deactivate (struct amf_healthcheck *healthcheck_active);
  178. static void lib_healthcheck_request (struct amf_healthcheck *healthcheck);
  179. static void timer_function_healthcheck_tmo (void *_healthcheck);
  180. static void lib_csi_set_request (
  181. struct amf_comp *comp,
  182. struct amf_csi_assignment *csi_assignment);
  183. /*
  184. * Life cycle functions
  185. */
  186. static struct clc_interface clc_interface_sa_aware = {
  187. clc_cli_instantiate,
  188. lib_comp_terminate_request,
  189. clc_cli_cleanup
  190. };
  191. static struct clc_interface clc_interface_proxied_pre = {
  192. clc_instantiate_callback,
  193. lib_comp_terminate_request,
  194. clc_cli_cleanup
  195. };
  196. static struct clc_interface clc_interface_proxied_non_pre = {
  197. clc_csi_set_callback,
  198. clc_csi_remove_callback,
  199. clc_cli_cleanup_local
  200. };
  201. static struct clc_interface clc_interface_non_proxied_non_saware = {
  202. clc_cli_instantiate,
  203. clc_cli_terminate,
  204. clc_cli_cleanup_local
  205. };
  206. static struct clc_interface *clc_interfaces[4] = {
  207. &clc_interface_sa_aware,
  208. &clc_interface_proxied_pre,
  209. &clc_interface_proxied_non_pre,
  210. &clc_interface_non_proxied_non_saware
  211. };
  212. struct invocation {
  213. void *data;
  214. int interface;
  215. int active;
  216. };
  217. static struct invocation *invocation_entries = 0;
  218. static int invocation_entries_size = 0;
  219. static int invocation_create (
  220. int interface,
  221. void *data)
  222. {
  223. struct invocation *invocation_addr = 0;
  224. struct invocation *invocation_temp;
  225. int i;
  226. int loc = 0;
  227. for (i = 0; i < invocation_entries_size; i++) {
  228. if (invocation_entries[i].active == 0) {
  229. invocation_addr = &invocation_entries[i];
  230. loc = i;
  231. break;
  232. }
  233. }
  234. if (invocation_addr == 0) {
  235. invocation_temp = (struct invocation *)realloc (invocation_entries,
  236. (invocation_entries_size + 1) * sizeof (struct invocation));
  237. if (invocation_temp == NULL) {
  238. openais_exit_error (AIS_DONE_OUT_OF_MEMORY);
  239. }
  240. invocation_entries = invocation_temp;
  241. invocation_addr = &invocation_entries[invocation_entries_size];
  242. loc = invocation_entries_size;
  243. invocation_entries_size += 1;
  244. }
  245. invocation_addr->interface = interface;
  246. invocation_addr->data = data;
  247. invocation_addr->active = 1;
  248. return (loc);
  249. }
  250. static int invocation_get_and_destroy (
  251. SaUint64T invocation, unsigned int *interface, void **data)
  252. {
  253. if (invocation > invocation_entries_size) {
  254. return (-1);
  255. }
  256. if (invocation_entries[invocation].active == 0) {
  257. return (-1);
  258. }
  259. *interface = invocation_entries[invocation].interface;
  260. *data = invocation_entries[invocation].data;
  261. memset (&invocation_entries[invocation], 0, sizeof (struct invocation));
  262. return (0);
  263. }
  264. static void invocation_destroy_by_data (void *data)
  265. {
  266. int i;
  267. for (i = 0; i < invocation_entries_size; i++) {
  268. if (invocation_entries[i].data == data) {
  269. memset (&invocation_entries[i], 0,
  270. sizeof (struct invocation));
  271. break;
  272. }
  273. }
  274. }
  275. /**
  276. * Set suspected error flag and report to SU.
  277. *
  278. * @param comp
  279. * @param recommended_recovery
  280. */
  281. static void report_error_suspected (
  282. struct amf_comp *comp,
  283. SaAmfRecommendedRecoveryT recommended_recovery)
  284. {
  285. comp->error_suspected = 1;
  286. amf_su_comp_error_suspected (
  287. comp->su, comp, recommended_recovery);
  288. }
  289. #ifndef xprintf
  290. #define xprintf(...)
  291. #endif
  292. static void *clc_command_run (void *context)
  293. {
  294. struct clc_command_run_data *clc_command_run_data =
  295. (struct clc_command_run_data *)context;
  296. pid_t pid;
  297. int res;
  298. char **argv = NULL;
  299. char **envp = NULL;
  300. int status;
  301. char path[PATH_MAX];
  302. char *cmd = 0;
  303. char *comp_argv = 0;
  304. char comp_name[SA_MAX_NAME_LENGTH + 24];
  305. int i;
  306. int argv_size;
  307. int envp_size;
  308. ENTER_VOID();
  309. pid = fork();
  310. if (pid == -1) {
  311. fprintf (stderr, "Couldn't fork process %s\n", strerror (errno));
  312. return (0);
  313. }
  314. if (pid) {
  315. xprintf ("waiting for pid %d to finish\n", pid);
  316. waitpid (pid, &status, 0);
  317. if (WIFEXITED (status) != 0 && WEXITSTATUS(status) != 0) {
  318. fprintf (stderr, "Error: CLC_CLI (%d) failed with exit status:"
  319. " %d - %s\n", pid, WEXITSTATUS(status),
  320. strerror (WEXITSTATUS(status)));
  321. /*
  322. * TODO: remove this and handle properly later...
  323. */
  324. openais_exit_error (AIS_DONE_FATAL_ERR);
  325. }
  326. if (WIFSIGNALED (status) != 0) {
  327. fprintf (stderr, "Error: CLC_CLI (%d) failed with exit status:"
  328. " %d\n", pid, WTERMSIG(status));
  329. /*
  330. * TODO: remove this and handle properly later...
  331. */
  332. openais_exit_error (AIS_DONE_FATAL_ERR);
  333. }
  334. xprintf ("process (%d) finished with %x\n", pid, status);
  335. if (clc_command_run_data->completion_callback) {
  336. clc_command_run_data->completion_callback (context);
  337. }
  338. pthread_exit(0);
  339. }
  340. switch (clc_command_run_data->type) {
  341. case CLC_COMMAND_RUN_OPERATION_TYPE_INSTANTIATE:
  342. cmd = clc_command_run_data->comp->saAmfCompInstantiateCmd;
  343. comp_argv = clc_command_run_data->comp->saAmfCompInstantiateCmdArgv;
  344. break;
  345. case CLC_COMMAND_RUN_OPERATION_TYPE_TERMINATE:
  346. cmd = clc_command_run_data->comp->saAmfCompTerminateCmd;
  347. comp_argv = clc_command_run_data->comp->saAmfCompTerminateCmdArgv;
  348. break;
  349. case CLC_COMMAND_RUN_OPERATION_TYPE_CLEANUP:
  350. cmd = clc_command_run_data->comp->saAmfCompCleanupCmd;
  351. comp_argv = clc_command_run_data->comp->saAmfCompCleanupCmdArgv;
  352. break;
  353. default:
  354. assert (0 != 1);
  355. break;
  356. }
  357. /* If command is not an absolute path, search for paths in parent objects */
  358. if (cmd[0] != '/') {
  359. if (clc_command_run_data->comp->clccli_path != NULL) {
  360. sprintf (path, "%s/%s",
  361. clc_command_run_data->comp->clccli_path, cmd);
  362. } else if (clc_command_run_data->comp->su->clccli_path != NULL) {
  363. sprintf (path, "%s/%s",
  364. clc_command_run_data->comp->su->clccli_path, cmd);
  365. } else if (clc_command_run_data->comp->su->sg->clccli_path != NULL) {
  366. sprintf (path, "%s/%s",
  367. clc_command_run_data->comp->su->sg->clccli_path, cmd);
  368. } else if (clc_command_run_data->comp->su->sg->application->clccli_path != NULL) {
  369. sprintf (path, "%s/%s",
  370. clc_command_run_data->comp->su->sg->application->clccli_path, cmd);
  371. }
  372. cmd = path;
  373. }
  374. argv_size = 2;
  375. argv = amf_malloc (sizeof (char*) * argv_size);
  376. argv[0] = cmd;
  377. {
  378. /* make a proper argv array */
  379. i = 1;
  380. char *ptrptr;
  381. char *arg = strtok_r(comp_argv, " ", &ptrptr);
  382. while (arg) {
  383. argv_size++;
  384. argv = realloc (argv, sizeof (char*) * argv_size);
  385. if (argv == NULL) {
  386. fprintf (stderr, "out-of-memory");
  387. exit (-1);
  388. }
  389. argv[i] = arg;
  390. arg = strtok_r(NULL, " ", &ptrptr);
  391. i++;
  392. }
  393. }
  394. argv[i] = NULL;
  395. i = snprintf (comp_name, SA_MAX_NAME_LENGTH,
  396. "SA_AMF_COMPONENT_NAME=safComp=%s,safSu=%s,safSg=%s,safApp=%s",
  397. clc_command_run_data->comp->name.value,
  398. clc_command_run_data->comp->su->name.value,
  399. clc_command_run_data->comp->su->sg->name.value,
  400. clc_command_run_data->comp->su->sg->application->name.value);
  401. assert (i <= sizeof (comp_name));
  402. /* two is for component name and NULL termination */
  403. envp_size = 2;
  404. envp = amf_malloc (sizeof (char*) * envp_size);
  405. envp[0] = comp_name;
  406. for (i = 1; clc_command_run_data->comp->saAmfCompCmdEnv &&
  407. clc_command_run_data->comp->saAmfCompCmdEnv[i - 1]; i++) {
  408. envp_size++;
  409. envp = realloc (envp, sizeof (char*) * envp_size);
  410. if (envp == NULL) {
  411. fprintf (stderr, "out-of-memory");
  412. exit (-1);
  413. }
  414. envp[i] = clc_command_run_data->comp->saAmfCompCmdEnv[i - 1];
  415. }
  416. envp[i] = NULL;
  417. xprintf ("running command '%s' with environment (%d):\n", cmd, envp_size);
  418. for (i = 0; envp[i] != NULL; i++) {
  419. xprintf (" %s\n", envp[i]);
  420. }
  421. xprintf (" and argv (%d):\n", argv_size);
  422. for (i = 0; argv[i] != NULL; i++) {
  423. xprintf (" %s\n", argv[i]);
  424. }
  425. res = execve (cmd, argv, envp);
  426. if (res == -1) {
  427. fprintf (stderr, "Couldn't exec program %s (%s)\n",
  428. cmd, strerror (errno));
  429. }
  430. exit (res); /* abnormal exit of forked process */
  431. return (0);
  432. }
  433. static void amf_comp_instantiate_tmo (void *component)
  434. {
  435. SaNameT compName;
  436. amf_comp_dn_make (component, &compName);
  437. amf_msg_mcast (MESSAGE_REQ_EXEC_AMF_COMPONENT_INSTANTIATE_TMO,
  438. &compName, sizeof (SaNameT));
  439. }
  440. static void start_component_instantiate_timer (struct amf_comp *component)
  441. {
  442. poll_timer_add (aisexec_poll_handle,
  443. component->saAmfCompInstantiateTimeout,
  444. component,
  445. amf_comp_instantiate_tmo,
  446. &component->instantiate_timeout_handle);
  447. }
  448. /*
  449. * Instantiate possible operations
  450. */
  451. static int clc_cli_instantiate (struct amf_comp *comp)
  452. {
  453. int res;
  454. pthread_t thread;
  455. pthread_attr_t thread_attr; /* thread attribute */
  456. struct clc_command_run_data *clc_command_run_data;
  457. ENTER("comp '%s'\n", getSaNameT (&comp->name));
  458. clc_command_run_data = amf_malloc (sizeof (struct clc_command_run_data));
  459. clc_command_run_data->comp = comp;
  460. clc_command_run_data->type = CLC_COMMAND_RUN_OPERATION_TYPE_INSTANTIATE;
  461. clc_command_run_data->completion_callback = NULL;
  462. pthread_attr_init (&thread_attr);
  463. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  464. res = pthread_create (&thread, &thread_attr, clc_command_run,
  465. (void *)clc_command_run_data);
  466. if (res != 0) {
  467. log_printf (LOG_LEVEL_ERROR, "pthread_create failed: %d", res);
  468. }
  469. start_component_instantiate_timer (comp);
  470. // clc_command_run_data->completion_callback (clc_command_run_data);
  471. // TODO error code from pthread_create
  472. return (res);
  473. }
  474. static int clc_instantiate_callback (struct amf_comp *comp)
  475. {
  476. ENTER("comp %s\n", getSaNameT (&comp->name));
  477. return (0);
  478. }
  479. static int clc_csi_set_callback (struct amf_comp *comp)
  480. {
  481. ENTER("comp %s\n", getSaNameT (&comp->name));
  482. return (0);
  483. }
  484. /*
  485. * Terminate possible operations
  486. */
  487. static int clc_cli_terminate (struct amf_comp *comp)
  488. {
  489. ENTER("comp %s\n", getSaNameT (&comp->name));
  490. return (0);
  491. }
  492. /**
  493. * Request component to terminate itself
  494. * @param comp
  495. *
  496. * @return int
  497. */
  498. static int lib_comp_terminate_request (struct amf_comp *comp)
  499. {
  500. struct res_lib_amf_componentterminatecallback res_lib;
  501. struct component_terminate_callback_data *component_terminate_callback_data;
  502. ENTER("comp %s\n", getSaNameT (&comp->name));
  503. res_lib.header.id = MESSAGE_RES_AMF_COMPONENTTERMINATECALLBACK;
  504. res_lib.header.size = sizeof (struct res_lib_amf_componentterminatecallback);
  505. res_lib.header.error = SA_AIS_OK;
  506. memcpy (&res_lib.compName, &comp->name, sizeof (SaNameT));
  507. component_terminate_callback_data =
  508. amf_malloc (sizeof (struct component_terminate_callback_data));
  509. component_terminate_callback_data->comp = comp;
  510. res_lib.invocation =
  511. invocation_create (
  512. AMF_RESPONSE_COMPONENTTERMINATECALLBACK,
  513. component_terminate_callback_data);
  514. openais_conn_send_response (
  515. openais_conn_partner_get (comp->conn),
  516. &res_lib,
  517. sizeof (struct res_lib_amf_componentterminatecallback));
  518. return (0);
  519. }
  520. static int clc_csi_remove_callback (struct amf_comp *comp)
  521. {
  522. dprintf ("clc_tcsi_remove_callback\n");
  523. return (0);
  524. }
  525. /*
  526. * Clean up completed
  527. */
  528. static void mcast_cleanup_completion_event (void *context)
  529. {
  530. struct clc_command_run_data *clc_command_run_data =
  531. (struct clc_command_run_data *)context;
  532. struct req_exec_amf_clc_cleanup_completed req;
  533. struct iovec iovec;
  534. req.header.size = sizeof (struct req_exec_amf_clc_cleanup_completed);
  535. req.header.id = SERVICE_ID_MAKE (AMF_SERVICE,
  536. MESSAGE_REQ_EXEC_AMF_CLC_CLEANUP_COMPLETED);
  537. amf_comp_dn_make (clc_command_run_data->comp, &req.compName);
  538. iovec.iov_base = (char *)&req;
  539. iovec.iov_len = sizeof (req);
  540. assert (totempg_groups_mcast_joined (openais_group_handle,
  541. &iovec, 1, TOTEMPG_AGREED) == 0);
  542. }
  543. /*
  544. * Cleanup possible operations
  545. */
  546. static int clc_cli_cleanup (struct amf_comp *comp)
  547. {
  548. int res;
  549. pthread_t thread;
  550. pthread_attr_t thread_attr; /* thread attribute */
  551. struct clc_command_run_data *clc_command_run_data;
  552. dprintf ("clc_cli_cleanup\n");
  553. clc_command_run_data = amf_malloc (sizeof (struct clc_command_run_data));
  554. clc_command_run_data->comp = comp;
  555. clc_command_run_data->type = CLC_COMMAND_RUN_OPERATION_TYPE_CLEANUP;
  556. clc_command_run_data->completion_callback = mcast_cleanup_completion_event;
  557. pthread_attr_init (&thread_attr);
  558. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  559. res = pthread_create (&thread, &thread_attr, clc_command_run,
  560. (void *)clc_command_run_data);
  561. if (res != 0) {
  562. log_printf (LOG_LEVEL_ERROR, "pthread_create failed: %d", res);
  563. }
  564. // TODO error code from pthread_create
  565. return (res);
  566. }
  567. static int clc_cli_cleanup_local (struct amf_comp *comp)
  568. {
  569. dprintf ("clc_cli_cleanup_local\n");
  570. return (0);
  571. }
  572. #if 0
  573. static int clc_terminate (struct amf_comp *comp)
  574. {
  575. int res;
  576. dprintf ("clc terminate for comp %s\n", getSaNameT (&comp->name));
  577. assert (0);
  578. operational_state_comp_set (comp, SA_AMF_OPERATIONAL_DISABLED);
  579. comp_presence_state_set (comp, SA_AMF_PRESENCE_TERMINATING);
  580. res = clc_interfaces[comp->comptype]->terminate (comp);
  581. return (0);
  582. }
  583. #endif
  584. char *amf_comp_dn_make (struct amf_comp *comp, SaNameT *name)
  585. {
  586. int i = snprintf ((char*) name->value, SA_MAX_NAME_LENGTH,
  587. "safComp=%s,safSu=%s,safSg=%s,safApp=%s",
  588. comp->name.value, comp->su->name.value,
  589. comp->su->sg->name.value, comp->su->sg->application->name.value);
  590. assert (i <= SA_MAX_NAME_LENGTH);
  591. name->length = i;
  592. return (char *)name->value;
  593. }
  594. struct amf_healthcheck *amf_comp_find_healthcheck (
  595. struct amf_comp *comp, SaAmfHealthcheckKeyT *key)
  596. {
  597. struct amf_healthcheck *healthcheck;
  598. struct amf_healthcheck *ret_healthcheck = 0;
  599. if (key == NULL) {
  600. return NULL;
  601. }
  602. for (healthcheck = comp->healthcheck_head;
  603. healthcheck != NULL;
  604. healthcheck = healthcheck->next) {
  605. if (key->keyLen == healthcheck->safHealthcheckKey.keyLen &&
  606. memcmp (key, &healthcheck->safHealthcheckKey,key->keyLen) == 0) {
  607. ret_healthcheck = healthcheck;
  608. break;
  609. }
  610. }
  611. return (ret_healthcheck);
  612. }
  613. /**
  614. * Constructor for component objects. Adds component last in
  615. * the list owned by the specified SU. Always returns a
  616. * valid comp object, out-of-memory problems are handled
  617. * here. Default values are initialized.
  618. * @param su
  619. * @param name
  620. *
  621. * @return struct amf_comp*
  622. */
  623. struct amf_comp *amf_comp_new(struct amf_su *su, char *name)
  624. {
  625. struct amf_comp *tail = su->comp_head;
  626. struct amf_comp *comp = amf_calloc (1, sizeof (struct amf_comp));
  627. while (tail != NULL) {
  628. if (tail->next == NULL) {
  629. break;
  630. }
  631. tail = tail->next;
  632. }
  633. if (tail == NULL) {
  634. su->comp_head = comp;
  635. } else {
  636. tail->next = comp;
  637. }
  638. comp->su = su;
  639. /* setup default values from spec. */
  640. comp->saAmfCompNumMaxInstantiateWithoutDelay = 2;
  641. comp->saAmfCompNumMaxAmStartAttempt = 2;
  642. comp->saAmfCompNumMaxAmStopAttempt = 2;
  643. comp->saAmfCompOperState = SA_AMF_OPERATIONAL_DISABLED;
  644. comp->saAmfCompPresenceState = SA_AMF_PRESENCE_UNINSTANTIATED;
  645. setSaNameT (&comp->name, name);
  646. return comp;
  647. }
  648. void amf_comp_delete (struct amf_comp *comp)
  649. {
  650. int i;
  651. struct amf_healthcheck *healthcheck;
  652. for (healthcheck = comp->healthcheck_head; healthcheck != NULL;) {
  653. struct amf_healthcheck *tmp = healthcheck;
  654. healthcheck = healthcheck->next;
  655. free (tmp);
  656. }
  657. for (i = 0; comp->saAmfCompCsTypes[i] != NULL; i++) {
  658. free (comp->saAmfCompCsTypes[i]);
  659. }
  660. for (i = 0; comp->saAmfCompCmdEnv[i] != NULL; i++) {
  661. free (comp->saAmfCompCmdEnv[i]);
  662. }
  663. free (comp->saAmfCompInstantiateCmd);
  664. free (comp->saAmfCompInstantiateCmdArgv);
  665. free (comp->saAmfCompTerminateCmd);
  666. free (comp->saAmfCompTerminateCmdArgv);
  667. free (comp->saAmfCompCleanupCmd);
  668. free (comp->saAmfCompCleanupCmdArgv);
  669. free (comp->saAmfCompAmStartCmd);
  670. free (comp->saAmfCompAmStartCmdArgv);
  671. free (comp->saAmfCompAmStopCmd);
  672. free (comp->saAmfCompAmStopCmdArgv);
  673. free (comp->clccli_path);
  674. free (comp);
  675. }
  676. struct amf_comp *amf_comp_find (struct amf_cluster *cluster, SaNameT *name)
  677. {
  678. struct amf_application *app;
  679. struct amf_sg *sg;
  680. struct amf_su *su;
  681. struct amf_comp *comp = NULL;
  682. char *app_name;
  683. char *sg_name;
  684. char *su_name;
  685. char *comp_name;
  686. char *ptrptr;
  687. char *buf;
  688. assert (cluster != NULL && name != NULL);
  689. /* malloc new buffer since strtok_r writes to its first argument */
  690. buf = amf_malloc (name->length + 1);
  691. memcpy (buf, name->value,name ->length + 1);
  692. comp_name = strtok_r(buf, ",", &ptrptr);
  693. su_name = strtok_r(NULL, ",", &ptrptr);
  694. sg_name = strtok_r(NULL, ",", &ptrptr);
  695. app_name = strtok_r(NULL, ",", &ptrptr);
  696. if (comp_name == NULL || su_name == NULL ||
  697. sg_name == NULL || app_name == NULL) {
  698. goto end;
  699. }
  700. comp_name += 8;
  701. su_name += 6;
  702. sg_name += 6;
  703. app_name += 7;
  704. app = amf_application_find (cluster, app_name);
  705. if (app == NULL) {
  706. goto end;
  707. }
  708. sg = amf_sg_find (app, sg_name);
  709. if (sg == NULL) {
  710. goto end;
  711. }
  712. for (su = sg->su_head; su != NULL; su = su->next) {
  713. if (strncmp (su_name, (char*)su->name.value, su->name.length) == 0) {
  714. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  715. if (comp->name.length == strlen(comp_name) &&
  716. strncmp (comp_name, (char*)comp->name.value,
  717. comp->name.length) == 0) {
  718. goto end;
  719. }
  720. }
  721. }
  722. }
  723. end:
  724. free (buf);
  725. return comp;
  726. }
  727. void amf_comp_healthcheck_deactivate (struct amf_comp *comp)
  728. {
  729. struct amf_healthcheck *healthcheck;
  730. if (!amf_su_is_local (comp->su))
  731. return;
  732. ENTER ("'%s'\n", getSaNameT (&comp->name));
  733. for (healthcheck = comp->healthcheck_head;
  734. healthcheck != NULL;
  735. healthcheck = healthcheck->next) {
  736. if (healthcheck->active) {
  737. healthcheck_deactivate (healthcheck);
  738. }
  739. }
  740. }
  741. static void comp_ha_state_set ( struct amf_comp *comp,
  742. struct amf_csi_assignment *csi_assignment,
  743. SaAmfHAStateT ha_state)
  744. {
  745. /* set confirmed HA state */
  746. csi_assignment->saAmfCSICompHAState = ha_state;
  747. TRACE1 ("Setting comp '%s', SU '%s' CSI '%s', HA state: %s\n",
  748. comp->name.value, comp->su->name.value,
  749. csi_assignment->csi->name.value,
  750. amf_ha_state (csi_assignment->saAmfCSICompHAState));
  751. amf_si_comp_set_ha_state_done (csi_assignment->csi->si, csi_assignment);
  752. }
  753. static void comp_presence_state_set (struct amf_comp *comp,
  754. SaAmfPresenceStateT presence_state)
  755. {
  756. comp->saAmfCompPresenceState = presence_state;
  757. TRACE1 ("Setting comp '%s', SU '%s' presence state: %s\n",
  758. comp->name.value, comp->su->name.value,
  759. amf_presence_state (comp->saAmfCompPresenceState));
  760. amf_su_comp_state_changed (
  761. comp->su, comp, SA_AMF_PRESENCE_STATE, presence_state);
  762. }
  763. struct amf_csi_assignment *amf_comp_get_next_csi_assignment (
  764. struct amf_comp *component,
  765. const struct amf_csi_assignment *csi_assignment)
  766. {
  767. struct amf_si *si;
  768. struct amf_csi *csi;
  769. struct amf_csi_assignment *tmp_csi_assignment;
  770. SaNameT dn;
  771. amf_comp_dn_make (component, &dn);
  772. if (csi_assignment == NULL) {
  773. si = component->su->sg->application->si_head;
  774. csi = si->csi_head;
  775. tmp_csi_assignment = csi->assigned_csis;
  776. } else {
  777. tmp_csi_assignment = csi_assignment->next;
  778. if (tmp_csi_assignment == NULL) {
  779. csi = csi_assignment->csi->next;
  780. if (csi == NULL) {
  781. si = csi_assignment->csi->si->next;
  782. if (si == NULL) {
  783. return NULL;
  784. } else {
  785. csi = si->csi_head;
  786. tmp_csi_assignment = csi->assigned_csis;
  787. }
  788. } else {
  789. si = csi->si;
  790. tmp_csi_assignment = csi->assigned_csis;
  791. }
  792. } else {
  793. csi = tmp_csi_assignment->csi;
  794. si = csi->si;
  795. }
  796. }
  797. for (; si != NULL; si = si->next) {
  798. if (tmp_csi_assignment == NULL && csi == NULL && si != NULL) {
  799. csi = si->csi_head;
  800. tmp_csi_assignment = csi->assigned_csis;
  801. }
  802. for (; csi != NULL; csi = csi->next) {
  803. if (tmp_csi_assignment == NULL && csi != NULL) {
  804. tmp_csi_assignment = csi->assigned_csis;
  805. }
  806. for (; tmp_csi_assignment != NULL;
  807. tmp_csi_assignment = tmp_csi_assignment->next) {
  808. if (name_match (&tmp_csi_assignment->name, &dn)) {
  809. return tmp_csi_assignment;
  810. }
  811. }
  812. }
  813. }
  814. return NULL;
  815. }
  816. void amf_comp_foreach_csi_assignment (
  817. struct amf_comp *component,
  818. void (*foreach_fn) (struct amf_comp *component,
  819. struct amf_csi_assignment *csi_assignment))
  820. {
  821. struct amf_csi_assignment *csi_assignment;
  822. assert (foreach_fn != NULL);
  823. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  824. while (csi_assignment != NULL) {
  825. foreach_fn (component, csi_assignment);
  826. csi_assignment = amf_comp_get_next_csi_assignment (
  827. component, csi_assignment);
  828. }
  829. }
  830. static struct amf_csi_assignment *csi_assignment_find_in (
  831. struct amf_comp *component, SaNameT *csi_name)
  832. {
  833. struct amf_csi_assignment *csi_assignment;
  834. SaNameT dn;
  835. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  836. while (csi_assignment != NULL) {
  837. amf_csi_dn_make (csi_assignment->csi, &dn);
  838. if (name_match (csi_name, &dn)) {
  839. return csi_assignment;
  840. }
  841. csi_assignment = amf_comp_get_next_csi_assignment (
  842. component, csi_assignment);
  843. }
  844. return NULL;
  845. }
  846. static void healthcheck_deactivate (
  847. struct amf_healthcheck *healthcheck_active)
  848. {
  849. dprintf ("deactivating healthcheck for component %s\n",
  850. getSaNameT (&healthcheck_active->comp->name));
  851. poll_timer_delete (aisexec_poll_handle,
  852. healthcheck_active->timer_handle_period);
  853. poll_timer_delete (aisexec_poll_handle,
  854. healthcheck_active->timer_handle_duration);
  855. invocation_destroy_by_data ((void *)healthcheck_active);
  856. healthcheck_active->active = 0;
  857. }
  858. /**
  859. * This function is called by the timer subsystem when AMF should request
  860. * a new healthcheck from a component.
  861. * @param data
  862. */
  863. static void timer_function_healthcheck_next_fn (void *_healthcheck)
  864. {
  865. struct amf_healthcheck *healthcheck = _healthcheck;
  866. /* send healthcheck request to component */
  867. lib_healthcheck_request (healthcheck);
  868. /* start duration timer for response */
  869. poll_timer_add (aisexec_poll_handle,
  870. healthcheck->saAmfHealthcheckMaxDuration,
  871. (void *)healthcheck,
  872. timer_function_healthcheck_tmo,
  873. &healthcheck->timer_handle_duration);
  874. }
  875. /**
  876. * Multicast a healthcheck timeout event.
  877. * @param healthcheck
  878. */
  879. static void mcast_healthcheck_tmo_event (
  880. struct amf_healthcheck *healthcheck)
  881. {
  882. struct req_exec_amf_healthcheck_tmo req_exec;
  883. struct iovec iovec;
  884. req_exec.header.size = sizeof (struct req_exec_amf_healthcheck_tmo);
  885. req_exec.header.id = SERVICE_ID_MAKE (AMF_SERVICE,
  886. MESSAGE_REQ_EXEC_AMF_HEALTHCHECK_TMO);
  887. amf_comp_dn_make (healthcheck->comp, &req_exec.compName);
  888. memcpy (&req_exec.safHealthcheckKey,
  889. &healthcheck->safHealthcheckKey, sizeof (SaAmfHealthcheckKeyT));
  890. iovec.iov_base = (char *)&req_exec;
  891. iovec.iov_len = sizeof (req_exec);
  892. assert (totempg_groups_mcast_joined (openais_group_handle,
  893. &iovec, 1, TOTEMPG_AGREED) == 0);
  894. }
  895. /**
  896. * This function is called by the timer subsystem when a component has not
  897. * performed a healthcheck on time.
  898. * The event is multicasted to the cluster.
  899. * @param data
  900. */
  901. static void timer_function_healthcheck_tmo (
  902. void *_healthcheck)
  903. {
  904. struct amf_healthcheck *healthcheck = (struct amf_healthcheck *)_healthcheck;
  905. TRACE2 ("timeout occured on healthcheck for component %s.\n",
  906. getSaNameT (&healthcheck->comp->name));
  907. mcast_healthcheck_tmo_event (healthcheck);
  908. }
  909. static void lib_healthcheck_request (struct amf_healthcheck *healthcheck)
  910. {
  911. struct res_lib_amf_healthcheckcallback res_lib;
  912. res_lib.header.id = MESSAGE_RES_AMF_HEALTHCHECKCALLBACK;
  913. res_lib.header.size = sizeof (struct res_lib_amf_healthcheckcallback);
  914. res_lib.header.error = SA_AIS_OK;
  915. res_lib.invocation =
  916. invocation_create (AMF_RESPONSE_HEALTHCHECKCALLBACK, healthcheck);
  917. amf_comp_dn_make (healthcheck->comp, &res_lib.compName);
  918. memcpy (&res_lib.key, &healthcheck->safHealthcheckKey,
  919. sizeof (SaAmfHealthcheckKeyT));
  920. TRACE7 ("sending healthcheck request to component %s",
  921. res_lib.compName.value);
  922. openais_conn_send_response (
  923. openais_conn_partner_get (healthcheck->comp->conn),
  924. &res_lib, sizeof (struct res_lib_amf_healthcheckcallback));
  925. }
  926. static void lib_csi_set_request (
  927. struct amf_comp *comp,
  928. struct amf_csi_assignment *csi_assignment)
  929. {
  930. struct res_lib_amf_csisetcallback* res_lib;
  931. void* p;
  932. struct amf_csi_attribute *attribute;
  933. size_t char_length_of_csi_attrs=0;
  934. size_t num_of_csi_attrs=0;
  935. int i;
  936. struct amf_csi *csi;
  937. char* csi_attribute_buf;
  938. unsigned int byte_offset;
  939. if (!amf_su_is_local (comp->su))
  940. return;
  941. csi = csi_assignment->csi;
  942. ENTER ("Assigning CSI '%s' state %s to comp '%s'\n",
  943. getSaNameT (&csi->name),
  944. amf_ha_state (csi_assignment->requested_ha_state),
  945. comp->name.value);
  946. for (attribute = csi->attributes_head;
  947. attribute != NULL;
  948. attribute = attribute->next) {
  949. for (i = 0; attribute->value[i] != NULL; i++) {
  950. num_of_csi_attrs++;
  951. char_length_of_csi_attrs += strlen(attribute->name);
  952. char_length_of_csi_attrs += strlen(attribute->value[i]);
  953. char_length_of_csi_attrs += 2;
  954. }
  955. }
  956. p = amf_malloc(sizeof(struct res_lib_amf_csisetcallback) +
  957. char_length_of_csi_attrs);
  958. res_lib = (struct res_lib_amf_csisetcallback*)p;
  959. /* Address of the buffer containing the Csi name value pair */
  960. csi_attribute_buf = res_lib->csi_attr_buf;
  961. /* Byteoffset start at the zero byte */
  962. byte_offset = 0;
  963. for (attribute = csi->attributes_head;
  964. attribute != NULL;
  965. attribute = attribute->next) {
  966. for (i = 0; attribute->value[i] != NULL; i++) {
  967. strcpy(&csi_attribute_buf[byte_offset], (char*)attribute->name);
  968. byte_offset += strlen(attribute->name) + 1;
  969. strcpy(&csi_attribute_buf[byte_offset], (char*)attribute->value[i]);
  970. byte_offset += strlen(attribute->value[i]) + 1;
  971. }
  972. }
  973. res_lib->number = num_of_csi_attrs;
  974. res_lib->csiFlags = SA_AMF_CSI_ADD_ONE;
  975. switch (csi_assignment->requested_ha_state) {
  976. case SA_AMF_HA_ACTIVE: {
  977. res_lib->csiStateDescriptor.activeDescriptor.activeCompName.length = 0;
  978. res_lib->csiStateDescriptor.activeDescriptor.transitionDescriptor =
  979. SA_AMF_CSI_NEW_ASSIGN;
  980. break;
  981. }
  982. case SA_AMF_HA_STANDBY: {
  983. res_lib->csiStateDescriptor.standbyDescriptor.activeCompName.length = 0;
  984. res_lib->csiStateDescriptor.standbyDescriptor.standbyRank = 1;
  985. break;
  986. }
  987. case SA_AMF_HA_QUIESCED: {
  988. /*TODO*/
  989. break;
  990. }
  991. case SA_AMF_HA_QUIESCING: {
  992. /*TODO*/
  993. break;
  994. }
  995. default: {
  996. assert(SA_AMF_HA_ACTIVE||SA_AMF_HA_STANDBY||SA_AMF_HA_QUIESCING||SA_AMF_HA_QUIESCED);
  997. break;
  998. }
  999. }
  1000. res_lib->header.id = MESSAGE_RES_AMF_CSISETCALLBACK;
  1001. res_lib->header.size =
  1002. sizeof (struct res_lib_amf_csisetcallback) +
  1003. char_length_of_csi_attrs;
  1004. res_lib->header.error = SA_AIS_OK;
  1005. amf_comp_dn_make (comp, &res_lib->compName);
  1006. amf_csi_dn_make (csi, &res_lib->csiName);
  1007. res_lib->haState = csi_assignment->requested_ha_state;
  1008. res_lib->invocation =
  1009. invocation_create (AMF_RESPONSE_CSISETCALLBACK, csi_assignment);
  1010. openais_conn_send_response (
  1011. openais_conn_partner_get (comp->conn), res_lib, res_lib->header.size);
  1012. free(p);
  1013. }
  1014. static void stop_component_instantiate_timer (struct amf_comp *component)
  1015. {
  1016. if (component->instantiate_timeout_handle) {
  1017. dprintf ("Stop component instantiate timer");
  1018. poll_timer_delete (aisexec_poll_handle,
  1019. component->instantiate_timeout_handle);
  1020. component->instantiate_timeout_handle = 0;
  1021. }
  1022. }
  1023. SaAisErrorT amf_comp_register (struct amf_comp *comp)
  1024. {
  1025. TRACE2("Exec comp register '%s'", comp->name.value);
  1026. stop_component_instantiate_timer (comp);
  1027. switch (comp->saAmfCompPresenceState) {
  1028. case SA_AMF_PRESENCE_RESTARTING:
  1029. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATED);
  1030. break;
  1031. case SA_AMF_PRESENCE_INSTANTIATING:
  1032. amf_comp_operational_state_set (comp, SA_AMF_OPERATIONAL_ENABLED);
  1033. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATED);
  1034. break;
  1035. case SA_AMF_PRESENCE_INSTANTIATION_FAILED:
  1036. /* ignore due to instantitate timeout a while ago */
  1037. break;
  1038. default:
  1039. assert (0);
  1040. break;
  1041. }
  1042. return SA_AIS_OK;
  1043. }
  1044. void amf_comp_error_report (struct amf_comp *comp, SaAmfRecommendedRecoveryT recommendedRecovery)
  1045. {
  1046. struct res_lib_amf_componenterrorreport res_lib;
  1047. TRACE2("Exec comp error report '%s'", comp->name.value);
  1048. if (amf_su_is_local (comp->su)) {
  1049. res_lib.header.size = sizeof (struct res_lib_amf_componenterrorreport);
  1050. res_lib.header.id = MESSAGE_RES_AMF_COMPONENTERRORREPORT;
  1051. res_lib.header.error = SA_AIS_OK;
  1052. openais_conn_send_response (comp->conn, &res_lib, sizeof (res_lib));
  1053. }
  1054. /* report to SU and let it handle the problem */
  1055. report_error_suspected (comp, recommendedRecovery);
  1056. }
  1057. /**
  1058. * Healthcheck timeout event handler
  1059. * @param comp
  1060. * @param healthcheck
  1061. */
  1062. void amf_comp_healthcheck_tmo (
  1063. struct amf_comp *comp, struct amf_healthcheck *healthcheck)
  1064. {
  1065. TRACE2("Exec healthcheck tmo for '%s'", comp->name.value);
  1066. /* report to SU and let it handle the problem */
  1067. report_error_suspected (comp, healthcheck->recommendedRecovery);
  1068. }
  1069. static void clear_ha_state (
  1070. struct amf_comp *comp, struct amf_csi_assignment *csi_assignment)
  1071. {
  1072. ENTER ("");
  1073. csi_assignment->saAmfCSICompHAState = 0;
  1074. }
  1075. /**
  1076. * Event method to be called when a cleanup completed event is received
  1077. * @param comp
  1078. */
  1079. void amf_comp_cleanup_completed (struct amf_comp *comp)
  1080. {
  1081. TRACE2("Exec CLC cleanup completed for '%s'", comp->name.value);
  1082. /* Set all CSI's confirmed HA state to unknown */
  1083. amf_comp_foreach_csi_assignment (comp, clear_ha_state);
  1084. /* clear error suspected flag, component is terminated now */
  1085. comp->error_suspected = 0;
  1086. if (comp->saAmfCompPresenceState == SA_AMF_PRESENCE_RESTARTING) {
  1087. amf_comp_instantiate (comp);
  1088. } else {
  1089. comp_presence_state_set (comp, SA_AMF_PRESENCE_UNINSTANTIATED);
  1090. }
  1091. }
  1092. /**
  1093. * Handle the request from a component to start a healthcheck
  1094. *
  1095. * @param comp
  1096. * @param healthcheckKey
  1097. * @param invocationType
  1098. * @param recommendedRecovery
  1099. *
  1100. * @return SaAisErrorT - return value to component
  1101. */
  1102. SaAisErrorT amf_comp_healthcheck_start (
  1103. struct amf_comp *comp,
  1104. SaAmfHealthcheckKeyT *healthcheckKey,
  1105. SaAmfHealthcheckInvocationT invocationType,
  1106. SaAmfRecommendedRecoveryT recommendedRecovery)
  1107. {
  1108. struct amf_healthcheck *healthcheck;
  1109. SaAisErrorT error = SA_AIS_OK;
  1110. healthcheck = amf_comp_find_healthcheck (comp, healthcheckKey);
  1111. if (healthcheck == 0) {
  1112. log_printf (LOG_ERR, "Healthcheckstart: Healthcheck '%s' not found",
  1113. healthcheckKey->key);
  1114. error = SA_AIS_ERR_NOT_EXIST;
  1115. goto error_exit;
  1116. }
  1117. dprintf ("Healthcheckstart: '%s', key '%s'",
  1118. comp->name.value, healthcheckKey->key);
  1119. /*
  1120. * Determine if this healthcheck is already active
  1121. */
  1122. if (healthcheck->active) {
  1123. error = SA_AIS_ERR_EXIST;
  1124. goto error_exit;
  1125. }
  1126. /*
  1127. * Initialise
  1128. */
  1129. healthcheck->invocationType = invocationType;
  1130. healthcheck->recommendedRecovery = recommendedRecovery;
  1131. healthcheck->timer_handle_duration = 0;
  1132. healthcheck->timer_handle_period = 0;
  1133. healthcheck->active = 1;
  1134. if (invocationType == SA_AMF_HEALTHCHECK_AMF_INVOKED) {
  1135. /* start timer to execute first healthcheck request */
  1136. poll_timer_add (aisexec_poll_handle,
  1137. healthcheck->saAmfHealthcheckPeriod,
  1138. (void *)healthcheck,
  1139. timer_function_healthcheck_next_fn,
  1140. &healthcheck->timer_handle_period);
  1141. } else if (invocationType == SA_AMF_HEALTHCHECK_COMPONENT_INVOKED) {
  1142. /* start supervision timer */
  1143. poll_timer_add (aisexec_poll_handle,
  1144. healthcheck->saAmfHealthcheckPeriod,
  1145. (void *)healthcheck,
  1146. timer_function_healthcheck_tmo,
  1147. &healthcheck->timer_handle_period);
  1148. } else {
  1149. error = SA_AIS_ERR_INVALID_PARAM;
  1150. }
  1151. error_exit:
  1152. return error;
  1153. }
  1154. /**
  1155. * Stop all or a specifed healthcheck
  1156. * @param comp
  1157. * @param healthcheckKey - NULL if all
  1158. *
  1159. * @return SaAisErrorT
  1160. */
  1161. SaAisErrorT amf_comp_healthcheck_stop (
  1162. struct amf_comp *comp,
  1163. SaAmfHealthcheckKeyT *healthcheckKey)
  1164. {
  1165. struct amf_healthcheck *healthcheck;
  1166. SaAisErrorT error = SA_AIS_OK;
  1167. dprintf ("Healthcheckstop: '%s'", comp->name.value);
  1168. if (!amf_su_is_local (comp->su)) {
  1169. return SA_AIS_OK;
  1170. }
  1171. if (healthcheckKey == NULL) {
  1172. for (healthcheck = comp->healthcheck_head;
  1173. healthcheck != NULL;
  1174. healthcheck = healthcheck->next) {
  1175. healthcheck_deactivate (healthcheck);
  1176. }
  1177. } else {
  1178. healthcheck = amf_comp_find_healthcheck (comp, healthcheckKey);
  1179. if (healthcheck == NULL) {
  1180. log_printf (LOG_ERR, "Healthcheckstop: Healthcheck '%s' not found",
  1181. healthcheckKey->key);
  1182. error = SA_AIS_ERR_NOT_EXIST;
  1183. } else {
  1184. healthcheck_deactivate (healthcheck);
  1185. }
  1186. }
  1187. return error;
  1188. }
  1189. /**
  1190. * Instantiate a component
  1191. * @param comp
  1192. */
  1193. void amf_comp_instantiate (struct amf_comp *comp)
  1194. {
  1195. ENTER ("'%s' SU '%s'", getSaNameT (&comp->name),
  1196. getSaNameT (&comp->su->name));
  1197. switch (comp->saAmfCompPresenceState) {
  1198. case SA_AMF_PRESENCE_RESTARTING:
  1199. /* fall through */
  1200. case SA_AMF_PRESENCE_UNINSTANTIATED:
  1201. if (amf_su_is_local (comp->su)) {
  1202. TRACE1("Send instantiate event for comp '%s' from host %s",
  1203. comp->name.value, comp->su->saAmfSUHostedByNode.value);
  1204. SaNameT compName;
  1205. amf_comp_dn_make (comp, &compName);
  1206. amf_msg_mcast (MESSAGE_REQ_EXEC_AMF_COMPONENT_INSTANTIATE,
  1207. &compName, sizeof (SaNameT));
  1208. }
  1209. break;
  1210. default:
  1211. dprintf("Instantiate ignored in Component presence state %d",
  1212. comp->saAmfCompPresenceState);
  1213. break;
  1214. }
  1215. }
  1216. void amf_comp_instantiate_tmo_event (struct amf_comp *comp)
  1217. {
  1218. ENTER ("Comp instantiate timeout after %d ms '%s' '%s'",
  1219. comp->saAmfCompInstantiateTimeout, comp->su->name.value,
  1220. comp->name.value);
  1221. switch (comp->saAmfCompPresenceState) {
  1222. case SA_AMF_PRESENCE_RESTARTING:
  1223. amf_comp_operational_state_set (comp, SA_AMF_OPERATIONAL_DISABLED);
  1224. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATION_FAILED);
  1225. break;
  1226. case SA_AMF_PRESENCE_INSTANTIATING:
  1227. amf_comp_operational_state_set (comp, SA_AMF_OPERATIONAL_DISABLED);
  1228. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATION_FAILED);
  1229. break;
  1230. case SA_AMF_PRESENCE_INSTANTIATED:
  1231. assert (comp->instantiate_timeout_handle == 0);
  1232. break;
  1233. default:
  1234. dprintf("Presence state = %d", comp->saAmfCompPresenceState);
  1235. assert (0);
  1236. break;
  1237. }
  1238. }
  1239. void amf_comp_instantiate_event (struct amf_comp *component)
  1240. {
  1241. int res;
  1242. ENTER ("");
  1243. switch (component->saAmfCompPresenceState) {
  1244. case SA_AMF_PRESENCE_INSTANTIATING:
  1245. case SA_AMF_PRESENCE_INSTANTIATED:
  1246. case SA_AMF_PRESENCE_TERMINATING:
  1247. case SA_AMF_PRESENCE_INSTANTIATION_FAILED:
  1248. case SA_AMF_PRESENCE_TERMINATION_FAILED:
  1249. dprintf("Instantiate ignored in Component presence state %d",
  1250. component->saAmfCompPresenceState);
  1251. break;
  1252. case SA_AMF_PRESENCE_UNINSTANTIATED:
  1253. comp_presence_state_set (component, SA_AMF_PRESENCE_INSTANTIATING);
  1254. amf_su_comp_state_changed(component->su,
  1255. component,SA_AMF_PRESENCE_STATE,SA_AMF_PRESENCE_INSTANTIATING);
  1256. if (amf_su_is_local (component->su)) {
  1257. res = clc_interfaces[component->comptype]->instantiate (
  1258. component);
  1259. }
  1260. break;
  1261. case SA_AMF_PRESENCE_RESTARTING:
  1262. if (amf_su_is_local (component->su)) {
  1263. res = clc_interfaces[component->comptype]->instantiate (
  1264. component);
  1265. }
  1266. break;
  1267. default:
  1268. dprintf("Component presence state %d",
  1269. component->saAmfCompPresenceState);
  1270. assert (0);
  1271. break;
  1272. }
  1273. }
  1274. void amf_comp_readiness_state_set (struct amf_comp *comp,
  1275. SaAmfReadinessStateT state)
  1276. {
  1277. TRACE1 ("Setting comp '%s' readiness state: %s\n",
  1278. comp->name.value, amf_readiness_state (state));
  1279. }
  1280. /**
  1281. * Handle a component response (received from the lib) of an earlier AMF request.
  1282. * This function should be invoked when the lib request is received.
  1283. * @param invocation [in] associates the response with the request (callback)
  1284. * @param error [in] response from the component of the associated callback
  1285. * @param retval [out] contains return value to component when needed
  1286. *
  1287. * @return ==0 respond to component, do not multicast
  1288. * @return >0 do not respond to component, multicast response
  1289. */
  1290. int amf_comp_response_1 (
  1291. SaInvocationT invocation, SaAisErrorT error, SaAisErrorT *retval,
  1292. SaUint32T *interface, SaNameT *dn)
  1293. {
  1294. int res;
  1295. void *data;
  1296. res = invocation_get_and_destroy (invocation, interface, &data);
  1297. if (res == -1) {
  1298. log_printf (LOG_ERR, "Lib response: invocation not found\n");
  1299. *retval = SA_AIS_ERR_INVALID_PARAM;
  1300. return 0;
  1301. }
  1302. switch (*interface) {
  1303. case AMF_RESPONSE_HEALTHCHECKCALLBACK: {
  1304. struct amf_healthcheck *healthcheck = data;
  1305. SaNameT name;
  1306. TRACE7 ("Healthcheck response from '%s': %d",
  1307. amf_comp_dn_make (healthcheck->comp, &name), error);
  1308. if (healthcheck->invocationType == SA_AMF_HEALTHCHECK_AMF_INVOKED) {
  1309. /* the response was on time, delete supervision timer */
  1310. poll_timer_delete (aisexec_poll_handle,
  1311. healthcheck->timer_handle_duration);
  1312. healthcheck->timer_handle_duration = 0;
  1313. /* start timer to execute next healthcheck request */
  1314. poll_timer_add (aisexec_poll_handle,
  1315. healthcheck->saAmfHealthcheckPeriod,
  1316. (void *)healthcheck,
  1317. timer_function_healthcheck_next_fn,
  1318. &healthcheck->timer_handle_period);
  1319. *retval = SA_AIS_OK;
  1320. } else {
  1321. *retval = SA_AIS_ERR_INVALID_PARAM;
  1322. }
  1323. return 0; /* do not multicast event */
  1324. break;
  1325. }
  1326. case AMF_RESPONSE_CSISETCALLBACK: /* fall-through */
  1327. case AMF_RESPONSE_CSIREMOVECALLBACK:
  1328. amf_csi_assignment_dn_make (data, dn);
  1329. return 1; /* multicast event */
  1330. break;
  1331. #if 0
  1332. case AMF_RESPONSE_COMPONENTTERMINATECALLBACK: {
  1333. struct component_terminate_callback_data *component_terminate_callback_data;
  1334. component_terminate_callback_data = data;
  1335. dprintf ("Lib component terminate callback response, error: %d", error);
  1336. amf_comp_healthcheck_deactivate (component_terminate_callback_data->comp);
  1337. escalation_policy_restart (component_terminate_callback_data->comp);
  1338. return 1;
  1339. break;
  1340. }
  1341. #endif
  1342. default:
  1343. assert (0);
  1344. break;
  1345. }
  1346. /* XXX we fall here in case NDEBUG is set */
  1347. *retval = -1;
  1348. return 0;
  1349. }
  1350. /**
  1351. * Handle a component response (received from EVS) of an earlier AMF request.
  1352. * This function should be invoked when the multicast request is received.
  1353. * @param invocation [in] associates the response with the request (callback)
  1354. * @param error [in] response from the component of the associated callback
  1355. * @param retval [out] contains return value to component when needed
  1356. *
  1357. * @return component to which the response should be sent
  1358. */
  1359. struct amf_comp *amf_comp_response_2 (
  1360. SaUint32T interface, SaNameT *dn, SaAisErrorT error, SaAisErrorT *retval)
  1361. {
  1362. struct amf_csi_assignment *csi_assignment;
  1363. struct amf_comp *comp = NULL;
  1364. assert (retval != NULL);
  1365. *retval = SA_AIS_OK;
  1366. switch (interface) {
  1367. case AMF_RESPONSE_CSISETCALLBACK: {
  1368. ENTER("'%s'", dn->value);
  1369. csi_assignment = amf_csi_assignment_find (amf_cluster, dn);
  1370. assert (csi_assignment != NULL);
  1371. comp = csi_assignment->comp;
  1372. dprintf ("CSI '%s' set callback response from '%s', error: %d",
  1373. csi_assignment->csi->name.value,
  1374. csi_assignment->comp->name.value, error);
  1375. comp = csi_assignment->comp;
  1376. if (error == SA_AIS_OK) {
  1377. comp_ha_state_set (
  1378. comp, csi_assignment, csi_assignment->requested_ha_state);
  1379. } else if (error == SA_AIS_ERR_FAILED_OPERATION) {
  1380. amf_si_comp_set_ha_state_failed (csi_assignment->csi->si,
  1381. csi_assignment);
  1382. } else {
  1383. *retval = SA_AIS_ERR_INVALID_PARAM;
  1384. }
  1385. break;
  1386. }
  1387. case AMF_RESPONSE_CSIREMOVECALLBACK: {
  1388. ENTER("'%s'", dn->value);
  1389. csi_assignment = amf_csi_assignment_find (amf_cluster, dn);
  1390. assert (csi_assignment != NULL);
  1391. dprintf ("Lib csi '%s' remove callback response from '%s', error: %d",
  1392. csi_assignment->csi->name.value,
  1393. csi_assignment->comp->name.value, error);
  1394. comp = csi_assignment->comp;
  1395. if (error == SA_AIS_OK || error == SA_AIS_ERR_FAILED_OPERATION) {
  1396. amf_si_comp_csi_removed (csi_assignment->csi->si,
  1397. csi_assignment, error);
  1398. } else {
  1399. *retval = SA_AIS_ERR_INVALID_PARAM;
  1400. }
  1401. break;
  1402. }
  1403. #if 0
  1404. case AMF_RESPONSE_COMPONENTTERMINATECALLBACK: {
  1405. struct component_terminate_callback_data *callback_data = data;
  1406. dprintf ("Lib comp '%s' terminate callback response, error: %d",
  1407. callback_data->comp->name.value, error);
  1408. comp_presence_state_set (callback_data->comp,
  1409. SA_AMF_PRESENCE_UNINSTANTIATED);
  1410. break;
  1411. }
  1412. #endif
  1413. default:
  1414. assert (0);
  1415. break;
  1416. }
  1417. return comp;
  1418. }
  1419. /**
  1420. * Request a component to assume a particular HA state
  1421. * @param comp
  1422. * @param csi_assignment
  1423. * @param requested_ha_state
  1424. */
  1425. void amf_comp_hastate_set (
  1426. struct amf_comp *component,
  1427. struct amf_csi_assignment *csi_assignment)
  1428. {
  1429. ENTER ("'%s'", csi_assignment->csi->name.value);
  1430. assert (component != NULL && csi_assignment != NULL);
  1431. if (!component->error_suspected) {
  1432. lib_csi_set_request(component, csi_assignment);
  1433. } else {
  1434. if (csi_assignment->requested_ha_state == SA_AMF_HA_QUIESCED) {
  1435. csi_assignment->saAmfCSICompHAState = csi_assignment->requested_ha_state;
  1436. } else {
  1437. assert (0);
  1438. }
  1439. }
  1440. LEAVE("");
  1441. }
  1442. /**
  1443. * Request termination of a component
  1444. * @param comp
  1445. */
  1446. void amf_comp_terminate (struct amf_comp *comp)
  1447. {
  1448. dprintf ("comp terminate '%s'\n", getSaNameT (&comp->name));
  1449. comp_presence_state_set (comp, SA_AMF_PRESENCE_TERMINATING);
  1450. if (amf_su_is_local (comp->su)) {
  1451. amf_comp_healthcheck_stop (comp, NULL);
  1452. if (comp->error_suspected) {
  1453. clc_interfaces[comp->comptype]->cleanup (comp);
  1454. } else {
  1455. clc_interfaces[comp->comptype]->terminate (comp);
  1456. }
  1457. }
  1458. }
  1459. /**
  1460. * Request restart of a component
  1461. * @param comp
  1462. */
  1463. void amf_comp_restart (struct amf_comp *comp)
  1464. {
  1465. dprintf ("comp restart '%s'\n", getSaNameT (&comp->name));
  1466. comp_presence_state_set (comp, SA_AMF_PRESENCE_RESTARTING);
  1467. comp->saAmfCompRestartCount += 1;
  1468. if (amf_su_is_local (comp->su)) {
  1469. amf_comp_healthcheck_stop (comp, NULL);
  1470. clc_interfaces[comp->comptype]->cleanup (comp);
  1471. }
  1472. }
  1473. /**
  1474. * Request to return the HA state for a components CSI
  1475. * @param comp
  1476. * @param csi_name
  1477. * @param ha_state
  1478. *
  1479. * @return SaAisErrorT
  1480. */
  1481. SaAisErrorT amf_comp_hastate_get (
  1482. struct amf_comp *comp, SaNameT *csi_name, SaAmfHAStateT *ha_state)
  1483. {
  1484. struct amf_csi_assignment *assignment;
  1485. assert (comp != NULL && csi_name != NULL && ha_state != NULL);
  1486. dprintf ("comp ha state get from comp '%s' CSI '%s'\n",
  1487. getSaNameT (&comp->name), csi_name->value);
  1488. assignment = csi_assignment_find_in (comp, csi_name);
  1489. if (assignment != NULL) {
  1490. *ha_state = assignment->saAmfCSICompHAState;
  1491. return SA_AIS_OK;
  1492. }
  1493. return SA_AIS_ERR_INVALID_PARAM;
  1494. }
  1495. /**
  1496. * Response from a component informs AMF that it has performed a healthcheck
  1497. * @param comp
  1498. * @param healthcheckKey
  1499. * @param healthcheckResult
  1500. *
  1501. * @return SaAisErrorT
  1502. */
  1503. SaAisErrorT amf_comp_healthcheck_confirm (
  1504. struct amf_comp *comp,
  1505. SaAmfHealthcheckKeyT *healthcheckKey,
  1506. SaAisErrorT healthcheckResult)
  1507. {
  1508. struct amf_healthcheck *healthcheck;
  1509. SaAisErrorT error = SA_AIS_OK;
  1510. healthcheck = amf_comp_find_healthcheck (comp, healthcheckKey);
  1511. if (healthcheck == NULL) {
  1512. log_printf (LOG_ERR, "Healthcheckstop: Healthcheck '%s' not found",
  1513. healthcheckKey->key);
  1514. error = SA_AIS_ERR_NOT_EXIST;
  1515. } else if (healthcheck->active) {
  1516. if (healthcheckResult == SA_AIS_OK) {
  1517. /* the response was on time, restart the supervision timer */
  1518. poll_timer_delete (aisexec_poll_handle,
  1519. healthcheck->timer_handle_period);
  1520. poll_timer_add (aisexec_poll_handle,
  1521. healthcheck->saAmfHealthcheckPeriod,
  1522. (void *)healthcheck,
  1523. timer_function_healthcheck_tmo,
  1524. &healthcheck->timer_handle_period);
  1525. } else if (healthcheckResult == SA_AIS_ERR_FAILED_OPERATION) {
  1526. /* send to cluster */
  1527. mcast_healthcheck_tmo_event (healthcheck);
  1528. } else {
  1529. error = SA_AIS_ERR_INVALID_PARAM;
  1530. }
  1531. } else {
  1532. error = SA_AIS_ERR_INVALID_PARAM;
  1533. }
  1534. return error;
  1535. }
  1536. void amf_comp_init (void)
  1537. {
  1538. log_init ("AMF");
  1539. }
  1540. void amf_comp_operational_state_set (struct amf_comp *comp,
  1541. SaAmfOperationalStateT oper_state)
  1542. {
  1543. comp->saAmfCompOperState = oper_state;
  1544. TRACE1 ("Setting comp '%s', SU '%s' operational state: %s\n",
  1545. comp->name.value, comp->su->name.value,
  1546. amf_op_state (comp->saAmfCompOperState));
  1547. amf_su_comp_state_changed (
  1548. comp->su, comp, SA_AMF_OP_STATE, oper_state);
  1549. }
  1550. int amf_comp_get_saAmfCompNumCurrActiveCsi(struct amf_comp *component)
  1551. {
  1552. int cnt = 0;
  1553. struct amf_csi_assignment *csi_assignment;
  1554. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  1555. while (csi_assignment != NULL) {
  1556. if (csi_assignment->saAmfCSICompHAState == SA_AMF_HA_ACTIVE) {
  1557. cnt++;
  1558. }
  1559. csi_assignment = amf_comp_get_next_csi_assignment (
  1560. component, csi_assignment);
  1561. }
  1562. return cnt;
  1563. }
  1564. int amf_comp_get_saAmfCompNumCurrStandbyCsi(struct amf_comp *component)
  1565. {
  1566. int cnt = 0;
  1567. struct amf_csi_assignment *csi_assignment;
  1568. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  1569. while (csi_assignment != NULL) {
  1570. if (csi_assignment->saAmfCSICompHAState == SA_AMF_HA_STANDBY) {
  1571. cnt++;
  1572. }
  1573. csi_assignment = amf_comp_get_next_csi_assignment (
  1574. component, csi_assignment);
  1575. }
  1576. return cnt;
  1577. }
  1578. SaAmfReadinessStateT amf_comp_get_saAmfCompReadinessState (
  1579. struct amf_comp *component)
  1580. {
  1581. if (component->saAmfCompOperState == SA_AMF_OPERATIONAL_ENABLED) {
  1582. return amf_su_get_saAmfSUReadinessState (component->su);
  1583. } else if (component->saAmfCompOperState == SA_AMF_OPERATIONAL_DISABLED) {
  1584. return SA_AMF_READINESS_OUT_OF_SERVICE;
  1585. }
  1586. assert (0);
  1587. /* XXX we fall here in case NDEBUG is set */
  1588. return -1;
  1589. }
  1590. /**
  1591. * Component is informed that the node where the 'real'
  1592. * component process is executing has unexpectadly left the
  1593. * node. If there is a pending interaction between AMF
  1594. * (component) and the 'real' component process, then component
  1595. * will indicate to its subordinate objects the interaction
  1596. * failed. Pending presence state changes is indicated by
  1597. * reporting the new state is uninstantiated while pending csi
  1598. * operations are indicated by 'operation failed'.
  1599. * @param comp
  1600. *
  1601. * @return void
  1602. */
  1603. void amf_comp_node_left (struct amf_comp *component)
  1604. {
  1605. int change_pending = 0;
  1606. struct amf_csi_assignment *csi_assignment;
  1607. ENTER("");
  1608. if (component->saAmfCompPresenceState == SA_AMF_PRESENCE_INSTANTIATING ||
  1609. component->saAmfCompPresenceState == SA_AMF_PRESENCE_RESTARTING ||
  1610. component->saAmfCompPresenceState == SA_AMF_PRESENCE_TERMINATING) {
  1611. change_pending = 1;
  1612. }
  1613. component->saAmfCompPresenceState = SA_AMF_PRESENCE_UNINSTANTIATED;
  1614. if (amf_su_presence_state_all_comps_in_su_are_set (component->su,
  1615. SA_AMF_PRESENCE_UNINSTANTIATED) != 0) {
  1616. component->su->saAmfSUPresenceState = SA_AMF_PRESENCE_UNINSTANTIATED;
  1617. }
  1618. if (change_pending) {
  1619. change_pending =0;
  1620. amf_su_comp_state_changed ( component->su,
  1621. component,
  1622. SA_AMF_PRESENCE_STATE,
  1623. SA_AMF_PRESENCE_UNINSTANTIATED);
  1624. }
  1625. if (component->saAmfCompOperState == SA_AMF_OPERATIONAL_ENABLED) {
  1626. change_pending = 1;
  1627. }
  1628. component->saAmfCompOperState = SA_AMF_OPERATIONAL_DISABLED;
  1629. if (change_pending) {
  1630. change_pending =0;
  1631. amf_su_comp_state_changed ( component->su,
  1632. component,
  1633. SA_AMF_OP_STATE,
  1634. SA_AMF_OPERATIONAL_DISABLED);
  1635. }
  1636. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  1637. while (csi_assignment != NULL) {
  1638. if (csi_assignment->requested_ha_state !=
  1639. csi_assignment->saAmfCSICompHAState) {
  1640. amf_si_comp_set_ha_state_failed (
  1641. csi_assignment->csi->si,csi_assignment);
  1642. }
  1643. csi_assignment = amf_comp_get_next_csi_assignment (
  1644. component, csi_assignment);
  1645. }
  1646. }
  1647. /**
  1648. * Serialize a component including variable length arrays and
  1649. * strings to a buffer returned. Buffer is to be freed by
  1650. * caller.
  1651. * @param component
  1652. * @param len
  1653. *
  1654. * @return void*
  1655. */
  1656. void *amf_comp_serialize (struct amf_comp *component, int *len)
  1657. {
  1658. char *buf = NULL;
  1659. int i, offset = 0, size = 0;
  1660. TRACE8 ("%s", component->name.value);
  1661. buf = amf_serialize_SaNameT (buf, &size, &offset, &component->name);
  1662. /* count cstypes and write to buf */
  1663. for (i = 0; component->saAmfCompCsTypes &&
  1664. component->saAmfCompCsTypes[i] != NULL; i++);
  1665. buf = amf_serialize_SaUint32T (buf, &size, &offset, i);
  1666. for (i = 0; component->saAmfCompCsTypes &&
  1667. component->saAmfCompCsTypes[i] != NULL; i++) {
  1668. buf = amf_serialize_SaNameT (
  1669. buf, &size, &offset, component->saAmfCompCsTypes[i]);
  1670. }
  1671. buf = amf_serialize_SaUint32T (
  1672. buf, &size, &offset, component->saAmfCompCategory);
  1673. buf = amf_serialize_SaUint32T (
  1674. buf, &size, &offset, component->saAmfCompCapability);
  1675. buf = amf_serialize_SaUint32T (
  1676. buf, &size, &offset, component->saAmfCompNumMaxActiveCsi);
  1677. buf = amf_serialize_SaUint32T (
  1678. buf, &size, &offset, component->saAmfCompNumMaxStandbyCsi);
  1679. /* count environment vars and write to buf */
  1680. for (i = 0; component->saAmfCompCmdEnv &&
  1681. component->saAmfCompCmdEnv[i] != NULL; i++);
  1682. buf = amf_serialize_SaUint32T (buf, &size, &offset, i);
  1683. for (i = 0; component->saAmfCompCmdEnv &&
  1684. component->saAmfCompCmdEnv[i] != NULL; i++) {
  1685. buf = amf_serialize_SaStringT (
  1686. buf, &size, &offset, component->saAmfCompCmdEnv[i]);
  1687. }
  1688. buf = amf_serialize_SaUint32T (
  1689. buf, &size, &offset, component->saAmfCompDefaultClcCliTimeout);
  1690. buf = amf_serialize_SaUint32T (
  1691. buf, &size, &offset, component->saAmfCompDefaultCallbackTimeOut);
  1692. buf = amf_serialize_SaStringT (
  1693. buf, &size, &offset, component->saAmfCompInstantiateCmd);
  1694. buf = amf_serialize_SaStringT (
  1695. buf, &size, &offset, component->saAmfCompInstantiateCmdArgv);
  1696. buf = amf_serialize_SaUint32T (
  1697. buf, &size, &offset, component->saAmfCompInstantiateTimeout);
  1698. buf = amf_serialize_SaUint32T (
  1699. buf, &size, &offset, component->saAmfCompInstantiationLevel);
  1700. buf = amf_serialize_SaUint32T (
  1701. buf, &size, &offset, component->saAmfCompNumMaxInstantiateWithoutDelay);
  1702. buf = amf_serialize_SaUint32T (
  1703. buf, &size, &offset, component->saAmfCompNumMaxInstantiateWithDelay);
  1704. buf = amf_serialize_SaUint32T (
  1705. buf, &size, &offset, component->saAmfCompDelayBetweenInstantiateAttempts);
  1706. buf = amf_serialize_SaStringT (
  1707. buf, &size, &offset, component->saAmfCompTerminateCmd);
  1708. buf = amf_serialize_SaUint32T (
  1709. buf, &size, &offset, component->saAmfCompTerminateTimeout);
  1710. buf = amf_serialize_SaStringT (
  1711. buf, &size, &offset, component->saAmfCompTerminateCmdArgv);
  1712. buf = amf_serialize_SaStringT (
  1713. buf, &size, &offset, component->saAmfCompCleanupCmd);
  1714. buf = amf_serialize_SaUint32T (
  1715. buf, &size, &offset, component->saAmfCompCleanupTimeout);
  1716. buf = amf_serialize_SaStringT (
  1717. buf, &size, &offset, component->saAmfCompCleanupCmdArgv);
  1718. buf = amf_serialize_SaStringT (
  1719. buf, &size, &offset, component->saAmfCompAmStartCmd);
  1720. buf = amf_serialize_SaUint32T (
  1721. buf, &size, &offset, component->saAmfCompAmStartTimeout);
  1722. buf = amf_serialize_SaStringT (
  1723. buf, &size, &offset, component->saAmfCompAmStartCmdArgv);
  1724. buf = amf_serialize_SaUint32T (
  1725. buf, &size, &offset, component->saAmfCompNumMaxAmStartAttempt);
  1726. buf = amf_serialize_SaStringT (
  1727. buf, &size, &offset, component->saAmfCompAmStopCmd);
  1728. buf = amf_serialize_SaUint32T (
  1729. buf, &size, &offset, component->saAmfCompAmStopTimeout);
  1730. buf = amf_serialize_SaStringT (
  1731. buf, &size, &offset, component->saAmfCompAmStopCmdArgv);
  1732. buf = amf_serialize_SaUint32T (
  1733. buf, &size, &offset, component->saAmfCompNumMaxAmStopAttempt);
  1734. buf = amf_serialize_SaUint32T (
  1735. buf, &size, &offset, component->saAmfCompTerminateCallbackTimeout);
  1736. buf = amf_serialize_SaUint32T (
  1737. buf, &size, &offset, component->saAmfCompCSISetCallbackTimeout);
  1738. buf = amf_serialize_SaUint32T (
  1739. buf, &size, &offset, component->saAmfCompQuiescingCompleteTimeout);
  1740. buf = amf_serialize_SaUint32T (
  1741. buf, &size, &offset, component->saAmfCompCSIRmvCallbackTimeout);
  1742. buf = amf_serialize_SaUint32T (
  1743. buf, &size, &offset, component->saAmfCompRecoveryOnError);
  1744. buf = amf_serialize_SaUint32T (
  1745. buf, &size, &offset, component->saAmfCompDisableRestart);
  1746. buf = amf_serialize_SaNameT (
  1747. buf, &size, &offset, &component->saAmfCompProxyCsi);
  1748. buf = amf_serialize_SaUint32T (
  1749. buf, &size, &offset, component->saAmfCompOperState);
  1750. buf = amf_serialize_SaUint32T (
  1751. buf, &size, &offset, component->saAmfCompPresenceState);
  1752. buf = amf_serialize_SaUint32T (
  1753. buf, &size, &offset, component->saAmfCompRestartCount);
  1754. buf = amf_serialize_SaNameT (
  1755. buf, &size, &offset, &component->saAmfCompCurrProxyName);
  1756. buf = amf_serialize_SaStringT (
  1757. buf, &size, &offset, component->clccli_path);
  1758. buf = amf_serialize_SaUint32T (
  1759. buf, &size, &offset, component->comptype);
  1760. buf = amf_serialize_SaUint32T (
  1761. buf, &size, &offset, component->error_suspected);
  1762. *len = offset;
  1763. return buf;
  1764. }
  1765. /**
  1766. * Deserialize a buffer into a AMF component object.
  1767. * @param su
  1768. * @param buf
  1769. * @param size
  1770. *
  1771. * @return struct amf_comp*
  1772. */
  1773. struct amf_comp *amf_comp_deserialize (struct amf_su *su, char *buf)
  1774. {
  1775. char *tmp = buf;
  1776. int i;
  1777. SaUint32T cnt;
  1778. struct amf_comp *component = amf_comp_new (su, "");
  1779. tmp = amf_deserialize_SaNameT (tmp, &component->name);
  1780. tmp = amf_deserialize_SaUint32T (tmp, &cnt);
  1781. component->saAmfCompCsTypes = amf_malloc ((cnt + 1) * sizeof (SaNameT*));
  1782. for (i = 0; i < cnt; i++) {
  1783. component->saAmfCompCsTypes[i] = amf_malloc (sizeof (SaNameT));
  1784. tmp = amf_deserialize_SaNameT (tmp, component->saAmfCompCsTypes[i]);
  1785. }
  1786. component->saAmfCompCsTypes[i] = NULL;
  1787. tmp = amf_deserialize_SaUint32T (tmp, &component->saAmfCompCategory);
  1788. tmp = amf_deserialize_SaUint32T (tmp, &component->saAmfCompCapability);
  1789. tmp = amf_deserialize_SaUint32T (tmp, &component->saAmfCompNumMaxActiveCsi);
  1790. tmp = amf_deserialize_SaUint32T (tmp, &component->saAmfCompNumMaxStandbyCsi);
  1791. tmp = amf_deserialize_SaUint32T (tmp, &cnt);
  1792. component->saAmfCompCmdEnv = amf_malloc ((cnt + 1) * sizeof (SaStringT*));
  1793. for (i = 0; i < cnt; i++) {
  1794. tmp = amf_deserialize_SaStringT (tmp, &component->saAmfCompCmdEnv[i]);
  1795. }
  1796. component->saAmfCompCmdEnv[i] = NULL;
  1797. tmp = amf_deserialize_SaUint32T (
  1798. tmp, &component->saAmfCompDefaultClcCliTimeout);
  1799. tmp = amf_deserialize_SaUint32T (
  1800. tmp, &component->saAmfCompDefaultCallbackTimeOut);
  1801. tmp = amf_deserialize_SaStringT (
  1802. tmp, &component->saAmfCompInstantiateCmd);
  1803. tmp = amf_deserialize_SaStringT (
  1804. tmp, &component->saAmfCompInstantiateCmdArgv);
  1805. tmp = amf_deserialize_SaUint32T (
  1806. tmp, &component->saAmfCompInstantiateTimeout);
  1807. tmp = amf_deserialize_SaUint32T (
  1808. tmp, &component->saAmfCompInstantiationLevel);
  1809. tmp = amf_deserialize_SaUint32T (
  1810. tmp, &component->saAmfCompNumMaxInstantiateWithoutDelay);
  1811. tmp = amf_deserialize_SaUint32T (
  1812. tmp, &component->saAmfCompNumMaxInstantiateWithDelay);
  1813. tmp = amf_deserialize_SaUint32T (
  1814. tmp, &component->saAmfCompDelayBetweenInstantiateAttempts);
  1815. tmp = amf_deserialize_SaStringT (
  1816. tmp, &component->saAmfCompTerminateCmd);
  1817. tmp = amf_deserialize_SaUint32T (
  1818. tmp, &component->saAmfCompTerminateTimeout);
  1819. tmp = amf_deserialize_SaStringT (
  1820. tmp, &component->saAmfCompTerminateCmdArgv);
  1821. tmp = amf_deserialize_SaStringT (
  1822. tmp, &component->saAmfCompCleanupCmd);
  1823. tmp = amf_deserialize_SaUint32T (
  1824. tmp, &component->saAmfCompCleanupTimeout);
  1825. tmp = amf_deserialize_SaStringT (
  1826. tmp, &component->saAmfCompCleanupCmdArgv);
  1827. tmp = amf_deserialize_SaStringT (
  1828. tmp, &component->saAmfCompAmStartCmd);
  1829. tmp = amf_deserialize_SaUint32T (
  1830. tmp, &component->saAmfCompAmStartTimeout);
  1831. tmp = amf_deserialize_SaStringT (
  1832. tmp, &component->saAmfCompAmStartCmdArgv);
  1833. tmp = amf_deserialize_SaUint32T (
  1834. tmp, &component->saAmfCompNumMaxAmStartAttempt);
  1835. tmp = amf_deserialize_SaStringT (
  1836. tmp, &component->saAmfCompAmStopCmd);
  1837. tmp = amf_deserialize_SaUint32T (
  1838. tmp, &component->saAmfCompAmStopTimeout);
  1839. tmp = amf_deserialize_SaStringT (
  1840. tmp, &component->saAmfCompAmStopCmdArgv);
  1841. tmp = amf_deserialize_SaUint32T (
  1842. tmp, &component->saAmfCompNumMaxAmStopAttempt);
  1843. tmp = amf_deserialize_SaUint32T (
  1844. tmp, &component->saAmfCompTerminateCallbackTimeout);
  1845. tmp = amf_deserialize_SaUint32T (
  1846. tmp, &component->saAmfCompCSISetCallbackTimeout);
  1847. tmp = amf_deserialize_SaUint32T (
  1848. tmp, &component->saAmfCompQuiescingCompleteTimeout);
  1849. tmp = amf_deserialize_SaUint32T (
  1850. tmp, &component->saAmfCompCSIRmvCallbackTimeout);
  1851. tmp = amf_deserialize_SaUint32T (
  1852. tmp, &component->saAmfCompRecoveryOnError);
  1853. tmp = amf_deserialize_SaUint32T (
  1854. tmp, &component->saAmfCompDisableRestart);
  1855. tmp = amf_deserialize_SaNameT (
  1856. tmp, &component->saAmfCompProxyCsi);
  1857. tmp = amf_deserialize_SaUint32T (
  1858. tmp, &component->saAmfCompOperState);
  1859. tmp = amf_deserialize_SaUint32T (
  1860. tmp, &component->saAmfCompPresenceState);
  1861. tmp = amf_deserialize_SaUint32T (
  1862. tmp, &component->saAmfCompRestartCount);
  1863. tmp = amf_deserialize_SaNameT (
  1864. tmp, &component->saAmfCompCurrProxyName);
  1865. tmp = amf_deserialize_SaStringT (
  1866. tmp, &component->clccli_path);
  1867. tmp = amf_deserialize_SaUint32T (
  1868. tmp, &component->comptype);
  1869. tmp = amf_deserialize_SaUint32T (
  1870. tmp, &component->error_suspected);
  1871. return component;
  1872. }
  1873. void *amf_healthcheck_serialize (struct amf_healthcheck *healthcheck, int *len)
  1874. {
  1875. char *buf = NULL;
  1876. int offset = 0, size = 0;
  1877. TRACE8 ("%s", healthcheck->safHealthcheckKey.key);
  1878. buf = amf_serialize_opaque (buf, &size, &offset,
  1879. &healthcheck->safHealthcheckKey.key, SA_AMF_HEALTHCHECK_KEY_MAX);
  1880. buf = amf_serialize_SaUint16T (buf, &size, &offset,
  1881. healthcheck->safHealthcheckKey.keyLen);
  1882. buf = amf_serialize_SaUint32T (buf, &size, &offset,
  1883. healthcheck->saAmfHealthcheckMaxDuration);
  1884. buf = amf_serialize_SaUint32T (buf, &size, &offset,
  1885. healthcheck->saAmfHealthcheckPeriod);
  1886. *len = offset;
  1887. return buf;
  1888. }
  1889. struct amf_healthcheck *amf_healthcheck_deserialize (
  1890. struct amf_comp *comp, char *buf)
  1891. {
  1892. char *tmp = buf;
  1893. int cnt;
  1894. amf_healthcheck_t *healthcheck = amf_healthcheck_new (comp);
  1895. tmp = amf_deserialize_opaque (tmp, &healthcheck->safHealthcheckKey.key, &cnt);
  1896. tmp = amf_deserialize_SaUint16T (tmp,
  1897. &healthcheck->safHealthcheckKey.keyLen);
  1898. tmp = amf_deserialize_SaUint32T (tmp,
  1899. &healthcheck->saAmfHealthcheckMaxDuration);
  1900. tmp = amf_deserialize_SaUint32T (tmp,
  1901. &healthcheck->saAmfHealthcheckPeriod);
  1902. return healthcheck;
  1903. }
  1904. amf_healthcheck_t *amf_healthcheck_new (struct amf_comp *comp)
  1905. {
  1906. amf_healthcheck_t *healthcheck = amf_calloc (1, sizeof (amf_healthcheck_t));
  1907. healthcheck->comp = comp;
  1908. healthcheck->next = comp->healthcheck_head;
  1909. comp->healthcheck_head = healthcheck;
  1910. return healthcheck;
  1911. }
  1912. void amf_comp_csi_remove (amf_comp_t *component,
  1913. amf_csi_assignment_t *csi_assignment)
  1914. {
  1915. struct res_lib_amf_csiremovecallback res_lib;
  1916. ENTER("");
  1917. res_lib.header.id = MESSAGE_RES_AMF_CSIREMOVECALLBACK;
  1918. res_lib.header.size = sizeof (struct res_lib_amf_csiremovecallback);
  1919. res_lib.header.error = SA_AIS_OK;
  1920. res_lib.invocation =
  1921. invocation_create (AMF_RESPONSE_CSIREMOVECALLBACK, csi_assignment);
  1922. amf_comp_dn_make (component, &res_lib.compName);
  1923. amf_csi_dn_make (csi_assignment->csi, &res_lib.csiName);
  1924. res_lib.csiFlags = SA_AMF_CSI_TARGET_ONE;
  1925. TRACE7 ("sending CSI remove request to component %s",
  1926. res_lib.compName.value);
  1927. openais_conn_send_response (
  1928. openais_conn_partner_get (component->conn),
  1929. &res_lib, sizeof (struct res_lib_amf_csiremovecallback));
  1930. }