evt.c 60 KB

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