evt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. #define DEBUG
  34. #include <sys/types.h>
  35. #include <malloc.h>
  36. #include <errno.h>
  37. #include "../include/ais_types.h"
  38. #include "../include/ais_msg.h"
  39. #include "../include/list.h"
  40. #include "../include/queue.h"
  41. #include "aispoll.h"
  42. #include "mempool.h"
  43. #include "parse.h"
  44. #include "main.h"
  45. #include "print.h"
  46. #include "gmi.h"
  47. #include "evt.h"
  48. static int message_handler_req_lib_activatepoll (struct conn_info *conn_info,
  49. void *message);
  50. static int lib_evt_open_channel(struct conn_info *conn_info, void *message);
  51. static int lib_evt_close_channel(struct conn_info *conn_info, void *message);
  52. static int lib_evt_channel_subscribe(struct conn_info *conn_info,
  53. void *message);
  54. static int lib_evt_channel_unsubscribe(struct conn_info *conn_info,
  55. void *message);
  56. static int lib_evt_event_publish(struct conn_info *conn_info, void *message);
  57. static int lib_evt_event_clear_retentiontime(struct conn_info *conn_info,
  58. void *message);
  59. static int evt_conf_change(
  60. struct sockaddr_in *member_list, int member_list_entries,
  61. struct sockaddr_in *left_list, int left_list_entries,
  62. struct sockaddr_in *joined_list, int joined_list_entries);
  63. static int evt_init(struct conn_info *conn_info, void *msg);
  64. static int evt_exit(struct conn_info *conn_info);
  65. static int evt_exec_init(void);
  66. static struct libais_handler evt_libais_handlers[] = {
  67. {
  68. .libais_handler_fn = message_handler_req_lib_activatepoll,
  69. .response_size = sizeof(struct res_lib_activatepoll),
  70. .response_id = MESSAGE_RES_LIB_ACTIVATEPOLL,
  71. .gmi_prio = GMI_PRIO_RECOVERY
  72. },
  73. {
  74. .libais_handler_fn = lib_evt_open_channel,
  75. .response_size = sizeof(struct res_evt_channel_open),
  76. .response_id = MESSAGE_RES_EVT_OPEN_CHANNEL,
  77. .gmi_prio = GMI_PRIO_MED
  78. },
  79. {
  80. .libais_handler_fn = lib_evt_close_channel,
  81. .response_size = sizeof(struct res_evt_channel_close),
  82. .response_id = MESSAGE_RES_EVT_CLOSE_CHANNEL,
  83. .gmi_prio = GMI_PRIO_RECOVERY
  84. },
  85. {
  86. .libais_handler_fn = lib_evt_channel_subscribe,
  87. .response_size = sizeof(struct res_evt_channel_subscribe),
  88. .response_id = MESSAGE_RES_EVT_SUBSCRIBE,
  89. .gmi_prio = GMI_PRIO_RECOVERY
  90. },
  91. {
  92. .libais_handler_fn = lib_evt_channel_unsubscribe,
  93. .response_size = sizeof(struct res_evt_channel_unsubscribe),
  94. .response_id = MESSAGE_RES_EVT_UNSUBSCRIBE,
  95. .gmi_prio = GMI_PRIO_RECOVERY
  96. },
  97. {
  98. .libais_handler_fn = lib_evt_event_publish,
  99. .response_size = sizeof(struct res_evt_event_publish),
  100. .response_id = MESSAGE_RES_EVT_PUBLISH,
  101. .gmi_prio = GMI_PRIO_LOW
  102. },
  103. {
  104. .libais_handler_fn = lib_evt_event_clear_retentiontime,
  105. .response_size = sizeof(struct res_evt_event_clear_retentiontime),
  106. .response_id = MESSAGE_REQ_EVT_CLEAR_RETENTIONTIME,
  107. .gmi_prio = GMI_PRIO_RECOVERY
  108. },
  109. };
  110. static int evt_remote_evt(void *msg, struct in_addr source_addr);
  111. static int evt_remote_chan_op(void *msg, struct in_addr source_addr);
  112. static int (*evt_exec_handler_fns[]) (void *m, struct in_addr s) = {
  113. evt_remote_evt,
  114. evt_remote_chan_op
  115. };
  116. struct service_handler evt_service_handler = {
  117. .libais_handlers = evt_libais_handlers,
  118. .libais_handlers_count = sizeof(evt_libais_handlers) /
  119. sizeof(struct libais_handler),
  120. .aisexec_handler_fns = evt_exec_handler_fns,
  121. .aisexec_handler_fns_count = sizeof(evt_exec_handler_fns) /
  122. sizeof(int (*)),
  123. .confchg_fn = evt_conf_change,
  124. .libais_init_fn = evt_init,
  125. .libais_exit_fn = evt_exit,
  126. .aisexec_init_fn = evt_exec_init
  127. };
  128. /*
  129. * Take the filters we received from the application via the library and
  130. * make them into a real SaEvtEventFilterArrayT
  131. */
  132. static SaErrorT evtfilt_to_aisfilt(struct req_evt_channel_subscribe *req,
  133. SaEvtEventFilterArrayT **evtfilters)
  134. {
  135. SaEvtEventFilterArrayT *filta = (SaEvtEventFilterArrayT *)req->ics_filter_data;
  136. SaEvtEventFilterArrayT *filters;
  137. SaEvtEventFilterT *filt = (void *)filta + sizeof(SaEvtEventFilterArrayT);
  138. SaUint8T *str = (void *)filta + sizeof(SaEvtEventFilterArrayT) +
  139. (sizeof(SaEvtEventFilterT) * filta->filtersNumber);
  140. int i;
  141. filters = malloc(sizeof(SaEvtEventFilterArrayT));
  142. if (!filters) {
  143. return SA_ERR_NO_MEMORY;
  144. }
  145. filters->filtersNumber = filta->filtersNumber;
  146. filters->filters = malloc(sizeof(SaEvtEventFilterT) *
  147. filta->filtersNumber);
  148. for (i = 0; i < filters->filtersNumber; i++) {
  149. filters->filters[i].filter.pattern =
  150. malloc(filt[i].filter.patternSize);
  151. /*
  152. * TODO: Back out of previous allocs if malloc fails
  153. */
  154. filters->filters[i].filter.patternSize =
  155. filt[i].filter.patternSize;
  156. memcpy(filters->filters[i].filter.pattern,
  157. str, filters->filters[i].filter.patternSize);
  158. str += filters->filters[i].filter.patternSize;
  159. }
  160. *evtfilters = filters;
  161. return SA_OK;
  162. }
  163. static int message_handler_req_lib_activatepoll(struct conn_info *conn_info,
  164. void *message)
  165. {
  166. struct res_lib_activatepoll res;
  167. res.header.error = SA_OK;
  168. res.header.size = sizeof (struct res_lib_activatepoll);
  169. res.header.id = MESSAGE_RES_LIB_ACTIVATEPOLL;
  170. libais_send_response(conn_info, &res, sizeof(res));
  171. return (0);
  172. }
  173. static int evt_init(struct conn_info *conn_info, void *msg)
  174. {
  175. struct res_lib_init res;
  176. res.header.size = sizeof (struct res_lib_init);
  177. res.header.id = MESSAGE_RES_INIT;
  178. res.header.error = SA_OK;
  179. log_printf(LOG_LEVEL_DEBUG,
  180. "Got request to initalize cluster event service.\n");
  181. if (!conn_info->authenticated) {
  182. log_printf(LOG_LEVEL_DEBUG,
  183. "event service: Not authenticated\n");
  184. res.header.error = SA_ERR_SECURITY;
  185. libais_send_response(conn_info, &res, sizeof(res));
  186. return -1;
  187. }
  188. conn_info->service = SOCKET_SERVICE_EVT;
  189. libais_send_response (conn_info, &res, sizeof(res));
  190. list_init (&conn_info->conn_list);
  191. return 0;
  192. }
  193. static int lib_evt_open_channel(struct conn_info *conn_info, void *message)
  194. {
  195. struct req_evt_channel_open *req;
  196. struct res_evt_channel_open res;
  197. req = message;
  198. log_printf(LOG_LEVEL_DEBUG, "Open channel request\n");
  199. log_printf(LOG_LEVEL_DEBUG,
  200. "size %d, id %d, handle 0x%x, to 0x%llx\n",
  201. req->ico_head.size,
  202. req->ico_head.id,
  203. req->ico_c_handle,
  204. req->ico_timeout);
  205. log_printf(LOG_LEVEL_DEBUG, "flags %x, channel name(%d) %s\n",
  206. req->ico_open_flag,
  207. req->ico_channel_name.length,
  208. req->ico_channel_name.value);
  209. /*
  210. * TODO: Add open code here
  211. */
  212. res.ico_head.size = sizeof(res);
  213. res.ico_head.id = MESSAGE_RES_EVT_OPEN_CHANNEL;
  214. res.ico_head.error = SA_OK;
  215. res.ico_channel_handle = req->ico_c_handle; /* TODO: fix this */
  216. libais_send_response (conn_info, &res, sizeof(res));
  217. return 0;
  218. }
  219. static int lib_evt_close_channel(struct conn_info *conn_info, void *message)
  220. {
  221. struct req_evt_channel_close *req;
  222. struct res_evt_channel_close res;
  223. req = message;
  224. log_printf(LOG_LEVEL_DEBUG, "Close channel request\n");
  225. log_printf(LOG_LEVEL_DEBUG, "size %d, id %d, handle 0x%x\n",
  226. req->icc_head.size,
  227. req->icc_head.id,
  228. req->icc_channel_handle);
  229. /*
  230. * TODO: Add close code here
  231. */
  232. res.icc_head.size = sizeof(res);
  233. res.icc_head.id = MESSAGE_RES_EVT_CLOSE_CHANNEL;
  234. res.icc_head.error = SA_OK;
  235. libais_send_response (conn_info, &res, sizeof(res));
  236. return 0;
  237. }
  238. static int lib_evt_channel_subscribe(struct conn_info *conn_info, void *message)
  239. {
  240. struct req_evt_channel_subscribe *req;
  241. struct res_evt_channel_subscribe res;
  242. SaEvtEventFilterArrayT *filters;
  243. SaErrorT error = SA_OK;
  244. int i;
  245. req = message;
  246. log_printf(LOG_LEVEL_DEBUG, "Subscribe channel request\n");
  247. log_printf(LOG_LEVEL_DEBUG, "size %d, id %d\n",
  248. req->ics_head.size,
  249. req->ics_head.id);
  250. log_printf(LOG_LEVEL_DEBUG, "subscription Id: 0x%x\n", req->ics_sub_id);
  251. error = evtfilt_to_aisfilt(req, &filters);
  252. if (error == SA_OK) {
  253. log_printf(LOG_LEVEL_DEBUG, "Subscribe filters count %d\n",
  254. filters->filtersNumber);
  255. for (i = 0; i < filters->filtersNumber; i++) {
  256. log_printf(LOG_LEVEL_DEBUG, "sz %d, type %d, <%s>\n",
  257. filters->filters[i].filterType,
  258. filters->filters[i].filter.patternSize,
  259. filters->filters[i].filter.pattern);
  260. }
  261. }
  262. /*
  263. * TODO: add subscribe code here.
  264. * TODO: remove filters for now to avoid a leak.
  265. */
  266. for (i = 0; i < filters->filtersNumber; i++) {
  267. free(filters->filters[i].filter.pattern);
  268. }
  269. free(filters->filters);
  270. free(filters);
  271. res.ics_head.size = sizeof(res);
  272. res.ics_head.id = MESSAGE_RES_EVT_SUBSCRIBE;
  273. res.ics_head.error = error;
  274. libais_send_response (conn_info, &res, sizeof(res));
  275. return 0;
  276. }
  277. static int lib_evt_channel_unsubscribe(struct conn_info *conn_info,
  278. void *message)
  279. {
  280. struct req_evt_channel_unsubscribe *req;
  281. struct res_evt_channel_unsubscribe res;
  282. SaErrorT error = SA_OK;
  283. req = message;
  284. log_printf(LOG_LEVEL_DEBUG, "Unsubscribe channel request\n");
  285. log_printf(LOG_LEVEL_DEBUG, "size %d, id %d\n",
  286. req->icu_head.size,
  287. req->icu_head.id);
  288. log_printf(LOG_LEVEL_DEBUG, "subscription Id: 0x%x\n", req->icu_sub_id);
  289. /*
  290. * TODO: Add unsubscribe code here
  291. */
  292. res.icu_head.size = sizeof(res);
  293. res.icu_head.id = MESSAGE_RES_EVT_UNSUBSCRIBE;
  294. res.icu_head.error = error;
  295. libais_send_response (conn_info, &res, sizeof(res));
  296. return 0;
  297. }
  298. static int lib_evt_event_publish(struct conn_info *conn_info, void *message)
  299. {
  300. struct lib_event_data *req;
  301. struct res_evt_event_publish res;
  302. SaEvtEventIdT event_id = 0x5a5a5a5a5a5a5a5aull;
  303. SaErrorT error = SA_OK;
  304. req = message;
  305. log_printf(LOG_LEVEL_DEBUG, "Publish event request\n");
  306. log_printf(LOG_LEVEL_DEBUG, "size %d, id %d\n",
  307. req->led_head.size,
  308. req->led_head.id);
  309. /*
  310. * TODO: Add publish code here
  311. */
  312. res.iep_head.size = sizeof(res);
  313. res.iep_head.id = MESSAGE_RES_EVT_PUBLISH;
  314. res.iep_head.error = error;
  315. res.iep_event_id = event_id;
  316. libais_send_response (conn_info, &res, sizeof(res));
  317. return 0;
  318. }
  319. static int lib_evt_event_clear_retentiontime(struct conn_info *conn_info, void *message)
  320. {
  321. struct req_evt_event_clear_retentiontime *req;
  322. struct res_evt_event_clear_retentiontime res;
  323. SaErrorT error = SA_OK;
  324. req = message;
  325. log_printf(LOG_LEVEL_DEBUG, "Clear event retentiontime request\n");
  326. log_printf(LOG_LEVEL_DEBUG,
  327. "size %d, id %d, event ID 0x%llx, chan handle 0x%x\n",
  328. req->iec_head.size,
  329. req->iec_head.id,
  330. req->iec_event_id,
  331. req->iec_channel_handle);
  332. /*
  333. * TODO: Add clear retention time code here
  334. */
  335. res.iec_head.size = sizeof(res);
  336. res.iec_head.id = MESSAGE_REQ_EVT_CLEAR_RETENTIONTIME;
  337. res.iec_head.error = error;
  338. libais_send_response (conn_info, &res, sizeof(res));
  339. return 0;
  340. }
  341. static int evt_conf_change(
  342. struct sockaddr_in *member_list, int member_list_entries,
  343. struct sockaddr_in *left_list, int left_list_entries,
  344. struct sockaddr_in *joined_list, int joined_list_entries)
  345. {
  346. log_printf(LOG_LEVEL_DEBUG, "Evt conf change\n");
  347. return 0;
  348. }
  349. static int evt_exit(struct conn_info *conn_info)
  350. {
  351. log_printf(LOG_LEVEL_DEBUG, "Evt exit request\n");
  352. /*
  353. * Delete track entry if there is one
  354. */
  355. list_del (&conn_info->conn_list);
  356. return 0;
  357. }
  358. static int evt_exec_init(void)
  359. {
  360. log_printf(LOG_LEVEL_DEBUG, "Evt exec exit request\n");
  361. return 0;
  362. }
  363. static int evt_remote_evt(void *msg, struct in_addr source_addr)
  364. {
  365. log_printf(LOG_LEVEL_DEBUG, "Remote event data received");
  366. return 0;
  367. }
  368. static int evt_remote_chan_op(void *msg, struct in_addr source_addr)
  369. {
  370. log_printf(LOG_LEVEL_DEBUG, "Remote channel operation request\n");
  371. return 0;
  372. }