evil.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 <netinet/in.h>
  51. #include <arpa/inet.h>
  52. #include <unistd.h>
  53. #include <fcntl.h>
  54. #include <stdlib.h>
  55. #include <stdio.h>
  56. #include <errno.h>
  57. #include <signal.h>
  58. #include <sched.h>
  59. #include <time.h>
  60. #include <corosync/totem/totempg.h>
  61. #include <corosync/swab.h>
  62. #include <corosync/corotypes.h>
  63. #include <corosync/coroipc_types.h>
  64. #include <corosync/corodefs.h>
  65. #include <corosync/list.h>
  66. #include <corosync/lcr/lcr_ifact.h>
  67. #include <corosync/engine/objdb.h>
  68. #include <corosync/engine/config.h>
  69. #include <corosync/engine/coroapi.h>
  70. #include <corosync/engine/logsys.h>
  71. #include <corosync/coroipcs.h>
  72. #include "sync.h"
  73. #include "evil.h"
  74. static void clm_sync_init (void);
  75. static int clm_sync_process (void);
  76. static void clm_sync_activate (void);
  77. static void clm_sync_abort (void);
  78. static int clm_nodejoin_send (void);
  79. static int clm_hack_init (void);
  80. static struct sync_callbacks clm_sync_operations = {
  81. .name = "dummy CLM service",
  82. .sync_init = clm_sync_init,
  83. .sync_process = clm_sync_process,
  84. .sync_activate = clm_sync_activate,
  85. .sync_abort = clm_sync_abort,
  86. };
  87. static struct corosync_api_v1 *api = NULL;
  88. static void sync_dummy_init (void)
  89. {
  90. }
  91. static int sync_dummy_process (void)
  92. {
  93. return (0);
  94. }
  95. static void sync_dummy_activate (void)
  96. {
  97. }
  98. static void sync_dummy_abort (void)
  99. {
  100. }
  101. void evil_init (struct corosync_api_v1 *api_in)
  102. {
  103. api = api_in;
  104. clm_hack_init ();
  105. }
  106. extern int evil_callbacks_load (int sync_id,
  107. struct sync_callbacks *callbacks)
  108. {
  109. int sync_dummy_found = 1;
  110. int callbacks_init = 1;
  111. memset (callbacks, 0, sizeof (struct sync_callbacks));
  112. switch (sync_id) {
  113. case EVS_SERVICE:
  114. callbacks_init = 0;
  115. break;
  116. case CLM_SERVICE:
  117. /*
  118. * ugh
  119. */
  120. memcpy (callbacks, &clm_sync_operations, sizeof (struct sync_callbacks));
  121. callbacks_init = 0;
  122. break;
  123. case AMF_SERVICE:
  124. callbacks->name = "dummy AMF service";
  125. break;
  126. case CKPT_SERVICE:
  127. callbacks->name = "dummy CKPT service";
  128. break;
  129. case EVT_SERVICE:
  130. callbacks->name = "dummy EVT service";
  131. break;
  132. case LCK_SERVICE:
  133. callbacks_init = 0;
  134. break;
  135. case MSG_SERVICE:
  136. callbacks_init = 0;
  137. break;
  138. case CFG_SERVICE:
  139. callbacks_init = 0;
  140. break;
  141. case CPG_SERVICE:
  142. callbacks->name = "dummy CPG service";
  143. break;
  144. case CONFDB_SERVICE:
  145. callbacks_init = 0;
  146. break;
  147. default:
  148. callbacks_init = 0;
  149. sync_dummy_found = 0;
  150. break;
  151. }
  152. if (callbacks_init) {
  153. callbacks->sync_init = sync_dummy_init;
  154. callbacks->sync_process = sync_dummy_process;
  155. callbacks->sync_activate = sync_dummy_activate;
  156. callbacks->sync_abort = sync_dummy_abort;
  157. }
  158. if (sync_dummy_found == 0) {
  159. return (-1);
  160. }
  161. return (0);
  162. }
  163. /*
  164. * This sends the clm nodejoin message required by clm services
  165. * on whitetank as well as the event service
  166. */
  167. enum clm_message_req_types {
  168. MESSAGE_REQ_EXEC_CLM_NODEJOIN = 0
  169. };
  170. #define MAR_CLM_MAX_ADDRESS_LENGTH 64
  171. #define SA_MAX_NAME_LENGTH 256
  172. typedef struct {
  173. int size __attribute__((aligned(8)));
  174. int id __attribute__((aligned(8)));
  175. } mar_req_header_t __attribute__((aligned(8)));
  176. typedef enum {
  177. MAR_CLM_AF_INET = 1,
  178. MAR_CLM_AF_INET6 = 2
  179. } mar_clm_node_address_family_t;
  180. typedef struct {
  181. unsigned short length __attribute__((aligned(8)));
  182. mar_clm_node_address_family_t family __attribute__((aligned(8)));
  183. unsigned char value[MAR_CLM_MAX_ADDRESS_LENGTH] __attribute__((aligned(8)));
  184. } mar_clm_node_address_t;
  185. typedef uint8_t mar_uint8_t;
  186. typedef uint16_t mar_uint16_t;
  187. typedef uint32_t mar_uint32_t;
  188. typedef uint64_t mar_uint64_t;
  189. typedef struct {
  190. mar_uint16_t length __attribute__((aligned(8)));
  191. mar_uint8_t value[SA_MAX_NAME_LENGTH] __attribute__((aligned(8)));
  192. } mar_name_t;
  193. typedef struct {
  194. mar_uint32_t node_id __attribute__((aligned(8)));
  195. mar_clm_node_address_t node_address __attribute__((aligned(8)));
  196. mar_name_t node_name __attribute__((aligned(8)));
  197. mar_uint32_t member __attribute__((aligned(8)));
  198. mar_uint64_t boot_timestamp __attribute__((aligned(8)));
  199. mar_uint64_t initial_view_number __attribute__((aligned(8)));
  200. } mar_clm_cluster_node_t;
  201. mar_clm_cluster_node_t my_cluster_node;
  202. struct req_exec_clm_nodejoin {
  203. mar_req_header_t header __attribute__((aligned(8)));
  204. mar_clm_cluster_node_t cluster_node __attribute__((aligned(8)));
  205. };
  206. static void my_cluster_node_load (void)
  207. {
  208. struct totem_ip_address interfaces[INTERFACE_MAX];
  209. unsigned int iface_count;
  210. char **status;
  211. const char *iface_string;
  212. totempg_ifaces_get (
  213. totempg_my_nodeid_get (),
  214. interfaces,
  215. &status,
  216. &iface_count);
  217. iface_string = totemip_print (&interfaces[0]);
  218. sprintf ((char *)my_cluster_node.node_address.value, "%s",
  219. iface_string);
  220. my_cluster_node.node_address.length =
  221. strlen ((char *)my_cluster_node.node_address.value);
  222. if (totempg_my_family_get () == AF_INET) {
  223. my_cluster_node.node_address.family = MAR_CLM_AF_INET;
  224. } else
  225. if (totempg_my_family_get () == AF_INET6) {
  226. my_cluster_node.node_address.family = MAR_CLM_AF_INET6;
  227. } else {
  228. assert (0);
  229. }
  230. strcpy ((char *)my_cluster_node.node_name.value,
  231. (char *)my_cluster_node.node_address.value);
  232. my_cluster_node.node_name.length =
  233. my_cluster_node.node_address.length;
  234. my_cluster_node.node_id = totempg_my_nodeid_get ();
  235. my_cluster_node.member = 1;
  236. }
  237. static int clm_hack_init (void)
  238. {
  239. #if defined(OPENAIS_LINUX)
  240. struct sysinfo s_info;
  241. time_t current_time;
  242. sysinfo (&s_info);
  243. current_time = time (NULL);
  244. /* (currenttime (s) - uptime (s)) * 1 billion (ns) / 1 (s) */
  245. my_cluster_node.boot_timestamp = ((SaTimeT)(current_time - s_info.uptime)) * 1000000000;
  246. #elif defined(COROSYNC_BSD) || defined(COROSYNC_DARWIN)
  247. int mib[2] = { CTL_KERN, KERN_BOOTTIME };
  248. struct timeval boot_time;
  249. size_t size = sizeof(boot_time);
  250. if ( sysctl(mib, 2, &boot_time, &size, NULL, 0) == -1 )
  251. boot_time.tv_sec = time (NULL);
  252. /* (currenttime (s) - uptime (s)) * 1 billion (ns) / 1 (s) */
  253. my_cluster_node.boot_timestamp = ((SaTimeT)boot_time.tv_sec) * 1000000000;
  254. #else /* defined(CTL_KERN) && defined(KERN_BOOTTIME) */
  255. #warning "no bootime support"
  256. #endif
  257. return (0);
  258. }
  259. static int clm_nodejoin_send (void)
  260. {
  261. struct req_exec_clm_nodejoin req_exec_clm_nodejoin;
  262. struct iovec req_exec_clm_iovec;
  263. int result;
  264. my_cluster_node_load ();
  265. req_exec_clm_nodejoin.header.size = sizeof (struct req_exec_clm_nodejoin);
  266. req_exec_clm_nodejoin.header.id =
  267. SERVICE_ID_MAKE (CLM_SERVICE, MESSAGE_REQ_EXEC_CLM_NODEJOIN);
  268. my_cluster_node.initial_view_number = 0;
  269. memcpy (&req_exec_clm_nodejoin.cluster_node, &my_cluster_node,
  270. sizeof (mar_clm_cluster_node_t));
  271. req_exec_clm_iovec.iov_base = (char *)&req_exec_clm_nodejoin;
  272. req_exec_clm_iovec.iov_len = sizeof (req_exec_clm_nodejoin);
  273. result = api->totem_mcast (&req_exec_clm_iovec, 1, TOTEMPG_AGREED);
  274. return (result);
  275. }
  276. /*
  277. * This is a noop for this service
  278. */
  279. static void clm_sync_init (void)
  280. {
  281. return;
  282. }
  283. static int clm_sync_process (void)
  284. {
  285. /*
  286. * Send node information to other nodes
  287. */
  288. return (clm_nodejoin_send ());
  289. }
  290. /*
  291. * This is a noop for this service
  292. */
  293. static void clm_sync_activate (void)
  294. {
  295. return;
  296. }
  297. /*
  298. * This is a noop for this service
  299. */
  300. static void clm_sync_abort (void)
  301. {
  302. return;
  303. }