4
0

corosync-cmapctl.c 20 KB

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