4
0

corosync-cmapctl.c 23 KB

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