amfcomp.c 61 KB

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