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