evt.c 83 KB

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