corosync-cmapctl.c 21 KB

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