amfconfig.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * Copyright (c) 2002-2005 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 <stdio.h>
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #include <errno.h>
  38. #include <assert.h>
  39. #include <sys/socket.h>
  40. #include <netinet/in.h>
  41. #include <arpa/inet.h>
  42. #include "../include/saAis.h"
  43. #include "../include/saAmf.h"
  44. #include "../include/ipc_amf.h"
  45. #include "../include/list.h"
  46. #include "util.h"
  47. #include "amfconfig.h"
  48. #include "mainconfig.h"
  49. #include "mempool.h"
  50. #include "print.h"
  51. #include "totem.h"
  52. DECLARE_LIST_INIT (amf_groupHead);
  53. DECLARE_LIST_INIT (amf_healthcheck_head);
  54. static char error_string_response[512];
  55. typedef enum {
  56. AMF_HEAD,
  57. AMF_GROUP,
  58. AMF_UNIT,
  59. AMF_COMPONENT,
  60. AMF_SERVICEINSTANCE,
  61. AMF_HEALTHCHECK
  62. } amf_parse_t;
  63. typedef enum {
  64. MAIN_HEAD,
  65. MAIN_NETWORK,
  66. MAIN_LOGGING,
  67. MAIN_KEY,
  68. MAIN_TIMEOUT,
  69. MAIN_EVENT
  70. } main_parse_t;
  71. void setSaNameT (SaNameT *name, char *str) {
  72. strncpy ((char *)name->value, str, SA_MAX_NAME_LENGTH);
  73. if (strlen ((char *)name->value) > SA_MAX_NAME_LENGTH) {
  74. name->length = SA_MAX_NAME_LENGTH;
  75. } else {
  76. name->length = strlen (str);
  77. }
  78. }
  79. int SaNameTisEqual (SaNameT *str1, char *str2) {
  80. if (str1->length == strlen (str2)) {
  81. return ((strncmp ((char *)str1->value, (char *)str2,
  82. str1->length)) == 0);
  83. } else {
  84. return 0;
  85. }
  86. }
  87. struct amf_healthcheck *find_healthcheck (SaAmfHealthcheckKeyT *key)
  88. {
  89. struct amf_healthcheck *healthcheck;
  90. struct amf_healthcheck *ret_healthcheck = 0;
  91. struct list_head *list;
  92. for (list = amf_healthcheck_head.next;
  93. list != &amf_healthcheck_head;
  94. list = list->next) {
  95. healthcheck = list_entry (list,
  96. struct amf_healthcheck, list);
  97. if (memcmp (key, &healthcheck->key, sizeof (SaAmfHealthcheckKeyT)) == 0) {
  98. ret_healthcheck = healthcheck;
  99. break;
  100. }
  101. }
  102. return (ret_healthcheck);
  103. }
  104. struct amf_comp *find_comp (SaNameT *name)
  105. {
  106. struct list_head *list_group = 0;
  107. struct list_head *list_unit = 0;
  108. struct list_head *AmfComponentList = 0;
  109. struct amf_group *amf_group = 0;
  110. struct amf_unit *amf_unit = 0;
  111. struct amf_comp *AmfComponent = 0;
  112. int found = 0;
  113. /*
  114. * Search all groups
  115. */
  116. for (list_group = amf_groupHead.next;
  117. list_group != &amf_groupHead && found == 0;
  118. list_group = list_group->next) {
  119. amf_group = list_entry (list_group,
  120. struct amf_group, group_list);
  121. /*
  122. * Search all units
  123. */
  124. for (list_unit = amf_group->unit_head.next;
  125. list_unit != &amf_group->unit_head && found == 0;
  126. list_unit = list_unit->next) {
  127. amf_unit = list_entry (list_unit,
  128. struct amf_unit, unit_list);
  129. /*
  130. * Search all components
  131. */
  132. for (AmfComponentList = amf_unit->comp_head.next;
  133. AmfComponentList != &amf_unit->comp_head && found == 0;
  134. AmfComponentList = AmfComponentList->next) {
  135. AmfComponent = list_entry (AmfComponentList,
  136. struct amf_comp, comp_list);
  137. if (name_match (name, &AmfComponent->name)) {
  138. found = 1;
  139. }
  140. }
  141. }
  142. }
  143. if (found) {
  144. return (AmfComponent);
  145. } else {
  146. return (0);
  147. }
  148. }
  149. struct amf_unit *find_unit (SaNameT *name)
  150. {
  151. struct list_head *list_group = 0;
  152. struct list_head *list_unit = 0;
  153. struct amf_group *amf_group = 0;
  154. struct amf_unit *amf_unit = 0;
  155. int found = 0;
  156. /*
  157. * Search all groups
  158. */
  159. for (list_group = amf_groupHead.next;
  160. list_group != &amf_groupHead && found == 0;
  161. list_group = list_group->next) {
  162. amf_group = list_entry (list_group,
  163. struct amf_group, group_list);
  164. /*
  165. * Search all units
  166. */
  167. for (list_unit = amf_group->unit_head.next;
  168. list_unit != &amf_group->unit_head && found == 0;
  169. list_unit = list_unit->next) {
  170. amf_unit = list_entry (list_unit,
  171. struct amf_unit, unit_list);
  172. if (name_match (name, &amf_unit->name)) {
  173. found = 1;
  174. }
  175. }
  176. }
  177. if (found) {
  178. return (amf_unit);
  179. } else {
  180. return (0);
  181. }
  182. }
  183. extern int openais_amf_config_read (char **error_string)
  184. {
  185. char line[255];
  186. FILE *fp;
  187. amf_parse_t current_parse = AMF_HEAD;
  188. int line_number = 0;
  189. char *loc;
  190. int i;
  191. struct amf_group *amf_group = 0;
  192. struct amf_unit *amf_unit = 0;
  193. struct amf_comp *amf_comp = 0;
  194. struct amf_si *amf_si = 0;
  195. struct amf_healthcheck *amf_healthcheck = 0;
  196. fp = fopen (OPENAIS_CONFDIR "/groups.conf", "r");
  197. if (fp == 0) {
  198. sprintf (error_string_response,
  199. "Can't read %s/groups.conf file reason = (%s).\n",
  200. OPENAIS_CONFDIR, strerror (errno));
  201. *error_string = error_string_response;
  202. return (-1);
  203. }
  204. while (fgets (line, 255, fp)) {
  205. line_number += 1;
  206. line[strlen(line) - 1] = '\0';
  207. /*
  208. * Clear out comments and empty lines
  209. */
  210. if (line[0] == '#' || line[0] == '\0' || line[0] == '\n') {
  211. continue;
  212. }
  213. /*
  214. * Clear out white space and tabs
  215. */
  216. for (i = strlen (line) - 1; i > -1; i--) {
  217. if (line[i] == '\t' || line[i] == ' ') {
  218. line[i] = '\0';
  219. } else {
  220. break;
  221. }
  222. }
  223. switch (current_parse) {
  224. case AMF_HEAD:
  225. if (strstr_rs (line, "group{")) {
  226. amf_group = (struct amf_group *)mempool_malloc (sizeof (struct amf_group));
  227. memset (amf_group, 0, sizeof (struct amf_group));
  228. list_init (&amf_group->group_list);
  229. list_init (&amf_group->unit_head);
  230. list_init (&amf_group->si_head);
  231. list_add (&amf_group->group_list, &amf_groupHead);
  232. memset (amf_group->clccli_path, 0, sizeof (&amf_unit->clccli_path));
  233. memset (amf_group->binary_path, 0, sizeof (&amf_unit->binary_path));
  234. current_parse = AMF_GROUP;
  235. } else
  236. if (strstr_rs (line, "healthcheck{")) {
  237. amf_healthcheck = (struct amf_healthcheck *)mempool_malloc (sizeof (struct amf_healthcheck));
  238. memset (amf_healthcheck, 0, sizeof (struct amf_healthcheck));
  239. list_init (&amf_healthcheck->list);
  240. list_add_tail (&amf_healthcheck->list,
  241. &amf_healthcheck_head);
  242. current_parse = AMF_HEALTHCHECK;
  243. } else {
  244. goto parse_error;
  245. }
  246. break;
  247. case AMF_GROUP:
  248. if ((loc = strstr_rs (line, "name=")) != 0) {
  249. setSaNameT (&amf_group->name, loc);
  250. } else
  251. if ((loc = strstr_rs (line, "model=")) != 0) {
  252. if (strcmp (loc, "2n") == 0) {
  253. amf_group->model = GROUPCAPABILITYMODEL_2N;
  254. } else
  255. if (strcmp (loc, "nplusm") == 0) {
  256. amf_group->model = GROUPCAPABILITYMODEL_NPLUSM;
  257. } else
  258. if (strcmp (loc, "nway") == 0) {
  259. printf ("nway redundancy model not supported.\n");
  260. goto parse_error;
  261. } else
  262. if (strcmp (loc, "nwayactive") == 0) {
  263. printf ("nway active redundancy model not supported.\n");
  264. goto parse_error;
  265. } else
  266. if (strcmp (loc, "noredundancy") == 0) {
  267. amf_group->model = GROUPCAPABILITYMODEL_NOREDUNDANCY;
  268. } else {
  269. goto parse_error;
  270. }
  271. } else
  272. if ((loc = strstr_rs (line, "preferred-active-units=")) != 0) {
  273. amf_group->preferred_active_units = atoi (loc);
  274. } else
  275. if ((loc = strstr_rs (line, "preferred-standby-units=")) != 0) {
  276. amf_group->preferred_standby_units = atoi (loc);
  277. } else
  278. if ((loc = strstr_rs (line, "maximum-active-instances=")) != 0) {
  279. amf_group->maximum_active_instances = atoi (loc);
  280. } else
  281. if ((loc = strstr_rs (line, "maximum-standby-instances=")) != 0) {
  282. amf_group->maximum_standby_instances = atoi (loc);
  283. } else
  284. if ((loc = strstr_rs (line, "clccli_path=")) != 0) {
  285. strcpy (amf_group->clccli_path, loc);
  286. } else
  287. if ((loc = strstr_rs (line, "binary_path=")) != 0) {
  288. strcpy (amf_group->binary_path, loc);
  289. } else
  290. if ((loc = strstr_rs (line, "component_restart_probation=")) != 0) {
  291. amf_group->component_restart_probation = atoi (loc);
  292. printf ("restart probation %d\n", amf_group->component_restart_probation);
  293. } else
  294. if ((loc = strstr_rs (line, "component_restart_max=")) != 0) {
  295. amf_group->component_restart_max = atoi (loc);
  296. printf ("restart max %d\n", amf_group->component_restart_max);
  297. } else
  298. if ((loc = strstr_rs (line, "unit_restart_probation=")) != 0) {
  299. amf_group->unit_restart_probation = atoi (loc);
  300. printf ("unit restart probation %d\n", amf_group->unit_restart_probation);
  301. } else
  302. if ((loc = strstr_rs (line, "unit_restart_max=")) != 0) {
  303. amf_group->unit_restart_max = atoi (loc);
  304. printf ("unit restart max %d\n", amf_group->unit_restart_max);
  305. } else
  306. if (strstr_rs (line, "unit{")) {
  307. amf_unit = (struct amf_unit *)mempool_malloc (sizeof (struct amf_unit));
  308. memset (amf_unit, 0, sizeof (struct amf_unit));
  309. amf_unit->amf_group = amf_group;
  310. amf_unit->operational_state = OPENAIS_CFG_OPERATIONALSTATE_DISABLED;
  311. amf_unit->presence_state = OPENAIS_CFG_PRESENCESTATE_UNINSTANTIATED;
  312. list_init (&amf_unit->comp_head);
  313. list_init (&amf_unit->si_head);
  314. amf_unit->escalation_level = ESCALATION_LEVEL_NO_ESCALATION;
  315. amf_unit->restart_count = 0;
  316. list_add_tail (&amf_unit->unit_list, &amf_group->unit_head);
  317. memset (amf_unit->clccli_path, 0, sizeof (&amf_unit->clccli_path));
  318. memset (amf_unit->binary_path, 0, sizeof (&amf_unit->binary_path));
  319. current_parse = AMF_UNIT;
  320. } else
  321. if (strstr_rs (line, "serviceinstance{")) {
  322. amf_si = (struct amf_si *)mempool_malloc (sizeof (struct amf_si));
  323. memset (amf_si, 0, sizeof (struct amf_si));
  324. list_init (&amf_si->csi_head);
  325. list_init (&amf_si->unit_list);
  326. list_init (&amf_si->pg_head);
  327. list_add_tail (&amf_si->list, &amf_group->si_head);
  328. amf_si->group = amf_group;
  329. current_parse = AMF_SERVICEINSTANCE;
  330. } else
  331. if (strstr_rs (line, "}")) {
  332. current_parse = AMF_HEAD;
  333. } else {
  334. goto parse_error;
  335. }
  336. break;
  337. case AMF_UNIT:
  338. if ((loc = strstr_rs (line, "name=")) != 0) {
  339. setSaNameT (&amf_unit->name, loc);
  340. } else
  341. if ((loc = strstr_rs (line, "component{")) != 0) {
  342. amf_comp = (struct amf_comp *)mempool_malloc (sizeof (struct amf_comp));
  343. memset (amf_comp, 0, sizeof (struct amf_comp));
  344. amf_comp->unit = amf_unit;
  345. amf_comp->operational_state = OPENAIS_CFG_OPERATIONALSTATE_DISABLED;
  346. amf_comp->presence_state = OPENAIS_CFG_PRESENCESTATE_UNINSTANTIATED;
  347. list_init (&amf_comp->comp_list);
  348. list_init (&amf_comp->healthcheck_list);
  349. list_add_tail (&amf_comp->comp_list, &amf_unit->comp_head);
  350. memset (amf_comp->clccli_path, 0, sizeof (&amf_comp->clccli_path));
  351. memset (amf_comp->binary_path, 0, sizeof (&amf_unit->binary_path));
  352. memset (amf_comp->binary_name, 0, sizeof (&amf_comp->binary_name));
  353. current_parse = AMF_COMPONENT;
  354. } else
  355. if ((loc = strstr_rs (line, "clccli_path=")) != 0) {
  356. strcpy (amf_unit->clccli_path, loc);
  357. } else
  358. if ((loc = strstr_rs (line, "binary_path=")) != 0) {
  359. strcpy (amf_unit->binary_path, loc);
  360. } else
  361. if (strstr_rs (line, "}")) {
  362. current_parse = AMF_GROUP;
  363. } else {
  364. goto parse_error;
  365. }
  366. break;
  367. case AMF_COMPONENT:
  368. if ((loc = strstr_rs (line, "name=")) != 0) {
  369. setSaNameT (&amf_comp->name, loc);
  370. } else
  371. #ifdef COMPILE_OUT
  372. if ((loc = strstr_rs (line, "model=")) != 0) {
  373. if (strcmp (loc, "x_active_and_y_standby") == 0) {
  374. amf_comp->componentCapabilityModel = SA_AMF_COMPONENT_CAPABILITY_X_ACTIVE_AND_Y_STANDBY;
  375. } else
  376. if (strcmp (loc, "x_active_or_y_standby") == 0) {
  377. amf_comp->componentCapabilityModel = SA_AMF_COMPONENT_CAPABILITY_X_ACTIVE_OR_Y_STANDBY;
  378. } else
  379. if (strcmp (loc, "1_active_or_y_standby") == 0) {
  380. amf_comp->componentCapabilityModel = SA_AMF_COMPONENT_CAPABILITY_1_ACTIVE_OR_Y_STANDBY;
  381. } else
  382. if (strcmp (loc, "1_active_or_1_standby") == 0) {
  383. amf_comp->componentCapabilityModel = SA_AMF_COMPONENT_CAPABILITY_1_ACTIVE_OR_1_STANDBY;
  384. } else
  385. if (strcmp (loc, "x_active") == 0) {
  386. amf_comp->componentCapabilityModel = SA_AMF_COMPONENT_CAPABILITY_X_ACTIVE;
  387. } else
  388. if (strcmp (loc, "1_active") == 0) {
  389. amf_comp->componentCapabilityModel = SA_AMF_COMPONENT_CAPABILITY_1_ACTIVE;
  390. } else
  391. if (strcmp (loc, "no_active") == 0) {
  392. amf_comp->componentCapabilityModel = SA_AMF_COMPONENT_CAPABILITY_NO_ACTIVE;
  393. } else {
  394. goto parse_error;
  395. }
  396. } else
  397. #endif
  398. if ((loc = strstr_rs(line, "comptype=")) != 0) {
  399. if (strstr (line, "sa_aware")) {
  400. amf_comp->comptype = clc_component_sa_aware;
  401. } else
  402. if (strstr (line, "proxied_pre")) {
  403. amf_comp->comptype = clc_component_proxied_pre;
  404. } else
  405. if (strstr (line, "proxied_non_pre")) {
  406. amf_comp->comptype = clc_component_proxied_non_pre;
  407. } else
  408. if (strstr (line, "non_proxied_non_sa_aware")) {
  409. amf_comp->comptype = clc_component_proxied_non_pre;
  410. } else {
  411. goto parse_error;
  412. }
  413. } else
  414. if ((loc = strstr_rs(line, "instantiate=")) != 0) {
  415. strcpy (amf_comp->instantiate_cmd, loc);
  416. } else
  417. if ((loc = strstr_rs(line, "terminate=")) != 0) {
  418. strcpy (amf_comp->terminate_cmd, loc);
  419. } else
  420. if ((loc = strstr_rs(line, "cleanup=")) != 0) {
  421. strcpy (amf_comp->cleanup_cmd, loc);
  422. } else
  423. if ((loc = strstr_rs(line, "am_start=")) != 0) {
  424. strcpy (amf_comp->am_start_cmd, loc);
  425. } else
  426. if ((loc = strstr_rs(line, "am_stop=")) != 0) {
  427. strcpy (amf_comp->am_stop_cmd, loc);
  428. } else
  429. if ((loc = strstr_rs (line, "clccli_path=")) != 0) {
  430. strcpy (amf_comp->clccli_path, loc);
  431. } else
  432. if ((loc = strstr_rs (line, "binary_path=")) != 0) {
  433. strcpy (amf_comp->binary_path, loc);
  434. } else
  435. if ((loc = strstr_rs (line, "bn=")) != 0) {
  436. strcpy (amf_comp->binary_name, loc);
  437. } else
  438. if (strstr_rs (line, "}")) {
  439. current_parse = AMF_UNIT;
  440. } else {
  441. goto parse_error;
  442. }
  443. break;
  444. case AMF_SERVICEINSTANCE:
  445. if ((loc = strstr_rs (line, "name=")) != 0) {
  446. setSaNameT (&amf_si->name, loc);
  447. } else
  448. if (strstr_rs (line, "}")) {
  449. current_parse = AMF_GROUP;
  450. } else {
  451. goto parse_error;
  452. }
  453. break;
  454. case AMF_HEALTHCHECK:
  455. if ((loc = strstr_rs (line, "key=")) != 0) {
  456. strcpy (amf_healthcheck->key.key, loc);
  457. amf_healthcheck->key.keyLen = strlen (loc);
  458. } else
  459. if ((loc = strstr_rs (line, "period=")) != 0) {
  460. amf_healthcheck->period = atoi (loc);
  461. } else
  462. if ((loc = strstr_rs (line, "maximum_duration=")) != 0) {
  463. amf_healthcheck->maximum_duration = atoi (loc);
  464. } else
  465. if (strstr_rs (line, "}")) {
  466. current_parse = AMF_HEAD;
  467. } else {
  468. goto parse_error;
  469. }
  470. break;
  471. default:
  472. printf ("Invalid state\n");
  473. goto parse_error;
  474. break;
  475. }
  476. }
  477. fclose (fp);
  478. return (0);
  479. parse_error:
  480. sprintf (error_string_response,
  481. "parse error at %s/groups.conf:%d.\n", OPENAIS_CONFDIR, line_number);
  482. *error_string = error_string_response;
  483. fclose (fp);
  484. return (-1);
  485. }