4
0

corosync-cmapctl.c 20 KB

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