amfcomp.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758
  1. /** @file amfcomp.c
  2. *
  3. * Copyright (c) 2006 Ericsson AB.
  4. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  5. * Copyright (c) 2006 Sun Microsystems, Inc. Copyright (c) 2006
  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 <dirent.h>
  134. #include "../include/saAis.h"
  135. #include "../include/saAmf.h"
  136. #include "../include/ipc_gen.h"
  137. #include "../include/ipc_amf.h"
  138. #include "totempg.h"
  139. #include "timer.h"
  140. #include "ipc.h"
  141. #include "service.h"
  142. #include "util.h"
  143. #include "amf.h"
  144. #include "logsys.h"
  145. #include "main.h"
  146. LOGSYS_DECLARE_SUBSYS ("AMF", LOG_INFO);
  147. enum clc_command_run_operation_type {
  148. CLC_COMMAND_RUN_OPERATION_TYPE_INSTANTIATE = 1,
  149. CLC_COMMAND_RUN_OPERATION_TYPE_TERMINATE = 2,
  150. CLC_COMMAND_RUN_OPERATION_TYPE_CLEANUP = 3
  151. };
  152. struct clc_command_run_data {
  153. struct amf_comp *comp;
  154. enum clc_command_run_operation_type type;
  155. void (*completion_callback) (void *context);
  156. int exit_code;
  157. };
  158. struct clc_interface {
  159. int (*instantiate) (struct amf_comp *comp);
  160. int (*terminate) (struct amf_comp *comp);
  161. int (*cleanup) (struct amf_comp *comp);
  162. };
  163. struct csi_remove_callback_data {
  164. struct amf_csi *csi;
  165. };
  166. struct component_terminate_callback_data {
  167. struct amf_comp *comp;
  168. };
  169. static void comp_presence_state_set (
  170. struct amf_comp *comp,
  171. SaAmfPresenceStateT presence_state);
  172. static int clc_cli_instantiate (struct amf_comp *comp);
  173. static int clc_instantiate_callback (struct amf_comp *comp);
  174. static int clc_csi_set_callback (struct amf_comp *comp);
  175. static int clc_cli_terminate (struct amf_comp *comp);
  176. static int lib_comp_terminate_request (struct amf_comp *comp);
  177. static int clc_csi_remove_callback (struct amf_comp *comp);
  178. static int clc_cli_cleanup (struct amf_comp *comp);
  179. static int clc_cli_cleanup_local (struct amf_comp *comp);
  180. static void healthcheck_deactivate (struct amf_healthcheck *healthcheck_active);
  181. static void lib_healthcheck_request (struct amf_healthcheck *healthcheck);
  182. static void timer_function_healthcheck_tmo (void *_healthcheck);
  183. static void lib_csi_set_request (
  184. struct amf_comp *comp,
  185. struct amf_csi_assignment *csi_assignment);
  186. static void comp_recover_action (amf_comp_t *comp,
  187. SaAmfRecommendedRecoveryT recommendedRecovery);
  188. /*
  189. * Life cycle functions
  190. */
  191. static struct clc_interface clc_interface_sa_aware = {
  192. clc_cli_instantiate,
  193. lib_comp_terminate_request,
  194. clc_cli_cleanup
  195. };
  196. static struct clc_interface clc_interface_proxied_pre = {
  197. clc_instantiate_callback,
  198. lib_comp_terminate_request,
  199. clc_cli_cleanup
  200. };
  201. static struct clc_interface clc_interface_proxied_non_pre = {
  202. clc_csi_set_callback,
  203. clc_csi_remove_callback,
  204. clc_cli_cleanup_local
  205. };
  206. static struct clc_interface clc_interface_non_proxied_non_saware = {
  207. clc_cli_instantiate,
  208. clc_cli_terminate,
  209. clc_cli_cleanup_local
  210. };
  211. static struct clc_interface *clc_interfaces[4] = {
  212. &clc_interface_sa_aware,
  213. &clc_interface_proxied_pre,
  214. &clc_interface_proxied_non_pre,
  215. &clc_interface_non_proxied_non_saware
  216. };
  217. struct invocation {
  218. void *data;
  219. int interface;
  220. int active;
  221. };
  222. static struct invocation *invocation_entries = 0;
  223. static int invocation_entries_size = 0;
  224. static int is_not_instantiating_or_instantiated_or_restarting (amf_comp_t *comp)
  225. {
  226. return (!(comp->saAmfCompPresenceState == SA_AMF_PRESENCE_INSTANTIATING ||
  227. comp->saAmfCompPresenceState == SA_AMF_PRESENCE_INSTANTIATED ||
  228. comp->saAmfCompPresenceState == SA_AMF_PRESENCE_RESTARTING));
  229. }
  230. static int invocation_create (
  231. int interface,
  232. void *data)
  233. {
  234. struct invocation *invocation_addr = 0;
  235. struct invocation *invocation_temp;
  236. int i;
  237. int loc = 0;
  238. for (i = 0; i < invocation_entries_size; i++) {
  239. if (invocation_entries[i].active == 0) {
  240. invocation_addr = &invocation_entries[i];
  241. loc = i;
  242. break;
  243. }
  244. }
  245. if (invocation_addr == 0) {
  246. invocation_temp = (struct invocation *)realloc (invocation_entries,
  247. (invocation_entries_size + 1) * sizeof (struct invocation));
  248. if (invocation_temp == NULL) {
  249. openais_exit_error (AIS_DONE_OUT_OF_MEMORY);
  250. }
  251. invocation_entries = invocation_temp;
  252. invocation_addr = &invocation_entries[invocation_entries_size];
  253. loc = invocation_entries_size;
  254. invocation_entries_size += 1;
  255. }
  256. invocation_addr->interface = interface;
  257. invocation_addr->data = data;
  258. invocation_addr->active = 1;
  259. return (loc);
  260. }
  261. static int invocation_get_and_destroy (
  262. SaUint64T invocation, unsigned int *interface, void **data)
  263. {
  264. if (invocation > invocation_entries_size) {
  265. return (-1);
  266. }
  267. if (invocation_entries[invocation].active == 0) {
  268. return (-1);
  269. }
  270. *interface = invocation_entries[invocation].interface;
  271. *data = invocation_entries[invocation].data;
  272. memset (&invocation_entries[invocation], 0, sizeof (struct invocation));
  273. return (0);
  274. }
  275. static void invocation_destroy_by_data (void *data)
  276. {
  277. int i;
  278. for (i = 0; i < invocation_entries_size; i++) {
  279. if (invocation_entries[i].data == data) {
  280. memset (&invocation_entries[i], 0,
  281. sizeof (struct invocation));
  282. break;
  283. }
  284. }
  285. }
  286. /**
  287. * Set suspected error flag and report to SU.
  288. *
  289. * @param comp
  290. * @param recommended_recovery
  291. */
  292. static void report_error_suspected (
  293. struct amf_comp *comp,
  294. SaAmfRecommendedRecoveryT recommended_recovery)
  295. {
  296. ENTER ("%s, recommended_recovery = %d",
  297. comp->name.value, recommended_recovery);
  298. amf_comp_error_suspected_set (comp);
  299. comp_recover_action (comp, recommended_recovery);
  300. }
  301. #ifndef xprintf
  302. #define xprintf(...)
  303. #endif
  304. static void *clc_command_run (void *context)
  305. {
  306. struct clc_command_run_data *clc_command_run_data =
  307. (struct clc_command_run_data *)context;
  308. clc_command_run_data->exit_code = 0;
  309. pid_t pid;
  310. int res;
  311. char **argv = NULL;
  312. char **envp = NULL;
  313. int status;
  314. char path[PATH_MAX];
  315. char *cmd = 0;
  316. char *comp_argv = 0;
  317. char comp_name[SA_MAX_NAME_LENGTH + 24];
  318. int i;
  319. int argv_size;
  320. int envp_size;
  321. ENTER_VOID();
  322. pid = fork();
  323. if (pid == -1) {
  324. fprintf (stderr, "Couldn't fork process %s\n", strerror (errno));
  325. return (0);
  326. }
  327. if (pid) {
  328. xprintf ("waiting for pid %d to finish\n", pid);
  329. waitpid (pid, &status, 0);
  330. if (WIFEXITED (status) != 0 && WEXITSTATUS(status) != 0) {
  331. fprintf (stderr, "Error: CLC_CLI (%d) failed with exit status:"
  332. " %d - %s\n", (int)pid, WEXITSTATUS(status),
  333. strerror (WEXITSTATUS(status)));
  334. /*
  335. * Store the exit code from the script in the return data.
  336. */
  337. clc_command_run_data->exit_code = WEXITSTATUS(status);
  338. }
  339. if (WIFSIGNALED (status) != 0) {
  340. fprintf (stderr, "Error: CLC_CLI (%d) failed with exit status:"
  341. " %d\n", (int)pid, WTERMSIG(status));
  342. /*
  343. * TODO: remove this and handle properly later...
  344. */
  345. /*
  346. * Healthcheck timout will expire laterfore the component
  347. * and this will lead to Intantiation failed for the component.
  348. */
  349. }
  350. xprintf ("process (%d) finished with %x\n", (int)pid, status);
  351. if (clc_command_run_data->completion_callback) {
  352. clc_command_run_data->completion_callback (context);
  353. }
  354. pthread_exit(0);
  355. }
  356. switch (clc_command_run_data->type) {
  357. case CLC_COMMAND_RUN_OPERATION_TYPE_INSTANTIATE:
  358. cmd = clc_command_run_data->comp->saAmfCompInstantiateCmd;
  359. comp_argv = clc_command_run_data->comp->saAmfCompInstantiateCmdArgv;
  360. break;
  361. case CLC_COMMAND_RUN_OPERATION_TYPE_TERMINATE:
  362. cmd = clc_command_run_data->comp->saAmfCompTerminateCmd;
  363. comp_argv = clc_command_run_data->comp->saAmfCompTerminateCmdArgv;
  364. break;
  365. case CLC_COMMAND_RUN_OPERATION_TYPE_CLEANUP:
  366. cmd = clc_command_run_data->comp->saAmfCompCleanupCmd;
  367. comp_argv = clc_command_run_data->comp->saAmfCompCleanupCmdArgv;
  368. break;
  369. default:
  370. assert (0 != 1);
  371. break;
  372. }
  373. /* If command is not an absolute path, search for paths in parent objects */
  374. if (cmd[0] != '/') {
  375. if (clc_command_run_data->comp->clccli_path != NULL) {
  376. sprintf (path, "%s/%s",
  377. clc_command_run_data->comp->clccli_path, cmd);
  378. } else if (clc_command_run_data->comp->su->clccli_path != NULL) {
  379. sprintf (path, "%s/%s",
  380. clc_command_run_data->comp->su->clccli_path, cmd);
  381. } else if (clc_command_run_data->comp->su->sg->clccli_path != NULL) {
  382. sprintf (path, "%s/%s",
  383. clc_command_run_data->comp->su->sg->clccli_path, cmd);
  384. } else if (clc_command_run_data->comp->su->sg->application->clccli_path != NULL) {
  385. sprintf (path, "%s/%s",
  386. clc_command_run_data->comp->su->sg->application->clccli_path, cmd);
  387. }
  388. cmd = path;
  389. }
  390. argv_size = 2;
  391. argv = amf_malloc (sizeof (char*) * argv_size);
  392. argv[0] = cmd;
  393. {
  394. /* make a proper argv array */
  395. i = 1;
  396. char *ptrptr;
  397. char *arg = strtok_r(comp_argv, " ", &ptrptr);
  398. while (arg) {
  399. argv_size++;
  400. argv = realloc (argv, sizeof (char*) * argv_size);
  401. if (argv == NULL) {
  402. fprintf (stderr, "out-of-memory");
  403. exit (-1);
  404. }
  405. argv[i] = arg;
  406. arg = strtok_r(NULL, " ", &ptrptr);
  407. i++;
  408. }
  409. }
  410. argv[i] = NULL;
  411. i = snprintf (comp_name, SA_MAX_NAME_LENGTH,
  412. "SA_AMF_COMPONENT_NAME=safComp=%s,safSu=%s,safSg=%s,safApp=%s",
  413. clc_command_run_data->comp->name.value,
  414. clc_command_run_data->comp->su->name.value,
  415. clc_command_run_data->comp->su->sg->name.value,
  416. clc_command_run_data->comp->su->sg->application->name.value);
  417. assert (i <= sizeof (comp_name));
  418. /* two is for component name and NULL termination */
  419. envp_size = 2;
  420. envp = amf_malloc (sizeof (char*) * envp_size);
  421. envp[0] = comp_name;
  422. for (i = 1; clc_command_run_data->comp->saAmfCompCmdEnv &&
  423. clc_command_run_data->comp->saAmfCompCmdEnv[i - 1]; i++) {
  424. envp_size++;
  425. envp = realloc (envp, sizeof (char*) * envp_size);
  426. if (envp == NULL) {
  427. fprintf (stderr, "out-of-memory");
  428. exit (-1);
  429. }
  430. envp[i] = clc_command_run_data->comp->saAmfCompCmdEnv[i - 1];
  431. }
  432. envp[i] = NULL;
  433. xprintf ("running command '%s' with environment (%d):\n", cmd, envp_size);
  434. for (i = 0; envp[i] != NULL; i++) {
  435. xprintf (" %s\n", envp[i]);
  436. }
  437. xprintf (" and argv (%d):\n", argv_size);
  438. for (i = 0; argv[i] != NULL; i++) {
  439. xprintf (" %s\n", argv[i]);
  440. }
  441. res = execve (cmd, argv, envp);
  442. if (res == -1) {
  443. fprintf (stderr, "Couldn't exec program %s (%s)\n",
  444. cmd, strerror (errno));
  445. }
  446. exit (res); /* abnormal exit of forked process */
  447. return (0);
  448. }
  449. static void amf_comp_instantiate_tmo (void *component)
  450. {
  451. SaNameT compName;
  452. amf_comp_dn_make (component, &compName);
  453. amf_msg_mcast (MESSAGE_REQ_EXEC_AMF_COMPONENT_INSTANTIATE_TMO,
  454. &compName, sizeof (SaNameT));
  455. }
  456. static void amf_comp_cleanup_tmo (void *component)
  457. {
  458. SaNameT compName;
  459. amf_comp_dn_make (component, &compName);
  460. amf_msg_mcast (MESSAGE_REQ_EXEC_AMF_COMPONENT_CLEANUP_TMO,
  461. &compName, sizeof (SaNameT));
  462. }
  463. static void start_component_instantiate_timer (struct amf_comp *component)
  464. {
  465. ENTER("%s",component->name.value);
  466. if (component->instantiate_timeout_handle == 0) {
  467. poll_timer_add (aisexec_poll_handle,
  468. component->saAmfCompInstantiateTimeout,
  469. component,
  470. amf_comp_instantiate_tmo,
  471. &component->instantiate_timeout_handle);
  472. }
  473. }
  474. static void start_component_cleanup_timer (struct amf_comp *component)
  475. {
  476. ENTER("%s",component->name.value);
  477. if (component->cleanup_timeout_handle == 0) {
  478. poll_timer_add (aisexec_poll_handle,
  479. component->saAmfCompCleanupTimeout,
  480. component,
  481. amf_comp_cleanup_tmo,
  482. &component->cleanup_timeout_handle);
  483. }
  484. }
  485. void stop_component_cleanup_timer (struct amf_comp *component)
  486. {
  487. ENTER("%s",component->name.value);
  488. if (component->cleanup_timeout_handle != 0) {
  489. poll_timer_delete (aisexec_poll_handle,
  490. component->cleanup_timeout_handle);
  491. component->cleanup_timeout_handle = 0;
  492. }
  493. }
  494. /*
  495. * Instantiate possible operations
  496. */
  497. static int clc_cli_instantiate (struct amf_comp *comp)
  498. {
  499. int res;
  500. pthread_t thread;
  501. pthread_attr_t thread_attr; /* thread attribute */
  502. struct clc_command_run_data *clc_command_run_data;
  503. ENTER("comp '%s'\n", getSaNameT (&comp->name));
  504. clc_command_run_data = amf_malloc (sizeof (struct clc_command_run_data));
  505. clc_command_run_data->comp = comp;
  506. clc_command_run_data->type = CLC_COMMAND_RUN_OPERATION_TYPE_INSTANTIATE;
  507. clc_command_run_data->completion_callback = NULL;
  508. pthread_attr_init (&thread_attr);
  509. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  510. res = pthread_create (&thread, &thread_attr, clc_command_run,
  511. (void *)clc_command_run_data);
  512. if (res != 0) {
  513. log_printf (LOG_LEVEL_ERROR, "pthread_create failed: %d", res);
  514. }
  515. start_component_instantiate_timer (comp);
  516. return (res);
  517. }
  518. static int clc_instantiate_callback (struct amf_comp *comp)
  519. {
  520. ENTER("comp %s\n", getSaNameT (&comp->name));
  521. return (0);
  522. }
  523. static int clc_csi_set_callback (struct amf_comp *comp)
  524. {
  525. ENTER("comp %s\n", getSaNameT (&comp->name));
  526. return (0);
  527. }
  528. /*
  529. * Terminate possible operations
  530. */
  531. static int clc_cli_terminate (struct amf_comp *comp)
  532. {
  533. ENTER("comp %s\n", getSaNameT (&comp->name));
  534. return (0);
  535. }
  536. /**
  537. * Request component to terminate itself
  538. * @param comp
  539. *
  540. * @return int
  541. */
  542. static int lib_comp_terminate_request (struct amf_comp *comp)
  543. {
  544. struct res_lib_amf_componentterminatecallback res_lib;
  545. struct component_terminate_callback_data *component_terminate_callback_data;
  546. ENTER("comp %s\n", getSaNameT (&comp->name));
  547. res_lib.header.id = MESSAGE_RES_AMF_COMPONENTTERMINATECALLBACK;
  548. res_lib.header.size = sizeof (struct res_lib_amf_componentterminatecallback);
  549. res_lib.header.error = SA_AIS_OK;
  550. memcpy (&res_lib.compName, &comp->name, sizeof (SaNameT));
  551. component_terminate_callback_data =
  552. amf_malloc (sizeof (struct component_terminate_callback_data));
  553. component_terminate_callback_data->comp = comp;
  554. res_lib.invocation =
  555. invocation_create (
  556. AMF_RESPONSE_COMPONENTTERMINATECALLBACK,
  557. component_terminate_callback_data);
  558. openais_conn_send_response (
  559. openais_conn_partner_get (comp->conn),
  560. &res_lib,
  561. sizeof (struct res_lib_amf_componentterminatecallback));
  562. return (0);
  563. }
  564. static int clc_csi_remove_callback (struct amf_comp *comp)
  565. {
  566. dprintf ("clc_tcsi_remove_callback\n");
  567. return (0);
  568. }
  569. /*
  570. * Clean up completed
  571. */
  572. static void mcast_cleanup_completion_event (void *context)
  573. {
  574. struct clc_command_run_data *clc_command_run_data =
  575. (struct clc_command_run_data *)context;
  576. struct req_exec_amf_clc_cleanup_completed req;
  577. struct iovec iovec;
  578. req.header.size = sizeof (struct req_exec_amf_clc_cleanup_completed);
  579. req.header.id = SERVICE_ID_MAKE (AMF_SERVICE,
  580. MESSAGE_REQ_EXEC_AMF_CLC_CLEANUP_COMPLETED);
  581. amf_comp_dn_make (clc_command_run_data->comp, &req.compName);
  582. iovec.iov_base = (char *)&req;
  583. iovec.iov_len = sizeof (req);
  584. /*
  585. * Exit code from the invoked cleanup script.
  586. */
  587. req.cleanup_exit_code = clc_command_run_data->exit_code;
  588. assert (totempg_groups_mcast_joined (openais_group_handle,
  589. &iovec, 1, TOTEMPG_AGREED) == 0);
  590. }
  591. /*
  592. * Cleanup possible operations
  593. */
  594. static int clc_cli_cleanup (struct amf_comp *comp)
  595. {
  596. int res;
  597. pthread_t thread;
  598. pthread_attr_t thread_attr; /* thread attribute */
  599. struct clc_command_run_data *clc_command_run_data;
  600. dprintf ("clc_cli_cleanup\n");
  601. clc_command_run_data = amf_malloc (sizeof (struct clc_command_run_data));
  602. clc_command_run_data->comp = comp;
  603. clc_command_run_data->type = CLC_COMMAND_RUN_OPERATION_TYPE_CLEANUP;
  604. clc_command_run_data->completion_callback = mcast_cleanup_completion_event;
  605. start_component_cleanup_timer (comp);
  606. pthread_attr_init (&thread_attr);
  607. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  608. res = pthread_create (&thread, &thread_attr, clc_command_run,
  609. (void *)clc_command_run_data);
  610. if (res != 0) {
  611. log_printf (LOG_LEVEL_ERROR, "pthread_create failed: %d", res);
  612. }
  613. // TODO error code from pthread_create
  614. return (res);
  615. }
  616. static int clc_cli_cleanup_local (struct amf_comp *comp)
  617. {
  618. dprintf ("clc_cli_cleanup_local\n");
  619. return (0);
  620. }
  621. #if 0
  622. static int clc_terminate (struct amf_comp *comp)
  623. {
  624. int res;
  625. dprintf ("clc terminate for comp %s\n", getSaNameT (&comp->name));
  626. assert (0);
  627. comp_presence_state_set (comp, SA_AMF_PRESENCE_TERMINATING);
  628. operational_state_comp_set (comp, SA_AMF_OPERATIONAL_DISABLED);
  629. res = clc_interfaces[comp->comptype]->terminate (comp);
  630. return (0);
  631. }
  632. #endif
  633. char *amf_comp_dn_make (struct amf_comp *comp, SaNameT *name)
  634. {
  635. int i = snprintf ((char*) name->value, SA_MAX_NAME_LENGTH,
  636. "safComp=%s,safSu=%s,safSg=%s,safApp=%s",
  637. comp->name.value, comp->su->name.value,
  638. comp->su->sg->name.value, comp->su->sg->application->name.value);
  639. assert (i <= SA_MAX_NAME_LENGTH);
  640. name->length = i;
  641. return (char *)name->value;
  642. }
  643. struct amf_healthcheck *amf_comp_find_healthcheck (
  644. struct amf_comp *comp, SaAmfHealthcheckKeyT *key)
  645. {
  646. struct amf_healthcheck *healthcheck;
  647. struct amf_healthcheck *ret_healthcheck = 0;
  648. if (key == NULL) {
  649. return NULL;
  650. }
  651. for (healthcheck = comp->healthcheck_head;
  652. healthcheck != NULL;
  653. healthcheck = healthcheck->next) {
  654. if (key->keyLen == healthcheck->safHealthcheckKey.keyLen &&
  655. memcmp (key, &healthcheck->safHealthcheckKey,key->keyLen) == 0) {
  656. ret_healthcheck = healthcheck;
  657. break;
  658. }
  659. }
  660. return (ret_healthcheck);
  661. }
  662. /**
  663. * Constructor for component objects. Adds component last in
  664. * the list owned by the specified SU. Always returns a
  665. * valid comp object, out-of-memory problems are handled
  666. * here. Default values are initialized.
  667. * @param su
  668. * @param name
  669. *
  670. * @return struct amf_comp*
  671. */
  672. struct amf_comp *amf_comp_new(struct amf_su *su, char *name)
  673. {
  674. struct amf_comp *tail = su->comp_head;
  675. struct amf_comp *comp = amf_calloc (1, sizeof (struct amf_comp));
  676. while (tail != NULL) {
  677. if (tail->next == NULL) {
  678. break;
  679. }
  680. tail = tail->next;
  681. }
  682. if (tail == NULL) {
  683. su->comp_head = comp;
  684. } else {
  685. tail->next = comp;
  686. }
  687. comp->su = su;
  688. /* setup default values from spec. */
  689. comp->saAmfCompNumMaxInstantiateWithoutDelay = 2;
  690. comp->saAmfCompNumMaxAmStartAttempt = 2;
  691. comp->saAmfCompNumMaxAmStopAttempt = 2;
  692. comp->saAmfCompOperState = SA_AMF_OPERATIONAL_DISABLED;
  693. comp->saAmfCompPresenceState = SA_AMF_PRESENCE_UNINSTANTIATED;
  694. amf_comp_error_suspected_clear (comp);
  695. setSaNameT (&comp->name, name);
  696. comp->instantiate_timeout_handle = 0;
  697. comp->cleanup_timeout_handle = 0;
  698. list_init(&comp->pm_head);
  699. return comp;
  700. }
  701. void amf_comp_delete (struct amf_comp *comp)
  702. {
  703. int i;
  704. struct amf_healthcheck *healthcheck;
  705. for (healthcheck = comp->healthcheck_head; healthcheck != NULL;) {
  706. struct amf_healthcheck *tmp = healthcheck;
  707. healthcheck = healthcheck->next;
  708. free (tmp);
  709. }
  710. for (i = 0; comp->saAmfCompCsTypes[i] != NULL; i++) {
  711. free (comp->saAmfCompCsTypes[i]);
  712. }
  713. for (i = 0; comp->saAmfCompCmdEnv[i] != NULL; i++) {
  714. free (comp->saAmfCompCmdEnv[i]);
  715. }
  716. free (comp->saAmfCompInstantiateCmd);
  717. free (comp->saAmfCompInstantiateCmdArgv);
  718. free (comp->saAmfCompTerminateCmd);
  719. free (comp->saAmfCompTerminateCmdArgv);
  720. free (comp->saAmfCompCleanupCmd);
  721. free (comp->saAmfCompCleanupCmdArgv);
  722. free (comp->saAmfCompAmStartCmd);
  723. free (comp->saAmfCompAmStartCmdArgv);
  724. free (comp->saAmfCompAmStopCmd);
  725. free (comp->saAmfCompAmStopCmdArgv);
  726. free (comp->clccli_path);
  727. free (comp);
  728. }
  729. struct amf_comp *amf_comp_find_from_conn_info (void *conn)
  730. {
  731. struct amf_application *app;
  732. struct amf_sg *sg;
  733. struct amf_su *su;
  734. struct amf_comp *comp = NULL;
  735. for (app = amf_cluster->application_head; app != NULL; app = app->next) {
  736. for (sg = app->sg_head; sg != NULL; sg = sg->next) {
  737. for (su = sg->su_head; su != NULL; su = su->next) {
  738. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  739. if (comp->conn == conn) {
  740. goto end;
  741. }
  742. }
  743. }
  744. }
  745. }
  746. end:
  747. return comp;
  748. }
  749. struct amf_comp *amf_comp_find (struct amf_cluster *cluster, SaNameT *name)
  750. {
  751. struct amf_application *app;
  752. struct amf_sg *sg;
  753. struct amf_su *su;
  754. struct amf_comp *comp = NULL;
  755. char *app_name;
  756. char *sg_name;
  757. char *su_name;
  758. char *comp_name;
  759. char *ptrptr;
  760. char *buf;
  761. assert (cluster != NULL && name != NULL);
  762. /* malloc new buffer since strtok_r writes to its first argument */
  763. buf = amf_malloc (name->length + 1);
  764. memcpy (buf, name->value,name ->length + 1);
  765. comp_name = strtok_r(buf, ",", &ptrptr);
  766. su_name = strtok_r(NULL, ",", &ptrptr);
  767. sg_name = strtok_r(NULL, ",", &ptrptr);
  768. app_name = strtok_r(NULL, ",", &ptrptr);
  769. if (comp_name == NULL || su_name == NULL ||
  770. sg_name == NULL || app_name == NULL) {
  771. goto end;
  772. }
  773. comp_name += 8;
  774. su_name += 6;
  775. sg_name += 6;
  776. app_name += 7;
  777. app = amf_application_find (cluster, app_name);
  778. if (app == NULL) {
  779. goto end;
  780. }
  781. sg = amf_sg_find (app, sg_name);
  782. if (sg == NULL) {
  783. goto end;
  784. }
  785. for (su = sg->su_head; su != NULL; su = su->next) {
  786. if (strncmp (su_name, (char*)su->name.value, su->name.length) == 0) {
  787. for (comp = su->comp_head; comp != NULL; comp = comp->next) {
  788. if (comp->name.length == strlen(comp_name) &&
  789. strncmp (comp_name, (char*)comp->name.value,
  790. comp->name.length) == 0) {
  791. goto end;
  792. }
  793. }
  794. }
  795. }
  796. end:
  797. free (buf);
  798. return comp;
  799. }
  800. void amf_comp_healthcheck_deactivate (struct amf_comp *comp)
  801. {
  802. struct amf_healthcheck *healthcheck;
  803. if (!amf_su_is_local (comp->su))
  804. return;
  805. ENTER ("'%s'\n", getSaNameT (&comp->name));
  806. for (healthcheck = comp->healthcheck_head;
  807. healthcheck != NULL;
  808. healthcheck = healthcheck->next) {
  809. if (healthcheck->active) {
  810. healthcheck_deactivate (healthcheck);
  811. }
  812. }
  813. }
  814. static void comp_ha_state_set ( struct amf_comp *comp,
  815. struct amf_csi_assignment *csi_assignment,
  816. SaAmfHAStateT ha_state)
  817. {
  818. /* set confirmed HA state */
  819. csi_assignment->saAmfCSICompHAState = ha_state;
  820. TRACE1 ("Setting comp '%s', SU '%s' CSI '%s', HA state: %s\n",
  821. comp->name.value, comp->su->name.value,
  822. csi_assignment->csi->name.value,
  823. amf_ha_state (csi_assignment->saAmfCSICompHAState));
  824. amf_si_comp_set_ha_state_done (csi_assignment->csi->si, csi_assignment);
  825. }
  826. static void comp_presence_state_set (struct amf_comp *comp,
  827. SaAmfPresenceStateT presence_state)
  828. {
  829. comp->saAmfCompPresenceState = presence_state;
  830. TRACE1 ("Setting comp '%s', SU '%s' presence state: %s\n",
  831. comp->name.value, comp->su->name.value,
  832. amf_presence_state (comp->saAmfCompPresenceState));
  833. amf_su_comp_state_changed (
  834. comp->su, comp, SA_AMF_PRESENCE_STATE, presence_state);
  835. }
  836. struct amf_csi_assignment *amf_comp_get_next_csi_assignment (
  837. struct amf_comp *component,
  838. const struct amf_csi_assignment *csi_assignment)
  839. {
  840. struct amf_si *si;
  841. struct amf_csi *csi;
  842. struct amf_csi_assignment *tmp_csi_assignment;
  843. SaNameT dn;
  844. amf_comp_dn_make (component, &dn);
  845. if (csi_assignment == NULL) {
  846. si = component->su->sg->application->si_head;
  847. csi = si->csi_head;
  848. tmp_csi_assignment = csi->assigned_csis;
  849. } else {
  850. tmp_csi_assignment = csi_assignment->next;
  851. if (tmp_csi_assignment == NULL) {
  852. csi = csi_assignment->csi->next;
  853. if (csi == NULL) {
  854. si = csi_assignment->csi->si->next;
  855. if (si == NULL) {
  856. return NULL;
  857. } else {
  858. csi = si->csi_head;
  859. tmp_csi_assignment = csi->assigned_csis;
  860. }
  861. } else {
  862. si = csi->si;
  863. tmp_csi_assignment = csi->assigned_csis;
  864. }
  865. } else {
  866. csi = tmp_csi_assignment->csi;
  867. si = csi->si;
  868. }
  869. }
  870. for (; si != NULL; si = si->next) {
  871. if (tmp_csi_assignment == NULL && csi == NULL && si != NULL) {
  872. csi = si->csi_head;
  873. tmp_csi_assignment = csi->assigned_csis;
  874. }
  875. for (; csi != NULL; csi = csi->next) {
  876. if (tmp_csi_assignment == NULL && csi != NULL) {
  877. tmp_csi_assignment = csi->assigned_csis;
  878. }
  879. for (; tmp_csi_assignment != NULL;
  880. tmp_csi_assignment = tmp_csi_assignment->next) {
  881. if (name_match (&tmp_csi_assignment->name, &dn)) {
  882. return tmp_csi_assignment;
  883. }
  884. }
  885. }
  886. }
  887. return NULL;
  888. }
  889. void amf_comp_foreach_csi_assignment (
  890. struct amf_comp *component,
  891. void (*foreach_fn) (struct amf_comp *component,
  892. struct amf_csi_assignment *csi_assignment))
  893. {
  894. struct amf_csi_assignment *csi_assignment;
  895. assert (foreach_fn != NULL);
  896. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  897. while (csi_assignment != NULL) {
  898. foreach_fn (component, csi_assignment);
  899. csi_assignment = amf_comp_get_next_csi_assignment (
  900. component, csi_assignment);
  901. }
  902. }
  903. static struct amf_csi_assignment *csi_assignment_find_in (
  904. struct amf_comp *component, SaNameT *csi_name)
  905. {
  906. struct amf_csi_assignment *csi_assignment;
  907. SaNameT dn;
  908. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  909. while (csi_assignment != NULL) {
  910. amf_csi_dn_make (csi_assignment->csi, &dn);
  911. if (name_match (csi_name, &dn)) {
  912. return csi_assignment;
  913. }
  914. csi_assignment = amf_comp_get_next_csi_assignment (
  915. component, csi_assignment);
  916. }
  917. return NULL;
  918. }
  919. static void healthcheck_deactivate (
  920. struct amf_healthcheck *healthcheck_active)
  921. {
  922. dprintf ("deactivating healthcheck for component %s\n",
  923. getSaNameT (&healthcheck_active->comp->name));
  924. poll_timer_delete (aisexec_poll_handle,
  925. healthcheck_active->timer_handle_period);
  926. poll_timer_delete (aisexec_poll_handle,
  927. healthcheck_active->timer_handle_duration);
  928. invocation_destroy_by_data ((void *)healthcheck_active);
  929. healthcheck_active->active = 0;
  930. }
  931. /**
  932. * This function is called by the timer subsystem when AMF should request
  933. * a new healthcheck from a component.
  934. * @param data
  935. */
  936. static void timer_function_healthcheck_next_fn (void *_healthcheck)
  937. {
  938. struct amf_healthcheck *healthcheck = _healthcheck;
  939. /* send healthcheck request to component */
  940. lib_healthcheck_request (healthcheck);
  941. /* start duration timer for response */
  942. poll_timer_add (aisexec_poll_handle,
  943. healthcheck->saAmfHealthcheckMaxDuration,
  944. (void *)healthcheck,
  945. timer_function_healthcheck_tmo,
  946. &healthcheck->timer_handle_duration);
  947. }
  948. /**
  949. * Multicast a healthcheck timeout event.
  950. * @param healthcheck
  951. */
  952. static void mcast_healthcheck_tmo_event (
  953. struct amf_healthcheck *healthcheck)
  954. {
  955. struct req_exec_amf_healthcheck_tmo req_exec;
  956. struct iovec iovec;
  957. if (healthcheck->active == 0) {
  958. log_printf (LOG_ERR, "Healthcheck timeout: ignored key = %s, "
  959. "due to wrong state = %d, comp = %s",
  960. healthcheck->safHealthcheckKey.key,
  961. healthcheck->comp->saAmfCompPresenceState,
  962. healthcheck->comp->name.value);
  963. goto out;
  964. }
  965. req_exec.header.size = sizeof (struct req_exec_amf_healthcheck_tmo);
  966. req_exec.header.id = SERVICE_ID_MAKE (AMF_SERVICE,
  967. MESSAGE_REQ_EXEC_AMF_HEALTHCHECK_TMO);
  968. amf_comp_dn_make (healthcheck->comp, &req_exec.compName);
  969. memcpy (&req_exec.safHealthcheckKey,
  970. &healthcheck->safHealthcheckKey, sizeof (SaAmfHealthcheckKeyT));
  971. req_exec.recommendedRecovery = healthcheck->recommendedRecovery;
  972. iovec.iov_base = (char *)&req_exec;
  973. iovec.iov_len = sizeof (req_exec);
  974. assert (totempg_groups_mcast_joined (openais_group_handle,
  975. &iovec, 1, TOTEMPG_AGREED) == 0);
  976. out:
  977. return;
  978. }
  979. /**
  980. * This function is called by the timer subsystem when a component has not
  981. * performed a healthcheck on time.
  982. * The event is multicasted to the cluster.
  983. * @param data
  984. */
  985. static void timer_function_healthcheck_tmo (
  986. void *_healthcheck)
  987. {
  988. struct amf_healthcheck *healthcheck = (struct amf_healthcheck *)_healthcheck;
  989. TRACE2 ("timeout occured on healthcheck for component %s.\n",
  990. getSaNameT (&healthcheck->comp->name));
  991. mcast_healthcheck_tmo_event (healthcheck);
  992. }
  993. static void lib_healthcheck_request (struct amf_healthcheck *healthcheck)
  994. {
  995. struct res_lib_amf_healthcheckcallback res_lib;
  996. res_lib.header.id = MESSAGE_RES_AMF_HEALTHCHECKCALLBACK;
  997. res_lib.header.size = sizeof (struct res_lib_amf_healthcheckcallback);
  998. res_lib.header.error = SA_AIS_OK;
  999. res_lib.invocation =
  1000. invocation_create (AMF_RESPONSE_HEALTHCHECKCALLBACK, healthcheck);
  1001. amf_comp_dn_make (healthcheck->comp, &res_lib.compName);
  1002. memcpy (&res_lib.key, &healthcheck->safHealthcheckKey,
  1003. sizeof (SaAmfHealthcheckKeyT));
  1004. TRACE7 ("sending healthcheck request to component %s",
  1005. res_lib.compName.value);
  1006. openais_conn_send_response (
  1007. openais_conn_partner_get (healthcheck->comp->conn),
  1008. &res_lib, sizeof (struct res_lib_amf_healthcheckcallback));
  1009. }
  1010. static void lib_csi_set_request (
  1011. struct amf_comp *comp,
  1012. struct amf_csi_assignment *csi_assignment)
  1013. {
  1014. struct res_lib_amf_csisetcallback* res_lib;
  1015. void* p;
  1016. struct amf_csi_attribute *attribute;
  1017. size_t char_length_of_csi_attrs=0;
  1018. size_t num_of_csi_attrs=0;
  1019. int i;
  1020. struct amf_csi *csi;
  1021. char* csi_attribute_buf;
  1022. unsigned int byte_offset;
  1023. if (!amf_su_is_local (comp->su))
  1024. return;
  1025. csi = csi_assignment->csi;
  1026. ENTER ("Assigning CSI '%s' state %s to comp '%s'\n",
  1027. getSaNameT (&csi->name),
  1028. amf_ha_state (csi_assignment->requested_ha_state),
  1029. comp->name.value);
  1030. for (attribute = csi->attributes_head;
  1031. attribute != NULL;
  1032. attribute = attribute->next) {
  1033. for (i = 0; attribute->value[i] != NULL; i++) {
  1034. num_of_csi_attrs++;
  1035. char_length_of_csi_attrs += strlen(attribute->name);
  1036. char_length_of_csi_attrs += strlen(attribute->value[i]);
  1037. char_length_of_csi_attrs += 2;
  1038. }
  1039. }
  1040. p = amf_malloc(sizeof(struct res_lib_amf_csisetcallback) +
  1041. char_length_of_csi_attrs);
  1042. res_lib = (struct res_lib_amf_csisetcallback*)p;
  1043. /* Address of the buffer containing the Csi name value pair */
  1044. csi_attribute_buf = res_lib->csi_attr_buf;
  1045. /* Byteoffset start at the zero byte */
  1046. byte_offset = 0;
  1047. for (attribute = csi->attributes_head;
  1048. attribute != NULL;
  1049. attribute = attribute->next) {
  1050. for (i = 0; attribute->value[i] != NULL; i++) {
  1051. strcpy(&csi_attribute_buf[byte_offset], (char*)attribute->name);
  1052. byte_offset += strlen(attribute->name) + 1;
  1053. strcpy(&csi_attribute_buf[byte_offset], (char*)attribute->value[i]);
  1054. byte_offset += strlen(attribute->value[i]) + 1;
  1055. }
  1056. }
  1057. res_lib->number = num_of_csi_attrs;
  1058. res_lib->csiFlags = SA_AMF_CSI_ADD_ONE;
  1059. switch (csi_assignment->requested_ha_state) {
  1060. case SA_AMF_HA_ACTIVE: {
  1061. res_lib->csiStateDescriptor.activeDescriptor.activeCompName.length = 0;
  1062. res_lib->csiStateDescriptor.activeDescriptor.transitionDescriptor =
  1063. SA_AMF_CSI_NEW_ASSIGN;
  1064. break;
  1065. }
  1066. case SA_AMF_HA_STANDBY: {
  1067. res_lib->csiStateDescriptor.standbyDescriptor.activeCompName.length = 0;
  1068. res_lib->csiStateDescriptor.standbyDescriptor.standbyRank = 1;
  1069. break;
  1070. }
  1071. case SA_AMF_HA_QUIESCED: {
  1072. /*TODO*/
  1073. break;
  1074. }
  1075. case SA_AMF_HA_QUIESCING: {
  1076. /*TODO*/
  1077. break;
  1078. }
  1079. default: {
  1080. assert(SA_AMF_HA_ACTIVE||SA_AMF_HA_STANDBY||SA_AMF_HA_QUIESCING||SA_AMF_HA_QUIESCED);
  1081. break;
  1082. }
  1083. }
  1084. res_lib->header.id = MESSAGE_RES_AMF_CSISETCALLBACK;
  1085. res_lib->header.size =
  1086. sizeof (struct res_lib_amf_csisetcallback) +
  1087. char_length_of_csi_attrs;
  1088. res_lib->header.error = SA_AIS_OK;
  1089. amf_comp_dn_make (comp, &res_lib->compName);
  1090. amf_csi_dn_make (csi, &res_lib->csiName);
  1091. res_lib->haState = csi_assignment->requested_ha_state;
  1092. res_lib->invocation =
  1093. invocation_create (AMF_RESPONSE_CSISETCALLBACK, csi_assignment);
  1094. openais_conn_send_response (
  1095. openais_conn_partner_get (comp->conn), res_lib, res_lib->header.size);
  1096. free(p);
  1097. }
  1098. static void stop_component_instantiate_timer (struct amf_comp *component)
  1099. {
  1100. ENTER("%s",component->name.value);
  1101. if (component->instantiate_timeout_handle) {
  1102. dprintf ("Stop component instantiate timer");
  1103. poll_timer_delete (aisexec_poll_handle,
  1104. component->instantiate_timeout_handle);
  1105. component->instantiate_timeout_handle = 0;
  1106. }
  1107. }
  1108. SaAisErrorT amf_comp_register (struct amf_comp *comp)
  1109. {
  1110. TRACE2("Exec comp register '%s'", comp->name.value);
  1111. stop_component_instantiate_timer (comp);
  1112. switch (comp->saAmfCompPresenceState) {
  1113. case SA_AMF_PRESENCE_RESTARTING:
  1114. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATED);
  1115. break;
  1116. case SA_AMF_PRESENCE_INSTANTIATING:
  1117. amf_comp_operational_state_set (comp, SA_AMF_OPERATIONAL_ENABLED);
  1118. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATED);
  1119. break;
  1120. case SA_AMF_PRESENCE_INSTANTIATION_FAILED:
  1121. /* ignore due to instantitate timeout a while ago */
  1122. break;
  1123. default:
  1124. log_printf(LOG_LEVEL_ERROR,"comp->saAmfCompPresenceState = %d",
  1125. comp->saAmfCompPresenceState);
  1126. assert (0);
  1127. break;
  1128. }
  1129. return SA_AIS_OK;
  1130. }
  1131. void amf_comp_error_report (struct amf_comp *comp, amf_comp_t* reporting_comp,
  1132. SaAmfRecommendedRecoveryT recommendedRecovery)
  1133. {
  1134. struct res_lib_amf_componenterrorreport res_lib;
  1135. if (reporting_comp != NULL) {
  1136. TRACE2("Exec comp error report on comp'%s' from %s", comp->name.value,
  1137. reporting_comp->name.value );
  1138. if (amf_su_is_local (reporting_comp->su)) {
  1139. res_lib.header.size = sizeof (struct res_lib_amf_componenterrorreport);
  1140. res_lib.header.id = MESSAGE_RES_AMF_COMPONENTERRORREPORT;
  1141. res_lib.header.error = SA_AIS_OK;
  1142. openais_conn_send_response (reporting_comp->conn, &res_lib, sizeof (res_lib));
  1143. }
  1144. } else {
  1145. TRACE2("Exec comp error report on comp'%s' from AMF", comp->name.value);
  1146. }
  1147. /* Report to SU and let it handle the problem */
  1148. report_error_suspected (comp, recommendedRecovery);
  1149. }
  1150. /**
  1151. * Healthcheck timeout event handler
  1152. * @param comp
  1153. * @param healthcheck
  1154. */
  1155. void amf_comp_healthcheck_tmo (
  1156. struct amf_comp *comp, SaAmfRecommendedRecoveryT recommendedRecovery)
  1157. {
  1158. TRACE2("Exec healthcheck tmo for '%s'", comp->name.value);
  1159. /* report to SU and let it handle the problem */
  1160. report_error_suspected (comp, recommendedRecovery);
  1161. }
  1162. static void clear_ha_state (
  1163. struct amf_comp *comp, struct amf_csi_assignment *csi_assignment)
  1164. {
  1165. ENTER ("");
  1166. csi_assignment->saAmfCSICompHAState = 0;
  1167. }
  1168. static void comp_recover_action (amf_comp_t *comp,
  1169. SaAmfRecommendedRecoveryT recommendedRecovery)
  1170. {
  1171. ENTER ("%s %d %d", comp->name.value,recommendedRecovery,
  1172. comp->saAmfCompRecoveryOnError);
  1173. amf_node_t *node = amf_node_find (&comp->su->saAmfSUHostedByNode);
  1174. switch (recommendedRecovery) {
  1175. case SA_AMF_NO_RECOMMENDATION: {
  1176. /*
  1177. * If the recommendation was SA_AMF_NO_RECOMMENDATION,
  1178. * then use the configured recovery action for the component
  1179. */
  1180. switch (comp->saAmfCompRecoveryOnError) {
  1181. case SA_AMF_NO_RECOMMENDATION:
  1182. if (comp->saAmfCompDisableRestart) {
  1183. /* Comp or SU failover */
  1184. amf_node_comp_failover_req (node, comp);
  1185. } else {
  1186. /* Component restart */
  1187. amf_su_comp_error_suspected (comp->su, comp,
  1188. recommendedRecovery);
  1189. }
  1190. case SA_AMF_COMPONENT_RESTART:
  1191. if (comp->saAmfCompDisableRestart) {
  1192. /* Comp or SU failover */
  1193. amf_node_comp_failover_req (node, comp);
  1194. } else {
  1195. /* Component restart */
  1196. amf_su_comp_error_suspected (comp->su, comp,
  1197. recommendedRecovery);
  1198. }
  1199. break;
  1200. case SA_AMF_COMPONENT_FAILOVER:
  1201. /* SU failover */
  1202. amf_node_comp_failover_req (node, comp);
  1203. break;
  1204. case SA_AMF_NODE_SWITCHOVER:
  1205. break;
  1206. case SA_AMF_NODE_FAILOVER: {
  1207. /* Node failover */
  1208. amf_node_t *node = amf_node_find (
  1209. &comp->su->saAmfSUHostedByNode);
  1210. amf_node_failover(node);
  1211. }
  1212. break;
  1213. case SA_AMF_NODE_FAILFAST:
  1214. break;
  1215. case SA_AMF_CLUSTER_RESET:
  1216. break;
  1217. case SA_AMF_APPLICATION_RESTART:
  1218. default:
  1219. dprintf("recommendedRecovery=%d",recommendedRecovery);
  1220. assert (0);
  1221. break;
  1222. }
  1223. break;
  1224. }
  1225. case SA_AMF_COMPONENT_RESTART:
  1226. if (comp->saAmfCompDisableRestart == SA_TRUE) {
  1227. amf_node_comp_failover_req (node, comp);
  1228. } else {
  1229. amf_su_comp_error_suspected (comp->su, comp, recommendedRecovery);
  1230. }
  1231. break;
  1232. case SA_AMF_COMPONENT_FAILOVER:
  1233. amf_node_comp_failover_req (node, comp);
  1234. break;
  1235. case SA_AMF_NODE_SWITCHOVER:
  1236. break;
  1237. case SA_AMF_NODE_FAILOVER:
  1238. /* Node failover */
  1239. amf_node_failover (amf_node_find (&comp->su->saAmfSUHostedByNode));
  1240. break;
  1241. case SA_AMF_NODE_FAILFAST:
  1242. break;
  1243. case SA_AMF_CLUSTER_RESET:
  1244. break;
  1245. case SA_AMF_APPLICATION_RESTART:
  1246. default:
  1247. assert (0);
  1248. break;
  1249. }
  1250. }
  1251. /**
  1252. * Event method to be called when a cleanup completed event is received
  1253. * with failure.
  1254. * @param comp
  1255. */
  1256. void amf_comp_cleanup_failed_completed (amf_comp_t *comp)
  1257. {
  1258. ENTER ("'%s'", comp->name.value);
  1259. stop_component_cleanup_timer (comp);
  1260. amf_comp_error_suspected_clear (comp);
  1261. amf_comp_operational_state_set (comp, SA_AMF_OPERATIONAL_DISABLED);
  1262. comp_presence_state_set (comp, SA_AMF_PRESENCE_TERMINATION_FAILED);
  1263. }
  1264. /**
  1265. * Event method to be called when a cleanup completed event is received
  1266. * @param comp
  1267. */
  1268. void amf_comp_cleanup_completed (struct amf_comp *comp)
  1269. {
  1270. TRACE2("Exec CLC cleanup completed for '%s' %d", comp->name.value,
  1271. comp->saAmfCompPresenceState);
  1272. stop_component_cleanup_timer (comp);
  1273. /* Set all CSI's confirmed HA state to unknown */
  1274. amf_comp_foreach_csi_assignment (comp, clear_ha_state);
  1275. amf_comp_error_suspected_clear (comp);
  1276. if (comp->saAmfCompPresenceState == SA_AMF_PRESENCE_RESTARTING) {
  1277. amf_comp_instantiate (comp);
  1278. } else if (comp->saAmfCompPresenceState ==
  1279. SA_AMF_PRESENCE_TERMINATION_FAILED) {
  1280. comp_presence_state_set (comp, SA_AMF_PRESENCE_TERMINATION_FAILED);
  1281. } else {
  1282. comp_presence_state_set (comp, SA_AMF_PRESENCE_UNINSTANTIATED);
  1283. }
  1284. }
  1285. /**
  1286. * go through the pids for this component and
  1287. * check the existence of of /proc/<pid>/stat
  1288. */
  1289. static void timer_function_pm_fn (void *data)
  1290. {
  1291. struct amf_comp *comp = (struct amf_comp *)data;
  1292. struct amf_pm *pm = NULL;
  1293. struct list_head *pmlist = NULL;
  1294. struct list_head *next = NULL;
  1295. SaBoolT reported = SA_FALSE;
  1296. char f[30];
  1297. assert (comp);
  1298. /* we are going to ignore the pmErrors
  1299. * and only check to see if the process exists.
  1300. */
  1301. for (pmlist = comp->pm_head.next;
  1302. pmlist != &comp->pm_head;
  1303. pmlist = next) {
  1304. pm = list_entry(pmlist,struct amf_pm,entry);
  1305. next = pmlist->next;
  1306. if (pm->errors == 0) {
  1307. list_del(pmlist);
  1308. free(pm);
  1309. continue;
  1310. }
  1311. sprintf(f,"/proc/%llu/stat", pm->pid);
  1312. if (access( f, R_OK) != 0) {
  1313. if ((comp->su->restart_control_state != SU_RC_RESTART_SU_DEACTIVATING) &&
  1314. (comp->su->restart_control_state != SU_RC_RESTART_SU_TERMINATING) &&
  1315. (reported == SA_FALSE)) {
  1316. /* don't report it as an error if we are busy
  1317. * shutting down
  1318. */
  1319. syslog(LOG_ALERT, "component %s:%s exited",
  1320. comp->su->saAmfSUHostedByNode.value, comp->name.value);
  1321. mcast_error_report_from_pm (comp, pm->recovery);
  1322. reported = SA_TRUE;
  1323. }
  1324. list_del(pmlist);
  1325. free(pm);
  1326. break;
  1327. }
  1328. }
  1329. if (!list_empty(&comp->pm_head)) {
  1330. pm = list_entry(comp->pm_head.next,struct amf_pm,entry);
  1331. poll_timer_add (aisexec_poll_handle,
  1332. 500,
  1333. (void *)comp,
  1334. timer_function_pm_fn,
  1335. &pm->timer_handle_period);
  1336. }
  1337. }
  1338. /**
  1339. * Find and add all children of a given PID
  1340. * @param comp the component
  1341. * @param pmErrors the errors to monitor
  1342. * @param recommendedRecovery
  1343. * @param dirList list of files in proc filesystem
  1344. * @param numProcEntriesFound number of file entries in proc filesystem
  1345. * @param ppid the process id to find children of
  1346. * @param depth the descendents tree depth to monitor
  1347. */
  1348. void amf_comp_find_and_add_child_pids(
  1349. struct amf_comp *comp,
  1350. SaAmfPmErrorsT pmErrors,
  1351. SaAmfRecommendedRecoveryT recommendedRecovery,
  1352. struct dirent **dirList,
  1353. SaInt32T numProcEntriesFound,
  1354. SaUint64T ppid,
  1355. SaInt32T depth)
  1356. {
  1357. SaUint64T parent;
  1358. SaUint64T p_id;
  1359. SaInt32T res;
  1360. SaInt32T n = numProcEntriesFound;
  1361. char f[30];
  1362. FILE *p;
  1363. struct amf_pm *pm = NULL;
  1364. while (n--) {
  1365. sprintf(f, "/proc/%s/stat", dirList[n]->d_name);
  1366. p = fopen(f, "r");
  1367. if (p == NULL)
  1368. continue;
  1369. res = fscanf(p, "%llu %*s %*c %llu", &p_id, &parent);
  1370. if ((res == 2) && (parent == ppid)) {
  1371. pm = amf_calloc(1, sizeof(struct amf_pm));
  1372. if ( pm == NULL ) {
  1373. return;
  1374. }
  1375. TRACE2 ("add child (pid=%llu) for comp pid=%llu (%s)\n", p_id, ppid, comp->name.value);
  1376. pm->pid = p_id;
  1377. pm->errors = pmErrors;
  1378. pm->recovery = recommendedRecovery;
  1379. pm->timer_handle_period = 0;
  1380. list_add(&pm->entry, &comp->pm_head);
  1381. if (depth > 1) {
  1382. amf_comp_find_and_add_child_pids(comp,
  1383. pmErrors,
  1384. recommendedRecovery,
  1385. dirList,
  1386. numProcEntriesFound,
  1387. p_id,
  1388. depth - 1);
  1389. }
  1390. }
  1391. fclose(p);
  1392. }
  1393. }
  1394. /**
  1395. * Handle the request to start passive monitoring
  1396. *
  1397. * @param comp the component
  1398. * @param pid the process id to monitor
  1399. * @param depth the descendents tree depth to monitor
  1400. * @param pmErrors the errors to monitor
  1401. * @param recommendedRecovery
  1402. *
  1403. * @return SaAisErrorT
  1404. */
  1405. SaAisErrorT amf_comp_pm_start (
  1406. struct amf_comp *comp,
  1407. SaUint64T pid,
  1408. SaInt32T depth,
  1409. SaAmfPmErrorsT pmErrors,
  1410. SaAmfRecommendedRecoveryT recommendedRecovery)
  1411. {
  1412. struct amf_pm *pm = NULL;
  1413. struct list_head *pmlist = NULL;
  1414. struct dirent **dirList;
  1415. SaInt32T numProcEntriesFound;
  1416. if (is_not_instantiating_or_instantiated_or_restarting (comp)) {
  1417. log_printf (LOG_ERR, "PmStart: ignored due to wrong state = %d, comp = %s",
  1418. comp->saAmfCompPresenceState, comp->name.value);
  1419. return SA_AIS_ERR_FAILED_OPERATION;
  1420. }
  1421. /* try and find one thats already there, and mod it */
  1422. for (pmlist = comp->pm_head.next;
  1423. pmlist != &comp->pm_head;
  1424. pmlist = pmlist->next) {
  1425. pm = list_entry(pmlist,struct amf_pm,entry);
  1426. if (pm->pid == pid) {
  1427. break;
  1428. }
  1429. }
  1430. if ( pm == NULL ) {
  1431. /* not found, create it */
  1432. pm = amf_calloc(1, sizeof(struct amf_pm));
  1433. if ( pm == NULL ) {
  1434. return SA_AIS_ERR_NO_MEMORY;
  1435. }
  1436. pm->pid = pid;
  1437. pm->errors = pmErrors;
  1438. pm->recovery = recommendedRecovery;
  1439. pm->timer_handle_period = 0;
  1440. if ( list_empty(&comp->pm_head)) {
  1441. /* only add a timer per comp */
  1442. /* TODO: should this timer period be a define or a config option?
  1443. */
  1444. poll_timer_add (aisexec_poll_handle,
  1445. 500,
  1446. (void *)comp,
  1447. timer_function_pm_fn,
  1448. &pm->timer_handle_period);
  1449. }
  1450. list_add(&pm->entry, &comp->pm_head);
  1451. numProcEntriesFound = scandir("/proc/", &dirList, 0, alphasort);
  1452. if (numProcEntriesFound < 0) {
  1453. perror("scandir");
  1454. return -2;
  1455. }
  1456. amf_comp_find_and_add_child_pids(comp,
  1457. pmErrors,
  1458. recommendedRecovery,
  1459. dirList,
  1460. numProcEntriesFound,
  1461. pid,
  1462. depth);
  1463. free(dirList);
  1464. } else {
  1465. /* only esculate the checking */
  1466. pm->errors |= pmErrors;
  1467. if (pm->recovery < recommendedRecovery) {
  1468. pm->recovery = recommendedRecovery;
  1469. }
  1470. }
  1471. return SA_AIS_OK;
  1472. }
  1473. /**
  1474. * Handle the request to stop passive monitoring on
  1475. * a component (or part of it)
  1476. *
  1477. * @param comp the component
  1478. * @param stopQualifier what processes to stop
  1479. * @param pid the process id to monitor
  1480. * @param pmErrors the errors to monitor
  1481. *
  1482. * @return SaAisErrorT - return value to component
  1483. */
  1484. SaAisErrorT amf_comp_pm_stop (
  1485. struct amf_comp *comp,
  1486. SaAmfPmStopQualifierT stopQualifier,
  1487. SaInt64T pid,
  1488. SaAmfPmErrorsT pmErrors)
  1489. {
  1490. struct amf_pm *pm = NULL;
  1491. struct list_head *pmlist = NULL;
  1492. for (pmlist = comp->pm_head.next; pmlist != &comp->pm_head; pmlist = pmlist->next) {
  1493. pm = list_entry(pmlist,struct amf_pm,entry);
  1494. if ((pm->pid == pid) ||
  1495. ( stopQualifier == SA_AMF_PM_ALL_PROCESSES)) {
  1496. /* remove the error to check */
  1497. pm->errors &= ~pmErrors;
  1498. }
  1499. }
  1500. return SA_AIS_OK;
  1501. }
  1502. /**
  1503. * Handle the request from a component to start a healthcheck
  1504. *
  1505. * @param comp
  1506. * @param healthcheckKey
  1507. * @param invocationType
  1508. * @param recommendedRecovery
  1509. *
  1510. * @return SaAisErrorT - return value to component
  1511. */
  1512. SaAisErrorT amf_comp_healthcheck_start (
  1513. struct amf_comp *comp,
  1514. SaAmfHealthcheckKeyT *healthcheckKey,
  1515. SaAmfHealthcheckInvocationT invocationType,
  1516. SaAmfRecommendedRecoveryT recommendedRecovery)
  1517. {
  1518. struct amf_healthcheck *healthcheck;
  1519. SaAisErrorT error = SA_AIS_OK;
  1520. if (is_not_instantiating_or_instantiated_or_restarting (comp)) {
  1521. log_printf (LOG_ERR, "Healthcheckstart: ignored key = %s, "
  1522. "due to wrong state = %d, comp = %s",
  1523. healthcheckKey->key, comp->saAmfCompPresenceState, comp->name.value);
  1524. error = SA_AIS_OK;
  1525. goto error_exit;
  1526. }
  1527. healthcheck = amf_comp_find_healthcheck (comp, healthcheckKey);
  1528. if (healthcheck == 0) {
  1529. log_printf (LOG_ERR, "Healthcheckstart: Healthcheck '%s' not found",
  1530. healthcheckKey->key);
  1531. error = SA_AIS_ERR_NOT_EXIST;
  1532. goto error_exit;
  1533. }
  1534. dprintf ("Healthcheckstart: '%s', key '%s'",
  1535. comp->name.value, healthcheckKey->key);
  1536. /*
  1537. * Determine if this healthcheck is already active
  1538. */
  1539. if (healthcheck->active) {
  1540. error = SA_AIS_ERR_EXIST;
  1541. goto error_exit;
  1542. }
  1543. /*
  1544. * Initialise
  1545. */
  1546. healthcheck->invocationType = invocationType;
  1547. healthcheck->recommendedRecovery = recommendedRecovery;
  1548. healthcheck->timer_handle_duration = 0;
  1549. healthcheck->timer_handle_period = 0;
  1550. healthcheck->active = 1;
  1551. if (invocationType == SA_AMF_HEALTHCHECK_AMF_INVOKED) {
  1552. /* start timer to execute first healthcheck request */
  1553. poll_timer_add (aisexec_poll_handle,
  1554. healthcheck->saAmfHealthcheckPeriod,
  1555. (void *)healthcheck,
  1556. timer_function_healthcheck_next_fn,
  1557. &healthcheck->timer_handle_period);
  1558. } else if (invocationType == SA_AMF_HEALTHCHECK_COMPONENT_INVOKED) {
  1559. /* start supervision timer */
  1560. poll_timer_add (aisexec_poll_handle,
  1561. healthcheck->saAmfHealthcheckPeriod,
  1562. (void *)healthcheck,
  1563. timer_function_healthcheck_tmo,
  1564. &healthcheck->timer_handle_period);
  1565. } else {
  1566. error = SA_AIS_ERR_INVALID_PARAM;
  1567. }
  1568. error_exit:
  1569. return error;
  1570. }
  1571. /**
  1572. * Stop all or a specifed healthcheck
  1573. * @param comp
  1574. * @param healthcheckKey - NULL if all
  1575. *
  1576. * @return SaAisErrorT
  1577. */
  1578. SaAisErrorT amf_comp_healthcheck_stop (
  1579. struct amf_comp *comp,
  1580. SaAmfHealthcheckKeyT *healthcheckKey)
  1581. {
  1582. struct amf_healthcheck *healthcheck;
  1583. SaAisErrorT error = SA_AIS_OK;
  1584. dprintf ("Healthcheckstop: '%s'", comp->name.value);
  1585. if (!amf_su_is_local (comp->su)) {
  1586. return SA_AIS_OK;
  1587. }
  1588. if (healthcheckKey == NULL) {
  1589. for (healthcheck = comp->healthcheck_head;
  1590. healthcheck != NULL;
  1591. healthcheck = healthcheck->next) {
  1592. healthcheck_deactivate (healthcheck);
  1593. }
  1594. } else {
  1595. healthcheck = amf_comp_find_healthcheck (comp, healthcheckKey);
  1596. if (healthcheck == NULL) {
  1597. log_printf (LOG_ERR, "Healthcheckstop: Healthcheck '%s' not found",
  1598. healthcheckKey->key);
  1599. error = SA_AIS_ERR_NOT_EXIST;
  1600. } else {
  1601. healthcheck_deactivate (healthcheck);
  1602. }
  1603. }
  1604. return error;
  1605. }
  1606. /**
  1607. * Instantiate a component
  1608. * @param comp
  1609. */
  1610. void amf_comp_instantiate (struct amf_comp *comp)
  1611. {
  1612. ENTER ("'%s' SU '%s'", getSaNameT (&comp->name),
  1613. getSaNameT (&comp->su->name));
  1614. switch (comp->saAmfCompPresenceState) {
  1615. case SA_AMF_PRESENCE_RESTARTING:
  1616. /* fall through */
  1617. case SA_AMF_PRESENCE_UNINSTANTIATED:
  1618. if (amf_su_is_local (comp->su)) {
  1619. TRACE1("Send instantiate event for comp '%s' from host %s",
  1620. comp->name.value, comp->su->saAmfSUHostedByNode.value);
  1621. SaNameT compName;
  1622. amf_comp_dn_make (comp, &compName);
  1623. amf_msg_mcast (MESSAGE_REQ_EXEC_AMF_COMPONENT_INSTANTIATE,
  1624. &compName, sizeof (SaNameT));
  1625. }
  1626. break;
  1627. default:
  1628. dprintf("Instantiate ignored in Component presence state %d",
  1629. comp->saAmfCompPresenceState);
  1630. break;
  1631. }
  1632. }
  1633. void amf_comp_cleanup_tmo_event (struct amf_comp *comp)
  1634. {
  1635. ENTER ("Comp cleanup timeout after %d ms '%s' '%s'",
  1636. comp->saAmfCompCleanupTimeout, comp->su->name.value,
  1637. comp->name.value);
  1638. amf_comp_error_suspected_clear(comp);
  1639. amf_comp_operational_state_set (comp, SA_AMF_OPERATIONAL_DISABLED);
  1640. comp_presence_state_set (comp, SA_AMF_PRESENCE_TERMINATION_FAILED);
  1641. }
  1642. void amf_comp_instantiate_tmo_event (struct amf_comp *comp)
  1643. {
  1644. ENTER ("Comp instantiate timeout after %d ms '%s' '%s'",
  1645. comp->saAmfCompInstantiateTimeout, comp->su->name.value,
  1646. comp->name.value);
  1647. switch (comp->saAmfCompPresenceState) {
  1648. case SA_AMF_PRESENCE_RESTARTING:
  1649. amf_comp_operational_state_set (comp, SA_AMF_OPERATIONAL_DISABLED);
  1650. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATION_FAILED);
  1651. break;
  1652. case SA_AMF_PRESENCE_INSTANTIATING:
  1653. amf_comp_operational_state_set (comp, SA_AMF_OPERATIONAL_DISABLED);
  1654. comp_presence_state_set (comp, SA_AMF_PRESENCE_INSTANTIATION_FAILED);
  1655. break;
  1656. case SA_AMF_PRESENCE_INSTANTIATED:
  1657. assert (comp->instantiate_timeout_handle == 0);
  1658. break;
  1659. default:
  1660. dprintf("Presence state = %d", comp->saAmfCompPresenceState);
  1661. assert (0);
  1662. break;
  1663. }
  1664. }
  1665. void amf_comp_instantiate_event (struct amf_comp *component)
  1666. {
  1667. int res;
  1668. ENTER ("");
  1669. switch (component->saAmfCompPresenceState) {
  1670. case SA_AMF_PRESENCE_INSTANTIATING:
  1671. case SA_AMF_PRESENCE_INSTANTIATED:
  1672. case SA_AMF_PRESENCE_TERMINATING:
  1673. case SA_AMF_PRESENCE_INSTANTIATION_FAILED:
  1674. case SA_AMF_PRESENCE_TERMINATION_FAILED:
  1675. dprintf("Instantiate ignored in Component presence state %d",
  1676. component->saAmfCompPresenceState);
  1677. break;
  1678. case SA_AMF_PRESENCE_UNINSTANTIATED:
  1679. comp_presence_state_set (component, SA_AMF_PRESENCE_INSTANTIATING);
  1680. amf_su_comp_state_changed(component->su,
  1681. component,SA_AMF_PRESENCE_STATE,SA_AMF_PRESENCE_INSTANTIATING);
  1682. if (amf_su_is_local (component->su)) {
  1683. res = clc_interfaces[component->comptype]->instantiate (
  1684. component);
  1685. }
  1686. break;
  1687. case SA_AMF_PRESENCE_RESTARTING:
  1688. if (amf_su_is_local (component->su)) {
  1689. res = clc_interfaces[component->comptype]->instantiate (
  1690. component);
  1691. }
  1692. break;
  1693. default:
  1694. dprintf("Component presence state %d",
  1695. component->saAmfCompPresenceState);
  1696. assert (0);
  1697. break;
  1698. }
  1699. }
  1700. void amf_comp_readiness_state_set (struct amf_comp *comp,
  1701. SaAmfReadinessStateT state)
  1702. {
  1703. TRACE1 ("Setting comp '%s' readiness state: %s\n",
  1704. comp->name.value, amf_readiness_state (state));
  1705. }
  1706. /**
  1707. * Handle a component response (received from the lib) of an earlier AMF request.
  1708. * This function should be invoked when the lib request is received.
  1709. * @param invocation [in] associates the response with the request (callback)
  1710. * @param error [in] response from the component of the associated callback
  1711. * @param retval [out] contains return value to component when needed
  1712. *
  1713. * @return ==0 respond to component, do not multicast
  1714. * @return >0 do not respond to component, multicast response
  1715. */
  1716. int amf_comp_response_1 (
  1717. SaInvocationT invocation, SaAisErrorT error, SaAisErrorT *retval,
  1718. SaUint32T *interface, SaNameT *dn, SaAmfHealthcheckKeyT *healtcheck_key,
  1719. SaAmfRecommendedRecoveryT *recommendedRecovery)
  1720. {
  1721. int res;
  1722. void *data;
  1723. res = invocation_get_and_destroy (invocation, interface, &data);
  1724. if (res == -1) {
  1725. log_printf (LOG_ERR, "Lib response: invocation not found\n");
  1726. *retval = SA_AIS_ERR_INVALID_PARAM;
  1727. return 0;
  1728. }
  1729. switch (*interface) {
  1730. case AMF_RESPONSE_HEALTHCHECKCALLBACK: {
  1731. struct amf_healthcheck *healthcheck = data;
  1732. amf_comp_dn_make (healthcheck->comp, dn);
  1733. TRACE7 ("Healthcheck response from '%s': %d",dn->value, error);
  1734. /*
  1735. * Healthcheck with erroneous response
  1736. * and no recovery action is in progress.
  1737. */
  1738. memcpy(healtcheck_key, &healthcheck->safHealthcheckKey,
  1739. sizeof (SaAmfHealthcheckKeyT));
  1740. *recommendedRecovery = healthcheck->recommendedRecovery;
  1741. if (error != SA_AIS_OK &&
  1742. !amf_comp_is_error_suspected (healthcheck->comp)) {
  1743. return 1;
  1744. }
  1745. if (is_not_instantiating_or_instantiated_or_restarting(
  1746. healthcheck->comp)) {
  1747. log_printf (LOG_ERR, "HealthcheckResponse: ignored for key = %s, "
  1748. "due to wrong state = %d comp = %s",
  1749. healthcheck->safHealthcheckKey.key,
  1750. healthcheck->comp->saAmfCompPresenceState,
  1751. healthcheck->comp->name.value);
  1752. *retval = SA_AIS_OK;
  1753. return 0; /* do not multicast event */
  1754. }
  1755. if (healthcheck->invocationType == SA_AMF_HEALTHCHECK_AMF_INVOKED) {
  1756. /* the response was on time, delete supervision timer */
  1757. poll_timer_delete (aisexec_poll_handle,
  1758. healthcheck->timer_handle_duration);
  1759. healthcheck->timer_handle_duration = 0;
  1760. /* start timer to execute next healthcheck request */
  1761. poll_timer_add (aisexec_poll_handle,
  1762. healthcheck->saAmfHealthcheckPeriod,
  1763. (void *)healthcheck,
  1764. timer_function_healthcheck_next_fn,
  1765. &healthcheck->timer_handle_period);
  1766. *retval = SA_AIS_OK;
  1767. } else {
  1768. *retval = SA_AIS_ERR_INVALID_PARAM;
  1769. }
  1770. return 0; /* do not multicast event */
  1771. break;
  1772. }
  1773. case AMF_RESPONSE_CSISETCALLBACK: /* fall-through */
  1774. case AMF_RESPONSE_CSIREMOVECALLBACK:
  1775. amf_csi_assignment_dn_make (data, dn);
  1776. return 1; /* multicast event */
  1777. break;
  1778. #if 0
  1779. case AMF_RESPONSE_COMPONENTTERMINATECALLBACK: {
  1780. struct component_terminate_callback_data *component_terminate_callback_data;
  1781. component_terminate_callback_data = data;
  1782. dprintf ("Lib component terminate callback response, error: %d", error);
  1783. amf_comp_healthcheck_deactivate (component_terminate_callback_data->comp);
  1784. escalation_policy_restart (component_terminate_callback_data->comp);
  1785. return 1;
  1786. break;
  1787. }
  1788. #endif
  1789. default:
  1790. assert (0);
  1791. break;
  1792. }
  1793. /* XXX we fall here in case NDEBUG is set */
  1794. *retval = -1;
  1795. return 0;
  1796. }
  1797. /**
  1798. * Handle a component response (received from EVS) of an earlier AMF request.
  1799. * This function should be invoked when the multicast request is received.
  1800. * @param invocation [in] associates the response with the request (callback)
  1801. * @param error [in] response from the component of the associated callback
  1802. * @param retval [out] contains return value to component when needed
  1803. *
  1804. * @return component to which the response should be sent
  1805. */
  1806. struct amf_comp *amf_comp_response_2 (SaUint32T interface, SaNameT *dn,
  1807. SaAmfHealthcheckKeyT *healthcheck_key, SaAisErrorT error,
  1808. SaAisErrorT *retval, SaAmfRecommendedRecoveryT recommendedRecovery)
  1809. {
  1810. struct amf_csi_assignment *csi_assignment;
  1811. struct amf_comp *comp = NULL;
  1812. assert (retval != NULL);
  1813. *retval = SA_AIS_OK;
  1814. switch (interface) {
  1815. case AMF_RESPONSE_CSISETCALLBACK: {
  1816. ENTER("'%s'", dn->value);
  1817. csi_assignment = amf_csi_assignment_find (amf_cluster, dn);
  1818. assert (csi_assignment != NULL);
  1819. comp = csi_assignment->comp;
  1820. dprintf ("CSI '%s' set callback response from '%s', error: %d",
  1821. csi_assignment->csi->name.value,
  1822. csi_assignment->comp->name.value, error);
  1823. comp = csi_assignment->comp;
  1824. if (error == SA_AIS_OK) {
  1825. comp_ha_state_set (
  1826. comp, csi_assignment, csi_assignment->requested_ha_state);
  1827. } else if (error == SA_AIS_ERR_FAILED_OPERATION) {
  1828. amf_si_comp_set_ha_state_failed (csi_assignment->csi->si,
  1829. csi_assignment);
  1830. } else {
  1831. *retval = SA_AIS_ERR_INVALID_PARAM;
  1832. }
  1833. break;
  1834. }
  1835. case AMF_RESPONSE_CSIREMOVECALLBACK: {
  1836. ENTER("'%s'", dn->value);
  1837. csi_assignment = amf_csi_assignment_find (amf_cluster, dn);
  1838. assert (csi_assignment != NULL);
  1839. dprintf ("Lib csi '%s' remove callback response from '%s', error: %d",
  1840. csi_assignment->csi->name.value,
  1841. csi_assignment->comp->name.value, error);
  1842. comp = csi_assignment->comp;
  1843. if (error == SA_AIS_OK || error == SA_AIS_ERR_FAILED_OPERATION) {
  1844. amf_si_comp_csi_removed (csi_assignment->csi->si,
  1845. csi_assignment, error);
  1846. } else {
  1847. *retval = SA_AIS_ERR_INVALID_PARAM;
  1848. }
  1849. break;
  1850. }
  1851. case AMF_RESPONSE_HEALTHCHECKCALLBACK: {
  1852. dprintf("AMF_RESPONSE_HEALTHCHECKCALLBACK for %s", dn->value);
  1853. comp = amf_comp_find (amf_cluster, dn);
  1854. assert (comp);
  1855. amf_healthcheck_t *healthcheck = amf_comp_find_healthcheck (
  1856. comp, healthcheck_key);
  1857. assert (comp);
  1858. healthcheck->recommendedRecovery = recommendedRecovery;
  1859. comp_recover_action (comp, healthcheck->recommendedRecovery);
  1860. break;
  1861. }
  1862. #if 0
  1863. case AMF_RESPONSE_COMPONENTTERMINATECALLBACK: {
  1864. struct component_terminate_callback_data *callback_data = data;
  1865. dprintf ("Lib comp '%s' terminate callback response, error: %d",
  1866. callback_data->comp->name.value, error);
  1867. comp_presence_state_set (callback_data->comp,
  1868. SA_AMF_PRESENCE_UNINSTANTIATED);
  1869. break;
  1870. }
  1871. #endif
  1872. default:
  1873. assert (0);
  1874. break;
  1875. }
  1876. return comp;
  1877. }
  1878. /**
  1879. * Request a component to assume a particular HA state
  1880. * @param comp
  1881. * @param csi_assignment
  1882. * @param requested_ha_state
  1883. */
  1884. void amf_comp_hastate_set (
  1885. struct amf_comp *component,
  1886. struct amf_csi_assignment *csi_assignment)
  1887. {
  1888. ENTER ("'%s'", csi_assignment->csi->name.value);
  1889. assert (component != NULL && csi_assignment != NULL);
  1890. if (!amf_comp_is_error_suspected (component)) {
  1891. lib_csi_set_request(component, csi_assignment);
  1892. } else {
  1893. if (csi_assignment->requested_ha_state == SA_AMF_HA_QUIESCED) {
  1894. csi_assignment->saAmfCSICompHAState = csi_assignment->requested_ha_state;
  1895. } else {
  1896. dprintf ("csi_assignment->requested_ha_state = %d",
  1897. component->error_suspected);
  1898. assert (0);
  1899. }
  1900. }
  1901. LEAVE("");
  1902. }
  1903. /**
  1904. * Request termination of a component
  1905. * @param comp
  1906. */
  1907. void amf_comp_terminate (struct amf_comp *comp)
  1908. {
  1909. dprintf ("comp terminate '%s'\n", getSaNameT (&comp->name));
  1910. comp_presence_state_set (comp, SA_AMF_PRESENCE_TERMINATING);
  1911. if (amf_su_is_local (comp->su)) {
  1912. amf_comp_healthcheck_stop (comp, NULL);
  1913. amf_comp_pm_stop(comp, SA_AMF_PM_ALL_PROCESSES, 0, SA_AMF_PM_ALL_ERRORS);
  1914. if (amf_comp_is_error_suspected(comp)) {
  1915. clc_interfaces[comp->comptype]->cleanup (comp);
  1916. } else {
  1917. /*TODO temination implementation */
  1918. /*clc_interfaces[comp->comptype]->terminate (comp);*/
  1919. clc_interfaces[comp->comptype]->cleanup (comp);
  1920. }
  1921. }
  1922. }
  1923. /**
  1924. * Request restart of a component
  1925. * @param comp
  1926. */
  1927. void amf_comp_restart (struct amf_comp *comp)
  1928. {
  1929. dprintf ("comp restart '%s'\n", getSaNameT (&comp->name));
  1930. comp_presence_state_set (comp, SA_AMF_PRESENCE_RESTARTING);
  1931. comp->saAmfCompRestartCount += 1;
  1932. if (amf_su_is_local (comp->su)) {
  1933. amf_comp_healthcheck_stop (comp, NULL);
  1934. amf_comp_pm_stop(comp, SA_AMF_PM_ALL_PROCESSES, 0, SA_AMF_PM_ALL_ERRORS);
  1935. clc_interfaces[comp->comptype]->cleanup (comp);
  1936. }
  1937. }
  1938. /**
  1939. * Request to return the HA state for a components CSI
  1940. * @param comp
  1941. * @param csi_name
  1942. * @param ha_state
  1943. *
  1944. * @return SaAisErrorT
  1945. */
  1946. SaAisErrorT amf_comp_hastate_get (
  1947. struct amf_comp *comp, SaNameT *csi_name, SaAmfHAStateT *ha_state)
  1948. {
  1949. struct amf_csi_assignment *assignment;
  1950. assert (comp != NULL && csi_name != NULL && ha_state != NULL);
  1951. dprintf ("comp ha state get from comp '%s' CSI '%s'\n",
  1952. getSaNameT (&comp->name), csi_name->value);
  1953. assignment = csi_assignment_find_in (comp, csi_name);
  1954. if (assignment != NULL) {
  1955. *ha_state = assignment->saAmfCSICompHAState;
  1956. return SA_AIS_OK;
  1957. }
  1958. return SA_AIS_ERR_NOT_EXIST;
  1959. }
  1960. /**
  1961. * Response from a component informs AMF that it has performed a healthcheck
  1962. * @param comp
  1963. * @param healthcheckKey
  1964. * @param healthcheckResult
  1965. *
  1966. * @return SaAisErrorT
  1967. */
  1968. SaAisErrorT amf_comp_healthcheck_confirm (
  1969. struct amf_comp *comp,
  1970. SaAmfHealthcheckKeyT *healthcheckKey,
  1971. SaAisErrorT healthcheckResult)
  1972. {
  1973. struct amf_healthcheck *healthcheck;
  1974. SaAisErrorT error = SA_AIS_OK;
  1975. healthcheck = amf_comp_find_healthcheck (comp, healthcheckKey);
  1976. if (is_not_instantiating_or_instantiated_or_restarting(comp)) {
  1977. log_printf (LOG_ERR, "HealthcheckConfirm: ignored for key = %s, "
  1978. "due to wrong state = %d, comp = %s",
  1979. healthcheckKey->key, comp->saAmfCompPresenceState, comp->name.value);
  1980. error = SA_AIS_OK;
  1981. goto out;
  1982. }
  1983. if (healthcheck == NULL) {
  1984. log_printf (LOG_ERR, "Healthcheckstop: Healthcheck '%s' not found",
  1985. healthcheckKey->key);
  1986. error = SA_AIS_ERR_NOT_EXIST;
  1987. } else if (healthcheck->active) {
  1988. if (healthcheckResult == SA_AIS_OK) {
  1989. /* the response was on time, restart the supervision timer */
  1990. poll_timer_delete (aisexec_poll_handle,
  1991. healthcheck->timer_handle_period);
  1992. poll_timer_add (aisexec_poll_handle,
  1993. healthcheck->saAmfHealthcheckPeriod,
  1994. (void *)healthcheck,
  1995. timer_function_healthcheck_tmo,
  1996. &healthcheck->timer_handle_period);
  1997. } else if (healthcheckResult == SA_AIS_ERR_FAILED_OPERATION) {
  1998. /* send to cluster */
  1999. if (!comp->error_suspected) {
  2000. poll_timer_delete (aisexec_poll_handle,
  2001. healthcheck->timer_handle_period);
  2002. mcast_healthcheck_tmo_event (healthcheck);
  2003. }
  2004. } else {
  2005. error = SA_AIS_ERR_INVALID_PARAM;
  2006. }
  2007. } else {
  2008. error = SA_AIS_ERR_INVALID_PARAM;
  2009. }
  2010. out:
  2011. return error;
  2012. }
  2013. void amf_comp_operational_state_set (struct amf_comp *comp,
  2014. SaAmfOperationalStateT oper_state)
  2015. {
  2016. comp->saAmfCompOperState = oper_state;
  2017. TRACE1 ("Setting comp '%s', SU '%s' operational state: %s\n",
  2018. comp->name.value, comp->su->name.value,
  2019. amf_op_state (comp->saAmfCompOperState));
  2020. amf_su_comp_state_changed (
  2021. comp->su, comp, SA_AMF_OP_STATE, oper_state);
  2022. }
  2023. int amf_comp_get_saAmfCompNumCurrActiveCsi(struct amf_comp *component)
  2024. {
  2025. int cnt = 0;
  2026. struct amf_csi_assignment *csi_assignment;
  2027. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  2028. while (csi_assignment != NULL) {
  2029. if (csi_assignment->saAmfCSICompHAState == SA_AMF_HA_ACTIVE) {
  2030. cnt++;
  2031. }
  2032. csi_assignment = amf_comp_get_next_csi_assignment (
  2033. component, csi_assignment);
  2034. }
  2035. return cnt;
  2036. }
  2037. int amf_comp_get_saAmfCompNumCurrStandbyCsi(struct amf_comp *component)
  2038. {
  2039. int cnt = 0;
  2040. struct amf_csi_assignment *csi_assignment;
  2041. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  2042. while (csi_assignment != NULL) {
  2043. if (csi_assignment->saAmfCSICompHAState == SA_AMF_HA_STANDBY) {
  2044. cnt++;
  2045. }
  2046. csi_assignment = amf_comp_get_next_csi_assignment (
  2047. component, csi_assignment);
  2048. }
  2049. return cnt;
  2050. }
  2051. SaAmfReadinessStateT amf_comp_get_saAmfCompReadinessState (
  2052. struct amf_comp *component)
  2053. {
  2054. if (component->saAmfCompOperState == SA_AMF_OPERATIONAL_ENABLED) {
  2055. return amf_su_get_saAmfSUReadinessState (component->su);
  2056. } else if (component->saAmfCompOperState == SA_AMF_OPERATIONAL_DISABLED) {
  2057. return SA_AMF_READINESS_OUT_OF_SERVICE;
  2058. }
  2059. assert (0);
  2060. /* XXX we fall here in case NDEBUG is set */
  2061. return -1;
  2062. }
  2063. /**
  2064. * Component is informed that the node where the 'real'
  2065. * component process is executing has unexpectadly left the
  2066. * node. If there is a pending interaction between AMF
  2067. * (component) and the 'real' component process, then component
  2068. * will indicate to its subordinate objects the interaction
  2069. * failed. Pending presence state changes is indicated by
  2070. * reporting the new state is uninstantiated while pending csi
  2071. * operations are indicated by 'operation failed'.
  2072. * @param comp
  2073. *
  2074. * @return void
  2075. */
  2076. void amf_comp_node_left (struct amf_comp *component)
  2077. {
  2078. int change_pending = 0;
  2079. struct amf_csi_assignment *csi_assignment;
  2080. ENTER("saAmfCompPresenceState = %d", component->saAmfCompPresenceState);
  2081. amf_comp_error_suspected_clear (component);
  2082. if (component->saAmfCompPresenceState == SA_AMF_PRESENCE_INSTANTIATING ||
  2083. component->saAmfCompPresenceState == SA_AMF_PRESENCE_RESTARTING ||
  2084. component->saAmfCompPresenceState == SA_AMF_PRESENCE_TERMINATING) {
  2085. change_pending = 1;
  2086. }
  2087. component->saAmfCompPresenceState = SA_AMF_PRESENCE_UNINSTANTIATED;
  2088. if (amf_su_are_all_comps_in_su (component->su,
  2089. SA_AMF_PRESENCE_UNINSTANTIATED)) {
  2090. component->su->saAmfSUPresenceState = SA_AMF_PRESENCE_UNINSTANTIATED;
  2091. }
  2092. if (change_pending) {
  2093. change_pending = 0;
  2094. amf_su_comp_state_changed ( component->su,
  2095. component,
  2096. SA_AMF_PRESENCE_STATE,
  2097. SA_AMF_PRESENCE_UNINSTANTIATED);
  2098. }
  2099. if (component->saAmfCompOperState == SA_AMF_OPERATIONAL_ENABLED) {
  2100. change_pending = 1;
  2101. }
  2102. component->saAmfCompOperState = SA_AMF_OPERATIONAL_DISABLED;
  2103. if (change_pending) {
  2104. change_pending =0;
  2105. amf_su_comp_state_changed (component->su,
  2106. component,
  2107. SA_AMF_OP_STATE,
  2108. SA_AMF_OPERATIONAL_DISABLED);
  2109. }
  2110. csi_assignment = amf_comp_get_next_csi_assignment (component, NULL);
  2111. while (csi_assignment != NULL) {
  2112. if (csi_assignment->requested_ha_state !=
  2113. csi_assignment->saAmfCSICompHAState) {
  2114. amf_si_comp_set_ha_state_failed (
  2115. csi_assignment->csi->si,csi_assignment);
  2116. }
  2117. csi_assignment = amf_comp_get_next_csi_assignment (
  2118. component, csi_assignment);
  2119. }
  2120. }
  2121. /**
  2122. * Serialize a component including variable length arrays and
  2123. * strings to a buffer returned. Buffer is to be freed by
  2124. * caller.
  2125. * @param component
  2126. * @param len
  2127. *
  2128. * @return void*
  2129. */
  2130. void *amf_comp_serialize (struct amf_comp *component, int *len)
  2131. {
  2132. char *buf = NULL;
  2133. int i, offset = 0, size = 0;
  2134. TRACE8 ("%s", component->name.value);
  2135. buf = amf_serialize_SaNameT (buf, &size, &offset, &component->name);
  2136. /* count cstypes and write to buf */
  2137. for (i = 0; component->saAmfCompCsTypes &&
  2138. component->saAmfCompCsTypes[i] != NULL; i++);
  2139. buf = amf_serialize_SaUint32T (buf, &size, &offset, i);
  2140. for (i = 0; component->saAmfCompCsTypes &&
  2141. component->saAmfCompCsTypes[i] != NULL; i++) {
  2142. buf = amf_serialize_SaNameT (
  2143. buf, &size, &offset, component->saAmfCompCsTypes[i]);
  2144. }
  2145. buf = amf_serialize_SaUint32T (
  2146. buf, &size, &offset, component->saAmfCompCategory);
  2147. buf = amf_serialize_SaUint32T (
  2148. buf, &size, &offset, component->saAmfCompCapability);
  2149. buf = amf_serialize_SaUint32T (
  2150. buf, &size, &offset, component->saAmfCompNumMaxActiveCsi);
  2151. buf = amf_serialize_SaUint32T (
  2152. buf, &size, &offset, component->saAmfCompNumMaxStandbyCsi);
  2153. /* count environment vars and write to buf */
  2154. for (i = 0; component->saAmfCompCmdEnv &&
  2155. component->saAmfCompCmdEnv[i] != NULL; i++);
  2156. buf = amf_serialize_SaUint32T (buf, &size, &offset, i);
  2157. for (i = 0; component->saAmfCompCmdEnv &&
  2158. component->saAmfCompCmdEnv[i] != NULL; i++) {
  2159. buf = amf_serialize_SaStringT (
  2160. buf, &size, &offset, component->saAmfCompCmdEnv[i]);
  2161. }
  2162. buf = amf_serialize_SaUint32T (
  2163. buf, &size, &offset, component->saAmfCompDefaultClcCliTimeout);
  2164. buf = amf_serialize_SaUint32T (
  2165. buf, &size, &offset, component->saAmfCompDefaultCallbackTimeOut);
  2166. buf = amf_serialize_SaStringT (
  2167. buf, &size, &offset, component->saAmfCompInstantiateCmd);
  2168. buf = amf_serialize_SaStringT (
  2169. buf, &size, &offset, component->saAmfCompInstantiateCmdArgv);
  2170. buf = amf_serialize_SaUint32T (
  2171. buf, &size, &offset, component->saAmfCompInstantiateTimeout);
  2172. buf = amf_serialize_SaUint32T (
  2173. buf, &size, &offset, component->saAmfCompInstantiationLevel);
  2174. buf = amf_serialize_SaUint32T (
  2175. buf, &size, &offset, component->saAmfCompNumMaxInstantiateWithoutDelay);
  2176. buf = amf_serialize_SaUint32T (
  2177. buf, &size, &offset, component->saAmfCompNumMaxInstantiateWithDelay);
  2178. buf = amf_serialize_SaUint32T (
  2179. buf, &size, &offset, component->saAmfCompDelayBetweenInstantiateAttempts);
  2180. buf = amf_serialize_SaStringT (
  2181. buf, &size, &offset, component->saAmfCompTerminateCmd);
  2182. buf = amf_serialize_SaUint32T (
  2183. buf, &size, &offset, component->saAmfCompTerminateTimeout);
  2184. buf = amf_serialize_SaStringT (
  2185. buf, &size, &offset, component->saAmfCompTerminateCmdArgv);
  2186. buf = amf_serialize_SaStringT (
  2187. buf, &size, &offset, component->saAmfCompCleanupCmd);
  2188. buf = amf_serialize_SaUint32T (
  2189. buf, &size, &offset, component->saAmfCompCleanupTimeout);
  2190. buf = amf_serialize_SaStringT (
  2191. buf, &size, &offset, component->saAmfCompCleanupCmdArgv);
  2192. buf = amf_serialize_SaStringT (
  2193. buf, &size, &offset, component->saAmfCompAmStartCmd);
  2194. buf = amf_serialize_SaUint32T (
  2195. buf, &size, &offset, component->saAmfCompAmStartTimeout);
  2196. buf = amf_serialize_SaStringT (
  2197. buf, &size, &offset, component->saAmfCompAmStartCmdArgv);
  2198. buf = amf_serialize_SaUint32T (
  2199. buf, &size, &offset, component->saAmfCompNumMaxAmStartAttempt);
  2200. buf = amf_serialize_SaStringT (
  2201. buf, &size, &offset, component->saAmfCompAmStopCmd);
  2202. buf = amf_serialize_SaUint32T (
  2203. buf, &size, &offset, component->saAmfCompAmStopTimeout);
  2204. buf = amf_serialize_SaStringT (
  2205. buf, &size, &offset, component->saAmfCompAmStopCmdArgv);
  2206. buf = amf_serialize_SaUint32T (
  2207. buf, &size, &offset, component->saAmfCompNumMaxAmStopAttempt);
  2208. buf = amf_serialize_SaUint32T (
  2209. buf, &size, &offset, component->saAmfCompTerminateCallbackTimeout);
  2210. buf = amf_serialize_SaUint32T (
  2211. buf, &size, &offset, component->saAmfCompCSISetCallbackTimeout);
  2212. buf = amf_serialize_SaUint32T (
  2213. buf, &size, &offset, component->saAmfCompQuiescingCompleteTimeout);
  2214. buf = amf_serialize_SaUint32T (
  2215. buf, &size, &offset, component->saAmfCompCSIRmvCallbackTimeout);
  2216. buf = amf_serialize_SaUint32T (
  2217. buf, &size, &offset, component->saAmfCompRecoveryOnError);
  2218. buf = amf_serialize_SaUint32T (
  2219. buf, &size, &offset, component->saAmfCompDisableRestart);
  2220. buf = amf_serialize_SaNameT (
  2221. buf, &size, &offset, &component->saAmfCompProxyCsi);
  2222. buf = amf_serialize_SaUint32T (
  2223. buf, &size, &offset, component->saAmfCompOperState);
  2224. buf = amf_serialize_SaUint32T (
  2225. buf, &size, &offset, component->saAmfCompPresenceState);
  2226. buf = amf_serialize_SaUint32T (
  2227. buf, &size, &offset, component->saAmfCompRestartCount);
  2228. buf = amf_serialize_SaNameT (
  2229. buf, &size, &offset, &component->saAmfCompCurrProxyName);
  2230. buf = amf_serialize_SaStringT (
  2231. buf, &size, &offset, component->clccli_path);
  2232. buf = amf_serialize_SaUint32T (
  2233. buf, &size, &offset, component->comptype);
  2234. buf = amf_serialize_SaUint32T (
  2235. buf, &size, &offset, component->error_suspected);
  2236. *len = offset;
  2237. return buf;
  2238. }
  2239. /**
  2240. * Deserialize a buffer into a AMF component object.
  2241. * @param su
  2242. * @param buf
  2243. * @param size
  2244. *
  2245. * @return struct amf_comp*
  2246. */
  2247. struct amf_comp *amf_comp_deserialize (struct amf_su *su, char *buf)
  2248. {
  2249. char *tmp = buf;
  2250. int i;
  2251. SaUint32T cnt;
  2252. struct amf_comp *component = amf_comp_new (su, "");
  2253. tmp = amf_deserialize_SaNameT (tmp, &component->name);
  2254. tmp = amf_deserialize_SaUint32T (tmp, &cnt);
  2255. component->saAmfCompCsTypes = amf_malloc ((cnt + 1) * sizeof (SaNameT*));
  2256. for (i = 0; i < cnt; i++) {
  2257. component->saAmfCompCsTypes[i] = amf_malloc (sizeof (SaNameT));
  2258. tmp = amf_deserialize_SaNameT (tmp, component->saAmfCompCsTypes[i]);
  2259. }
  2260. component->saAmfCompCsTypes[i] = NULL;
  2261. tmp = amf_deserialize_SaUint32T (tmp, &component->saAmfCompCategory);
  2262. tmp = amf_deserialize_SaUint32T (tmp, &component->saAmfCompCapability);
  2263. tmp = amf_deserialize_SaUint32T (tmp, &component->saAmfCompNumMaxActiveCsi);
  2264. tmp = amf_deserialize_SaUint32T (tmp, &component->saAmfCompNumMaxStandbyCsi);
  2265. tmp = amf_deserialize_SaUint32T (tmp, &cnt);
  2266. component->saAmfCompCmdEnv = amf_malloc ((cnt + 1) * sizeof (SaStringT*));
  2267. for (i = 0; i < cnt; i++) {
  2268. tmp = amf_deserialize_SaStringT (tmp, &component->saAmfCompCmdEnv[i]);
  2269. }
  2270. component->saAmfCompCmdEnv[i] = NULL;
  2271. tmp = amf_deserialize_SaUint32T (
  2272. tmp, &component->saAmfCompDefaultClcCliTimeout);
  2273. tmp = amf_deserialize_SaUint32T (
  2274. tmp, &component->saAmfCompDefaultCallbackTimeOut);
  2275. tmp = amf_deserialize_SaStringT (
  2276. tmp, &component->saAmfCompInstantiateCmd);
  2277. tmp = amf_deserialize_SaStringT (
  2278. tmp, &component->saAmfCompInstantiateCmdArgv);
  2279. tmp = amf_deserialize_SaUint32T (
  2280. tmp, &component->saAmfCompInstantiateTimeout);
  2281. tmp = amf_deserialize_SaUint32T (
  2282. tmp, &component->saAmfCompInstantiationLevel);
  2283. tmp = amf_deserialize_SaUint32T (
  2284. tmp, &component->saAmfCompNumMaxInstantiateWithoutDelay);
  2285. tmp = amf_deserialize_SaUint32T (
  2286. tmp, &component->saAmfCompNumMaxInstantiateWithDelay);
  2287. tmp = amf_deserialize_SaUint32T (
  2288. tmp, &component->saAmfCompDelayBetweenInstantiateAttempts);
  2289. tmp = amf_deserialize_SaStringT (
  2290. tmp, &component->saAmfCompTerminateCmd);
  2291. tmp = amf_deserialize_SaUint32T (
  2292. tmp, &component->saAmfCompTerminateTimeout);
  2293. tmp = amf_deserialize_SaStringT (
  2294. tmp, &component->saAmfCompTerminateCmdArgv);
  2295. tmp = amf_deserialize_SaStringT (
  2296. tmp, &component->saAmfCompCleanupCmd);
  2297. tmp = amf_deserialize_SaUint32T (
  2298. tmp, &component->saAmfCompCleanupTimeout);
  2299. tmp = amf_deserialize_SaStringT (
  2300. tmp, &component->saAmfCompCleanupCmdArgv);
  2301. tmp = amf_deserialize_SaStringT (
  2302. tmp, &component->saAmfCompAmStartCmd);
  2303. tmp = amf_deserialize_SaUint32T (
  2304. tmp, &component->saAmfCompAmStartTimeout);
  2305. tmp = amf_deserialize_SaStringT (
  2306. tmp, &component->saAmfCompAmStartCmdArgv);
  2307. tmp = amf_deserialize_SaUint32T (
  2308. tmp, &component->saAmfCompNumMaxAmStartAttempt);
  2309. tmp = amf_deserialize_SaStringT (
  2310. tmp, &component->saAmfCompAmStopCmd);
  2311. tmp = amf_deserialize_SaUint32T (
  2312. tmp, &component->saAmfCompAmStopTimeout);
  2313. tmp = amf_deserialize_SaStringT (
  2314. tmp, &component->saAmfCompAmStopCmdArgv);
  2315. tmp = amf_deserialize_SaUint32T (
  2316. tmp, &component->saAmfCompNumMaxAmStopAttempt);
  2317. tmp = amf_deserialize_SaUint32T (
  2318. tmp, &component->saAmfCompTerminateCallbackTimeout);
  2319. tmp = amf_deserialize_SaUint32T (
  2320. tmp, &component->saAmfCompCSISetCallbackTimeout);
  2321. tmp = amf_deserialize_SaUint32T (
  2322. tmp, &component->saAmfCompQuiescingCompleteTimeout);
  2323. tmp = amf_deserialize_SaUint32T (
  2324. tmp, &component->saAmfCompCSIRmvCallbackTimeout);
  2325. tmp = amf_deserialize_SaUint32T (
  2326. tmp, &component->saAmfCompRecoveryOnError);
  2327. tmp = amf_deserialize_SaUint32T (
  2328. tmp, &component->saAmfCompDisableRestart);
  2329. tmp = amf_deserialize_SaNameT (
  2330. tmp, &component->saAmfCompProxyCsi);
  2331. tmp = amf_deserialize_SaUint32T (
  2332. tmp, &component->saAmfCompOperState);
  2333. tmp = amf_deserialize_SaUint32T (
  2334. tmp, &component->saAmfCompPresenceState);
  2335. tmp = amf_deserialize_SaUint32T (
  2336. tmp, &component->saAmfCompRestartCount);
  2337. tmp = amf_deserialize_SaNameT (
  2338. tmp, &component->saAmfCompCurrProxyName);
  2339. tmp = amf_deserialize_SaStringT (
  2340. tmp, &component->clccli_path);
  2341. tmp = amf_deserialize_SaUint32T (
  2342. tmp, &component->comptype);
  2343. tmp = amf_deserialize_SaUint32T (
  2344. tmp, &component->error_suspected);
  2345. return component;
  2346. }
  2347. void *amf_healthcheck_serialize (struct amf_healthcheck *healthcheck, int *len)
  2348. {
  2349. char *buf = NULL;
  2350. int offset = 0, size = 0;
  2351. TRACE8 ("%s", healthcheck->safHealthcheckKey.key);
  2352. buf = amf_serialize_opaque (buf, &size, &offset,
  2353. &healthcheck->safHealthcheckKey.key, SA_AMF_HEALTHCHECK_KEY_MAX);
  2354. buf = amf_serialize_SaUint16T (buf, &size, &offset,
  2355. healthcheck->safHealthcheckKey.keyLen);
  2356. buf = amf_serialize_SaUint32T (buf, &size, &offset,
  2357. healthcheck->saAmfHealthcheckMaxDuration);
  2358. buf = amf_serialize_SaUint32T (buf, &size, &offset,
  2359. healthcheck->saAmfHealthcheckPeriod);
  2360. *len = offset;
  2361. return buf;
  2362. }
  2363. struct amf_healthcheck *amf_healthcheck_deserialize (
  2364. struct amf_comp *comp, char *buf)
  2365. {
  2366. char *tmp = buf;
  2367. int cnt;
  2368. amf_healthcheck_t *healthcheck = amf_healthcheck_new (comp);
  2369. tmp = amf_deserialize_opaque (tmp, &healthcheck->safHealthcheckKey.key, &cnt);
  2370. tmp = amf_deserialize_SaUint16T (tmp,
  2371. &healthcheck->safHealthcheckKey.keyLen);
  2372. tmp = amf_deserialize_SaUint32T (tmp,
  2373. &healthcheck->saAmfHealthcheckMaxDuration);
  2374. tmp = amf_deserialize_SaUint32T (tmp,
  2375. &healthcheck->saAmfHealthcheckPeriod);
  2376. return healthcheck;
  2377. }
  2378. amf_healthcheck_t *amf_healthcheck_new (struct amf_comp *comp)
  2379. {
  2380. amf_healthcheck_t *healthcheck = amf_calloc (1, sizeof (amf_healthcheck_t));
  2381. healthcheck->comp = comp;
  2382. healthcheck->next = comp->healthcheck_head;
  2383. comp->healthcheck_head = healthcheck;
  2384. return healthcheck;
  2385. }
  2386. void amf_comp_csi_remove (amf_comp_t *component,
  2387. amf_csi_assignment_t *csi_assignment)
  2388. {
  2389. struct res_lib_amf_csiremovecallback res_lib;
  2390. ENTER("");
  2391. res_lib.header.id = MESSAGE_RES_AMF_CSIREMOVECALLBACK;
  2392. res_lib.header.size = sizeof (struct res_lib_amf_csiremovecallback);
  2393. res_lib.header.error = SA_AIS_OK;
  2394. res_lib.invocation =
  2395. invocation_create (AMF_RESPONSE_CSIREMOVECALLBACK, csi_assignment);
  2396. amf_comp_dn_make (component, &res_lib.compName);
  2397. amf_csi_dn_make (csi_assignment->csi, &res_lib.csiName);
  2398. res_lib.csiFlags = SA_AMF_CSI_TARGET_ONE;
  2399. TRACE7 ("sending CSI remove request to component %s",
  2400. res_lib.compName.value);
  2401. openais_conn_send_response (
  2402. openais_conn_partner_get (component->conn),
  2403. &res_lib, sizeof (struct res_lib_amf_csiremovecallback));
  2404. }
  2405. void amf_comp_error_suspected_clear (amf_comp_t *comp)
  2406. {
  2407. comp->error_suspected = 0;
  2408. }
  2409. void amf_comp_error_suspected_set (amf_comp_t *comp)
  2410. {
  2411. comp->error_suspected = 1;
  2412. }
  2413. int amf_comp_is_error_suspected (amf_comp_t *comp)
  2414. {
  2415. return comp->error_suspected ? 1 : 0;
  2416. }