evt.c 73 KB

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