amfcomp.c 65 KB

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