4
0

corosync-cmapctl.c 17 KB

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