corosync-cmapctl.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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 [-b] [-adghsTt] [params...]\n");
  85. printf("Set key:\n");
  86. printf(" corosync-cmapctl -s key_name type value\n");
  87. printf("\n");
  88. printf(" where type is one of ([i|u][8|16|32|64] | flt | dbl | str | bin)\n");
  89. printf(" for bin, value is file name (or - for stdin)\n");
  90. printf("\n");
  91. printf("Delete key:\n");
  92. printf(" corosync-cmapctl -d key_name...\n");
  93. printf("\n");
  94. printf("Get key:\n");
  95. printf(" corosync-cmapctl [-b] -g key_name...\n");
  96. printf("\n");
  97. printf("Display all keys:\n");
  98. printf(" corosync-cmapctl [-b] -a\n");
  99. printf("\n");
  100. printf("Display keys with prefix key_name:\n");
  101. printf(" corosync-cmapctl [-b] key_name...\n");
  102. printf("\n");
  103. printf("Track changes on keys with prefix key_name:\n");
  104. printf(" corosync-cmapctl [-b] -t key_name\n");
  105. printf("\n");
  106. printf("Track changes on keys with key_name name:\n");
  107. printf(" corosync-cmapctl [-b] -T key_name\n");
  108. printf("\n");
  109. return (0);
  110. }
  111. static void print_binary_key (char *value, size_t value_len)
  112. {
  113. size_t i;
  114. char c;
  115. for (i = 0; i < value_len; i++) {
  116. c = value[i];
  117. if (c >= ' ' && c < 0x7f && c != '\\') {
  118. fputc (c, stdout);
  119. } else {
  120. if (c == '\\') {
  121. printf ("\\\\");
  122. } else {
  123. printf ("\\x%02X", c);
  124. }
  125. }
  126. }
  127. }
  128. static void print_key(cmap_handle_t handle,
  129. const char *key_name,
  130. size_t value_len,
  131. const void *value,
  132. cmap_value_types_t type)
  133. {
  134. char *str;
  135. char *bin_value;
  136. cs_error_t err;
  137. int8_t i8;
  138. uint8_t u8;
  139. int16_t i16;
  140. uint16_t u16;
  141. int32_t i32;
  142. uint32_t u32;
  143. int64_t i64;
  144. uint64_t u64;
  145. float flt;
  146. double dbl;
  147. int end_loop;
  148. int no_retries;
  149. size_t bin_value_len;
  150. end_loop = 0;
  151. no_retries = 0;
  152. err = CS_OK;
  153. while (!end_loop) {
  154. switch (type) {
  155. case CMAP_VALUETYPE_INT8:
  156. if (value == NULL) {
  157. err = cmap_get_int8(handle, key_name, &i8);
  158. } else {
  159. i8 = *((int8_t *)value);
  160. }
  161. break;
  162. case CMAP_VALUETYPE_INT16:
  163. if (value == NULL) {
  164. err = cmap_get_int16(handle, key_name, &i16);
  165. } else {
  166. i16 = *((int16_t *)value);
  167. }
  168. break;
  169. case CMAP_VALUETYPE_INT32:
  170. if (value == NULL) {
  171. err = cmap_get_int32(handle, key_name, &i32);
  172. } else {
  173. i32 = *((int32_t *)value);
  174. }
  175. break;
  176. case CMAP_VALUETYPE_INT64:
  177. if (value == NULL) {
  178. err = cmap_get_int64(handle, key_name, &i64);
  179. } else {
  180. i64 = *((int64_t *)value);
  181. }
  182. break;
  183. case CMAP_VALUETYPE_UINT8:
  184. if (value == NULL) {
  185. err = cmap_get_uint8(handle, key_name, &u8);
  186. } else {
  187. u8 = *((uint8_t *)value);
  188. }
  189. break;
  190. case CMAP_VALUETYPE_UINT16:
  191. if (value == NULL) {
  192. err = cmap_get_uint16(handle, key_name, &u16);
  193. } else {
  194. u16 = *((uint16_t *)value);
  195. }
  196. break;
  197. case CMAP_VALUETYPE_UINT32:
  198. if (value == NULL) {
  199. err = cmap_get_uint32(handle, key_name, &u32);
  200. } else {
  201. u32 = *((uint32_t *)value);
  202. }
  203. break;
  204. case CMAP_VALUETYPE_UINT64:
  205. if (value == NULL) {
  206. err = cmap_get_uint64(handle, key_name, &u64);
  207. } else {
  208. u64 = *((uint64_t *)value);
  209. }
  210. break;
  211. case CMAP_VALUETYPE_FLOAT:
  212. if (value == NULL) {
  213. err = cmap_get_float(handle, key_name, &flt);
  214. } else {
  215. flt = *((float *)value);
  216. }
  217. break;
  218. case CMAP_VALUETYPE_DOUBLE:
  219. if (value == NULL) {
  220. err = cmap_get_double(handle, key_name, &dbl);
  221. } else {
  222. dbl = *((double *)value);
  223. }
  224. break;
  225. case CMAP_VALUETYPE_STRING:
  226. if (value == NULL) {
  227. err = cmap_get_string(handle, key_name, &str);
  228. } else {
  229. str = (char *)value;
  230. }
  231. break;
  232. case CMAP_VALUETYPE_BINARY:
  233. if (show_binary) {
  234. if (value == NULL) {
  235. bin_value = malloc(value_len);
  236. if (bin_value == NULL) {
  237. fprintf(stderr, "Can't alloc memory\n");
  238. exit(EXIT_FAILURE);
  239. }
  240. bin_value_len = value_len;
  241. err = cmap_get(handle, key_name, bin_value, &bin_value_len, NULL);
  242. } else {
  243. bin_value = (char *)value;
  244. }
  245. }
  246. break;
  247. }
  248. if (err == CS_OK)
  249. end_loop = 1;
  250. if (err == CS_ERR_TRY_AGAIN) {
  251. sleep(1);
  252. no_retries++;
  253. }
  254. if (no_retries > MAX_TRY_AGAIN) {
  255. end_loop = 1;
  256. }
  257. };
  258. if (err != CS_OK) {
  259. fprintf(stderr, "Can't get value of %s. Error %s\n", key_name, cs_strerror(err));
  260. return ;
  261. }
  262. printf("%s (", key_name);
  263. switch (type) {
  264. case CMAP_VALUETYPE_INT8:
  265. printf("%s) = %"PRId8, "i8", i8);
  266. break;
  267. case CMAP_VALUETYPE_UINT8:
  268. printf("%s) = %"PRIu8, "u8", u8);
  269. break;
  270. case CMAP_VALUETYPE_INT16:
  271. printf("%s) = %"PRId16, "i16", i16);
  272. break;
  273. case CMAP_VALUETYPE_UINT16:
  274. printf("%s) = %"PRIu16, "u16", u16);
  275. break;
  276. case CMAP_VALUETYPE_INT32:
  277. printf("%s) = %"PRId32, "i32", i32);
  278. break;
  279. case CMAP_VALUETYPE_UINT32:
  280. printf("%s) = %"PRIu32, "u32", u32);
  281. break;
  282. case CMAP_VALUETYPE_INT64:
  283. printf("%s) = %"PRId64, "i64", i64);
  284. break;
  285. case CMAP_VALUETYPE_UINT64:
  286. printf("%s) = %"PRIu64, "u64", u64);
  287. break;
  288. case CMAP_VALUETYPE_FLOAT:
  289. printf("%s) = %f", "flt", flt);
  290. break;
  291. case CMAP_VALUETYPE_DOUBLE:
  292. printf("%s) = %lf", "dbl", dbl);
  293. break;
  294. case CMAP_VALUETYPE_STRING:
  295. printf("%s) = %s", "str", str);
  296. if (value == NULL) {
  297. free(str);
  298. }
  299. break;
  300. case CMAP_VALUETYPE_BINARY:
  301. printf("%s)", "bin");
  302. if (show_binary) {
  303. printf(" = ");
  304. print_binary_key(bin_value, value_len);
  305. if (value == NULL) {
  306. free(bin_value);
  307. }
  308. }
  309. break;
  310. }
  311. printf("\n");
  312. }
  313. static void print_iter(enum user_action action, cmap_handle_t handle, const char *prefix)
  314. {
  315. cmap_iter_handle_t iter_handle;
  316. char key_name[CMAP_KEYNAME_MAXLEN + 1];
  317. size_t value_len;
  318. cmap_value_types_t type;
  319. cs_error_t err;
  320. err = cmap_iter_init(handle, prefix, &iter_handle);
  321. if (err != CS_OK) {
  322. fprintf (stderr, "Failed to initialize iteration. Error %s\n", cs_strerror(err));
  323. exit (EXIT_FAILURE);
  324. }
  325. while ((err = cmap_iter_next(handle, iter_handle, key_name, &value_len, &type)) == CS_OK) {
  326. if (action == ACTION_PRINT_DEFAULT &&
  327. strncmp(key_name, "internal_configuration", strlen("internal_configuration") == 0))
  328. continue;
  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;
  457. int scanf_res;
  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, "hagsdtTb")) != -1) {
  579. switch (c) {
  580. case 'h':
  581. return print_help();
  582. break;
  583. case 'a':
  584. action = ACTION_PRINT_ALL;
  585. break;
  586. case 'b':
  587. show_binary++;
  588. break;
  589. case 'g':
  590. action = ACTION_GET;
  591. break;
  592. case 's':
  593. action = ACTION_SET;
  594. break;
  595. case 'd':
  596. action = ACTION_DELETE;
  597. break;
  598. case 't':
  599. action = ACTION_TRACK;
  600. break;
  601. case 'T':
  602. action = ACTION_TRACK;
  603. track_prefix = 0;
  604. break;
  605. case '?':
  606. return (EXIT_FAILURE);
  607. break;
  608. default:
  609. action = ACTION_PRINT_PREFIX;
  610. break;
  611. }
  612. }
  613. if (argc == 1 || (argc == 2 && show_binary)) {
  614. action = ACTION_PRINT_DEFAULT;
  615. }
  616. argc -= optind;
  617. argv += optind;
  618. if (argc == 0 && action != ACTION_PRINT_DEFAULT && action != ACTION_PRINT_ALL) {
  619. fprintf(stderr, "Expected key after options\n");
  620. return (EXIT_FAILURE);
  621. }
  622. no_retries = 0;
  623. while ((err = cmap_initialize(&handle)) == CS_ERR_TRY_AGAIN && no_retries++ < MAX_TRY_AGAIN) {
  624. sleep(1);
  625. }
  626. if (err != CS_OK) {
  627. fprintf (stderr, "Failed to initialize the cmap API. Error %s\n", cs_strerror(err));
  628. exit (EXIT_FAILURE);
  629. }
  630. switch (action) {
  631. case ACTION_PRINT_DEFAULT:
  632. case ACTION_PRINT_ALL:
  633. print_iter(action, handle, NULL);
  634. break;
  635. case ACTION_PRINT_PREFIX:
  636. for (i = 0; i < argc; i++) {
  637. print_iter(action, handle, argv[i]);
  638. }
  639. break;
  640. case ACTION_GET:
  641. for (i = 0; i < argc; i++) {
  642. err = cmap_get(handle, argv[i], NULL, &value_len, &type);
  643. if (err == CS_OK) {
  644. print_key(handle, argv[i], value_len, NULL, type);
  645. } else {
  646. fprintf(stderr, "Can't get key %s. Error %s\n", argv[i], cs_strerror(err));
  647. }
  648. }
  649. break;
  650. case ACTION_DELETE:
  651. for (i = 0; i < argc; i++) {
  652. err = cmap_delete(handle, argv[i]);
  653. if (err != CS_OK) {
  654. fprintf(stderr, "Can't delete key %s. Error %s\n", argv[i], cs_strerror(err));
  655. }
  656. }
  657. break;
  658. case ACTION_TRACK:
  659. for (i = 0; i < argc; i++) {
  660. add_track(handle, argv[i], track_prefix);
  661. }
  662. track_changes(handle);
  663. break;
  664. case ACTION_SET:
  665. if (argc < 3) {
  666. fprintf(stderr, "At least 3 parameters are expected for set\n");
  667. return (EXIT_FAILURE);
  668. }
  669. set_key(handle, argv[0], argv[1], argv[2]);
  670. break;
  671. }
  672. err = cmap_finalize(handle);
  673. if (err != CS_OK) {
  674. fprintf (stderr, "Failed to finalize the cmap API. Error %s\n", cs_strerror(err));
  675. exit (EXIT_FAILURE);
  676. }
  677. return (0);
  678. }