amfcomp.c 60 KB

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