evs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * Copyright (c) 2004 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@mvista.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <sys/types.h>
  35. #include <sys/socket.h>
  36. #include <sys/un.h>
  37. #include <sys/sysinfo.h>
  38. #include <sys/ioctl.h>
  39. #include <netinet/in.h>
  40. #include <sys/uio.h>
  41. #include <unistd.h>
  42. #include <fcntl.h>
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #include <errno.h>
  46. #include <signal.h>
  47. #include <time.h>
  48. #include <netinet/in.h>
  49. #include <arpa/inet.h>
  50. #include "../include/ais_types.h"
  51. #include "../include/ais_msg.h"
  52. #include "../include/list.h"
  53. #include "../include/queue.h"
  54. #include "aispoll.h"
  55. #include "gmi.h"
  56. #include "parse.h"
  57. #include "main.h"
  58. #include "print.h"
  59. #include "mempool.h"
  60. #include "handlers.h"
  61. static DECLARE_LIST_INIT (confchg_notify);
  62. /*
  63. * Service Interfaces required by service_message_handler struct
  64. */
  65. static int evs_executive_initialize (void);
  66. static int evs_confchg_fn (
  67. enum gmi_configuration_type configuration_type,
  68. struct sockaddr_in *member_list, int member_list_entries,
  69. struct sockaddr_in *left_list, int left_list_entries,
  70. struct sockaddr_in *joined_list, int joined_list_entries);
  71. static int message_handler_req_exec_mcast (void *message, struct in_addr source_addr);
  72. static int message_handler_req_evs_init (struct conn_info *conn_info,
  73. void *message);
  74. static int message_handler_req_lib_activatepoll (struct conn_info *conn_info,
  75. void *message);
  76. static int message_handler_req_evs_join (struct conn_info *conn_info, void *message);
  77. static int message_handler_req_evs_leave (struct conn_info *conn_info, void *message);
  78. static int message_handler_req_evs_mcast_joined (struct conn_info *conn_info, void *message);
  79. static int message_handler_req_evs_mcast_groups (struct conn_info *conn_info, void *message);
  80. static int evs_exit_fn (struct conn_info *conn_info);
  81. struct libais_handler evs_libais_handlers[] =
  82. {
  83. { /* 0 */
  84. .libais_handler_fn = message_handler_req_lib_activatepoll,
  85. .response_size = sizeof (struct res_lib_activatepoll),
  86. .response_id = MESSAGE_RES_LIB_ACTIVATEPOLL, // TODO RESPONSE
  87. .gmi_prio = GMI_PRIO_RECOVERY
  88. },
  89. { /* 1 */
  90. .libais_handler_fn = message_handler_req_evs_join,
  91. .response_size = sizeof (struct res_lib_evs_join),
  92. .response_id = MESSAGE_RES_EVS_JOIN,
  93. .gmi_prio = GMI_PRIO_RECOVERY
  94. },
  95. { /* 2 */
  96. .libais_handler_fn = message_handler_req_evs_leave,
  97. .response_size = sizeof (struct res_lib_evs_leave),
  98. .response_id = MESSAGE_RES_EVS_LEAVE,
  99. .gmi_prio = GMI_PRIO_RECOVERY
  100. },
  101. { /* 3 */
  102. .libais_handler_fn = message_handler_req_evs_mcast_joined,
  103. .response_size = sizeof (struct res_lib_evs_mcast_joined),
  104. .response_id = MESSAGE_RES_EVS_MCAST_JOINED,
  105. .gmi_prio = GMI_PRIO_LOW
  106. },
  107. { /* 4 */
  108. .libais_handler_fn = message_handler_req_evs_mcast_groups,
  109. .response_size = sizeof (struct res_lib_evs_mcast_groups),
  110. .response_id = MESSAGE_RES_EVS_MCAST_GROUPS,
  111. .gmi_prio = GMI_PRIO_LOW
  112. }
  113. };
  114. static int (*evs_aisexec_handler_fns[]) (void *, struct in_addr source_addr) = {
  115. message_handler_req_exec_mcast
  116. };
  117. struct service_handler evs_service_handler = {
  118. .libais_handlers = evs_libais_handlers,
  119. .libais_handlers_count = sizeof (evs_libais_handlers) / sizeof (struct libais_handler),
  120. .aisexec_handler_fns = evs_aisexec_handler_fns,
  121. .aisexec_handler_fns_count = sizeof (evs_aisexec_handler_fns) / sizeof (int (*)),
  122. .confchg_fn = evs_confchg_fn,
  123. .libais_init_fn = message_handler_req_evs_init,
  124. .libais_exit_fn = evs_exit_fn,
  125. .exec_init_fn = evs_executive_initialize,
  126. .exec_dump_fn = 0
  127. };
  128. static int evs_executive_initialize (void)
  129. {
  130. return (0);
  131. }
  132. static int evs_exit_fn (struct conn_info *conn_info)
  133. {
  134. list_del (&conn_info->conn_list);
  135. return (0);
  136. }
  137. static int evs_confchg_fn (
  138. enum gmi_configuration_type configuration_type,
  139. struct sockaddr_in *member_list, int member_list_entries,
  140. struct sockaddr_in *left_list, int left_list_entries,
  141. struct sockaddr_in *joined_list, int joined_list_entries) {
  142. int i;
  143. struct list_head *list;
  144. struct res_evs_confchg_callback res_evs_confchg_callback;
  145. struct conn_info *conn_info;
  146. /*
  147. * Build configuration change message
  148. */
  149. res_evs_confchg_callback.header.size = sizeof (struct res_evs_confchg_callback);
  150. res_evs_confchg_callback.header.id = MESSAGE_RES_EVS_CONFCHG_CALLBACK;
  151. res_evs_confchg_callback.header.error = SA_OK;
  152. for (i = 0; i < member_list_entries; i++) {
  153. res_evs_confchg_callback.member_list[i].s_addr = member_list[i].sin_addr.s_addr;
  154. }
  155. res_evs_confchg_callback.member_list_entries = member_list_entries;
  156. for (i = 0; i < left_list_entries; i++) {
  157. res_evs_confchg_callback.left_list[i].s_addr = left_list[i].sin_addr.s_addr;
  158. }
  159. res_evs_confchg_callback.left_list_entries = left_list_entries;
  160. for (i = 0; i < joined_list_entries; i++) {
  161. res_evs_confchg_callback.joined_list[i].s_addr = joined_list[i].sin_addr.s_addr;
  162. }
  163. res_evs_confchg_callback.joined_list_entries = joined_list_entries;
  164. /*
  165. * Send configuration change message to every EVS library user
  166. */
  167. for (list = confchg_notify.next; list != &confchg_notify; list = list->next) {
  168. conn_info = list_entry (list, struct conn_info, conn_list);
  169. libais_send_response (conn_info, &res_evs_confchg_callback,
  170. sizeof (res_evs_confchg_callback));
  171. }
  172. return (0);
  173. }
  174. static int message_handler_req_evs_init (struct conn_info *conn_info, void *message)
  175. {
  176. SaErrorT error = SA_ERR_SECURITY;
  177. struct res_lib_init res_lib_init;
  178. log_printf (LOG_LEVEL_DEBUG, "Got request to initalize evs service.\n");
  179. if (conn_info->authenticated) {
  180. conn_info->service = SOCKET_SERVICE_EVS;
  181. error = SA_OK;
  182. }
  183. res_lib_init.header.size = sizeof (struct res_lib_init);
  184. res_lib_init.header.id = MESSAGE_RES_INIT;
  185. res_lib_init.header.error = error;
  186. libais_send_response (conn_info, &res_lib_init, sizeof (res_lib_init));
  187. list_add (&conn_info->conn_list, &confchg_notify);
  188. if (conn_info->authenticated) {
  189. return (0);
  190. }
  191. return (-1);
  192. }
  193. static int message_handler_req_lib_activatepoll (struct conn_info *conn_info, void *message)
  194. {
  195. struct res_lib_activatepoll res_lib_activatepoll;
  196. res_lib_activatepoll.header.size = sizeof (struct res_lib_activatepoll);
  197. res_lib_activatepoll.header.id = MESSAGE_RES_LIB_ACTIVATEPOLL;
  198. res_lib_activatepoll.header.error = SA_OK;
  199. libais_send_response (conn_info, &res_lib_activatepoll,
  200. sizeof (struct res_lib_activatepoll));
  201. return (0);
  202. }
  203. static int message_handler_req_evs_join (struct conn_info *conn_info, void *message)
  204. {
  205. evs_error_t error = EVS_OK;
  206. struct req_lib_evs_join *req_lib_evs_join = (struct req_lib_evs_join *)message;
  207. struct res_lib_evs_join res_lib_evs_join;
  208. void *addr;
  209. if (req_lib_evs_join->group_entries > 50) {
  210. error = EVS_ERR_TOO_MANY_GROUPS;
  211. goto exit_error;
  212. }
  213. #ifdef DEBUG
  214. { int i;
  215. for (i = 0; i < req_lib_evs_join->group_entries; i++) {
  216. printf ("Joining group %s\n", req_lib_evs_join->groups[i].key);
  217. }
  218. }
  219. #endif
  220. addr = realloc (conn_info->ais_ci.u.libevs_ci.groups,
  221. sizeof (struct evs_group) *
  222. (conn_info->ais_ci.u.libevs_ci.group_entries + req_lib_evs_join->group_entries));
  223. if (addr == 0) {
  224. error = SA_ERR_NO_MEMORY;
  225. goto exit_error;
  226. }
  227. conn_info->ais_ci.u.libevs_ci.groups = addr;
  228. memcpy (&conn_info->ais_ci.u.libevs_ci.groups[conn_info->ais_ci.u.libevs_ci.group_entries],
  229. req_lib_evs_join->groups,
  230. sizeof (struct evs_group) * req_lib_evs_join->group_entries);
  231. conn_info->ais_ci.u.libevs_ci.group_entries += req_lib_evs_join->group_entries;
  232. exit_error:
  233. res_lib_evs_join.header.size = sizeof (struct res_lib_evs_join);
  234. res_lib_evs_join.header.id = MESSAGE_RES_EVS_JOIN;
  235. res_lib_evs_join.header.error = error;
  236. libais_send_response (conn_info, &res_lib_evs_join,
  237. sizeof (struct res_lib_evs_join));
  238. return (0);
  239. }
  240. static int message_handler_req_evs_leave (struct conn_info *conn_info, void *message)
  241. {
  242. struct req_lib_evs_leave *req_lib_evs_leave = (struct req_lib_evs_leave *)message;
  243. struct res_lib_evs_leave res_lib_evs_leave;
  244. evs_error_t error = EVS_OK;
  245. int error_index;
  246. int i, j;
  247. int found;
  248. for (i = 0; i < req_lib_evs_leave->group_entries; i++) {
  249. found = 0;
  250. for (j = 0; j < conn_info->ais_ci.u.libevs_ci.group_entries;) {
  251. if (memcmp (&req_lib_evs_leave->groups[i],
  252. &conn_info->ais_ci.u.libevs_ci.groups[j],
  253. sizeof (struct evs_group)) == 0) {
  254. /*
  255. * Delete entry
  256. */
  257. memmove (&conn_info->ais_ci.u.libevs_ci.groups[j],
  258. &conn_info->ais_ci.u.libevs_ci.groups[j + 1],
  259. (conn_info->ais_ci.u.libevs_ci.group_entries - j - 1) *
  260. sizeof (struct evs_group));
  261. conn_info->ais_ci.u.libevs_ci.group_entries -= 1;
  262. found = 1;
  263. break;
  264. } else {
  265. j++;
  266. }
  267. }
  268. if (found == 0) {
  269. error = EVS_ERR_NOT_EXIST;
  270. error_index = i;
  271. break;
  272. }
  273. }
  274. #ifdef DEBUG
  275. for (i = 0; i < conn_info->ais_ci.u.libevs_ci.group_entries; i++) {
  276. printf ("Groups Left %s\n",
  277. &conn_info->ais_ci.u.libevs_ci.groups[i].key);
  278. }
  279. #endif
  280. res_lib_evs_leave.header.size = sizeof (struct res_lib_evs_leave);
  281. res_lib_evs_leave.header.id = MESSAGE_RES_EVS_LEAVE;
  282. res_lib_evs_leave.header.error = error;
  283. libais_send_response (conn_info, &res_lib_evs_leave,
  284. sizeof (struct res_lib_evs_leave));
  285. return (0);
  286. }
  287. static int message_handler_req_evs_mcast_joined (struct conn_info *conn_info, void *message)
  288. {
  289. evs_error_t error = EVS_OK;
  290. struct req_lib_evs_mcast_joined *req_lib_evs_mcast_joined = (struct req_lib_evs_mcast_joined *)message;
  291. struct res_lib_evs_mcast_joined res_lib_evs_mcast_joined;
  292. struct iovec req_exec_evs_mcast_iovec[3];
  293. struct req_exec_evs_mcast req_exec_evs_mcast;
  294. req_exec_evs_mcast.header.size = sizeof (struct req_exec_evs_mcast);
  295. req_exec_evs_mcast.header.id = MESSAGE_REQ_EXEC_EVS_MCAST;
  296. req_exec_evs_mcast.msg_len = req_lib_evs_mcast_joined->msg_len;
  297. req_exec_evs_mcast.group_entries = conn_info->ais_ci.u.libevs_ci.group_entries;
  298. req_exec_evs_mcast_iovec[0].iov_base = &req_exec_evs_mcast;
  299. req_exec_evs_mcast_iovec[0].iov_len = sizeof (req_exec_evs_mcast);
  300. req_exec_evs_mcast_iovec[1].iov_base = conn_info->ais_ci.u.libevs_ci.groups;
  301. req_exec_evs_mcast_iovec[1].iov_len = conn_info->ais_ci.u.libevs_ci.group_entries * sizeof (struct evs_group);
  302. req_exec_evs_mcast_iovec[2].iov_base = &req_lib_evs_mcast_joined->msg;
  303. req_exec_evs_mcast_iovec[2].iov_len = req_lib_evs_mcast_joined->msg_len;
  304. assert (gmi_mcast (&aisexec_groupname, req_exec_evs_mcast_iovec, 3,
  305. req_lib_evs_mcast_joined->priority) == 0);
  306. res_lib_evs_mcast_joined.header.size = sizeof (struct res_lib_evs_mcast_joined);
  307. res_lib_evs_mcast_joined.header.id = MESSAGE_RES_EVS_MCAST_JOINED;
  308. res_lib_evs_mcast_joined.header.error = error;
  309. libais_send_response (conn_info, &res_lib_evs_mcast_joined,
  310. sizeof (struct res_lib_evs_mcast_joined));
  311. return (0);
  312. }
  313. static int message_handler_req_evs_mcast_groups (struct conn_info *conn_info, void *message)
  314. {
  315. evs_error_t error = EVS_OK;
  316. struct req_lib_evs_mcast_groups *req_lib_evs_mcast_groups = (struct req_lib_evs_mcast_groups *)message;
  317. struct res_lib_evs_mcast_groups res_lib_evs_mcast_groups;
  318. struct iovec req_exec_evs_mcast_iovec[3];
  319. struct req_exec_evs_mcast req_exec_evs_mcast;
  320. char *msg_addr;
  321. req_exec_evs_mcast.header.size = sizeof (struct req_exec_evs_mcast);
  322. req_exec_evs_mcast.header.id = MESSAGE_REQ_EXEC_EVS_MCAST;
  323. req_exec_evs_mcast.msg_len = req_lib_evs_mcast_groups->msg_len;
  324. req_exec_evs_mcast.group_entries = req_lib_evs_mcast_groups->group_entries;
  325. msg_addr = (char *)req_lib_evs_mcast_groups +
  326. sizeof (struct req_lib_evs_mcast_groups) +
  327. (sizeof (struct evs_group) * req_lib_evs_mcast_groups->group_entries);
  328. req_exec_evs_mcast_iovec[0].iov_base = &req_exec_evs_mcast;
  329. req_exec_evs_mcast_iovec[0].iov_len = sizeof (req_exec_evs_mcast);
  330. req_exec_evs_mcast_iovec[1].iov_base = &req_lib_evs_mcast_groups->groups;
  331. req_exec_evs_mcast_iovec[1].iov_len = sizeof (struct evs_group) * req_lib_evs_mcast_groups->group_entries;
  332. req_exec_evs_mcast_iovec[2].iov_base = msg_addr;
  333. req_exec_evs_mcast_iovec[2].iov_len = req_lib_evs_mcast_groups->msg_len;
  334. assert (gmi_mcast (&aisexec_groupname, req_exec_evs_mcast_iovec, 3,
  335. req_lib_evs_mcast_groups->priority) == 0);
  336. res_lib_evs_mcast_groups.header.size = sizeof (struct res_lib_evs_mcast_groups);
  337. res_lib_evs_mcast_groups.header.id = MESSAGE_RES_EVS_MCAST_GROUPS;
  338. res_lib_evs_mcast_groups.header.error = error;
  339. libais_send_response (conn_info, &res_lib_evs_mcast_groups,
  340. sizeof (struct res_lib_evs_mcast_groups));
  341. return (0);
  342. }
  343. static int message_handler_req_exec_mcast (void *message, struct in_addr source_addr)
  344. {
  345. struct req_exec_evs_mcast *req_exec_evs_mcast = (struct req_exec_evs_mcast *)message;
  346. struct res_evs_deliver_callback res_evs_deliver_callback;
  347. char *msg_addr;
  348. struct conn_info *conn_info;
  349. struct list_head *list;
  350. int found = 0;
  351. int i, j;
  352. res_evs_deliver_callback.header.size = sizeof (struct res_evs_deliver_callback) +
  353. req_exec_evs_mcast->msg_len;
  354. res_evs_deliver_callback.header.id = MESSAGE_RES_EVS_DELIVER_CALLBACK;
  355. res_evs_deliver_callback.header.error = SA_OK;
  356. res_evs_deliver_callback.msglen = req_exec_evs_mcast->msg_len;
  357. msg_addr = (char *)req_exec_evs_mcast + sizeof (struct req_exec_evs_mcast) +
  358. (sizeof (struct evs_group) * req_exec_evs_mcast->group_entries);
  359. for (list = confchg_notify.next; list != &confchg_notify; list = list->next) {
  360. found = 0;
  361. conn_info = list_entry (list, struct conn_info, conn_list);
  362. for (i = 0; i < conn_info->ais_ci.u.libevs_ci.group_entries; i++) {
  363. for (j = 0; j < req_exec_evs_mcast->group_entries; j++) {
  364. if (memcmp (&conn_info->ais_ci.u.libevs_ci.groups[i],
  365. &req_exec_evs_mcast->groups[j],
  366. sizeof (struct evs_group)) == 0) {
  367. found = 1;
  368. break;
  369. }
  370. }
  371. if (found) {
  372. break;
  373. }
  374. }
  375. if (found) {
  376. libais_send_response (conn_info, &res_evs_deliver_callback,
  377. sizeof (struct res_evs_deliver_callback));
  378. libais_send_response (conn_info, msg_addr,
  379. req_exec_evs_mcast->msg_len);
  380. }
  381. }
  382. return (0);
  383. }