4
0

corosync-cmapctl.c 20 KB

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