evt.c 48 KB

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