evil.c 9.0 KB

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