corosync-cmapctl.c 16 KB

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