amfcomp.c 61 KB

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