evt.c 61 KB

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