4
0

evt.c 73 KB

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