evt.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172
  1. /*
  2. * Copyright (c) 2004 Mark Haverkamp
  3. * Copyright (c) 2004 Open Source Development Lab
  4. *
  5. * All rights reserved.
  6. *
  7. * This software licensed under BSD license, the text of which follows:
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * - Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * - Neither the name of the Open Source Development Lab nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  31. * THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. //#define DEBUG
  34. //#define EVT_EVENT_LIST_CHECK
  35. //#define EVT_ALLOC_CHECK
  36. #include <sys/types.h>
  37. #include <malloc.h>
  38. #include <errno.h>
  39. #include <sys/time.h>
  40. #include <sys/uio.h>
  41. #include <sys/socket.h>
  42. #include <netinet/in.h>
  43. #include <arpa/inet.h>
  44. #include "../include/ais_types.h"
  45. #include "../include/ais_msg.h"
  46. #include "../include/list.h"
  47. #include "../include/queue.h"
  48. #include "util.h"
  49. #include "aispoll.h"
  50. #include "mempool.h"
  51. #include "parse.h"
  52. #include "main.h"
  53. #include "print.h"
  54. #include "gmi.h"
  55. #include "hdb.h"
  56. #include "clm.h"
  57. #include "evt.h"
  58. static int message_handler_req_lib_activatepoll (struct conn_info *conn_info,
  59. void *message);
  60. static int lib_evt_open_channel(struct conn_info *conn_info, void *message);
  61. static int lib_evt_close_channel(struct conn_info *conn_info, void *message);
  62. static int lib_evt_event_subscribe(struct conn_info *conn_info,
  63. void *message);
  64. static int lib_evt_event_unsubscribe(struct conn_info *conn_info,
  65. void *message);
  66. static int lib_evt_event_publish(struct conn_info *conn_info, void *message);
  67. static int lib_evt_event_clear_retentiontime(struct conn_info *conn_info,
  68. void *message);
  69. static int lib_evt_event_data_get(struct conn_info *conn_info,
  70. void *message);
  71. static int evt_conf_change(
  72. enum gmi_configuration_type configuration_type,
  73. struct sockaddr_in *member_list, int member_list_entries,
  74. struct sockaddr_in *left_list, int left_list_entries,
  75. struct sockaddr_in *joined_list, int joined_list_entries);
  76. static int evt_initialize(struct conn_info *conn_info, void *msg);
  77. static int evt_finalize(struct conn_info *conn_info);
  78. static int evt_exec_init(void);
  79. static struct libais_handler evt_libais_handlers[] = {
  80. {
  81. .libais_handler_fn = message_handler_req_lib_activatepoll,
  82. .response_size = sizeof(struct res_lib_activatepoll),
  83. .response_id = MESSAGE_RES_LIB_ACTIVATEPOLL,
  84. .gmi_prio = GMI_PRIO_RECOVERY
  85. },
  86. {
  87. .libais_handler_fn = lib_evt_open_channel,
  88. .response_size = sizeof(struct res_evt_channel_open),
  89. .response_id = MESSAGE_RES_EVT_OPEN_CHANNEL,
  90. .gmi_prio = GMI_PRIO_MED
  91. },
  92. {
  93. .libais_handler_fn = lib_evt_close_channel,
  94. .response_size = sizeof(struct res_evt_channel_close),
  95. .response_id = MESSAGE_RES_EVT_CLOSE_CHANNEL,
  96. .gmi_prio = GMI_PRIO_RECOVERY
  97. },
  98. {
  99. .libais_handler_fn = lib_evt_event_subscribe,
  100. .response_size = sizeof(struct res_evt_event_subscribe),
  101. .response_id = MESSAGE_RES_EVT_SUBSCRIBE,
  102. .gmi_prio = GMI_PRIO_RECOVERY
  103. },
  104. {
  105. .libais_handler_fn = lib_evt_event_unsubscribe,
  106. .response_size = sizeof(struct res_evt_event_unsubscribe),
  107. .response_id = MESSAGE_RES_EVT_UNSUBSCRIBE,
  108. .gmi_prio = GMI_PRIO_RECOVERY
  109. },
  110. {
  111. .libais_handler_fn = lib_evt_event_publish,
  112. .response_size = sizeof(struct res_evt_event_publish),
  113. .response_id = MESSAGE_RES_EVT_PUBLISH,
  114. .gmi_prio = GMI_PRIO_LOW
  115. },
  116. {
  117. .libais_handler_fn = lib_evt_event_clear_retentiontime,
  118. .response_size = sizeof(struct res_evt_event_clear_retentiontime),
  119. .response_id = MESSAGE_REQ_EVT_CLEAR_RETENTIONTIME,
  120. .gmi_prio = GMI_PRIO_MED
  121. },
  122. {
  123. .libais_handler_fn = lib_evt_event_data_get,
  124. .response_size = sizeof(struct lib_event_data),
  125. .response_id = MESSAGE_RES_EVT_EVENT_DATA,
  126. .gmi_prio = GMI_PRIO_RECOVERY
  127. },
  128. };
  129. static int evt_remote_evt(void *msg, struct in_addr source_addr);
  130. static int evt_remote_recovery_evt(void *msg, struct in_addr source_addr);
  131. static int evt_remote_chan_op(void *msg, struct in_addr source_addr);
  132. static int (*evt_exec_handler_fns[]) (void *m, struct in_addr s) = {
  133. evt_remote_evt,
  134. evt_remote_chan_op,
  135. evt_remote_recovery_evt
  136. };
  137. struct service_handler evt_service_handler = {
  138. .libais_handlers = evt_libais_handlers,
  139. .libais_handlers_count = sizeof(evt_libais_handlers) /
  140. sizeof(struct libais_handler),
  141. .aisexec_handler_fns = evt_exec_handler_fns,
  142. .aisexec_handler_fns_count = sizeof(evt_exec_handler_fns) /
  143. sizeof(int (*)),
  144. .confchg_fn = evt_conf_change,
  145. .libais_init_fn = evt_initialize,
  146. .libais_exit_fn = evt_finalize,
  147. .exec_init_fn = evt_exec_init,
  148. .exec_dump_fn = 0
  149. };
  150. static gmi_recovery_plug_handle evt_recovery_plug_handle;
  151. /*
  152. * list of all retained events
  153. * struct event_data
  154. */
  155. static DECLARE_LIST_INIT(retained_list);
  156. /*
  157. * list of all event channel information
  158. * struct event_svr_channel_instance
  159. */
  160. static DECLARE_LIST_INIT(esc_head);
  161. /*
  162. * list of all active event conn_info structs.
  163. * struct conn_info
  164. */
  165. static DECLARE_LIST_INIT(ci_head);
  166. /*
  167. * Structure to track pending channel open requests.
  168. * ocp_async: 1 for async open
  169. * ocp_invocation: invocation for async open
  170. * ocp_chan_name: requested channel
  171. * ocp_conn_info: conn_info for returning to the library.
  172. * ocp_open_flags: channel open flags
  173. * ocp_timer_handle: timer handle for sync open
  174. * ocp_entry: list entry for pending open list.
  175. */
  176. struct open_chan_pending {
  177. int ocp_async;
  178. SaInvocationT ocp_invocation;
  179. SaNameT ocp_chan_name;
  180. struct conn_info *ocp_conn_info;
  181. SaEvtChannelOpenFlagsT ocp_open_flag;
  182. poll_timer_handle ocp_timer_handle;
  183. uint32_t ocp_c_handle;
  184. struct list_head ocp_entry;
  185. };
  186. /*
  187. * list of pending channel opens
  188. */
  189. static DECLARE_LIST_INIT(open_pending);
  190. static void chan_open_timeout(void *data);
  191. #define min(a,b) ((a) < (b) ? (a) : (b))
  192. /*
  193. * Throttle event delivery to applications to keep
  194. * the exec from using too much memory if the app is
  195. * slow to process its events.
  196. */
  197. #define MAX_EVT_DELIVERY_QUEUE 1000
  198. #define MIN_EVT_QUEUE_RESUME (MAX_EVT_DELIVERY_QUEUE / 2)
  199. #define LOST_PUB "EVENT_SERIVCE"
  200. #define LOST_CHAN "LOST EVENT"
  201. /*
  202. * Event to send when the delivery queue gets too full
  203. */
  204. char lost_evt[] = SA_EVT_LOST_EVENT;
  205. static int dropped_event_size;
  206. static struct event_data *dropped_event;
  207. struct evt_pattern {
  208. SaEvtEventPatternT pat;
  209. char str[sizeof(lost_evt)];
  210. };
  211. static struct evt_pattern dropped_pattern = {
  212. .pat = {&dropped_pattern.str[0],
  213. sizeof(lost_evt)},
  214. .str = {SA_EVT_LOST_EVENT}
  215. };
  216. SaNameT lost_chan = {
  217. .value = LOST_CHAN,
  218. .length = sizeof(LOST_CHAN)
  219. };
  220. SaNameT dropped_publisher = {
  221. .value = LOST_PUB,
  222. .length = sizeof(LOST_PUB)
  223. };
  224. struct event_svr_channel_open;
  225. struct event_svr_channel_subscr;
  226. struct open_count {
  227. SaClmNodeIdT oc_node_id;
  228. uint32_t oc_open_count;
  229. };
  230. /*
  231. * Structure to contain global channel releated information
  232. *
  233. * esc_channel_name: The name of this channel.
  234. * esc_total_opens: The total number of opens on this channel including
  235. * other nodes.
  236. * esc_local_opens: The total number of opens on this channel for this
  237. * node.
  238. * esc_oc_size: The total number of entries in esc_node_opens;
  239. * esc_node_opens: list of node IDs and how many opens are associated.
  240. * esc_retained_count: How many retained events for this channel
  241. * esc_open_chans: list of opens of this channel.
  242. * (event_svr_channel_open.eco_entry)
  243. * esc_entry: links to other channels. (used by esc_head)
  244. */
  245. struct event_svr_channel_instance {
  246. SaNameT esc_channel_name;
  247. uint32_t esc_total_opens;
  248. uint32_t esc_local_opens;
  249. uint32_t esc_oc_size;
  250. struct open_count *esc_node_opens;
  251. uint32_t esc_retained_count;
  252. struct list_head esc_open_chans;
  253. struct list_head esc_entry;
  254. };
  255. /*
  256. * Has the event data in the correct format to send to the library API
  257. * with aditional field for accounting.
  258. *
  259. * ed_ref_count: how many other strutures are referencing.
  260. * ed_retained: retained event list.
  261. * ed_timer_handle: Timer handle for retained event expiration.
  262. * ed_delivered: arrays of open channel pointers that this event
  263. * has been delivered to. (only used for events
  264. * with a retention time).
  265. * ed_delivered_count: Number of entries available in ed_delivered.
  266. * ed_delivered_next: Next free spot in ed_delivered
  267. * ed_my_chan: pointer to the global channel instance associated
  268. * with this event.
  269. * ed_event: The event data formatted to be ready to send.
  270. */
  271. struct event_data {
  272. uint32_t ed_ref_count;
  273. struct list_head ed_retained;
  274. poll_timer_handle ed_timer_handle;
  275. struct event_svr_channel_open **ed_delivered;
  276. uint32_t ed_delivered_count;
  277. uint32_t ed_delivered_next;
  278. struct event_svr_channel_instance *ed_my_chan;
  279. struct lib_event_data ed_event;
  280. };
  281. /*
  282. * Contains a list of pending events to be delivered to a subscribed
  283. * application.
  284. *
  285. * cel_chan_handle: associated library channel handle
  286. * cel_sub_id: associated library subscription ID
  287. * cel_event: event structure to deliver.
  288. * cel_entry: list of pending events
  289. * (struct event_server_instance.esi_events)
  290. */
  291. struct chan_event_list {
  292. uint32_t cel_chan_handle;
  293. uint32_t cel_sub_id;
  294. struct event_data* cel_event;
  295. struct list_head cel_entry;
  296. };
  297. /*
  298. * Contains information about each open for a given channel
  299. *
  300. * eco_flags: How the channel was opened.
  301. * eco_lib_handle: channel handle in the app. Used for event delivery.
  302. * eco_my_handle: the handle used to access this data structure.
  303. * eco_channel: Pointer to global channel info.
  304. * eco_entry: links to other opeinings of this channel.
  305. * eco_instance_entry: links to other channel opeinings for the
  306. * associated server instance.
  307. * eco_subscr: head of list of sbuscriptions for this channel open.
  308. * (event_svr_channel_subscr.ecs_entry)
  309. * eco_conn_info: refrence to EvtInitialize who owns this open.
  310. */
  311. struct event_svr_channel_open {
  312. uint8_t eco_flags;
  313. uint32_t eco_lib_handle;
  314. uint32_t eco_my_handle;
  315. struct event_svr_channel_instance *eco_channel;
  316. struct list_head eco_entry;
  317. struct list_head eco_instance_entry;
  318. struct list_head eco_subscr;
  319. struct conn_info *eco_conn_info;
  320. };
  321. /*
  322. * Contains information about each channel subscription
  323. *
  324. * ecs_open_chan: Link to our open channel.
  325. * ecs_sub_id: Subscription ID.
  326. * ecs_filter_count: number of filters in ecs_filters
  327. * ecs_filters: filters for determining event delivery.
  328. * ecs_entry: Links to other subscriptions to this channel opening.
  329. */
  330. struct event_svr_channel_subscr {
  331. struct event_svr_channel_open *ecs_open_chan;
  332. uint32_t ecs_sub_id;
  333. SaEvtEventFilterArrayT *ecs_filters;
  334. struct list_head ecs_entry;
  335. };
  336. /*
  337. * Member node data
  338. * mn_node_info: cluster node info from membership
  339. * mn_last_evt_id: last seen event ID for this node
  340. * mn_started: Indicates that event service has started
  341. * on this node.
  342. * mn_next: pointer to the next node in the hash chain.
  343. * mn_entry: List of all nodes.
  344. */
  345. struct member_node_data {
  346. struct in_addr mn_node_addr;
  347. SaClmClusterNodeT mn_node_info;
  348. SaEvtEventIdT mn_last_evt_id;
  349. SaClmNodeIdT mn_started;
  350. struct member_node_data *mn_next;
  351. struct list_head mn_entry;
  352. };
  353. DECLARE_LIST_INIT(mnd);
  354. /*
  355. * Global varaibles used by the event service
  356. *
  357. * base_id: Next event ID to assign
  358. * my_node_id: My cluster node id
  359. * in_cfg_change: Config change occurred. Figure out who sends retained evts.
  360. * cleared when retained events have been delivered.
  361. * total_members: how many members in this cluster
  362. * checked_in: keep track during config change.
  363. * any_joined: did any nodes join on this change?
  364. * recovery_node: True if we're the recovery node.
  365. * tok_call_handle: gmi token callback handle for recovery.
  366. * next_retained: pointer to next retained message to send during recovery.
  367. * next_chan: pointer to next channel to send during recovery.
  368. *
  369. */
  370. static SaEvtEventIdT base_id = 0;
  371. static SaClmNodeIdT my_node_id = 0;
  372. static int in_cfg_change = 0;
  373. static int total_members = 0;
  374. static int checked_in = 0;
  375. static int any_joined = 0;
  376. static int recovery_node = 0;
  377. static void *tok_call_handle = 0;
  378. static struct list_head *next_retained = 0;
  379. static struct list_head *next_chan = 0;
  380. /*
  381. * Take the filters we received from the application via the library and
  382. * make them into a real SaEvtEventFilterArrayT
  383. */
  384. static SaErrorT evtfilt_to_aisfilt(struct req_evt_event_subscribe *req,
  385. SaEvtEventFilterArrayT **evtfilters)
  386. {
  387. SaEvtEventFilterArrayT *filta =
  388. (SaEvtEventFilterArrayT *)req->ics_filter_data;
  389. SaEvtEventFilterArrayT *filters;
  390. SaEvtEventFilterT *filt = (void *)filta + sizeof(SaEvtEventFilterArrayT);
  391. SaUint8T *str = (void *)filta + sizeof(SaEvtEventFilterArrayT) +
  392. (sizeof(SaEvtEventFilterT) * filta->filtersNumber);
  393. int i;
  394. int j;
  395. filters = malloc(sizeof(SaEvtEventFilterArrayT));
  396. if (!filters) {
  397. return SA_ERR_NO_MEMORY;
  398. }
  399. filters->filtersNumber = filta->filtersNumber;
  400. filters->filters = malloc(sizeof(SaEvtEventFilterT) *
  401. filta->filtersNumber);
  402. if (!filters->filters) {
  403. free(filters);
  404. return SA_ERR_NO_MEMORY;
  405. }
  406. for (i = 0; i < filters->filtersNumber; i++) {
  407. filters->filters[i].filter.pattern =
  408. malloc(filt[i].filter.patternSize);
  409. if (!filters->filters[i].filter.pattern) {
  410. for (j = 0; j < i; j++) {
  411. free(filters->filters[j].filter.pattern);
  412. }
  413. free(filters->filters);
  414. free(filters);
  415. return SA_ERR_NO_MEMORY;
  416. }
  417. filters->filters[i].filter.patternSize =
  418. filt[i].filter.patternSize;
  419. memcpy(filters->filters[i].filter.pattern,
  420. str, filters->filters[i].filter.patternSize);
  421. filters->filters[i].filterType = filt[i].filterType;
  422. str += filters->filters[i].filter.patternSize;
  423. }
  424. *evtfilters = filters;
  425. return SA_OK;
  426. }
  427. /*
  428. * Free up filter data
  429. */
  430. static void free_filters(SaEvtEventFilterArrayT *fp)
  431. {
  432. int i;
  433. for (i = 0; i < fp->filtersNumber; i++) {
  434. free(fp->filters[i].filter.pattern);
  435. }
  436. free(fp->filters);
  437. free(fp);
  438. }
  439. /*
  440. * Look up a channel in the global channel list
  441. */
  442. static struct event_svr_channel_instance *
  443. find_channel(SaNameT *chan_name)
  444. {
  445. struct list_head *l;
  446. struct event_svr_channel_instance *eci;
  447. for (l = esc_head.next; l != &esc_head; l = l->next) {
  448. eci = list_entry(l, struct event_svr_channel_instance, esc_entry);
  449. if (!name_match(chan_name, &eci->esc_channel_name)) {
  450. continue;
  451. }
  452. return eci;
  453. }
  454. return 0;
  455. }
  456. /*
  457. * Create and initialize a channel instance structure
  458. */
  459. static struct event_svr_channel_instance *create_channel(SaNameT *cn)
  460. {
  461. struct event_svr_channel_instance *eci;
  462. eci = (struct event_svr_channel_instance *) malloc(sizeof(*eci));
  463. if (!eci) {
  464. return (eci);
  465. }
  466. memset(eci, 0, sizeof(*eci));
  467. list_init(&eci->esc_entry);
  468. list_init(&eci->esc_open_chans);
  469. eci->esc_oc_size = total_members;
  470. eci->esc_node_opens = malloc(sizeof(struct open_count) * total_members);
  471. if (!eci->esc_node_opens) {
  472. free(eci);
  473. return 0;
  474. }
  475. memset(eci->esc_node_opens, 0, sizeof(struct open_count) * total_members);
  476. eci->esc_channel_name = *cn;
  477. eci->esc_channel_name.value[eci->esc_channel_name.length] = '\0';
  478. list_add(&eci->esc_entry, &esc_head);
  479. return eci;
  480. }
  481. /*
  482. * Make sure that the list of nodes is large enough to hold the whole
  483. * membership
  484. */
  485. static int check_open_size(struct event_svr_channel_instance *eci)
  486. {
  487. if (total_members > eci->esc_oc_size) {
  488. eci->esc_node_opens = realloc(eci->esc_node_opens,
  489. sizeof(struct open_count) * total_members);
  490. if (!eci->esc_node_opens) {
  491. log_printf(LOG_LEVEL_DEBUG, "(EVT) Error realloc of node list\n");
  492. return -1;
  493. }
  494. memset(&eci->esc_node_opens[eci->esc_oc_size], 0,
  495. total_members - eci->esc_oc_size);
  496. eci->esc_oc_size = total_members;
  497. }
  498. return 0;
  499. }
  500. /*
  501. * Find the specified node ID in the node list of the channel.
  502. */
  503. static struct open_count* find_open_count(
  504. struct event_svr_channel_instance *eci,
  505. SaClmNodeIdT node_id)
  506. {
  507. int i;
  508. for (i = 0; i < eci->esc_oc_size; i++) {
  509. if (eci->esc_node_opens[i].oc_node_id == 0) {
  510. eci->esc_node_opens[i].oc_node_id = node_id;
  511. }
  512. if (eci->esc_node_opens[i].oc_node_id == node_id) {
  513. return &eci->esc_node_opens[i];
  514. }
  515. }
  516. log_printf(LOG_LEVEL_DEBUG,
  517. "(EVT) find_open_count: node id 0x%x not found\n",
  518. node_id);
  519. return 0;
  520. }
  521. /*
  522. * Replace the current open count for a node with the specified value.
  523. */
  524. static int set_open_count(struct event_svr_channel_instance *eci,
  525. SaClmNodeIdT node_id, uint32_t open_count)
  526. {
  527. struct open_count *oc;
  528. int i;
  529. if ((i = check_open_size(eci)) != 0) {
  530. return i;
  531. }
  532. oc = find_open_count(eci, node_id);
  533. if (oc) {
  534. eci->esc_total_opens -=
  535. (eci->esc_node_opens[i].oc_open_count + open_count);
  536. oc->oc_open_count = open_count;
  537. return 0;
  538. }
  539. return -1;
  540. }
  541. /*
  542. * Increment the open count for the specified node.
  543. */
  544. static int inc_open_count(struct event_svr_channel_instance *eci,
  545. SaClmNodeIdT node_id)
  546. {
  547. struct open_count *oc;
  548. int i;
  549. if ((i = check_open_size(eci)) != 0) {
  550. return i;
  551. }
  552. oc = find_open_count(eci, node_id);
  553. if (oc) {
  554. eci->esc_total_opens++;
  555. oc->oc_open_count++;
  556. return 0;
  557. }
  558. return -1;
  559. }
  560. /*
  561. * Decrement the open count for the specified node in the
  562. * specified channel.
  563. */
  564. static int dec_open_count(struct event_svr_channel_instance *eci,
  565. SaClmNodeIdT node_id)
  566. {
  567. struct open_count *oc;
  568. int i;
  569. if ((i = check_open_size(eci)) != 0) {
  570. return i;
  571. }
  572. oc = find_open_count(eci, node_id);
  573. if (oc) {
  574. eci->esc_total_opens--;
  575. oc->oc_open_count--;
  576. return 0;
  577. }
  578. return -1;
  579. }
  580. /*
  581. * Remove a channel and free its memory if it's not in use anymore.
  582. */
  583. static void delete_channel(struct event_svr_channel_instance *eci)
  584. {
  585. log_printf(LOG_LEVEL_DEBUG,
  586. "(EVT) Called Delete channel %s t %d, l %d, r %d\n",
  587. eci->esc_channel_name.value,
  588. eci->esc_total_opens, eci->esc_local_opens,
  589. eci->esc_retained_count);
  590. if ((eci->esc_retained_count == 0) && (eci->esc_total_opens == 0)) {
  591. log_printf(LOG_LEVEL_DEBUG, "(EVT) Delete channel %s\n",
  592. eci->esc_channel_name.value);
  593. if (!list_empty(&eci->esc_open_chans)) {
  594. log_printf(LOG_LEVEL_NOTICE,
  595. "(EVT) Last channel close request for %s (still open)\n",
  596. eci->esc_channel_name.value);
  597. return;
  598. }
  599. list_del(&eci->esc_entry);
  600. if (eci->esc_node_opens) {
  601. free(eci->esc_node_opens);
  602. }
  603. free(eci);
  604. }
  605. }
  606. /*
  607. * Remove the specified node from the node list in this channel.
  608. */
  609. static int remove_open_count(
  610. struct event_svr_channel_instance *eci,
  611. SaClmNodeIdT node_id)
  612. {
  613. int i;
  614. int j;
  615. /*
  616. * Find the node, remove it and re-pack the array.
  617. */
  618. for (i = 0; i < eci->esc_oc_size; i++) {
  619. if (eci->esc_node_opens[i].oc_node_id == 0) {
  620. eci->esc_node_opens[i].oc_node_id = node_id;
  621. }
  622. if (eci->esc_node_opens[i].oc_node_id == node_id) {
  623. eci->esc_total_opens -= eci->esc_node_opens[i].oc_open_count;
  624. for (j = i+1; j < eci->esc_oc_size; j++, i++) {
  625. eci->esc_node_opens[i].oc_node_id =
  626. eci->esc_node_opens[j].oc_node_id;
  627. eci->esc_node_opens[i].oc_open_count =
  628. eci->esc_node_opens[j].oc_open_count;
  629. }
  630. eci->esc_node_opens[eci->esc_oc_size-1].oc_node_id = 0;
  631. eci->esc_node_opens[eci->esc_oc_size-1].oc_open_count = 0;
  632. }
  633. /*
  634. * Remove the channel if it's not being used anymore
  635. */
  636. delete_channel(eci);
  637. return 0;
  638. }
  639. return -1;
  640. }
  641. /*
  642. * Send a request to open a channel to the rest of the cluster.
  643. */
  644. static SaErrorT evt_open_channel(SaNameT *cn, SaUint8T flgs)
  645. {
  646. struct req_evt_chan_command cpkt;
  647. struct event_svr_channel_instance *eci;
  648. struct iovec chn_iovec;
  649. int res;
  650. SaErrorT ret;
  651. ret = SA_OK;
  652. eci = find_channel(cn);
  653. /*
  654. * If the create flag set, and it doesn't exist, we can make the channel.
  655. * Otherwise, it's an error since we're notified of channels being created
  656. * and opened.
  657. */
  658. if (!eci && !(flgs & SA_EVT_CHANNEL_CREATE)) {
  659. ret = SA_ERR_NOT_EXIST;
  660. goto chan_open_end;
  661. }
  662. /*
  663. * create the channel packet to send. Tell the the cluster
  664. * to create the channel.
  665. */
  666. memset(&cpkt, 0, sizeof(cpkt));
  667. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  668. cpkt.chc_head.size = sizeof(cpkt);
  669. cpkt.chc_op = EVT_OPEN_CHAN_OP;
  670. cpkt.u.chc_chan = *cn;
  671. chn_iovec.iov_base = &cpkt;
  672. chn_iovec.iov_len = cpkt.chc_head.size;
  673. res = gmi_mcast (&aisexec_groupname, &chn_iovec, 1, GMI_PRIO_MED);
  674. if (res != 0) {
  675. ret = SA_ERR_SYSTEM;
  676. }
  677. chan_open_end:
  678. return ret;
  679. }
  680. /*
  681. * Send a request to close a channel with the rest of the cluster.
  682. */
  683. static SaErrorT evt_close_channel(SaNameT *cn)
  684. {
  685. struct req_evt_chan_command cpkt;
  686. struct iovec chn_iovec;
  687. int res;
  688. SaErrorT ret;
  689. ret = SA_OK;
  690. /*
  691. * create the channel packet to send. Tell the the cluster
  692. * to close the channel.
  693. */
  694. memset(&cpkt, 0, sizeof(cpkt));
  695. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  696. cpkt.chc_head.size = sizeof(cpkt);
  697. cpkt.chc_op = EVT_CLOSE_CHAN_OP;
  698. cpkt.u.chc_chan = *cn;
  699. chn_iovec.iov_base = &cpkt;
  700. chn_iovec.iov_len = cpkt.chc_head.size;
  701. res = gmi_mcast (&aisexec_groupname, &chn_iovec, 1, GMI_PRIO_MED);
  702. if (res != 0) {
  703. ret = SA_ERR_SYSTEM;
  704. }
  705. return ret;
  706. }
  707. /*
  708. * Node data access functions. Used to keep track of event IDs
  709. * delivery of messages.
  710. *
  711. * add_node: Add a new member node to our list.
  712. * remove_node: Remove a node that left membership.
  713. * find_node: Given the node ID return a pointer to node information.
  714. *
  715. */
  716. #define NODE_HASH_SIZE 256
  717. static struct member_node_data *nl[NODE_HASH_SIZE] = {0};
  718. inline int
  719. hash_sock_addr(struct in_addr addr)
  720. {
  721. return addr.s_addr & (NODE_HASH_SIZE - 1);
  722. }
  723. static struct member_node_data **lookup_node(struct in_addr addr)
  724. {
  725. int index = hash_sock_addr(addr);
  726. struct member_node_data **nlp;
  727. nlp = &nl[index];
  728. for (nlp = &nl[index]; *nlp; nlp = &((*nlp)->mn_next)) {
  729. if ((*nlp)->mn_node_addr.s_addr == addr.s_addr) {
  730. break;
  731. }
  732. }
  733. return nlp;
  734. }
  735. static struct member_node_data *
  736. evt_find_node(struct in_addr addr)
  737. {
  738. struct member_node_data **nlp;
  739. nlp = lookup_node(addr);
  740. if (!nlp) {
  741. log_printf(LOG_LEVEL_DEBUG, "(EVT) find_node: Got NULL nlp?\n");
  742. return 0;
  743. }
  744. return *nlp;
  745. }
  746. static SaErrorT
  747. evt_add_node(struct in_addr addr, SaClmClusterNodeT *cn)
  748. {
  749. struct member_node_data **nlp;
  750. struct member_node_data *nl;
  751. SaErrorT err = SA_ERR_EXIST;
  752. nlp = lookup_node(addr);
  753. if (!nlp) {
  754. log_printf(LOG_LEVEL_DEBUG, "(EVT) add_node: Got NULL nlp?\n");
  755. goto an_out;
  756. }
  757. if (*nlp) {
  758. goto an_out;
  759. }
  760. *nlp = malloc(sizeof(struct member_node_data));
  761. if (!nlp) {
  762. return SA_ERR_NO_MEMORY;
  763. }
  764. nl = *nlp;
  765. if (nl) {
  766. memset(nl, 0, sizeof(*nl));
  767. err = SA_OK;
  768. nl->mn_node_addr = addr;
  769. nl->mn_started = 1;
  770. }
  771. list_init(&nl->mn_entry);
  772. list_add(&nl->mn_entry, &mnd);
  773. nl->mn_node_info = *cn;
  774. an_out:
  775. return err;
  776. }
  777. #ifdef REMOVE_NODE
  778. static SaErrorT
  779. evt_remove_node(struct in_addr addr)
  780. {
  781. struct member_node_data **nlp;
  782. struct member_node_data *nl;
  783. SaErrorT err = SA_ERR_NOT_EXIST;
  784. nlp = lookup_node(addr);
  785. if (!nlp) {
  786. log_printf(LOG_LEVEL_DEBUG, "(EVT) remove_node: Got NULL nlp?\n");
  787. goto an_out;
  788. }
  789. if (!(*nlp)) {
  790. goto an_out;
  791. }
  792. nl = *nlp;
  793. list_del(&nl->mn_entry);
  794. *nlp = nl->mn_next;
  795. free(*nlp);
  796. err = SA_OK;
  797. an_out:
  798. return err;
  799. }
  800. #endif /* REMOVE_NODE */
  801. /*
  802. * Find the oldest node in the membership. This is the one we choose to
  803. * perform some cluster-wide functions like distributing retained events.
  804. */
  805. static struct member_node_data* oldest_node()
  806. {
  807. struct list_head *l;
  808. struct member_node_data *mn = 0;
  809. struct member_node_data *oldest = 0;
  810. for (l = mnd.next; l != &mnd; l = l->next) {
  811. mn = list_entry(l, struct member_node_data, mn_entry);
  812. if (mn->mn_started == 0) {
  813. continue;
  814. }
  815. if ((oldest == NULL) ||
  816. (mn->mn_node_info.bootTimestamp <
  817. oldest->mn_node_info.bootTimestamp )) {
  818. oldest = mn;
  819. } else if (mn->mn_node_info.bootTimestamp ==
  820. oldest->mn_node_info.bootTimestamp) {
  821. if (mn->mn_node_info.nodeId < oldest->mn_node_info.nodeId) {
  822. oldest = mn;
  823. }
  824. }
  825. }
  826. return oldest;
  827. }
  828. /*
  829. * Token callback routine. Send as many mcasts as possible to distribute
  830. * retained events on a config change.
  831. */
  832. static int send_next_retained(void *data)
  833. {
  834. struct req_evt_chan_command cpkt;
  835. struct iovec chn_iovec;
  836. struct event_data *evt;
  837. int res;
  838. if (in_cfg_change && recovery_node) {
  839. /*
  840. * Process messages. When we're done, send the done message
  841. * to the nodes.
  842. */
  843. for (;next_retained != &retained_list;
  844. next_retained = next_retained->next) {
  845. log_printf(LOG_LEVEL_DEBUG, "(EVT) Sending next retained event\n");
  846. evt = list_entry(next_retained, struct event_data, ed_retained);
  847. evt->ed_event.led_head.id = MESSAGE_REQ_EXEC_EVT_RECOVERY_EVENTDATA;
  848. chn_iovec.iov_base = &evt->ed_event;
  849. chn_iovec.iov_len = evt->ed_event.led_head.size;
  850. res = gmi_mcast(&aisexec_groupname, &chn_iovec, 1,
  851. GMI_PRIO_RECOVERY);
  852. if (res != 0) {
  853. /*
  854. * Try again later.
  855. */
  856. return -1;
  857. }
  858. }
  859. log_printf(LOG_LEVEL_DEBUG, "(EVT) DONE Sending retained events\n");
  860. memset(&cpkt, 0, sizeof(cpkt));
  861. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  862. cpkt.chc_head.size = sizeof(cpkt);
  863. cpkt.chc_op = EVT_CONF_DONE;
  864. chn_iovec.iov_base = &cpkt;
  865. chn_iovec.iov_len = cpkt.chc_head.size;
  866. res = gmi_mcast (&aisexec_groupname, &chn_iovec, 1,
  867. GMI_PRIO_RECOVERY);
  868. }
  869. tok_call_handle = 0;
  870. return 0;
  871. }
  872. /*
  873. * Send our retained events. If we've been chosen as the recovery node, kick
  874. * kick off the process of sending retained events.
  875. */
  876. static void send_retained()
  877. {
  878. struct req_evt_chan_command cpkt;
  879. struct iovec chn_iovec;
  880. int res;
  881. if (list_empty(&retained_list) || !any_joined) {
  882. memset(&cpkt, 0, sizeof(cpkt));
  883. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  884. cpkt.chc_head.size = sizeof(cpkt);
  885. cpkt.chc_op = EVT_CONF_DONE;
  886. chn_iovec.iov_base = &cpkt;
  887. chn_iovec.iov_len = cpkt.chc_head.size;
  888. log_printf(LOG_LEVEL_DEBUG, "(EVT) No messages to send\n");
  889. res = gmi_mcast (&aisexec_groupname, &chn_iovec, 1,
  890. GMI_PRIO_RECOVERY);
  891. } else {
  892. log_printf(LOG_LEVEL_DEBUG,
  893. "(EVT) Start sending retained messages\n");
  894. recovery_node = 1;
  895. next_retained = retained_list.next;
  896. res = gmi_token_callback_create(&tok_call_handle, send_next_retained,
  897. NULL);
  898. }
  899. if (res != 0) {
  900. log_printf(LOG_LEVEL_ERROR, "(EVT) ERROR sending evt recovery data\n");
  901. }
  902. }
  903. /*
  904. * Token callback routine. Send as many mcasts as possible to distribute
  905. * open counts on a config change.
  906. */
  907. static int send_next_open_count(void *data)
  908. {
  909. struct req_evt_chan_command cpkt;
  910. struct iovec chn_iovec;
  911. struct event_svr_channel_instance *eci;
  912. int res;
  913. if (in_cfg_change) {
  914. /*
  915. * Process messages. When we're done, send the done message
  916. * to the nodes.
  917. */
  918. memset(&cpkt, 0, sizeof(cpkt));
  919. for (;next_chan != &esc_head;
  920. next_chan = next_chan->next) {
  921. log_printf(LOG_LEVEL_DEBUG, "(EVT) Sending next open count\n");
  922. eci = list_entry(next_chan, struct event_svr_channel_instance,
  923. esc_entry);
  924. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  925. cpkt.chc_head.size = sizeof(cpkt);
  926. cpkt.chc_op = EVT_OPEN_COUNT;
  927. cpkt.u.chc_set_opens.chc_chan_name = eci->esc_channel_name;
  928. cpkt.u.chc_set_opens.chc_open_count = eci->esc_local_opens;
  929. chn_iovec.iov_base = &cpkt;
  930. chn_iovec.iov_len = cpkt.chc_head.size;
  931. res = gmi_mcast(&aisexec_groupname, &chn_iovec, 1,
  932. GMI_PRIO_RECOVERY);
  933. if (res != 0) {
  934. /*
  935. * Try again later.
  936. */
  937. return -1;
  938. }
  939. }
  940. log_printf(LOG_LEVEL_DEBUG, "(EVT) DONE Sending open counts\n");
  941. memset(&cpkt, 0, sizeof(cpkt));
  942. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  943. cpkt.chc_head.size = sizeof(cpkt);
  944. cpkt.chc_op = EVT_OPEN_COUNT_DONE;
  945. chn_iovec.iov_base = &cpkt;
  946. chn_iovec.iov_len = cpkt.chc_head.size;
  947. res = gmi_mcast (&aisexec_groupname, &chn_iovec, 1,
  948. GMI_PRIO_RECOVERY);
  949. }
  950. tok_call_handle = 0;
  951. return 0;
  952. }
  953. /*
  954. * kick off the process of sending open channel counts during recovery.
  955. * Every node does this.
  956. */
  957. static void send_open_count()
  958. {
  959. struct req_evt_chan_command cpkt;
  960. struct iovec chn_iovec;
  961. int res;
  962. if (list_empty(&esc_head)) {
  963. memset(&cpkt, 0, sizeof(cpkt));
  964. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  965. cpkt.chc_head.size = sizeof(cpkt);
  966. cpkt.chc_op = EVT_OPEN_COUNT_DONE;
  967. chn_iovec.iov_base = &cpkt;
  968. chn_iovec.iov_len = cpkt.chc_head.size;
  969. log_printf(LOG_LEVEL_DEBUG, "(EVT) No channels to send\n");
  970. res = gmi_mcast (&aisexec_groupname, &chn_iovec, 1,
  971. GMI_PRIO_RECOVERY);
  972. } else {
  973. log_printf(LOG_LEVEL_DEBUG,
  974. "(EVT) Start sending open channel count\n");
  975. next_chan = esc_head.next;
  976. res = gmi_token_callback_create(&tok_call_handle, send_next_open_count,
  977. NULL);
  978. }
  979. if (res != 0) {
  980. log_printf(LOG_LEVEL_ERROR, "(EVT) ERROR sending evt recovery data\n");
  981. }
  982. }
  983. /*
  984. * keep track of the last event ID from a node.
  985. * If we get an event ID less than our last, we've already
  986. * seen it. It's probably a retained event being sent to
  987. * a new node.
  988. */
  989. static int check_last_event(struct lib_event_data *evtpkt,
  990. struct in_addr addr)
  991. {
  992. struct member_node_data *nd;
  993. SaClmClusterNodeT *cn;
  994. nd = evt_find_node(addr);
  995. if (!nd) {
  996. log_printf(LOG_LEVEL_DEBUG,
  997. "(EVT) Node ID 0x%x not found for event %llx\n",
  998. evtpkt->led_publisher_node_id, evtpkt->led_event_id);
  999. cn = clm_get_by_nodeid(addr);
  1000. if (!cn) {
  1001. log_printf(LOG_LEVEL_DEBUG,
  1002. "(EVT) Cluster Node 0x%x not found for event %llx\n",
  1003. evtpkt->led_publisher_node_id, evtpkt->led_event_id);
  1004. } else {
  1005. evt_add_node(addr, cn);
  1006. nd = evt_find_node(addr);
  1007. }
  1008. }
  1009. if (!nd) {
  1010. return 0;
  1011. }
  1012. if ((nd->mn_last_evt_id < evtpkt->led_event_id)) {
  1013. nd->mn_last_evt_id = evtpkt->led_event_id;
  1014. return 0;
  1015. }
  1016. return 1;
  1017. }
  1018. /*
  1019. * Send a message to the app to wake it up if it is polling
  1020. */
  1021. static int message_handler_req_lib_activatepoll(struct conn_info *conn_info,
  1022. void *message)
  1023. {
  1024. struct res_lib_activatepoll res;
  1025. res.header.error = SA_OK;
  1026. res.header.size = sizeof (struct res_lib_activatepoll);
  1027. res.header.id = MESSAGE_RES_LIB_ACTIVATEPOLL;
  1028. libais_send_response(conn_info, &res, sizeof(res));
  1029. return (0);
  1030. }
  1031. /*
  1032. * event id generating code. We use the node ID for this node for the
  1033. * upper 32 bits of the event ID to make sure that we can generate a cluster
  1034. * wide unique event ID for a given event.
  1035. */
  1036. SaErrorT set_event_id(SaClmNodeIdT node_id)
  1037. {
  1038. SaErrorT err = SA_OK;
  1039. if (base_id) {
  1040. err = SA_ERR_EXIST;
  1041. }
  1042. base_id = (SaEvtEventIdT)node_id << 32;
  1043. return err;
  1044. }
  1045. static SaErrorT get_event_id(uint64_t *event_id)
  1046. {
  1047. *event_id = base_id++;
  1048. return SA_OK;
  1049. }
  1050. #ifdef EVT_ALLOC_CHECK
  1051. static uint32_t evt_alloc = 0;
  1052. static uint32_t evt_free = 0;
  1053. #endif
  1054. /*
  1055. * Free up an event structure if it isn't being used anymore.
  1056. */
  1057. static void
  1058. free_event_data(struct event_data *edp)
  1059. {
  1060. if (--edp->ed_ref_count) {
  1061. return;
  1062. }
  1063. log_printf(LOG_LEVEL_DEBUG, "(EVT) Freeing event ID: 0x%llx\n",
  1064. edp->ed_event.led_event_id);
  1065. if (edp->ed_delivered) {
  1066. free(edp->ed_delivered);
  1067. }
  1068. #ifdef EVT_ALLOC_CHECK
  1069. evt_free++;
  1070. if ((evt_free % 1000) == 0) {
  1071. log_printf(LOG_LEVEL_NOTICE, "(EVT) evt alloc: %u, *evt free: %u\n",
  1072. evt_alloc, evt_free);
  1073. }
  1074. #endif
  1075. free(edp);
  1076. }
  1077. /*
  1078. * Timer handler to delete expired events.
  1079. *
  1080. */
  1081. static void
  1082. event_retention_timeout(void *data)
  1083. {
  1084. struct event_data *edp = data;
  1085. log_printf(LOG_LEVEL_DEBUG, "(EVT) Event ID %llx expired\n",
  1086. edp->ed_event.led_event_id);
  1087. /*
  1088. * adjust next_retained if we're in recovery and
  1089. * were in charge of sending retained events.
  1090. */
  1091. if (in_cfg_change && recovery_node) {
  1092. if (next_retained == &edp->ed_retained) {
  1093. next_retained = edp->ed_retained.next;
  1094. }
  1095. }
  1096. list_del(&edp->ed_retained);
  1097. list_init(&edp->ed_retained);
  1098. /*
  1099. * Check to see it the channel isn't in use anymore.
  1100. */
  1101. edp->ed_my_chan->esc_retained_count--;
  1102. delete_channel(edp->ed_my_chan);
  1103. free_event_data(edp);
  1104. }
  1105. /*
  1106. * clear a particular event's retention time.
  1107. * This will free the event as long as it isn't being
  1108. * currently used.
  1109. *
  1110. */
  1111. static void
  1112. clear_retention_time(SaEvtEventIdT event_id)
  1113. {
  1114. struct event_data *edp;
  1115. struct list_head *l, *nxt;
  1116. int ret;
  1117. log_printf(LOG_LEVEL_DEBUG, "(EVT) Search for Event ID %llx\n", event_id);
  1118. for(l = retained_list.next; l != &retained_list; l = nxt) {
  1119. nxt = l->next;
  1120. edp = list_entry(l, struct event_data, ed_retained);
  1121. if (edp->ed_event.led_event_id != event_id) {
  1122. continue;
  1123. }
  1124. log_printf(LOG_LEVEL_DEBUG,
  1125. "(EVT) Clear retention time for Event ID %llx\n",
  1126. edp->ed_event.led_event_id);
  1127. ret = poll_timer_delete(aisexec_poll_handle, edp->ed_timer_handle);
  1128. if (ret != 0 ) {
  1129. log_printf(LOG_LEVEL_ERROR, "(EVT) Error expiring event ID %llx\n",
  1130. edp->ed_event.led_event_id);
  1131. return;
  1132. }
  1133. edp->ed_event.led_retention_time = 0;
  1134. list_del(&edp->ed_retained);
  1135. list_init(&edp->ed_retained);
  1136. /*
  1137. * Check to see if the channel isn't in use anymore.
  1138. */
  1139. edp->ed_my_chan->esc_retained_count--;
  1140. delete_channel(edp->ed_my_chan);
  1141. free_event_data(edp);
  1142. break;
  1143. }
  1144. }
  1145. /*
  1146. * Remove specified channel from event delivery list
  1147. */
  1148. static void
  1149. remove_delivered_channel(struct event_svr_channel_open *eco)
  1150. {
  1151. int i;
  1152. struct list_head *l;
  1153. struct event_data *edp;
  1154. for (l = retained_list.next; l != &retained_list; l = l->next) {
  1155. edp = list_entry(l, struct event_data, ed_retained);
  1156. for (i = 0; i < edp->ed_delivered_next; i++) {
  1157. if (edp->ed_delivered[i] == eco) {
  1158. edp->ed_delivered_next--;
  1159. if (edp->ed_delivered_next == i) {
  1160. break;
  1161. }
  1162. memmove(&edp->ed_delivered[i],
  1163. &edp->ed_delivered[i+1],
  1164. &edp->ed_delivered[edp->ed_delivered_next] -
  1165. &edp->ed_delivered[i]);
  1166. break;
  1167. }
  1168. }
  1169. }
  1170. return;
  1171. }
  1172. /*
  1173. * If there is a retention time, add this open channel to the event so
  1174. * we can check if we've already delivered this message later if a new
  1175. * subscription matches.
  1176. */
  1177. #define DELIVER_SIZE 8
  1178. static void
  1179. evt_delivered(struct event_data *evt, struct event_svr_channel_open *eco)
  1180. {
  1181. if (!evt->ed_event.led_retention_time) {
  1182. return;
  1183. }
  1184. log_printf(LOG_LEVEL_DEBUG, "(EVT) delivered ID %llx to eco %p\n",
  1185. evt->ed_event.led_event_id, eco);
  1186. if (evt->ed_delivered_count == evt->ed_delivered_next) {
  1187. evt->ed_delivered = realloc(evt->ed_delivered,
  1188. DELIVER_SIZE * sizeof(struct event_svr_channel_open *));
  1189. memset(evt->ed_delivered + evt->ed_delivered_next, 0,
  1190. DELIVER_SIZE * sizeof(struct event_svr_channel_open *));
  1191. evt->ed_delivered_next = evt->ed_delivered_count;
  1192. evt->ed_delivered_count += DELIVER_SIZE;
  1193. }
  1194. evt->ed_delivered[evt->ed_delivered_next++] = eco;
  1195. }
  1196. /*
  1197. * Check to see if an event has already been delivered to this open channel
  1198. */
  1199. static int
  1200. evt_already_delivered(struct event_data *evt,
  1201. struct event_svr_channel_open *eco)
  1202. {
  1203. int i;
  1204. if (!evt->ed_event.led_retention_time) {
  1205. return 0;
  1206. }
  1207. log_printf(LOG_LEVEL_DEBUG, "(EVT) Deliver count: %d deliver_next %d\n",
  1208. evt->ed_delivered_count, evt->ed_delivered_next);
  1209. for (i = 0; i < evt->ed_delivered_next; i++) {
  1210. log_printf(LOG_LEVEL_DEBUG, "(EVT) Checking ID %llx delivered %p eco %p\n",
  1211. evt->ed_event.led_event_id, evt->ed_delivered[i], eco);
  1212. if (evt->ed_delivered[i] == eco) {
  1213. return 1;
  1214. }
  1215. }
  1216. return 0;
  1217. }
  1218. /*
  1219. * Compare a filter to a given pattern.
  1220. * return SA_OK if the pattern matches a filter
  1221. */
  1222. static SaErrorT
  1223. filter_match(SaEvtEventPatternT *ep, SaEvtEventFilterT *ef)
  1224. {
  1225. int ret;
  1226. ret = SA_ERR_FAILED_OPERATION;
  1227. switch (ef->filterType) {
  1228. case SA_EVT_PREFIX_FILTER:
  1229. if (ef->filter.patternSize > ep->patternSize) {
  1230. break;
  1231. }
  1232. if (strncmp(ef->filter.pattern, ep->pattern,
  1233. ef->filter.patternSize) == 0) {
  1234. ret = SA_OK;
  1235. }
  1236. break;
  1237. case SA_EVT_SUFFIX_FILTER:
  1238. if (ef->filter.patternSize > ep->patternSize) {
  1239. break;
  1240. }
  1241. if (strncmp(ef->filter.pattern,
  1242. &ep->pattern[ep->patternSize - ef->filter.patternSize],
  1243. ef->filter.patternSize) == 0) {
  1244. ret = SA_OK;
  1245. }
  1246. break;
  1247. case SA_EVT_EXACT_FILTER:
  1248. if (ef->filter.patternSize != ep->patternSize) {
  1249. break;
  1250. }
  1251. if (strncmp(ef->filter.pattern, ep->pattern,
  1252. ef->filter.patternSize) == 0) {
  1253. ret = SA_OK;
  1254. }
  1255. break;
  1256. case SA_EVT_PASS_ALL_FILTER:
  1257. ret = SA_OK;
  1258. break;
  1259. default:
  1260. break;
  1261. }
  1262. return ret;
  1263. }
  1264. /*
  1265. * compare the event's patterns with the subscription's filter rules.
  1266. * SA_OK is returned if the event matches the filter rules.
  1267. */
  1268. static SaErrorT
  1269. event_match(struct event_data *evt,
  1270. struct event_svr_channel_subscr *ecs)
  1271. {
  1272. SaEvtEventFilterT *ef;
  1273. SaEvtEventPatternT *ep;
  1274. uint32_t filt_count;
  1275. SaErrorT ret = SA_OK;
  1276. int i;
  1277. ep = (SaEvtEventPatternT *)(&evt->ed_event.led_body[0]);
  1278. ef = ecs->ecs_filters->filters;
  1279. filt_count = min(ecs->ecs_filters->filtersNumber,
  1280. evt->ed_event.led_patterns_number);
  1281. for (i = 0; i < filt_count; i++) {
  1282. ret = filter_match(ep, ef);
  1283. if (ret != SA_OK) {
  1284. break;
  1285. }
  1286. ep++;
  1287. ef++;
  1288. }
  1289. return ret;
  1290. }
  1291. /*
  1292. * Scan undelivered pending events and either remove them if no subscription
  1293. * filters match anymore or re-assign them to another matching subscription
  1294. */
  1295. static void
  1296. filter_undelivered_events(struct event_svr_channel_open *op_chan)
  1297. {
  1298. struct event_svr_channel_open *eco;
  1299. struct event_svr_channel_instance *eci;
  1300. struct event_svr_channel_subscr *ecs;
  1301. struct chan_event_list *cel;
  1302. struct libevt_ci *esip = &op_chan->eco_conn_info->ais_ci.u.libevt_ci;
  1303. struct list_head *l, *nxt;
  1304. struct list_head *l1, *l2;
  1305. int i;
  1306. eci = op_chan->eco_channel;
  1307. /*
  1308. * Scan each of the priority queues for messages
  1309. */
  1310. for (i = SA_EVT_HIGHEST_PRIORITY; i <= SA_EVT_LOWEST_PRIORITY; i++) {
  1311. /*
  1312. * examine each message queued for delivery
  1313. */
  1314. for (l = esip->esi_events[i].next; l != &esip->esi_events[i]; l = nxt) {
  1315. nxt = l->next;
  1316. cel = list_entry(l, struct chan_event_list, cel_entry);
  1317. /*
  1318. * Check open channels
  1319. */
  1320. for (l1 = eci->esc_open_chans.next;
  1321. l1 != &eci->esc_open_chans; l1 = l1->next) {
  1322. eco = list_entry(l1, struct event_svr_channel_open, eco_entry);
  1323. /*
  1324. * See if this channel open instance belongs
  1325. * to this evtinitialize instance
  1326. */
  1327. if (eco->eco_conn_info != op_chan->eco_conn_info) {
  1328. continue;
  1329. }
  1330. /*
  1331. * See if enabled to receive
  1332. */
  1333. if (!(eco->eco_flags & SA_EVT_CHANNEL_SUBSCRIBER)) {
  1334. continue;
  1335. }
  1336. /*
  1337. * Check subscriptions
  1338. */
  1339. for (l2 = eco->eco_subscr.next;
  1340. l2 != &eco->eco_subscr; l2 = l2->next) {
  1341. ecs = list_entry(l2,
  1342. struct event_svr_channel_subscr, ecs_entry);
  1343. if (event_match(cel->cel_event, ecs) == SA_OK) {
  1344. /*
  1345. * Something still matches.
  1346. * We'll assign it to
  1347. * the new subscription.
  1348. */
  1349. cel->cel_sub_id = ecs->ecs_sub_id;
  1350. cel->cel_chan_handle = eco->eco_lib_handle;
  1351. goto next_event;
  1352. }
  1353. }
  1354. }
  1355. /*
  1356. * No subscription filter matches anymore. We
  1357. * can delete this event.
  1358. */
  1359. list_del(&cel->cel_entry);
  1360. list_init(&cel->cel_entry);
  1361. esip->esi_nevents--;
  1362. #ifdef EVT_EVENT_LIST_CHECK
  1363. if (esip->esi_nevents < 0) {
  1364. log_printf(LOG_LEVEL_NOTICE,
  1365. "(EVT) event count went negative\n");
  1366. esip->esi_nevents = 0;
  1367. }
  1368. #endif
  1369. free_event_data(cel->cel_event);
  1370. free(cel);
  1371. next_event:
  1372. continue;
  1373. }
  1374. }
  1375. }
  1376. /*
  1377. * Notify the library of a pending event
  1378. */
  1379. static void __notify_event(struct conn_info *conn_info)
  1380. {
  1381. struct res_evt_event_data res;
  1382. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  1383. log_printf(LOG_LEVEL_DEBUG, "(EVT) DELIVER: notify\n");
  1384. if (esip->esi_nevents != 0) {
  1385. res.evd_head.size = sizeof(res);
  1386. res.evd_head.id = MESSAGE_RES_EVT_AVAILABLE;
  1387. res.evd_head.error = SA_OK;
  1388. libais_send_response(conn_info, &res, sizeof(res));
  1389. }
  1390. }
  1391. inline void notify_event(struct conn_info *conn_info)
  1392. {
  1393. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  1394. /*
  1395. * Give the library a kick if there aren't already
  1396. * events queued for delivery.
  1397. */
  1398. if (esip->esi_nevents++ == 0) {
  1399. __notify_event(conn_info);
  1400. }
  1401. }
  1402. /*
  1403. * sends/queues up an event for a subscribed channel.
  1404. */
  1405. static void
  1406. deliver_event(struct event_data *evt,
  1407. struct event_svr_channel_open *eco,
  1408. struct event_svr_channel_subscr *ecs)
  1409. {
  1410. struct chan_event_list *ep;
  1411. struct libevt_ci *esip = &eco->eco_conn_info->ais_ci.u.libevt_ci;
  1412. SaEvtEventPriorityT evt_prio = evt->ed_event.led_priority;
  1413. struct chan_event_list *cel;
  1414. int do_deliver_event = 0;
  1415. int do_deliver_warning = 0;
  1416. int i;
  1417. if (evt_prio > SA_EVT_LOWEST_PRIORITY) {
  1418. evt_prio = SA_EVT_LOWEST_PRIORITY;
  1419. }
  1420. /*
  1421. * Delivery queue check.
  1422. * - If the queue is blocked, see if we've sent enough messages to
  1423. * unblock it.
  1424. * - If it isn't blocked, see if this message will put us over the top.
  1425. * - If we can't deliver this message, see if we can toss some lower
  1426. * priority message to make room for this one.
  1427. * - If we toss any messages, queue up an event of SA_EVT_LOST_EVENT_PATTERN
  1428. * to let the application know that we dropped some messages.
  1429. */
  1430. if (esip->esi_queue_blocked) {
  1431. if (esip->esi_nevents < MIN_EVT_QUEUE_RESUME) {
  1432. esip->esi_queue_blocked = 0;
  1433. log_printf(LOG_LEVEL_DEBUG, "(EVT) unblock\n");
  1434. }
  1435. }
  1436. if (!esip->esi_queue_blocked &&
  1437. (esip->esi_nevents >= MAX_EVT_DELIVERY_QUEUE)) {
  1438. log_printf(LOG_LEVEL_DEBUG, "(EVT) block\n");
  1439. esip->esi_queue_blocked = 1;
  1440. do_deliver_warning = 1;
  1441. }
  1442. if (esip->esi_queue_blocked) {
  1443. do_deliver_event = 0;
  1444. for (i = SA_EVT_LOWEST_PRIORITY; i > evt_prio; i--) {
  1445. if (!list_empty(&esip->esi_events[i])) {
  1446. /*
  1447. * Get the last item on the list, so we drop the most
  1448. * recent lowest priority event.
  1449. */
  1450. cel = list_entry(esip->esi_events[i].prev,
  1451. struct chan_event_list, cel_entry);
  1452. log_printf(LOG_LEVEL_DEBUG, "(EVT) Drop 0x%0llx\n",
  1453. cel->cel_event->ed_event.led_event_id);
  1454. list_del(&cel->cel_entry);
  1455. free_event_data(cel->cel_event);
  1456. free(cel);
  1457. esip->esi_nevents--;
  1458. do_deliver_event = 1;
  1459. break;
  1460. }
  1461. }
  1462. } else {
  1463. do_deliver_event = 1;
  1464. }
  1465. /*
  1466. * Queue the event for delivery
  1467. */
  1468. if (do_deliver_event) {
  1469. ep = malloc(sizeof(*ep));
  1470. if (!ep) {
  1471. log_printf(LOG_LEVEL_WARNING,
  1472. "(EVT) Memory allocation error, can't deliver event\n");
  1473. return;
  1474. }
  1475. evt->ed_ref_count++;
  1476. ep->cel_chan_handle = eco->eco_lib_handle;
  1477. ep->cel_sub_id = ecs->ecs_sub_id;
  1478. list_init(&ep->cel_entry);
  1479. ep->cel_event = evt;
  1480. list_add_tail(&ep->cel_entry, &esip->esi_events[evt_prio]);
  1481. evt_delivered(evt, eco);
  1482. notify_event(eco->eco_conn_info);
  1483. }
  1484. /*
  1485. * If we dropped an event, queue this so that the application knows
  1486. * what has happened.
  1487. */
  1488. if (do_deliver_warning) {
  1489. struct event_data *ed;
  1490. ed = malloc(dropped_event_size);
  1491. if (!ed) {
  1492. log_printf(LOG_LEVEL_WARNING,
  1493. "(EVT) Memory allocation error, can't deliver event\n");
  1494. return;
  1495. }
  1496. log_printf(LOG_LEVEL_DEBUG, "(EVT) Warn 0x%0llx\n",
  1497. evt->ed_event.led_event_id);
  1498. memcpy(ed, dropped_event, dropped_event_size);
  1499. ed->ed_event.led_publish_time = clust_time_now();
  1500. list_init(&ed->ed_retained);
  1501. ep = malloc(sizeof(*ep));
  1502. if (!ep) {
  1503. log_printf(LOG_LEVEL_WARNING,
  1504. "(EVT) Memory allocation error, can't deliver event\n");
  1505. return;
  1506. }
  1507. ep->cel_chan_handle = eco->eco_lib_handle;
  1508. ep->cel_sub_id = ecs->ecs_sub_id;
  1509. list_init(&ep->cel_entry);
  1510. ep->cel_event = ed;
  1511. list_add_tail(&ep->cel_entry, &esip->esi_events[SA_EVT_HIGHEST_PRIORITY]);
  1512. notify_event(eco->eco_conn_info);
  1513. }
  1514. }
  1515. /*
  1516. * Take an event received from the network and fix it up to be usable.
  1517. * - fix up pointers for pattern list.
  1518. * - fill in some channel info
  1519. */
  1520. static struct event_data *
  1521. make_local_event(struct lib_event_data *p,
  1522. struct event_svr_channel_instance *eci)
  1523. {
  1524. struct event_data *ed;
  1525. SaEvtEventPatternT *eps;
  1526. SaUint8T *str;
  1527. uint32_t ed_size;
  1528. int i;
  1529. ed_size = sizeof(*ed) + p->led_user_data_offset + p->led_user_data_size;
  1530. ed = malloc(ed_size);
  1531. if (!ed) {
  1532. return 0;
  1533. }
  1534. memset(ed, 0, ed_size);
  1535. list_init(&ed->ed_retained);
  1536. ed->ed_my_chan = eci;
  1537. /*
  1538. * Fill in lib_event_data and make the pattern pointers valid
  1539. */
  1540. memcpy(&ed->ed_event, p, sizeof(*p) +
  1541. p->led_user_data_offset + p->led_user_data_size);
  1542. eps = (SaEvtEventPatternT *)ed->ed_event.led_body;
  1543. str = ed->ed_event.led_body +
  1544. (ed->ed_event.led_patterns_number * sizeof(SaEvtEventPatternT));
  1545. for (i = 0; i < ed->ed_event.led_patterns_number; i++) {
  1546. eps->pattern = str;
  1547. str += eps->patternSize;
  1548. eps++;
  1549. }
  1550. #ifdef EVT_ALLOC_CHECK
  1551. evt_alloc++;
  1552. if ((evt_alloc % 1000) == 0) {
  1553. log_printf(LOG_LEVEL_NOTICE, "(EVT) *evt alloc: %u, evt free: %u\n",
  1554. evt_alloc, evt_free);
  1555. }
  1556. #endif
  1557. ed->ed_ref_count++;
  1558. return ed;
  1559. }
  1560. /*
  1561. * Set an event to be retained.
  1562. */
  1563. static void retain_event(struct event_data *evt)
  1564. {
  1565. uint32_t ret;
  1566. int msec_in_future;
  1567. evt->ed_ref_count++;
  1568. evt->ed_my_chan->esc_retained_count++;
  1569. list_add_tail(&evt->ed_retained, &retained_list);
  1570. /*
  1571. * Time in nanoseconds - convert to miliseconds
  1572. */
  1573. msec_in_future = (uint32_t)((evt->ed_event.led_retention_time) / 1000000ULL);
  1574. ret = poll_timer_add(aisexec_poll_handle,
  1575. msec_in_future,
  1576. evt,
  1577. event_retention_timeout,
  1578. &evt->ed_timer_handle);
  1579. if (ret != 0) {
  1580. log_printf(LOG_LEVEL_ERROR,
  1581. "(EVT) retention of event id 0x%llx failed\n",
  1582. evt->ed_event.led_event_id);
  1583. } else {
  1584. log_printf(LOG_LEVEL_DEBUG, "(EVT) Retain event ID 0x%llx\n",
  1585. evt->ed_event.led_event_id);
  1586. }
  1587. }
  1588. /*
  1589. * Scan the subscription list and look for the specified subsctiption ID.
  1590. * Only look for the ID in subscriptions that are associated with the
  1591. * saEvtInitialize associated with the specified open channel.
  1592. */
  1593. static struct event_svr_channel_subscr *find_subscr(
  1594. struct event_svr_channel_open *open_chan, SaEvtSubscriptionIdT sub_id)
  1595. {
  1596. struct event_svr_channel_instance *eci;
  1597. struct event_svr_channel_subscr *ecs;
  1598. struct event_svr_channel_open *eco;
  1599. struct list_head *l, *l1;
  1600. struct conn_info* conn_info = open_chan->eco_conn_info;
  1601. eci = open_chan->eco_channel;
  1602. /*
  1603. * Check for subscription id already in use.
  1604. * Subscriptions are unique within saEvtInitialize (Callback scope).
  1605. */
  1606. for (l = eci->esc_open_chans.next; l != &eci->esc_open_chans; l = l->next) {
  1607. eco = list_entry(l, struct event_svr_channel_open, eco_entry);
  1608. /*
  1609. * Don't bother with open channels associated with another
  1610. * EvtInitialize
  1611. */
  1612. if (eco->eco_conn_info != conn_info) {
  1613. continue;
  1614. }
  1615. for (l1 = eco->eco_subscr.next; l1 != &eco->eco_subscr; l1 = l1->next) {
  1616. ecs = list_entry(l1, struct event_svr_channel_subscr, ecs_entry);
  1617. if (ecs->ecs_sub_id == sub_id) {
  1618. return ecs;
  1619. }
  1620. }
  1621. }
  1622. return 0;
  1623. }
  1624. /*
  1625. * Handler for saEvtInitialize
  1626. */
  1627. static int evt_initialize(struct conn_info *conn_info, void *msg)
  1628. {
  1629. struct res_lib_init res;
  1630. struct libevt_ci *libevt_ci = &conn_info->ais_ci.u.libevt_ci;
  1631. int i;
  1632. res.header.size = sizeof (struct res_lib_init);
  1633. res.header.id = MESSAGE_RES_INIT;
  1634. res.header.error = SA_OK;
  1635. log_printf(LOG_LEVEL_DEBUG, "(EVT) saEvtInitialize request.\n");
  1636. if (!conn_info->authenticated) {
  1637. log_printf(LOG_LEVEL_DEBUG, "(EVT) event service: Not authenticated\n");
  1638. res.header.error = SA_ERR_SECURITY;
  1639. libais_send_response(conn_info, &res, sizeof(res));
  1640. return -1;
  1641. }
  1642. memset(libevt_ci, 0, sizeof(*libevt_ci));
  1643. list_init(&libevt_ci->esi_open_chans);
  1644. for (i = SA_EVT_HIGHEST_PRIORITY; i <= SA_EVT_LOWEST_PRIORITY; i++) {
  1645. list_init(&libevt_ci->esi_events[i]);
  1646. }
  1647. conn_info->service = SOCKET_SERVICE_EVT;
  1648. list_init (&conn_info->conn_list);
  1649. list_add_tail(&conn_info->conn_list, &ci_head);
  1650. libais_send_response (conn_info, &res, sizeof(res));
  1651. return 0;
  1652. }
  1653. /*
  1654. * Handler for saEvtChannelOpen
  1655. */
  1656. static int lib_evt_open_channel(struct conn_info *conn_info, void *message)
  1657. {
  1658. uint32_t handle;
  1659. SaErrorT error;
  1660. struct req_evt_channel_open *req;
  1661. struct res_evt_channel_open res;
  1662. struct open_chan_pending *ocp;
  1663. int msec_in_future;
  1664. int ret;
  1665. req = message;
  1666. log_printf(LOG_LEVEL_DEBUG,
  1667. "(EVT) saEvtChannelOpen (Open channel request)\n");
  1668. log_printf(LOG_LEVEL_DEBUG,
  1669. "(EVT) handle 0x%x, to 0x%llx\n",
  1670. req->ico_c_handle,
  1671. req->ico_timeout);
  1672. log_printf(LOG_LEVEL_DEBUG, "(EVT) flags %x, channel name(%d) %s\n",
  1673. req->ico_open_flag,
  1674. req->ico_channel_name.length,
  1675. req->ico_channel_name.value);
  1676. /*
  1677. * Open the channel.
  1678. *
  1679. */
  1680. error = evt_open_channel(&req->ico_channel_name, req->ico_open_flag);
  1681. if (error != SA_OK) {
  1682. goto open_return;
  1683. }
  1684. ocp = malloc(sizeof(struct open_chan_pending));
  1685. if (!ocp) {
  1686. error = SA_ERR_NO_MEMORY;
  1687. goto open_return;
  1688. }
  1689. ocp->ocp_async = 0;
  1690. ocp->ocp_invocation = 0;
  1691. ocp->ocp_chan_name = req->ico_channel_name;
  1692. ocp->ocp_open_flag = req->ico_open_flag;
  1693. ocp->ocp_conn_info = conn_info;
  1694. ocp->ocp_timer_handle = 0;
  1695. list_init(&ocp->ocp_entry);
  1696. list_add_tail(&ocp->ocp_entry, &open_pending);
  1697. if (req->ico_timeout != 0) {
  1698. msec_in_future = (uint32_t)(req->ico_timeout / 1000000ULL);
  1699. ret = poll_timer_add(aisexec_poll_handle,
  1700. msec_in_future,
  1701. ocp,
  1702. chan_open_timeout,
  1703. &ocp->ocp_timer_handle);
  1704. if (ret != 0) {
  1705. log_printf(LOG_LEVEL_WARNING,
  1706. "(EVT) Error setting timeout for open channel %s\n",
  1707. req->ico_channel_name.value);
  1708. }
  1709. }
  1710. return 0;
  1711. open_return:
  1712. res.ico_head.size = sizeof(res);
  1713. res.ico_head.id = MESSAGE_RES_EVT_OPEN_CHANNEL;
  1714. res.ico_head.error = error;
  1715. res.ico_channel_handle = handle;
  1716. libais_send_response (conn_info, &res, sizeof(res));
  1717. return 0;
  1718. }
  1719. /*
  1720. * Used by the channel close code and by the implicit close
  1721. * when saEvtFinalize is called with channels open.
  1722. */
  1723. static SaErrorT
  1724. common_chan_close(struct event_svr_channel_open *eco, struct libevt_ci *esip)
  1725. {
  1726. struct event_svr_channel_subscr *ecs;
  1727. struct list_head *l, *nxt;
  1728. log_printf(LOG_LEVEL_DEBUG, "(EVT) Close channel %s flags 0x%02x\n",
  1729. eco->eco_channel->esc_channel_name.value,
  1730. eco->eco_flags);
  1731. /*
  1732. * Unlink the channel open structure.
  1733. *
  1734. * Check for subscriptions and deal with them. In this case
  1735. * if there are any, we just implicitly unsubscribe.
  1736. *
  1737. * When We're done with the channel open data then we can
  1738. * remove it's handle (this frees the memory too).
  1739. *
  1740. */
  1741. list_del(&eco->eco_entry);
  1742. list_del(&eco->eco_instance_entry);
  1743. for (l = eco->eco_subscr.next; l != &eco->eco_subscr; l = nxt) {
  1744. nxt = l->next;
  1745. ecs = list_entry(l, struct event_svr_channel_subscr, ecs_entry);
  1746. log_printf(LOG_LEVEL_DEBUG, "(EVT) Unsubscribe ID: %x\n",
  1747. ecs->ecs_sub_id);
  1748. list_del(&ecs->ecs_entry);
  1749. free(ecs);
  1750. /*
  1751. * Purge any pending events associated with this subscription
  1752. * that don't match another subscription.
  1753. */
  1754. filter_undelivered_events(eco);
  1755. }
  1756. /*
  1757. * Remove this channel from the retained event's notion
  1758. * of who they have been delivered to.
  1759. */
  1760. remove_delivered_channel(eco);
  1761. return evt_close_channel(&eco->eco_channel->esc_channel_name);
  1762. }
  1763. /*
  1764. * Handler for saEvtChannelClose
  1765. */
  1766. static int lib_evt_close_channel(struct conn_info *conn_info, void *message)
  1767. {
  1768. struct req_evt_channel_close *req;
  1769. struct res_evt_channel_close res;
  1770. struct event_svr_channel_open *eco;
  1771. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  1772. SaErrorT error;
  1773. req = message;
  1774. log_printf(LOG_LEVEL_DEBUG,
  1775. "(EVT) saEvtChannelClose (Close channel request)\n");
  1776. log_printf(LOG_LEVEL_DEBUG, "(EVT) handle 0x%x\n", req->icc_channel_handle);
  1777. /*
  1778. * look up the channel handle
  1779. */
  1780. error = saHandleInstanceGet(&esip->esi_hdb,
  1781. req->icc_channel_handle, (void**)&eco);
  1782. if (error != SA_OK) {
  1783. goto chan_close_done;
  1784. }
  1785. common_chan_close(eco, esip);
  1786. saHandleDestroy(&esip->esi_hdb, req->icc_channel_handle);
  1787. saHandleInstancePut(&esip->esi_hdb, req->icc_channel_handle);
  1788. chan_close_done:
  1789. res.icc_head.size = sizeof(res);
  1790. res.icc_head.id = MESSAGE_RES_EVT_CLOSE_CHANNEL;
  1791. res.icc_head.error = error;
  1792. libais_send_response (conn_info, &res, sizeof(res));
  1793. return 0;
  1794. }
  1795. /*
  1796. * Subscribe to an event channel.
  1797. *
  1798. * - First look up the channel to subscribe.
  1799. * - Make sure that the subscription ID is not already in use.
  1800. * - Fill in the subscription data structures and add them to the channels
  1801. * subscription list.
  1802. * - See if there are any events with retetion times that need to be delivered
  1803. * because of the new subscription.
  1804. */
  1805. static char *filter_types[] = {
  1806. "INVALID FILTER TYPE",
  1807. "SA_EVT_PREFIX_FILTER",
  1808. "SA_EVT_SUFFIX_FILTER",
  1809. "SA_EVT_EXACT_FILTER",
  1810. "SA_EVT_PASS_ALL_FILTER",
  1811. };
  1812. /*
  1813. * saEvtEventSubscribe Handler
  1814. */
  1815. static int lib_evt_event_subscribe(struct conn_info *conn_info, void *message)
  1816. {
  1817. struct req_evt_event_subscribe *req;
  1818. struct res_evt_event_subscribe res;
  1819. SaEvtEventFilterArrayT *filters;
  1820. SaErrorT error = SA_OK;
  1821. struct event_svr_channel_open *eco;
  1822. struct event_svr_channel_instance *eci;
  1823. struct event_svr_channel_subscr *ecs;
  1824. struct event_data *evt;
  1825. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  1826. struct list_head *l;
  1827. int i;
  1828. req = message;
  1829. log_printf(LOG_LEVEL_DEBUG,
  1830. "(EVT) saEvtEventSubscribe (Subscribe request)\n");
  1831. log_printf(LOG_LEVEL_DEBUG, "(EVT) subscription Id: 0x%x\n",
  1832. req->ics_sub_id);
  1833. error = evtfilt_to_aisfilt(req, &filters);
  1834. if (error == SA_OK) {
  1835. log_printf(LOG_LEVEL_DEBUG, "(EVT) Subscribe filters count %d\n",
  1836. filters->filtersNumber);
  1837. for (i = 0; i < filters->filtersNumber; i++) {
  1838. log_printf(LOG_LEVEL_DEBUG, "(EVT) type %s(%d) sz %d, <%s>\n",
  1839. filter_types[filters->filters[i].filterType],
  1840. filters->filters[i].filterType,
  1841. filters->filters[i].filter.patternSize,
  1842. (filters->filters[i].filter.patternSize)
  1843. ? (char *)filters->filters[i].filter.pattern
  1844. : "");
  1845. }
  1846. }
  1847. if (error != SA_OK) {
  1848. goto subr_done;
  1849. }
  1850. /*
  1851. * look up the channel handle
  1852. */
  1853. error = saHandleInstanceGet(&esip->esi_hdb,
  1854. req->ics_channel_handle, (void**)&eco);
  1855. if (error != SA_OK) {
  1856. goto subr_done;
  1857. }
  1858. eci = eco->eco_channel;
  1859. /*
  1860. * See if the id is already being used
  1861. */
  1862. ecs = find_subscr(eco, req->ics_sub_id);
  1863. if (ecs) {
  1864. error = SA_ERR_EXIST;
  1865. goto subr_put;
  1866. }
  1867. ecs = (struct event_svr_channel_subscr *)malloc(sizeof(*ecs));
  1868. if (!ecs) {
  1869. error = SA_ERR_NO_MEMORY;
  1870. goto subr_put;
  1871. }
  1872. ecs->ecs_filters = filters;
  1873. ecs->ecs_sub_id = req->ics_sub_id;
  1874. list_init(&ecs->ecs_entry);
  1875. list_add(&ecs->ecs_entry, &eco->eco_subscr);
  1876. res.ics_head.size = sizeof(res);
  1877. res.ics_head.id = MESSAGE_RES_EVT_SUBSCRIBE;
  1878. res.ics_head.error = error;
  1879. libais_send_response (conn_info, &res, sizeof(res));
  1880. /*
  1881. * See if an existing event with a retention time
  1882. * needs to be delivered based on this subscription
  1883. */
  1884. for (l = retained_list.next; l != &retained_list; l = l->next) {
  1885. evt = list_entry(l, struct event_data, ed_retained);
  1886. log_printf(LOG_LEVEL_DEBUG,
  1887. "(EVT) Checking event ID %llx chanp %p -- sub chanp %p\n",
  1888. evt->ed_event.led_event_id, evt->ed_my_chan, eci);
  1889. if (evt->ed_my_chan == eci) {
  1890. if (evt_already_delivered(evt, eco)) {
  1891. continue;
  1892. }
  1893. if (event_match(evt, ecs) == SA_OK) {
  1894. log_printf(LOG_LEVEL_DEBUG,
  1895. "(EVT) deliver event ID: 0x%llx\n",
  1896. evt->ed_event.led_event_id);
  1897. deliver_event(evt, eco, ecs);
  1898. }
  1899. }
  1900. }
  1901. saHandleInstancePut(&esip->esi_hdb, req->ics_channel_handle);
  1902. return 0;
  1903. subr_put:
  1904. saHandleInstancePut(&esip->esi_hdb, req->ics_channel_handle);
  1905. subr_done:
  1906. res.ics_head.size = sizeof(res);
  1907. res.ics_head.id = MESSAGE_RES_EVT_SUBSCRIBE;
  1908. res.ics_head.error = error;
  1909. libais_send_response (conn_info, &res, sizeof(res));
  1910. return 0;
  1911. }
  1912. /*
  1913. * saEvtEventUnsubscribe Handler
  1914. */
  1915. static int lib_evt_event_unsubscribe(struct conn_info *conn_info,
  1916. void *message)
  1917. {
  1918. struct req_evt_event_unsubscribe *req;
  1919. struct res_evt_event_unsubscribe res;
  1920. struct event_svr_channel_open *eco;
  1921. struct event_svr_channel_instance *eci;
  1922. struct event_svr_channel_subscr *ecs;
  1923. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  1924. SaErrorT error = SA_OK;
  1925. req = message;
  1926. log_printf(LOG_LEVEL_DEBUG,
  1927. "(EVT) saEvtEventUnsubscribe (Unsubscribe request)\n");
  1928. log_printf(LOG_LEVEL_DEBUG, "(EVT) subscription Id: 0x%x\n",
  1929. req->icu_sub_id);
  1930. /*
  1931. * look up the channel handle, get the open channel
  1932. * data.
  1933. */
  1934. error = saHandleInstanceGet(&esip->esi_hdb,
  1935. req->icu_channel_handle, (void**)&eco);
  1936. if (error != SA_OK) {
  1937. goto unsubr_done;
  1938. }
  1939. eci = eco->eco_channel;
  1940. /*
  1941. * Make sure that the id exists.
  1942. */
  1943. ecs = find_subscr(eco, req->icu_sub_id);
  1944. if (!ecs) {
  1945. error = SA_ERR_INVALID_PARAM;
  1946. goto unsubr_put;
  1947. }
  1948. list_del(&ecs->ecs_entry);
  1949. log_printf(LOG_LEVEL_DEBUG,
  1950. "(EVT) unsubscribe from channel %s subscription ID 0x%x "
  1951. "with %d filters\n",
  1952. eci->esc_channel_name.value,
  1953. ecs->ecs_sub_id, ecs->ecs_filters->filtersNumber);
  1954. free_filters(ecs->ecs_filters);
  1955. free(ecs);
  1956. unsubr_put:
  1957. saHandleInstancePut(&esip->esi_hdb, req->icu_channel_handle);
  1958. unsubr_done:
  1959. res.icu_head.size = sizeof(res);
  1960. res.icu_head.id = MESSAGE_RES_EVT_UNSUBSCRIBE;
  1961. res.icu_head.error = error;
  1962. libais_send_response (conn_info, &res, sizeof(res));
  1963. return 0;
  1964. }
  1965. /*
  1966. * saEvtEventPublish Handler
  1967. */
  1968. static int lib_evt_event_publish(struct conn_info *conn_info, void *message)
  1969. {
  1970. struct lib_event_data *req;
  1971. struct res_evt_event_publish res;
  1972. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  1973. struct event_svr_channel_open *eco;
  1974. struct event_svr_channel_instance *eci;
  1975. SaEvtEventIdT event_id = 0;
  1976. SaErrorT error = SA_OK;
  1977. struct iovec pub_iovec;
  1978. int result;
  1979. req = message;
  1980. log_printf(LOG_LEVEL_DEBUG,
  1981. "(EVT) saEvtEventPublish (Publish event request)\n");
  1982. /*
  1983. * look up and validate open channel info
  1984. */
  1985. error = saHandleInstanceGet(&esip->esi_hdb,
  1986. req->led_svr_channel_handle, (void**)&eco);
  1987. if (error != SA_OK) {
  1988. goto pub_done;
  1989. }
  1990. eci = eco->eco_channel;
  1991. /*
  1992. * modify the request structure for sending event data to subscribed
  1993. * processes.
  1994. */
  1995. get_event_id(&event_id);
  1996. req->led_head.id = MESSAGE_REQ_EXEC_EVT_EVENTDATA;
  1997. req->led_chan_name = eci->esc_channel_name;
  1998. req->led_event_id = event_id;
  1999. /*
  2000. * Distribute the event.
  2001. * The multicasted event will be picked up and delivered
  2002. * locally by the local network event receiver.
  2003. */
  2004. pub_iovec.iov_base = req;
  2005. pub_iovec.iov_len = req->led_head.size;
  2006. result = gmi_mcast (&aisexec_groupname, &pub_iovec, 1, GMI_PRIO_LOW);
  2007. if (result != 0) {
  2008. error = SA_ERR_SYSTEM;
  2009. }
  2010. saHandleInstancePut(&esip->esi_hdb, req->led_svr_channel_handle);
  2011. pub_done:
  2012. res.iep_head.size = sizeof(res);
  2013. res.iep_head.id = MESSAGE_RES_EVT_PUBLISH;
  2014. res.iep_head.error = error;
  2015. res.iep_event_id = event_id;
  2016. libais_send_response (conn_info, &res, sizeof(res));
  2017. return 0;
  2018. }
  2019. /*
  2020. * saEvtEventRetentionTimeClear handler
  2021. */
  2022. static int lib_evt_event_clear_retentiontime(struct conn_info *conn_info,
  2023. void *message)
  2024. {
  2025. struct req_evt_event_clear_retentiontime *req;
  2026. struct res_evt_event_clear_retentiontime res;
  2027. struct req_evt_chan_command cpkt;
  2028. struct iovec rtn_iovec;
  2029. SaErrorT error = SA_OK;
  2030. int ret;
  2031. req = message;
  2032. log_printf(LOG_LEVEL_DEBUG,
  2033. "(EVT) saEvtEventRetentionTimeClear (Clear event retentiontime request)\n");
  2034. log_printf(LOG_LEVEL_DEBUG,
  2035. "(EVT) event ID 0x%llx, chan handle 0x%x\n",
  2036. req->iec_event_id,
  2037. req->iec_channel_handle);
  2038. memset(&cpkt, 0, sizeof(cpkt));
  2039. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  2040. cpkt.chc_head.size = sizeof(cpkt);
  2041. cpkt.chc_op = EVT_CLEAR_RET_OP;
  2042. cpkt.u.chc_event_id = req->iec_event_id;
  2043. rtn_iovec.iov_base = &cpkt;
  2044. rtn_iovec.iov_len = cpkt.chc_head.size;
  2045. ret = gmi_mcast (&aisexec_groupname, &rtn_iovec, 1, GMI_PRIO_MED);
  2046. if (ret != 0) {
  2047. error = SA_ERR_SYSTEM;
  2048. }
  2049. res.iec_head.size = sizeof(res);
  2050. res.iec_head.id = MESSAGE_REQ_EVT_CLEAR_RETENTIONTIME;
  2051. res.iec_head.error = error;
  2052. libais_send_response (conn_info, &res, sizeof(res));
  2053. return 0;
  2054. }
  2055. /*
  2056. * Send requested event data to the application
  2057. */
  2058. static int lib_evt_event_data_get(struct conn_info *conn_info, void *message)
  2059. {
  2060. struct lib_event_data res;
  2061. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  2062. struct chan_event_list *cel;
  2063. struct event_data *edp;
  2064. int i;
  2065. /*
  2066. * Deliver events in publish order within priority
  2067. */
  2068. for (i = SA_EVT_HIGHEST_PRIORITY; i <= SA_EVT_LOWEST_PRIORITY; i++) {
  2069. if (!list_empty(&esip->esi_events[i])) {
  2070. cel = list_entry(esip->esi_events[i].next, struct chan_event_list,
  2071. cel_entry);
  2072. list_del(&cel->cel_entry);
  2073. list_init(&cel->cel_entry);
  2074. esip->esi_nevents--;
  2075. if (esip->esi_queue_blocked &&
  2076. (esip->esi_nevents < MIN_EVT_QUEUE_RESUME)) {
  2077. esip->esi_queue_blocked = 0;
  2078. log_printf(LOG_LEVEL_DEBUG, "(EVT) unblock\n");
  2079. }
  2080. #ifdef EVT_EVENT_LIST_CHECK
  2081. if (esip->esi_nevents < 0) {
  2082. log_printf(LOG_LEVEL_NOTICE, "(EVT) event count went negative\n");
  2083. if (!list_empty(&esip->esi_events[i])) {
  2084. log_printf(LOG_LEVEL_NOTICE, "(EVT) event list isn't empty\n");
  2085. }
  2086. esip->esi_nevents = 0;
  2087. }
  2088. #endif
  2089. edp = cel->cel_event;
  2090. edp->ed_event.led_lib_channel_handle = cel->cel_chan_handle;
  2091. edp->ed_event.led_sub_id = cel->cel_sub_id;
  2092. edp->ed_event.led_head.id = MESSAGE_RES_EVT_EVENT_DATA;
  2093. edp->ed_event.led_head.error = SA_OK;
  2094. free(cel);
  2095. libais_send_response(conn_info, &edp->ed_event,
  2096. edp->ed_event.led_head.size);
  2097. free_event_data(edp);
  2098. goto data_get_done;
  2099. }
  2100. }
  2101. res.led_head.size = sizeof(res.led_head);
  2102. res.led_head.id = MESSAGE_RES_EVT_EVENT_DATA;
  2103. res.led_head.error = SA_ERR_NOT_EXIST;
  2104. libais_send_response(conn_info, &res, res.led_head.size);
  2105. /*
  2106. * See if there are any events that the app doesn't know about
  2107. * because the notify pipe was full.
  2108. */
  2109. data_get_done:
  2110. if (esip->esi_nevents) {
  2111. __notify_event(conn_info);
  2112. }
  2113. return 0;
  2114. }
  2115. /*
  2116. * Scan the list of channels and remove the specified node.
  2117. */
  2118. static void remove_chan_open_info(SaClmNodeIdT node_id)
  2119. {
  2120. struct list_head *l;
  2121. struct event_svr_channel_instance *eci;
  2122. for (l = esc_head.next; l != &esc_head; l = l->next) {
  2123. eci = list_entry(l, struct event_svr_channel_instance, esc_entry);
  2124. remove_open_count(eci, node_id);
  2125. }
  2126. }
  2127. /*
  2128. * Called when there is a configuration change in the cluster.
  2129. * This function looks at any joiners and leavers and updates the evt
  2130. * node list. The node list is used to keep track of event IDs
  2131. * received for each node for the detection of duplicate events.
  2132. */
  2133. static int evt_conf_change(
  2134. enum gmi_configuration_type configuration_type,
  2135. struct sockaddr_in *member_list, int member_list_entries,
  2136. struct sockaddr_in *left_list, int left_list_entries,
  2137. struct sockaddr_in *joined_list, int joined_list_entries)
  2138. {
  2139. struct in_addr my_node = {SA_CLM_LOCAL_NODE_ID};
  2140. SaClmClusterNodeT *cn;
  2141. static int first = 1;
  2142. struct sockaddr_in *add_list;
  2143. struct member_node_data *md;
  2144. int add_count;
  2145. struct req_evt_chan_command cpkt;
  2146. struct iovec chn_iovec;
  2147. int res;
  2148. log_printf(LOG_LEVEL_DEBUG, "(EVT) Evt conf change %d\n",
  2149. configuration_type);
  2150. log_printf(LOG_LEVEL_DEBUG, "(EVT) m %d, j %d, l %d\n",
  2151. member_list_entries,
  2152. joined_list_entries,
  2153. left_list_entries);
  2154. /*
  2155. * Stop any recovery callbacks in progress.
  2156. */
  2157. if (tok_call_handle) {
  2158. gmi_token_callback_destroy(tok_call_handle);
  2159. tok_call_handle = 0;
  2160. }
  2161. /*
  2162. * Don't seem to be able to tell who joined if we're just coming up. Not all
  2163. * nodes show up in the join list. If this is the first time through,
  2164. * choose the members list to use to add nodes, after that use the join
  2165. * list. Always use the left list for removing nodes.
  2166. */
  2167. if (first) {
  2168. add_list = member_list;
  2169. add_count = member_list_entries;
  2170. first = 0;
  2171. } else {
  2172. add_list = joined_list;
  2173. add_count = joined_list_entries;
  2174. }
  2175. while (add_count--) {
  2176. /*
  2177. * If we've seen this node before, send out the last event ID
  2178. * that we've seen from him. He will set his base event ID to
  2179. * the highest one seen.
  2180. */
  2181. md = evt_find_node(add_list->sin_addr);
  2182. if (md != NULL) {
  2183. if (!md->mn_started) {
  2184. log_printf(LOG_LEVEL_DEBUG,
  2185. "(EVT)Send set evt ID %llx to %s\n",
  2186. md->mn_last_evt_id, inet_ntoa(add_list->sin_addr));
  2187. md->mn_started = 1;
  2188. memset(&cpkt, 0, sizeof(cpkt));
  2189. cpkt.chc_head.id = MESSAGE_REQ_EXEC_EVT_CHANCMD;
  2190. cpkt.chc_head.size = sizeof(cpkt);
  2191. cpkt.chc_op = EVT_SET_ID_OP;
  2192. cpkt.u.chc_set_id.chc_addr = add_list->sin_addr;
  2193. cpkt.u.chc_set_id.chc_last_id = md->mn_last_evt_id;
  2194. chn_iovec.iov_base = &cpkt;
  2195. chn_iovec.iov_len = cpkt.chc_head.size;
  2196. res = gmi_mcast (&aisexec_groupname, &chn_iovec, 1,
  2197. GMI_PRIO_RECOVERY);
  2198. if (res != 0) {
  2199. log_printf(LOG_LEVEL_WARNING,
  2200. "(EVT) Unable to send event id to %s\n",
  2201. inet_ntoa(add_list->sin_addr));
  2202. }
  2203. }
  2204. }
  2205. add_list++;
  2206. }
  2207. while (left_list_entries--) {
  2208. md = evt_find_node(left_list->sin_addr);
  2209. if (md == 0) {
  2210. log_printf(LOG_LEVEL_DEBUG,
  2211. "(EVT) Can't find cluster node at %s\n",
  2212. inet_ntoa(left_list->sin_addr));
  2213. /*
  2214. * Mark this one as down.
  2215. */
  2216. } else {
  2217. log_printf(LOG_LEVEL_DEBUG, "(EVT) cluster node at %s down\n",
  2218. inet_ntoa(left_list->sin_addr));
  2219. md->mn_started = 0;
  2220. remove_chan_open_info(md->mn_node_info.nodeId);
  2221. }
  2222. left_list++;
  2223. }
  2224. /*
  2225. * Set the base event id
  2226. */
  2227. cn = clm_get_by_nodeid(my_node);
  2228. if (!base_id) {
  2229. log_printf(LOG_LEVEL_DEBUG, "(EVT) My node ID 0x%x\n", cn->nodeId);
  2230. my_node_id = cn->nodeId;
  2231. set_event_id(my_node_id);
  2232. }
  2233. /*
  2234. * Notify that a config change happened. The exec handler will
  2235. * then determine what to do.
  2236. */
  2237. if (configuration_type == GMI_CONFIGURATION_REGULAR) {
  2238. if (in_cfg_change) {
  2239. log_printf(LOG_LEVEL_DEBUG,
  2240. "(EVT) Already in config change, Starting over, m %d, c %d\n",
  2241. total_members, checked_in);
  2242. }
  2243. in_cfg_change = 1;
  2244. total_members = member_list_entries;
  2245. checked_in = 0;
  2246. any_joined = joined_list_entries;
  2247. /*
  2248. * Start by updating all the nodes on our
  2249. * open channel count. Once that is done, proceed to determining who
  2250. * sends ratained events. Then we can start normal operation again.
  2251. */
  2252. send_open_count();
  2253. }
  2254. return 0;
  2255. }
  2256. /*
  2257. * saEvtFinalize Handler
  2258. */
  2259. static int evt_finalize(struct conn_info *conn_info)
  2260. {
  2261. struct libevt_ci *esip = &conn_info->ais_ci.u.libevt_ci;
  2262. struct event_svr_channel_open *eco;
  2263. struct list_head *l, *nxt;
  2264. log_printf(LOG_LEVEL_DEBUG, "(EVT) saEvtFinalize (Event exit request)\n");
  2265. log_printf(LOG_LEVEL_DEBUG, "(EVT) saEvtFinalize %d evts on list\n",
  2266. esip->esi_nevents);
  2267. /*
  2268. * Clean up any open channels and associated subscriptions.
  2269. */
  2270. for (l = esip->esi_open_chans.next; l != &esip->esi_open_chans; l = nxt) {
  2271. nxt = l->next;
  2272. eco = list_entry(l, struct event_svr_channel_open, eco_instance_entry);
  2273. common_chan_close(eco, esip);
  2274. saHandleDestroy(&esip->esi_hdb, eco->eco_my_handle);
  2275. }
  2276. #ifdef EVT_EVENT_LIST_CHECK
  2277. {
  2278. int i;
  2279. if (esip->esi_nevents) {
  2280. log_printf(LOG_LEVEL_WARNING,
  2281. "(EVT) %d Events left on delivery list after finalize\n",
  2282. esip->esi_nevents);
  2283. }
  2284. for (i = SA_EVT_HIGHEST_PRIORITY; i <= SA_EVT_LOWEST_PRIORITY; i++) {
  2285. if (!list_empty(&esip->esi_events[i])) {
  2286. log_printf(LOG_LEVEL_WARNING,
  2287. "(EVT) Events list not empty after finalize\n");
  2288. }
  2289. }
  2290. }
  2291. #endif
  2292. /*
  2293. * Delete track entry if there is one
  2294. */
  2295. list_del (&conn_info->conn_list);
  2296. return 0;
  2297. }
  2298. /*
  2299. * Called at service start time.
  2300. */
  2301. static int evt_exec_init(void)
  2302. {
  2303. int res;
  2304. log_printf(LOG_LEVEL_DEBUG, "(EVT) Evt exec init request\n");
  2305. res = gmi_recovery_plug_create (&evt_recovery_plug_handle);
  2306. if (res != 0) {
  2307. log_printf(LOG_LEVEL_ERROR,
  2308. "(EVT) Could not create recovery plug for event service.\n");
  2309. return (-1);
  2310. }
  2311. /*
  2312. * Create an event to be sent when we have to drop messages
  2313. * for an application.
  2314. */
  2315. dropped_event_size = sizeof(*dropped_event) + sizeof(dropped_pattern);
  2316. dropped_event = malloc(dropped_event_size);
  2317. if (dropped_event == 0) {
  2318. log_printf(LOG_LEVEL_ERROR,
  2319. "(EVT) Memory Allocation Failure, event service not started\n");
  2320. res = gmi_recovery_plug_destroy (evt_recovery_plug_handle);
  2321. errno = ENOMEM;
  2322. return -1;
  2323. }
  2324. memset(dropped_event, 0, sizeof(*dropped_event) + sizeof(dropped_pattern));
  2325. dropped_event->ed_ref_count = 1;
  2326. list_init(&dropped_event->ed_retained);
  2327. dropped_event->ed_event.led_head.size =
  2328. sizeof(*dropped_event) + sizeof(dropped_pattern);
  2329. dropped_event->ed_event.led_head.error = SA_OK;
  2330. dropped_event->ed_event.led_priority = SA_EVT_HIGHEST_PRIORITY;
  2331. dropped_event->ed_event.led_chan_name = lost_chan;
  2332. dropped_event->ed_event.led_publisher_name = dropped_publisher;
  2333. dropped_event->ed_event.led_patterns_number = 1;
  2334. memcpy(&dropped_event->ed_event.led_body[0],
  2335. &dropped_pattern, sizeof(dropped_pattern));
  2336. return 0;
  2337. }
  2338. /*
  2339. * Receive the network event message and distribute it to local subscribers
  2340. */
  2341. static int evt_remote_evt(void *msg, struct in_addr source_addr)
  2342. {
  2343. /*
  2344. * - retain events that have a retention time
  2345. * - Find assocated channel
  2346. * - Scan list of subscribers
  2347. * - Apply filters
  2348. * - Deliver events that pass the filter test
  2349. */
  2350. struct lib_event_data *evtpkt = msg;
  2351. struct event_svr_channel_instance *eci;
  2352. struct event_svr_channel_open *eco;
  2353. struct event_svr_channel_subscr *ecs;
  2354. struct event_data *evt;
  2355. struct list_head *l, *l1;
  2356. SaClmClusterNodeT *cn;
  2357. log_printf(LOG_LEVEL_DEBUG, "(EVT) Remote event data received from %s\n",
  2358. inet_ntoa(source_addr));
  2359. /*
  2360. * See where the message came from so that we can set the
  2361. * publishing node id in the message before delivery.
  2362. */
  2363. cn = clm_get_by_nodeid (source_addr);
  2364. if (!cn) {
  2365. /*
  2366. * Not sure how this can happen...
  2367. */
  2368. log_printf(LOG_LEVEL_DEBUG, "(EVT) No cluster node for %s\n",
  2369. inet_ntoa(source_addr));
  2370. errno = ENXIO;
  2371. return -1;
  2372. }
  2373. log_printf(LOG_LEVEL_DEBUG, "(EVT) Cluster node ID 0x%x name %s\n",
  2374. cn->nodeId, cn->nodeName.value);
  2375. evtpkt->led_publisher_node_id = cn->nodeId;
  2376. evtpkt->led_in_addr = source_addr;
  2377. evtpkt->led_receive_time = clust_time_now();
  2378. eci = find_channel(&evtpkt->led_chan_name);
  2379. /*
  2380. * If we don't kmow about the channel, then no one has opened it yet.
  2381. * We create the channel if there is a retention time, otherwise we can
  2382. * just throw it away since no one here is looking for this event.
  2383. */
  2384. if (!eci) {
  2385. if (evtpkt->led_retention_time) {
  2386. eci = create_channel(&evtpkt->led_chan_name);
  2387. if (!eci) {
  2388. log_printf(LOG_LEVEL_WARNING, "(EVT) Can't create channel %s\n",
  2389. evtpkt->led_chan_name.value);
  2390. }
  2391. } else {
  2392. return 0;
  2393. }
  2394. }
  2395. if (check_last_event(evtpkt, source_addr)) {
  2396. return 0;
  2397. }
  2398. evt = make_local_event(evtpkt, eci);
  2399. if (!evt) {
  2400. log_printf(LOG_LEVEL_WARNING,
  2401. "(EVT) Memory allocation error, can't deliver event\n");
  2402. errno = ENOMEM;
  2403. return -1;
  2404. }
  2405. if (evt->ed_event.led_retention_time) {
  2406. retain_event(evt);
  2407. }
  2408. /*
  2409. * Check open channels
  2410. */
  2411. for (l = eci->esc_open_chans.next; l != &eci->esc_open_chans; l = l->next) {
  2412. eco = list_entry(l, struct event_svr_channel_open, eco_entry);
  2413. /*
  2414. * See if enabled to receive
  2415. */
  2416. if (!(eco->eco_flags & SA_EVT_CHANNEL_SUBSCRIBER)) {
  2417. continue;
  2418. }
  2419. /*
  2420. * Check subscriptions
  2421. */
  2422. for (l1 = eco->eco_subscr.next; l1 != &eco->eco_subscr; l1 = l1->next) {
  2423. ecs = list_entry(l1, struct event_svr_channel_subscr, ecs_entry);
  2424. /*
  2425. * Apply filter rules and deliver if patterns
  2426. * match filters.
  2427. * Only deliver one event per open channel
  2428. */
  2429. if (event_match(evt, ecs) == SA_OK) {
  2430. deliver_event(evt, eco, ecs);
  2431. break;
  2432. }
  2433. }
  2434. }
  2435. free_event_data(evt);
  2436. return 0;
  2437. }
  2438. /*
  2439. * Calculate the remaining retention time of a received event during recovery
  2440. */
  2441. inline SaTimeT calc_retention_time(SaTimeT retention,
  2442. SaTimeT received, SaTimeT now)
  2443. {
  2444. if ((received < now) && ((now - received) < retention)) {
  2445. return retention - (now - received);
  2446. } else {
  2447. return 0;
  2448. }
  2449. }
  2450. /*
  2451. * Receive a recovery network event message and save it in the retained list
  2452. */
  2453. static int evt_remote_recovery_evt(void *msg, struct in_addr source_addr)
  2454. {
  2455. /*
  2456. * - retain events that have a retention time
  2457. * - Find assocated channel
  2458. */
  2459. struct lib_event_data *evtpkt = msg;
  2460. struct event_svr_channel_instance *eci;
  2461. struct event_data *evt;
  2462. struct member_node_data *md;
  2463. SaTimeT now;
  2464. now = clust_time_now();
  2465. log_printf(LOG_LEVEL_DEBUG,
  2466. "(EVT) Remote recovery event data received from %s\n",
  2467. inet_ntoa(source_addr));
  2468. if (!in_cfg_change) {
  2469. log_printf(LOG_LEVEL_NOTICE,
  2470. "(EVT) Received recovery data, not in recovery mode\n");
  2471. return 0;
  2472. }
  2473. log_printf(LOG_LEVEL_DEBUG,
  2474. "(EVT) Processing recovery of retained events\n");
  2475. if (recovery_node) {
  2476. log_printf(LOG_LEVEL_DEBUG, "(EVT) This node is the recovery node\n");
  2477. }
  2478. log_printf(LOG_LEVEL_DEBUG, "(EVT) (1)EVT ID: %llx, Time: %llx\n",
  2479. evtpkt->led_event_id, evtpkt->led_retention_time);
  2480. /*
  2481. * Calculate remaining retention time
  2482. */
  2483. evtpkt->led_retention_time = calc_retention_time(
  2484. evtpkt->led_retention_time,
  2485. evtpkt->led_receive_time,
  2486. now);
  2487. log_printf(LOG_LEVEL_DEBUG,
  2488. "(EVT) (2)EVT ID: %llx, ret: %llx, rec: %llx, now: %llx\n",
  2489. evtpkt->led_event_id,
  2490. evtpkt->led_retention_time, evtpkt->led_receive_time, now);
  2491. /*
  2492. * If we haven't seen this event yet and it has remaining time, process
  2493. * the event.
  2494. */
  2495. if (!check_last_event(evtpkt, evtpkt->led_in_addr) &&
  2496. evtpkt->led_retention_time) {
  2497. /*
  2498. * See where the message came from so that we can set the
  2499. * publishing node id in the message before delivery.
  2500. */
  2501. md = evt_find_node(evtpkt->led_in_addr);
  2502. if (!md) {
  2503. /*
  2504. * Not sure how this can happen
  2505. */
  2506. log_printf(LOG_LEVEL_DEBUG, "(EVT) No node for %s\n",
  2507. inet_ntoa(evtpkt->led_in_addr));
  2508. errno = ENXIO;
  2509. return -1;
  2510. }
  2511. log_printf(LOG_LEVEL_DEBUG, "(EVT) Cluster node ID 0x%x name %s\n",
  2512. md->mn_node_info.nodeId,
  2513. md->mn_node_info.nodeName.value);
  2514. eci = find_channel(&evtpkt->led_chan_name);
  2515. /*
  2516. * If the channel doesn't exist, then create it
  2517. * since we're in recovery mode, so that we can save this message.
  2518. */
  2519. if (!eci) {
  2520. eci = create_channel(&evtpkt->led_chan_name);
  2521. if (!eci) {
  2522. log_printf(LOG_LEVEL_WARNING, "(EVT) Can't create channel %s\n",
  2523. evtpkt->led_chan_name.value);
  2524. return 0;
  2525. }
  2526. }
  2527. evt = make_local_event(evtpkt, eci);
  2528. if (!evt) {
  2529. log_printf(LOG_LEVEL_WARNING,
  2530. "(EVT) Memory allocation error, can't deliver event\n");
  2531. errno = ENOMEM;
  2532. return -1;
  2533. }
  2534. retain_event(evt);
  2535. free_event_data(evt);
  2536. }
  2537. return 0;
  2538. }
  2539. /*
  2540. * Timeout handler for event channel open.
  2541. */
  2542. static void chan_open_timeout(void *data)
  2543. {
  2544. struct open_chan_pending *ocp = (struct open_chan_pending *)data;
  2545. struct res_evt_channel_open res;
  2546. res.ico_head.size = sizeof(res);
  2547. res.ico_head.id = MESSAGE_RES_EVT_OPEN_CHANNEL;
  2548. res.ico_head.error = SA_ERR_TIMEOUT;
  2549. libais_send_response (ocp->ocp_conn_info, &res, sizeof(res));
  2550. list_del(&ocp->ocp_entry);
  2551. free(ocp);
  2552. }
  2553. /*
  2554. * Called by the channel open exec handler to finish the open and
  2555. * respond to the application
  2556. */
  2557. static void evt_chan_open_finish(struct open_chan_pending *ocp,
  2558. struct event_svr_channel_instance *eci)
  2559. {
  2560. uint32_t handle;
  2561. struct res_evt_channel_open res;
  2562. struct event_svr_channel_open *eco;
  2563. SaErrorT error;
  2564. struct libevt_ci *esip = &ocp->ocp_conn_info->ais_ci.u.libevt_ci;
  2565. int ret = 0;
  2566. if (!ocp->ocp_async && ocp->ocp_timer_handle) {
  2567. ret = poll_timer_delete(aisexec_poll_handle, ocp->ocp_timer_handle);
  2568. if (ret != 0 ) {
  2569. log_printf(LOG_LEVEL_WARNING,
  2570. "(EVT) Error clearing timeout for open channel of %s\n",
  2571. ocp->ocp_chan_name);
  2572. }
  2573. }
  2574. /*
  2575. * Create a handle to give back to the caller to associate
  2576. * with this channel open instance.
  2577. */
  2578. error = saHandleCreate(&esip->esi_hdb, sizeof(*eco), &handle);
  2579. if (error != SA_OK) {
  2580. goto open_return;
  2581. }
  2582. error = saHandleInstanceGet(&esip->esi_hdb, handle, (void**)&eco);
  2583. if (error != SA_OK) {
  2584. goto open_return;
  2585. }
  2586. /*
  2587. * Initailize and link into the global channel structure.
  2588. */
  2589. list_init(&eco->eco_subscr);
  2590. list_init(&eco->eco_entry);
  2591. list_init(&eco->eco_instance_entry);
  2592. eco->eco_flags = ocp->ocp_open_flag;
  2593. eco->eco_channel = eci;
  2594. eco->eco_lib_handle = ocp->ocp_c_handle;
  2595. eco->eco_my_handle = handle;
  2596. eco->eco_conn_info = ocp->ocp_conn_info;
  2597. list_add_tail(&eco->eco_entry, &eci->esc_open_chans);
  2598. list_add_tail(&eco->eco_instance_entry, &esip->esi_open_chans);
  2599. /*
  2600. * respond back with a handle to access this channel
  2601. * open instance for later subscriptions, etc.
  2602. */
  2603. saHandleInstancePut(&esip->esi_hdb, handle);
  2604. open_return:
  2605. res.ico_head.size = sizeof(res);
  2606. res.ico_head.id = MESSAGE_RES_EVT_OPEN_CHANNEL;
  2607. res.ico_head.error = error;
  2608. res.ico_channel_handle = handle;
  2609. libais_send_response (ocp->ocp_conn_info, &res, sizeof(res));
  2610. if (ret == 0) {
  2611. list_del(&ocp->ocp_entry);
  2612. free(ocp);
  2613. }
  2614. }
  2615. /*
  2616. * Receive and process remote event operations.
  2617. * Used to communicate channel opens/closes, clear retention time,
  2618. * config change updates...
  2619. */
  2620. static int evt_remote_chan_op(void *msg, struct in_addr source_addr)
  2621. {
  2622. struct req_evt_chan_command *cpkt = msg;
  2623. struct in_addr local_node = {SA_CLM_LOCAL_NODE_ID};
  2624. SaClmClusterNodeT *cn, *my_node;
  2625. struct member_node_data *mn;
  2626. struct event_svr_channel_instance *eci;
  2627. log_printf(LOG_LEVEL_DEBUG, "(EVT) Remote channel operation request\n");
  2628. my_node = clm_get_by_nodeid(local_node);
  2629. mn = evt_find_node(source_addr);
  2630. if (mn == NULL) {
  2631. cn = clm_get_by_nodeid(source_addr);
  2632. if (cn == NULL) {
  2633. log_printf(LOG_LEVEL_WARNING,
  2634. "(EVT) Evt remote channel op: Node data for addr %s is NULL\n",
  2635. inet_ntoa(source_addr));
  2636. } else {
  2637. evt_add_node(source_addr, cn);
  2638. mn = evt_find_node(source_addr);
  2639. }
  2640. }
  2641. switch (cpkt->chc_op) {
  2642. /*
  2643. * Open channel remote command. The open channel request is done
  2644. * in two steps. When an pplication tries to open, we send an open
  2645. * channel message to the other nodes. When we receive an open channel
  2646. * message, we may create the channel structure if it doesn't exist
  2647. * and also complete applicaiton open requests for the specified
  2648. * channel. We keep a counter of total opens for the given channel and
  2649. * later when it has been completely closed (everywhere in the cluster)
  2650. * we will free up the assocated channel data.
  2651. */
  2652. case EVT_OPEN_CHAN_OP: {
  2653. struct open_chan_pending *ocp;
  2654. struct list_head *l, *nxt;
  2655. log_printf(LOG_LEVEL_DEBUG, "(EVT) Opening channel %s for node 0x%x\n",
  2656. cpkt->u.chc_chan.value, mn->mn_node_info.nodeId);
  2657. eci = find_channel(&cpkt->u.chc_chan);
  2658. if (!eci) {
  2659. eci = create_channel(&cpkt->u.chc_chan);
  2660. }
  2661. if (!eci) {
  2662. log_printf(LOG_LEVEL_WARNING, "(EVT) Could not create channel %s\n",
  2663. &cpkt->u.chc_chan.value);
  2664. break;
  2665. }
  2666. inc_open_count(eci, mn->mn_node_info.nodeId);
  2667. if (mn->mn_node_info.nodeId == my_node->nodeId) {
  2668. eci->esc_local_opens++;
  2669. /*
  2670. * Complete one of our pending open requests
  2671. */
  2672. for (l = open_pending.next; l != &open_pending; l = nxt) {
  2673. nxt = l->next;
  2674. ocp = list_entry(l, struct open_chan_pending, ocp_entry);
  2675. if (name_match(&ocp->ocp_chan_name, &eci->esc_channel_name)) {
  2676. evt_chan_open_finish(ocp, eci);
  2677. break;
  2678. }
  2679. }
  2680. }
  2681. log_printf(LOG_LEVEL_DEBUG,
  2682. "(EVT) Open channel %s t %d, l %d, r %d\n",
  2683. eci->esc_channel_name.value,
  2684. eci->esc_total_opens, eci->esc_local_opens,
  2685. eci->esc_retained_count);
  2686. break;
  2687. }
  2688. /*
  2689. * Handle a channel close. We'll decrement the global open counter and
  2690. * free up channel associated data when all instances are closed.
  2691. */
  2692. case EVT_CLOSE_CHAN_OP:
  2693. log_printf(LOG_LEVEL_DEBUG, "(EVT) Closing channel %s for node 0x%x\n",
  2694. cpkt->u.chc_chan.value, mn->mn_node_info.nodeId);
  2695. eci = find_channel(&cpkt->u.chc_chan);
  2696. if (!eci) {
  2697. log_printf(LOG_LEVEL_NOTICE,
  2698. "(EVT) Channel close request for %s not found\n",
  2699. cpkt->u.chc_chan);
  2700. break;
  2701. }
  2702. if (mn->mn_node_info.nodeId == my_node->nodeId) {
  2703. eci->esc_local_opens--;
  2704. }
  2705. /*
  2706. * if last instance, we can free up assocated data.
  2707. */
  2708. dec_open_count(eci, mn->mn_node_info.nodeId);
  2709. delete_channel(eci);
  2710. break;
  2711. /*
  2712. * saEvtClearRetentiotime handler.
  2713. */
  2714. case EVT_CLEAR_RET_OP:
  2715. log_printf(LOG_LEVEL_DEBUG, "(EVT) Clear retention time request %llx\n",
  2716. cpkt->u.chc_event_id);
  2717. clear_retention_time(cpkt->u.chc_event_id);
  2718. break;
  2719. /*
  2720. * Set our next event ID based on the largest event ID seen
  2721. * by others in the cluster. This way, if we've left and re-joined, we'll
  2722. * start using an event ID that is unique.
  2723. */
  2724. case EVT_SET_ID_OP: {
  2725. struct in_addr my_addr;
  2726. my_addr.s_addr = my_node->nodeId;
  2727. log_printf(LOG_LEVEL_DEBUG,
  2728. "(EVT) Received Set event ID OP from %x to %llx for %x my addr %x base %llx\n",
  2729. inet_ntoa(source_addr),
  2730. cpkt->u.chc_set_id.chc_last_id,
  2731. cpkt->u.chc_set_id.chc_addr.s_addr,
  2732. my_addr.s_addr,
  2733. base_id);
  2734. if (cpkt->u.chc_set_id.chc_addr.s_addr == my_addr.s_addr) {
  2735. if (cpkt->u.chc_set_id.chc_last_id > base_id) {
  2736. log_printf(LOG_LEVEL_DEBUG,
  2737. "(EVT) Set event ID from %s to %llx\n",
  2738. inet_ntoa(source_addr), cpkt->u.chc_set_id.chc_last_id);
  2739. base_id = cpkt->u.chc_set_id.chc_last_id + 1;
  2740. }
  2741. }
  2742. break;
  2743. }
  2744. /*
  2745. * Receive the open count for a particular channel during recovery.
  2746. * This insures that everyone has the same notion of who has a channel
  2747. * open so that it can be removed when no one else has it open anymore.
  2748. */
  2749. case EVT_OPEN_COUNT:
  2750. if (!in_cfg_change) {
  2751. log_printf(LOG_LEVEL_ERROR,
  2752. "(EVT) Evt open count msg from %s, but not in membership change\n",
  2753. inet_ntoa(source_addr));
  2754. }
  2755. log_printf(LOG_LEVEL_DEBUG,
  2756. "(EVT) Open channel count %s is %d for node 0x%x\n",
  2757. cpkt->u.chc_set_opens.chc_chan_name.value,
  2758. cpkt->u.chc_set_opens.chc_open_count,
  2759. mn->mn_node_info.nodeId);
  2760. eci = find_channel(&cpkt->u.chc_set_opens.chc_chan_name);
  2761. if (!eci) {
  2762. eci = create_channel(&cpkt->u.chc_set_opens.chc_chan_name);
  2763. }
  2764. if (!eci) {
  2765. log_printf(LOG_LEVEL_WARNING, "(EVT) Could not create channel %s\n",
  2766. &cpkt->u.chc_set_opens.chc_chan_name.value);
  2767. break;
  2768. }
  2769. if (set_open_count(eci, mn->mn_node_info.nodeId,
  2770. cpkt->u.chc_set_opens.chc_open_count)) {
  2771. log_printf(LOG_LEVEL_ERROR,
  2772. "(EVT) Error setting Open channel count %s for node 0x%x\n",
  2773. cpkt->u.chc_set_opens.chc_chan_name.value,
  2774. mn->mn_node_info.nodeId);
  2775. }
  2776. break;
  2777. /*
  2778. * Once we get all the messages from
  2779. * the current membership, determine who delivers any retained events.
  2780. */
  2781. case EVT_OPEN_COUNT_DONE: {
  2782. if (!in_cfg_change) {
  2783. log_printf(LOG_LEVEL_ERROR,
  2784. "(EVT) Evt config msg from %s, but not in membership change\n",
  2785. inet_ntoa(source_addr));
  2786. }
  2787. log_printf(LOG_LEVEL_DEBUG,
  2788. "(EVT) Receive EVT_CONF_CHANGE_DONE from %s members %d checked in %d\n",
  2789. inet_ntoa(source_addr), total_members, checked_in+1);
  2790. if (!mn) {
  2791. log_printf(LOG_LEVEL_DEBUG,
  2792. "(EVT) NO NODE DATA AVAILABLE FOR %s\n",
  2793. inet_ntoa(source_addr));
  2794. }
  2795. if (++checked_in == total_members) {
  2796. /*
  2797. * We're all here, now figure out who should send the
  2798. * retained events, if any.
  2799. */
  2800. mn = oldest_node();
  2801. if (mn->mn_node_info.nodeId == my_node_id) {
  2802. log_printf(LOG_LEVEL_DEBUG, "(EVT) I am oldest\n");
  2803. send_retained();
  2804. }
  2805. }
  2806. break;
  2807. }
  2808. /*
  2809. * OK, We're done with recovery, continue operations.
  2810. */
  2811. case EVT_CONF_DONE: {
  2812. log_printf(LOG_LEVEL_DEBUG,
  2813. "(EVT) Receive EVT_CONF_DONE from %s\n",
  2814. inet_ntoa(source_addr));
  2815. in_cfg_change = 0;
  2816. gmi_recovery_plug_unplug (evt_recovery_plug_handle);
  2817. break;
  2818. }
  2819. default:
  2820. log_printf(LOG_LEVEL_NOTICE, "(EVT) Invalid channel operation %d\n",
  2821. cpkt->chc_op);
  2822. break;
  2823. }
  2824. return 0;
  2825. }