corosync-cmapctl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /*
  2. * Copyright (c) 2011 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.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 Red Hat, 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 <config.h>
  35. #include <ctype.h>
  36. #include <stdio.h>
  37. #include <poll.h>
  38. #include <corosync/corotypes.h>
  39. #include <corosync/cmap.h>
  40. #include "../lib/util.h"
  41. #ifndef INFTIM
  42. #define INFTIM -1
  43. #endif
  44. #define MAX_TRY_AGAIN 10
  45. enum user_action {
  46. ACTION_GET,
  47. ACTION_SET,
  48. ACTION_DELETE,
  49. ACTION_PRINT_ALL,
  50. ACTION_PRINT_PREFIX,
  51. ACTION_TRACK,
  52. ACTION_LOAD,
  53. };
  54. struct name_to_type_item {
  55. const char *name;
  56. cmap_value_types_t type;
  57. };
  58. struct name_to_type_item name_to_type[] = {
  59. {"i8", CMAP_VALUETYPE_INT8},
  60. {"u8", CMAP_VALUETYPE_UINT8},
  61. {"i16", CMAP_VALUETYPE_INT16},
  62. {"u16", CMAP_VALUETYPE_UINT16},
  63. {"i32", CMAP_VALUETYPE_INT32},
  64. {"u32", CMAP_VALUETYPE_UINT32},
  65. {"i64", CMAP_VALUETYPE_INT64},
  66. {"u64", CMAP_VALUETYPE_UINT64},
  67. {"flt", CMAP_VALUETYPE_FLOAT},
  68. {"dbl", CMAP_VALUETYPE_DOUBLE},
  69. {"str", CMAP_VALUETYPE_STRING},
  70. {"bin", CMAP_VALUETYPE_BINARY}};
  71. int show_binary = 0;
  72. static int convert_name_to_type(const char *name)
  73. {
  74. int i;
  75. for (i = 0; i < sizeof(name_to_type) / sizeof(*name_to_type); i++) {
  76. if (strcmp(name, name_to_type[i].name) == 0) {
  77. return (name_to_type[i].type);
  78. }
  79. }
  80. return (-1);
  81. }
  82. static int print_help(void)
  83. {
  84. printf("\n");
  85. printf("usage: corosync-cmapctl [-b] [-adghsTtp] [params...]\n");
  86. printf("Set key:\n");
  87. printf(" corosync-cmapctl -s key_name type value\n");
  88. printf("\n");
  89. printf(" where type is one of ([i|u][8|16|32|64] | flt | dbl | str | bin)\n");
  90. printf(" for bin, value is file name (or - for stdin)\n");
  91. printf("\n");
  92. printf("Load settings from a file:\n");
  93. printf(" corosync-cmapctl -p filename\n");
  94. printf("\n");
  95. printf(" the format of the file is:\n");
  96. printf(" <key_name> <type> <value>\n");
  97. printf("\n");
  98. printf("Delete key:\n");
  99. printf(" corosync-cmapctl -d key_name...\n");
  100. printf("\n");
  101. printf("Get key:\n");
  102. printf(" corosync-cmapctl [-b] -g key_name...\n");
  103. printf("\n");
  104. printf("Display all keys:\n");
  105. printf(" corosync-cmapctl [-b]\n");
  106. printf("\n");
  107. printf("Display keys with prefix key_name:\n");
  108. printf(" corosync-cmapctl [-b] key_name...\n");
  109. printf("\n");
  110. printf("Track changes on keys with prefix key_name:\n");
  111. printf(" corosync-cmapctl [-b] -t key_name\n");
  112. printf("\n");
  113. printf("Track changes on keys with key_name name:\n");
  114. printf(" corosync-cmapctl [-b] -T key_name\n");
  115. printf("\n");
  116. return (0);
  117. }
  118. static void print_binary_key (char *value, size_t value_len)
  119. {
  120. size_t i;
  121. char c;
  122. for (i = 0; i < value_len; i++) {
  123. c = value[i];
  124. if (c >= ' ' && c < 0x7f && c != '\\') {
  125. fputc (c, stdout);
  126. } else {
  127. if (c == '\\') {
  128. printf ("\\\\");
  129. } else {
  130. printf ("\\x%02X", c);
  131. }
  132. }
  133. }
  134. }
  135. static void print_key(cmap_handle_t handle,
  136. const char *key_name,
  137. size_t value_len,
  138. const void *value,
  139. cmap_value_types_t type)
  140. {
  141. char *str;
  142. char *bin_value = NULL;
  143. cs_error_t err;
  144. int8_t i8;
  145. uint8_t u8;
  146. int16_t i16;
  147. uint16_t u16;
  148. int32_t i32;
  149. uint32_t u32;
  150. int64_t i64;
  151. uint64_t u64;
  152. float flt;
  153. double dbl;
  154. int end_loop;
  155. int no_retries;
  156. size_t bin_value_len;
  157. end_loop = 0;
  158. no_retries = 0;
  159. err = CS_OK;
  160. while (!end_loop) {
  161. switch (type) {
  162. case CMAP_VALUETYPE_INT8:
  163. if (value == NULL) {
  164. err = cmap_get_int8(handle, key_name, &i8);
  165. } else {
  166. i8 = *((int8_t *)value);
  167. }
  168. break;
  169. case CMAP_VALUETYPE_INT16:
  170. if (value == NULL) {
  171. err = cmap_get_int16(handle, key_name, &i16);
  172. } else {
  173. i16 = *((int16_t *)value);
  174. }
  175. break;
  176. case CMAP_VALUETYPE_INT32:
  177. if (value == NULL) {
  178. err = cmap_get_int32(handle, key_name, &i32);
  179. } else {
  180. i32 = *((int32_t *)value);
  181. }
  182. break;
  183. case CMAP_VALUETYPE_INT64:
  184. if (value == NULL) {
  185. err = cmap_get_int64(handle, key_name, &i64);
  186. } else {
  187. i64 = *((int64_t *)value);
  188. }
  189. break;
  190. case CMAP_VALUETYPE_UINT8:
  191. if (value == NULL) {
  192. err = cmap_get_uint8(handle, key_name, &u8);
  193. } else {
  194. u8 = *((uint8_t *)value);
  195. }
  196. break;
  197. case CMAP_VALUETYPE_UINT16:
  198. if (value == NULL) {
  199. err = cmap_get_uint16(handle, key_name, &u16);
  200. } else {
  201. u16 = *((uint16_t *)value);
  202. }
  203. break;
  204. case CMAP_VALUETYPE_UINT32:
  205. if (value == NULL) {
  206. err = cmap_get_uint32(handle, key_name, &u32);
  207. } else {
  208. u32 = *((uint32_t *)value);
  209. }
  210. break;
  211. case CMAP_VALUETYPE_UINT64:
  212. if (value == NULL) {
  213. err = cmap_get_uint64(handle, key_name, &u64);
  214. } else {
  215. u64 = *((uint64_t *)value);
  216. }
  217. break;
  218. case CMAP_VALUETYPE_FLOAT:
  219. if (value == NULL) {
  220. err = cmap_get_float(handle, key_name, &flt);
  221. } else {
  222. flt = *((float *)value);
  223. }
  224. break;
  225. case CMAP_VALUETYPE_DOUBLE:
  226. if (value == NULL) {
  227. err = cmap_get_double(handle, key_name, &dbl);
  228. } else {
  229. dbl = *((double *)value);
  230. }
  231. break;
  232. case CMAP_VALUETYPE_STRING:
  233. if (value == NULL) {
  234. err = cmap_get_string(handle, key_name, &str);
  235. } else {
  236. str = (char *)value;
  237. }
  238. break;
  239. case CMAP_VALUETYPE_BINARY:
  240. if (show_binary) {
  241. if (value == NULL) {
  242. bin_value = malloc(value_len);
  243. if (bin_value == NULL) {
  244. fprintf(stderr, "Can't alloc memory\n");
  245. exit(EXIT_FAILURE);
  246. }
  247. bin_value_len = value_len;
  248. err = cmap_get(handle, key_name, bin_value, &bin_value_len, NULL);
  249. } else {
  250. bin_value = (char *)value;
  251. }
  252. }
  253. break;
  254. }
  255. if (err == CS_OK)
  256. end_loop = 1;
  257. if (err == CS_ERR_TRY_AGAIN) {
  258. sleep(1);
  259. no_retries++;
  260. }
  261. if (no_retries > MAX_TRY_AGAIN) {
  262. end_loop = 1;
  263. }
  264. };
  265. if (err != CS_OK) {
  266. fprintf(stderr, "Can't get value of %s. Error %s\n", key_name, cs_strerror(err));
  267. return ;
  268. }
  269. printf("%s (", key_name);
  270. switch (type) {
  271. case CMAP_VALUETYPE_INT8:
  272. printf("%s) = %"PRId8, "i8", i8);
  273. break;
  274. case CMAP_VALUETYPE_UINT8:
  275. printf("%s) = %"PRIu8, "u8", u8);
  276. break;
  277. case CMAP_VALUETYPE_INT16:
  278. printf("%s) = %"PRId16, "i16", i16);
  279. break;
  280. case CMAP_VALUETYPE_UINT16:
  281. printf("%s) = %"PRIu16, "u16", u16);
  282. break;
  283. case CMAP_VALUETYPE_INT32:
  284. printf("%s) = %"PRId32, "i32", i32);
  285. break;
  286. case CMAP_VALUETYPE_UINT32:
  287. printf("%s) = %"PRIu32, "u32", u32);
  288. break;
  289. case CMAP_VALUETYPE_INT64:
  290. printf("%s) = %"PRId64, "i64", i64);
  291. break;
  292. case CMAP_VALUETYPE_UINT64:
  293. printf("%s) = %"PRIu64, "u64", u64);
  294. break;
  295. case CMAP_VALUETYPE_FLOAT:
  296. printf("%s) = %f", "flt", flt);
  297. break;
  298. case CMAP_VALUETYPE_DOUBLE:
  299. printf("%s) = %lf", "dbl", dbl);
  300. break;
  301. case CMAP_VALUETYPE_STRING:
  302. printf("%s) = %s", "str", str);
  303. if (value == NULL) {
  304. free(str);
  305. }
  306. break;
  307. case CMAP_VALUETYPE_BINARY:
  308. printf("%s)", "bin");
  309. if (show_binary) {
  310. printf(" = ");
  311. if (bin_value) {
  312. print_binary_key(bin_value, value_len);
  313. if (value == NULL) {
  314. free(bin_value);
  315. }
  316. } else {
  317. printf("*empty*");
  318. }
  319. }
  320. break;
  321. }
  322. printf("\n");
  323. }
  324. static void print_iter(cmap_handle_t handle, const char *prefix)
  325. {
  326. cmap_iter_handle_t iter_handle;
  327. char key_name[CMAP_KEYNAME_MAXLEN + 1];
  328. size_t value_len;
  329. cmap_value_types_t type;
  330. cs_error_t err;
  331. err = cmap_iter_init(handle, prefix, &iter_handle);
  332. if (err != CS_OK) {
  333. fprintf (stderr, "Failed to initialize iteration. Error %s\n", cs_strerror(err));
  334. exit (EXIT_FAILURE);
  335. }
  336. while ((err = cmap_iter_next(handle, iter_handle, key_name, &value_len, &type)) == CS_OK) {
  337. print_key(handle, key_name, value_len, NULL, type);
  338. }
  339. }
  340. static void cmap_notify_fn(
  341. cmap_handle_t cmap_handle,
  342. cmap_track_handle_t cmap_track_handle,
  343. int32_t event,
  344. const char *key_name,
  345. struct cmap_notify_value new_val,
  346. struct cmap_notify_value old_val,
  347. void *user_data)
  348. {
  349. switch (event) {
  350. case CMAP_TRACK_ADD:
  351. printf("create> ");
  352. print_key(cmap_handle, key_name, new_val.len, new_val.data, new_val.type);
  353. break;
  354. case CMAP_TRACK_DELETE:
  355. printf("delete> ");
  356. print_key(cmap_handle, key_name, old_val.len, old_val.data, old_val.type);
  357. break;
  358. case CMAP_TRACK_MODIFY:
  359. printf("modify> ");
  360. print_key(cmap_handle, key_name, new_val.len, new_val.data, new_val.type);
  361. break;
  362. default:
  363. printf("unknown change> ");
  364. break;
  365. }
  366. }
  367. static void add_track(cmap_handle_t handle, const char *key_name, int prefix)
  368. {
  369. cmap_track_handle_t track_handle;
  370. int32_t track_type;
  371. cs_error_t err;
  372. track_type = CMAP_TRACK_ADD | CMAP_TRACK_DELETE | CMAP_TRACK_MODIFY;
  373. if (prefix) {
  374. track_type |= CMAP_TRACK_PREFIX;
  375. }
  376. err = cmap_track_add(handle, key_name, track_type, cmap_notify_fn, NULL, &track_handle);
  377. if (err != CS_OK) {
  378. fprintf(stderr, "Failed to add tracking function. Error %s\n", cs_strerror(err));
  379. exit (EXIT_FAILURE);
  380. }
  381. }
  382. static void track_changes(cmap_handle_t handle)
  383. {
  384. struct pollfd pfd[2];
  385. int cmap_fd;
  386. cs_error_t err;
  387. int poll_res;
  388. char inbuf[3];
  389. int quit = CS_FALSE;
  390. err = cmap_fd_get(handle, &cmap_fd);
  391. if (err != CS_OK) {
  392. fprintf(stderr, "Failed to get file handle. Error %s\n", cs_strerror(err));
  393. exit (EXIT_FAILURE);
  394. }
  395. pfd[0].fd = cmap_fd;
  396. pfd[1].fd = STDIN_FILENO;
  397. pfd[0].events = pfd[1].events = POLLIN;
  398. printf("Type \"q\" to finish\n");
  399. do {
  400. pfd[0].revents = pfd[1].revents = 0;
  401. poll_res = poll(pfd, 2, INFTIM);
  402. if (poll_res == -1) {
  403. perror("poll");
  404. }
  405. if (pfd[1].revents & POLLIN) {
  406. if (fgets(inbuf, sizeof(inbuf), stdin) == NULL) {
  407. quit = CS_TRUE;
  408. } else if (strncmp(inbuf, "q", 1) == 0) {
  409. quit = CS_TRUE;
  410. }
  411. }
  412. if (pfd[0].revents & POLLIN) {
  413. err = cmap_dispatch(handle, CS_DISPATCH_ALL);
  414. if (err != CS_OK) {
  415. fprintf(stderr, "Dispatch error %s\n", cs_strerror(err));
  416. quit = CS_TRUE;
  417. }
  418. }
  419. } while (poll_res > 0 && !quit);
  420. }
  421. static cs_error_t set_key_bin(cmap_handle_t handle, const char *key_name, const char *fname)
  422. {
  423. FILE *f;
  424. char *val;
  425. char buf[4096];
  426. size_t size;
  427. size_t readed;
  428. size_t pos;
  429. cs_error_t err;
  430. if (strcmp(fname, "-") == 0) {
  431. f = stdin;
  432. } else {
  433. f = fopen(fname, "rb");
  434. if (f == NULL) {
  435. perror("Can't open input file");
  436. exit(EXIT_FAILURE);
  437. }
  438. }
  439. val = NULL;
  440. size = 0;
  441. pos = 0;
  442. while ((readed = fread(buf, 1, sizeof(buf), f)) != 0) {
  443. size += readed;
  444. if ((val = realloc(val, size)) == NULL) {
  445. fprintf(stderr, "Can't alloc memory\n");
  446. exit (EXIT_FAILURE);
  447. }
  448. memcpy(val + pos, buf, readed);
  449. pos += readed;
  450. }
  451. if (f != stdin) {
  452. fclose(f);
  453. }
  454. err = cmap_set(handle, key_name, val, size, CMAP_VALUETYPE_BINARY);
  455. free(val);
  456. return (err);
  457. }
  458. static void set_key(cmap_handle_t handle, const char *key_name, const char *key_type_s, const char *key_value_s)
  459. {
  460. int64_t i64;
  461. uint64_t u64;
  462. double dbl;
  463. float flt;
  464. cs_error_t err = CS_OK;
  465. int scanf_res = 0;
  466. cmap_value_types_t type;
  467. if (convert_name_to_type(key_type_s) == -1) {
  468. fprintf(stderr, "Unknown type %s\n", key_type_s);
  469. exit (EXIT_FAILURE);
  470. }
  471. type = convert_name_to_type(key_type_s);
  472. switch (type) {
  473. case CMAP_VALUETYPE_INT8:
  474. case CMAP_VALUETYPE_INT16:
  475. case CMAP_VALUETYPE_INT32:
  476. case CMAP_VALUETYPE_INT64:
  477. scanf_res = sscanf(key_value_s, "%"PRId64, &i64);
  478. break;
  479. case CMAP_VALUETYPE_UINT8:
  480. case CMAP_VALUETYPE_UINT16:
  481. case CMAP_VALUETYPE_UINT32:
  482. case CMAP_VALUETYPE_UINT64:
  483. scanf_res = sscanf(key_value_s, "%"PRIu64, &u64);
  484. break;
  485. case CMAP_VALUETYPE_FLOAT:
  486. scanf_res = sscanf(key_value_s, "%f", &flt);
  487. break;
  488. case CMAP_VALUETYPE_DOUBLE:
  489. scanf_res = sscanf(key_value_s, "%lf", &dbl);
  490. break;
  491. case CMAP_VALUETYPE_STRING:
  492. case CMAP_VALUETYPE_BINARY:
  493. /*
  494. * Do nothing
  495. */
  496. scanf_res = 1;
  497. break;
  498. }
  499. if (scanf_res != 1) {
  500. fprintf(stderr, "%s is not valid %s type value\n", key_value_s, key_type_s);
  501. exit(EXIT_FAILURE);
  502. }
  503. /*
  504. * We have parsed value, so insert value
  505. */
  506. switch (type) {
  507. case CMAP_VALUETYPE_INT8:
  508. if (i64 > INT8_MAX || i64 < INT8_MIN) {
  509. fprintf(stderr, "%s is not valid i8 integer\n", key_value_s);
  510. exit(EXIT_FAILURE);
  511. }
  512. err = cmap_set_int8(handle, key_name, i64);
  513. break;
  514. case CMAP_VALUETYPE_INT16:
  515. if (i64 > INT16_MAX || i64 < INT16_MIN) {
  516. fprintf(stderr, "%s is not valid i16 integer\n", key_value_s);
  517. exit(EXIT_FAILURE);
  518. }
  519. err = cmap_set_int16(handle, key_name, i64);
  520. break;
  521. case CMAP_VALUETYPE_INT32:
  522. if (i64 > INT32_MAX || i64 < INT32_MIN) {
  523. fprintf(stderr, "%s is not valid i32 integer\n", key_value_s);
  524. exit(EXIT_FAILURE);
  525. }
  526. err = cmap_set_int32(handle, key_name, i64);
  527. break;
  528. case CMAP_VALUETYPE_INT64:
  529. err = cmap_set_int64(handle, key_name, i64);
  530. break;
  531. case CMAP_VALUETYPE_UINT8:
  532. if (u64 > UINT8_MAX) {
  533. fprintf(stderr, "%s is not valid u8 integer\n", key_value_s);
  534. exit(EXIT_FAILURE);
  535. }
  536. err = cmap_set_uint8(handle, key_name, u64);
  537. break;
  538. case CMAP_VALUETYPE_UINT16:
  539. if (u64 > UINT16_MAX) {
  540. fprintf(stderr, "%s is not valid u16 integer\n", key_value_s);
  541. exit(EXIT_FAILURE);
  542. }
  543. err = cmap_set_uint16(handle, key_name, u64);
  544. break;
  545. case CMAP_VALUETYPE_UINT32:
  546. if (u64 > UINT32_MAX) {
  547. fprintf(stderr, "%s is not valid u32 integer\n", key_value_s);
  548. exit(EXIT_FAILURE);
  549. }
  550. err = cmap_set_uint32(handle, key_name, u64);
  551. break;
  552. case CMAP_VALUETYPE_UINT64:
  553. err = cmap_set_uint64(handle, key_name, u64);
  554. break;
  555. case CMAP_VALUETYPE_FLOAT:
  556. err = cmap_set_float(handle, key_name, flt);
  557. break;
  558. case CMAP_VALUETYPE_DOUBLE:
  559. err = cmap_set_double(handle, key_name, dbl);
  560. break;
  561. case CMAP_VALUETYPE_STRING:
  562. err = cmap_set_string(handle, key_name, key_value_s);
  563. break;
  564. case CMAP_VALUETYPE_BINARY:
  565. err = set_key_bin(handle, key_name, key_value_s);
  566. break;
  567. }
  568. if (err != CS_OK) {
  569. fprintf (stderr, "Failed to set key %s. Error %s\n", key_name, cs_strerror(err));
  570. exit (EXIT_FAILURE);
  571. }
  572. }
  573. static void read_in_config_file(cmap_handle_t handle, char * filename)
  574. {
  575. int ignore;
  576. int c;
  577. FILE* fh;
  578. char buf[1024];
  579. char * line;
  580. char *key_name;
  581. char *key_type_s;
  582. char *key_value_s;
  583. if (access (filename, R_OK) != 0) {
  584. perror ("Couldn't access file.");
  585. return;
  586. }
  587. fh = fopen(filename, "r");
  588. if (fh == NULL) {
  589. perror ("Couldn't open file.");
  590. return;
  591. }
  592. while (fgets (buf, 1024, fh) != NULL) {
  593. /* find the first real character, if it is
  594. * a '#' then ignore this line.
  595. * else process.
  596. * if no real characters then also ignore.
  597. */
  598. ignore = 1;
  599. for (c = 0; c < 1024; c++) {
  600. if (isblank (buf[c])) {
  601. continue;
  602. }
  603. if (buf[c] == '#' || buf[c] == '\n') {
  604. ignore = 1;
  605. break;
  606. }
  607. ignore = 0;
  608. line = &buf[c];
  609. break;
  610. }
  611. if (ignore == 1) {
  612. continue;
  613. }
  614. /*
  615. * should be:
  616. * <key> <type> <value>
  617. */
  618. key_name = strtok(line, " \n");
  619. key_type_s = strtok(NULL, " \n");
  620. key_value_s = strtok(NULL, " \n");
  621. set_key(handle, key_name, key_type_s, key_value_s);
  622. }
  623. fclose (fh);
  624. }
  625. int main(int argc, char *argv[])
  626. {
  627. enum user_action action;
  628. int c;
  629. cs_error_t err;
  630. cmap_handle_t handle;
  631. int i;
  632. size_t value_len;
  633. cmap_value_types_t type;
  634. int track_prefix;
  635. int no_retries;
  636. char * settings_file = NULL;
  637. action = ACTION_PRINT_PREFIX;
  638. track_prefix = 1;
  639. while ((c = getopt(argc, argv, "hgsdtTbp:")) != -1) {
  640. switch (c) {
  641. case 'h':
  642. return print_help();
  643. break;
  644. case 'b':
  645. show_binary++;
  646. break;
  647. case 'g':
  648. action = ACTION_GET;
  649. break;
  650. case 's':
  651. action = ACTION_SET;
  652. break;
  653. case 'd':
  654. action = ACTION_DELETE;
  655. break;
  656. case 'p':
  657. settings_file = optarg;
  658. action = ACTION_LOAD;
  659. break;
  660. case 't':
  661. action = ACTION_TRACK;
  662. break;
  663. case 'T':
  664. action = ACTION_TRACK;
  665. track_prefix = 0;
  666. break;
  667. case '?':
  668. return (EXIT_FAILURE);
  669. break;
  670. default:
  671. action = ACTION_PRINT_PREFIX;
  672. break;
  673. }
  674. }
  675. if (argc == 1 || (argc == 2 && show_binary)) {
  676. action = ACTION_PRINT_ALL;
  677. }
  678. argc -= optind;
  679. argv += optind;
  680. if (argc == 0 &&
  681. action != ACTION_LOAD &&
  682. action != ACTION_PRINT_ALL) {
  683. fprintf(stderr, "Expected key after options\n");
  684. return (EXIT_FAILURE);
  685. }
  686. no_retries = 0;
  687. while ((err = cmap_initialize(&handle)) == CS_ERR_TRY_AGAIN && no_retries++ < MAX_TRY_AGAIN) {
  688. sleep(1);
  689. }
  690. if (err != CS_OK) {
  691. fprintf (stderr, "Failed to initialize the cmap API. Error %s\n", cs_strerror(err));
  692. exit (EXIT_FAILURE);
  693. }
  694. switch (action) {
  695. case ACTION_PRINT_ALL:
  696. print_iter(handle, NULL);
  697. break;
  698. case ACTION_PRINT_PREFIX:
  699. for (i = 0; i < argc; i++) {
  700. print_iter(handle, argv[i]);
  701. }
  702. break;
  703. case ACTION_GET:
  704. for (i = 0; i < argc; i++) {
  705. err = cmap_get(handle, argv[i], NULL, &value_len, &type);
  706. if (err == CS_OK) {
  707. print_key(handle, argv[i], value_len, NULL, type);
  708. } else {
  709. fprintf(stderr, "Can't get key %s. Error %s\n", argv[i], cs_strerror(err));
  710. }
  711. }
  712. break;
  713. case ACTION_DELETE:
  714. for (i = 0; i < argc; i++) {
  715. err = cmap_delete(handle, argv[i]);
  716. if (err != CS_OK) {
  717. fprintf(stderr, "Can't delete key %s. Error %s\n", argv[i], cs_strerror(err));
  718. }
  719. }
  720. break;
  721. case ACTION_LOAD:
  722. read_in_config_file(handle, settings_file);
  723. break;
  724. case ACTION_TRACK:
  725. for (i = 0; i < argc; i++) {
  726. add_track(handle, argv[i], track_prefix);
  727. }
  728. track_changes(handle);
  729. break;
  730. case ACTION_SET:
  731. if (argc < 3) {
  732. fprintf(stderr, "At least 3 parameters are expected for set\n");
  733. return (EXIT_FAILURE);
  734. }
  735. set_key(handle, argv[0], argv[1], argv[2]);
  736. break;
  737. }
  738. err = cmap_finalize(handle);
  739. if (err != CS_OK) {
  740. fprintf (stderr, "Failed to finalize the cmap API. Error %s\n", cs_strerror(err));
  741. exit (EXIT_FAILURE);
  742. }
  743. return (0);
  744. }