evt.c 83 KB

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