evt.c 59 KB

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