evt.c 62 KB

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