amfconfig.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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 ("/etc/ais/groups.conf", "r");
  197. if (fp == 0) {
  198. sprintf (error_string_response,
  199. "Can't read /etc/ais/groups.conf file reason = (%s).\n", strerror (errno));
  200. *error_string = error_string_response;
  201. return (-1);
  202. }
  203. while (fgets (line, 255, fp)) {
  204. line_number += 1;
  205. line[strlen(line) - 1] = '\0';
  206. /*
  207. * Clear out comments and empty lines
  208. */
  209. if (line[0] == '#' || line[0] == '\0' || line[0] == '\n') {
  210. continue;
  211. }
  212. /*
  213. * Clear out white space and tabs
  214. */
  215. for (i = strlen (line) - 1; i > -1; i--) {
  216. if (line[i] == '\t' || line[i] == ' ') {
  217. line[i] = '\0';
  218. } else {
  219. break;
  220. }
  221. }
  222. switch (current_parse) {
  223. case AMF_HEAD:
  224. if (strstr_rs (line, "group{")) {
  225. amf_group = (struct amf_group *)mempool_malloc (sizeof (struct amf_group));
  226. memset (amf_group, 0, sizeof (struct amf_group));
  227. list_init (&amf_group->group_list);
  228. list_init (&amf_group->unit_head);
  229. list_init (&amf_group->si_head);
  230. list_add (&amf_group->group_list, &amf_groupHead);
  231. memset (amf_group->clccli_path, 0, sizeof (&amf_unit->clccli_path));
  232. memset (amf_group->binary_path, 0, sizeof (&amf_unit->binary_path));
  233. current_parse = AMF_GROUP;
  234. } else
  235. if (strstr_rs (line, "healthcheck{")) {
  236. amf_healthcheck = (struct amf_healthcheck *)mempool_malloc (sizeof (struct amf_healthcheck));
  237. memset (amf_healthcheck, 0, sizeof (struct amf_healthcheck));
  238. list_init (&amf_healthcheck->list);
  239. list_add_tail (&amf_healthcheck->list,
  240. &amf_healthcheck_head);
  241. current_parse = AMF_HEALTHCHECK;
  242. } else {
  243. goto parse_error;
  244. }
  245. break;
  246. case AMF_GROUP:
  247. if ((loc = strstr_rs (line, "name=")) != 0) {
  248. setSaNameT (&amf_group->name, loc);
  249. } else
  250. if ((loc = strstr_rs (line, "model=")) != 0) {
  251. if (strcmp (loc, "2n") == 0) {
  252. amf_group->model = GROUPCAPABILITYMODEL_2N;
  253. } else
  254. if (strcmp (loc, "nplusm") == 0) {
  255. amf_group->model = GROUPCAPABILITYMODEL_NPLUSM;
  256. } else
  257. if (strcmp (loc, "nway") == 0) {
  258. printf ("nway redundancy model not supported.\n");
  259. goto parse_error;
  260. } else
  261. if (strcmp (loc, "nwayactive") == 0) {
  262. printf ("nway active redundancy model not supported.\n");
  263. goto parse_error;
  264. } else
  265. if (strcmp (loc, "noredundancy") == 0) {
  266. amf_group->model = GROUPCAPABILITYMODEL_NOREDUNDANCY;
  267. } else {
  268. goto parse_error;
  269. }
  270. } else
  271. if ((loc = strstr_rs (line, "preferred-active-units=")) != 0) {
  272. amf_group->preferred_active_units = atoi (loc);
  273. } else
  274. if ((loc = strstr_rs (line, "preferred-standby-units=")) != 0) {
  275. amf_group->preferred_standby_units = atoi (loc);
  276. } else
  277. if ((loc = strstr_rs (line, "maximum-active-instances=")) != 0) {
  278. amf_group->maximum_active_instances = atoi (loc);
  279. } else
  280. if ((loc = strstr_rs (line, "maximum-standby-instances=")) != 0) {
  281. amf_group->maximum_standby_instances = atoi (loc);
  282. } else
  283. if ((loc = strstr_rs (line, "clccli_path=")) != 0) {
  284. strcpy (amf_group->clccli_path, loc);
  285. } else
  286. if ((loc = strstr_rs (line, "binary_path=")) != 0) {
  287. strcpy (amf_group->binary_path, loc);
  288. } else
  289. if ((loc = strstr_rs (line, "component_restart_probation=")) != 0) {
  290. amf_group->component_restart_probation = atoi (loc);
  291. printf ("restart probation %d\n", amf_group->component_restart_probation);
  292. } else
  293. if ((loc = strstr_rs (line, "component_restart_max=")) != 0) {
  294. amf_group->component_restart_max = atoi (loc);
  295. printf ("restart max %d\n", amf_group->component_restart_max);
  296. } else
  297. if ((loc = strstr_rs (line, "unit_restart_probation=")) != 0) {
  298. amf_group->unit_restart_probation = atoi (loc);
  299. printf ("unit restart probation %d\n", amf_group->unit_restart_probation);
  300. } else
  301. if ((loc = strstr_rs (line, "unit_restart_max=")) != 0) {
  302. amf_group->unit_restart_max = atoi (loc);
  303. printf ("unit restart max %d\n", amf_group->unit_restart_max);
  304. } else
  305. if (strstr_rs (line, "unit{")) {
  306. amf_unit = (struct amf_unit *)mempool_malloc (sizeof (struct amf_unit));
  307. memset (amf_unit, 0, sizeof (struct amf_unit));
  308. amf_unit->amf_group = amf_group;
  309. amf_unit->operational_state = OPENAIS_CFG_OPERATIONALSTATE_DISABLED;
  310. amf_unit->presence_state = OPENAIS_CFG_PRESENCESTATE_UNINSTANTIATED;
  311. list_init (&amf_unit->comp_head);
  312. list_init (&amf_unit->si_head);
  313. amf_unit->escalation_level = ESCALATION_LEVEL_NO_ESCALATION;
  314. amf_unit->restart_count = 0;
  315. list_add_tail (&amf_unit->unit_list, &amf_group->unit_head);
  316. memset (amf_unit->clccli_path, 0, sizeof (&amf_unit->clccli_path));
  317. memset (amf_unit->binary_path, 0, sizeof (&amf_unit->binary_path));
  318. current_parse = AMF_UNIT;
  319. } else
  320. if (strstr_rs (line, "serviceinstance{")) {
  321. amf_si = (struct amf_si *)mempool_malloc (sizeof (struct amf_si));
  322. memset (amf_si, 0, sizeof (struct amf_si));
  323. list_init (&amf_si->csi_head);
  324. list_init (&amf_si->unit_list);
  325. list_init (&amf_si->pg_head);
  326. list_add_tail (&amf_si->list, &amf_group->si_head);
  327. amf_si->group = amf_group;
  328. current_parse = AMF_SERVICEINSTANCE;
  329. } else
  330. if (strstr_rs (line, "}")) {
  331. current_parse = AMF_HEAD;
  332. } else {
  333. goto parse_error;
  334. }
  335. break;
  336. case AMF_UNIT:
  337. if ((loc = strstr_rs (line, "name=")) != 0) {
  338. setSaNameT (&amf_unit->name, loc);
  339. } else
  340. if ((loc = strstr_rs (line, "component{")) != 0) {
  341. amf_comp = (struct amf_comp *)mempool_malloc (sizeof (struct amf_comp));
  342. memset (amf_comp, 0, sizeof (struct amf_comp));
  343. amf_comp->unit = amf_unit;
  344. amf_comp->operational_state = OPENAIS_CFG_OPERATIONALSTATE_DISABLED;
  345. amf_comp->presence_state = OPENAIS_CFG_PRESENCESTATE_UNINSTANTIATED;
  346. list_init (&amf_comp->comp_list);
  347. list_init (&amf_comp->healthcheck_list);
  348. list_add_tail (&amf_comp->comp_list, &amf_unit->comp_head);
  349. memset (amf_comp->clccli_path, 0, sizeof (&amf_comp->clccli_path));
  350. memset (amf_comp->binary_path, 0, sizeof (&amf_unit->binary_path));
  351. memset (amf_comp->binary_name, 0, sizeof (&amf_comp->binary_name));
  352. current_parse = AMF_COMPONENT;
  353. } else
  354. if ((loc = strstr_rs (line, "clccli_path=")) != 0) {
  355. strcpy (amf_unit->clccli_path, loc);
  356. } else
  357. if ((loc = strstr_rs (line, "binary_path=")) != 0) {
  358. strcpy (amf_unit->binary_path, loc);
  359. } else
  360. if (strstr_rs (line, "}")) {
  361. current_parse = AMF_GROUP;
  362. } else {
  363. goto parse_error;
  364. }
  365. break;
  366. case AMF_COMPONENT:
  367. if ((loc = strstr_rs (line, "name=")) != 0) {
  368. setSaNameT (&amf_comp->name, loc);
  369. } else
  370. #ifdef COMPILE_OUT
  371. if ((loc = strstr_rs (line, "model=")) != 0) {
  372. if (strcmp (loc, "x_active_and_y_standby") == 0) {
  373. amf_comp->componentCapabilityModel = SA_AMF_COMPONENT_CAPABILITY_X_ACTIVE_AND_Y_STANDBY;
  374. } else
  375. if (strcmp (loc, "x_active_or_y_standby") == 0) {
  376. amf_comp->componentCapabilityModel = SA_AMF_COMPONENT_CAPABILITY_X_ACTIVE_OR_Y_STANDBY;
  377. } else
  378. if (strcmp (loc, "1_active_or_y_standby") == 0) {
  379. amf_comp->componentCapabilityModel = SA_AMF_COMPONENT_CAPABILITY_1_ACTIVE_OR_Y_STANDBY;
  380. } else
  381. if (strcmp (loc, "1_active_or_1_standby") == 0) {
  382. amf_comp->componentCapabilityModel = SA_AMF_COMPONENT_CAPABILITY_1_ACTIVE_OR_1_STANDBY;
  383. } else
  384. if (strcmp (loc, "x_active") == 0) {
  385. amf_comp->componentCapabilityModel = SA_AMF_COMPONENT_CAPABILITY_X_ACTIVE;
  386. } else
  387. if (strcmp (loc, "1_active") == 0) {
  388. amf_comp->componentCapabilityModel = SA_AMF_COMPONENT_CAPABILITY_1_ACTIVE;
  389. } else
  390. if (strcmp (loc, "no_active") == 0) {
  391. amf_comp->componentCapabilityModel = SA_AMF_COMPONENT_CAPABILITY_NO_ACTIVE;
  392. } else {
  393. goto parse_error;
  394. }
  395. } else
  396. #endif
  397. if ((loc = strstr_rs(line, "comptype=")) != 0) {
  398. if (strstr (line, "sa_aware")) {
  399. amf_comp->comptype = clc_component_sa_aware;
  400. } else
  401. if (strstr (line, "proxied_pre")) {
  402. amf_comp->comptype = clc_component_proxied_pre;
  403. } else
  404. if (strstr (line, "proxied_non_pre")) {
  405. amf_comp->comptype = clc_component_proxied_non_pre;
  406. } else
  407. if (strstr (line, "non_proxied_non_sa_aware")) {
  408. amf_comp->comptype = clc_component_proxied_non_pre;
  409. } else {
  410. goto parse_error;
  411. }
  412. } else
  413. if ((loc = strstr_rs(line, "instantiate=")) != 0) {
  414. strcpy (amf_comp->instantiate_cmd, loc);
  415. } else
  416. if ((loc = strstr_rs(line, "terminate=")) != 0) {
  417. strcpy (amf_comp->terminate_cmd, loc);
  418. } else
  419. if ((loc = strstr_rs(line, "cleanup=")) != 0) {
  420. strcpy (amf_comp->cleanup_cmd, loc);
  421. } else
  422. if ((loc = strstr_rs(line, "am_start=")) != 0) {
  423. strcpy (amf_comp->am_start_cmd, loc);
  424. } else
  425. if ((loc = strstr_rs(line, "am_stop=")) != 0) {
  426. strcpy (amf_comp->am_stop_cmd, loc);
  427. } else
  428. if ((loc = strstr_rs (line, "clccli_path=")) != 0) {
  429. strcpy (amf_comp->clccli_path, loc);
  430. } else
  431. if ((loc = strstr_rs (line, "binary_path=")) != 0) {
  432. strcpy (amf_comp->binary_path, loc);
  433. } else
  434. if ((loc = strstr_rs (line, "bn=")) != 0) {
  435. strcpy (amf_comp->binary_name, loc);
  436. } else
  437. if (strstr_rs (line, "}")) {
  438. current_parse = AMF_UNIT;
  439. } else {
  440. goto parse_error;
  441. }
  442. break;
  443. case AMF_SERVICEINSTANCE:
  444. if ((loc = strstr_rs (line, "name=")) != 0) {
  445. setSaNameT (&amf_si->name, loc);
  446. } else
  447. if (strstr_rs (line, "}")) {
  448. current_parse = AMF_GROUP;
  449. } else {
  450. goto parse_error;
  451. }
  452. break;
  453. case AMF_HEALTHCHECK:
  454. if ((loc = strstr_rs (line, "key=")) != 0) {
  455. strcpy (amf_healthcheck->key.key, loc);
  456. amf_healthcheck->key.keyLen = strlen (loc);
  457. } else
  458. if ((loc = strstr_rs (line, "period=")) != 0) {
  459. amf_healthcheck->period = atoi (loc);
  460. } else
  461. if ((loc = strstr_rs (line, "maximum_duration=")) != 0) {
  462. amf_healthcheck->maximum_duration = atoi (loc);
  463. } else
  464. if (strstr_rs (line, "}")) {
  465. current_parse = AMF_HEAD;
  466. } else {
  467. goto parse_error;
  468. }
  469. break;
  470. default:
  471. printf ("Invalid state\n");
  472. goto parse_error;
  473. break;
  474. }
  475. }
  476. fclose (fp);
  477. return (0);
  478. parse_error:
  479. sprintf (error_string_response,
  480. "parse error at /etc/groups.conf:%d.\n", line_number);
  481. *error_string = error_string_response;
  482. fclose (fp);
  483. return (-1);
  484. }