evs.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 "mempool.h"
  59. #include "handlers.h"
  60. #define LOG_SERVICE LOG_SERVICE_EVS
  61. #include "print.h"
  62. static DECLARE_LIST_INIT (confchg_notify);
  63. /*
  64. * Service Interfaces required by service_message_handler struct
  65. */
  66. static int evs_executive_initialize (void);
  67. static int evs_confchg_fn (
  68. enum gmi_configuration_type configuration_type,
  69. struct sockaddr_in *member_list, int member_list_entries,
  70. struct sockaddr_in *left_list, int left_list_entries,
  71. struct sockaddr_in *joined_list, int joined_list_entries);
  72. static int message_handler_req_exec_mcast (void *message, struct in_addr source_addr);
  73. static int message_handler_req_evs_init (struct conn_info *conn_info,
  74. void *message);
  75. static int message_handler_req_lib_activatepoll (struct conn_info *conn_info,
  76. void *message);
  77. static int message_handler_req_evs_join (struct conn_info *conn_info, void *message);
  78. static int message_handler_req_evs_leave (struct conn_info *conn_info, void *message);
  79. static int message_handler_req_evs_mcast_joined (struct conn_info *conn_info, void *message);
  80. static int message_handler_req_evs_mcast_groups (struct conn_info *conn_info, void *message);
  81. static int evs_exit_fn (struct conn_info *conn_info);
  82. struct libais_handler evs_libais_handlers[] =
  83. {
  84. { /* 0 */
  85. .libais_handler_fn = message_handler_req_lib_activatepoll,
  86. .response_size = sizeof (struct res_lib_activatepoll),
  87. .response_id = MESSAGE_RES_LIB_ACTIVATEPOLL, // TODO RESPONSE
  88. .gmi_prio = GMI_PRIO_RECOVERY
  89. },
  90. { /* 1 */
  91. .libais_handler_fn = message_handler_req_evs_join,
  92. .response_size = sizeof (struct res_lib_evs_join),
  93. .response_id = MESSAGE_RES_EVS_JOIN,
  94. .gmi_prio = GMI_PRIO_RECOVERY
  95. },
  96. { /* 2 */
  97. .libais_handler_fn = message_handler_req_evs_leave,
  98. .response_size = sizeof (struct res_lib_evs_leave),
  99. .response_id = MESSAGE_RES_EVS_LEAVE,
  100. .gmi_prio = GMI_PRIO_RECOVERY
  101. },
  102. { /* 3 */
  103. .libais_handler_fn = message_handler_req_evs_mcast_joined,
  104. .response_size = sizeof (struct res_lib_evs_mcast_joined),
  105. .response_id = MESSAGE_RES_EVS_MCAST_JOINED,
  106. .gmi_prio = GMI_PRIO_LOW
  107. },
  108. { /* 4 */
  109. .libais_handler_fn = message_handler_req_evs_mcast_groups,
  110. .response_size = sizeof (struct res_lib_evs_mcast_groups),
  111. .response_id = MESSAGE_RES_EVS_MCAST_GROUPS,
  112. .gmi_prio = GMI_PRIO_LOW
  113. }
  114. };
  115. static int (*evs_aisexec_handler_fns[]) (void *, struct in_addr source_addr) = {
  116. message_handler_req_exec_mcast
  117. };
  118. struct service_handler evs_service_handler = {
  119. .libais_handlers = evs_libais_handlers,
  120. .libais_handlers_count = sizeof (evs_libais_handlers) / sizeof (struct libais_handler),
  121. .aisexec_handler_fns = evs_aisexec_handler_fns,
  122. .aisexec_handler_fns_count = sizeof (evs_aisexec_handler_fns) / sizeof (int (*)),
  123. .confchg_fn = evs_confchg_fn,
  124. .libais_init_fn = message_handler_req_evs_init,
  125. .libais_exit_fn = evs_exit_fn,
  126. .exec_init_fn = evs_executive_initialize,
  127. .exec_dump_fn = 0
  128. };
  129. static int evs_executive_initialize (void)
  130. {
  131. return (0);
  132. }
  133. static int evs_exit_fn (struct conn_info *conn_info)
  134. {
  135. list_del (&conn_info->conn_list);
  136. return (0);
  137. }
  138. static int evs_confchg_fn (
  139. enum gmi_configuration_type configuration_type,
  140. struct sockaddr_in *member_list, int member_list_entries,
  141. struct sockaddr_in *left_list, int left_list_entries,
  142. struct sockaddr_in *joined_list, int joined_list_entries) {
  143. int i;
  144. struct list_head *list;
  145. struct res_evs_confchg_callback res_evs_confchg_callback;
  146. struct conn_info *conn_info;
  147. /*
  148. * Build configuration change message
  149. */
  150. res_evs_confchg_callback.header.size = sizeof (struct res_evs_confchg_callback);
  151. res_evs_confchg_callback.header.id = MESSAGE_RES_EVS_CONFCHG_CALLBACK;
  152. res_evs_confchg_callback.header.error = SA_OK;
  153. for (i = 0; i < member_list_entries; i++) {
  154. res_evs_confchg_callback.member_list[i].s_addr = member_list[i].sin_addr.s_addr;
  155. }
  156. res_evs_confchg_callback.member_list_entries = member_list_entries;
  157. for (i = 0; i < left_list_entries; i++) {
  158. res_evs_confchg_callback.left_list[i].s_addr = left_list[i].sin_addr.s_addr;
  159. }
  160. res_evs_confchg_callback.left_list_entries = left_list_entries;
  161. for (i = 0; i < joined_list_entries; i++) {
  162. res_evs_confchg_callback.joined_list[i].s_addr = joined_list[i].sin_addr.s_addr;
  163. }
  164. res_evs_confchg_callback.joined_list_entries = joined_list_entries;
  165. /*
  166. * Send configuration change message to every EVS library user
  167. */
  168. for (list = confchg_notify.next; list != &confchg_notify; list = list->next) {
  169. conn_info = list_entry (list, struct conn_info, conn_list);
  170. libais_send_response (conn_info, &res_evs_confchg_callback,
  171. sizeof (res_evs_confchg_callback));
  172. }
  173. return (0);
  174. }
  175. static int message_handler_req_evs_init (struct conn_info *conn_info, void *message)
  176. {
  177. SaErrorT error = SA_ERR_SECURITY;
  178. struct res_lib_init res_lib_init;
  179. log_printf (LOG_LEVEL_DEBUG, "Got request to initalize evs service.\n");
  180. if (conn_info->authenticated) {
  181. conn_info->service = SOCKET_SERVICE_EVS;
  182. error = SA_OK;
  183. }
  184. res_lib_init.header.size = sizeof (struct res_lib_init);
  185. res_lib_init.header.id = MESSAGE_RES_INIT;
  186. res_lib_init.header.error = error;
  187. libais_send_response (conn_info, &res_lib_init, sizeof (res_lib_init));
  188. list_add (&conn_info->conn_list, &confchg_notify);
  189. if (conn_info->authenticated) {
  190. return (0);
  191. }
  192. return (-1);
  193. }
  194. static int message_handler_req_lib_activatepoll (struct conn_info *conn_info, void *message)
  195. {
  196. struct res_lib_activatepoll res_lib_activatepoll;
  197. res_lib_activatepoll.header.size = sizeof (struct res_lib_activatepoll);
  198. res_lib_activatepoll.header.id = MESSAGE_RES_LIB_ACTIVATEPOLL;
  199. res_lib_activatepoll.header.error = SA_OK;
  200. libais_send_response (conn_info, &res_lib_activatepoll,
  201. sizeof (struct res_lib_activatepoll));
  202. return (0);
  203. }
  204. static int message_handler_req_evs_join (struct conn_info *conn_info, void *message)
  205. {
  206. evs_error_t error = EVS_OK;
  207. struct req_lib_evs_join *req_lib_evs_join = (struct req_lib_evs_join *)message;
  208. struct res_lib_evs_join res_lib_evs_join;
  209. void *addr;
  210. if (req_lib_evs_join->group_entries > 50) {
  211. error = EVS_ERR_TOO_MANY_GROUPS;
  212. goto exit_error;
  213. }
  214. #ifdef DEBUG
  215. { int i;
  216. for (i = 0; i < req_lib_evs_join->group_entries; i++) {
  217. printf ("Joining group %s\n", req_lib_evs_join->groups[i].key);
  218. }
  219. }
  220. #endif
  221. addr = realloc (conn_info->ais_ci.u.libevs_ci.groups,
  222. sizeof (struct evs_group) *
  223. (conn_info->ais_ci.u.libevs_ci.group_entries + req_lib_evs_join->group_entries));
  224. if (addr == 0) {
  225. error = SA_ERR_NO_MEMORY;
  226. goto exit_error;
  227. }
  228. conn_info->ais_ci.u.libevs_ci.groups = addr;
  229. memcpy (&conn_info->ais_ci.u.libevs_ci.groups[conn_info->ais_ci.u.libevs_ci.group_entries],
  230. req_lib_evs_join->groups,
  231. sizeof (struct evs_group) * req_lib_evs_join->group_entries);
  232. conn_info->ais_ci.u.libevs_ci.group_entries += req_lib_evs_join->group_entries;
  233. exit_error:
  234. res_lib_evs_join.header.size = sizeof (struct res_lib_evs_join);
  235. res_lib_evs_join.header.id = MESSAGE_RES_EVS_JOIN;
  236. res_lib_evs_join.header.error = error;
  237. libais_send_response (conn_info, &res_lib_evs_join,
  238. sizeof (struct res_lib_evs_join));
  239. return (0);
  240. }
  241. static int message_handler_req_evs_leave (struct conn_info *conn_info, void *message)
  242. {
  243. struct req_lib_evs_leave *req_lib_evs_leave = (struct req_lib_evs_leave *)message;
  244. struct res_lib_evs_leave res_lib_evs_leave;
  245. evs_error_t error = EVS_OK;
  246. int error_index;
  247. int i, j;
  248. int found;
  249. for (i = 0; i < req_lib_evs_leave->group_entries; i++) {
  250. found = 0;
  251. for (j = 0; j < conn_info->ais_ci.u.libevs_ci.group_entries;) {
  252. if (memcmp (&req_lib_evs_leave->groups[i],
  253. &conn_info->ais_ci.u.libevs_ci.groups[j],
  254. sizeof (struct evs_group)) == 0) {
  255. /*
  256. * Delete entry
  257. */
  258. memmove (&conn_info->ais_ci.u.libevs_ci.groups[j],
  259. &conn_info->ais_ci.u.libevs_ci.groups[j + 1],
  260. (conn_info->ais_ci.u.libevs_ci.group_entries - j - 1) *
  261. sizeof (struct evs_group));
  262. conn_info->ais_ci.u.libevs_ci.group_entries -= 1;
  263. found = 1;
  264. break;
  265. } else {
  266. j++;
  267. }
  268. }
  269. if (found == 0) {
  270. error = EVS_ERR_NOT_EXIST;
  271. error_index = i;
  272. break;
  273. }
  274. }
  275. #ifdef DEBUG
  276. for (i = 0; i < conn_info->ais_ci.u.libevs_ci.group_entries; i++) {
  277. printf ("Groups Left %s\n",
  278. &conn_info->ais_ci.u.libevs_ci.groups[i].key);
  279. }
  280. #endif
  281. res_lib_evs_leave.header.size = sizeof (struct res_lib_evs_leave);
  282. res_lib_evs_leave.header.id = MESSAGE_RES_EVS_LEAVE;
  283. res_lib_evs_leave.header.error = error;
  284. libais_send_response (conn_info, &res_lib_evs_leave,
  285. sizeof (struct res_lib_evs_leave));
  286. return (0);
  287. }
  288. static int message_handler_req_evs_mcast_joined (struct conn_info *conn_info, void *message)
  289. {
  290. evs_error_t error = EVS_OK;
  291. struct req_lib_evs_mcast_joined *req_lib_evs_mcast_joined = (struct req_lib_evs_mcast_joined *)message;
  292. struct res_lib_evs_mcast_joined res_lib_evs_mcast_joined;
  293. struct iovec req_exec_evs_mcast_iovec[3];
  294. struct req_exec_evs_mcast req_exec_evs_mcast;
  295. req_exec_evs_mcast.header.size = sizeof (struct req_exec_evs_mcast);
  296. req_exec_evs_mcast.header.id = MESSAGE_REQ_EXEC_EVS_MCAST;
  297. req_exec_evs_mcast.msg_len = req_lib_evs_mcast_joined->msg_len;
  298. req_exec_evs_mcast.group_entries = conn_info->ais_ci.u.libevs_ci.group_entries;
  299. req_exec_evs_mcast_iovec[0].iov_base = &req_exec_evs_mcast;
  300. req_exec_evs_mcast_iovec[0].iov_len = sizeof (req_exec_evs_mcast);
  301. req_exec_evs_mcast_iovec[1].iov_base = conn_info->ais_ci.u.libevs_ci.groups;
  302. req_exec_evs_mcast_iovec[1].iov_len = conn_info->ais_ci.u.libevs_ci.group_entries * sizeof (struct evs_group);
  303. req_exec_evs_mcast_iovec[2].iov_base = &req_lib_evs_mcast_joined->msg;
  304. req_exec_evs_mcast_iovec[2].iov_len = req_lib_evs_mcast_joined->msg_len;
  305. assert (gmi_mcast (&aisexec_groupname, req_exec_evs_mcast_iovec, 3,
  306. req_lib_evs_mcast_joined->priority) == 0);
  307. res_lib_evs_mcast_joined.header.size = sizeof (struct res_lib_evs_mcast_joined);
  308. res_lib_evs_mcast_joined.header.id = MESSAGE_RES_EVS_MCAST_JOINED;
  309. res_lib_evs_mcast_joined.header.error = error;
  310. libais_send_response (conn_info, &res_lib_evs_mcast_joined,
  311. sizeof (struct res_lib_evs_mcast_joined));
  312. return (0);
  313. }
  314. static int message_handler_req_evs_mcast_groups (struct conn_info *conn_info, void *message)
  315. {
  316. evs_error_t error = EVS_OK;
  317. struct req_lib_evs_mcast_groups *req_lib_evs_mcast_groups = (struct req_lib_evs_mcast_groups *)message;
  318. struct res_lib_evs_mcast_groups res_lib_evs_mcast_groups;
  319. struct iovec req_exec_evs_mcast_iovec[3];
  320. struct req_exec_evs_mcast req_exec_evs_mcast;
  321. char *msg_addr;
  322. req_exec_evs_mcast.header.size = sizeof (struct req_exec_evs_mcast);
  323. req_exec_evs_mcast.header.id = MESSAGE_REQ_EXEC_EVS_MCAST;
  324. req_exec_evs_mcast.msg_len = req_lib_evs_mcast_groups->msg_len;
  325. req_exec_evs_mcast.group_entries = req_lib_evs_mcast_groups->group_entries;
  326. msg_addr = (char *)req_lib_evs_mcast_groups +
  327. sizeof (struct req_lib_evs_mcast_groups) +
  328. (sizeof (struct evs_group) * req_lib_evs_mcast_groups->group_entries);
  329. req_exec_evs_mcast_iovec[0].iov_base = &req_exec_evs_mcast;
  330. req_exec_evs_mcast_iovec[0].iov_len = sizeof (req_exec_evs_mcast);
  331. req_exec_evs_mcast_iovec[1].iov_base = &req_lib_evs_mcast_groups->groups;
  332. req_exec_evs_mcast_iovec[1].iov_len = sizeof (struct evs_group) * req_lib_evs_mcast_groups->group_entries;
  333. req_exec_evs_mcast_iovec[2].iov_base = msg_addr;
  334. req_exec_evs_mcast_iovec[2].iov_len = req_lib_evs_mcast_groups->msg_len;
  335. assert (gmi_mcast (&aisexec_groupname, req_exec_evs_mcast_iovec, 3,
  336. req_lib_evs_mcast_groups->priority) == 0);
  337. res_lib_evs_mcast_groups.header.size = sizeof (struct res_lib_evs_mcast_groups);
  338. res_lib_evs_mcast_groups.header.id = MESSAGE_RES_EVS_MCAST_GROUPS;
  339. res_lib_evs_mcast_groups.header.error = error;
  340. libais_send_response (conn_info, &res_lib_evs_mcast_groups,
  341. sizeof (struct res_lib_evs_mcast_groups));
  342. return (0);
  343. }
  344. static int message_handler_req_exec_mcast (void *message, struct in_addr source_addr)
  345. {
  346. struct req_exec_evs_mcast *req_exec_evs_mcast = (struct req_exec_evs_mcast *)message;
  347. struct res_evs_deliver_callback res_evs_deliver_callback;
  348. char *msg_addr;
  349. struct conn_info *conn_info;
  350. struct list_head *list;
  351. int found = 0;
  352. int i, j;
  353. res_evs_deliver_callback.header.size = sizeof (struct res_evs_deliver_callback) +
  354. req_exec_evs_mcast->msg_len;
  355. res_evs_deliver_callback.header.id = MESSAGE_RES_EVS_DELIVER_CALLBACK;
  356. res_evs_deliver_callback.header.error = SA_OK;
  357. res_evs_deliver_callback.msglen = req_exec_evs_mcast->msg_len;
  358. msg_addr = (char *)req_exec_evs_mcast + sizeof (struct req_exec_evs_mcast) +
  359. (sizeof (struct evs_group) * req_exec_evs_mcast->group_entries);
  360. for (list = confchg_notify.next; list != &confchg_notify; list = list->next) {
  361. found = 0;
  362. conn_info = list_entry (list, struct conn_info, conn_list);
  363. for (i = 0; i < conn_info->ais_ci.u.libevs_ci.group_entries; i++) {
  364. for (j = 0; j < req_exec_evs_mcast->group_entries; j++) {
  365. if (memcmp (&conn_info->ais_ci.u.libevs_ci.groups[i],
  366. &req_exec_evs_mcast->groups[j],
  367. sizeof (struct evs_group)) == 0) {
  368. found = 1;
  369. break;
  370. }
  371. }
  372. if (found) {
  373. break;
  374. }
  375. }
  376. if (found) {
  377. res_evs_deliver_callback.source_addr.s_addr = source_addr.s_addr;
  378. libais_send_response (conn_info, &res_evs_deliver_callback,
  379. sizeof (struct res_evs_deliver_callback));
  380. libais_send_response (conn_info, msg_addr,
  381. req_exec_evs_mcast->msg_len);
  382. }
  383. }
  384. return (0);
  385. }