corosync-cmapctl.c 22 KB

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