4
0

corosync-cmapctl.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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;
  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. print_binary_key(bin_value, value_len);
  304. if (value == NULL) {
  305. free(bin_value);
  306. }
  307. }
  308. break;
  309. }
  310. printf("\n");
  311. }
  312. static void print_iter(cmap_handle_t handle, const char *prefix)
  313. {
  314. cmap_iter_handle_t iter_handle;
  315. char key_name[CMAP_KEYNAME_MAXLEN + 1];
  316. size_t value_len;
  317. cmap_value_types_t type;
  318. cs_error_t err;
  319. err = cmap_iter_init(handle, prefix, &iter_handle);
  320. if (err != CS_OK) {
  321. fprintf (stderr, "Failed to initialize iteration. Error %s\n", cs_strerror(err));
  322. exit (EXIT_FAILURE);
  323. }
  324. while ((err = cmap_iter_next(handle, iter_handle, key_name, &value_len, &type)) == CS_OK) {
  325. print_key(handle, key_name, value_len, NULL, type);
  326. }
  327. }
  328. static void cmap_notify_fn(
  329. cmap_handle_t cmap_handle,
  330. cmap_track_handle_t cmap_track_handle,
  331. int32_t event,
  332. const char *key_name,
  333. struct cmap_notify_value new_val,
  334. struct cmap_notify_value old_val,
  335. void *user_data)
  336. {
  337. switch (event) {
  338. case CMAP_TRACK_ADD:
  339. printf("create> ");
  340. print_key(cmap_handle, key_name, new_val.len, new_val.data, new_val.type);
  341. break;
  342. case CMAP_TRACK_DELETE:
  343. printf("delete> ");
  344. print_key(cmap_handle, key_name, old_val.len, old_val.data, old_val.type);
  345. break;
  346. case CMAP_TRACK_MODIFY:
  347. printf("modify> ");
  348. print_key(cmap_handle, key_name, new_val.len, new_val.data, new_val.type);
  349. break;
  350. default:
  351. printf("unknown change> ");
  352. break;
  353. }
  354. }
  355. static void add_track(cmap_handle_t handle, const char *key_name, int prefix)
  356. {
  357. cmap_track_handle_t track_handle;
  358. int32_t track_type;
  359. cs_error_t err;
  360. track_type = CMAP_TRACK_ADD | CMAP_TRACK_DELETE | CMAP_TRACK_MODIFY;
  361. if (prefix) {
  362. track_type |= CMAP_TRACK_PREFIX;
  363. }
  364. err = cmap_track_add(handle, key_name, track_type, cmap_notify_fn, NULL, &track_handle);
  365. if (err != CS_OK) {
  366. fprintf(stderr, "Failed to add tracking function. Error %s\n", cs_strerror(err));
  367. exit (EXIT_FAILURE);
  368. }
  369. }
  370. static void track_changes(cmap_handle_t handle)
  371. {
  372. struct pollfd pfd[2];
  373. int cmap_fd;
  374. cs_error_t err;
  375. int poll_res;
  376. char inbuf[3];
  377. int quit = CS_FALSE;
  378. err = cmap_fd_get(handle, &cmap_fd);
  379. if (err != CS_OK) {
  380. fprintf(stderr, "Failed to get file handle. Error %s\n", cs_strerror(err));
  381. exit (EXIT_FAILURE);
  382. }
  383. pfd[0].fd = cmap_fd;
  384. pfd[1].fd = STDIN_FILENO;
  385. pfd[0].events = pfd[1].events = POLLIN;
  386. printf("Type \"q\" to finish\n");
  387. do {
  388. pfd[0].revents = pfd[1].revents = 0;
  389. poll_res = poll(pfd, 2, INFTIM);
  390. if (poll_res == -1) {
  391. perror("poll");
  392. }
  393. if (pfd[1].revents & POLLIN) {
  394. if (fgets(inbuf, sizeof(inbuf), stdin) == NULL) {
  395. quit = CS_TRUE;
  396. } else if (strncmp(inbuf, "q", 1) == 0) {
  397. quit = CS_TRUE;
  398. }
  399. }
  400. if (pfd[0].revents & POLLIN) {
  401. err = cmap_dispatch(handle, CS_DISPATCH_ALL);
  402. if (err != CS_OK) {
  403. fprintf(stderr, "Dispatch error %s\n", cs_strerror(err));
  404. quit = CS_TRUE;
  405. }
  406. }
  407. } while (poll_res > 0 && !quit);
  408. }
  409. static cs_error_t set_key_bin(cmap_handle_t handle, const char *key_name, const char *fname)
  410. {
  411. FILE *f;
  412. char *val;
  413. char buf[4096];
  414. size_t size;
  415. size_t readed;
  416. size_t pos;
  417. cs_error_t err;
  418. if (strcmp(fname, "-") == 0) {
  419. f = stdin;
  420. } else {
  421. f = fopen(fname, "rb");
  422. if (f == NULL) {
  423. perror("Can't open input file");
  424. exit(EXIT_FAILURE);
  425. }
  426. }
  427. val = NULL;
  428. size = 0;
  429. pos = 0;
  430. while ((readed = fread(buf, 1, sizeof(buf), f)) != 0) {
  431. size += readed;
  432. if ((val = realloc(val, size)) == NULL) {
  433. fprintf(stderr, "Can't alloc memory\n");
  434. exit (EXIT_FAILURE);
  435. }
  436. memcpy(val + pos, buf, readed);
  437. pos += readed;
  438. }
  439. if (f != stdin) {
  440. fclose(f);
  441. }
  442. err = cmap_set(handle, key_name, val, size, CMAP_VALUETYPE_BINARY);
  443. free(val);
  444. return (err);
  445. }
  446. static void set_key(cmap_handle_t handle, const char *key_name, const char *key_type_s, const char *key_value_s)
  447. {
  448. int64_t i64;
  449. uint64_t u64;
  450. double dbl;
  451. float flt;
  452. cs_error_t err;
  453. int scanf_res;
  454. cmap_value_types_t type;
  455. if (convert_name_to_type(key_type_s) == -1) {
  456. fprintf(stderr, "Unknown type %s\n", key_type_s);
  457. exit (EXIT_FAILURE);
  458. }
  459. type = convert_name_to_type(key_type_s);
  460. switch (type) {
  461. case CMAP_VALUETYPE_INT8:
  462. case CMAP_VALUETYPE_INT16:
  463. case CMAP_VALUETYPE_INT32:
  464. case CMAP_VALUETYPE_INT64:
  465. scanf_res = sscanf(key_value_s, "%"PRId64, &i64);
  466. break;
  467. case CMAP_VALUETYPE_UINT8:
  468. case CMAP_VALUETYPE_UINT16:
  469. case CMAP_VALUETYPE_UINT32:
  470. case CMAP_VALUETYPE_UINT64:
  471. scanf_res = sscanf(key_value_s, "%"PRIu64, &u64);
  472. break;
  473. case CMAP_VALUETYPE_FLOAT:
  474. scanf_res = sscanf(key_value_s, "%f", &flt);
  475. break;
  476. case CMAP_VALUETYPE_DOUBLE:
  477. scanf_res = sscanf(key_value_s, "%lf", &dbl);
  478. break;
  479. case CMAP_VALUETYPE_STRING:
  480. case CMAP_VALUETYPE_BINARY:
  481. /*
  482. * Do nothing
  483. */
  484. scanf_res = 1;
  485. break;
  486. }
  487. if (scanf_res != 1) {
  488. fprintf(stderr, "%s is not valid %s type value\n", key_value_s, key_type_s);
  489. exit(EXIT_FAILURE);
  490. }
  491. /*
  492. * We have parsed value, so insert value
  493. */
  494. switch (type) {
  495. case CMAP_VALUETYPE_INT8:
  496. if (i64 > INT8_MAX || i64 < INT8_MIN) {
  497. fprintf(stderr, "%s is not valid i8 integer\n", key_value_s);
  498. exit(EXIT_FAILURE);
  499. }
  500. err = cmap_set_int8(handle, key_name, i64);
  501. break;
  502. case CMAP_VALUETYPE_INT16:
  503. if (i64 > INT16_MAX || i64 < INT16_MIN) {
  504. fprintf(stderr, "%s is not valid i16 integer\n", key_value_s);
  505. exit(EXIT_FAILURE);
  506. }
  507. err = cmap_set_int16(handle, key_name, i64);
  508. break;
  509. case CMAP_VALUETYPE_INT32:
  510. if (i64 > INT32_MAX || i64 < INT32_MIN) {
  511. fprintf(stderr, "%s is not valid i32 integer\n", key_value_s);
  512. exit(EXIT_FAILURE);
  513. }
  514. err = cmap_set_int32(handle, key_name, i64);
  515. break;
  516. case CMAP_VALUETYPE_INT64:
  517. err = cmap_set_int64(handle, key_name, i64);
  518. break;
  519. case CMAP_VALUETYPE_UINT8:
  520. if (u64 > UINT8_MAX) {
  521. fprintf(stderr, "%s is not valid u8 integer\n", key_value_s);
  522. exit(EXIT_FAILURE);
  523. }
  524. err = cmap_set_uint8(handle, key_name, u64);
  525. break;
  526. case CMAP_VALUETYPE_UINT16:
  527. if (u64 > UINT16_MAX) {
  528. fprintf(stderr, "%s is not valid u16 integer\n", key_value_s);
  529. exit(EXIT_FAILURE);
  530. }
  531. err = cmap_set_uint16(handle, key_name, u64);
  532. break;
  533. case CMAP_VALUETYPE_UINT32:
  534. if (u64 > UINT32_MAX) {
  535. fprintf(stderr, "%s is not valid u32 integer\n", key_value_s);
  536. exit(EXIT_FAILURE);
  537. }
  538. err = cmap_set_uint32(handle, key_name, u64);
  539. break;
  540. case CMAP_VALUETYPE_UINT64:
  541. err = cmap_set_uint64(handle, key_name, u64);
  542. break;
  543. case CMAP_VALUETYPE_FLOAT:
  544. err = cmap_set_float(handle, key_name, flt);
  545. break;
  546. case CMAP_VALUETYPE_DOUBLE:
  547. err = cmap_set_double(handle, key_name, dbl);
  548. break;
  549. case CMAP_VALUETYPE_STRING:
  550. err = cmap_set_string(handle, key_name, key_value_s);
  551. break;
  552. case CMAP_VALUETYPE_BINARY:
  553. err = set_key_bin(handle, key_name, key_value_s);
  554. break;
  555. }
  556. if (err != CS_OK) {
  557. fprintf (stderr, "Failed to set key %s. Error %s\n", key_name, cs_strerror(err));
  558. exit (EXIT_FAILURE);
  559. }
  560. }
  561. int main(int argc, char *argv[])
  562. {
  563. enum user_action action;
  564. int c;
  565. cs_error_t err;
  566. cmap_handle_t handle;
  567. int i;
  568. size_t value_len;
  569. cmap_value_types_t type;
  570. int track_prefix;
  571. int no_retries;
  572. action = ACTION_PRINT_PREFIX;
  573. track_prefix = 1;
  574. while ((c = getopt(argc, argv, "hgsdtTb")) != -1) {
  575. switch (c) {
  576. case 'h':
  577. return print_help();
  578. break;
  579. case 'b':
  580. show_binary++;
  581. break;
  582. case 'g':
  583. action = ACTION_GET;
  584. break;
  585. case 's':
  586. action = ACTION_SET;
  587. break;
  588. case 'd':
  589. action = ACTION_DELETE;
  590. break;
  591. case 't':
  592. action = ACTION_TRACK;
  593. break;
  594. case 'T':
  595. action = ACTION_TRACK;
  596. track_prefix = 0;
  597. break;
  598. case '?':
  599. return (EXIT_FAILURE);
  600. break;
  601. default:
  602. action = ACTION_PRINT_PREFIX;
  603. break;
  604. }
  605. }
  606. if (argc == 1 || (argc == 2 && show_binary)) {
  607. action = ACTION_PRINT_ALL;
  608. }
  609. argc -= optind;
  610. argv += optind;
  611. if (argc == 0 && action != ACTION_PRINT_ALL) {
  612. fprintf(stderr, "Expected key after options\n");
  613. return (EXIT_FAILURE);
  614. }
  615. no_retries = 0;
  616. while ((err = cmap_initialize(&handle)) == CS_ERR_TRY_AGAIN && no_retries++ < MAX_TRY_AGAIN) {
  617. sleep(1);
  618. }
  619. if (err != CS_OK) {
  620. fprintf (stderr, "Failed to initialize the cmap API. Error %s\n", cs_strerror(err));
  621. exit (EXIT_FAILURE);
  622. }
  623. switch (action) {
  624. case ACTION_PRINT_ALL:
  625. print_iter(handle, NULL);
  626. break;
  627. case ACTION_PRINT_PREFIX:
  628. for (i = 0; i < argc; i++) {
  629. print_iter(handle, argv[i]);
  630. }
  631. break;
  632. case ACTION_GET:
  633. for (i = 0; i < argc; i++) {
  634. err = cmap_get(handle, argv[i], NULL, &value_len, &type);
  635. if (err == CS_OK) {
  636. print_key(handle, argv[i], value_len, NULL, type);
  637. } else {
  638. fprintf(stderr, "Can't get key %s. Error %s\n", argv[i], cs_strerror(err));
  639. }
  640. }
  641. break;
  642. case ACTION_DELETE:
  643. for (i = 0; i < argc; i++) {
  644. err = cmap_delete(handle, argv[i]);
  645. if (err != CS_OK) {
  646. fprintf(stderr, "Can't delete key %s. Error %s\n", argv[i], cs_strerror(err));
  647. }
  648. }
  649. break;
  650. case ACTION_TRACK:
  651. for (i = 0; i < argc; i++) {
  652. add_track(handle, argv[i], track_prefix);
  653. }
  654. track_changes(handle);
  655. break;
  656. case ACTION_SET:
  657. if (argc < 3) {
  658. fprintf(stderr, "At least 3 parameters are expected for set\n");
  659. return (EXIT_FAILURE);
  660. }
  661. set_key(handle, argv[0], argv[1], argv[2]);
  662. break;
  663. }
  664. err = cmap_finalize(handle);
  665. if (err != CS_OK) {
  666. fprintf (stderr, "Failed to finalize the cmap API. Error %s\n", cs_strerror(err));
  667. exit (EXIT_FAILURE);
  668. }
  669. return (0);
  670. }