evs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. };
  127. static int evs_executive_initialize (void)
  128. {
  129. return (0);
  130. }
  131. static int evs_exit_fn (struct conn_info *conn_info)
  132. {
  133. list_del (&conn_info->conn_list);
  134. return (0);
  135. }
  136. static int evs_confchg_fn (
  137. enum gmi_configuration_type configuration_type,
  138. struct sockaddr_in *member_list, int member_list_entries,
  139. struct sockaddr_in *left_list, int left_list_entries,
  140. struct sockaddr_in *joined_list, int joined_list_entries) {
  141. int i;
  142. struct list_head *list;
  143. struct res_evs_confchg_callback res_evs_confchg_callback;
  144. struct conn_info *conn_info;
  145. /*
  146. * Build configuration change message
  147. */
  148. res_evs_confchg_callback.header.size = sizeof (struct res_evs_confchg_callback);
  149. res_evs_confchg_callback.header.id = MESSAGE_RES_EVS_CONFCHG_CALLBACK;
  150. res_evs_confchg_callback.header.error = SA_OK;
  151. for (i = 0; i < member_list_entries; i++) {
  152. res_evs_confchg_callback.member_list[i].s_addr = member_list[i].sin_addr.s_addr;
  153. }
  154. res_evs_confchg_callback.member_list_entries = member_list_entries;
  155. for (i = 0; i < left_list_entries; i++) {
  156. res_evs_confchg_callback.left_list[i].s_addr = left_list[i].sin_addr.s_addr;
  157. }
  158. res_evs_confchg_callback.left_list_entries = left_list_entries;
  159. for (i = 0; i < joined_list_entries; i++) {
  160. res_evs_confchg_callback.joined_list[i].s_addr = joined_list[i].sin_addr.s_addr;
  161. }
  162. res_evs_confchg_callback.joined_list_entries = joined_list_entries;
  163. /*
  164. * Send configuration change message to every EVS library user
  165. */
  166. for (list = confchg_notify.next; list != &confchg_notify; list = list->next) {
  167. conn_info = list_entry (list, struct conn_info, conn_list);
  168. libais_send_response (conn_info, &res_evs_confchg_callback,
  169. sizeof (res_evs_confchg_callback));
  170. }
  171. return (0);
  172. }
  173. static int message_handler_req_evs_init (struct conn_info *conn_info, void *message)
  174. {
  175. SaErrorT error = SA_ERR_SECURITY;
  176. struct res_lib_init res_lib_init;
  177. log_printf (LOG_LEVEL_DEBUG, "Got request to initalize evs service.\n");
  178. if (conn_info->authenticated) {
  179. conn_info->service = SOCKET_SERVICE_EVS;
  180. error = SA_OK;
  181. }
  182. res_lib_init.header.size = sizeof (struct res_lib_init);
  183. res_lib_init.header.id = MESSAGE_RES_INIT;
  184. res_lib_init.header.error = error;
  185. libais_send_response (conn_info, &res_lib_init, sizeof (res_lib_init));
  186. list_add (&conn_info->conn_list, &confchg_notify);
  187. if (conn_info->authenticated) {
  188. return (0);
  189. }
  190. return (-1);
  191. }
  192. static int message_handler_req_lib_activatepoll (struct conn_info *conn_info, void *message)
  193. {
  194. struct res_lib_activatepoll res_lib_activatepoll;
  195. res_lib_activatepoll.header.size = sizeof (struct res_lib_activatepoll);
  196. res_lib_activatepoll.header.id = MESSAGE_RES_LIB_ACTIVATEPOLL;
  197. res_lib_activatepoll.header.error = SA_OK;
  198. libais_send_response (conn_info, &res_lib_activatepoll,
  199. sizeof (struct res_lib_activatepoll));
  200. return (0);
  201. }
  202. static int message_handler_req_evs_join (struct conn_info *conn_info, void *message)
  203. {
  204. evs_error_t error = EVS_OK;
  205. struct req_lib_evs_join *req_lib_evs_join = (struct req_lib_evs_join *)message;
  206. struct res_lib_evs_join res_lib_evs_join;
  207. void *addr;
  208. if (req_lib_evs_join->group_entries > 50) {
  209. error = EVS_ERR_TOO_MANY_GROUPS;
  210. goto exit_error;
  211. }
  212. #ifdef DEBUG
  213. { int i;
  214. for (i = 0; i < req_lib_evs_join->group_entries; i++) {
  215. printf ("Joining group %s\n", req_lib_evs_join->groups[i].key);
  216. }
  217. }
  218. #endif
  219. addr = realloc (conn_info->ais_ci.u.libevs_ci.groups,
  220. sizeof (struct evs_group) *
  221. (conn_info->ais_ci.u.libevs_ci.group_entries + req_lib_evs_join->group_entries));
  222. if (addr == 0) {
  223. error = SA_ERR_NO_MEMORY;
  224. goto exit_error;
  225. }
  226. conn_info->ais_ci.u.libevs_ci.groups = addr;
  227. memcpy (&conn_info->ais_ci.u.libevs_ci.groups[conn_info->ais_ci.u.libevs_ci.group_entries],
  228. req_lib_evs_join->groups,
  229. sizeof (struct evs_group) * req_lib_evs_join->group_entries);
  230. conn_info->ais_ci.u.libevs_ci.group_entries += req_lib_evs_join->group_entries;
  231. exit_error:
  232. res_lib_evs_join.header.size = sizeof (struct res_lib_evs_join);
  233. res_lib_evs_join.header.id = MESSAGE_RES_EVS_JOIN;
  234. res_lib_evs_join.header.error = error;
  235. libais_send_response (conn_info, &res_lib_evs_join,
  236. sizeof (struct res_lib_evs_join));
  237. return (0);
  238. }
  239. static int message_handler_req_evs_leave (struct conn_info *conn_info, void *message)
  240. {
  241. struct req_lib_evs_leave *req_lib_evs_leave = (struct req_lib_evs_leave *)message;
  242. struct res_lib_evs_leave res_lib_evs_leave;
  243. evs_error_t error = EVS_OK;
  244. int error_index;
  245. int i, j;
  246. int found;
  247. for (i = 0; i < req_lib_evs_leave->group_entries; i++) {
  248. found = 0;
  249. for (j = 0; j < conn_info->ais_ci.u.libevs_ci.group_entries;) {
  250. if (memcmp (&req_lib_evs_leave->groups[i],
  251. &conn_info->ais_ci.u.libevs_ci.groups[j],
  252. sizeof (struct evs_group)) == 0) {
  253. /*
  254. * Delete entry
  255. */
  256. memmove (&conn_info->ais_ci.u.libevs_ci.groups[j],
  257. &conn_info->ais_ci.u.libevs_ci.groups[j + 1],
  258. (conn_info->ais_ci.u.libevs_ci.group_entries - j - 1) *
  259. sizeof (struct evs_group));
  260. conn_info->ais_ci.u.libevs_ci.group_entries -= 1;
  261. found = 1;
  262. break;
  263. } else {
  264. j++;
  265. }
  266. }
  267. if (found == 0) {
  268. error = EVS_ERR_NOT_EXIST;
  269. error_index = i;
  270. break;
  271. }
  272. }
  273. #ifdef DEBUG
  274. for (i = 0; i < conn_info->ais_ci.u.libevs_ci.group_entries; i++) {
  275. printf ("Groups Left %s\n",
  276. &conn_info->ais_ci.u.libevs_ci.groups[i].key);
  277. }
  278. #endif
  279. res_lib_evs_leave.header.size = sizeof (struct res_lib_evs_leave);
  280. res_lib_evs_leave.header.id = MESSAGE_RES_EVS_LEAVE;
  281. res_lib_evs_leave.header.error = error;
  282. libais_send_response (conn_info, &res_lib_evs_leave,
  283. sizeof (struct res_lib_evs_leave));
  284. return (0);
  285. }
  286. static int message_handler_req_evs_mcast_joined (struct conn_info *conn_info, void *message)
  287. {
  288. evs_error_t error = EVS_OK;
  289. struct req_lib_evs_mcast_joined *req_lib_evs_mcast_joined = (struct req_lib_evs_mcast_joined *)message;
  290. struct res_lib_evs_mcast_joined res_lib_evs_mcast_joined;
  291. struct iovec req_exec_evs_mcast_iovec[3];
  292. struct req_exec_evs_mcast req_exec_evs_mcast;
  293. req_exec_evs_mcast.header.size = sizeof (struct req_exec_evs_mcast);
  294. req_exec_evs_mcast.header.id = MESSAGE_REQ_EXEC_EVS_MCAST;
  295. req_exec_evs_mcast.msg_len = req_lib_evs_mcast_joined->msg_len;
  296. req_exec_evs_mcast.group_entries = conn_info->ais_ci.u.libevs_ci.group_entries;
  297. req_exec_evs_mcast_iovec[0].iov_base = &req_exec_evs_mcast;
  298. req_exec_evs_mcast_iovec[0].iov_len = sizeof (req_exec_evs_mcast);
  299. req_exec_evs_mcast_iovec[1].iov_base = conn_info->ais_ci.u.libevs_ci.groups;
  300. req_exec_evs_mcast_iovec[1].iov_len = conn_info->ais_ci.u.libevs_ci.group_entries * sizeof (struct evs_group);
  301. req_exec_evs_mcast_iovec[2].iov_base = &req_lib_evs_mcast_joined->msg;
  302. req_exec_evs_mcast_iovec[2].iov_len = req_lib_evs_mcast_joined->msg_len;
  303. assert (gmi_mcast (&aisexec_groupname, req_exec_evs_mcast_iovec, 3,
  304. req_lib_evs_mcast_joined->priority) == 0);
  305. res_lib_evs_mcast_joined.header.size = sizeof (struct res_lib_evs_mcast_joined);
  306. res_lib_evs_mcast_joined.header.id = MESSAGE_RES_EVS_MCAST_JOINED;
  307. res_lib_evs_mcast_joined.header.error = error;
  308. libais_send_response (conn_info, &res_lib_evs_mcast_joined,
  309. sizeof (struct res_lib_evs_mcast_joined));
  310. return (0);
  311. }
  312. static int message_handler_req_evs_mcast_groups (struct conn_info *conn_info, void *message)
  313. {
  314. evs_error_t error = EVS_OK;
  315. struct req_lib_evs_mcast_groups *req_lib_evs_mcast_groups = (struct req_lib_evs_mcast_groups *)message;
  316. struct res_lib_evs_mcast_groups res_lib_evs_mcast_groups;
  317. struct iovec req_exec_evs_mcast_iovec[3];
  318. struct req_exec_evs_mcast req_exec_evs_mcast;
  319. char *msg_addr;
  320. req_exec_evs_mcast.header.size = sizeof (struct req_exec_evs_mcast);
  321. req_exec_evs_mcast.header.id = MESSAGE_REQ_EXEC_EVS_MCAST;
  322. req_exec_evs_mcast.msg_len = req_lib_evs_mcast_groups->msg_len;
  323. req_exec_evs_mcast.group_entries = req_lib_evs_mcast_groups->group_entries;
  324. msg_addr = (char *)req_lib_evs_mcast_groups +
  325. sizeof (struct req_lib_evs_mcast_groups) +
  326. (sizeof (struct evs_group) * req_lib_evs_mcast_groups->group_entries);
  327. req_exec_evs_mcast_iovec[0].iov_base = &req_exec_evs_mcast;
  328. req_exec_evs_mcast_iovec[0].iov_len = sizeof (req_exec_evs_mcast);
  329. req_exec_evs_mcast_iovec[1].iov_base = &req_lib_evs_mcast_groups->groups;
  330. req_exec_evs_mcast_iovec[1].iov_len = sizeof (struct evs_group) * req_lib_evs_mcast_groups->group_entries;
  331. req_exec_evs_mcast_iovec[2].iov_base = msg_addr;
  332. req_exec_evs_mcast_iovec[2].iov_len = req_lib_evs_mcast_groups->msg_len;
  333. assert (gmi_mcast (&aisexec_groupname, req_exec_evs_mcast_iovec, 3,
  334. req_lib_evs_mcast_groups->priority) == 0);
  335. res_lib_evs_mcast_groups.header.size = sizeof (struct res_lib_evs_mcast_groups);
  336. res_lib_evs_mcast_groups.header.id = MESSAGE_RES_EVS_MCAST_GROUPS;
  337. res_lib_evs_mcast_groups.header.error = error;
  338. libais_send_response (conn_info, &res_lib_evs_mcast_groups,
  339. sizeof (struct res_lib_evs_mcast_groups));
  340. return (0);
  341. }
  342. static int message_handler_req_exec_mcast (void *message, struct in_addr source_addr)
  343. {
  344. struct req_exec_evs_mcast *req_exec_evs_mcast = (struct req_exec_evs_mcast *)message;
  345. struct res_evs_deliver_callback res_evs_deliver_callback;
  346. char *msg_addr;
  347. struct conn_info *conn_info;
  348. struct list_head *list;
  349. int found = 0;
  350. int i, j;
  351. res_evs_deliver_callback.header.size = sizeof (struct res_evs_deliver_callback) +
  352. req_exec_evs_mcast->msg_len;
  353. res_evs_deliver_callback.header.id = MESSAGE_RES_EVS_DELIVER_CALLBACK;
  354. res_evs_deliver_callback.header.error = SA_OK;
  355. res_evs_deliver_callback.msglen = req_exec_evs_mcast->msg_len;
  356. msg_addr = (char *)req_exec_evs_mcast + sizeof (struct req_exec_evs_mcast) +
  357. (sizeof (struct evs_group) * req_exec_evs_mcast->group_entries);
  358. for (list = confchg_notify.next; list != &confchg_notify; list = list->next) {
  359. found = 0;
  360. conn_info = list_entry (list, struct conn_info, conn_list);
  361. for (i = 0; i < conn_info->ais_ci.u.libevs_ci.group_entries; i++) {
  362. for (j = 0; j < req_exec_evs_mcast->group_entries; j++) {
  363. if (memcmp (&conn_info->ais_ci.u.libevs_ci.groups[i],
  364. &req_exec_evs_mcast->groups[j],
  365. sizeof (struct evs_group)) == 0) {
  366. found = 1;
  367. break;
  368. }
  369. }
  370. if (found) {
  371. break;
  372. }
  373. }
  374. if (found) {
  375. libais_send_response (conn_info, &res_evs_deliver_callback,
  376. sizeof (struct res_evs_deliver_callback));
  377. libais_send_response (conn_info, msg_addr,
  378. req_exec_evs_mcast->msg_len);
  379. }
  380. }
  381. return (0);
  382. }