amfcomp.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222
  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. char *amf_comp_dn_make (struct amf_comp *comp, SaNameT *name)
  290. {
  291. int i = snprintf ((char*) name->value, SA_MAX_NAME_LENGTH,
  292. "safComp=%s,safSu=%s,safSg=%s,safApp=%s",
  293. comp->name.value, comp->su->name.value,
  294. comp->su->sg->name.value, comp->su->sg->application->name.value);
  295. assert (i <= SA_MAX_NAME_LENGTH);
  296. name->length = i;
  297. return (char *)name->value;
  298. }
  299. #ifndef xprintf
  300. #define xprintf(...)
  301. #endif
  302. static void *clc_command_run (void *context)
  303. {
  304. struct clc_command_run_data *clc_command_run_data =
  305. (struct clc_command_run_data *)context;
  306. pid_t pid;
  307. int res;
  308. char **argv = NULL;
  309. char **envp = NULL;
  310. int status;
  311. char path[PATH_MAX];
  312. char *cmd = 0;
  313. char *comp_argv = 0;
  314. char comp_name[SA_MAX_NAME_LENGTH + 24];
  315. int i;
  316. int argv_size;
  317. int envp_size;
  318. ENTER_VOID();
  319. pid = fork();
  320. if (pid == -1) {
  321. fprintf (stderr, "Couldn't fork process %s\n", strerror (errno));
  322. return (0);
  323. }
  324. if (pid) {
  325. xprintf ("waiting for pid %d to finish\n", pid);
  326. waitpid (pid, &status, 0);
  327. if (WIFEXITED (status) != 0 && WEXITSTATUS(status) != 0) {
  328. fprintf (stderr, "Error: CLC_CLI (%d) failed with exit status:"
  329. " %d - %s\n", pid, WEXITSTATUS(status),
  330. strerror (WEXITSTATUS(status)));
  331. /*
  332. * TODO: remove this and handle properly later...
  333. */
  334. openais_exit_error (AIS_DONE_FATAL_ERR);
  335. }
  336. if (WIFSIGNALED (status) != 0) {
  337. fprintf (stderr, "Error: CLC_CLI (%d) failed with exit status:"
  338. " %d\n", pid, WTERMSIG(status));
  339. /*
  340. * TODO: remove this and handle properly later...
  341. */
  342. openais_exit_error (AIS_DONE_FATAL_ERR);
  343. }
  344. xprintf ("process (%d) finished with %x\n", pid, status);
  345. if (clc_command_run_data->completion_callback) {
  346. clc_command_run_data->completion_callback (context);
  347. }
  348. pthread_exit(0);
  349. }
  350. switch (clc_command_run_data->type) {
  351. case CLC_COMMAND_RUN_OPERATION_TYPE_INSTANTIATE:
  352. cmd = clc_command_run_data->comp->saAmfCompInstantiateCmd;
  353. comp_argv = clc_command_run_data->comp->saAmfCompInstantiateCmdArgv;
  354. break;
  355. case CLC_COMMAND_RUN_OPERATION_TYPE_TERMINATE:
  356. cmd = clc_command_run_data->comp->saAmfCompTerminateCmd;
  357. comp_argv = clc_command_run_data->comp->saAmfCompTerminateCmdArgv;
  358. break;
  359. case CLC_COMMAND_RUN_OPERATION_TYPE_CLEANUP:
  360. cmd = clc_command_run_data->comp->saAmfCompCleanupCmd;
  361. comp_argv = clc_command_run_data->comp->saAmfCompCleanupCmdArgv;
  362. break;
  363. default:
  364. assert (0 != 1);
  365. break;
  366. }
  367. /* If command is not an absolute path, search for paths in parent objects */
  368. if (cmd[0] != '/') {
  369. if (clc_command_run_data->comp->clccli_path != NULL) {
  370. sprintf (path, "%s/%s",
  371. clc_command_run_data->comp->clccli_path, cmd);
  372. } else if (clc_command_run_data->comp->su->clccli_path != NULL) {
  373. sprintf (path, "%s/%s",
  374. clc_command_run_data->comp->su->clccli_path, cmd);
  375. } else if (clc_command_run_data->comp->su->sg->clccli_path != NULL) {
  376. sprintf (path, "%s/%s",
  377. clc_command_run_data->comp->su->sg->clccli_path, cmd);
  378. } else if (clc_command_run_data->comp->su->sg->application->clccli_path != NULL) {
  379. sprintf (path, "%s/%s",
  380. clc_command_run_data->comp->su->sg->application->clccli_path, cmd);
  381. }
  382. cmd = path;
  383. }
  384. argv_size = 2;
  385. argv = amf_malloc (sizeof (char*) * argv_size);
  386. argv[0] = cmd;
  387. {
  388. /* make a proper argv array */
  389. i = 1;
  390. char *ptrptr;
  391. char *arg = strtok_r(comp_argv, " ", &ptrptr);
  392. while (arg) {
  393. argv_size++;
  394. argv = realloc (argv, sizeof (char*) * argv_size);
  395. if (argv == NULL) {
  396. fprintf (stderr, "out-of-memory");
  397. exit (-1);
  398. }
  399. argv[i] = arg;
  400. arg = strtok_r(NULL, " ", &ptrptr);
  401. i++;
  402. }
  403. }
  404. argv[i] = NULL;
  405. i = snprintf (comp_name, SA_MAX_NAME_LENGTH,
  406. "SA_AMF_COMPONENT_NAME=safComp=%s,safSu=%s,safSg=%s,safApp=%s",
  407. clc_command_run_data->comp->name.value,
  408. clc_command_run_data->comp->su->name.value,
  409. clc_command_run_data->comp->su->sg->name.value,
  410. clc_command_run_data->comp->su->sg->application->name.value);
  411. assert (i <= sizeof (comp_name));
  412. /* two is for component name and NULL termination */
  413. envp_size = 2;
  414. envp = amf_malloc (sizeof (char*) * envp_size);
  415. envp[0] = comp_name;
  416. for (i = 1; clc_command_run_data->comp->saAmfCompCmdEnv &&
  417. clc_command_run_data->comp->saAmfCompCmdEnv[i - 1]; i++) {
  418. envp_size++;
  419. envp = realloc (envp, sizeof (char*) * envp_size);
  420. if (envp == NULL) {
  421. fprintf (stderr, "out-of-memory");
  422. exit (-1);
  423. }
  424. envp[i] = clc_command_run_data->comp->saAmfCompCmdEnv[i - 1];
  425. }
  426. envp[i] = NULL;
  427. xprintf ("running command '%s' with environment (%d):\n", cmd, envp_size);
  428. for (i = 0; envp[i] != NULL; i++) {
  429. xprintf (" %s\n", envp[i]);
  430. }
  431. xprintf (" and argv (%d):\n", argv_size);
  432. for (i = 0; argv[i] != NULL; i++) {
  433. xprintf (" %s\n", argv[i]);
  434. }
  435. res = execve (cmd, argv, envp);
  436. if (res == -1) {
  437. fprintf (stderr, "Couldn't exec program %s (%s)\n",
  438. cmd, strerror (errno));
  439. }
  440. exit (res); /* abnormal exit of forked process */
  441. return (0);
  442. }
  443. static void amf_comp_instantiate_tmo (void *component)
  444. {
  445. SaNameT compName;
  446. amf_comp_dn_make (component, &compName);
  447. amf_msg_mcast (MESSAGE_REQ_EXEC_AMF_COMPONENT_INSTANTIATE_TMO,
  448. &compName, sizeof (SaNameT));
  449. }
  450. static void start_component_instantiate_timer (struct amf_comp *component)
  451. {
  452. poll_timer_add (aisexec_poll_handle,
  453. component->saAmfCompInstantiateTimeout,
  454. component,
  455. amf_comp_instantiate_tmo,
  456. &component->instantiate_timeout_handle);
  457. }
  458. /*
  459. * Instantiate possible operations
  460. */
  461. static int clc_cli_instantiate (struct amf_comp *comp)
  462. {
  463. int res;
  464. pthread_t thread;
  465. pthread_attr_t thread_attr; /* thread attribute */
  466. struct clc_command_run_data *clc_command_run_data;
  467. ENTER("comp '%s'\n", getSaNameT (&comp->name));
  468. clc_command_run_data = amf_malloc (sizeof (struct clc_command_run_data));
  469. clc_command_run_data->comp = comp;
  470. clc_command_run_data->type = CLC_COMMAND_RUN_OPERATION_TYPE_INSTANTIATE;
  471. clc_command_run_data->completion_callback = NULL;
  472. pthread_attr_init (&thread_attr);
  473. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  474. res = pthread_create (&thread, &thread_attr, clc_command_run,
  475. (void *)clc_command_run_data);
  476. if (res != 0) {
  477. log_printf (LOG_LEVEL_ERROR, "pthread_create failed: %d", res);
  478. }
  479. start_component_instantiate_timer (comp);
  480. // clc_command_run_data->completion_callback (clc_command_run_data);
  481. // TODO error code from pthread_create
  482. return (res);
  483. }
  484. static int clc_instantiate_callback (struct amf_comp *comp)
  485. {
  486. ENTER("comp %s\n", getSaNameT (&comp->name));
  487. return (0);
  488. }
  489. static int clc_csi_set_callback (struct amf_comp *comp)
  490. {
  491. ENTER("comp %s\n", getSaNameT (&comp->name));
  492. return (0);
  493. }
  494. /*
  495. * Terminate possible operations
  496. */
  497. static int clc_cli_terminate (struct amf_comp *comp)
  498. {
  499. ENTER("comp %s\n", getSaNameT (&comp->name));
  500. return (0);
  501. }
  502. /**
  503. * Request component to terminate itself
  504. * @param comp
  505. *
  506. * @return int
  507. */
  508. static int lib_comp_terminate_request (struct amf_comp *comp)
  509. {
  510. struct res_lib_amf_componentterminatecallback res_lib;
  511. struct component_terminate_callback_data *component_terminate_callback_data;
  512. ENTER("comp %s\n", getSaNameT (&comp->name));
  513. res_lib.header.id = MESSAGE_RES_AMF_COMPONENTTERMINATECALLBACK;
  514. res_lib.header.size = sizeof (struct res_lib_amf_componentterminatecallback);
  515. res_lib.header.error = SA_AIS_OK;
  516. memcpy (&res_lib.compName, &comp->name, sizeof (SaNameT));
  517. component_terminate_callback_data =
  518. amf_malloc (sizeof (struct component_terminate_callback_data));
  519. component_terminate_callback_data->comp = comp;
  520. res_lib.invocation =
  521. invocation_create (
  522. AMF_RESPONSE_COMPONENTTERMINATECALLBACK,
  523. component_terminate_callback_data);
  524. openais_conn_send_response (
  525. openais_conn_partner_get (comp->conn),
  526. &res_lib,
  527. sizeof (struct res_lib_amf_componentterminatecallback));
  528. return (0);
  529. }
  530. static int clc_csi_remove_callback (struct amf_comp *comp)
  531. {
  532. dprintf ("clc_tcsi_remove_callback\n");
  533. return (0);
  534. }
  535. /*
  536. * Clean up completed
  537. */
  538. static void mcast_cleanup_completion_event (void *context)
  539. {
  540. struct clc_command_run_data *clc_command_run_data =
  541. (struct clc_command_run_data *)context;
  542. struct req_exec_amf_clc_cleanup_completed req;
  543. struct iovec iovec;
  544. req.header.size = sizeof (struct req_exec_amf_clc_cleanup_completed);
  545. req.header.id = SERVICE_ID_MAKE (AMF_SERVICE,
  546. MESSAGE_REQ_EXEC_AMF_CLC_CLEANUP_COMPLETED);
  547. amf_comp_dn_make (clc_command_run_data->comp, &req.compName);
  548. iovec.iov_base = (char *)&req;
  549. iovec.iov_len = sizeof (req);
  550. assert (totempg_groups_mcast_joined (openais_group_handle,
  551. &iovec, 1, TOTEMPG_AGREED) == 0);
  552. }
  553. /*
  554. * Cleanup possible operations
  555. */
  556. static int clc_cli_cleanup (struct amf_comp *comp)
  557. {
  558. int res;
  559. pthread_t thread;
  560. pthread_attr_t thread_attr; /* thread attribute */
  561. struct clc_command_run_data *clc_command_run_data;
  562. dprintf ("clc_cli_cleanup\n");
  563. clc_command_run_data = amf_malloc (sizeof (struct clc_command_run_data));
  564. clc_command_run_data->comp = comp;
  565. clc_command_run_data->type = CLC_COMMAND_RUN_OPERATION_TYPE_CLEANUP;
  566. clc_command_run_data->completion_callback = mcast_cleanup_completion_event;
  567. pthread_attr_init (&thread_attr);
  568. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  569. res = pthread_create (&thread, &thread_attr, clc_command_run,
  570. (void *)clc_command_run_data);
  571. if (res != 0) {
  572. log_printf (LOG_LEVEL_ERROR, "pthread_create failed: %d", res);
  573. }
  574. // TODO error code from pthread_create
  575. return (res);
  576. }
  577. static int clc_cli_cleanup_local (struct amf_comp *comp)
  578. {
  579. dprintf ("clc_cli_cleanup_local\n");
  580. return (0);
  581. }
  582. #if 0
  583. static int clc_terminate (struct amf_comp *comp)
  584. {
  585. int res;
  586. dprintf ("clc terminate for comp %s\n", getSaNameT (&comp->name));
  587. assert (0);
  588. operational_state_comp_set (comp, SA_AMF_OPERATIONAL_DISABLED);
  589. comp_presence_state_set (comp, SA_AMF_PRESENCE_TERMINATING);
  590. res = clc_interfaces[comp->comptype]->terminate (comp);
  591. return (0);
  592. }
  593. #endif
  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 (memcmp (key, &healthcheck->safHealthcheckKey,
  606. sizeof (SaAmfHealthcheckKeyT)) == 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. #if 0
  764. static void lib_csi_remove_request (struct amf_comp *comp,
  765. struct amf_csi *csi)
  766. {
  767. struct res_lib_amf_csiremovecallback res_lib_amf_csiremovecallback;
  768. struct csi_remove_callback_data *csi_remove_callback_data;
  769. dprintf ("\t%s\n", getSaNameT (&comp->name));
  770. res_lib_amf_csiremovecallback.header.id = MESSAGE_RES_AMF_CSIREMOVECALLBACK;
  771. res_lib_amf_csiremovecallback.header.size = sizeof (struct res_lib_amf_csiremovecallback);
  772. res_lib_amf_csiremovecallback.header.error = SA_AIS_OK;
  773. csi_remove_callback_data = malloc (sizeof (struct csi_remove_callback_data));
  774. assert (csi_remove_callback_data); // TODO failure here of malloc
  775. csi_remove_callback_data->csi = csi;
  776. res_lib_amf_csiremovecallback.invocation =
  777. invocation_create (
  778. AMF_RESPONSE_CSIREMOVECALLBACK,
  779. csi_remove_callback_data);
  780. memcpy (&res_lib_amf_csiremovecallback.compName,
  781. &comp->name, sizeof (SaNameT));
  782. memcpy (&res_lib_amf_csiremovecallback.csiName,
  783. &csi->name, sizeof (SaNameT));
  784. res_lib_amf_csiremovecallback.csiFlags = 0;
  785. openais_conn_send_response (
  786. openais_conn_partner_get (comp->conn),
  787. &res_lib_amf_csiremovecallback,
  788. sizeof (struct res_lib_amf_csiremovecallback));
  789. }
  790. #endif
  791. struct amf_csi_assignment *amf_comp_get_next_csi_assignment (
  792. struct amf_comp *component,
  793. const struct amf_csi_assignment *csi_assignment)
  794. {
  795. struct amf_si *si;
  796. struct amf_csi *csi;
  797. struct amf_csi_assignment *tmp_csi_assignment;
  798. SaNameT dn;
  799. amf_comp_dn_make (component, &dn);
  800. if (csi_assignment == NULL) {
  801. si = component->su->sg->application->si_head;
  802. csi = si->csi_head;
  803. tmp_csi_assignment = csi->assigned_csis;
  804. } else {
  805. tmp_csi_assignment = csi_assignment->next;
  806. if (tmp_csi_assignment == NULL) {
  807. csi = csi_assignment->csi->next;
  808. if (csi == NULL) {
  809. si = csi_assignment->csi->si->next;
  810. if (si == NULL) {
  811. return NULL;
  812. } else {
  813. csi = si->csi_head;
  814. tmp_csi_assignment = csi->assigned_csis;
  815. }
  816. } else {
  817. si = csi->si;
  818. tmp_csi_assignment = csi->assigned_csis;
  819. }
  820. } else {
  821. csi = tmp_csi_assignment->csi;
  822. si = csi->si;
  823. }
  824. }
  825. for (; si != NULL; si = si->next) {
  826. if (tmp_csi_assignment == NULL && csi == NULL && si != NULL) {
  827. csi = si->csi_head;
  828. tmp_csi_assignment = csi->assigned_csis;
  829. }
  830. for (; csi != NULL; csi = csi->next) {
  831. if (tmp_csi_assignment == NULL && csi != NULL) {
  832. tmp_csi_assignment = csi->assigned_csis;
  833. }
  834. for (; tmp_csi_assignment != NULL;
  835. tmp_csi_assignment = tmp_csi_assignment->next) {
  836. if (name_match (&tmp_csi_assignment->name, &dn)) {
  837. return tmp_csi_assignment;
  838. }
  839. }
  840. }
  841. }
  842. return NULL;
  843. }
  844. void amf_comp_foreach_csi_assignment (
  845. struct amf_comp *component,
  846. void (*foreach_fn) (struct amf_comp *component,
  847. struct amf_csi_assignment *csi_assignment))
  848. {
  849. struct amf_csi_assignment *csi_assignment;
  850. assert (foreach_fn != NULL);
  851. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  852. while (csi_assignment != NULL) {
  853. foreach_fn (component, csi_assignment);
  854. csi_assignment = amf_comp_get_next_csi_assignment (
  855. component, csi_assignment);
  856. }
  857. }
  858. static struct amf_csi_assignment *csi_assignment_find_in (
  859. struct amf_comp *component, SaNameT *csi_name)
  860. {
  861. struct amf_csi_assignment *csi_assignment;
  862. SaNameT dn;
  863. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  864. while (csi_assignment != NULL) {
  865. amf_csi_dn_make (csi_assignment->csi, &dn);
  866. if (name_match (csi_name, &dn)) {
  867. return csi_assignment;
  868. }
  869. csi_assignment = amf_comp_get_next_csi_assignment (
  870. component, csi_assignment);
  871. }
  872. return NULL;
  873. }
  874. static void healthcheck_deactivate (
  875. struct amf_healthcheck *healthcheck_active)
  876. {
  877. dprintf ("deactivating healthcheck for component %s\n",
  878. getSaNameT (&healthcheck_active->comp->name));
  879. poll_timer_delete (aisexec_poll_handle,
  880. healthcheck_active->timer_handle_period);
  881. poll_timer_delete (aisexec_poll_handle,
  882. healthcheck_active->timer_handle_duration);
  883. invocation_destroy_by_data ((void *)healthcheck_active);
  884. healthcheck_active->active = 0;
  885. }
  886. /**
  887. * This function is called by the timer subsystem when AMF should request
  888. * a new healthcheck from a component.
  889. * @param data
  890. */
  891. static void timer_function_healthcheck_next_fn (void *_healthcheck)
  892. {
  893. struct amf_healthcheck *healthcheck = _healthcheck;
  894. /* send healthcheck request to component */
  895. lib_healthcheck_request (healthcheck);
  896. /* start duration timer for response */
  897. poll_timer_add (aisexec_poll_handle,
  898. healthcheck->saAmfHealthcheckMaxDuration,
  899. (void *)healthcheck,
  900. timer_function_healthcheck_tmo,
  901. &healthcheck->timer_handle_duration);
  902. }
  903. /**
  904. * Multicast a healthcheck timeout event.
  905. * @param healthcheck
  906. */
  907. static void mcast_healthcheck_tmo_event (
  908. struct amf_healthcheck *healthcheck)
  909. {
  910. struct req_exec_amf_healthcheck_tmo req_exec;
  911. struct iovec iovec;
  912. req_exec.header.size = sizeof (struct req_exec_amf_healthcheck_tmo);
  913. req_exec.header.id = SERVICE_ID_MAKE (AMF_SERVICE,
  914. MESSAGE_REQ_EXEC_AMF_HEALTHCHECK_TMO);
  915. amf_comp_dn_make (healthcheck->comp, &req_exec.compName);
  916. memcpy (&req_exec.safHealthcheckKey,
  917. &healthcheck->safHealthcheckKey, sizeof (SaAmfHealthcheckKeyT));
  918. iovec.iov_base = (char *)&req_exec;
  919. iovec.iov_len = sizeof (req_exec);
  920. assert (totempg_groups_mcast_joined (openais_group_handle,
  921. &iovec, 1, TOTEMPG_AGREED) == 0);
  922. }
  923. /**
  924. * This function is called by the timer subsystem when a component has not
  925. * performed a healthcheck on time.
  926. * The event is multicasted to the cluster.
  927. * @param data
  928. */
  929. static void timer_function_healthcheck_tmo (
  930. void *_healthcheck)
  931. {
  932. struct amf_healthcheck *healthcheck = (struct amf_healthcheck *)_healthcheck;
  933. TRACE2 ("timeout occured on healthcheck for component %s.\n",
  934. getSaNameT (&healthcheck->comp->name));
  935. mcast_healthcheck_tmo_event (healthcheck);
  936. }
  937. static void lib_healthcheck_request (struct amf_healthcheck *healthcheck)
  938. {
  939. struct res_lib_amf_healthcheckcallback res_lib;
  940. res_lib.header.id = MESSAGE_RES_AMF_HEALTHCHECKCALLBACK;
  941. res_lib.header.size = sizeof (struct res_lib_amf_healthcheckcallback);
  942. res_lib.header.error = SA_AIS_OK;
  943. res_lib.invocation =
  944. invocation_create (AMF_RESPONSE_HEALTHCHECKCALLBACK, healthcheck);
  945. amf_comp_dn_make (healthcheck->comp, &res_lib.compName);
  946. memcpy (&res_lib.key, &healthcheck->safHealthcheckKey,
  947. sizeof (SaAmfHealthcheckKeyT));
  948. TRACE7 ("sending healthcheck request to component %s",
  949. res_lib.compName.value);
  950. openais_conn_send_response (
  951. openais_conn_partner_get (healthcheck->comp->conn),
  952. &res_lib, sizeof (struct res_lib_amf_healthcheckcallback));
  953. }
  954. static void lib_csi_set_request (
  955. struct amf_comp *comp,
  956. struct amf_csi_assignment *csi_assignment)
  957. {
  958. struct res_lib_amf_csisetcallback* res_lib;
  959. void* p;
  960. struct amf_csi_attribute *attribute;
  961. size_t char_length_of_csi_attrs=0;
  962. size_t num_of_csi_attrs=0;
  963. int i;
  964. struct amf_csi *csi;
  965. char* csi_attribute_buf;
  966. unsigned int byte_offset;
  967. if (!amf_su_is_local (comp->su))
  968. return;
  969. csi = csi_assignment->csi;
  970. ENTER ("Assigning CSI '%s' state %s to comp '%s'\n",
  971. getSaNameT (&csi->name),
  972. amf_ha_state (csi_assignment->requested_ha_state),
  973. comp->name.value);
  974. for (attribute = csi->attributes_head;
  975. attribute != NULL;
  976. attribute = attribute->next) {
  977. for (i = 0; attribute->value[i] != NULL; i++) {
  978. num_of_csi_attrs++;
  979. char_length_of_csi_attrs += strlen(attribute->name);
  980. char_length_of_csi_attrs += strlen(attribute->value[i]);
  981. char_length_of_csi_attrs += 2;
  982. }
  983. }
  984. p = amf_malloc(sizeof(struct res_lib_amf_csisetcallback) +
  985. char_length_of_csi_attrs);
  986. res_lib = (struct res_lib_amf_csisetcallback*)p;
  987. /* Address of the buffer containing the Csi name value pair */
  988. csi_attribute_buf = res_lib->csi_attr_buf;
  989. /* Byteoffset start at the zero byte */
  990. byte_offset = 0;
  991. for (attribute = csi->attributes_head;
  992. attribute != NULL;
  993. attribute = attribute->next) {
  994. for (i = 0; attribute->value[i] != NULL; i++) {
  995. strcpy(&csi_attribute_buf[byte_offset], (char*)attribute->name);
  996. byte_offset += strlen(attribute->name) + 1;
  997. strcpy(&csi_attribute_buf[byte_offset], (char*)attribute->value[i]);
  998. byte_offset += strlen(attribute->value[i]) + 1;
  999. }
  1000. }
  1001. res_lib->number = num_of_csi_attrs;
  1002. res_lib->csiFlags = SA_AMF_CSI_ADD_ONE;
  1003. switch (csi_assignment->requested_ha_state) {
  1004. case SA_AMF_HA_ACTIVE: {
  1005. res_lib->csiStateDescriptor.activeDescriptor.activeCompName.length = 0;
  1006. res_lib->csiStateDescriptor.activeDescriptor.transitionDescriptor =
  1007. SA_AMF_CSI_NEW_ASSIGN;
  1008. break;
  1009. }
  1010. case SA_AMF_HA_STANDBY: {
  1011. res_lib->csiStateDescriptor.standbyDescriptor.activeCompName.length = 0;
  1012. res_lib->csiStateDescriptor.standbyDescriptor.standbyRank = 1;
  1013. break;
  1014. }
  1015. case SA_AMF_HA_QUIESCED: {
  1016. /*TODO*/
  1017. break;
  1018. }
  1019. case SA_AMF_HA_QUIESCING: {
  1020. /*TODO*/
  1021. break;
  1022. }
  1023. default: {
  1024. assert(SA_AMF_HA_ACTIVE||SA_AMF_HA_STANDBY||SA_AMF_HA_QUIESCING||SA_AMF_HA_QUIESCED);
  1025. break;
  1026. }
  1027. }
  1028. res_lib->header.id = MESSAGE_RES_AMF_CSISETCALLBACK;
  1029. res_lib->header.size =
  1030. sizeof (struct res_lib_amf_csisetcallback) +
  1031. char_length_of_csi_attrs;
  1032. res_lib->header.error = SA_AIS_OK;
  1033. amf_comp_dn_make (comp, &res_lib->compName);
  1034. amf_csi_dn_make (csi, &res_lib->csiName);
  1035. res_lib->haState = csi_assignment->requested_ha_state;
  1036. res_lib->invocation =
  1037. invocation_create (AMF_RESPONSE_CSISETCALLBACK, csi_assignment);
  1038. openais_conn_send_response (
  1039. openais_conn_partner_get (comp->conn), res_lib, res_lib->header.size);
  1040. free(p);
  1041. }
  1042. static void stop_component_instantiate_timer (struct amf_comp *component)
  1043. {
  1044. if (component->instantiate_timeout_handle) {
  1045. dprintf ("Stop cluster startup timer");
  1046. poll_timer_delete (aisexec_poll_handle,
  1047. component->instantiate_timeout_handle);
  1048. component->instantiate_timeout_handle = 0;
  1049. }
  1050. }
  1051. SaAisErrorT amf_comp_register (struct amf_comp *comp)
  1052. {
  1053. TRACE2("Exec comp register '%s'", comp->name.value);
  1054. stop_component_instantiate_timer (comp);
  1055. switch (comp->saAmfCompPresenceState) {
  1056. case SA_AMF_PRESENCE_RESTARTING:
  1057. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATED);
  1058. break;
  1059. case SA_AMF_PRESENCE_INSTANTIATING:
  1060. amf_comp_operational_state_set (comp, SA_AMF_OPERATIONAL_ENABLED);
  1061. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATED);
  1062. break;
  1063. case SA_AMF_PRESENCE_INSTANTIATION_FAILED:
  1064. /* ignore due to instantitate timeout a while ago */
  1065. break;
  1066. default:
  1067. assert (0);
  1068. break;
  1069. }
  1070. return SA_AIS_OK;
  1071. }
  1072. void amf_comp_error_report (struct amf_comp *comp, SaAmfRecommendedRecoveryT recommendedRecovery)
  1073. {
  1074. struct res_lib_amf_componenterrorreport res_lib;
  1075. TRACE2("Exec comp error report '%s'", comp->name.value);
  1076. if (amf_su_is_local (comp->su)) {
  1077. res_lib.header.size = sizeof (struct res_lib_amf_componenterrorreport);
  1078. res_lib.header.id = MESSAGE_RES_AMF_COMPONENTERRORREPORT;
  1079. res_lib.header.error = SA_AIS_OK;
  1080. openais_conn_send_response (comp->conn, &res_lib, sizeof (res_lib));
  1081. }
  1082. /* report to SU and let it handle the problem */
  1083. report_error_suspected (comp, recommendedRecovery);
  1084. }
  1085. /**
  1086. * Healthcheck timeout event handler
  1087. * @param comp
  1088. * @param healthcheck
  1089. */
  1090. void amf_comp_healthcheck_tmo (
  1091. struct amf_comp *comp, struct amf_healthcheck *healthcheck)
  1092. {
  1093. TRACE2("Exec healthcheck tmo for '%s'", comp->name.value);
  1094. /* report to SU and let it handle the problem */
  1095. report_error_suspected (comp, healthcheck->recommendedRecovery);
  1096. }
  1097. static void clear_ha_state (
  1098. struct amf_comp *comp, struct amf_csi_assignment *csi_assignment)
  1099. {
  1100. ENTER ("");
  1101. csi_assignment->saAmfCSICompHAState = 0;
  1102. }
  1103. /**
  1104. * Event method to be called when a cleanup completed event is received
  1105. * @param comp
  1106. */
  1107. void amf_comp_cleanup_completed (struct amf_comp *comp)
  1108. {
  1109. TRACE2("Exec CLC cleanup completed for '%s'", comp->name.value);
  1110. /* Set all CSI's confirmed HA state to unknown */
  1111. amf_comp_foreach_csi_assignment (comp, clear_ha_state);
  1112. /* clear error suspected flag, component is terminated now */
  1113. comp->error_suspected = 0;
  1114. if (comp->saAmfCompPresenceState == SA_AMF_PRESENCE_RESTARTING) {
  1115. amf_comp_instantiate (comp);
  1116. } else {
  1117. comp_presence_state_set (comp, SA_AMF_PRESENCE_UNINSTANTIATED);
  1118. }
  1119. }
  1120. /**
  1121. * Handle the request from a component to start a healthcheck
  1122. *
  1123. * @param comp
  1124. * @param healthcheckKey
  1125. * @param invocationType
  1126. * @param recommendedRecovery
  1127. *
  1128. * @return SaAisErrorT - return value to component
  1129. */
  1130. SaAisErrorT amf_comp_healthcheck_start (
  1131. struct amf_comp *comp,
  1132. SaAmfHealthcheckKeyT *healthcheckKey,
  1133. SaAmfHealthcheckInvocationT invocationType,
  1134. SaAmfRecommendedRecoveryT recommendedRecovery)
  1135. {
  1136. struct amf_healthcheck *healthcheck;
  1137. SaAisErrorT error = SA_AIS_OK;
  1138. healthcheck = amf_comp_find_healthcheck (comp, healthcheckKey);
  1139. if (healthcheck == 0) {
  1140. log_printf (LOG_ERR, "Healthcheckstart: Healthcheck '%s' not found",
  1141. healthcheckKey->key);
  1142. error = SA_AIS_ERR_NOT_EXIST;
  1143. goto error_exit;
  1144. }
  1145. dprintf ("Healthcheckstart: '%s', key '%s'",
  1146. comp->name.value, healthcheckKey->key);
  1147. /*
  1148. * Determine if this healthcheck is already active
  1149. */
  1150. if (healthcheck->active) {
  1151. error = SA_AIS_ERR_EXIST;
  1152. goto error_exit;
  1153. }
  1154. /*
  1155. * Initialise
  1156. */
  1157. healthcheck->invocationType = invocationType;
  1158. healthcheck->recommendedRecovery = recommendedRecovery;
  1159. healthcheck->timer_handle_duration = 0;
  1160. healthcheck->timer_handle_period = 0;
  1161. healthcheck->active = 1;
  1162. if (invocationType == SA_AMF_HEALTHCHECK_AMF_INVOKED) {
  1163. /* start timer to execute first healthcheck request */
  1164. poll_timer_add (aisexec_poll_handle,
  1165. healthcheck->saAmfHealthcheckPeriod,
  1166. (void *)healthcheck,
  1167. timer_function_healthcheck_next_fn,
  1168. &healthcheck->timer_handle_period);
  1169. } else if (invocationType == SA_AMF_HEALTHCHECK_COMPONENT_INVOKED) {
  1170. /* start supervision timer */
  1171. poll_timer_add (aisexec_poll_handle,
  1172. healthcheck->saAmfHealthcheckPeriod,
  1173. (void *)healthcheck,
  1174. timer_function_healthcheck_tmo,
  1175. &healthcheck->timer_handle_period);
  1176. } else {
  1177. error = SA_AIS_ERR_INVALID_PARAM;
  1178. }
  1179. error_exit:
  1180. return error;
  1181. }
  1182. /**
  1183. * Stop all or a specifed healthcheck
  1184. * @param comp
  1185. * @param healthcheckKey - NULL if all
  1186. *
  1187. * @return SaAisErrorT
  1188. */
  1189. SaAisErrorT amf_comp_healthcheck_stop (
  1190. struct amf_comp *comp,
  1191. SaAmfHealthcheckKeyT *healthcheckKey)
  1192. {
  1193. struct amf_healthcheck *healthcheck;
  1194. SaAisErrorT error = SA_AIS_OK;
  1195. dprintf ("Healthcheckstop: '%s'", comp->name.value);
  1196. if (!amf_su_is_local (comp->su)) {
  1197. return SA_AIS_OK;
  1198. }
  1199. if (healthcheckKey == NULL) {
  1200. for (healthcheck = comp->healthcheck_head;
  1201. healthcheck != NULL;
  1202. healthcheck = healthcheck->next) {
  1203. healthcheck_deactivate (healthcheck);
  1204. }
  1205. } else {
  1206. healthcheck = amf_comp_find_healthcheck (comp, healthcheckKey);
  1207. if (healthcheck == NULL) {
  1208. log_printf (LOG_ERR, "Healthcheckstop: Healthcheck '%s' not found",
  1209. healthcheckKey->key);
  1210. error = SA_AIS_ERR_NOT_EXIST;
  1211. } else {
  1212. healthcheck_deactivate (healthcheck);
  1213. }
  1214. }
  1215. return error;
  1216. }
  1217. /**
  1218. * Instantiate a component
  1219. * @param comp
  1220. */
  1221. void amf_comp_instantiate (struct amf_comp *comp)
  1222. {
  1223. ENTER ("'%s' SU '%s'", getSaNameT (&comp->name),
  1224. getSaNameT (&comp->su->name));
  1225. switch (comp->saAmfCompPresenceState) {
  1226. case SA_AMF_PRESENCE_RESTARTING:
  1227. /* fall through */
  1228. case SA_AMF_PRESENCE_UNINSTANTIATED:
  1229. if (amf_su_is_local (comp->su)) {
  1230. TRACE1("Send instantiate event for comp '%s' from host %s",
  1231. comp->name.value, comp->su->saAmfSUHostedByNode.value);
  1232. SaNameT compName;
  1233. amf_comp_dn_make (comp, &compName);
  1234. amf_msg_mcast (MESSAGE_REQ_EXEC_AMF_COMPONENT_INSTANTIATE,
  1235. &compName, sizeof (SaNameT));
  1236. }
  1237. break;
  1238. default:
  1239. dprintf("Instantiate ignored in Component presence state %d",
  1240. comp->saAmfCompPresenceState);
  1241. break;
  1242. }
  1243. }
  1244. void amf_comp_instantiate_tmo_event (struct amf_comp *comp)
  1245. {
  1246. ENTER ("Comp instantiate timeout after %d seconds '%s' '%s'",
  1247. comp->saAmfCompInstantiateTimeout, comp->su->name.value,
  1248. comp->name.value);
  1249. switch (comp->saAmfCompPresenceState) {
  1250. case SA_AMF_PRESENCE_RESTARTING:
  1251. amf_comp_operational_state_set (comp, SA_AMF_OPERATIONAL_DISABLED);
  1252. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATION_FAILED);
  1253. break;
  1254. case SA_AMF_PRESENCE_INSTANTIATING:
  1255. amf_comp_operational_state_set (comp, SA_AMF_OPERATIONAL_DISABLED);
  1256. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATION_FAILED);
  1257. break;
  1258. default:
  1259. assert (0);
  1260. break;
  1261. }
  1262. }
  1263. void amf_comp_instantiate_event (struct amf_comp *component)
  1264. {
  1265. int res;
  1266. ENTER ("");
  1267. switch (component->saAmfCompPresenceState) {
  1268. case SA_AMF_PRESENCE_INSTANTIATING:
  1269. case SA_AMF_PRESENCE_INSTANTIATED:
  1270. case SA_AMF_PRESENCE_TERMINATING:
  1271. case SA_AMF_PRESENCE_INSTANTIATION_FAILED:
  1272. case SA_AMF_PRESENCE_TERMINATION_FAILED:
  1273. dprintf("Instantiate ignored in Component presence state %d",
  1274. component->saAmfCompPresenceState);
  1275. break;
  1276. case SA_AMF_PRESENCE_UNINSTANTIATED:
  1277. comp_presence_state_set (component, SA_AMF_PRESENCE_INSTANTIATING);
  1278. amf_su_comp_state_changed(component->su,
  1279. component,SA_AMF_PRESENCE_STATE,SA_AMF_PRESENCE_INSTANTIATING);
  1280. if (amf_su_is_local (component->su)) {
  1281. res = clc_interfaces[component->comptype]->instantiate (
  1282. component);
  1283. }
  1284. break;
  1285. case SA_AMF_PRESENCE_RESTARTING:
  1286. if (amf_su_is_local (component->su)) {
  1287. res = clc_interfaces[component->comptype]->instantiate (
  1288. component);
  1289. }
  1290. break;
  1291. default:
  1292. dprintf("Component presence state %d",
  1293. component->saAmfCompPresenceState);
  1294. assert (0);
  1295. break;
  1296. }
  1297. }
  1298. void amf_comp_readiness_state_set (struct amf_comp *comp,
  1299. SaAmfReadinessStateT state)
  1300. {
  1301. TRACE1 ("Setting comp '%s' readiness state: %s\n",
  1302. comp->name.value, amf_readiness_state (state));
  1303. }
  1304. /**
  1305. * Handle a component response (received from the lib) of an earlier AMF request.
  1306. * This function should be invoked when the lib request is received.
  1307. * @param invocation [in] associates the response with the request (callback)
  1308. * @param error [in] response from the component of the associated callback
  1309. * @param retval [out] contains return value to component when needed
  1310. *
  1311. * @return ==0 respond to component, do not multicast
  1312. * @return >0 do not respond to component, multicast response
  1313. */
  1314. int amf_comp_response_1 (
  1315. SaInvocationT invocation, SaAisErrorT error, SaAisErrorT *retval,
  1316. SaUint32T *interface, SaNameT *dn)
  1317. {
  1318. int res;
  1319. void *data;
  1320. res = invocation_get_and_destroy (invocation, interface, &data);
  1321. if (res == -1) {
  1322. log_printf (LOG_ERR, "Lib response: invocation not found\n");
  1323. *retval = SA_AIS_ERR_INVALID_PARAM;
  1324. return 0;
  1325. }
  1326. switch (*interface) {
  1327. case AMF_RESPONSE_HEALTHCHECKCALLBACK: {
  1328. struct amf_healthcheck *healthcheck = data;
  1329. SaNameT name;
  1330. TRACE7 ("Healthcheck response from '%s': %d",
  1331. amf_comp_dn_make (healthcheck->comp, &name), error);
  1332. if (healthcheck->invocationType == SA_AMF_HEALTHCHECK_AMF_INVOKED) {
  1333. /* the response was on time, delete supervision timer */
  1334. poll_timer_delete (aisexec_poll_handle,
  1335. healthcheck->timer_handle_duration);
  1336. healthcheck->timer_handle_duration = 0;
  1337. /* start timer to execute next healthcheck request */
  1338. poll_timer_add (aisexec_poll_handle,
  1339. healthcheck->saAmfHealthcheckPeriod,
  1340. (void *)healthcheck,
  1341. timer_function_healthcheck_next_fn,
  1342. &healthcheck->timer_handle_period);
  1343. *retval = SA_AIS_OK;
  1344. } else {
  1345. *retval = SA_AIS_ERR_INVALID_PARAM;
  1346. }
  1347. return 0; /* do not multicast event */
  1348. break;
  1349. }
  1350. case AMF_RESPONSE_CSISETCALLBACK: /* fall-through */
  1351. case AMF_RESPONSE_CSIREMOVECALLBACK:
  1352. amf_csi_assignment_dn_make (data, dn);
  1353. return 1; /* multicast event */
  1354. break;
  1355. #if 0
  1356. case AMF_RESPONSE_COMPONENTTERMINATECALLBACK: {
  1357. struct component_terminate_callback_data *component_terminate_callback_data;
  1358. component_terminate_callback_data = data;
  1359. dprintf ("Lib component terminate callback response, error: %d", error);
  1360. amf_comp_healthcheck_deactivate (component_terminate_callback_data->comp);
  1361. escalation_policy_restart (component_terminate_callback_data->comp);
  1362. return 1;
  1363. break;
  1364. }
  1365. #endif
  1366. default:
  1367. assert (0);
  1368. break;
  1369. }
  1370. /* XXX we fall here in case NDEBUG is set */
  1371. *retval = -1;
  1372. return 0;
  1373. }
  1374. /**
  1375. * Handle a component response (received from EVS) of an earlier AMF request.
  1376. * This function should be invoked when the multicast request is received.
  1377. * @param invocation [in] associates the response with the request (callback)
  1378. * @param error [in] response from the component of the associated callback
  1379. * @param retval [out] contains return value to component when needed
  1380. *
  1381. * @return component to which the response should be sent
  1382. */
  1383. struct amf_comp *amf_comp_response_2 (
  1384. SaUint32T interface, SaNameT *dn, SaAisErrorT error, SaAisErrorT *retval)
  1385. {
  1386. struct amf_csi_assignment *csi_assignment;
  1387. struct amf_comp *comp = NULL;
  1388. assert (retval != NULL);
  1389. *retval = SA_AIS_OK;
  1390. switch (interface) {
  1391. case AMF_RESPONSE_CSISETCALLBACK: {
  1392. csi_assignment = amf_csi_assignment_find (amf_cluster, dn);
  1393. assert (csi_assignment != NULL);
  1394. comp = csi_assignment->comp;
  1395. dprintf ("CSI '%s' set callback response from '%s', error: %d",
  1396. csi_assignment->csi->name.value,
  1397. csi_assignment->comp->name.value, error);
  1398. comp = csi_assignment->comp;
  1399. if (error == SA_AIS_OK) {
  1400. comp_ha_state_set (
  1401. comp, csi_assignment, csi_assignment->requested_ha_state);
  1402. } else if (error == SA_AIS_ERR_FAILED_OPERATION) {
  1403. amf_si_comp_set_ha_state_failed (csi_assignment->csi->si,
  1404. csi_assignment);
  1405. } else {
  1406. *retval = SA_AIS_ERR_INVALID_PARAM;
  1407. }
  1408. break;
  1409. }
  1410. case AMF_RESPONSE_CSIREMOVECALLBACK: {
  1411. csi_assignment = amf_csi_assignment_find (amf_cluster, dn);
  1412. assert (csi_assignment != NULL);
  1413. dprintf ("Lib csi '%s' remove callback response from '%s', error: %d",
  1414. csi_assignment->csi->name.value,
  1415. csi_assignment->comp->name.value, error);
  1416. comp = csi_assignment->comp;
  1417. if (error == SA_AIS_OK) {
  1418. comp_ha_state_set (comp, csi_assignment,
  1419. csi_assignment->requested_ha_state);
  1420. } else if (error == SA_AIS_ERR_FAILED_OPERATION) {
  1421. amf_si_comp_set_ha_state_failed (csi_assignment->csi->si,
  1422. csi_assignment);
  1423. } else {
  1424. *retval = SA_AIS_ERR_INVALID_PARAM;
  1425. }
  1426. break;
  1427. }
  1428. #if 0
  1429. case AMF_RESPONSE_COMPONENTTERMINATECALLBACK: {
  1430. struct component_terminate_callback_data *callback_data = data;
  1431. dprintf ("Lib comp '%s' terminate callback response, error: %d",
  1432. callback_data->comp->name.value, error);
  1433. comp_presence_state_set (callback_data->comp,
  1434. SA_AMF_PRESENCE_UNINSTANTIATED);
  1435. break;
  1436. }
  1437. #endif
  1438. default:
  1439. assert (0);
  1440. break;
  1441. }
  1442. return comp;
  1443. }
  1444. /**
  1445. * Request a component to assume a particular HA state
  1446. * @param comp
  1447. * @param csi_assignment
  1448. * @param requested_ha_state
  1449. */
  1450. void amf_comp_hastate_set (
  1451. struct amf_comp *component,
  1452. struct amf_csi_assignment *csi_assignment)
  1453. {
  1454. ENTER ("'%s'", csi_assignment->csi->name.value);
  1455. assert (component != NULL && csi_assignment != NULL);
  1456. if (!component->error_suspected) {
  1457. lib_csi_set_request(component, csi_assignment);
  1458. } else {
  1459. if (csi_assignment->requested_ha_state == SA_AMF_HA_QUIESCED) {
  1460. csi_assignment->saAmfCSICompHAState = csi_assignment->requested_ha_state;
  1461. } else {
  1462. assert (0);
  1463. }
  1464. }
  1465. LEAVE("");
  1466. }
  1467. /**
  1468. * Request termination of a component
  1469. * @param comp
  1470. */
  1471. void amf_comp_terminate (struct amf_comp *comp)
  1472. {
  1473. dprintf ("comp terminate '%s'\n", getSaNameT (&comp->name));
  1474. comp_presence_state_set (comp, SA_AMF_PRESENCE_TERMINATING);
  1475. if (amf_su_is_local (comp->su)) {
  1476. amf_comp_healthcheck_stop (comp, NULL);
  1477. if (comp->error_suspected) {
  1478. clc_interfaces[comp->comptype]->cleanup (comp);
  1479. } else {
  1480. clc_interfaces[comp->comptype]->terminate (comp);
  1481. }
  1482. }
  1483. }
  1484. /**
  1485. * Request restart of a component
  1486. * @param comp
  1487. */
  1488. void amf_comp_restart (struct amf_comp *comp)
  1489. {
  1490. dprintf ("comp restart '%s'\n", getSaNameT (&comp->name));
  1491. comp_presence_state_set (comp, SA_AMF_PRESENCE_RESTARTING);
  1492. comp->saAmfCompRestartCount += 1;
  1493. if (amf_su_is_local (comp->su)) {
  1494. amf_comp_healthcheck_stop (comp, NULL);
  1495. clc_interfaces[comp->comptype]->cleanup (comp);
  1496. }
  1497. }
  1498. /**
  1499. * Request to return the HA state for a components CSI
  1500. * @param comp
  1501. * @param csi_name
  1502. * @param ha_state
  1503. *
  1504. * @return SaAisErrorT
  1505. */
  1506. SaAisErrorT amf_comp_hastate_get (
  1507. struct amf_comp *comp, SaNameT *csi_name, SaAmfHAStateT *ha_state)
  1508. {
  1509. struct amf_csi_assignment *assignment;
  1510. assert (comp != NULL && csi_name != NULL && ha_state != NULL);
  1511. dprintf ("comp ha state get from comp '%s' CSI '%s'\n",
  1512. getSaNameT (&comp->name), csi_name->value);
  1513. assignment = csi_assignment_find_in (comp, csi_name);
  1514. if (assignment != NULL) {
  1515. *ha_state = assignment->saAmfCSICompHAState;
  1516. return SA_AIS_OK;
  1517. }
  1518. return SA_AIS_ERR_INVALID_PARAM;
  1519. }
  1520. /**
  1521. * Response from a component informs AMF that it has performed a healthcheck
  1522. * @param comp
  1523. * @param healthcheckKey
  1524. * @param healthcheckResult
  1525. *
  1526. * @return SaAisErrorT
  1527. */
  1528. SaAisErrorT amf_comp_healthcheck_confirm (
  1529. struct amf_comp *comp,
  1530. SaAmfHealthcheckKeyT *healthcheckKey,
  1531. SaAisErrorT healthcheckResult)
  1532. {
  1533. struct amf_healthcheck *healthcheck;
  1534. SaAisErrorT error = SA_AIS_OK;
  1535. dprintf ("Healthcheckconfirm: '%s', key '%s'",
  1536. comp->name.value, healthcheckKey->key);
  1537. healthcheck = amf_comp_find_healthcheck (comp, healthcheckKey);
  1538. if (healthcheck == NULL) {
  1539. log_printf (LOG_ERR, "Healthcheckstop: Healthcheck '%s' not found",
  1540. healthcheckKey->key);
  1541. error = SA_AIS_ERR_NOT_EXIST;
  1542. } else if (healthcheck->active) {
  1543. if (healthcheckResult == SA_AIS_OK) {
  1544. /* the response was on time, restart the supervision timer */
  1545. poll_timer_delete (aisexec_poll_handle,
  1546. healthcheck->timer_handle_period);
  1547. poll_timer_add (aisexec_poll_handle,
  1548. healthcheck->saAmfHealthcheckPeriod,
  1549. (void *)healthcheck,
  1550. timer_function_healthcheck_tmo,
  1551. &healthcheck->timer_handle_period);
  1552. } else if (healthcheckResult == SA_AIS_ERR_FAILED_OPERATION) {
  1553. /* send to cluster */
  1554. mcast_healthcheck_tmo_event (healthcheck);
  1555. } else {
  1556. error = SA_AIS_ERR_INVALID_PARAM;
  1557. }
  1558. } else {
  1559. error = SA_AIS_ERR_INVALID_PARAM;
  1560. }
  1561. return error;
  1562. }
  1563. void amf_comp_init (void)
  1564. {
  1565. log_init ("AMF");
  1566. }
  1567. void amf_comp_operational_state_set (struct amf_comp *comp,
  1568. SaAmfOperationalStateT oper_state)
  1569. {
  1570. comp->saAmfCompOperState = oper_state;
  1571. TRACE1 ("Setting comp '%s', SU '%s' operational state: %s\n",
  1572. comp->name.value, comp->su->name.value,
  1573. amf_op_state (comp->saAmfCompOperState));
  1574. amf_su_comp_state_changed (
  1575. comp->su, comp, SA_AMF_OP_STATE, oper_state);
  1576. }
  1577. int amf_comp_get_saAmfCompNumCurrActiveCsi(struct amf_comp *component)
  1578. {
  1579. int cnt = 0;
  1580. struct amf_csi_assignment *csi_assignment;
  1581. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  1582. while (csi_assignment != NULL) {
  1583. if (csi_assignment->saAmfCSICompHAState == SA_AMF_HA_ACTIVE) {
  1584. cnt++;
  1585. }
  1586. csi_assignment = amf_comp_get_next_csi_assignment (
  1587. component, csi_assignment);
  1588. }
  1589. return cnt;
  1590. }
  1591. int amf_comp_get_saAmfCompNumCurrStandbyCsi(struct amf_comp *component)
  1592. {
  1593. int cnt = 0;
  1594. struct amf_csi_assignment *csi_assignment;
  1595. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  1596. while (csi_assignment != NULL) {
  1597. if (csi_assignment->saAmfCSICompHAState == SA_AMF_HA_STANDBY) {
  1598. cnt++;
  1599. }
  1600. csi_assignment = amf_comp_get_next_csi_assignment (
  1601. component, csi_assignment);
  1602. }
  1603. return cnt;
  1604. }
  1605. SaAmfReadinessStateT amf_comp_get_saAmfCompReadinessState (
  1606. struct amf_comp *component)
  1607. {
  1608. if (component->saAmfCompOperState == SA_AMF_OPERATIONAL_ENABLED) {
  1609. return amf_su_get_saAmfSUReadinessState (component->su);
  1610. } else if (component->saAmfCompOperState == SA_AMF_OPERATIONAL_DISABLED) {
  1611. return SA_AMF_READINESS_OUT_OF_SERVICE;
  1612. }
  1613. assert (0);
  1614. /* XXX we fall here in case NDEBUG is set */
  1615. return -1;
  1616. }
  1617. /**
  1618. * Component is informed that the node where the 'real'
  1619. * component process is executing has unexpectadly left the
  1620. * node. If there is a pending interaction between AMF
  1621. * (component) and the 'real' component process, then component
  1622. * will indicate to its subordinate objects the the interaction
  1623. * failed. Pending presence state changes is indicated by
  1624. * reporting the new state is uninstantiated while pending csi
  1625. * operations are indicated by 'operation failed'.
  1626. * @param comp
  1627. *
  1628. * @return void
  1629. */
  1630. void amf_comp_node_left (struct amf_comp *component)
  1631. {
  1632. int change_pending = 0;
  1633. struct amf_csi_assignment *csi_assignment;
  1634. ENTER("");
  1635. if (component->saAmfCompPresenceState == SA_AMF_PRESENCE_INSTANTIATING ||
  1636. component->saAmfCompPresenceState == SA_AMF_PRESENCE_RESTARTING ||
  1637. component->saAmfCompPresenceState == SA_AMF_PRESENCE_TERMINATING) {
  1638. change_pending = 1;
  1639. }
  1640. component->saAmfCompPresenceState = SA_AMF_PRESENCE_UNINSTANTIATED;
  1641. if (amf_su_presence_state_all_comps_in_su_are_set (component->su,
  1642. SA_AMF_PRESENCE_UNINSTANTIATED) != 0) {
  1643. component->su->saAmfSUPresenceState = SA_AMF_PRESENCE_UNINSTANTIATED;
  1644. }
  1645. if (change_pending) {
  1646. change_pending =0;
  1647. amf_su_comp_state_changed ( component->su,
  1648. component,
  1649. SA_AMF_PRESENCE_STATE,
  1650. SA_AMF_PRESENCE_UNINSTANTIATED);
  1651. }
  1652. if (component->saAmfCompOperState == SA_AMF_OPERATIONAL_ENABLED) {
  1653. change_pending = 1;
  1654. }
  1655. component->saAmfCompOperState = SA_AMF_OPERATIONAL_DISABLED;
  1656. if (change_pending) {
  1657. change_pending =0;
  1658. amf_su_comp_state_changed ( component->su,
  1659. component,
  1660. SA_AMF_OP_STATE,
  1661. SA_AMF_OPERATIONAL_DISABLED);
  1662. }
  1663. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  1664. while (csi_assignment != NULL) {
  1665. if (csi_assignment->requested_ha_state !=
  1666. csi_assignment->saAmfCSICompHAState) {
  1667. amf_si_comp_set_ha_state_failed (
  1668. csi_assignment->csi->si,csi_assignment);
  1669. }
  1670. csi_assignment = amf_comp_get_next_csi_assignment (
  1671. component, csi_assignment);
  1672. }
  1673. }
  1674. /**
  1675. * Serialize a component including variable length arrays and
  1676. * strings to a buffer returned. Buffer is to be freed by
  1677. * caller.
  1678. * @param component
  1679. * @param len
  1680. *
  1681. * @return void*
  1682. */
  1683. void *amf_comp_serialize (struct amf_comp *component, int *len)
  1684. {
  1685. char *buf = NULL;
  1686. int i, offset = 0, size = 0;
  1687. TRACE8 ("%s", component->name.value);
  1688. buf = amf_serialize_SaNameT (buf, &size, &offset, &component->name);
  1689. /* count cstypes and write to buf */
  1690. for (i = 0; component->saAmfCompCsTypes &&
  1691. component->saAmfCompCsTypes[i] != NULL; i++);
  1692. buf = amf_serialize_SaUint32T (buf, &size, &offset, i);
  1693. for (i = 0; component->saAmfCompCsTypes &&
  1694. component->saAmfCompCsTypes[i] != NULL; i++) {
  1695. buf = amf_serialize_SaNameT (
  1696. buf, &size, &offset, component->saAmfCompCsTypes[i]);
  1697. }
  1698. buf = amf_serialize_SaUint32T (
  1699. buf, &size, &offset, component->saAmfCompCategory);
  1700. buf = amf_serialize_SaUint32T (
  1701. buf, &size, &offset, component->saAmfCompCapability);
  1702. buf = amf_serialize_SaUint32T (
  1703. buf, &size, &offset, component->saAmfCompNumMaxActiveCsi);
  1704. buf = amf_serialize_SaUint32T (
  1705. buf, &size, &offset, component->saAmfCompNumMaxStandbyCsi);
  1706. /* count environment vars and write to buf */
  1707. for (i = 0; component->saAmfCompCmdEnv &&
  1708. component->saAmfCompCmdEnv[i] != NULL; i++);
  1709. buf = amf_serialize_SaUint32T (buf, &size, &offset, i);
  1710. for (i = 0; component->saAmfCompCmdEnv &&
  1711. component->saAmfCompCmdEnv[i] != NULL; i++) {
  1712. buf = amf_serialize_SaStringT (
  1713. buf, &size, &offset, component->saAmfCompCmdEnv[i]);
  1714. }
  1715. buf = amf_serialize_SaUint32T (
  1716. buf, &size, &offset, component->saAmfCompDefaultClcCliTimeout);
  1717. buf = amf_serialize_SaUint32T (
  1718. buf, &size, &offset, component->saAmfCompDefaultCallbackTimeOut);
  1719. buf = amf_serialize_SaStringT (
  1720. buf, &size, &offset, component->saAmfCompInstantiateCmd);
  1721. buf = amf_serialize_SaStringT (
  1722. buf, &size, &offset, component->saAmfCompInstantiateCmdArgv);
  1723. buf = amf_serialize_SaUint32T (
  1724. buf, &size, &offset, component->saAmfCompInstantiateTimeout);
  1725. buf = amf_serialize_SaUint32T (
  1726. buf, &size, &offset, component->saAmfCompInstantiationLevel);
  1727. buf = amf_serialize_SaUint32T (
  1728. buf, &size, &offset, component->saAmfCompNumMaxInstantiateWithoutDelay);
  1729. buf = amf_serialize_SaUint32T (
  1730. buf, &size, &offset, component->saAmfCompNumMaxInstantiateWithDelay);
  1731. buf = amf_serialize_SaUint32T (
  1732. buf, &size, &offset, component->saAmfCompDelayBetweenInstantiateAttempts);
  1733. buf = amf_serialize_SaStringT (
  1734. buf, &size, &offset, component->saAmfCompTerminateCmd);
  1735. buf = amf_serialize_SaUint32T (
  1736. buf, &size, &offset, component->saAmfCompTerminateTimeout);
  1737. buf = amf_serialize_SaStringT (
  1738. buf, &size, &offset, component->saAmfCompTerminateCmdArgv);
  1739. buf = amf_serialize_SaStringT (
  1740. buf, &size, &offset, component->saAmfCompCleanupCmd);
  1741. buf = amf_serialize_SaUint32T (
  1742. buf, &size, &offset, component->saAmfCompCleanupTimeout);
  1743. buf = amf_serialize_SaStringT (
  1744. buf, &size, &offset, component->saAmfCompCleanupCmdArgv);
  1745. buf = amf_serialize_SaStringT (
  1746. buf, &size, &offset, component->saAmfCompAmStartCmd);
  1747. buf = amf_serialize_SaUint32T (
  1748. buf, &size, &offset, component->saAmfCompAmStartTimeout);
  1749. buf = amf_serialize_SaStringT (
  1750. buf, &size, &offset, component->saAmfCompAmStartCmdArgv);
  1751. buf = amf_serialize_SaUint32T (
  1752. buf, &size, &offset, component->saAmfCompNumMaxAmStartAttempt);
  1753. buf = amf_serialize_SaStringT (
  1754. buf, &size, &offset, component->saAmfCompAmStopCmd);
  1755. buf = amf_serialize_SaUint32T (
  1756. buf, &size, &offset, component->saAmfCompAmStopTimeout);
  1757. buf = amf_serialize_SaStringT (
  1758. buf, &size, &offset, component->saAmfCompAmStopCmdArgv);
  1759. buf = amf_serialize_SaUint32T (
  1760. buf, &size, &offset, component->saAmfCompNumMaxAmStopAttempt);
  1761. buf = amf_serialize_SaUint32T (
  1762. buf, &size, &offset, component->saAmfCompTerminateCallbackTimeout);
  1763. buf = amf_serialize_SaUint32T (
  1764. buf, &size, &offset, component->saAmfCompCSISetCallbackTimeout);
  1765. buf = amf_serialize_SaUint32T (
  1766. buf, &size, &offset, component->saAmfCompQuiescingCompleteTimeout);
  1767. buf = amf_serialize_SaUint32T (
  1768. buf, &size, &offset, component->saAmfCompCSIRmvCallbackTimeout);
  1769. buf = amf_serialize_SaUint32T (
  1770. buf, &size, &offset, component->saAmfCompRecoveryOnError);
  1771. buf = amf_serialize_SaUint32T (
  1772. buf, &size, &offset, component->saAmfCompDisableRestart);
  1773. buf = amf_serialize_SaNameT (
  1774. buf, &size, &offset, &component->saAmfCompProxyCsi);
  1775. buf = amf_serialize_SaUint32T (
  1776. buf, &size, &offset, component->saAmfCompOperState);
  1777. buf = amf_serialize_SaUint32T (
  1778. buf, &size, &offset, component->saAmfCompPresenceState);
  1779. buf = amf_serialize_SaUint32T (
  1780. buf, &size, &offset, component->saAmfCompRestartCount);
  1781. buf = amf_serialize_SaNameT (
  1782. buf, &size, &offset, &component->saAmfCompCurrProxyName);
  1783. buf = amf_serialize_SaStringT (
  1784. buf, &size, &offset, component->clccli_path);
  1785. buf = amf_serialize_SaUint32T (
  1786. buf, &size, &offset, component->comptype);
  1787. buf = amf_serialize_SaUint32T (
  1788. buf, &size, &offset, component->error_suspected);
  1789. *len = offset;
  1790. return buf;
  1791. }
  1792. /**
  1793. * Deserialize a buffer into a AMF component object.
  1794. * @param su
  1795. * @param buf
  1796. * @param size
  1797. *
  1798. * @return struct amf_comp*
  1799. */
  1800. struct amf_comp *amf_comp_deserialize (struct amf_su *su, char *buf)
  1801. {
  1802. char *tmp = buf;
  1803. int i;
  1804. SaUint32T cnt;
  1805. struct amf_comp *component = amf_comp_new (su, "");
  1806. tmp = amf_deserialize_SaNameT (tmp, &component->name);
  1807. tmp = amf_deserialize_SaUint32T (tmp, &cnt);
  1808. component->saAmfCompCsTypes = amf_malloc ((cnt + 1) * sizeof (SaNameT*));
  1809. for (i = 0; i < cnt; i++) {
  1810. component->saAmfCompCsTypes[i] = amf_malloc (sizeof (SaNameT));
  1811. tmp = amf_deserialize_SaNameT (tmp, component->saAmfCompCsTypes[i]);
  1812. }
  1813. component->saAmfCompCsTypes[i] = NULL;
  1814. tmp = amf_deserialize_SaUint32T (tmp, &component->saAmfCompCategory);
  1815. tmp = amf_deserialize_SaUint32T (tmp, &component->saAmfCompCapability);
  1816. tmp = amf_deserialize_SaUint32T (tmp, &component->saAmfCompNumMaxActiveCsi);
  1817. tmp = amf_deserialize_SaUint32T (tmp, &component->saAmfCompNumMaxStandbyCsi);
  1818. tmp = amf_deserialize_SaUint32T (tmp, &cnt);
  1819. component->saAmfCompCmdEnv = amf_malloc ((cnt + 1) * sizeof (SaStringT*));
  1820. for (i = 0; i < cnt; i++) {
  1821. tmp = amf_deserialize_SaStringT (tmp, &component->saAmfCompCmdEnv[i]);
  1822. }
  1823. component->saAmfCompCmdEnv[i] = NULL;
  1824. tmp = amf_deserialize_SaUint32T (
  1825. tmp, &component->saAmfCompDefaultClcCliTimeout);
  1826. tmp = amf_deserialize_SaUint32T (
  1827. tmp, &component->saAmfCompDefaultCallbackTimeOut);
  1828. tmp = amf_deserialize_SaStringT (
  1829. tmp, &component->saAmfCompInstantiateCmd);
  1830. tmp = amf_deserialize_SaStringT (
  1831. tmp, &component->saAmfCompInstantiateCmdArgv);
  1832. tmp = amf_deserialize_SaUint32T (
  1833. tmp, &component->saAmfCompInstantiateTimeout);
  1834. tmp = amf_deserialize_SaUint32T (
  1835. tmp, &component->saAmfCompInstantiationLevel);
  1836. tmp = amf_deserialize_SaUint32T (
  1837. tmp, &component->saAmfCompNumMaxInstantiateWithoutDelay);
  1838. tmp = amf_deserialize_SaUint32T (
  1839. tmp, &component->saAmfCompNumMaxInstantiateWithDelay);
  1840. tmp = amf_deserialize_SaUint32T (
  1841. tmp, &component->saAmfCompDelayBetweenInstantiateAttempts);
  1842. tmp = amf_deserialize_SaStringT (
  1843. tmp, &component->saAmfCompTerminateCmd);
  1844. tmp = amf_deserialize_SaUint32T (
  1845. tmp, &component->saAmfCompTerminateTimeout);
  1846. tmp = amf_deserialize_SaStringT (
  1847. tmp, &component->saAmfCompTerminateCmdArgv);
  1848. tmp = amf_deserialize_SaStringT (
  1849. tmp, &component->saAmfCompCleanupCmd);
  1850. tmp = amf_deserialize_SaUint32T (
  1851. tmp, &component->saAmfCompCleanupTimeout);
  1852. tmp = amf_deserialize_SaStringT (
  1853. tmp, &component->saAmfCompCleanupCmdArgv);
  1854. tmp = amf_deserialize_SaStringT (
  1855. tmp, &component->saAmfCompAmStartCmd);
  1856. tmp = amf_deserialize_SaUint32T (
  1857. tmp, &component->saAmfCompAmStartTimeout);
  1858. tmp = amf_deserialize_SaStringT (
  1859. tmp, &component->saAmfCompAmStartCmdArgv);
  1860. tmp = amf_deserialize_SaUint32T (
  1861. tmp, &component->saAmfCompNumMaxAmStartAttempt);
  1862. tmp = amf_deserialize_SaStringT (
  1863. tmp, &component->saAmfCompAmStopCmd);
  1864. tmp = amf_deserialize_SaUint32T (
  1865. tmp, &component->saAmfCompAmStopTimeout);
  1866. tmp = amf_deserialize_SaStringT (
  1867. tmp, &component->saAmfCompAmStopCmdArgv);
  1868. tmp = amf_deserialize_SaUint32T (
  1869. tmp, &component->saAmfCompNumMaxAmStopAttempt);
  1870. tmp = amf_deserialize_SaUint32T (
  1871. tmp, &component->saAmfCompTerminateCallbackTimeout);
  1872. tmp = amf_deserialize_SaUint32T (
  1873. tmp, &component->saAmfCompCSISetCallbackTimeout);
  1874. tmp = amf_deserialize_SaUint32T (
  1875. tmp, &component->saAmfCompQuiescingCompleteTimeout);
  1876. tmp = amf_deserialize_SaUint32T (
  1877. tmp, &component->saAmfCompCSIRmvCallbackTimeout);
  1878. tmp = amf_deserialize_SaUint32T (
  1879. tmp, &component->saAmfCompRecoveryOnError);
  1880. tmp = amf_deserialize_SaUint32T (
  1881. tmp, &component->saAmfCompDisableRestart);
  1882. tmp = amf_deserialize_SaNameT (
  1883. tmp, &component->saAmfCompProxyCsi);
  1884. tmp = amf_deserialize_SaUint32T (
  1885. tmp, &component->saAmfCompOperState);
  1886. tmp = amf_deserialize_SaUint32T (
  1887. tmp, &component->saAmfCompPresenceState);
  1888. tmp = amf_deserialize_SaUint32T (
  1889. tmp, &component->saAmfCompRestartCount);
  1890. tmp = amf_deserialize_SaNameT (
  1891. tmp, &component->saAmfCompCurrProxyName);
  1892. tmp = amf_deserialize_SaStringT (
  1893. tmp, &component->clccli_path);
  1894. tmp = amf_deserialize_SaUint32T (
  1895. tmp, &component->comptype);
  1896. tmp = amf_deserialize_SaUint32T (
  1897. tmp, &component->error_suspected);
  1898. return component;
  1899. }
  1900. void *amf_healthcheck_serialize (struct amf_healthcheck *healthcheck, int *len)
  1901. {
  1902. char *buf = NULL;
  1903. int offset = 0, size = 0;
  1904. TRACE8 ("%s", healthcheck->safHealthcheckKey.key);
  1905. buf = amf_serialize_opaque (buf, &size, &offset,
  1906. &healthcheck->safHealthcheckKey.key, SA_AMF_HEALTHCHECK_KEY_MAX);
  1907. buf = amf_serialize_SaUint16T (buf, &size, &offset,
  1908. healthcheck->safHealthcheckKey.keyLen);
  1909. buf = amf_serialize_SaUint32T (buf, &size, &offset,
  1910. healthcheck->saAmfHealthcheckMaxDuration);
  1911. buf = amf_serialize_SaUint32T (buf, &size, &offset,
  1912. healthcheck->saAmfHealthcheckPeriod);
  1913. *len = offset;
  1914. return buf;
  1915. }
  1916. struct amf_healthcheck *amf_healthcheck_deserialize (
  1917. struct amf_comp *comp, char *buf)
  1918. {
  1919. char *tmp = buf;
  1920. int cnt;
  1921. amf_healthcheck_t *healthcheck = amf_healthcheck_new (comp);
  1922. tmp = amf_deserialize_opaque (tmp, &healthcheck->safHealthcheckKey.key, &cnt);
  1923. tmp = amf_deserialize_SaUint16T (tmp,
  1924. &healthcheck->safHealthcheckKey.keyLen);
  1925. tmp = amf_deserialize_SaUint32T (tmp,
  1926. &healthcheck->saAmfHealthcheckMaxDuration);
  1927. tmp = amf_deserialize_SaUint32T (tmp,
  1928. &healthcheck->saAmfHealthcheckPeriod);
  1929. return healthcheck;
  1930. }
  1931. amf_healthcheck_t *amf_healthcheck_new (struct amf_comp *comp)
  1932. {
  1933. amf_healthcheck_t *healthcheck = amf_calloc (1, sizeof (amf_healthcheck_t));
  1934. healthcheck->comp = comp;
  1935. healthcheck->next = comp->healthcheck_head;
  1936. comp->healthcheck_head = healthcheck;
  1937. return healthcheck;
  1938. }