totemconfig.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2008 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. #include <stdio.h>
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #include <errno.h>
  39. #include <assert.h>
  40. #include <unistd.h>
  41. #include <sys/socket.h>
  42. #include <sys/types.h>
  43. #include <sys/stat.h>
  44. #include <fcntl.h>
  45. #include <netinet/in.h>
  46. #include <arpa/inet.h>
  47. #include <sys/param.h>
  48. #include <corosync/swab.h>
  49. #include <corosync/list.h>
  50. #include <corosync/totem/totem.h>
  51. #include <corosync/engine/objdb.h>
  52. #include <corosync/engine/config.h>
  53. #include <corosync/engine/logsys.h>
  54. #include "util.h"
  55. #include "totemconfig.h"
  56. #include "tlist.h" /* for HZ */
  57. #define TOKEN_RETRANSMITS_BEFORE_LOSS_CONST 4
  58. #define TOKEN_TIMEOUT 1000
  59. #define TOKEN_RETRANSMIT_TIMEOUT (int)(TOKEN_TIMEOUT / (TOKEN_RETRANSMITS_BEFORE_LOSS_CONST + 0.2))
  60. #define TOKEN_HOLD_TIMEOUT (int)(TOKEN_RETRANSMIT_TIMEOUT * 0.8 - (1000/(int)HZ))
  61. #define JOIN_TIMEOUT 50
  62. #define CONSENSUS_TIMEOUT 800
  63. #define MERGE_TIMEOUT 200
  64. #define DOWNCHECK_TIMEOUT 1000
  65. #define FAIL_TO_RECV_CONST 50
  66. #define SEQNO_UNCHANGED_CONST 30
  67. #define MINIMUM_TIMEOUT (int)(1000/HZ)*3
  68. #define MAX_NETWORK_DELAY 50
  69. #define WINDOW_SIZE 50
  70. #define MAX_MESSAGES 17
  71. #define RRP_PROBLEM_COUNT_TIMEOUT 2000
  72. #define RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT 10
  73. #define RRP_PROBLEM_COUNT_THRESHOLD_MIN 5
  74. static char error_string_response[512];
  75. static struct objdb_iface_ver0 *global_objdb;
  76. static void add_totem_config_notification(
  77. struct objdb_iface_ver0 *objdb,
  78. struct totem_config *totem_config,
  79. hdb_handle_t totem_object_handle);
  80. /* These just makes the code below a little neater */
  81. static inline int objdb_get_string (
  82. struct objdb_iface_ver0 *objdb,
  83. hdb_handle_t object_service_handle,
  84. char *key, char **value)
  85. {
  86. int res;
  87. *value = NULL;
  88. if ( !(res = objdb->object_key_get (object_service_handle,
  89. key,
  90. strlen (key),
  91. (void *)value,
  92. NULL))) {
  93. if (*value) {
  94. return 0;
  95. }
  96. }
  97. return -1;
  98. }
  99. static inline void objdb_get_int (
  100. struct objdb_iface_ver0 *objdb,
  101. hdb_handle_t object_service_handle,
  102. char *key, unsigned int *intvalue)
  103. {
  104. char *value = NULL;
  105. if (!objdb->object_key_get (object_service_handle,
  106. key,
  107. strlen (key),
  108. (void *)&value,
  109. NULL)) {
  110. if (value) {
  111. *intvalue = atoi(value);
  112. }
  113. }
  114. }
  115. static unsigned int totem_handle_find (
  116. struct objdb_iface_ver0 *objdb,
  117. hdb_handle_t *totem_find_handle) {
  118. hdb_handle_t object_find_handle;
  119. unsigned int res;
  120. /*
  121. * Find a network section
  122. */
  123. objdb->object_find_create (
  124. OBJECT_PARENT_HANDLE,
  125. "network",
  126. strlen ("network"),
  127. &object_find_handle);
  128. res = objdb->object_find_next (
  129. object_find_handle,
  130. totem_find_handle);
  131. objdb->object_find_destroy (object_find_handle);
  132. /*
  133. * Network section not found in configuration, checking for totem
  134. */
  135. if (res == -1) {
  136. objdb->object_find_create (
  137. OBJECT_PARENT_HANDLE,
  138. "totem",
  139. strlen ("totem"),
  140. &object_find_handle);
  141. res = objdb->object_find_next (
  142. object_find_handle,
  143. totem_find_handle);
  144. objdb->object_find_destroy (object_find_handle);
  145. }
  146. if (res == -1) {
  147. return (-1);
  148. }
  149. return (0);
  150. }
  151. static void totem_volatile_config_read (
  152. struct objdb_iface_ver0 *objdb,
  153. struct totem_config *totem_config,
  154. hdb_handle_t object_totem_handle)
  155. {
  156. objdb_get_int (objdb,object_totem_handle, "token", &totem_config->token_timeout);
  157. objdb_get_int (objdb,object_totem_handle, "token_retransmit", &totem_config->token_retransmit_timeout);
  158. objdb_get_int (objdb,object_totem_handle, "hold", &totem_config->token_hold_timeout);
  159. objdb_get_int (objdb,object_totem_handle, "token_retransmits_before_loss_const", &totem_config->token_retransmits_before_loss_const);
  160. objdb_get_int (objdb,object_totem_handle, "join", &totem_config->join_timeout);
  161. objdb_get_int (objdb,object_totem_handle, "send_join", &totem_config->send_join_timeout);
  162. objdb_get_int (objdb,object_totem_handle, "consensus", &totem_config->consensus_timeout);
  163. objdb_get_int (objdb,object_totem_handle, "merge", &totem_config->merge_timeout);
  164. objdb_get_int (objdb,object_totem_handle, "downcheck", &totem_config->downcheck_timeout);
  165. objdb_get_int (objdb,object_totem_handle, "fail_recv_const", &totem_config->fail_to_recv_const);
  166. objdb_get_int (objdb,object_totem_handle, "seqno_unchanged_const", &totem_config->seqno_unchanged_const);
  167. objdb_get_int (objdb,object_totem_handle, "rrp_token_expired_timeout", &totem_config->rrp_token_expired_timeout);
  168. objdb_get_int (objdb,object_totem_handle, "rrp_problem_count_timeout", &totem_config->rrp_problem_count_timeout);
  169. objdb_get_int (objdb,object_totem_handle, "rrp_problem_count_threshold", &totem_config->rrp_problem_count_threshold);
  170. objdb_get_int (objdb,object_totem_handle, "heartbeat_failures_allowed", &totem_config->heartbeat_failures_allowed);
  171. objdb_get_int (objdb,object_totem_handle, "max_network_delay", &totem_config->max_network_delay);
  172. objdb_get_int (objdb,object_totem_handle, "window_size", &totem_config->window_size);
  173. objdb_get_string (objdb, object_totem_handle, "vsftype", &totem_config->vsf_type);
  174. objdb_get_int (objdb,object_totem_handle, "max_messages", &totem_config->max_messages);
  175. }
  176. extern int totem_config_read (
  177. struct objdb_iface_ver0 *objdb,
  178. struct totem_config *totem_config,
  179. const char **error_string)
  180. {
  181. int res = 0;
  182. hdb_handle_t object_totem_handle;
  183. hdb_handle_t object_interface_handle;
  184. char *str;
  185. unsigned int ringnumber = 0;
  186. hdb_handle_t object_find_interface_handle;
  187. res = totem_handle_find (objdb, &object_totem_handle);
  188. if (res == -1) {
  189. printf ("couldn't find totem handle\n");
  190. return (-1);
  191. }
  192. memset (totem_config, 0, sizeof (struct totem_config));
  193. totem_config->interfaces = malloc (sizeof (struct totem_interface) * INTERFACE_MAX);
  194. if (totem_config->interfaces == 0) {
  195. *error_string = "Out of memory trying to allocate ethernet interface storage area";
  196. return -1;
  197. }
  198. memset (totem_config->interfaces, 0,
  199. sizeof (struct totem_interface) * INTERFACE_MAX);
  200. totem_config->secauth = 1;
  201. strcpy (totem_config->rrp_mode, "none");
  202. if (!objdb_get_string (objdb, object_totem_handle, "version", &str)) {
  203. if (strcmp (str, "2") == 0) {
  204. totem_config->version = 2;
  205. }
  206. }
  207. if (!objdb_get_string (objdb, object_totem_handle, "secauth", &str)) {
  208. if (strcmp (str, "on") == 0) {
  209. totem_config->secauth = 1;
  210. }
  211. if (strcmp (str, "off") == 0) {
  212. totem_config->secauth = 0;
  213. }
  214. }
  215. if (!objdb_get_string (objdb, object_totem_handle, "rrp_mode", &str)) {
  216. strcpy (totem_config->rrp_mode, str);
  217. }
  218. /*
  219. * Get interface node id
  220. */
  221. objdb_get_int (objdb, object_totem_handle, "nodeid", &totem_config->node_id);
  222. objdb_get_int (objdb,object_totem_handle, "threads", &totem_config->threads);
  223. objdb_get_int (objdb,object_totem_handle, "netmtu", &totem_config->net_mtu);
  224. /*
  225. * Get things that might change in the future
  226. */
  227. totem_volatile_config_read (objdb, totem_config, object_totem_handle);
  228. objdb->object_find_create (
  229. object_totem_handle,
  230. "interface",
  231. strlen ("interface"),
  232. &object_find_interface_handle);
  233. while (objdb->object_find_next (
  234. object_find_interface_handle,
  235. &object_interface_handle) == 0) {
  236. objdb_get_int (objdb, object_interface_handle, "ringnumber", &ringnumber);
  237. /*
  238. * Get interface multicast address
  239. */
  240. if (!objdb_get_string (objdb, object_interface_handle, "mcastaddr", &str)) {
  241. res = totemip_parse (&totem_config->interfaces[ringnumber].mcast_addr, str, 0);
  242. }
  243. /*
  244. * Get mcast port
  245. */
  246. if (!objdb_get_string (objdb, object_interface_handle, "mcastport", &str)) {
  247. totem_config->interfaces[ringnumber].ip_port = atoi (str);
  248. }
  249. /*
  250. * Get the bind net address
  251. */
  252. if (!objdb_get_string (objdb, object_interface_handle, "bindnetaddr", &str)) {
  253. res = totemip_parse (&totem_config->interfaces[ringnumber].bindnet, str,
  254. totem_config->interfaces[ringnumber].mcast_addr.family);
  255. }
  256. totem_config->interface_count++;
  257. }
  258. objdb->object_find_destroy (object_find_interface_handle);
  259. add_totem_config_notification(objdb, totem_config, object_totem_handle);
  260. return 0;
  261. }
  262. int totem_config_validate (
  263. struct totem_config *totem_config,
  264. const char **error_string)
  265. {
  266. static char local_error_reason[512];
  267. char parse_error[512];
  268. char *error_reason = local_error_reason;
  269. int i;
  270. unsigned int interface_max = INTERFACE_MAX;
  271. if (totem_config->interface_count == 0) {
  272. error_reason = "No interfaces defined";
  273. goto parse_error;
  274. }
  275. for (i = 0; i < totem_config->interface_count; i++) {
  276. /*
  277. * Some error checking of parsed data to make sure its valid
  278. */
  279. if ((int *)&totem_config->interfaces[i].mcast_addr.addr == 0) {
  280. error_reason = "No multicast address specified";
  281. goto parse_error;
  282. }
  283. if (totem_config->interfaces[i].ip_port == 0) {
  284. error_reason = "No multicast port specified";
  285. goto parse_error;
  286. }
  287. if (totem_config->interfaces[i].mcast_addr.family == AF_INET6 &&
  288. totem_config->node_id == 0) {
  289. error_reason = "An IPV6 network requires that a node ID be specified.";
  290. goto parse_error;
  291. }
  292. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  293. error_reason = "Multicast address family does not match bind address family";
  294. goto parse_error;
  295. }
  296. if (totem_config->interfaces[i].mcast_addr.family != totem_config->interfaces[i].bindnet.family) {
  297. error_reason = "Not all bind address belong to the same IP family";
  298. goto parse_error;
  299. }
  300. }
  301. if (totem_config->version != 2) {
  302. error_reason = "This totem parser can only parse version 2 configurations.";
  303. goto parse_error;
  304. }
  305. if (totem_config->token_retransmits_before_loss_const == 0) {
  306. totem_config->token_retransmits_before_loss_const =
  307. TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  308. }
  309. /*
  310. * Setup timeout values that are not setup by user
  311. */
  312. if (totem_config->token_timeout == 0) {
  313. totem_config->token_timeout = TOKEN_TIMEOUT;
  314. if (totem_config->token_retransmits_before_loss_const == 0) {
  315. totem_config->token_retransmits_before_loss_const = TOKEN_RETRANSMITS_BEFORE_LOSS_CONST;
  316. }
  317. if (totem_config->token_retransmit_timeout == 0) {
  318. totem_config->token_retransmit_timeout =
  319. (int)(totem_config->token_timeout /
  320. (totem_config->token_retransmits_before_loss_const + 0.2));
  321. }
  322. if (totem_config->token_hold_timeout == 0) {
  323. totem_config->token_hold_timeout =
  324. (int)(totem_config->token_retransmit_timeout * 0.8 -
  325. (1000/HZ));
  326. }
  327. }
  328. if (totem_config->max_network_delay == 0) {
  329. totem_config->max_network_delay = MAX_NETWORK_DELAY;
  330. }
  331. if (totem_config->max_network_delay < MINIMUM_TIMEOUT) {
  332. sprintf (local_error_reason, "The max_network_delay parameter (%d ms) may not be less then (%d ms).",
  333. totem_config->max_network_delay, MINIMUM_TIMEOUT);
  334. goto parse_error;
  335. }
  336. if (totem_config->window_size == 0) {
  337. totem_config->window_size = WINDOW_SIZE;
  338. }
  339. if (totem_config->max_messages == 0) {
  340. totem_config->max_messages = MAX_MESSAGES;
  341. }
  342. if (totem_config->token_timeout < MINIMUM_TIMEOUT) {
  343. sprintf (local_error_reason, "The token timeout parameter (%d ms) may not be less then (%d ms).",
  344. totem_config->token_timeout, MINIMUM_TIMEOUT);
  345. goto parse_error;
  346. }
  347. if (totem_config->token_retransmit_timeout == 0) {
  348. totem_config->token_retransmit_timeout =
  349. (int)(totem_config->token_timeout /
  350. (totem_config->token_retransmits_before_loss_const + 0.2));
  351. }
  352. if (totem_config->token_hold_timeout == 0) {
  353. totem_config->token_hold_timeout =
  354. (int)(totem_config->token_retransmit_timeout * 0.8 -
  355. (1000/HZ));
  356. }
  357. if (totem_config->token_retransmit_timeout < MINIMUM_TIMEOUT) {
  358. sprintf (local_error_reason, "The token retransmit timeout parameter (%d ms) may not be less then (%d ms).",
  359. totem_config->token_retransmit_timeout, MINIMUM_TIMEOUT);
  360. goto parse_error;
  361. }
  362. if (totem_config->token_hold_timeout == 0) {
  363. totem_config->token_hold_timeout = TOKEN_HOLD_TIMEOUT;
  364. }
  365. if (totem_config->token_hold_timeout < MINIMUM_TIMEOUT) {
  366. sprintf (local_error_reason, "The token hold timeout parameter (%d ms) may not be less then (%d ms).",
  367. totem_config->token_hold_timeout, MINIMUM_TIMEOUT);
  368. goto parse_error;
  369. }
  370. if (totem_config->join_timeout == 0) {
  371. totem_config->join_timeout = JOIN_TIMEOUT;
  372. }
  373. if (totem_config->join_timeout < MINIMUM_TIMEOUT) {
  374. sprintf (local_error_reason, "The join timeout parameter (%d ms) may not be less then (%d ms).",
  375. totem_config->join_timeout, MINIMUM_TIMEOUT);
  376. goto parse_error;
  377. }
  378. if (totem_config->consensus_timeout == 0) {
  379. totem_config->consensus_timeout = CONSENSUS_TIMEOUT;
  380. }
  381. if (totem_config->consensus_timeout < MINIMUM_TIMEOUT) {
  382. sprintf (local_error_reason, "The consensus timeout parameter (%d ms) may not be less then (%d ms).",
  383. totem_config->consensus_timeout, MINIMUM_TIMEOUT);
  384. goto parse_error;
  385. }
  386. if (totem_config->merge_timeout == 0) {
  387. totem_config->merge_timeout = MERGE_TIMEOUT;
  388. }
  389. if (totem_config->merge_timeout < MINIMUM_TIMEOUT) {
  390. sprintf (local_error_reason, "The merge timeout parameter (%d ms) may not be less then (%d ms).",
  391. totem_config->merge_timeout, MINIMUM_TIMEOUT);
  392. goto parse_error;
  393. }
  394. if (totem_config->downcheck_timeout == 0) {
  395. totem_config->downcheck_timeout = DOWNCHECK_TIMEOUT;
  396. }
  397. if (totem_config->downcheck_timeout < MINIMUM_TIMEOUT) {
  398. sprintf (local_error_reason, "The downcheck timeout parameter (%d ms) may not be less then (%d ms).",
  399. totem_config->downcheck_timeout, MINIMUM_TIMEOUT);
  400. goto parse_error;
  401. }
  402. /*
  403. * RRP values validation
  404. */
  405. if (strcmp (totem_config->rrp_mode, "none") &&
  406. strcmp (totem_config->rrp_mode, "active") &&
  407. strcmp (totem_config->rrp_mode, "passive")) {
  408. sprintf (local_error_reason, "The RRP mode \"%s\" specified is invalid. It must be none, active, or passive.\n", totem_config->rrp_mode);
  409. goto parse_error;
  410. }
  411. if (totem_config->rrp_problem_count_timeout == 0) {
  412. totem_config->rrp_problem_count_timeout = RRP_PROBLEM_COUNT_TIMEOUT;
  413. }
  414. if (totem_config->rrp_problem_count_timeout < MINIMUM_TIMEOUT) {
  415. sprintf (local_error_reason, "The RRP problem count timeout parameter (%d ms) may not be less then (%d ms).",
  416. totem_config->rrp_problem_count_timeout, MINIMUM_TIMEOUT);
  417. goto parse_error;
  418. }
  419. if (totem_config->rrp_problem_count_threshold == 0) {
  420. totem_config->rrp_problem_count_threshold = RRP_PROBLEM_COUNT_THRESHOLD_DEFAULT;
  421. }
  422. if (totem_config->rrp_problem_count_threshold < RRP_PROBLEM_COUNT_THRESHOLD_MIN) {
  423. sprintf (local_error_reason, "The RRP problem count threshold (%d problem count) may not be less then (%d problem count).",
  424. totem_config->rrp_problem_count_threshold, RRP_PROBLEM_COUNT_THRESHOLD_MIN);
  425. goto parse_error;
  426. }
  427. if (totem_config->rrp_token_expired_timeout == 0) {
  428. totem_config->rrp_token_expired_timeout =
  429. totem_config->token_retransmit_timeout;
  430. }
  431. if (totem_config->rrp_token_expired_timeout < MINIMUM_TIMEOUT) {
  432. sprintf (local_error_reason, "The RRP token expired timeout parameter (%d ms) may not be less then (%d ms).",
  433. totem_config->rrp_token_expired_timeout, MINIMUM_TIMEOUT);
  434. goto parse_error;
  435. }
  436. if (strcmp (totem_config->rrp_mode, "none") == 0) {
  437. interface_max = 1;
  438. }
  439. if (interface_max < totem_config->interface_count) {
  440. sprintf (parse_error,
  441. "%d is too many configured interfaces for the rrp_mode setting %s.",
  442. totem_config->interface_count,
  443. totem_config->rrp_mode);
  444. error_reason = parse_error;
  445. goto parse_error;
  446. }
  447. if (totem_config->fail_to_recv_const == 0) {
  448. totem_config->fail_to_recv_const = FAIL_TO_RECV_CONST;
  449. }
  450. if (totem_config->seqno_unchanged_const == 0) {
  451. totem_config->seqno_unchanged_const = SEQNO_UNCHANGED_CONST;
  452. }
  453. if (totem_config->net_mtu == 0) {
  454. totem_config->net_mtu = 1500;
  455. }
  456. if ((MESSAGE_QUEUE_MAX) < totem_config->max_messages) {
  457. sprintf (local_error_reason, "The max_messages parameter (%d messages) may not be greater then (%d messages).",
  458. totem_config->max_messages, MESSAGE_QUEUE_MAX);
  459. goto parse_error;
  460. }
  461. if (totem_config->threads > SEND_THREADS_MAX) {
  462. totem_config->threads = SEND_THREADS_MAX;
  463. }
  464. if (totem_config->secauth == 0) {
  465. totem_config->threads = 0;
  466. }
  467. if (totem_config->net_mtu > FRAME_SIZE_MAX) {
  468. error_reason = "This net_mtu parameter is greater then the maximum frame size";
  469. goto parse_error;
  470. }
  471. if (totem_config->vsf_type == NULL) {
  472. totem_config->vsf_type = "none";
  473. }
  474. return (0);
  475. parse_error:
  476. sprintf (error_string_response,
  477. "parse error in config: %s\n", error_reason);
  478. *error_string = error_string_response;
  479. return (-1);
  480. }
  481. static int read_keyfile (
  482. char *key_location,
  483. struct totem_config *totem_config,
  484. const char **error_string)
  485. {
  486. int fd;
  487. int res;
  488. fd = open (key_location, O_RDONLY);
  489. if (fd == -1) {
  490. sprintf (error_string_response, "Could not open %s: %s\n",
  491. key_location, strerror (errno));
  492. goto parse_error;
  493. }
  494. res = read (fd, totem_config->private_key, 128);
  495. if (res == -1) {
  496. close (fd);
  497. sprintf (error_string_response, "Could not read %s: %s\n",
  498. key_location, strerror (errno));
  499. goto parse_error;
  500. }
  501. totem_config->private_key_len = 128;
  502. if (res != 128) {
  503. close (fd);
  504. sprintf (error_string_response, "Could only read %d bits of 1024 bits from %s.\n",
  505. res * 8, key_location);
  506. goto parse_error;
  507. }
  508. return 0;
  509. parse_error:
  510. *error_string = error_string_response;
  511. return (-1);
  512. }
  513. int totem_config_keyread (
  514. struct objdb_iface_ver0 *objdb,
  515. struct totem_config *totem_config,
  516. const char **error_string)
  517. {
  518. int got_key = 0;
  519. char *key_location = NULL;
  520. hdb_handle_t object_totem_handle;
  521. int res;
  522. memset (totem_config->private_key, 0, 128);
  523. totem_config->private_key_len = 128;
  524. if (totem_config->secauth == 0) {
  525. return (0);
  526. }
  527. res = totem_handle_find (objdb, &object_totem_handle);
  528. if (res == -1) {
  529. return (-1);
  530. }
  531. /* objdb may store the location of the key file */
  532. if (!objdb_get_string (objdb,object_totem_handle, "keyfile", &key_location)
  533. && key_location) {
  534. res = read_keyfile(key_location, totem_config, error_string);
  535. if (res) {
  536. goto key_error;
  537. }
  538. got_key = 1;
  539. } else { /* Or the key itself may be in the objdb */
  540. char *key = NULL;
  541. int key_len;
  542. res = objdb->object_key_get (object_totem_handle,
  543. "key",
  544. strlen ("key"),
  545. (void *)&key,
  546. &key_len);
  547. if (res == 0 && key) {
  548. memcpy(totem_config->private_key, key, key_len);
  549. totem_config->private_key_len = key_len;
  550. got_key = 1;
  551. }
  552. }
  553. /* In desperation we read the default filename */
  554. if (!got_key) {
  555. char *filename = getenv("COROSYNC_TOTEM_AUTHKEY_FILE");
  556. if (!filename)
  557. filename = SYSCONFDIR "/ais/authkey";
  558. res = read_keyfile(filename, totem_config, error_string);
  559. if (res)
  560. goto key_error;
  561. }
  562. return (0);
  563. *error_string = error_string_response;
  564. key_error:
  565. return (-1);
  566. }
  567. static void totem_key_change_notify(object_change_type_t change_type,
  568. hdb_handle_t parent_object_handle,
  569. hdb_handle_t object_handle,
  570. void *object_name_pt, int object_name_len,
  571. void *key_name_pt, int key_len,
  572. void *key_value_pt, int key_value_len,
  573. void *priv_data_pt)
  574. {
  575. struct totem_config *totem_config = priv_data_pt;
  576. if (memcmp(object_name_pt, "totem", object_name_len) == 0)
  577. totem_volatile_config_read(global_objdb,
  578. totem_config,
  579. object_handle); // CHECK
  580. }
  581. static void totem_objdb_reload_notify(objdb_reload_notify_type_t type, int flush,
  582. void *priv_data_pt)
  583. {
  584. struct totem_config *totem_config = priv_data_pt;
  585. hdb_handle_t totem_object_handle;
  586. /*
  587. * A new totem {} key might exist, cancel the
  588. * existing notification at the start of reload,
  589. * and start a new one on the new object when
  590. * it's all settled.
  591. */
  592. if (type == OBJDB_RELOAD_NOTIFY_START) {
  593. global_objdb->object_track_stop(
  594. totem_key_change_notify,
  595. NULL,
  596. NULL,
  597. NULL,
  598. NULL);
  599. }
  600. if (type == OBJDB_RELOAD_NOTIFY_END ||
  601. type == OBJDB_RELOAD_NOTIFY_FAILED) {
  602. if (!totem_handle_find(global_objdb,
  603. &totem_object_handle)) {
  604. add_totem_config_notification(global_objdb, totem_config, totem_object_handle);
  605. /*
  606. * Reload the configuration
  607. */
  608. totem_volatile_config_read(global_objdb,
  609. totem_config,
  610. totem_object_handle);
  611. }
  612. else {
  613. log_printf(LOG_LEVEL_ERROR, "totem objdb tracking stopped, cannot find totem{} handle on objdb\n");
  614. }
  615. }
  616. }
  617. static void add_totem_config_notification(
  618. struct objdb_iface_ver0 *objdb,
  619. struct totem_config *totem_config,
  620. hdb_handle_t totem_object_handle)
  621. {
  622. global_objdb = objdb;
  623. objdb->object_track_start(totem_object_handle,
  624. 1,
  625. totem_key_change_notify,
  626. NULL, // object_create_notify,
  627. NULL, // object_destroy_notify,
  628. NULL, // object_reload_notify
  629. totem_config); // priv_data
  630. /*
  631. * Reload notify must be on the parent object
  632. */
  633. objdb->object_track_start(OBJECT_PARENT_HANDLE,
  634. 1,
  635. NULL, // key_change_notify,
  636. NULL, // object_create_notify,
  637. NULL, // object_destroy_notify,
  638. totem_objdb_reload_notify, // object_reload_notify
  639. totem_config); // priv_data
  640. }