corosync-cmapctl.c 22 KB

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