evt.c 57 KB

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