4
0

evt.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275
  1. /*
  2. * Copyright (c) 2004-2005 Mark Haverkamp
  3. * Copyright (c) 2004-2005 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 Developement 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. #include <sys/types.h>
  34. #include <errno.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <unistd.h>
  39. #include <sys/time.h>
  40. #include <sys/socket.h>
  41. #include "../include/ipc_evt.h"
  42. #include "util.h"
  43. #include "../exec/totem.h"
  44. #include "../include/list.h"
  45. static void evtHandleInstanceDestructor(void *instance);
  46. static void chanHandleInstanceDestructor(void *instance);
  47. static void eventHandleInstanceDestructor(void *instance);
  48. /*
  49. * Versions of the SAF AIS specification supported by this library
  50. */
  51. static SaVersionT supported_versions[] = {
  52. {'B', 0x01, 0x01}
  53. };
  54. static struct saVersionDatabase evt_version_database = {
  55. sizeof(supported_versions) / sizeof(SaVersionT),
  56. supported_versions
  57. };
  58. /*
  59. * Event instance data
  60. */
  61. struct saHandleDatabase evt_instance_handle_db = {
  62. .handleCount = 0,
  63. .handles = 0,
  64. .mutex = PTHREAD_MUTEX_INITIALIZER,
  65. .handleInstanceDestructor = evtHandleInstanceDestructor
  66. };
  67. /*
  68. * Channel instance data
  69. */
  70. struct saHandleDatabase channel_handle_db = {
  71. .handleCount = 0,
  72. .handles = 0,
  73. .mutex = PTHREAD_MUTEX_INITIALIZER,
  74. .handleInstanceDestructor = chanHandleInstanceDestructor
  75. };
  76. /*
  77. * Event instance data
  78. */
  79. struct saHandleDatabase event_handle_db = {
  80. .handleCount = 0,
  81. .handles = 0,
  82. .mutex = PTHREAD_MUTEX_INITIALIZER,
  83. .handleInstanceDestructor = eventHandleInstanceDestructor
  84. };
  85. struct res_overlay {
  86. struct res_header header;
  87. char data[MESSAGE_SIZE_MAX];
  88. };
  89. struct handle_list {
  90. SaUint64T hl_handle;
  91. struct list_head hl_entry;
  92. };
  93. /*
  94. * data required to support events for a given initialization
  95. *
  96. * ei_dispatch_fd: fd used for getting callback data e.g. async event data
  97. * ei_response_fd: fd used for everything else (i.e. evt sync api commands).
  98. * ei_callback: callback function.
  99. * ei_version: version sent to the evtInitialize call.
  100. * ei_node_id: our node id.
  101. * ei_node_name: our node name.
  102. * ei_finalize: instance in finalize flag
  103. * ei_dispatch_mutex: mutex for dispatch fd
  104. * ei_response_mutex: mutex for response fd
  105. * ei_channel_list: list of associated channels (struct handle_list)
  106. * ei_data_available: Indicates that there is a pending event message though
  107. * there may not be a poll event. This can happen
  108. * when we get a SA_AIS_ERR_TRY_AGAIN when asking for an
  109. * event.
  110. *
  111. */
  112. struct event_instance {
  113. int ei_dispatch_fd;
  114. int ei_response_fd;
  115. SaEvtCallbacksT ei_callback;
  116. SaVersionT ei_version;
  117. SaClmNodeIdT ei_node_id;
  118. SaNameT ei_node_name;
  119. int ei_finalize;
  120. pthread_mutex_t ei_dispatch_mutex;
  121. pthread_mutex_t ei_response_mutex;
  122. struct list_head ei_channel_list;
  123. int ei_data_available;
  124. };
  125. /*
  126. * Data associated with an opened channel
  127. *
  128. * eci_channel_name: name of channel
  129. * eci_open_flags: channel open flags
  130. * eci_svr_channel_handle: channel handle returned from server
  131. * eci_closing: channel in process of being closed
  132. * eci_mutex: channel mutex
  133. * eci_event_list: events associated with this
  134. * channel (struct handle_list)
  135. * eci_hl: pointer to event instance handle struct
  136. * for this channel.
  137. */
  138. struct event_channel_instance {
  139. SaNameT eci_channel_name;
  140. SaEvtChannelOpenFlagsT eci_open_flags;
  141. uint32_t eci_svr_channel_handle;
  142. SaEvtHandleT eci_instance_handle;
  143. int eci_closing;
  144. pthread_mutex_t eci_mutex;
  145. struct list_head eci_event_list;
  146. struct handle_list *eci_hl;
  147. };
  148. /*
  149. * Event data.
  150. *
  151. * Store event data from saEvtEventAllocate function.
  152. * Store event data from received events.
  153. *
  154. * edi_channel_handle: handle (local) of assocated channel
  155. * edi_patterns: event patterns
  156. * edi_priority: event priority
  157. * edi_retention_time: event's retention time
  158. * edi_pub_name: event's publisher name
  159. * edi_pub_node: event's publisher node
  160. * edi_pub_time: event's publish time
  161. * edi_event_id: event's Id
  162. * edi_event_data: event's data
  163. * edi_event_data_size: size of edi_event_data
  164. * edi_freeing: event is being freed
  165. * edi_mutex: event data mutex
  166. * edi_hl: pointer to channel's handle
  167. * struct for this event.
  168. * edi_ro: read only flag
  169. */
  170. struct event_data_instance {
  171. SaEvtChannelHandleT edi_channel_handle;
  172. SaEvtEventPatternArrayT edi_patterns;
  173. SaUint8T edi_priority;
  174. SaTimeT edi_retention_time;
  175. SaNameT edi_pub_name;
  176. SaClmNodeIdT edi_pub_node;
  177. SaTimeT edi_pub_time;
  178. SaEvtEventIdT edi_event_id;
  179. void *edi_event_data;
  180. SaSizeT edi_event_data_size;
  181. int edi_freeing;
  182. pthread_mutex_t edi_mutex;
  183. struct handle_list *edi_hl;
  184. int edi_ro;
  185. };
  186. #define min(a,b) ((a) < (b) ? (a) : (b))
  187. static inline int is_valid_event_id(SaEvtEventIdT evt_id)
  188. {
  189. if (evt_id > 1000) {
  190. return 1;
  191. } else {
  192. return 0;
  193. }
  194. }
  195. /*
  196. * Clean up function for an evt instance (saEvtInitialize) handle
  197. * Not to be confused with event data.
  198. */
  199. static void evtHandleInstanceDestructor(void *instance)
  200. {
  201. struct event_instance *evti = instance;
  202. struct event_channel_instance *eci;
  203. struct handle_list *hl;
  204. struct list_head *l, *nxt;
  205. uint64_t handle;
  206. SaAisErrorT error;
  207. /*
  208. * Free up any channel data
  209. */
  210. for (l = evti->ei_channel_list.next;
  211. l != &evti->ei_channel_list; l = nxt) {
  212. nxt = l->next;
  213. hl = list_entry(l, struct handle_list, hl_entry);
  214. handle = hl->hl_handle;
  215. error = saHandleInstanceGet(&channel_handle_db, hl->hl_handle,
  216. (void*)&eci);
  217. if (error != SA_AIS_OK) {
  218. /*
  219. * already gone
  220. */
  221. continue;
  222. }
  223. saHandleDestroy(&channel_handle_db, handle);
  224. saHandleInstancePut(&channel_handle_db, handle);
  225. }
  226. }
  227. /*
  228. * Clean up function for an open channel handle
  229. */
  230. static void chanHandleInstanceDestructor(void *instance)
  231. {
  232. struct event_channel_instance *eci = instance;
  233. struct list_head *l, *nxt;
  234. struct handle_list *hl;
  235. uint64_t handle;
  236. if (eci->eci_hl) {
  237. list_del(&eci->eci_hl->hl_entry);
  238. free(eci->eci_hl);
  239. eci->eci_hl = 0;
  240. }
  241. /*
  242. * Free up any channel associated events
  243. */
  244. for (l = eci->eci_event_list.next; l != &eci->eci_event_list; l = nxt) {
  245. nxt = l->next;
  246. hl = list_entry(l, struct handle_list, hl_entry);
  247. handle = hl->hl_handle;
  248. saEvtEventFree(handle);
  249. }
  250. }
  251. /*
  252. * Clean up function for an event handle
  253. */
  254. static void eventHandleInstanceDestructor(void *instance)
  255. {
  256. struct event_data_instance *edi = instance;
  257. int i;
  258. if (edi->edi_hl) {
  259. list_del(&edi->edi_hl->hl_entry);
  260. free(edi->edi_hl);
  261. edi->edi_hl = 0;
  262. }
  263. if (edi->edi_patterns.patterns) {
  264. for (i = 0; i < edi->edi_patterns.patternsNumber; i++) {
  265. free(edi->edi_patterns.patterns[i].pattern);
  266. }
  267. free(edi->edi_patterns.patterns);
  268. }
  269. if (edi->edi_event_data) {
  270. free(edi->edi_event_data);
  271. }
  272. }
  273. static SaAisErrorT evt_recv_event(int fd, struct lib_event_data **msg)
  274. {
  275. SaAisErrorT error;
  276. struct res_header hdr;
  277. void *data;
  278. error = saRecvRetry(fd, &hdr, sizeof(hdr));
  279. if (error != SA_AIS_OK) {
  280. goto msg_out;
  281. }
  282. *msg = malloc(hdr.size);
  283. if (!*msg) {
  284. error = SA_AIS_ERR_LIBRARY;
  285. goto msg_out;
  286. }
  287. data = (void *)((unsigned long)*msg) + sizeof(hdr);
  288. memcpy(*msg, &hdr, sizeof(hdr));
  289. if (hdr.size > sizeof(hdr)) {
  290. error = saRecvRetry(fd, data, hdr.size - sizeof(hdr));
  291. if (error != SA_AIS_OK) {
  292. goto msg_out;
  293. }
  294. }
  295. msg_out:
  296. return error;
  297. }
  298. /*
  299. * The saEvtInitialize() function initializes the Event Service for the
  300. * invoking process. A user of the Event Service must invoke this function
  301. * before it invokes any other function of the Event Service API. Each
  302. * initialization returns a different callback handle that the process
  303. * can use to communicate with that library instance.
  304. */
  305. SaAisErrorT
  306. saEvtInitialize(
  307. SaEvtHandleT *evtHandle,
  308. const SaEvtCallbacksT *callbacks,
  309. SaVersionT *version)
  310. {
  311. SaAisErrorT error = SA_AIS_OK;
  312. struct event_instance *evti;
  313. if (!version || !evtHandle) {
  314. error = SA_AIS_ERR_INVALID_PARAM;
  315. goto error_nofree;
  316. }
  317. /*
  318. * validate the requested version with what we support
  319. */
  320. error = saVersionVerify(&evt_version_database, version);
  321. if (error != SA_AIS_OK) {
  322. goto error_nofree;
  323. }
  324. /*
  325. * Allocate instance data, allocate unique handle for instance,
  326. * assign instance data to unique handle
  327. */
  328. error = saHandleCreate(&evt_instance_handle_db, sizeof(*evti),
  329. evtHandle);
  330. if (error != SA_AIS_OK) {
  331. goto error_nofree;
  332. }
  333. error = saHandleInstanceGet(&evt_instance_handle_db, *evtHandle,
  334. (void*)&evti);
  335. if (error != SA_AIS_OK) {
  336. if (error == SA_AIS_ERR_BAD_HANDLE) {
  337. error = SA_AIS_ERR_LIBRARY;
  338. }
  339. goto error_handle_free;
  340. }
  341. memset(evti, 0, sizeof(*evti));
  342. list_init(&evti->ei_channel_list);
  343. /*
  344. * Save the version so we can check with the event server
  345. * and see if it supports this version.
  346. */
  347. evti->ei_version = *version;
  348. /*
  349. * Set up communication with the event server
  350. */
  351. error = saServiceConnectTwo(&evti->ei_response_fd,
  352. &evti->ei_dispatch_fd, EVT_SERVICE);
  353. if (error != SA_AIS_OK) {
  354. goto error_handle_put;
  355. }
  356. /*
  357. * The callback function is saved in the event instance for
  358. * saEvtDispatch() to use.
  359. */
  360. if (callbacks) {
  361. memcpy(&evti->ei_callback, callbacks,
  362. sizeof(evti->ei_callback));
  363. }
  364. pthread_mutex_init(&evti->ei_dispatch_mutex, NULL);
  365. pthread_mutex_init(&evti->ei_response_mutex, NULL);
  366. saHandleInstancePut(&evt_instance_handle_db, *evtHandle);
  367. return SA_AIS_OK;
  368. error_handle_put:
  369. saHandleInstancePut(&evt_instance_handle_db, *evtHandle);
  370. error_handle_free:
  371. (void)saHandleDestroy(&evt_instance_handle_db, *evtHandle);
  372. error_nofree:
  373. return error;
  374. }
  375. /*
  376. * The saEvtSelectionObjectGet() function returns the operating system
  377. * handle selectionObject, associated with the handle evtHandle, allowing
  378. * the invoking process to ascertain when callbacks are pending. This
  379. * function allows a process to avoid repeated invoking saEvtDispatch() to
  380. * see if there is a new event, thus, needlessly consuming CPU time. In a
  381. * POSIX environment the system handle could be a file descriptor that is
  382. * used with the poll() or select() system calls to detect incoming callbacks.
  383. */
  384. SaAisErrorT
  385. saEvtSelectionObjectGet(
  386. SaEvtHandleT evtHandle,
  387. SaSelectionObjectT *selectionObject)
  388. {
  389. struct event_instance *evti;
  390. SaAisErrorT error;
  391. if (!selectionObject) {
  392. return SA_AIS_ERR_INVALID_PARAM;
  393. }
  394. error = saHandleInstanceGet(&evt_instance_handle_db, evtHandle,
  395. (void *)&evti);
  396. if (error != SA_AIS_OK) {
  397. return error;
  398. }
  399. *selectionObject = evti->ei_dispatch_fd;
  400. saHandleInstancePut(&evt_instance_handle_db, evtHandle);
  401. return SA_AIS_OK;
  402. }
  403. /*
  404. * Alocate an event data structure and associated handle to be
  405. * used to supply event data to a call back function.
  406. */
  407. static SaAisErrorT make_event(SaEvtEventHandleT *event_handle,
  408. struct lib_event_data *evt)
  409. {
  410. struct event_data_instance *edi;
  411. struct event_channel_instance *eci;
  412. SaEvtEventPatternT *pat;
  413. SaUint8T *str;
  414. SaAisErrorT error;
  415. struct handle_list *hl;
  416. int i;
  417. error = saHandleCreate(&event_handle_db, sizeof(*edi),
  418. event_handle);
  419. if (error != SA_AIS_OK) {
  420. if (error == SA_AIS_ERR_NO_MEMORY) {
  421. error = SA_AIS_ERR_LIBRARY;
  422. }
  423. goto make_evt_done;
  424. }
  425. error = saHandleInstanceGet(&event_handle_db, *event_handle,
  426. (void*)&edi);
  427. if (error != SA_AIS_OK) {
  428. saHandleDestroy(&event_handle_db, *event_handle);
  429. goto make_evt_done;
  430. }
  431. error = saHandleInstanceGet(&channel_handle_db,
  432. evt->led_lib_channel_handle,
  433. (void*)&eci);
  434. if (error != SA_AIS_OK) {
  435. saHandleDestroy(&event_handle_db, *event_handle);
  436. goto make_evt_done_put;
  437. }
  438. pthread_mutex_init(&edi->edi_mutex, NULL);
  439. edi->edi_ro = 1;
  440. edi->edi_freeing = 0;
  441. edi->edi_channel_handle = evt->led_lib_channel_handle;
  442. edi->edi_priority = evt->led_priority;
  443. edi->edi_retention_time = evt->led_retention_time;
  444. edi->edi_pub_node = evt->led_publisher_node_id;
  445. edi->edi_pub_time = evt->led_publish_time;
  446. edi->edi_event_data_size = evt->led_user_data_size;
  447. edi->edi_event_id = evt->led_event_id;
  448. edi->edi_pub_name = evt->led_publisher_name;
  449. if (edi->edi_event_data_size) {
  450. edi->edi_event_data = malloc(edi->edi_event_data_size);
  451. if (!edi->edi_event_data) {
  452. saHandleDestroy(&event_handle_db, *event_handle);
  453. /*
  454. * saEvtDispatch doesn't return SA_AIS_ERR_NO_MEMORY
  455. */
  456. error = SA_AIS_ERR_LIBRARY;
  457. goto make_evt_done_put2;
  458. }
  459. memcpy(edi->edi_event_data,
  460. evt->led_body + evt->led_user_data_offset,
  461. edi->edi_event_data_size);
  462. }
  463. /*
  464. * Move the pattern bits into the SaEvtEventPatternArrayT
  465. */
  466. edi->edi_patterns.patternsNumber = evt->led_patterns_number;
  467. edi->edi_patterns.allocatedNumber = evt->led_patterns_number;
  468. edi->edi_patterns.patterns = malloc(sizeof(SaEvtEventPatternT) *
  469. edi->edi_patterns.patternsNumber);
  470. if (!edi->edi_patterns.patterns) {
  471. /*
  472. * The destructor will take care of freeing event data already
  473. * allocated.
  474. */
  475. edi->edi_patterns.patternsNumber = 0;
  476. saHandleDestroy(&event_handle_db, *event_handle);
  477. /*
  478. * saEvtDispatch doesn't return SA_AIS_ERR_NO_MEMORY
  479. */
  480. error = SA_AIS_ERR_LIBRARY;
  481. goto make_evt_done_put2;
  482. }
  483. memset(edi->edi_patterns.patterns, 0, sizeof(SaEvtEventPatternT) *
  484. edi->edi_patterns.patternsNumber);
  485. pat = (SaEvtEventPatternT *)evt->led_body;
  486. str = evt->led_body + sizeof(SaEvtEventPatternT) *
  487. edi->edi_patterns.patternsNumber;
  488. for (i = 0; i < evt->led_patterns_number; i++) {
  489. edi->edi_patterns.patterns[i].patternSize = pat->patternSize;
  490. edi->edi_patterns.patterns[i].allocatedSize = pat->patternSize;
  491. edi->edi_patterns.patterns[i].pattern = malloc(pat->patternSize);
  492. if (!edi->edi_patterns.patterns[i].pattern) {
  493. printf("make_event: couldn't alloc %llu bytes\n",
  494. (unsigned long long)pat->patternSize);
  495. saHandleDestroy(&event_handle_db, *event_handle);
  496. error = SA_AIS_ERR_LIBRARY;
  497. goto make_evt_done_put2;
  498. }
  499. memcpy(edi->edi_patterns.patterns[i].pattern,
  500. str, pat->patternSize);
  501. str += pat->patternSize;
  502. pat++;
  503. }
  504. hl = malloc(sizeof(*hl));
  505. if (!hl) {
  506. saHandleDestroy(&event_handle_db, *event_handle);
  507. error = SA_AIS_ERR_LIBRARY;
  508. } else {
  509. edi->edi_hl = hl;
  510. hl->hl_handle = *event_handle;
  511. list_init(&hl->hl_entry);
  512. list_add(&hl->hl_entry, &eci->eci_event_list);
  513. }
  514. make_evt_done_put2:
  515. saHandleInstancePut (&channel_handle_db, evt->led_lib_channel_handle);
  516. make_evt_done_put:
  517. saHandleInstancePut (&event_handle_db, *event_handle);
  518. make_evt_done:
  519. return error;
  520. }
  521. /*
  522. * The saEvtDispatch() function invokes, in the context of the calling
  523. * thread, one or all of the pending callbacks for the handle evtHandle.
  524. */
  525. SaAisErrorT
  526. saEvtDispatch(
  527. SaEvtHandleT evtHandle,
  528. SaDispatchFlagsT dispatchFlags)
  529. {
  530. struct pollfd ufds;
  531. int timeout = -1;
  532. SaAisErrorT error;
  533. int dispatch_avail;
  534. struct event_instance *evti;
  535. SaEvtEventHandleT event_handle;
  536. SaEvtCallbacksT callbacks;
  537. int ignore_dispatch = 0;
  538. int cont = 1; /* always continue do loop except when set to 0 */
  539. int poll_fd;
  540. struct res_overlay dispatch_data;
  541. struct lib_event_data *evt = 0;
  542. struct res_evt_event_data res;
  543. if (dispatchFlags < SA_DISPATCH_ONE ||
  544. dispatchFlags > SA_DISPATCH_BLOCKING) {
  545. return SA_AIS_ERR_INVALID_PARAM;
  546. }
  547. error = saHandleInstanceGet(&evt_instance_handle_db, evtHandle,
  548. (void *)&evti);
  549. if (error != SA_AIS_OK) {
  550. return error;
  551. }
  552. /*
  553. * Timeout instantly for SA_DISPATCH_ALL
  554. */
  555. if (dispatchFlags == SA_DISPATCH_ALL || dispatchFlags == SA_DISPATCH_ONE) {
  556. timeout = 0;
  557. }
  558. do {
  559. poll_fd = evti->ei_dispatch_fd;
  560. ufds.fd = poll_fd;
  561. ufds.events = POLLIN;
  562. ufds.revents = 0;
  563. error = saPollRetry(&ufds, 1, timeout);
  564. if (error != SA_AIS_OK) {
  565. goto dispatch_put;
  566. }
  567. pthread_mutex_lock(&evti->ei_dispatch_mutex);
  568. /*
  569. * Handle has been finalized in another thread
  570. */
  571. if (evti->ei_finalize == 1) {
  572. error = SA_AIS_OK;
  573. goto dispatch_unlock;
  574. }
  575. /*
  576. * If we know that we have an event waiting, we can skip the
  577. * polling and just ask for it.
  578. */
  579. if (!evti->ei_data_available) {
  580. /*
  581. * Check the poll data in case the fd status has changed
  582. * since taking the lock
  583. */
  584. error = saPollRetry(&ufds, 1, 0);
  585. if (error != SA_AIS_OK) {
  586. goto dispatch_unlock;
  587. }
  588. if ((ufds.revents & (POLLERR|POLLHUP|POLLNVAL)) != 0) {
  589. error = SA_AIS_ERR_BAD_HANDLE;
  590. goto dispatch_unlock;
  591. }
  592. dispatch_avail = ufds.revents & POLLIN;
  593. if (dispatch_avail == 0 &&
  594. (dispatchFlags == SA_DISPATCH_ALL ||
  595. dispatchFlags == SA_DISPATCH_ONE)) {
  596. pthread_mutex_unlock(&evti->ei_dispatch_mutex);
  597. break; /* exit do while cont is 1 loop */
  598. } else if (dispatch_avail == 0) {
  599. pthread_mutex_unlock(&evti->ei_dispatch_mutex);
  600. continue; /* next poll */
  601. }
  602. if (ufds.revents & POLLIN) {
  603. error = saRecvRetry (evti->ei_dispatch_fd, &dispatch_data.header,
  604. sizeof (struct res_header));
  605. if (error != SA_AIS_OK) {
  606. goto dispatch_unlock;
  607. }
  608. if (dispatch_data.header.size > sizeof (struct res_header)) {
  609. error = saRecvRetry (evti->ei_dispatch_fd, &dispatch_data.data,
  610. dispatch_data.header.size - sizeof (struct res_header));
  611. if (error != SA_AIS_OK) {
  612. goto dispatch_unlock;
  613. }
  614. }
  615. } else {
  616. pthread_mutex_unlock(&evti->ei_dispatch_mutex);
  617. continue;
  618. }
  619. } else {
  620. /*
  621. * We know that we have an event available from before.
  622. * Fake up a header message and the switch statement will
  623. * take care of the rest.
  624. */
  625. dispatch_data.header.id = MESSAGE_RES_EVT_AVAILABLE;
  626. }
  627. /*
  628. * Make copy of callbacks, message data, unlock instance,
  629. * and call callback. A risk of this dispatch method is that
  630. * the callback routines may operate at the same time that
  631. * EvtFinalize has been called in another thread.
  632. */
  633. memcpy(&callbacks, &evti->ei_callback, sizeof(evti->ei_callback));
  634. pthread_mutex_unlock(&evti->ei_dispatch_mutex);
  635. /*
  636. * Dispatch incoming response
  637. */
  638. switch (dispatch_data.header.id) {
  639. case MESSAGE_RES_EVT_AVAILABLE:
  640. evti->ei_data_available = 0;
  641. /*
  642. * There are events available. Send a request for one and then
  643. * dispatch it.
  644. */
  645. res.evd_head.id = MESSAGE_REQ_EVT_EVENT_DATA;
  646. res.evd_head.size = sizeof(res);
  647. pthread_mutex_lock(&evti->ei_response_mutex);
  648. error = saSendRetry(evti->ei_response_fd, &res, sizeof(res));
  649. if (error != SA_AIS_OK) {
  650. printf("MESSAGE_RES_EVT_AVAILABLE: send failed: %d\n", error);
  651. pthread_mutex_unlock(&evti->ei_response_mutex);
  652. break;
  653. }
  654. error = evt_recv_event(evti->ei_response_fd, &evt);
  655. pthread_mutex_unlock(&evti->ei_response_mutex);
  656. if (error != SA_AIS_OK) {
  657. printf("MESSAGE_RES_EVT_AVAILABLE: receive failed: %d\n",
  658. error);
  659. break;
  660. }
  661. /*
  662. * No data available. This is OK, another thread may have
  663. * grabbed it.
  664. */
  665. if (evt->led_head.error == SA_AIS_ERR_NOT_EXIST) {
  666. // printf("MESSAGE_RES_EVT_AVAILABLE: No event data\n");
  667. error = SA_AIS_OK;
  668. break;
  669. }
  670. if (evt->led_head.error != SA_AIS_OK) {
  671. error = evt->led_head.error;
  672. /*
  673. * If we get a try again response, we've lost the poll event
  674. * so we have a data available flag so that we know that there
  675. * really is an event waiting the next time dispatch gets
  676. * called.
  677. */
  678. if (error == SA_AIS_ERR_TRY_AGAIN) {
  679. evti->ei_data_available = 1;
  680. } else {
  681. printf("MESSAGE_RES_EVT_AVAILABLE: Error returned: %d\n",
  682. error);
  683. }
  684. break;
  685. }
  686. error = make_event(&event_handle, evt);
  687. if (error != SA_AIS_OK) {
  688. break;
  689. }
  690. /*
  691. * Only call if there was a function registered
  692. */
  693. if (callbacks.saEvtEventDeliverCallback) {
  694. callbacks.saEvtEventDeliverCallback(evt->led_sub_id,
  695. event_handle, evt->led_user_data_size);
  696. }
  697. break;
  698. case MESSAGE_RES_EVT_CHAN_OPEN_CALLBACK:
  699. {
  700. struct res_evt_open_chan_async *resa =
  701. (struct res_evt_open_chan_async *)&dispatch_data;
  702. struct event_channel_instance *eci;
  703. /*
  704. * Check for errors. If there are none, then
  705. * look up the local channel via the handle that we
  706. * got from the callback request. All we need to do
  707. * is place in the handle from the server side and then
  708. * we can call the callback.
  709. */
  710. error = resa->ica_head.error;
  711. if (error == SA_AIS_OK) {
  712. error = saHandleInstanceGet(&channel_handle_db,
  713. resa->ica_c_handle, (void*)&eci);
  714. if (error == SA_AIS_OK) {
  715. eci->eci_svr_channel_handle = resa->ica_channel_handle;
  716. saHandleInstancePut (&channel_handle_db,
  717. resa->ica_c_handle);
  718. }
  719. }
  720. /*
  721. * Only call if there was a function registered
  722. */
  723. if (callbacks.saEvtChannelOpenCallback) {
  724. callbacks.saEvtChannelOpenCallback(resa->ica_invocation,
  725. resa->ica_c_handle, error);
  726. }
  727. }
  728. break;
  729. default:
  730. printf("Dispatch: Bad message type 0x%x\n",
  731. dispatch_data.header.id);
  732. error = SA_AIS_ERR_LIBRARY;
  733. goto dispatch_put;
  734. }
  735. /*
  736. * If empty is zero it means the we got the
  737. * message from the queue and we are responsible
  738. * for freeing it.
  739. */
  740. if (evt) {
  741. free(evt);
  742. evt = 0;
  743. }
  744. /*
  745. * Determine if more messages should be processed
  746. */
  747. switch (dispatchFlags) {
  748. case SA_DISPATCH_ONE:
  749. if (ignore_dispatch) {
  750. ignore_dispatch = 0;
  751. } else {
  752. cont = 0;
  753. }
  754. break;
  755. case SA_DISPATCH_ALL:
  756. if (ignore_dispatch) {
  757. ignore_dispatch = 0;
  758. }
  759. break;
  760. case SA_DISPATCH_BLOCKING:
  761. break;
  762. }
  763. } while (cont);
  764. goto dispatch_put;
  765. dispatch_unlock:
  766. pthread_mutex_unlock(&evti->ei_dispatch_mutex);
  767. dispatch_put:
  768. saHandleInstancePut(&evt_instance_handle_db, evtHandle);
  769. return error;
  770. }
  771. /*
  772. * The saEvtFinalize() function closes the association, represented by the
  773. * evtHandle parameter, between the process and the Event Service. It may
  774. * free up resources.
  775. * This function cannot be invoked before the process has invoked the
  776. * corresponding saEvtInitialize() function for the Event Service. After
  777. * this function is invoked, the selection object is no longer valid.
  778. * Moreover, the Event Service is unavailable for further use unless it is
  779. * reinitialized using the saEvtInitialize() function.
  780. */
  781. SaAisErrorT
  782. saEvtFinalize(SaEvtHandleT evtHandle)
  783. {
  784. struct event_instance *evti;
  785. SaAisErrorT error;
  786. error = saHandleInstanceGet(&evt_instance_handle_db, evtHandle,
  787. (void *)&evti);
  788. if (error != SA_AIS_OK) {
  789. return error;
  790. }
  791. pthread_mutex_lock(&evti->ei_response_mutex);
  792. /*
  793. * Another thread has already started finalizing
  794. */
  795. if (evti->ei_finalize) {
  796. pthread_mutex_unlock(&evti->ei_response_mutex);
  797. saHandleInstancePut(&evt_instance_handle_db, evtHandle);
  798. return SA_AIS_ERR_BAD_HANDLE;
  799. }
  800. evti->ei_finalize = 1;
  801. pthread_mutex_unlock(&evti->ei_response_mutex);
  802. saHandleDestroy(&evt_instance_handle_db, evtHandle);
  803. /*
  804. * Disconnect from the server
  805. */
  806. if (evti->ei_response_fd != -1) {
  807. shutdown(evti->ei_response_fd, 0);
  808. close(evti->ei_response_fd);
  809. }
  810. if (evti->ei_dispatch_fd != -1) {
  811. shutdown(evti->ei_dispatch_fd, 0);
  812. close(evti->ei_dispatch_fd);
  813. }
  814. saHandleInstancePut(&evt_instance_handle_db, evtHandle);
  815. return error;
  816. }
  817. /*
  818. * The saEvtChannelOpen() function creates a new event channel or open an
  819. * existing channel. The saEvtChannelOpen() function is a blocking operation
  820. * and returns a new event channel handle. An event channel may be opened
  821. * multiple times by the same or different processes for publishing, and
  822. * subscribing to, events. If a process opens an event channel multiple
  823. * times, it is possible to receive the same event multiple times. However,
  824. * a process shall never receive an event more than once on a particular
  825. * event channel handle. If a process opens a channel twice and an event is
  826. * matched on both open channels, the Event Service performs two
  827. * callbacks -- one for each opened channel.
  828. */
  829. SaAisErrorT
  830. saEvtChannelOpen(
  831. SaEvtHandleT evtHandle,
  832. const SaNameT *channelName,
  833. SaEvtChannelOpenFlagsT channelOpenFlags,
  834. SaTimeT timeout,
  835. SaEvtChannelHandleT *channelHandle)
  836. {
  837. struct event_instance *evti;
  838. struct req_evt_channel_open req;
  839. struct res_evt_channel_open res;
  840. struct event_channel_instance *eci;
  841. struct handle_list *hl;
  842. SaAisErrorT error;
  843. struct iovec iov;
  844. if (!channelHandle || !channelName) {
  845. return SA_AIS_ERR_INVALID_PARAM;
  846. }
  847. if ((channelOpenFlags & ~(SA_EVT_CHANNEL_CREATE|SA_EVT_CHANNEL_PUBLISHER|
  848. SA_EVT_CHANNEL_SUBSCRIBER)) != 0) {
  849. return SA_AIS_ERR_BAD_FLAGS;
  850. }
  851. error = saHandleInstanceGet(&evt_instance_handle_db, evtHandle,
  852. (void*)&evti);
  853. if (error != SA_AIS_OK) {
  854. goto chan_open_done;
  855. }
  856. /*
  857. * create a handle for this open channel
  858. */
  859. error = saHandleCreate(&channel_handle_db, sizeof(*eci),
  860. channelHandle);
  861. if (error != SA_AIS_OK) {
  862. goto chan_open_put;
  863. }
  864. error = saHandleInstanceGet(&channel_handle_db, *channelHandle,
  865. (void*)&eci);
  866. if (error != SA_AIS_OK) {
  867. saHandleDestroy(&channel_handle_db, *channelHandle);
  868. goto chan_open_put;
  869. }
  870. list_init(&eci->eci_event_list);
  871. /*
  872. * Send the request to the server and wait for a response
  873. */
  874. req.ico_head.size = sizeof(req);
  875. req.ico_head.id = MESSAGE_REQ_EVT_OPEN_CHANNEL;
  876. req.ico_c_handle = *channelHandle;
  877. req.ico_timeout = timeout;
  878. req.ico_open_flag = channelOpenFlags;
  879. req.ico_channel_name = *channelName;
  880. iov.iov_base = &req;
  881. iov.iov_len = sizeof(req);
  882. pthread_mutex_lock(&evti->ei_response_mutex);
  883. error = saSendMsgReceiveReply(evti->ei_response_fd, &iov, 1,
  884. &res, sizeof(res));
  885. pthread_mutex_unlock (&evti->ei_response_mutex);
  886. if (error != SA_AIS_OK) {
  887. goto chan_open_free;
  888. }
  889. if (res.ico_head.id != MESSAGE_RES_EVT_OPEN_CHANNEL) {
  890. error = SA_AIS_ERR_LIBRARY;
  891. goto chan_open_free;
  892. }
  893. error = res.ico_head.error;
  894. if (error != SA_AIS_OK) {
  895. goto chan_open_free;
  896. }
  897. eci->eci_svr_channel_handle = res.ico_channel_handle;
  898. eci->eci_channel_name = *channelName;
  899. eci->eci_open_flags = channelOpenFlags;
  900. eci->eci_instance_handle = evtHandle;
  901. eci->eci_closing = 0;
  902. hl = malloc(sizeof(*hl));
  903. if (!hl) {
  904. error = SA_AIS_ERR_NO_MEMORY;
  905. goto chan_open_free;
  906. }
  907. eci->eci_hl = hl;
  908. hl->hl_handle = *channelHandle;
  909. list_init(&hl->hl_entry);
  910. list_add(&hl->hl_entry, &evti->ei_channel_list);
  911. pthread_mutex_init(&eci->eci_mutex, NULL);
  912. saHandleInstancePut (&evt_instance_handle_db, evtHandle);
  913. saHandleInstancePut (&channel_handle_db, *channelHandle);
  914. return SA_AIS_OK;
  915. chan_open_free:
  916. saHandleDestroy(&channel_handle_db, *channelHandle);
  917. saHandleInstancePut (&channel_handle_db, *channelHandle);
  918. chan_open_put:
  919. saHandleInstancePut (&evt_instance_handle_db, evtHandle);
  920. chan_open_done:
  921. return error;
  922. }
  923. /*
  924. * The saEvtChannelClose() function closes an event channel and frees
  925. * resources allocated for that event channel in the invoking process.
  926. */
  927. SaAisErrorT
  928. saEvtChannelClose(SaEvtChannelHandleT channelHandle)
  929. {
  930. SaAisErrorT error;
  931. struct event_instance *evti;
  932. struct event_channel_instance *eci;
  933. struct req_evt_channel_close req;
  934. struct res_evt_channel_close res;
  935. struct iovec iov;
  936. error = saHandleInstanceGet(&channel_handle_db, channelHandle,
  937. (void*)&eci);
  938. if (error != SA_AIS_OK) {
  939. goto chan_close_done;
  940. }
  941. /*
  942. * get the evt handle for the fd
  943. */
  944. error = saHandleInstanceGet(&evt_instance_handle_db,
  945. eci->eci_instance_handle, (void*)&evti);
  946. if (error != SA_AIS_OK) {
  947. goto chan_close_put1;
  948. }
  949. /*
  950. * Make sure that the channel isn't being closed elsewhere
  951. */
  952. pthread_mutex_lock(&eci->eci_mutex);
  953. if (eci->eci_closing) {
  954. pthread_mutex_unlock(&eci->eci_mutex);
  955. saHandleInstancePut(&channel_handle_db, channelHandle);
  956. return SA_AIS_ERR_BAD_HANDLE;
  957. }
  958. eci->eci_closing = 1;
  959. pthread_mutex_unlock(&eci->eci_mutex);
  960. /*
  961. * Send the request to the server and wait for a response
  962. */
  963. req.icc_head.size = sizeof(req);
  964. req.icc_head.id = MESSAGE_REQ_EVT_CLOSE_CHANNEL;
  965. req.icc_channel_handle = eci->eci_svr_channel_handle;
  966. iov.iov_base = &req;
  967. iov.iov_len = sizeof (req);
  968. pthread_mutex_lock(&evti->ei_response_mutex);
  969. error = saSendMsgReceiveReply (evti->ei_response_fd, &iov, 1,
  970. &res, sizeof (res));
  971. pthread_mutex_unlock(&evti->ei_response_mutex);
  972. if (error != SA_AIS_OK) {
  973. eci->eci_closing = 0;
  974. goto chan_close_put2;
  975. }
  976. if (res.icc_head.id != MESSAGE_RES_EVT_CLOSE_CHANNEL) {
  977. error = SA_AIS_ERR_LIBRARY;
  978. eci->eci_closing = 0;
  979. goto chan_close_put2;
  980. }
  981. error = res.icc_head.error;
  982. if (error == SA_AIS_ERR_TRY_AGAIN) {
  983. pthread_mutex_lock(&eci->eci_mutex);
  984. eci->eci_closing = 0;
  985. pthread_mutex_unlock(&eci->eci_mutex);
  986. goto chan_close_put2;
  987. }
  988. saHandleInstancePut(&evt_instance_handle_db,
  989. eci->eci_instance_handle);
  990. saHandleDestroy(&channel_handle_db, channelHandle);
  991. saHandleInstancePut(&channel_handle_db, channelHandle);
  992. return error;
  993. chan_close_put2:
  994. saHandleInstancePut(&evt_instance_handle_db,
  995. eci->eci_instance_handle);
  996. chan_close_put1:
  997. saHandleInstancePut(&channel_handle_db, channelHandle);
  998. chan_close_done:
  999. return error;
  1000. }
  1001. /*
  1002. * The saEvtChannelOpenAsync() function creates a new event channel or open an
  1003. * existing channel. The saEvtChannelOpenAsync() function is a non-blocking
  1004. * operation. A new event channel handle is returned in the channel open
  1005. * callback function (SaEvtChannelOpenCallbackT).
  1006. */
  1007. SaAisErrorT
  1008. saEvtChannelOpenAsync(SaEvtHandleT evtHandle,
  1009. SaInvocationT invocation,
  1010. const SaNameT *channelName,
  1011. SaEvtChannelOpenFlagsT channelOpenFlags)
  1012. {
  1013. struct event_instance *evti;
  1014. struct req_evt_channel_open req;
  1015. struct res_evt_channel_open res;
  1016. struct event_channel_instance *eci;
  1017. SaEvtChannelHandleT channel_handle;
  1018. SaAisErrorT error;
  1019. struct handle_list *hl;
  1020. struct iovec iov;
  1021. if (!channelName) {
  1022. return SA_AIS_ERR_INVALID_PARAM;
  1023. }
  1024. if ((channelOpenFlags & ~(SA_EVT_CHANNEL_CREATE|SA_EVT_CHANNEL_PUBLISHER|
  1025. SA_EVT_CHANNEL_SUBSCRIBER)) != 0) {
  1026. return SA_AIS_ERR_BAD_FLAGS;
  1027. }
  1028. error = saHandleInstanceGet(&evt_instance_handle_db, evtHandle,
  1029. (void*)&evti);
  1030. if (error != SA_AIS_OK) {
  1031. goto chan_open_done;
  1032. }
  1033. /*
  1034. * Make sure that an open channel callback has been
  1035. * registered before allowing the open to continue.
  1036. */
  1037. if (!evti->ei_callback.saEvtChannelOpenCallback) {
  1038. error = SA_AIS_ERR_INIT;
  1039. goto chan_open_put;
  1040. }
  1041. /*
  1042. * create a handle for this open channel
  1043. */
  1044. error = saHandleCreate(&channel_handle_db, sizeof(*eci),
  1045. &channel_handle);
  1046. if (error != SA_AIS_OK) {
  1047. goto chan_open_put;
  1048. }
  1049. error = saHandleInstanceGet(&channel_handle_db, channel_handle,
  1050. (void*)&eci);
  1051. if (error != SA_AIS_OK) {
  1052. saHandleDestroy(&channel_handle_db, channel_handle);
  1053. goto chan_open_put;
  1054. }
  1055. list_init(&eci->eci_event_list);
  1056. /*
  1057. * Send the request to the server. The response isn't the open channel,
  1058. * just an ack. The open channel will be returned when the channel open
  1059. * callback is called.
  1060. */
  1061. req.ico_head.size = sizeof(req);
  1062. req.ico_head.id = MESSAGE_REQ_EVT_OPEN_CHANNEL_ASYNC;
  1063. req.ico_c_handle = channel_handle;
  1064. req.ico_timeout = 0;
  1065. req.ico_invocation = invocation;
  1066. req.ico_open_flag = channelOpenFlags;
  1067. req.ico_channel_name = *channelName;
  1068. iov.iov_base = &req;
  1069. iov.iov_len = sizeof(req);
  1070. pthread_mutex_lock(&evti->ei_response_mutex);
  1071. error = saSendMsgReceiveReply (evti->ei_response_fd, &iov, 1,
  1072. &res, sizeof (res));
  1073. pthread_mutex_unlock(&evti->ei_response_mutex);
  1074. if (error != SA_AIS_OK) {
  1075. goto chan_open_free;
  1076. }
  1077. if (res.ico_head.id != MESSAGE_RES_EVT_OPEN_CHANNEL) {
  1078. error = SA_AIS_ERR_LIBRARY;
  1079. goto chan_open_free;
  1080. }
  1081. error = res.ico_head.error;
  1082. if (error != SA_AIS_OK) {
  1083. goto chan_open_free;
  1084. }
  1085. eci->eci_svr_channel_handle = 0; /* filled in by callback */
  1086. eci->eci_channel_name = *channelName;
  1087. eci->eci_open_flags = channelOpenFlags;
  1088. eci->eci_instance_handle = evtHandle;
  1089. eci->eci_closing = 0;
  1090. list_init(&eci->eci_event_list);
  1091. hl = malloc(sizeof(*hl));
  1092. if (!hl) {
  1093. error = SA_AIS_ERR_NO_MEMORY;
  1094. goto chan_open_free;
  1095. }
  1096. eci->eci_hl = hl;
  1097. hl->hl_handle = channel_handle;
  1098. list_init(&hl->hl_entry);
  1099. list_add(&hl->hl_entry, &evti->ei_channel_list);
  1100. pthread_mutex_init(&eci->eci_mutex, NULL);
  1101. saHandleInstancePut (&evt_instance_handle_db, evtHandle);
  1102. saHandleInstancePut (&channel_handle_db, channel_handle);
  1103. return SA_AIS_OK;
  1104. chan_open_free:
  1105. saHandleDestroy(&channel_handle_db, channel_handle);
  1106. saHandleInstancePut (&channel_handle_db, channel_handle);
  1107. chan_open_put:
  1108. saHandleInstancePut (&evt_instance_handle_db, evtHandle);
  1109. chan_open_done:
  1110. return error;
  1111. }
  1112. /*
  1113. * The SaEvtChannelUnlink function deletes an existing event channel
  1114. * from the cluster.
  1115. *
  1116. * After completion of the invocation:
  1117. * - An open of the channel name without a create will fail.
  1118. * - The channel remains available to any already opened instances.
  1119. * - If an open/create is performed on this channel name a new instance
  1120. * is created.
  1121. * - The ulinked channel's resources will be deleted when the last open
  1122. * instance is closed.
  1123. *
  1124. * Note that an event channel is only deleted from the cluster
  1125. * namespace when saEvtChannelUnlink() is invoked on it. The deletion
  1126. * of an event channel frees all resources allocated by the Event
  1127. * Service for it, such as published events with non-zero retention
  1128. * time.
  1129. */
  1130. SaAisErrorT
  1131. saEvtChannelUnlink(
  1132. SaEvtHandleT evtHandle,
  1133. const SaNameT *channelName)
  1134. {
  1135. struct event_instance *evti;
  1136. struct req_evt_channel_unlink req;
  1137. struct res_evt_channel_unlink res;
  1138. struct iovec iov;
  1139. SaAisErrorT error;
  1140. if (!channelName) {
  1141. return SA_AIS_ERR_INVALID_PARAM;
  1142. }
  1143. error = saHandleInstanceGet(&evt_instance_handle_db, evtHandle,
  1144. (void*)&evti);
  1145. if (error != SA_AIS_OK) {
  1146. goto chan_unlink_done;
  1147. }
  1148. /*
  1149. * Send the request to the server and wait for a response
  1150. */
  1151. req.iuc_head.size = sizeof(req);
  1152. req.iuc_head.id = MESSAGE_REQ_EVT_UNLINK_CHANNEL;
  1153. req.iuc_channel_name = *channelName;
  1154. iov.iov_base = &req;
  1155. iov.iov_len = sizeof(req);
  1156. pthread_mutex_lock(&evti->ei_response_mutex);
  1157. error = saSendMsgReceiveReply (evti->ei_response_fd, &iov, 1,
  1158. &res, sizeof (res));
  1159. pthread_mutex_unlock(&evti->ei_response_mutex);
  1160. if (error != SA_AIS_OK) {
  1161. goto chan_unlink_put;
  1162. }
  1163. if (res.iuc_head.id != MESSAGE_RES_EVT_UNLINK_CHANNEL) {
  1164. error = SA_AIS_ERR_LIBRARY;
  1165. goto chan_unlink_put;
  1166. }
  1167. error = res.iuc_head.error;
  1168. chan_unlink_put:
  1169. saHandleInstancePut (&evt_instance_handle_db, evtHandle);
  1170. chan_unlink_done:
  1171. return error;
  1172. }
  1173. /*
  1174. * The saEvtEventAllocate() function allocates memory for the event, but not
  1175. * for the eventHandle, and initializes all event attributes to default values.
  1176. * The event allocated by saEvtEventAllocate() is freed by invoking
  1177. * saEvtEventFree().
  1178. * The memory associated with the eventHandle is not deallocated by the
  1179. * saEvtEventAllocate() function or the saEvtEventFree() function. It is the
  1180. * responsibility of the invoking process to deallocate the memory for the
  1181. * eventHandle when the process has published the event and has freed the
  1182. * event by invoking saEvtEventFree().
  1183. */
  1184. SaAisErrorT
  1185. saEvtEventAllocate(
  1186. const SaEvtChannelHandleT channelHandle,
  1187. SaEvtEventHandleT *eventHandle)
  1188. {
  1189. SaAisErrorT error;
  1190. struct event_data_instance *edi;
  1191. struct event_instance *evti;
  1192. struct event_channel_instance *eci;
  1193. struct handle_list *hl;
  1194. if (!eventHandle) {
  1195. return SA_AIS_ERR_INVALID_PARAM;
  1196. }
  1197. error = saHandleInstanceGet(&channel_handle_db, channelHandle,
  1198. (void*)&eci);
  1199. if (error != SA_AIS_OK) {
  1200. goto alloc_done;
  1201. }
  1202. error = saHandleInstanceGet(&evt_instance_handle_db,
  1203. eci->eci_instance_handle, (void*)&evti);
  1204. if (error != SA_AIS_OK) {
  1205. goto alloc_put1;
  1206. }
  1207. error = saHandleCreate(&event_handle_db, sizeof(*edi),
  1208. eventHandle);
  1209. if (error != SA_AIS_OK) {
  1210. goto alloc_put2;
  1211. }
  1212. error = saHandleInstanceGet(&event_handle_db, *eventHandle,
  1213. (void*)&edi);
  1214. if (error != SA_AIS_OK) {
  1215. saHandleDestroy(&event_handle_db, *eventHandle);
  1216. goto alloc_put2;
  1217. }
  1218. pthread_mutex_init(&edi->edi_mutex, NULL);
  1219. edi->edi_ro = 0;
  1220. edi->edi_freeing = 0;
  1221. edi->edi_channel_handle = channelHandle;
  1222. edi->edi_pub_node = evti->ei_node_id;
  1223. edi->edi_priority = SA_EVT_LOWEST_PRIORITY;
  1224. edi->edi_event_id = SA_EVT_EVENTID_NONE;
  1225. edi->edi_pub_time = SA_TIME_UNKNOWN;
  1226. edi->edi_retention_time = 0;
  1227. hl = malloc(sizeof(*hl));
  1228. if (!hl) {
  1229. saHandleDestroy(&event_handle_db, *eventHandle);
  1230. error = SA_AIS_ERR_NO_MEMORY;
  1231. goto alloc_put2;
  1232. }
  1233. edi->edi_hl = hl;
  1234. hl->hl_handle = *eventHandle;
  1235. list_init(&hl->hl_entry);
  1236. list_add(&hl->hl_entry, &eci->eci_event_list);
  1237. saHandleInstancePut (&event_handle_db, *eventHandle);
  1238. alloc_put2:
  1239. saHandleInstancePut (&evt_instance_handle_db, eci->eci_instance_handle);
  1240. alloc_put1:
  1241. saHandleInstancePut (&channel_handle_db, channelHandle);
  1242. alloc_done:
  1243. return error;
  1244. }
  1245. /*
  1246. * The saEvtEventFree() function gives the Event Service premission to
  1247. * deallocate the memory that contains the attributes of the event that is
  1248. * associated with eventHandle. The function is used to free events allocated
  1249. * by saEvtEventAllocate() and by saEvtEventDeliverCallback().
  1250. */
  1251. SaAisErrorT
  1252. saEvtEventFree(SaEvtEventHandleT eventHandle)
  1253. {
  1254. SaAisErrorT error;
  1255. struct event_data_instance *edi;
  1256. error = saHandleInstanceGet(&event_handle_db, eventHandle,
  1257. (void*)&edi);
  1258. if (error != SA_AIS_OK) {
  1259. goto evt_free_done;
  1260. }
  1261. /*
  1262. * Make sure that the event isn't being freed elsewhere
  1263. */
  1264. pthread_mutex_lock(&edi->edi_mutex);
  1265. if (edi->edi_freeing) {
  1266. pthread_mutex_unlock(&edi->edi_mutex);
  1267. saHandleInstancePut(&event_handle_db, eventHandle);
  1268. return SA_AIS_ERR_BAD_HANDLE;
  1269. }
  1270. edi->edi_freeing = 1;
  1271. pthread_mutex_unlock(&edi->edi_mutex);
  1272. saHandleDestroy(&event_handle_db, eventHandle);
  1273. saHandleInstancePut(&event_handle_db, eventHandle);
  1274. evt_free_done:
  1275. return error;
  1276. }
  1277. /*
  1278. * This function may be used to assign writeable event attributes. It takes
  1279. * as arguments an event handle eventHandle and the attribute to be set in
  1280. * the event structure of the event with that handle. Note: The only
  1281. * attributes that a process can set are the patternArray, priority,
  1282. * retentionTime and publisherName attributes.
  1283. */
  1284. SaAisErrorT
  1285. saEvtEventAttributesSet(
  1286. const SaEvtEventHandleT eventHandle,
  1287. const SaEvtEventPatternArrayT *patternArray,
  1288. SaEvtEventPriorityT priority,
  1289. SaTimeT retentionTime,
  1290. const SaNameT *publisherName)
  1291. {
  1292. SaEvtEventPatternT *oldpatterns;
  1293. SaSizeT oldnumber;
  1294. SaAisErrorT error;
  1295. struct event_data_instance *edi;
  1296. int i;
  1297. if (priority > SA_EVT_LOWEST_PRIORITY) {
  1298. return SA_AIS_ERR_INVALID_PARAM;
  1299. }
  1300. error = saHandleInstanceGet(&event_handle_db, eventHandle,
  1301. (void*)&edi);
  1302. if (error != SA_AIS_OK) {
  1303. goto attr_set_done;
  1304. }
  1305. pthread_mutex_lock(&edi->edi_mutex);
  1306. /*
  1307. * Cannot modify an event returned via callback.
  1308. */
  1309. if (edi->edi_ro) {
  1310. error = SA_AIS_ERR_ACCESS;
  1311. goto attr_set_unlock;
  1312. }
  1313. edi->edi_priority = priority;
  1314. edi->edi_retention_time = retentionTime;
  1315. if (publisherName) {
  1316. edi->edi_pub_name = *publisherName;
  1317. }
  1318. if (!patternArray) {
  1319. goto attr_set_unlock;
  1320. }
  1321. oldpatterns = edi->edi_patterns.patterns;
  1322. oldnumber = edi->edi_patterns.patternsNumber;
  1323. edi->edi_patterns.patterns = 0;
  1324. edi->edi_patterns.patterns = malloc(sizeof(SaEvtEventPatternT) *
  1325. patternArray->patternsNumber);
  1326. if (!edi->edi_patterns.patterns) {
  1327. error = SA_AIS_ERR_NO_MEMORY;
  1328. goto attr_set_done_reset;
  1329. }
  1330. edi->edi_patterns.patternsNumber = patternArray->patternsNumber;
  1331. edi->edi_patterns.allocatedNumber = patternArray->patternsNumber;
  1332. /*
  1333. * copy the patterns from the caller. allocating memory for
  1334. * of all the strings.
  1335. */
  1336. for (i = 0; i < patternArray->patternsNumber; i++) {
  1337. edi->edi_patterns.patterns[i].pattern =
  1338. malloc(patternArray->patterns[i].patternSize);
  1339. if (!edi->edi_patterns.patterns[i].pattern) {
  1340. int j;
  1341. for (j = 0; j < i; j++) {
  1342. free(edi->edi_patterns.patterns[j].pattern);
  1343. }
  1344. free(edi->edi_patterns.patterns);
  1345. error = SA_AIS_ERR_NO_MEMORY;
  1346. goto attr_set_done_reset;
  1347. }
  1348. memcpy(edi->edi_patterns.patterns[i].pattern,
  1349. patternArray->patterns[i].pattern,
  1350. patternArray->patterns[i].patternSize);
  1351. edi->edi_patterns.patterns[i].patternSize =
  1352. patternArray->patterns[i].patternSize;
  1353. edi->edi_patterns.patterns[i].allocatedSize =
  1354. patternArray->patterns[i].patternSize;
  1355. }
  1356. /*
  1357. * free up the old pattern memory
  1358. */
  1359. if (oldpatterns) {
  1360. for (i = 0; i < oldnumber; i++) {
  1361. if (oldpatterns[i].pattern) {
  1362. free(oldpatterns[i].pattern);
  1363. }
  1364. }
  1365. free (oldpatterns);
  1366. }
  1367. goto attr_set_unlock;
  1368. attr_set_done_reset:
  1369. edi->edi_patterns.patterns = oldpatterns;
  1370. edi->edi_patterns.patternsNumber = oldnumber;
  1371. attr_set_unlock:
  1372. pthread_mutex_unlock(&edi->edi_mutex);
  1373. saHandleInstancePut(&event_handle_db, eventHandle);
  1374. attr_set_done:
  1375. return error;
  1376. }
  1377. /*
  1378. * This function takes as parameters an event handle eventHandle and the
  1379. * attributes of the event with that handle. The function retrieves the
  1380. * value of the attributes for the event and stores them at the address
  1381. * provided for the out parameters.
  1382. * It is the responsibility of the invoking process to allocate memory for
  1383. * the out parameters before it invokes this function. The Event Service
  1384. * assigns the out values into that memory. For each of the out, or in/out,
  1385. * parameters, if the invoking process provides a NULL reference, the
  1386. * Availability Management Framework does not return the out value.
  1387. * Similarly, it is the responsibility of the invoking process to allocate
  1388. * memory for the patternArray.
  1389. */
  1390. SaAisErrorT
  1391. saEvtEventAttributesGet(
  1392. SaEvtEventHandleT eventHandle,
  1393. SaEvtEventPatternArrayT *patternArray,
  1394. SaEvtEventPriorityT *priority,
  1395. SaTimeT *retentionTime,
  1396. SaNameT *publisherName,
  1397. SaTimeT *publishTime,
  1398. SaEvtEventIdT *eventId)
  1399. {
  1400. SaAisErrorT error;
  1401. struct event_data_instance *edi;
  1402. SaSizeT npats;
  1403. int i;
  1404. error = saHandleInstanceGet(&event_handle_db, eventHandle,
  1405. (void*)&edi);
  1406. if (error != SA_AIS_OK) {
  1407. goto attr_get_done;
  1408. }
  1409. pthread_mutex_lock(&edi->edi_mutex);
  1410. /*
  1411. * Go through the args and send back information if the pointer
  1412. * isn't NULL
  1413. */
  1414. if (eventId) {
  1415. *eventId = edi->edi_event_id;
  1416. }
  1417. if (publishTime) {
  1418. *publishTime = edi->edi_pub_time;
  1419. }
  1420. if (publisherName) {
  1421. *publisherName = edi->edi_pub_name;
  1422. }
  1423. if (retentionTime) {
  1424. *retentionTime = edi->edi_retention_time;
  1425. }
  1426. if (priority) {
  1427. *priority = edi->edi_priority;
  1428. }
  1429. if (!patternArray) {
  1430. goto attr_get_unlock;
  1431. }
  1432. /*
  1433. * The spec says that if the called passes in a NULL patterns array,
  1434. * then we allocate the required data and the caller is responsible
  1435. * for dealocating later. Otherwise, we copy to pre-allocated space.
  1436. * If there are more patterns than allcated space, we set the return
  1437. * code to SA_AIS_ERR_NO_SPACE and copy as much as will fit. We will
  1438. * return the total number of patterns available in the patternsNumber
  1439. * regardless of how much was allocated.
  1440. *
  1441. */
  1442. if (patternArray->patterns == NULL) {
  1443. npats = edi->edi_patterns.patternsNumber;
  1444. patternArray->allocatedNumber = edi->edi_patterns.patternsNumber;
  1445. patternArray->patternsNumber = edi->edi_patterns.patternsNumber;
  1446. patternArray->patterns = malloc(sizeof(*patternArray->patterns) *
  1447. edi->edi_patterns.patternsNumber);
  1448. if (!patternArray->patterns) {
  1449. error = SA_AIS_ERR_LIBRARY;
  1450. goto attr_get_unlock;
  1451. }
  1452. for (i = 0; i < edi->edi_patterns.patternsNumber; i++) {
  1453. patternArray->patterns[i].allocatedSize =
  1454. edi->edi_patterns.patterns[i].allocatedSize;
  1455. patternArray->patterns[i].patternSize =
  1456. edi->edi_patterns.patterns[i].patternSize;
  1457. patternArray->patterns[i].pattern =
  1458. malloc(edi->edi_patterns.patterns[i].patternSize);
  1459. if (!patternArray->patterns[i].pattern) {
  1460. int j;
  1461. /*
  1462. * back out previous mallocs
  1463. */
  1464. for (j = 0; j < i; j++) {
  1465. free(patternArray->patterns[j].pattern);
  1466. }
  1467. free(patternArray->patterns);
  1468. /*
  1469. * saEvtEventAttributesGet doesn't return
  1470. * SA_AIS_ERR_NO_MEMORY
  1471. */
  1472. error = SA_AIS_ERR_LIBRARY;
  1473. goto attr_get_unlock;
  1474. }
  1475. }
  1476. } else {
  1477. if (patternArray->allocatedNumber < edi->edi_patterns.allocatedNumber) {
  1478. error = SA_AIS_ERR_NO_SPACE;
  1479. npats = patternArray->allocatedNumber;
  1480. } else {
  1481. npats = edi->edi_patterns.patternsNumber;
  1482. }
  1483. }
  1484. patternArray->patternsNumber = edi->edi_patterns.patternsNumber;
  1485. /*
  1486. * copy the patterns to the callers structure. If we have pre-allocated
  1487. * data, the patterns may not fit in the supplied space. In that case we
  1488. * return NO_SPACE.
  1489. */
  1490. for (i = 0; i < npats; i++) {
  1491. memcpy(patternArray->patterns[i].pattern,
  1492. edi->edi_patterns.patterns[i].pattern,
  1493. min(patternArray->patterns[i].allocatedSize,
  1494. edi->edi_patterns.patterns[i].patternSize));
  1495. if (patternArray->patterns[i].allocatedSize <
  1496. edi->edi_patterns.patterns[i].patternSize) {
  1497. error = SA_AIS_ERR_NO_SPACE;
  1498. }
  1499. patternArray->patterns[i].patternSize =
  1500. edi->edi_patterns.patterns[i].patternSize;
  1501. }
  1502. attr_get_unlock:
  1503. pthread_mutex_unlock(&edi->edi_mutex);
  1504. saHandleInstancePut(&event_handle_db, eventHandle);
  1505. attr_get_done:
  1506. return error;
  1507. }
  1508. /*
  1509. * The saEvtEventDataGet() function allows a process to retrieve the data
  1510. * associated with an event previously delivered by
  1511. * saEvtEventDeliverCallback().
  1512. */
  1513. SaAisErrorT
  1514. saEvtEventDataGet(
  1515. const SaEvtEventHandleT eventHandle,
  1516. void *eventData,
  1517. SaSizeT *eventDataSize)
  1518. {
  1519. SaAisErrorT error = SA_AIS_ERR_INVALID_PARAM;
  1520. struct event_data_instance *edi;
  1521. SaSizeT xfsize;
  1522. if (!eventDataSize) {
  1523. goto data_get_done;
  1524. }
  1525. error = saHandleInstanceGet(&event_handle_db, eventHandle,
  1526. (void*)&edi);
  1527. if (error != SA_AIS_OK) {
  1528. goto data_get_done;
  1529. }
  1530. pthread_mutex_lock(&edi->edi_mutex);
  1531. /*
  1532. * If no buffer was supplied, then just tell the caller
  1533. * how large a buffer is needed.
  1534. */
  1535. if (!eventData) {
  1536. error = SA_AIS_ERR_NO_SPACE;
  1537. *eventDataSize = edi->edi_event_data_size;
  1538. goto unlock_put;
  1539. }
  1540. /*
  1541. * Can't get data from an event that wasn't
  1542. * a delivered event.
  1543. */
  1544. if (!edi->edi_ro) {
  1545. error = SA_AIS_ERR_BAD_HANDLE;
  1546. goto unlock_put;
  1547. }
  1548. if (edi->edi_event_data && edi->edi_event_data_size) {
  1549. xfsize = min(*eventDataSize, edi->edi_event_data_size);
  1550. if (*eventDataSize < edi->edi_event_data_size) {
  1551. error = SA_AIS_ERR_NO_SPACE;
  1552. }
  1553. *eventDataSize = edi->edi_event_data_size;
  1554. memcpy(eventData, edi->edi_event_data, xfsize);
  1555. } else {
  1556. *eventDataSize = 0;
  1557. }
  1558. unlock_put:
  1559. pthread_mutex_unlock(&edi->edi_mutex);
  1560. saHandleInstancePut(&event_handle_db, eventHandle);
  1561. data_get_done:
  1562. return error;
  1563. }
  1564. /*
  1565. * Calculate the size in bytes for patterns
  1566. */
  1567. static size_t patt_size(const SaEvtEventPatternArrayT *patterns)
  1568. {
  1569. int i;
  1570. size_t size = sizeof(SaEvtEventPatternArrayT);
  1571. for (i = 0; i < patterns->patternsNumber; i++) {
  1572. size += sizeof(SaEvtEventPatternT);
  1573. size += patterns->patterns[i].patternSize;
  1574. }
  1575. return size;
  1576. }
  1577. /*
  1578. * copy patterns to a form for sending to the server
  1579. */
  1580. static uint32_t aispatt_to_evt_patt(const SaEvtEventPatternArrayT *patterns,
  1581. void *data)
  1582. {
  1583. int i;
  1584. SaEvtEventPatternT *pats = data;
  1585. SaUint8T *str = (SaUint8T *)pats +
  1586. (patterns->patternsNumber * sizeof(*pats));
  1587. /*
  1588. * Pointers are replaced with offsets into the data array. These
  1589. * will be later converted back into pointers when received as events.
  1590. */
  1591. for (i = 0; i < patterns->patternsNumber; i++) {
  1592. memcpy(str, patterns->patterns[i].pattern,
  1593. patterns->patterns[i].patternSize);
  1594. pats->patternSize = patterns->patterns[i].patternSize;
  1595. pats->pattern = (SaUint8T *)((void *)str - data);
  1596. str += patterns->patterns[i].patternSize;
  1597. pats++;
  1598. }
  1599. return patterns->patternsNumber;
  1600. }
  1601. /*
  1602. * Calculate the size in bytes for filters
  1603. */
  1604. static size_t filt_size(const SaEvtEventFilterArrayT *filters)
  1605. {
  1606. int i;
  1607. size_t size = sizeof(SaEvtEventFilterArrayT);
  1608. for (i = 0; i < filters->filtersNumber; i++) {
  1609. size += sizeof(SaEvtEventFilterT);
  1610. size += filters->filters[i].filter.patternSize;
  1611. }
  1612. return size;
  1613. }
  1614. /*
  1615. * Convert the Sa filters to a form that can be sent over the network
  1616. * i.e. replace pointers with offsets. The pointers will be reconstituted
  1617. * by the receiver.
  1618. */
  1619. static uint32_t aisfilt_to_evt_filt(const SaEvtEventFilterArrayT *filters,
  1620. void *data)
  1621. {
  1622. int i;
  1623. SaEvtEventFilterArrayT *filta = data;
  1624. SaEvtEventFilterT *filts = data + sizeof(SaEvtEventFilterArrayT);
  1625. SaUint8T *str = (SaUint8T *)filts +
  1626. (filters->filtersNumber * sizeof(*filts));
  1627. /*
  1628. * Pointers are replaced with offsets into the data array. These
  1629. * will be later converted back into pointers by the evt server.
  1630. */
  1631. filta->filters = (SaEvtEventFilterT *)((void *)filts - data);
  1632. filta->filtersNumber = filters->filtersNumber;
  1633. for (i = 0; i < filters->filtersNumber; i++) {
  1634. filts->filterType = filters->filters[i].filterType;
  1635. filts->filter.patternSize =
  1636. filters->filters[i].filter.patternSize;
  1637. memcpy(str,
  1638. filters->filters[i].filter.pattern,
  1639. filters->filters[i].filter.patternSize);
  1640. filts->filter.pattern = (SaUint8T *)((void *)str - data);
  1641. str += filters->filters[i].filter.patternSize;
  1642. filts++;
  1643. }
  1644. return filters->filtersNumber;
  1645. }
  1646. /*
  1647. * The saEvtEventPublish() function publishes an event on the associated
  1648. * channel. The event to be published consists of a
  1649. * standard set of attributes (the event header) and an optional data part.
  1650. * Before an event can be published, the publisher process must invoke the
  1651. * saEvtEventPatternArraySet() function to set the event patterns. The event
  1652. * is delivered to subscribers whose subscription filter matches the event
  1653. * patterns.
  1654. * When the Event Service publishes an event, it automatically sets
  1655. * the following readonly event attributes:
  1656. * - Event attribute time
  1657. * - Event publisher identifier
  1658. * - Event publisher node identifier
  1659. * - Event identifier
  1660. * In addition to the event attributes, a process can supply values for the
  1661. * eventData and eventDataSize parameters for publication as part of the
  1662. * event. The data portion of the event may be at most SA_EVT_DATA_MAX_LEN
  1663. * bytes in length.
  1664. * The process may assume that the invocation of saEvtEventPublish() copies
  1665. * all pertinent parameters, including the memory associated with the
  1666. * eventHandle and eventData parameters, to its own local memory. However,
  1667. * the invocation does not automatically deallocate memory associated with
  1668. * the eventHandle and eventData parameters. It is the responsibility of the
  1669. * invoking process to deallocate the memory for those parameters after
  1670. * saEvtEventPublish() returns.
  1671. */
  1672. SaAisErrorT
  1673. saEvtEventPublish(
  1674. const SaEvtEventHandleT eventHandle,
  1675. const void *eventData,
  1676. SaSizeT eventDataSize,
  1677. SaEvtEventIdT *eventId)
  1678. {
  1679. SaAisErrorT error;
  1680. struct event_data_instance *edi;
  1681. struct event_instance *evti;
  1682. struct event_channel_instance *eci;
  1683. struct lib_event_data *req;
  1684. struct res_evt_event_publish res;
  1685. size_t pattern_size;
  1686. struct event_pattern *patterns;
  1687. void *data_start;
  1688. struct iovec iov;
  1689. if (!eventId) {
  1690. return SA_AIS_ERR_INVALID_PARAM;
  1691. }
  1692. if (eventDataSize > SA_EVT_DATA_MAX_LEN) {
  1693. error = SA_AIS_ERR_TOO_BIG;
  1694. goto pub_done;
  1695. }
  1696. error = saHandleInstanceGet(&event_handle_db, eventHandle,
  1697. (void*)&edi);
  1698. if (error != SA_AIS_OK) {
  1699. goto pub_done;
  1700. }
  1701. pthread_mutex_lock(&edi->edi_mutex);
  1702. error = saHandleInstanceGet(&channel_handle_db, edi->edi_channel_handle,
  1703. (void*)&eci);
  1704. if (error != SA_AIS_OK) {
  1705. goto pub_put1;
  1706. }
  1707. /*
  1708. * See if we can publish to this channel
  1709. */
  1710. if (!(eci->eci_open_flags & SA_EVT_CHANNEL_PUBLISHER)) {
  1711. error = SA_AIS_ERR_ACCESS;
  1712. goto pub_put2;
  1713. }
  1714. error = saHandleInstanceGet(&evt_instance_handle_db,
  1715. eci->eci_instance_handle, (void*)&evti);
  1716. if (error != SA_AIS_OK) {
  1717. goto pub_put2;
  1718. }
  1719. /*
  1720. * Figure out how much memory we need for the patterns and data
  1721. */
  1722. pattern_size = patt_size(&edi->edi_patterns);
  1723. req = malloc(sizeof(*req) + eventDataSize + pattern_size);
  1724. if (!req) {
  1725. error = SA_AIS_ERR_NO_MEMORY;
  1726. goto pub_put3;
  1727. }
  1728. patterns = (struct event_pattern *)req->led_body;
  1729. data_start = (void *)req->led_body + pattern_size;
  1730. /*
  1731. * copy everything to the request structure
  1732. */
  1733. aispatt_to_evt_patt(&edi->edi_patterns, patterns);
  1734. req->led_patterns_number = edi->edi_patterns.patternsNumber;
  1735. req->led_user_data_offset = pattern_size;
  1736. if (eventData && eventDataSize) {
  1737. memcpy(data_start, eventData, eventDataSize);
  1738. req->led_user_data_size = eventDataSize;
  1739. } else {
  1740. req->led_user_data_size = 0;
  1741. }
  1742. req->led_head.id = MESSAGE_REQ_EVT_PUBLISH;
  1743. req->led_head.size = sizeof(*req) + pattern_size + eventDataSize;
  1744. req->led_svr_channel_handle = eci->eci_svr_channel_handle;
  1745. req->led_retention_time = edi->edi_retention_time;
  1746. req->led_publish_time = clustTimeNow();
  1747. req->led_priority = edi->edi_priority;
  1748. req->led_publisher_name = edi->edi_pub_name;
  1749. iov.iov_base = req;
  1750. iov.iov_len = req->led_head.size;
  1751. pthread_mutex_lock(&evti->ei_response_mutex);
  1752. error = saSendMsgReceiveReply(evti->ei_response_fd, &iov, 1, &res,
  1753. sizeof(res));
  1754. pthread_mutex_unlock (&evti->ei_response_mutex);
  1755. free(req);
  1756. if (error != SA_AIS_OK) {
  1757. pthread_mutex_unlock (&evti->ei_response_mutex);
  1758. goto pub_put3;
  1759. }
  1760. error = res.iep_head.error;
  1761. if (error == SA_AIS_OK) {
  1762. *eventId = res.iep_event_id;
  1763. }
  1764. pub_put3:
  1765. saHandleInstancePut (&evt_instance_handle_db, eci->eci_instance_handle);
  1766. pub_put2:
  1767. saHandleInstancePut (&channel_handle_db, edi->edi_channel_handle);
  1768. pub_put1:
  1769. pthread_mutex_unlock(&edi->edi_mutex);
  1770. saHandleInstancePut(&event_handle_db, eventHandle);
  1771. pub_done:
  1772. return error;
  1773. }
  1774. /*
  1775. * The saEvtEventSubscribe() function enables a process to subscribe for
  1776. * events on an event channel by registering one or more filters on that
  1777. * event channel. The process must have opened the event channel, designated
  1778. * by channelHandle, with the SA_EVT_CHANNEL_SUBSCRIBER flag set for an
  1779. * invocation of this function to be successful.
  1780. * The memory associated with the filters is not deallocated by the
  1781. * saEvtEventSubscribe() function. It is the responsibility of the invoking
  1782. * process to deallocate the memory when the saEvtEventSubscribe() function
  1783. * returns.
  1784. * For a given subscription, the filters parameter cannot be modified. To
  1785. * change the filters parameter without losing events, a process must
  1786. * establish a new subscription with the new filters parameter. After the new
  1787. * subscription is established, the old subscription can be removed by
  1788. * invoking the saEvtEventUnsubscribe() function.
  1789. */
  1790. SaAisErrorT
  1791. saEvtEventSubscribe(
  1792. const SaEvtChannelHandleT channelHandle,
  1793. const SaEvtEventFilterArrayT *filters,
  1794. SaEvtSubscriptionIdT subscriptionId)
  1795. {
  1796. SaAisErrorT error;
  1797. struct event_instance *evti;
  1798. struct event_channel_instance *eci;
  1799. struct req_evt_event_subscribe *req;
  1800. struct res_evt_event_subscribe res;
  1801. int sz;
  1802. struct iovec iov;
  1803. if (!filters) {
  1804. return SA_AIS_ERR_INVALID_PARAM;
  1805. }
  1806. error = saHandleInstanceGet(&channel_handle_db, channelHandle,
  1807. (void*)&eci);
  1808. if (error != SA_AIS_OK) {
  1809. goto subscribe_done;
  1810. }
  1811. /*
  1812. * get the evt handle for the fd
  1813. */
  1814. error = saHandleInstanceGet(&evt_instance_handle_db,
  1815. eci->eci_instance_handle, (void*)&evti);
  1816. if (error != SA_AIS_OK) {
  1817. goto subscribe_put1;
  1818. }
  1819. /*
  1820. * Make sure that a deliver callback has been
  1821. * registered before allowing the subscribe to continue.
  1822. */
  1823. if (!evti->ei_callback.saEvtEventDeliverCallback) {
  1824. error = SA_AIS_ERR_INIT;
  1825. goto subscribe_put2;
  1826. }
  1827. /*
  1828. * See if we can subscribe to this channel
  1829. */
  1830. if (!(eci->eci_open_flags & SA_EVT_CHANNEL_SUBSCRIBER)) {
  1831. error = SA_AIS_ERR_ACCESS;
  1832. goto subscribe_put2;
  1833. }
  1834. /*
  1835. * calculate size needed to store the filters
  1836. */
  1837. sz = filt_size(filters);
  1838. req = malloc(sizeof(*req) + sz);
  1839. if (!req) {
  1840. error = SA_AIS_ERR_NO_MEMORY;
  1841. goto subscribe_put2;
  1842. }
  1843. /*
  1844. * Copy the supplied filters to the request
  1845. */
  1846. req->ics_filter_count = aisfilt_to_evt_filt(filters,
  1847. req->ics_filter_data);
  1848. req->ics_head.id = MESSAGE_REQ_EVT_SUBSCRIBE;
  1849. req->ics_head.size = sizeof(*req) + sz;
  1850. req->ics_channel_handle = eci->eci_svr_channel_handle;
  1851. req->ics_sub_id = subscriptionId;
  1852. req->ics_filter_size = sz;
  1853. iov.iov_base = req;
  1854. iov.iov_len = req->ics_head.size;
  1855. pthread_mutex_lock(&evti->ei_response_mutex);
  1856. error = saSendMsgReceiveReply(evti->ei_response_fd, &iov, 1,
  1857. &res, sizeof(res));
  1858. pthread_mutex_unlock (&evti->ei_response_mutex);
  1859. free(req);
  1860. if (res.ics_head.id != MESSAGE_RES_EVT_SUBSCRIBE) {
  1861. goto subscribe_put2;
  1862. }
  1863. error = res.ics_head.error;
  1864. subscribe_put2:
  1865. saHandleInstancePut(&evt_instance_handle_db,
  1866. eci->eci_instance_handle);
  1867. subscribe_put1:
  1868. saHandleInstancePut(&channel_handle_db, channelHandle);
  1869. subscribe_done:
  1870. return error;
  1871. }
  1872. /*
  1873. * The saEvtEventUnsubscribe() function allows a process to stop receiving
  1874. * events for a particular subscription on an event channel by removing the
  1875. * subscription. The saEvtEventUnsubscribe() operation is successful if the
  1876. * subscriptionId parameter matches a previously registered subscription.
  1877. * Pending events that no longer match any subscription, because the
  1878. * saEvtEventUnsubscribe() operation had been invoked, are purged. a process
  1879. * that wishes to modify a subscription without losing any events must
  1880. * establish the new subscription before removing the existing subscription.
  1881. */
  1882. SaAisErrorT
  1883. saEvtEventUnsubscribe(
  1884. const SaEvtChannelHandleT channelHandle,
  1885. SaEvtSubscriptionIdT subscriptionId)
  1886. {
  1887. SaAisErrorT error;
  1888. struct event_instance *evti;
  1889. struct event_channel_instance *eci;
  1890. struct req_evt_event_unsubscribe req;
  1891. struct res_evt_event_unsubscribe res;
  1892. struct iovec iov;
  1893. error = saHandleInstanceGet(&channel_handle_db, channelHandle,
  1894. (void*)&eci);
  1895. if (error != SA_AIS_OK) {
  1896. goto unsubscribe_done;
  1897. }
  1898. error = saHandleInstanceGet(&evt_instance_handle_db,
  1899. eci->eci_instance_handle, (void*)&evti);
  1900. if (error != SA_AIS_OK) {
  1901. goto unsubscribe_put1;
  1902. }
  1903. req.icu_head.id = MESSAGE_REQ_EVT_UNSUBSCRIBE;
  1904. req.icu_head.size = sizeof(req);
  1905. req.icu_channel_handle = eci->eci_svr_channel_handle;
  1906. req.icu_sub_id = subscriptionId;
  1907. iov.iov_base = &req;
  1908. iov.iov_len = sizeof(req);
  1909. pthread_mutex_lock(&evti->ei_response_mutex);
  1910. error = saSendMsgReceiveReply(evti->ei_response_fd, &iov, 1,
  1911. &res, sizeof(res));
  1912. pthread_mutex_unlock (&evti->ei_response_mutex);
  1913. if (error != SA_AIS_OK) {
  1914. goto unsubscribe_put2;
  1915. }
  1916. if (res.icu_head.id != MESSAGE_RES_EVT_UNSUBSCRIBE) {
  1917. error = SA_AIS_ERR_LIBRARY;
  1918. goto unsubscribe_put2;
  1919. }
  1920. error = res.icu_head.error;
  1921. unsubscribe_put2:
  1922. saHandleInstancePut(&evt_instance_handle_db,
  1923. eci->eci_instance_handle);
  1924. unsubscribe_put1:
  1925. saHandleInstancePut(&channel_handle_db, channelHandle);
  1926. unsubscribe_done:
  1927. return error;
  1928. }
  1929. /*
  1930. * The saEvtEventRetentionTimeClear() function is used to clear the retention
  1931. * time of a published event. It indicates to the Event Service that it does
  1932. * not need to keep the event any longer for potential new subscribers. Once
  1933. * the value of the retention time is reset to 0, the event is no longer
  1934. * available for new subscribers. The event is held until all old subscribers
  1935. * in the system process the event and free the event using saEvtEventFree().
  1936. */
  1937. SaAisErrorT
  1938. saEvtEventRetentionTimeClear(
  1939. const SaEvtChannelHandleT channelHandle,
  1940. const SaEvtEventIdT eventId)
  1941. {
  1942. SaAisErrorT error;
  1943. struct event_instance *evti;
  1944. struct event_channel_instance *eci;
  1945. struct req_evt_event_clear_retentiontime req;
  1946. struct res_evt_event_clear_retentiontime res;
  1947. struct iovec iov;
  1948. if (!is_valid_event_id(eventId)) {
  1949. return SA_AIS_ERR_INVALID_PARAM;
  1950. }
  1951. error = saHandleInstanceGet(&channel_handle_db, channelHandle,
  1952. (void*)&eci);
  1953. if (error != SA_AIS_OK) {
  1954. goto ret_time_done;
  1955. }
  1956. error = saHandleInstanceGet(&evt_instance_handle_db,
  1957. eci->eci_instance_handle, (void*)&evti);
  1958. if (error != SA_AIS_OK) {
  1959. goto ret_time_put1;
  1960. }
  1961. req.iec_head.id = MESSAGE_REQ_EVT_CLEAR_RETENTIONTIME;
  1962. req.iec_head.size = sizeof(req);
  1963. req.iec_channel_handle = eci->eci_svr_channel_handle;
  1964. req.iec_event_id = eventId;
  1965. iov.iov_base = &req;
  1966. iov.iov_len = sizeof(req);
  1967. pthread_mutex_lock(&evti->ei_response_mutex);
  1968. error = saSendMsgReceiveReply(evti->ei_response_fd, &iov, 1,
  1969. &res, sizeof(res));
  1970. pthread_mutex_unlock (&evti->ei_response_mutex);
  1971. if (error != SA_AIS_OK) {
  1972. goto ret_time_put2;
  1973. }
  1974. if (res.iec_head.id != MESSAGE_RES_EVT_CLEAR_RETENTIONTIME) {
  1975. error = SA_AIS_ERR_LIBRARY;
  1976. goto ret_time_put2;
  1977. }
  1978. error = res.iec_head.error;
  1979. ret_time_put2:
  1980. saHandleInstancePut(&evt_instance_handle_db,
  1981. eci->eci_instance_handle);
  1982. ret_time_put1:
  1983. saHandleInstancePut(&channel_handle_db, channelHandle);
  1984. ret_time_done:
  1985. return error;
  1986. }
  1987. /*
  1988. * vi: set autoindent tabstop=4 shiftwidth=4 :
  1989. */