evil.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. /*
  36. * Don't look, you will be disappointed
  37. */
  38. #include <config.h>
  39. #include <pthread.h>
  40. #include <assert.h>
  41. #include <sys/types.h>
  42. #include <sys/poll.h>
  43. #include <sys/uio.h>
  44. #include <sys/mman.h>
  45. #include <sys/socket.h>
  46. #include <sys/un.h>
  47. #include <sys/time.h>
  48. #include <sys/resource.h>
  49. #include <sys/stat.h>
  50. #include <sys/sysinfo.h>
  51. #include <netinet/in.h>
  52. #include <arpa/inet.h>
  53. #include <unistd.h>
  54. #include <fcntl.h>
  55. #include <stdlib.h>
  56. #include <stdio.h>
  57. #include <errno.h>
  58. #include <signal.h>
  59. #include <sched.h>
  60. #include <time.h>
  61. #include <corosync/totem/totempg.h>
  62. #include <corosync/swab.h>
  63. #include <corosync/corotypes.h>
  64. #include <corosync/coroipc_types.h>
  65. #include <corosync/corodefs.h>
  66. #include <corosync/list.h>
  67. #include <corosync/lcr/lcr_ifact.h>
  68. #include <corosync/engine/objdb.h>
  69. #include <corosync/engine/config.h>
  70. #include <corosync/engine/coroapi.h>
  71. #include <corosync/engine/logsys.h>
  72. #include <corosync/coroipcs.h>
  73. #include "sync.h"
  74. #include "evil.h"
  75. static void clm_sync_init (
  76. const unsigned int *member_list,
  77. size_t member_list_entries,
  78. const struct memb_ring_id *ring_id);
  79. static int clm_sync_process (void);
  80. static void clm_sync_activate (void);
  81. static void clm_sync_abort (void);
  82. static int clm_nodejoin_send (void);
  83. static int clm_hack_init (void);
  84. static struct sync_callbacks clm_sync_operations = {
  85. .name = "dummy CLM service",
  86. .sync_init = clm_sync_init,
  87. .sync_process = clm_sync_process,
  88. .sync_activate = clm_sync_activate,
  89. .sync_abort = clm_sync_abort,
  90. };
  91. static struct corosync_api_v1 *api = NULL;
  92. static void sync_dummy_init (
  93. const unsigned int *member_list,
  94. size_t member_list_entries,
  95. const struct memb_ring_id *ring_id)
  96. {
  97. }
  98. static int sync_dummy_process (void)
  99. {
  100. return (0);
  101. }
  102. static void sync_dummy_activate (void)
  103. {
  104. }
  105. static void sync_dummy_abort (void)
  106. {
  107. }
  108. void evil_init (struct corosync_api_v1 *api_in)
  109. {
  110. api = api_in;
  111. clm_hack_init ();
  112. }
  113. extern int evil_callbacks_load (int sync_id,
  114. struct sync_callbacks *callbacks)
  115. {
  116. int sync_dummy_found = 1;
  117. int callbacks_init = 1;
  118. memset (callbacks, 0, sizeof (struct sync_callbacks));
  119. switch (sync_id) {
  120. case EVS_SERVICE:
  121. callbacks_init = 0;
  122. break;
  123. case CLM_SERVICE:
  124. /*
  125. * ugh
  126. */
  127. memcpy (callbacks, &clm_sync_operations, sizeof (struct sync_callbacks));
  128. callbacks_init = 0;
  129. break;
  130. case AMF_SERVICE:
  131. callbacks->name = "dummy AMF service";
  132. break;
  133. case CKPT_SERVICE:
  134. callbacks->name = "dummy CKPT service";
  135. break;
  136. case EVT_SERVICE:
  137. callbacks->name = "dummy EVT service";
  138. break;
  139. case LCK_SERVICE:
  140. callbacks_init = 0;
  141. break;
  142. case MSG_SERVICE:
  143. callbacks_init = 0;
  144. break;
  145. case CFG_SERVICE:
  146. callbacks_init = 0;
  147. break;
  148. case CPG_SERVICE:
  149. callbacks->name = "dummy CPG service";
  150. break;
  151. case CONFDB_SERVICE:
  152. callbacks_init = 0;
  153. break;
  154. default:
  155. callbacks_init = 0;
  156. sync_dummy_found = 0;
  157. break;
  158. }
  159. if (callbacks_init) {
  160. callbacks->sync_init = sync_dummy_init;
  161. callbacks->sync_process = sync_dummy_process;
  162. callbacks->sync_activate = sync_dummy_activate;
  163. callbacks->sync_abort = sync_dummy_abort;
  164. }
  165. if (sync_dummy_found == 0) {
  166. return (-1);
  167. }
  168. return (0);
  169. }
  170. /*
  171. * This sends the clm nodejoin message required by clm services
  172. * on whitetank as well as the event service
  173. */
  174. enum clm_message_req_types {
  175. MESSAGE_REQ_EXEC_CLM_NODEJOIN = 0
  176. };
  177. #define MAR_CLM_MAX_ADDRESS_LENGTH 64
  178. #define SA_MAX_NAME_LENGTH 256
  179. typedef struct {
  180. int size __attribute__((aligned(8)));
  181. int id __attribute__((aligned(8)));
  182. } mar_req_header_t __attribute__((aligned(8)));
  183. typedef enum {
  184. MAR_CLM_AF_INET = 1,
  185. MAR_CLM_AF_INET6 = 2
  186. } mar_clm_node_address_family_t;
  187. typedef struct {
  188. unsigned short length __attribute__((aligned(8)));
  189. mar_clm_node_address_family_t family __attribute__((aligned(8)));
  190. unsigned char value[MAR_CLM_MAX_ADDRESS_LENGTH] __attribute__((aligned(8)));
  191. } mar_clm_node_address_t;
  192. typedef uint8_t mar_uint8_t;
  193. typedef uint16_t mar_uint16_t;
  194. typedef uint32_t mar_uint32_t;
  195. typedef uint64_t mar_uint64_t;
  196. typedef struct {
  197. mar_uint16_t length __attribute__((aligned(8)));
  198. mar_uint8_t value[SA_MAX_NAME_LENGTH] __attribute__((aligned(8)));
  199. } mar_name_t;
  200. typedef struct {
  201. mar_uint32_t node_id __attribute__((aligned(8)));
  202. mar_clm_node_address_t node_address __attribute__((aligned(8)));
  203. mar_name_t node_name __attribute__((aligned(8)));
  204. mar_uint32_t member __attribute__((aligned(8)));
  205. mar_uint64_t boot_timestamp __attribute__((aligned(8)));
  206. mar_uint64_t initial_view_number __attribute__((aligned(8)));
  207. } mar_clm_cluster_node_t;
  208. mar_clm_cluster_node_t my_cluster_node;
  209. struct req_exec_clm_nodejoin {
  210. mar_req_header_t header __attribute__((aligned(8)));
  211. mar_clm_cluster_node_t cluster_node __attribute__((aligned(8)));
  212. };
  213. static void my_cluster_node_load (void)
  214. {
  215. struct totem_ip_address interfaces[INTERFACE_MAX];
  216. unsigned int iface_count;
  217. char **status;
  218. const char *iface_string;
  219. totempg_ifaces_get (
  220. totempg_my_nodeid_get (),
  221. interfaces,
  222. &status,
  223. &iface_count);
  224. iface_string = totemip_print (&interfaces[0]);
  225. sprintf ((char *)my_cluster_node.node_address.value, "%s",
  226. iface_string);
  227. my_cluster_node.node_address.length =
  228. strlen ((char *)my_cluster_node.node_address.value);
  229. if (totempg_my_family_get () == AF_INET) {
  230. my_cluster_node.node_address.family = MAR_CLM_AF_INET;
  231. } else
  232. if (totempg_my_family_get () == AF_INET6) {
  233. my_cluster_node.node_address.family = MAR_CLM_AF_INET6;
  234. } else {
  235. assert (0);
  236. }
  237. strcpy ((char *)my_cluster_node.node_name.value,
  238. (char *)my_cluster_node.node_address.value);
  239. my_cluster_node.node_name.length =
  240. my_cluster_node.node_address.length;
  241. my_cluster_node.node_id = totempg_my_nodeid_get ();
  242. my_cluster_node.member = 1;
  243. }
  244. static int clm_hack_init (void)
  245. {
  246. #if defined(COROSYNC_LINUX)
  247. struct sysinfo s_info;
  248. time_t current_time;
  249. sysinfo (&s_info);
  250. current_time = time (NULL);
  251. /* (currenttime (s) - uptime (s)) * 1 billion (ns) / 1 (s) */
  252. my_cluster_node.boot_timestamp = ((uint64_t)(current_time - s_info.uptime)) * 1000000000ULL;
  253. #elif defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  254. int mib[2] = { CTL_KERN, KERN_BOOTTIME };
  255. struct timeval boot_time;
  256. size_t size = sizeof(boot_time);
  257. if ( sysctl(mib, 2, &boot_time, &size, NULL, 0) == -1 )
  258. boot_time.tv_sec = time (NULL);
  259. /* (currenttime (s) - uptime (s)) * 1 billion (ns) / 1 (s) */
  260. my_cluster_node.boot_timestamp = ((uint64_t)boot_time.tv_sec) * 1000000000ULL;
  261. #else /* defined(CTL_KERN) && defined(KERN_BOOTTIME) */
  262. #warning "no bootime support"
  263. #endif
  264. return (0);
  265. }
  266. static int clm_nodejoin_send (void)
  267. {
  268. struct req_exec_clm_nodejoin req_exec_clm_nodejoin;
  269. struct iovec req_exec_clm_iovec;
  270. int result;
  271. my_cluster_node_load ();
  272. req_exec_clm_nodejoin.header.size = sizeof (struct req_exec_clm_nodejoin);
  273. req_exec_clm_nodejoin.header.id =
  274. SERVICE_ID_MAKE (CLM_SERVICE, MESSAGE_REQ_EXEC_CLM_NODEJOIN);
  275. my_cluster_node.initial_view_number = 0;
  276. memcpy (&req_exec_clm_nodejoin.cluster_node, &my_cluster_node,
  277. sizeof (mar_clm_cluster_node_t));
  278. req_exec_clm_iovec.iov_base = (char *)&req_exec_clm_nodejoin;
  279. req_exec_clm_iovec.iov_len = sizeof (req_exec_clm_nodejoin);
  280. result = api->totem_mcast (&req_exec_clm_iovec, 1, TOTEMPG_AGREED);
  281. return (result);
  282. }
  283. /*
  284. * This is a noop for this service
  285. */
  286. static void clm_sync_init (
  287. const unsigned int *member_list,
  288. size_t member_list_entries,
  289. const struct memb_ring_id *ring_id)
  290. {
  291. return;
  292. }
  293. static int clm_sync_process (void)
  294. {
  295. /*
  296. * Send node information to other nodes
  297. */
  298. return (clm_nodejoin_send ());
  299. }
  300. /*
  301. * This is a noop for this service
  302. */
  303. static void clm_sync_activate (void)
  304. {
  305. return;
  306. }
  307. /*
  308. * This is a noop for this service
  309. */
  310. static void clm_sync_abort (void)
  311. {
  312. return;
  313. }