evt.c 58 KB

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