4
0

corosync-cmapctl.c 22 KB

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