evt.c 43 KB

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