evt.c 61 KB

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