corosync-cmapctl.c 23 KB

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