evt.c 43 KB

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