evt.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  1. /*
  2. * Copyright (c) 2004 Mark Haverkamp
  3. * Copyright (c) 2004 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/ais_evt.h"
  42. #include "../include/ais_msg.h"
  43. #include "util.h"
  44. #include "../exec/totempg.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. {'A', 0x01, 0x01},
  53. {'a', 0x01, 0x01}
  54. };
  55. static struct saVersionDatabase evt_version_database = {
  56. sizeof(supported_versions) / sizeof(SaVersionT),
  57. supported_versions
  58. };
  59. /*
  60. * Event instance data
  61. */
  62. struct saHandleDatabase evt_instance_handle_db = {
  63. .handleCount = 0,
  64. .handles = 0,
  65. .mutex = PTHREAD_MUTEX_INITIALIZER,
  66. .handleInstanceDestructor = evtHandleInstanceDestructor
  67. };
  68. /*
  69. * Channel instance data
  70. */
  71. struct saHandleDatabase channel_handle_db = {
  72. .handleCount = 0,
  73. .handles = 0,
  74. .mutex = PTHREAD_MUTEX_INITIALIZER,
  75. .handleInstanceDestructor = chanHandleInstanceDestructor
  76. };
  77. /*
  78. * Event instance data
  79. */
  80. struct saHandleDatabase event_handle_db = {
  81. .handleCount = 0,
  82. .handles = 0,
  83. .mutex = PTHREAD_MUTEX_INITIALIZER,
  84. .handleInstanceDestructor = eventHandleInstanceDestructor
  85. };
  86. struct message_overlay {
  87. struct res_header header;
  88. char data[0];
  89. };
  90. /*
  91. * data required to support events for a given initialization
  92. *
  93. * ei_fd: fd received from the evtInitialize call.
  94. * ei_callback: callback function.
  95. * ei_version: version sent to the evtInitialize call.
  96. * ei_node_id: our node id.
  97. * ei_node_name: our node name.
  98. * ei_finalize: instance in finalize flag
  99. * ei_queue: queue for async messages while doing sync commands
  100. * ei_mutex: instance mutex
  101. *
  102. */
  103. struct event_instance {
  104. int ei_fd;
  105. SaEvtCallbacksT ei_callback;
  106. SaVersionT ei_version;
  107. SaClmNodeIdT ei_node_id;
  108. SaNameT ei_node_name;
  109. int ei_finalize;
  110. struct queue ei_inq;
  111. char ei_message[MESSAGE_SIZE_MAX];
  112. pthread_mutex_t ei_mutex;
  113. };
  114. /*
  115. * Data associated with an opened channel
  116. *
  117. * eci_channel_name: name of channel
  118. * eci_open_flags: channel open flags
  119. * eci_svr_channel_handle: channel handle returned from server
  120. * eci_closing: channel in process of being closed
  121. * eci_mutex: channel mutex
  122. *
  123. */
  124. struct event_channel_instance {
  125. SaNameT eci_channel_name;
  126. SaEvtChannelOpenFlagsT eci_open_flags;
  127. uint32_t eci_svr_channel_handle;
  128. SaEvtHandleT eci_instance_handle;
  129. int eci_closing;
  130. pthread_mutex_t eci_mutex;
  131. };
  132. /*
  133. * Event data.
  134. *
  135. * Store event data from saEvtEventAllocate function.
  136. * Store event data from received events.
  137. *
  138. * edi_channel_handle: handle (local) of assocated channel
  139. * edi_patterns: event patterns
  140. * edi_priority: event priority
  141. * edi_retention_time: event's retention time
  142. * edi_pub_name: event's publisher name
  143. * edi_pub_node: event's publisher node
  144. * edi_pub_time: event's publish time
  145. * edi_event_id: event's Id
  146. * edi_event_data: event's data
  147. * edi_event_data_size: size of edi_event_data
  148. * edi_freeing: event is being freed
  149. * edi_mutex: event data mutex
  150. *
  151. */
  152. struct event_data_instance {
  153. SaEvtChannelHandleT edi_channel_handle;
  154. SaEvtEventPatternArrayT edi_patterns;
  155. SaUint8T edi_priority;
  156. SaTimeT edi_retention_time;
  157. SaNameT edi_pub_name;
  158. SaClmNodeIdT edi_pub_node;
  159. SaTimeT edi_pub_time;
  160. SaEvtEventIdT edi_event_id;
  161. void *edi_event_data;
  162. SaSizeT edi_event_data_size;
  163. int edi_freeing;
  164. pthread_mutex_t edi_mutex;
  165. };
  166. #define PUB_SLEEP_NSEC 0L;
  167. #define PUB_SLEEP_SEC 1
  168. #define PUB_SLEEP_TRYS 20
  169. #define min(a,b) ((a) < (b) ? (a) : (b))
  170. /*
  171. * Clean up function for an evt instance (saEvtInitialize) handle
  172. */
  173. static void evtHandleInstanceDestructor(void *instance)
  174. {
  175. struct event_instance *evti = instance;
  176. void **msg;
  177. int empty;
  178. /*
  179. * Empty out the queue if there are any pending messages
  180. */
  181. while ((saQueueIsEmpty(&evti->ei_inq, &empty) == SA_OK) && !empty) {
  182. saQueueItemGet(&evti->ei_inq, (void *)&msg);
  183. saQueueItemRemove(&evti->ei_inq);
  184. free(*msg);
  185. }
  186. /*
  187. * clean up the queue itself
  188. */
  189. if (evti->ei_inq.items) {
  190. free(evti->ei_inq.items);
  191. }
  192. /*
  193. * Disconnect from the server
  194. */
  195. if (evti->ei_fd != -1) {
  196. shutdown(evti->ei_fd, 0);
  197. close(evti->ei_fd);
  198. }
  199. }
  200. /*
  201. * Clean up function for an open channel handle
  202. */
  203. static void chanHandleInstanceDestructor(void *instance)
  204. {
  205. }
  206. /*
  207. * Clean up function for an event handle
  208. */
  209. static void eventHandleInstanceDestructor(void *instance)
  210. {
  211. struct event_data_instance *edi = instance;
  212. int i;
  213. for (i = 0; i < edi->edi_patterns.patternsNumber; i++) {
  214. free(edi->edi_patterns.patterns[i].pattern);
  215. }
  216. if (edi->edi_patterns.patterns) {
  217. free(edi->edi_patterns.patterns);
  218. }
  219. if (edi->edi_event_data) {
  220. free(edi->edi_event_data);
  221. }
  222. }
  223. /*
  224. * The saEvtInitialize() function initializes the Event Service for the
  225. * invoking process. A user of the Event Service must invoke this function
  226. * before it invokes any other function of the Event Service API. Each
  227. * initialization returns a different callback handle that the process
  228. * can use to communicate with that library instance.
  229. */
  230. SaErrorT
  231. saEvtInitialize(
  232. SaEvtHandleT *evt_handle,
  233. const SaEvtCallbacksT *callbacks,
  234. SaVersionT *version)
  235. {
  236. SaErrorT error = SA_OK;
  237. struct event_instance *evti;
  238. /*
  239. * validate the requested version with what we support
  240. */
  241. error = saVersionVerify(&evt_version_database, version);
  242. if (error != SA_OK) {
  243. goto error_nofree;
  244. }
  245. /*
  246. * Allocate instance data, allocate unique handle for instance,
  247. * assign instance data to unique handle
  248. */
  249. error = saHandleCreate(&evt_instance_handle_db, sizeof(*evti),
  250. (void*)evt_handle);
  251. if (error != SA_OK) {
  252. goto error_nofree;
  253. }
  254. error = saHandleInstanceGet(&evt_instance_handle_db, *evt_handle,
  255. (void*)&evti);
  256. if (error != SA_OK) {
  257. goto error_handle_free;
  258. }
  259. memset(evti, 0, sizeof(*evti));
  260. /*
  261. * Save the version so we can check with the event server
  262. * and see if it supports this version.
  263. */
  264. evti->ei_version = *version;
  265. /*
  266. * An inq is needed to store async messages while waiting for a
  267. * sync response
  268. */
  269. error = saQueueInit(&evti->ei_inq, 1024, sizeof(void *));
  270. if (error != SA_OK) {
  271. goto error_handle_put;
  272. }
  273. /*
  274. * Set up communication with the event server
  275. */
  276. error = saServiceConnect(&evti->ei_fd, MESSAGE_REQ_EVT_INIT);
  277. if (error != SA_OK) {
  278. goto error_handle_put;
  279. }
  280. /*
  281. * The callback function is saved in the event instance for
  282. * saEvtDispatch() to use.
  283. */
  284. if (callbacks) {
  285. memcpy(&evti->ei_callback, callbacks,
  286. sizeof(evti->ei_callback));
  287. }
  288. pthread_mutex_init(&evti->ei_mutex, NULL);
  289. saHandleInstancePut(&evt_instance_handle_db, *evt_handle);
  290. return SA_OK;
  291. error_handle_put:
  292. saHandleInstancePut(&evt_instance_handle_db, *evt_handle);
  293. error_handle_free:
  294. (void)saHandleDestroy(&evt_instance_handle_db, *evt_handle);
  295. error_nofree:
  296. return error;
  297. }
  298. /*
  299. * The saEvtSelectionObjectGet() function returns the operating system
  300. * handle selection_object, associated with the handle evt_handle, allowing
  301. * the invoking process to ascertain when callbacks are pending. This
  302. * function allows a process to avoid repeated invoking saEvtDispatch() to
  303. * see if there is a new event, thus, needlessly consuming CPU time. In a
  304. * POSIX environment the system handle could be a file descriptor that is
  305. * used with the poll() or select() system calls to detect incoming callbacks.
  306. */
  307. SaErrorT
  308. saEvtSelectionObjectGet(
  309. SaEvtHandleT evt_handle,
  310. SaSelectionObjectT *selection_object)
  311. {
  312. struct event_instance *evti;
  313. SaErrorT error;
  314. error = saHandleInstanceGet(&evt_instance_handle_db, evt_handle,
  315. (void *)&evti);
  316. if (error != SA_OK) {
  317. return error;
  318. }
  319. *selection_object = evti->ei_fd;
  320. saHandleInstancePut(&evt_instance_handle_db, evt_handle);
  321. return SA_OK;
  322. }
  323. /*
  324. * Alocate an event data structure and associated handle to be
  325. * used to supply event data to a call back function.
  326. */
  327. static SaErrorT make_event(SaEvtEventHandleT *event_handle,
  328. struct lib_event_data *evt)
  329. {
  330. struct event_data_instance *edi;
  331. SaEvtEventPatternT *pat;
  332. SaUint8T *str;
  333. SaErrorT error;
  334. int i;
  335. error = saHandleCreate(&event_handle_db, sizeof(*edi),
  336. (void*)event_handle);
  337. if (error != SA_OK) {
  338. goto make_evt_done;
  339. }
  340. error = saHandleInstanceGet(&event_handle_db, *event_handle,
  341. (void*)&edi);
  342. if (error != SA_OK) {
  343. goto make_evt_done;
  344. }
  345. memset(edi, 0, sizeof(*edi));
  346. pthread_mutex_init(&edi->edi_mutex, NULL);
  347. edi->edi_freeing = 0;
  348. edi->edi_channel_handle = evt->led_lib_channel_handle;
  349. edi->edi_priority = evt->led_priority;
  350. edi->edi_retention_time = evt->led_retention_time;
  351. edi->edi_pub_node = evt->led_publisher_node_id;
  352. edi->edi_pub_time = evt->led_publish_time;
  353. edi->edi_event_data_size = evt->led_user_data_size;
  354. edi->edi_event_id = evt->led_event_id;
  355. edi->edi_pub_name = evt->led_publisher_name;
  356. if (edi->edi_event_data_size) {
  357. edi->edi_event_data = malloc(edi->edi_event_data_size);
  358. memcpy(edi->edi_event_data,
  359. evt->led_body + evt->led_user_data_offset,
  360. edi->edi_event_data_size);
  361. }
  362. /*
  363. * Move the pattern bits into the SaEvtEventPatternArrayT
  364. */
  365. edi->edi_patterns.patternsNumber = evt->led_patterns_number;
  366. edi->edi_patterns.patterns = malloc(sizeof(SaEvtEventPatternT) *
  367. edi->edi_patterns.patternsNumber);
  368. pat = (SaEvtEventPatternT *)evt->led_body;
  369. str = evt->led_body + sizeof(SaEvtEventPatternT) *
  370. edi->edi_patterns.patternsNumber;
  371. for (i = 0; i < evt->led_patterns_number; i++) {
  372. edi->edi_patterns.patterns[i].patternSize = pat->patternSize;
  373. edi->edi_patterns.patterns[i].pattern = malloc(pat->patternSize);
  374. if (!edi->edi_patterns.patterns[i].pattern) {
  375. printf("make_event: couldn't alloc %d bytes\n", pat->patternSize);
  376. error = SA_ERR_NO_MEMORY;
  377. break;
  378. }
  379. memcpy(edi->edi_patterns.patterns[i].pattern,
  380. str, pat->patternSize);
  381. str += pat->patternSize;
  382. pat++;
  383. }
  384. saHandleInstancePut (&event_handle_db, *event_handle);
  385. make_evt_done:
  386. return error;
  387. }
  388. /*
  389. * The saEvtDispatch() function invokes, in the context of the calling
  390. * thread, one or all of the pending callbacks for the handle evt_handle.
  391. */
  392. SaErrorT
  393. saEvtDispatch(
  394. SaEvtHandleT evt_handle,
  395. SaDispatchFlagsT dispatch_flags)
  396. {
  397. struct pollfd ufds;
  398. int timeout = -1;
  399. SaErrorT error;
  400. int dispatch_avail;
  401. struct event_instance *evti;
  402. SaEvtEventHandleT event_handle;
  403. SaEvtCallbacksT callbacks;
  404. struct res_header **queue_msg;
  405. struct res_header *msg = 0;
  406. int empty;
  407. int ignore_dispatch = 0;
  408. int cont = 1; /* always continue do loop except when set to 0 */
  409. int poll_fd;
  410. struct message_overlay *dispatch_data;
  411. struct lib_event_data *evt;
  412. struct res_evt_event_data res;
  413. error = saHandleInstanceGet(&evt_instance_handle_db, evt_handle,
  414. (void *)&evti);
  415. if (error != SA_OK) {
  416. return error;
  417. }
  418. /*
  419. * Timeout instantly for SA_DISPATCH_ALL
  420. */
  421. if (dispatch_flags == SA_DISPATCH_ALL) {
  422. timeout = 0;
  423. }
  424. do {
  425. poll_fd = evti->ei_fd;
  426. ufds.fd = poll_fd;
  427. ufds.events = POLLIN;
  428. ufds.revents = 0;
  429. pthread_mutex_lock(&evti->ei_mutex);
  430. saQueueIsEmpty(&evti->ei_inq, &empty);
  431. /*
  432. * Read from the socket if there is nothing in
  433. * our queue.
  434. */
  435. if (empty == 1) {
  436. pthread_mutex_unlock(&evti->ei_mutex);
  437. error = saPollRetry(&ufds, 1, timeout);
  438. if (error != SA_OK) {
  439. goto error_nounlock;
  440. }
  441. pthread_mutex_lock(&evti->ei_mutex);
  442. }
  443. /*
  444. * Handle has been finalized in another thread
  445. */
  446. if (evti->ei_finalize == 1) {
  447. error = SA_OK;
  448. pthread_mutex_unlock(&evti->ei_mutex);
  449. goto error_unlock;
  450. }
  451. dispatch_avail = (ufds.revents & POLLIN) | (empty == 0);
  452. if (dispatch_avail == 0 && dispatch_flags == SA_DISPATCH_ALL) {
  453. pthread_mutex_unlock(&evti->ei_mutex);
  454. break; /* exit do while cont is 1 loop */
  455. } else
  456. if (dispatch_avail == 0) {
  457. pthread_mutex_unlock(&evti->ei_mutex);
  458. continue; /* next poll */
  459. }
  460. saQueueIsEmpty(&evti->ei_inq, &empty);
  461. if (empty == 0) {
  462. /*
  463. * Queue is not empty, read data from queue
  464. */
  465. saQueueItemGet(&evti->ei_inq, (void *)&queue_msg);
  466. msg = *queue_msg;
  467. dispatch_data = (struct message_overlay *)msg;
  468. saQueueItemRemove(&evti->ei_inq);
  469. } else {
  470. /*
  471. * Queue empty, read response from socket
  472. */
  473. dispatch_data = (struct message_overlay *)&evti->ei_message;
  474. error = saRecvRetry(evti->ei_fd, &dispatch_data->header,
  475. sizeof(struct res_header), MSG_WAITALL | MSG_NOSIGNAL);
  476. if (error != SA_OK) {
  477. pthread_mutex_unlock(&evti->ei_mutex);
  478. goto error_unlock;
  479. }
  480. if (dispatch_data->header.size > sizeof(struct res_header)) {
  481. error = saRecvRetry(evti->ei_fd, dispatch_data->data,
  482. dispatch_data->header.size - sizeof(struct res_header),
  483. MSG_WAITALL | MSG_NOSIGNAL);
  484. if (error != SA_OK) {
  485. pthread_mutex_unlock(&evti->ei_mutex);
  486. goto error_unlock;
  487. }
  488. }
  489. }
  490. /*
  491. * Make copy of callbacks, message data, unlock instance,
  492. * and call callback. A risk of this dispatch method is that
  493. * the callback routines may operate at the same time that
  494. * EvtFinalize has been called in another thread.
  495. */
  496. memcpy(&callbacks, &evti->ei_callback, sizeof(evti->ei_callback));
  497. pthread_mutex_unlock(&evti->ei_mutex);
  498. /*
  499. * Dispatch incoming response
  500. */
  501. switch (dispatch_data->header.id) {
  502. case MESSAGE_RES_LIB_ACTIVATEPOLL:
  503. /*
  504. * This is a do nothing message which the node
  505. * executive sends to activate the file evt_handle
  506. * in poll when the library has queued a message into
  507. * evti->ei_inq. The dispatch is ignored for the
  508. * following two cases:
  509. * 1) setting of timeout to zero for the
  510. * DISPATCH_ALL case
  511. * 2) expiration of the do loop for the
  512. * DISPATCH_ONE case
  513. */
  514. ignore_dispatch = 1;
  515. break;
  516. case MESSAGE_RES_EVT_AVAILABLE:
  517. /*
  518. * There are events available. Send a request for one and then
  519. * dispatch it.
  520. */
  521. evt = (struct lib_event_data *)&evti->ei_message;
  522. res.evd_head.id = MESSAGE_REQ_EVT_EVENT_DATA;
  523. res.evd_head.size = sizeof(res);
  524. error = saSendRetry(evti->ei_fd, &res, sizeof(res), MSG_NOSIGNAL);
  525. if (error != SA_OK) {
  526. printf("MESSAGE_RES_EVT_AVAILABLE: send failed: %d\n", error);
  527. break;
  528. }
  529. error = saRecvQueue(evti->ei_fd, evt, &evti->ei_inq,
  530. MESSAGE_RES_EVT_EVENT_DATA);
  531. if (error != SA_OK) {
  532. printf("MESSAGE_RES_EVT_AVAILABLE: receive failed: %d\n",
  533. error);
  534. break;
  535. }
  536. /*
  537. * No data available. This is OK.
  538. */
  539. if (evt->led_head.error == SA_ERR_NOT_EXIST) {
  540. // printf("MESSAGE_RES_EVT_AVAILABLE: No event data\n");
  541. error = SA_OK;
  542. break;
  543. }
  544. if (evt->led_head.error != SA_OK) {
  545. error = evt->led_head.error;
  546. printf("MESSAGE_RES_EVT_AVAILABLE: Error returned: %d\n",
  547. error);
  548. break;
  549. }
  550. error = make_event(&event_handle, evt);
  551. if (error != SA_OK) {
  552. break;
  553. }
  554. callbacks.saEvtEventDeliverCallback(evt->led_sub_id, event_handle,
  555. evt->led_user_data_size);
  556. break;
  557. case MESSAGE_RES_EVT_CHAN_OPEN_CALLBACK:
  558. /*
  559. * TODO: do something here
  560. */
  561. printf("Dispatch: Open callback\n");
  562. break;
  563. default:
  564. printf("Dispatch: Bad message type 0x%x\n",
  565. dispatch_data->header.id);
  566. error = SA_ERR_LIBRARY;
  567. goto error_nounlock;
  568. break;
  569. }
  570. /*
  571. * If empty is zero it means the we got the
  572. * message from the queue and we are responsible
  573. * for freeing it.
  574. */
  575. if (empty == 0) {
  576. free(msg);
  577. }
  578. /*
  579. * Determine if more messages should be processed
  580. */
  581. switch (dispatch_flags) {
  582. case SA_DISPATCH_ONE:
  583. if (ignore_dispatch) {
  584. ignore_dispatch = 0;
  585. } else {
  586. cont = 0;
  587. }
  588. break;
  589. case SA_DISPATCH_ALL:
  590. if (ignore_dispatch) {
  591. ignore_dispatch = 0;
  592. }
  593. break;
  594. case SA_DISPATCH_BLOCKING:
  595. break;
  596. }
  597. } while (cont);
  598. error_unlock:
  599. saHandleInstancePut(&evt_instance_handle_db, evt_handle);
  600. error_nounlock:
  601. return error;
  602. }
  603. /*
  604. * The saEvtFinalize() function closes the association, represented by the
  605. * evt_handle parameter, between the process and the Event Service. It may
  606. * free up resources.
  607. * This function cannot be invoked before the process has invoked the
  608. * corresponding saEvtInitialize() function for the Event Service. After
  609. * this function is invoked, the selection object is no longer valid.
  610. * Moreover, the Event Service is unavailable for further use unless it is
  611. * reinitialized using the saEvtInitialize() function.
  612. */
  613. SaErrorT
  614. saEvtFinalize(SaEvtHandleT evt_handle)
  615. {
  616. struct event_instance *evti;
  617. SaErrorT error;
  618. error = saHandleInstanceGet(&evt_instance_handle_db, evt_handle,
  619. (void *)&evti);
  620. if (error != SA_OK) {
  621. return error;
  622. }
  623. pthread_mutex_lock(&evti->ei_mutex);
  624. /*
  625. * Another thread has already started finalizing
  626. */
  627. if (evti->ei_finalize) {
  628. pthread_mutex_unlock(&evti->ei_mutex);
  629. saHandleInstancePut(&evt_instance_handle_db, evt_handle);
  630. return SA_ERR_BAD_HANDLE;
  631. }
  632. evti->ei_finalize = 1;
  633. saActivatePoll(evti->ei_fd);
  634. pthread_mutex_unlock(&evti->ei_mutex);
  635. saHandleDestroy(&evt_instance_handle_db, evt_handle);
  636. saHandleInstancePut(&evt_instance_handle_db, evt_handle);
  637. return error;
  638. }
  639. /*
  640. * The saEvtChannelOpen() function creates a new event channel or open an
  641. * existing channel. The saEvtChannelOpen() function is a blocking operation
  642. * and returns a new event channel handle. An event channel may be opened
  643. * multiple times by the same or different processes for publishing, and
  644. * subscribing to, events. If a process opens an event channel multiple
  645. * times, it is possible to receive the same event multiple times. However,
  646. * a process shall never receive an event more than once on a particular
  647. * event channel handle. If a process opens a channel twice and an event is
  648. * matched on both open channels, the Event Service performs two
  649. * callbacks -- one for each opened channel.
  650. */
  651. SaErrorT
  652. saEvtChannelOpen(
  653. SaEvtHandleT evt_handle,
  654. const SaNameT *channel_name,
  655. SaEvtChannelOpenFlagsT channel_open_flags,
  656. SaTimeT timeout,
  657. SaEvtChannelHandleT *channel_handle)
  658. {
  659. struct event_instance *evti;
  660. struct req_evt_channel_open req;
  661. struct res_evt_channel_open res;
  662. struct event_channel_instance *eci;
  663. SaErrorT error;
  664. error = saHandleInstanceGet(&evt_instance_handle_db, evt_handle,
  665. (void*)&evti);
  666. if (error != SA_OK) {
  667. goto chan_open_done;
  668. }
  669. /*
  670. * create a handle for this open channel
  671. */
  672. error = saHandleCreate(&channel_handle_db, sizeof(*eci),
  673. (void*)channel_handle);
  674. if (error != SA_OK) {
  675. goto chan_open_put;
  676. }
  677. error = saHandleInstanceGet(&channel_handle_db, *channel_handle,
  678. (void*)&eci);
  679. if (error != SA_OK) {
  680. saHandleDestroy(&channel_handle_db, *channel_handle);
  681. goto chan_open_put;
  682. }
  683. /*
  684. * Send the request to the server and wait for a response
  685. */
  686. req.ico_head.size = sizeof(req);
  687. req.ico_head.id = MESSAGE_REQ_EVT_OPEN_CHANNEL;
  688. req.ico_c_handle = *channel_handle;
  689. req.ico_timeout = timeout;
  690. req.ico_open_flag = channel_open_flags;
  691. req.ico_channel_name = *channel_name;
  692. pthread_mutex_lock(&evti->ei_mutex);
  693. error = saSendRetry(evti->ei_fd, &req, sizeof(req), MSG_NOSIGNAL);
  694. if (error != SA_OK) {
  695. pthread_mutex_unlock (&evti->ei_mutex);
  696. goto chan_open_free;
  697. }
  698. error = saRecvQueue(evti->ei_fd, &res, &evti->ei_inq,
  699. MESSAGE_RES_EVT_OPEN_CHANNEL);
  700. pthread_mutex_unlock (&evti->ei_mutex);
  701. if (error != SA_OK) {
  702. goto chan_open_free;
  703. }
  704. error = res.ico_head.error;
  705. if (error != SA_OK) {
  706. goto chan_open_free;
  707. }
  708. eci->eci_svr_channel_handle = res.ico_channel_handle;
  709. eci->eci_channel_name = *channel_name;
  710. eci->eci_open_flags = channel_open_flags;
  711. eci->eci_instance_handle = evt_handle;
  712. eci->eci_closing = 0;
  713. pthread_mutex_init(&eci->eci_mutex, NULL);
  714. saHandleInstancePut (&evt_instance_handle_db, evt_handle);
  715. saHandleInstancePut (&channel_handle_db, *channel_handle);
  716. return SA_OK;
  717. chan_open_free:
  718. saHandleDestroy(&channel_handle_db, *channel_handle);
  719. saHandleInstancePut (&channel_handle_db, *channel_handle);
  720. chan_open_put:
  721. saHandleInstancePut (&evt_instance_handle_db, evt_handle);
  722. chan_open_done:
  723. return error;
  724. }
  725. /*
  726. * The saEvtChannelClose() function closes an event channel and frees
  727. * resources allo-cated for that event channel in the invoking process. If
  728. * the event channel is not refer-enced by any process and does not hold
  729. * any events with non-zero retention time, the Event Service automatically
  730. * deletes the event channel from the cluster namespace.
  731. */
  732. SaErrorT
  733. saEvtChannelClose(SaEvtChannelHandleT channel_handle)
  734. {
  735. SaErrorT error;
  736. struct event_instance *evti;
  737. struct event_channel_instance *eci;
  738. struct req_evt_channel_close req;
  739. struct res_evt_channel_close res;
  740. error = saHandleInstanceGet(&channel_handle_db, channel_handle,
  741. (void*)&eci);
  742. if (error != SA_OK) {
  743. goto chan_close_done;
  744. }
  745. /*
  746. * get the evt handle for the fd
  747. */
  748. error = saHandleInstanceGet(&evt_instance_handle_db,
  749. eci->eci_instance_handle, (void*)&evti);
  750. if (error != SA_OK) {
  751. goto chan_close_put1;
  752. }
  753. /*
  754. * Make sure that the channel isn't being closed elsewhere
  755. */
  756. pthread_mutex_lock(&eci->eci_mutex);
  757. if (eci->eci_closing) {
  758. pthread_mutex_unlock(&eci->eci_mutex);
  759. saHandleInstancePut(&channel_handle_db, channel_handle);
  760. return SA_ERR_BAD_HANDLE;
  761. }
  762. eci->eci_closing = 1;
  763. saActivatePoll(evti->ei_fd);
  764. pthread_mutex_unlock(&eci->eci_mutex);
  765. /*
  766. * Send the request to the server and wait for a response
  767. */
  768. req.icc_head.size = sizeof(req);
  769. req.icc_head.id = MESSAGE_REQ_EVT_CLOSE_CHANNEL;
  770. req.icc_channel_handle = eci->eci_svr_channel_handle;
  771. pthread_mutex_lock(&evti->ei_mutex);
  772. error = saSendRetry(evti->ei_fd, &req, sizeof(req), MSG_NOSIGNAL);
  773. if (error != SA_OK) {
  774. pthread_mutex_unlock(&evti->ei_mutex);
  775. eci->eci_closing = 0;
  776. goto chan_close_put2;
  777. }
  778. error = saRecvQueue(evti->ei_fd, &res, &evti->ei_inq,
  779. MESSAGE_RES_EVT_CLOSE_CHANNEL);
  780. pthread_mutex_unlock(&evti->ei_mutex);
  781. if (error != SA_OK) {
  782. eci->eci_closing = 0;
  783. goto chan_close_put2;
  784. }
  785. error = res.icc_head.error;
  786. saHandleInstancePut(&evt_instance_handle_db,
  787. eci->eci_instance_handle);
  788. saHandleDestroy(&channel_handle_db, channel_handle);
  789. saHandleInstancePut(&channel_handle_db, channel_handle);
  790. return error;
  791. chan_close_put2:
  792. saHandleInstancePut(&evt_instance_handle_db,
  793. eci->eci_instance_handle);
  794. chan_close_put1:
  795. saHandleInstancePut(&channel_handle_db, channel_handle);
  796. chan_close_done:
  797. return error;
  798. }
  799. SaErrorT
  800. saEvtChannelOpenAsync(SaEvtHandleT evt_handle,
  801. SaInvocationT invocation,
  802. const SaNameT *channel_name,
  803. SaEvtChannelOpenFlagsT channel_open_flags)
  804. {
  805. /*
  806. * TODO: Fill in code
  807. */
  808. return SA_ERR_LIBRARY;
  809. }
  810. SaErrorT
  811. SaEvtChannelUnlink(
  812. SaEvtHandleT evtHandle,
  813. const SaNameT *channelName)
  814. {
  815. /*
  816. * TODO: Fill in code
  817. */
  818. return SA_ERR_LIBRARY;
  819. }
  820. /*
  821. * The saEvtEventAllocate() function allocates memory for the event, but not
  822. * for the eventHandle, and initializes all event attributes to default values.
  823. * The event allocated by saEvtEventAllocate() is freed by invoking
  824. * saEvtEventFree().
  825. * The memory associated with the eventHandle is not deallocated by the
  826. * saEvtEventAllocate() function or the saEvtEventFree() function. It is the
  827. * responsibility of the invoking process to deallocate the memory for the
  828. * eventHandle when the process has published the event and has freed the
  829. * event by invoking saEvtEventFree().
  830. */
  831. SaErrorT
  832. saEvtEventAllocate(
  833. const SaEvtChannelHandleT channel_handle,
  834. SaEvtEventHandleT *event_handle)
  835. {
  836. SaErrorT error;
  837. struct event_data_instance *edi;
  838. struct event_instance *evti;
  839. struct event_channel_instance *eci;
  840. error = saHandleInstanceGet(&channel_handle_db, channel_handle,
  841. (void*)&eci);
  842. if (error != SA_OK) {
  843. goto alloc_done;
  844. }
  845. error = saHandleInstanceGet(&evt_instance_handle_db,
  846. eci->eci_instance_handle, (void*)&evti);
  847. if (error != SA_OK) {
  848. goto alloc_put1;
  849. }
  850. error = saHandleCreate(&event_handle_db, sizeof(*edi),
  851. (void*)event_handle);
  852. if (error != SA_OK) {
  853. goto alloc_put2;
  854. }
  855. error = saHandleInstanceGet(&event_handle_db, *event_handle,
  856. (void*)&edi);
  857. if (error != SA_OK) {
  858. goto alloc_put2;
  859. }
  860. memset(edi, 0, sizeof(*edi));
  861. pthread_mutex_init(&edi->edi_mutex, NULL);
  862. edi->edi_freeing = 0;
  863. edi->edi_channel_handle = channel_handle;
  864. edi->edi_pub_node = evti->ei_node_id;
  865. edi->edi_priority = SA_EVT_LOWEST_PRIORITY;
  866. saHandleInstancePut (&event_handle_db, *event_handle);
  867. alloc_put2:
  868. saHandleInstancePut (&evt_instance_handle_db, eci->eci_instance_handle);
  869. alloc_put1:
  870. saHandleInstancePut (&channel_handle_db, edi->edi_channel_handle);
  871. alloc_done:
  872. return error;
  873. }
  874. /*
  875. * The saEvtEventFree() function gives the Event Service premission to
  876. * deallocate the memory that contains the attributes of the event that is
  877. * associated with eventHandle. The function is used to free events allocated
  878. * by saEvtEventAllocate() and by saEvtEventDeliverCallback().
  879. */
  880. SaErrorT
  881. saEvtEventFree(SaEvtEventHandleT event_handle)
  882. {
  883. SaErrorT error;
  884. struct event_data_instance *edi;
  885. error = saHandleInstanceGet(&event_handle_db, event_handle,
  886. (void*)&edi);
  887. if (error != SA_OK) {
  888. goto evt_free_done;
  889. }
  890. /*
  891. * Make sure that the event isn't being freed elsewhere
  892. */
  893. pthread_mutex_lock(&edi->edi_mutex);
  894. if (edi->edi_freeing) {
  895. pthread_mutex_unlock(&edi->edi_mutex);
  896. saHandleInstancePut(&event_handle_db, event_handle);
  897. return SA_ERR_BAD_HANDLE;
  898. }
  899. edi->edi_freeing = 1;
  900. pthread_mutex_unlock(&edi->edi_mutex);
  901. saHandleDestroy(&event_handle_db, event_handle);
  902. saHandleInstancePut(&event_handle_db, event_handle);
  903. evt_free_done:
  904. return error;
  905. }
  906. /*
  907. * This function may be used to assign writeable event attributes. It takes
  908. * as arguments an event handle event_handle and the attribute to be set in
  909. * the event structure of the event with that handle. Note: The only
  910. * attributes that a process can set are the pattern_array, priority,
  911. * retention_time and publisher_name attributes.
  912. */
  913. SaErrorT
  914. saEvtEventAttributesSet(
  915. const SaEvtEventHandleT event_handle,
  916. const SaEvtEventPatternArrayT *pattern_array,
  917. SaEvtEventPriorityT priority,
  918. SaTimeT retention_time,
  919. const SaNameT *publisher_name)
  920. {
  921. SaEvtEventPatternT *oldpatterns;
  922. SaSizeT oldnumber;
  923. SaErrorT error;
  924. struct event_data_instance *edi;
  925. int i;
  926. error = saHandleInstanceGet(&event_handle_db, event_handle,
  927. (void*)&edi);
  928. if (error != SA_OK) {
  929. goto attr_set_done;
  930. }
  931. pthread_mutex_lock(&edi->edi_mutex);
  932. edi->edi_priority = priority;
  933. edi->edi_retention_time = retention_time;
  934. /*
  935. * publisher_name or pattern_array not allowed to be NULL
  936. */
  937. if (!publisher_name || !pattern_array) {
  938. error = SA_ERR_INVALID_PARAM;
  939. goto attr_set_unlock;
  940. }
  941. edi->edi_pub_name = *publisher_name;
  942. oldpatterns = edi->edi_patterns.patterns;
  943. oldnumber = edi->edi_patterns.patternsNumber;
  944. edi->edi_patterns.patterns = 0;
  945. edi->edi_patterns.patterns = malloc(sizeof(SaEvtEventPatternT) *
  946. pattern_array->patternsNumber);
  947. if (!edi->edi_patterns.patterns) {
  948. error = SA_ERR_NO_MEMORY;
  949. goto attr_set_done_reset;
  950. }
  951. edi->edi_patterns.patternsNumber = pattern_array->patternsNumber;
  952. /*
  953. * copy the patterns from the caller. allocating memory for
  954. * of all the strings.
  955. */
  956. for (i = 0; i < pattern_array->patternsNumber; i++) {
  957. edi->edi_patterns.patterns[i].pattern =
  958. malloc(pattern_array->patterns[i].patternSize);
  959. if (!edi->edi_patterns.patterns[i].pattern) {
  960. int j;
  961. for (j = 0; j < i; j++) {
  962. free(edi->edi_patterns.patterns[j].pattern);
  963. }
  964. free(edi->edi_patterns.patterns);
  965. error = SA_ERR_NO_MEMORY;
  966. goto attr_set_done_reset;
  967. }
  968. memcpy(edi->edi_patterns.patterns[i].pattern,
  969. pattern_array->patterns[i].pattern,
  970. pattern_array->patterns[i].patternSize);
  971. edi->edi_patterns.patterns[i].patternSize =
  972. pattern_array->patterns[i].patternSize;
  973. }
  974. /*
  975. * free up the old pattern memory
  976. */
  977. if (oldpatterns) {
  978. for (i = 0; i < oldnumber; i++) {
  979. if (oldpatterns[i].pattern) {
  980. free(oldpatterns[i].pattern);
  981. }
  982. }
  983. free (oldpatterns);
  984. }
  985. goto attr_set_unlock;
  986. attr_set_done_reset:
  987. edi->edi_patterns.patterns = oldpatterns;
  988. edi->edi_patterns.patternsNumber = oldnumber;
  989. attr_set_unlock:
  990. pthread_mutex_unlock(&edi->edi_mutex);
  991. saHandleInstancePut(&event_handle_db, event_handle);
  992. attr_set_done:
  993. return error;
  994. }
  995. /*
  996. * This function takes as parameters an event handle eventHandle and the
  997. * attributes of the event with that handle. The function retrieves the
  998. * value of the attributes for the event and stores them at the address
  999. * provided for the out parameters.
  1000. * It is the responsibility of the invoking process to allocate memory for
  1001. * the out parameters before it invokes this function. The Event Service
  1002. * assigns the out values into that memory. For each of the out, or in/out,
  1003. * parameters, if the invoking process provides a NULL reference, the
  1004. * Availability Management Framework does not return the out value.
  1005. * Similarly, it is the responsibility of the invoking process to allocate
  1006. * memory for the pattern_array.
  1007. */
  1008. SaErrorT
  1009. saEvtEventAttributesGet(
  1010. SaEvtEventHandleT event_handle,
  1011. SaEvtEventPatternArrayT *pattern_array,
  1012. SaEvtEventPriorityT *priority,
  1013. SaTimeT *retention_time,
  1014. SaNameT *publisher_name,
  1015. SaTimeT *publish_time,
  1016. SaEvtEventIdT *event_id)
  1017. {
  1018. SaErrorT error;
  1019. struct event_data_instance *edi;
  1020. SaSizeT npats;
  1021. int i;
  1022. error = saHandleInstanceGet(&event_handle_db, event_handle,
  1023. (void*)&edi);
  1024. if (error != SA_OK) {
  1025. goto attr_get_done;
  1026. }
  1027. pthread_mutex_lock(&edi->edi_mutex);
  1028. /*
  1029. * Go through the args and send back information if the pointer
  1030. * isn't NULL
  1031. */
  1032. if (event_id) {
  1033. *event_id = edi->edi_event_id;
  1034. }
  1035. if (publish_time) {
  1036. *publish_time = edi->edi_pub_time;
  1037. }
  1038. if (publisher_name) {
  1039. *publisher_name = edi->edi_pub_name;
  1040. }
  1041. if (retention_time) {
  1042. *retention_time = edi->edi_retention_time;
  1043. }
  1044. if (priority) {
  1045. *priority = edi->edi_priority;
  1046. }
  1047. if (!pattern_array) {
  1048. goto attr_get_unlock;
  1049. }
  1050. npats = min(pattern_array->patternsNumber,
  1051. edi->edi_patterns.patternsNumber);
  1052. /*
  1053. * We set the returned number of patterns to the actual
  1054. * pattern count of the event. This way the caller can tell
  1055. * if it got all the possible patterns. If the returned number
  1056. * is more that the number supplied, then some available patterns
  1057. * were not returned.
  1058. *
  1059. * The same thing happens when copying the pattern strings.
  1060. */
  1061. pattern_array->patternsNumber = edi->edi_patterns.patternsNumber;
  1062. for (i = 0; i < npats; i++) {
  1063. memcpy(pattern_array->patterns[i].pattern,
  1064. edi->edi_patterns.patterns[i].pattern,
  1065. min(pattern_array->patterns[i].patternSize,
  1066. edi->edi_patterns.patterns[i].patternSize));
  1067. pattern_array->patterns[i].patternSize =
  1068. edi->edi_patterns.patterns[i].patternSize;
  1069. }
  1070. attr_get_unlock:
  1071. pthread_mutex_unlock(&edi->edi_mutex);
  1072. saHandleInstancePut(&event_handle_db, event_handle);
  1073. attr_get_done:
  1074. return error;
  1075. }
  1076. /*
  1077. * The saEvtEventDataGet() function allows a process to retrieve the data
  1078. * associated with an event previously delivered by
  1079. * saEvtEventDeliverCallback().
  1080. */
  1081. SaErrorT
  1082. saEvtEventDataGet(
  1083. const SaEvtEventHandleT event_handle,
  1084. void *event_data,
  1085. SaSizeT *event_data_size)
  1086. {
  1087. SaErrorT error = SA_OK;
  1088. struct event_data_instance *edi;
  1089. SaSizeT xfsize;
  1090. if (!event_data || !event_data_size) {
  1091. goto data_get_done;
  1092. }
  1093. error = saHandleInstanceGet(&event_handle_db, event_handle,
  1094. (void*)&edi);
  1095. if (error != SA_OK) {
  1096. goto data_get_done;
  1097. }
  1098. pthread_mutex_lock(&edi->edi_mutex);
  1099. if (edi->edi_event_data && edi->edi_event_data_size) {
  1100. xfsize = min(*event_data_size, edi->edi_event_data_size);
  1101. *event_data_size = edi->edi_event_data_size;
  1102. memcpy(event_data, edi->edi_event_data, xfsize);
  1103. } else {
  1104. *event_data_size = 0;
  1105. }
  1106. pthread_mutex_unlock(&edi->edi_mutex);
  1107. saHandleInstancePut(&event_handle_db, event_handle);
  1108. data_get_done:
  1109. return error;
  1110. }
  1111. /*
  1112. * Calculate the size in bytes for patterns
  1113. */
  1114. static size_t patt_size(const SaEvtEventPatternArrayT *patterns)
  1115. {
  1116. int i;
  1117. size_t size = sizeof(SaEvtEventPatternArrayT);
  1118. for (i = 0; i < patterns->patternsNumber; i++) {
  1119. size += sizeof(SaEvtEventPatternT);
  1120. size += patterns->patterns[i].patternSize;
  1121. }
  1122. return size;
  1123. }
  1124. /*
  1125. * copy patterns to a form for sending to the server
  1126. */
  1127. static uint32_t aispatt_to_evt_patt(const SaEvtEventPatternArrayT *patterns,
  1128. void *data)
  1129. {
  1130. int i;
  1131. SaEvtEventPatternT *pats = data;
  1132. SaUint8T *str = (SaUint8T *)pats +
  1133. (patterns->patternsNumber * sizeof(*pats));
  1134. /*
  1135. * Pointers are replaced with offsets into the data array. These
  1136. * will be later converted back into pointers when received as events.
  1137. */
  1138. for (i = 0; i < patterns->patternsNumber; i++) {
  1139. memcpy(str, patterns->patterns[i].pattern,
  1140. patterns->patterns[i].patternSize);
  1141. pats->patternSize = patterns->patterns[i].patternSize;
  1142. pats->pattern = (SaUint8T *)((void *)str - data);
  1143. str += patterns->patterns[i].patternSize;
  1144. pats++;
  1145. }
  1146. return patterns->patternsNumber;
  1147. }
  1148. /*
  1149. * Calculate the size in bytes for filters
  1150. */
  1151. static size_t filt_size(const SaEvtEventFilterArrayT *filters)
  1152. {
  1153. int i;
  1154. size_t size = sizeof(SaEvtEventFilterArrayT);
  1155. for (i = 0; i < filters->filtersNumber; i++) {
  1156. size += sizeof(SaEvtEventFilterT);
  1157. size += filters->filters[i].filter.patternSize;
  1158. }
  1159. return size;
  1160. }
  1161. /*
  1162. * Convert the Sa filters to a form that can be sent over the network
  1163. * i.e. replace pointers with offsets. The pointers will be reconstituted
  1164. * by the receiver.
  1165. */
  1166. static uint32_t aisfilt_to_evt_filt(const SaEvtEventFilterArrayT *filters,
  1167. void *data)
  1168. {
  1169. int i;
  1170. SaEvtEventFilterArrayT *filta = data;
  1171. SaEvtEventFilterT *filts = data + sizeof(SaEvtEventFilterArrayT);
  1172. SaUint8T *str = (SaUint8T *)filts +
  1173. (filters->filtersNumber * sizeof(*filts));
  1174. /*
  1175. * Pointers are replaced with offsets into the data array. These
  1176. * will be later converted back into pointers by the evt server.
  1177. */
  1178. filta->filters = (SaEvtEventFilterT *)((void *)filts - data);
  1179. filta->filtersNumber = filters->filtersNumber;
  1180. for (i = 0; i < filters->filtersNumber; i++) {
  1181. filts->filterType = filters->filters[i].filterType;
  1182. filts->filter.patternSize =
  1183. filters->filters[i].filter.patternSize;
  1184. memcpy(str,
  1185. filters->filters[i].filter.pattern,
  1186. filters->filters[i].filter.patternSize);
  1187. filts->filter.pattern = (SaUint8T *)((void *)str - data);
  1188. str += filters->filters[i].filter.patternSize;
  1189. filts++;
  1190. }
  1191. return filters->filtersNumber;
  1192. }
  1193. /*
  1194. * The saEvtEventPublish() function publishes an event on the channel
  1195. * designated by channel_handle. The event to be published consists of a
  1196. * standard set of attributes (the event header) and an optional data part.
  1197. * Before an event can be published, the publisher process must invoke the
  1198. * saEvtEventPatternArraySet() function to set the event patterns. The event
  1199. * is delivered to subscribers whose subscription filter matches the event
  1200. * patterns.
  1201. * When the Event Service publishes an event, it automatically sets
  1202. * the following readonly event attributes:
  1203. * - Event attribute time
  1204. * - Event publisher identifier
  1205. * - Event publisher node identifier
  1206. * - Event identifier
  1207. * In addition to the event attributes, a process can supply values for the
  1208. * eventData and eventDataSize parameters for publication as part of the
  1209. * event. The data portion of the event may be at most SA_EVT_DATA_MAX_LEN
  1210. * bytes in length.
  1211. * The process may assume that the invocation of saEvtEventPublish() copies
  1212. * all pertinent parameters, including the memory associated with the
  1213. * eventHandle and eventData parameters, to its own local memory. However,
  1214. * the invocation does not automatically deallocate memory associated with
  1215. * the eventHandle and eventData parameters. It is the responsibility of the
  1216. * invoking process to deallocate the memory for those parameters after
  1217. * saEvtEventPublish() returns.
  1218. */
  1219. SaErrorT
  1220. saEvtEventPublish(
  1221. const SaEvtEventHandleT event_handle,
  1222. const void *event_data,
  1223. SaSizeT event_data_size,
  1224. SaEvtEventIdT *eventid)
  1225. {
  1226. SaErrorT error;
  1227. struct event_data_instance *edi;
  1228. struct event_instance *evti;
  1229. struct event_channel_instance *eci;
  1230. struct lib_event_data *req;
  1231. struct res_evt_event_publish res;
  1232. size_t pattern_size;
  1233. struct event_pattern *patterns;
  1234. void *data_start;
  1235. int pub_sleep_trys = PUB_SLEEP_TRYS;
  1236. if (event_data_size > SA_EVT_DATA_MAX_LEN) {
  1237. error = SA_ERR_INVALID_PARAM;
  1238. goto pub_done;
  1239. }
  1240. error = saHandleInstanceGet(&event_handle_db, event_handle,
  1241. (void*)&edi);
  1242. if (error != SA_OK) {
  1243. goto pub_done;
  1244. }
  1245. pthread_mutex_lock(&edi->edi_mutex);
  1246. /*
  1247. * See if patterns have been set for this event. If not, we
  1248. * can't publish.
  1249. */
  1250. if (!edi->edi_patterns.patterns) {
  1251. error = SA_ERR_INVALID_PARAM;
  1252. goto pub_put1;
  1253. }
  1254. error = saHandleInstanceGet(&channel_handle_db, edi->edi_channel_handle,
  1255. (void*)&eci);
  1256. if (error != SA_OK) {
  1257. goto pub_put1;
  1258. }
  1259. /*
  1260. * See if we can publish to this channel
  1261. */
  1262. if (!(eci->eci_open_flags & SA_EVT_CHANNEL_PUBLISHER)) {
  1263. error = SA_ERR_ACCESS;
  1264. goto pub_put2;
  1265. }
  1266. error = saHandleInstanceGet(&evt_instance_handle_db,
  1267. eci->eci_instance_handle, (void*)&evti);
  1268. if (error != SA_OK) {
  1269. goto pub_put2;
  1270. }
  1271. /*
  1272. * Figure out how much memory we need for the patterns and data
  1273. */
  1274. pattern_size = patt_size(&edi->edi_patterns);
  1275. req = malloc(sizeof(*req) + event_data_size + pattern_size);
  1276. patterns = (struct event_pattern *)req->led_body;
  1277. data_start = (void *)req->led_body + pattern_size;
  1278. if (!req) {
  1279. error = SA_ERR_NO_MEMORY;
  1280. goto pub_put3;
  1281. }
  1282. /*
  1283. * copy everything to the request structure
  1284. */
  1285. aispatt_to_evt_patt(&edi->edi_patterns, patterns);
  1286. req->led_patterns_number = edi->edi_patterns.patternsNumber;
  1287. req->led_user_data_offset = pattern_size;
  1288. if (event_data && event_data_size) {
  1289. memcpy(data_start, event_data, event_data_size);
  1290. req->led_user_data_size = event_data_size;
  1291. } else {
  1292. req->led_user_data_size = 0;
  1293. }
  1294. req->led_head.id = MESSAGE_REQ_EVT_PUBLISH;
  1295. req->led_head.size = sizeof(*req) + pattern_size + event_data_size;
  1296. req->led_svr_channel_handle = eci->eci_svr_channel_handle;
  1297. req->led_retention_time = edi->edi_retention_time;
  1298. req->led_publish_time = clustTimeNow();
  1299. req->led_priority = edi->edi_priority;
  1300. req->led_publisher_name = edi->edi_pub_name;
  1301. while (--pub_sleep_trys) {
  1302. pthread_mutex_lock(&evti->ei_mutex);
  1303. error = saSendRetry(evti->ei_fd, req, req->led_head.size, MSG_NOSIGNAL);
  1304. if (error != SA_OK) {
  1305. pthread_mutex_unlock (&evti->ei_mutex);
  1306. goto pub_put3_free;
  1307. }
  1308. error = saRecvQueue(evti->ei_fd, &res, &evti->ei_inq,
  1309. MESSAGE_RES_EVT_PUBLISH);
  1310. pthread_mutex_unlock (&evti->ei_mutex);
  1311. if (error != SA_OK) {
  1312. goto pub_put3_free;
  1313. }
  1314. error = res.iep_head.error;
  1315. if (error == SA_ERR_TRY_AGAIN) {
  1316. struct timespec ts;
  1317. struct timespec rem;
  1318. ts.tv_sec = PUB_SLEEP_SEC;
  1319. ts.tv_nsec = PUB_SLEEP_NSEC;
  1320. pub_sleep:
  1321. if (nanosleep(&ts, &rem) < 0) {
  1322. if (errno == EINTR) {
  1323. ts = rem;
  1324. goto pub_sleep;
  1325. }
  1326. error = SA_ERR_TIMEOUT;
  1327. goto pub_put3_free;
  1328. }
  1329. continue;
  1330. }
  1331. *eventid = res.iep_event_id;
  1332. break;
  1333. }
  1334. if (error == SA_ERR_TRY_AGAIN) {
  1335. error = SA_ERR_TIMEOUT;
  1336. }
  1337. pub_put3_free:
  1338. free(req);
  1339. pub_put3:
  1340. saHandleInstancePut (&evt_instance_handle_db, eci->eci_instance_handle);
  1341. pub_put2:
  1342. saHandleInstancePut (&channel_handle_db, edi->edi_channel_handle);
  1343. pub_put1:
  1344. pthread_mutex_unlock(&edi->edi_mutex);
  1345. saHandleInstancePut(&event_handle_db, event_handle);
  1346. pub_done:
  1347. return error;
  1348. }
  1349. /*
  1350. * The saEvtEventSubscribe() function enables a process to subscribe for
  1351. * events on an event channel by registering one or more filters on that
  1352. * event channel. The process must have opened the event channel, designated
  1353. * by channel_handle, with the SA_EVT_CHANNEL_SUBSCRIBER flag set for an
  1354. * invocation of this function to be successful.
  1355. * The memory associated with the filters is not deallocated by the
  1356. * saEvtEventSubscribe() function. It is the responsibility of the invoking
  1357. * process to deallocate the memory when the saEvtEventSubscribe() function
  1358. * returns.
  1359. * For a given subscription, the filters parameter cannot be modified. To
  1360. * change the filters parameter without losing events, a process must
  1361. * establish a new subscription with the new filters parameter. After the new
  1362. * subscription is established, the old subscription can be removed by
  1363. * invoking the saEvtEventUnsubscribe() function.
  1364. */
  1365. SaErrorT
  1366. saEvtEventSubscribe(
  1367. const SaEvtChannelHandleT channel_handle,
  1368. const SaEvtEventFilterArrayT *filters,
  1369. SaEvtSubscriptionIdT subscription_id)
  1370. {
  1371. SaErrorT error;
  1372. struct event_instance *evti;
  1373. struct event_channel_instance *eci;
  1374. struct req_evt_event_subscribe *req;
  1375. struct res_evt_event_subscribe res;
  1376. int sz;
  1377. error = saHandleInstanceGet(&channel_handle_db, channel_handle,
  1378. (void*)&eci);
  1379. if (error != SA_OK) {
  1380. goto subscribe_done;
  1381. }
  1382. /*
  1383. * get the evt handle for the fd
  1384. */
  1385. error = saHandleInstanceGet(&evt_instance_handle_db,
  1386. eci->eci_instance_handle, (void*)&evti);
  1387. if (error != SA_OK) {
  1388. goto subscribe_put1;
  1389. }
  1390. /*
  1391. * See if we can subscribe to this channel
  1392. */
  1393. if (!(eci->eci_open_flags & SA_EVT_CHANNEL_SUBSCRIBER)) {
  1394. error = SA_ERR_INVALID_PARAM;
  1395. goto subscribe_put2;
  1396. }
  1397. /*
  1398. * calculate size needed to store the filters
  1399. */
  1400. sz = filt_size(filters);
  1401. req = malloc(sizeof(*req) + sz);
  1402. if (!req) {
  1403. error = SA_ERR_NO_MEMORY;
  1404. goto subscribe_put2;
  1405. }
  1406. /*
  1407. * Copy the supplied filters to the request
  1408. */
  1409. req->ics_filter_count = aisfilt_to_evt_filt(filters,
  1410. req->ics_filter_data);
  1411. req->ics_head.id = MESSAGE_REQ_EVT_SUBSCRIBE;
  1412. req->ics_head.size = sizeof(*req) + sz;
  1413. req->ics_channel_handle = eci->eci_svr_channel_handle;
  1414. req->ics_sub_id = subscription_id;
  1415. req->ics_filter_size = sz;
  1416. pthread_mutex_lock(&evti->ei_mutex);
  1417. error = saSendRetry(evti->ei_fd, req, req->ics_head.size, MSG_NOSIGNAL);
  1418. free(req);
  1419. if (error != SA_OK) {
  1420. pthread_mutex_unlock (&evti->ei_mutex);
  1421. goto subscribe_put2;
  1422. }
  1423. error = saRecvQueue(evti->ei_fd, &res, &evti->ei_inq,
  1424. MESSAGE_RES_EVT_SUBSCRIBE);
  1425. pthread_mutex_unlock (&evti->ei_mutex);
  1426. if (error != SA_OK) {
  1427. goto subscribe_put2;
  1428. }
  1429. error = res.ics_head.error;
  1430. subscribe_put2:
  1431. saHandleInstancePut(&evt_instance_handle_db,
  1432. eci->eci_instance_handle);
  1433. subscribe_put1:
  1434. saHandleInstancePut(&channel_handle_db, channel_handle);
  1435. subscribe_done:
  1436. return error;
  1437. }
  1438. /*
  1439. * The saEvtEventUnsubscribe() function allows a process to stop receiving
  1440. * events for a particular subscription on an event channel by removing the
  1441. * subscription. The saEvtEventUnsubscribe() operation is successful if the
  1442. * subscriptionId parameter matches a previously registered subscription.
  1443. * Pending events that no longer match any subscription, because the
  1444. * saEvtEventUnsubscribe() operation had been invoked, are purged. a process
  1445. * that wishes to modify a subscription without losing any events must
  1446. * establish the new subscription before removing the existing subscription.
  1447. */
  1448. SaErrorT
  1449. saEvtEventUnsubscribe(
  1450. const SaEvtChannelHandleT channel_handle,
  1451. SaEvtSubscriptionIdT subscription_id)
  1452. {
  1453. SaErrorT error;
  1454. struct event_instance *evti;
  1455. struct event_channel_instance *eci;
  1456. struct req_evt_event_unsubscribe req;
  1457. struct res_evt_event_unsubscribe res;
  1458. error = saHandleInstanceGet(&channel_handle_db, channel_handle,
  1459. (void*)&eci);
  1460. if (error != SA_OK) {
  1461. goto unsubscribe_done;
  1462. }
  1463. error = saHandleInstanceGet(&evt_instance_handle_db,
  1464. eci->eci_instance_handle, (void*)&evti);
  1465. if (error != SA_OK) {
  1466. goto unsubscribe_put1;
  1467. }
  1468. req.icu_head.id = MESSAGE_REQ_EVT_UNSUBSCRIBE;
  1469. req.icu_head.size = sizeof(req);
  1470. req.icu_channel_handle = eci->eci_svr_channel_handle;
  1471. req.icu_sub_id = subscription_id;
  1472. pthread_mutex_lock(&evti->ei_mutex);
  1473. error = saSendRetry(evti->ei_fd, &req, sizeof(req), MSG_NOSIGNAL);
  1474. if (error != SA_OK) {
  1475. pthread_mutex_unlock (&evti->ei_mutex);
  1476. goto unsubscribe_put2;
  1477. }
  1478. error = saRecvQueue(evti->ei_fd, &res, &evti->ei_inq,
  1479. MESSAGE_RES_EVT_UNSUBSCRIBE);
  1480. pthread_mutex_unlock (&evti->ei_mutex);
  1481. if (error != SA_OK) {
  1482. goto unsubscribe_put2;
  1483. }
  1484. error = res.icu_head.error;
  1485. unsubscribe_put2:
  1486. saHandleInstancePut(&evt_instance_handle_db,
  1487. eci->eci_instance_handle);
  1488. unsubscribe_put1:
  1489. saHandleInstancePut(&channel_handle_db, channel_handle);
  1490. unsubscribe_done:
  1491. return error;
  1492. }
  1493. /*
  1494. * The saEvtEventRetentionTimeClear() function is used to clear the retention
  1495. * time of a published event. It indicates to the Event Service that it does
  1496. * not need to keep the event any longer for potential new subscribers. Once
  1497. * the value of the retention time is reset to 0, the event is no longer
  1498. * available for new subscribers. The event is held until all old subscribers
  1499. * in the system process the event and free the event using saEvtEventFree().
  1500. */
  1501. SaErrorT
  1502. saEvtEventRetentionTimeClear(
  1503. const SaEvtChannelHandleT channel_handle,
  1504. const SaEvtEventIdT event_id)
  1505. {
  1506. SaErrorT error;
  1507. struct event_instance *evti;
  1508. struct event_channel_instance *eci;
  1509. struct req_evt_event_clear_retentiontime req;
  1510. struct res_evt_event_clear_retentiontime res;
  1511. error = saHandleInstanceGet(&channel_handle_db, channel_handle,
  1512. (void*)&eci);
  1513. if (error != SA_OK) {
  1514. goto ret_time_done;
  1515. }
  1516. error = saHandleInstanceGet(&evt_instance_handle_db,
  1517. eci->eci_instance_handle, (void*)&evti);
  1518. if (error != SA_OK) {
  1519. goto ret_time_put1;
  1520. }
  1521. req.iec_head.id = MESSAGE_REQ_EVT_CLEAR_RETENTIONTIME;
  1522. req.iec_head.size = sizeof(req);
  1523. req.iec_channel_handle = eci->eci_svr_channel_handle;
  1524. req.iec_event_id = event_id;
  1525. pthread_mutex_lock(&evti->ei_mutex);
  1526. error = saSendRetry(evti->ei_fd, &req, sizeof(req), MSG_NOSIGNAL);
  1527. if (error != SA_OK) {
  1528. pthread_mutex_unlock (&evti->ei_mutex);
  1529. goto ret_time_put2;
  1530. }
  1531. error = saRecvQueue(evti->ei_fd, &res, &evti->ei_inq,
  1532. MESSAGE_RES_EVT_CLEAR_RETENTIONTIME);
  1533. pthread_mutex_unlock (&evti->ei_mutex);
  1534. if (error != SA_OK) {
  1535. goto ret_time_put2;
  1536. }
  1537. error = res.iec_head.error;
  1538. ret_time_put2:
  1539. saHandleInstancePut(&evt_instance_handle_db,
  1540. eci->eci_instance_handle);
  1541. ret_time_put1:
  1542. saHandleInstancePut(&channel_handle_db, channel_handle);
  1543. ret_time_done:
  1544. return error;
  1545. }