amfcomp.c 80 KB

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