evs.c 15 KB

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