corosync-cmapctl.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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 <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. }
  347. static void delete_with_prefix(cmap_handle_t handle, const char *prefix)
  348. {
  349. cmap_iter_handle_t iter_handle;
  350. char key_name[CMAP_KEYNAME_MAXLEN + 1];
  351. size_t value_len;
  352. cmap_value_types_t type;
  353. cs_error_t err;
  354. cs_error_t err2;
  355. err = cmap_iter_init(handle, prefix, &iter_handle);
  356. if (err != CS_OK) {
  357. fprintf (stderr, "Failed to initialize iteration. Error %s\n", cs_strerror(err));
  358. exit (EXIT_FAILURE);
  359. }
  360. while ((err = cmap_iter_next(handle, iter_handle, key_name, &value_len, &type)) == CS_OK) {
  361. err2 = cmap_delete(handle, key_name);
  362. if (err2 != CS_OK) {
  363. fprintf(stderr, "Can't delete key %s. Error %s\n", key_name, cs_strerror(err2));
  364. }
  365. }
  366. }
  367. static void cmap_notify_fn(
  368. cmap_handle_t cmap_handle,
  369. cmap_track_handle_t cmap_track_handle,
  370. int32_t event,
  371. const char *key_name,
  372. struct cmap_notify_value new_val,
  373. struct cmap_notify_value old_val,
  374. void *user_data)
  375. {
  376. switch (event) {
  377. case CMAP_TRACK_ADD:
  378. printf("create> ");
  379. print_key(cmap_handle, key_name, new_val.len, new_val.data, new_val.type);
  380. break;
  381. case CMAP_TRACK_DELETE:
  382. printf("delete> ");
  383. print_key(cmap_handle, key_name, old_val.len, old_val.data, old_val.type);
  384. break;
  385. case CMAP_TRACK_MODIFY:
  386. printf("modify> ");
  387. print_key(cmap_handle, key_name, new_val.len, new_val.data, new_val.type);
  388. break;
  389. default:
  390. printf("unknown change> ");
  391. break;
  392. }
  393. }
  394. static void add_track(cmap_handle_t handle, const char *key_name, int prefix)
  395. {
  396. cmap_track_handle_t track_handle;
  397. int32_t track_type;
  398. cs_error_t err;
  399. track_type = CMAP_TRACK_ADD | CMAP_TRACK_DELETE | CMAP_TRACK_MODIFY;
  400. if (prefix) {
  401. track_type |= CMAP_TRACK_PREFIX;
  402. }
  403. err = cmap_track_add(handle, key_name, track_type, cmap_notify_fn, NULL, &track_handle);
  404. if (err != CS_OK) {
  405. fprintf(stderr, "Failed to add tracking function. Error %s\n", cs_strerror(err));
  406. exit (EXIT_FAILURE);
  407. }
  408. }
  409. static void track_changes(cmap_handle_t handle)
  410. {
  411. struct pollfd pfd[2];
  412. int cmap_fd;
  413. cs_error_t err;
  414. int poll_res;
  415. char inbuf[3];
  416. int quit = CS_FALSE;
  417. err = cmap_fd_get(handle, &cmap_fd);
  418. if (err != CS_OK) {
  419. fprintf(stderr, "Failed to get file handle. Error %s\n", cs_strerror(err));
  420. exit (EXIT_FAILURE);
  421. }
  422. pfd[0].fd = cmap_fd;
  423. pfd[1].fd = STDIN_FILENO;
  424. pfd[0].events = pfd[1].events = POLLIN;
  425. printf("Type \"q\" to finish\n");
  426. do {
  427. pfd[0].revents = pfd[1].revents = 0;
  428. poll_res = poll(pfd, 2, INFTIM);
  429. if (poll_res == -1) {
  430. perror("poll");
  431. }
  432. if (pfd[1].revents & POLLIN) {
  433. if (fgets(inbuf, sizeof(inbuf), stdin) == NULL) {
  434. quit = CS_TRUE;
  435. } else if (strncmp(inbuf, "q", 1) == 0) {
  436. quit = CS_TRUE;
  437. }
  438. }
  439. if (pfd[0].revents & POLLIN) {
  440. err = cmap_dispatch(handle, CS_DISPATCH_ALL);
  441. if (err != CS_OK) {
  442. fprintf(stderr, "Dispatch error %s\n", cs_strerror(err));
  443. quit = CS_TRUE;
  444. }
  445. }
  446. } while (poll_res > 0 && !quit);
  447. }
  448. static cs_error_t set_key_bin(cmap_handle_t handle, const char *key_name, const char *fname)
  449. {
  450. FILE *f;
  451. char *val;
  452. char buf[4096];
  453. size_t size;
  454. size_t readed;
  455. size_t pos;
  456. cs_error_t err;
  457. if (strcmp(fname, "-") == 0) {
  458. f = stdin;
  459. } else {
  460. f = fopen(fname, "rb");
  461. if (f == NULL) {
  462. perror("Can't open input file");
  463. exit(EXIT_FAILURE);
  464. }
  465. }
  466. val = NULL;
  467. size = 0;
  468. pos = 0;
  469. while ((readed = fread(buf, 1, sizeof(buf), f)) != 0) {
  470. size += readed;
  471. if ((val = realloc(val, size)) == NULL) {
  472. fprintf(stderr, "Can't alloc memory\n");
  473. exit (EXIT_FAILURE);
  474. }
  475. memcpy(val + pos, buf, readed);
  476. pos += readed;
  477. }
  478. if (f != stdin) {
  479. fclose(f);
  480. }
  481. err = cmap_set(handle, key_name, val, size, CMAP_VALUETYPE_BINARY);
  482. free(val);
  483. return (err);
  484. }
  485. static void set_key(cmap_handle_t handle, const char *key_name, const char *key_type_s, const char *key_value_s)
  486. {
  487. int64_t i64;
  488. uint64_t u64;
  489. double dbl;
  490. float flt;
  491. cs_error_t err = CS_OK;
  492. int scanf_res = 0;
  493. cmap_value_types_t type;
  494. if (convert_name_to_type(key_type_s) == -1) {
  495. fprintf(stderr, "Unknown type %s\n", key_type_s);
  496. exit (EXIT_FAILURE);
  497. }
  498. type = convert_name_to_type(key_type_s);
  499. switch (type) {
  500. case CMAP_VALUETYPE_INT8:
  501. case CMAP_VALUETYPE_INT16:
  502. case CMAP_VALUETYPE_INT32:
  503. case CMAP_VALUETYPE_INT64:
  504. scanf_res = sscanf(key_value_s, "%"PRId64, &i64);
  505. break;
  506. case CMAP_VALUETYPE_UINT8:
  507. case CMAP_VALUETYPE_UINT16:
  508. case CMAP_VALUETYPE_UINT32:
  509. case CMAP_VALUETYPE_UINT64:
  510. scanf_res = sscanf(key_value_s, "%"PRIu64, &u64);
  511. break;
  512. case CMAP_VALUETYPE_FLOAT:
  513. scanf_res = sscanf(key_value_s, "%f", &flt);
  514. break;
  515. case CMAP_VALUETYPE_DOUBLE:
  516. scanf_res = sscanf(key_value_s, "%lf", &dbl);
  517. break;
  518. case CMAP_VALUETYPE_STRING:
  519. case CMAP_VALUETYPE_BINARY:
  520. /*
  521. * Do nothing
  522. */
  523. scanf_res = 1;
  524. break;
  525. }
  526. if (scanf_res != 1) {
  527. fprintf(stderr, "%s is not valid %s type value\n", key_value_s, key_type_s);
  528. exit(EXIT_FAILURE);
  529. }
  530. /*
  531. * We have parsed value, so insert value
  532. */
  533. switch (type) {
  534. case CMAP_VALUETYPE_INT8:
  535. if (i64 > INT8_MAX || i64 < INT8_MIN) {
  536. fprintf(stderr, "%s is not valid i8 integer\n", key_value_s);
  537. exit(EXIT_FAILURE);
  538. }
  539. err = cmap_set_int8(handle, key_name, i64);
  540. break;
  541. case CMAP_VALUETYPE_INT16:
  542. if (i64 > INT16_MAX || i64 < INT16_MIN) {
  543. fprintf(stderr, "%s is not valid i16 integer\n", key_value_s);
  544. exit(EXIT_FAILURE);
  545. }
  546. err = cmap_set_int16(handle, key_name, i64);
  547. break;
  548. case CMAP_VALUETYPE_INT32:
  549. if (i64 > INT32_MAX || i64 < INT32_MIN) {
  550. fprintf(stderr, "%s is not valid i32 integer\n", key_value_s);
  551. exit(EXIT_FAILURE);
  552. }
  553. err = cmap_set_int32(handle, key_name, i64);
  554. break;
  555. case CMAP_VALUETYPE_INT64:
  556. err = cmap_set_int64(handle, key_name, i64);
  557. break;
  558. case CMAP_VALUETYPE_UINT8:
  559. if (u64 > UINT8_MAX) {
  560. fprintf(stderr, "%s is not valid u8 integer\n", key_value_s);
  561. exit(EXIT_FAILURE);
  562. }
  563. err = cmap_set_uint8(handle, key_name, u64);
  564. break;
  565. case CMAP_VALUETYPE_UINT16:
  566. if (u64 > UINT16_MAX) {
  567. fprintf(stderr, "%s is not valid u16 integer\n", key_value_s);
  568. exit(EXIT_FAILURE);
  569. }
  570. err = cmap_set_uint16(handle, key_name, u64);
  571. break;
  572. case CMAP_VALUETYPE_UINT32:
  573. if (u64 > UINT32_MAX) {
  574. fprintf(stderr, "%s is not valid u32 integer\n", key_value_s);
  575. exit(EXIT_FAILURE);
  576. }
  577. err = cmap_set_uint32(handle, key_name, u64);
  578. break;
  579. case CMAP_VALUETYPE_UINT64:
  580. err = cmap_set_uint64(handle, key_name, u64);
  581. break;
  582. case CMAP_VALUETYPE_FLOAT:
  583. err = cmap_set_float(handle, key_name, flt);
  584. break;
  585. case CMAP_VALUETYPE_DOUBLE:
  586. err = cmap_set_double(handle, key_name, dbl);
  587. break;
  588. case CMAP_VALUETYPE_STRING:
  589. err = cmap_set_string(handle, key_name, key_value_s);
  590. break;
  591. case CMAP_VALUETYPE_BINARY:
  592. err = set_key_bin(handle, key_name, key_value_s);
  593. break;
  594. }
  595. if (err != CS_OK) {
  596. fprintf (stderr, "Failed to set key %s. Error %s\n", key_name, cs_strerror(err));
  597. exit (EXIT_FAILURE);
  598. }
  599. }
  600. static void read_in_config_file(cmap_handle_t handle, char * filename)
  601. {
  602. int ignore;
  603. int c;
  604. FILE* fh;
  605. char buf[1024];
  606. char * line;
  607. char *key_name;
  608. char *key_type_s;
  609. char *key_value_s;
  610. if (access (filename, R_OK) != 0) {
  611. perror ("Couldn't access file.");
  612. return;
  613. }
  614. fh = fopen(filename, "r");
  615. if (fh == NULL) {
  616. perror ("Couldn't open file.");
  617. return;
  618. }
  619. while (fgets (buf, 1024, fh) != NULL) {
  620. /* find the first real character, if it is
  621. * a '#' then ignore this line.
  622. * else process.
  623. * if no real characters then also ignore.
  624. */
  625. ignore = 1;
  626. for (c = 0; c < 1024; c++) {
  627. if (isblank (buf[c])) {
  628. continue;
  629. }
  630. if (buf[c] == '#' || buf[c] == '\n') {
  631. ignore = 1;
  632. break;
  633. }
  634. ignore = 0;
  635. line = &buf[c];
  636. break;
  637. }
  638. if (ignore == 1) {
  639. continue;
  640. }
  641. /*
  642. * should be:
  643. * <key> <type> <value>
  644. */
  645. key_name = strtok(line, " \n");
  646. key_type_s = strtok(NULL, " \n");
  647. key_value_s = strtok(NULL, " \n");
  648. set_key(handle, key_name, key_type_s, key_value_s);
  649. }
  650. fclose (fh);
  651. }
  652. int main(int argc, char *argv[])
  653. {
  654. enum user_action action;
  655. int c;
  656. cs_error_t err;
  657. cmap_handle_t handle;
  658. int i;
  659. size_t value_len;
  660. cmap_value_types_t type;
  661. int track_prefix;
  662. int no_retries;
  663. char * settings_file = NULL;
  664. action = ACTION_PRINT_PREFIX;
  665. track_prefix = 1;
  666. while ((c = getopt(argc, argv, "hgsdtTbp:")) != -1) {
  667. switch (c) {
  668. case 'h':
  669. return print_help();
  670. break;
  671. case 'b':
  672. show_binary++;
  673. break;
  674. case 'g':
  675. action = ACTION_GET;
  676. break;
  677. case 's':
  678. action = ACTION_SET;
  679. break;
  680. case 'd':
  681. action = ACTION_DELETE;
  682. break;
  683. case 'D':
  684. action = ACTION_DELETE_PREFIX;
  685. break;
  686. case 'p':
  687. settings_file = optarg;
  688. action = ACTION_LOAD;
  689. break;
  690. case 't':
  691. action = ACTION_TRACK;
  692. track_prefix = 0;
  693. break;
  694. case 'T':
  695. action = ACTION_TRACK;
  696. break;
  697. case '?':
  698. return (EXIT_FAILURE);
  699. break;
  700. default:
  701. action = ACTION_PRINT_PREFIX;
  702. break;
  703. }
  704. }
  705. if (argc == 1 || (argc == 2 && show_binary)) {
  706. action = ACTION_PRINT_ALL;
  707. }
  708. argc -= optind;
  709. argv += optind;
  710. if (argc == 0 &&
  711. action != ACTION_LOAD &&
  712. action != ACTION_PRINT_ALL) {
  713. fprintf(stderr, "Expected key after options\n");
  714. return (EXIT_FAILURE);
  715. }
  716. no_retries = 0;
  717. while ((err = cmap_initialize(&handle)) == CS_ERR_TRY_AGAIN && no_retries++ < MAX_TRY_AGAIN) {
  718. sleep(1);
  719. }
  720. if (err != CS_OK) {
  721. fprintf (stderr, "Failed to initialize the cmap API. Error %s\n", cs_strerror(err));
  722. exit (EXIT_FAILURE);
  723. }
  724. switch (action) {
  725. case ACTION_PRINT_ALL:
  726. print_iter(handle, NULL);
  727. break;
  728. case ACTION_PRINT_PREFIX:
  729. for (i = 0; i < argc; i++) {
  730. print_iter(handle, argv[i]);
  731. }
  732. break;
  733. case ACTION_GET:
  734. for (i = 0; i < argc; i++) {
  735. err = cmap_get(handle, argv[i], NULL, &value_len, &type);
  736. if (err == CS_OK) {
  737. print_key(handle, argv[i], value_len, NULL, type);
  738. } else {
  739. fprintf(stderr, "Can't get key %s. Error %s\n", argv[i], cs_strerror(err));
  740. }
  741. }
  742. break;
  743. case ACTION_DELETE:
  744. for (i = 0; i < argc; i++) {
  745. err = cmap_delete(handle, argv[i]);
  746. if (err != CS_OK) {
  747. fprintf(stderr, "Can't delete key %s. Error %s\n", argv[i], cs_strerror(err));
  748. }
  749. }
  750. break;
  751. case ACTION_DELETE_PREFIX:
  752. for (i = 0; i < argc; i++) {
  753. delete_with_prefix(handle, argv[i]);
  754. }
  755. break;
  756. case ACTION_LOAD:
  757. read_in_config_file(handle, settings_file);
  758. break;
  759. case ACTION_TRACK:
  760. for (i = 0; i < argc; i++) {
  761. add_track(handle, argv[i], track_prefix);
  762. }
  763. track_changes(handle);
  764. break;
  765. case ACTION_SET:
  766. if (argc < 3) {
  767. fprintf(stderr, "At least 3 parameters are expected for set\n");
  768. return (EXIT_FAILURE);
  769. }
  770. set_key(handle, argv[0], argv[1], argv[2]);
  771. break;
  772. }
  773. err = cmap_finalize(handle);
  774. if (err != CS_OK) {
  775. fprintf (stderr, "Failed to finalize the cmap API. Error %s\n", cs_strerror(err));
  776. exit (EXIT_FAILURE);
  777. }
  778. return (0);
  779. }