corosync-cmapctl.c 23 KB

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