corosync-cmapctl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /*
  2. * Copyright (c) 2011 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 <stdio.h>
  36. #include <poll.h>
  37. #include <corosync/corotypes.h>
  38. #include <corosync/cmap.h>
  39. #include "../lib/util.h"
  40. #ifndef INFTIM
  41. #define INFTIM -1
  42. #endif
  43. #define MAX_TRY_AGAIN 10
  44. enum user_action {
  45. ACTION_GET,
  46. ACTION_SET,
  47. ACTION_DELETE,
  48. ACTION_PRINT_ALL,
  49. ACTION_PRINT_DEFAULT,
  50. ACTION_PRINT_PREFIX,
  51. ACTION_TRACK
  52. };
  53. struct name_to_type_item {
  54. const char *name;
  55. cmap_value_types_t type;
  56. };
  57. struct name_to_type_item name_to_type[] = {
  58. {"i8", CMAP_VALUETYPE_INT8},
  59. {"u8", CMAP_VALUETYPE_UINT8},
  60. {"i16", CMAP_VALUETYPE_INT16},
  61. {"u16", CMAP_VALUETYPE_UINT16},
  62. {"i32", CMAP_VALUETYPE_INT32},
  63. {"u32", CMAP_VALUETYPE_UINT32},
  64. {"i64", CMAP_VALUETYPE_INT64},
  65. {"u64", CMAP_VALUETYPE_UINT64},
  66. {"flt", CMAP_VALUETYPE_FLOAT},
  67. {"dbl", CMAP_VALUETYPE_DOUBLE},
  68. {"str", CMAP_VALUETYPE_STRING},
  69. {"bin", CMAP_VALUETYPE_BINARY}};
  70. static int convert_name_to_type(const char *name)
  71. {
  72. int i;
  73. for (i = 0; i < sizeof(name_to_type) / sizeof(*name_to_type); i++) {
  74. if (strcmp(name, name_to_type[i].name) == 0) {
  75. return (name_to_type[i].type);
  76. }
  77. }
  78. return (-1);
  79. }
  80. static int print_help(void)
  81. {
  82. printf("\n");
  83. printf("usage: corosync-cmapctl ");
  84. printf("\n");
  85. return (0);
  86. }
  87. static void print_key(cmap_handle_t handle,
  88. const char *key_name,
  89. size_t value_len,
  90. const void *value,
  91. cmap_value_types_t type)
  92. {
  93. char *str;
  94. cs_error_t err;
  95. int8_t i8;
  96. uint8_t u8;
  97. int16_t i16;
  98. uint16_t u16;
  99. int32_t i32;
  100. uint32_t u32;
  101. int64_t i64;
  102. uint64_t u64;
  103. float flt;
  104. double dbl;
  105. int end_loop;
  106. int no_retries;
  107. end_loop = 0;
  108. no_retries = 0;
  109. err = CS_OK;
  110. while (!end_loop) {
  111. switch (type) {
  112. case CMAP_VALUETYPE_INT8:
  113. if (value == NULL) {
  114. err = cmap_get_int8(handle, key_name, &i8);
  115. } else {
  116. i8 = *((int8_t *)value);
  117. }
  118. break;
  119. case CMAP_VALUETYPE_INT16:
  120. if (value == NULL) {
  121. err = cmap_get_int16(handle, key_name, &i16);
  122. } else {
  123. i16 = *((int16_t *)value);
  124. }
  125. break;
  126. case CMAP_VALUETYPE_INT32:
  127. if (value == NULL) {
  128. err = cmap_get_int32(handle, key_name, &i32);
  129. } else {
  130. i32 = *((int32_t *)value);
  131. }
  132. break;
  133. case CMAP_VALUETYPE_INT64:
  134. if (value == NULL) {
  135. err = cmap_get_int64(handle, key_name, &i64);
  136. } else {
  137. i64 = *((int64_t *)value);
  138. }
  139. break;
  140. case CMAP_VALUETYPE_UINT8:
  141. if (value == NULL) {
  142. err = cmap_get_uint8(handle, key_name, &u8);
  143. } else {
  144. u8 = *((uint8_t *)value);
  145. }
  146. break;
  147. case CMAP_VALUETYPE_UINT16:
  148. if (value == NULL) {
  149. err = cmap_get_uint16(handle, key_name, &u16);
  150. } else {
  151. u16 = *((uint16_t *)value);
  152. }
  153. break;
  154. case CMAP_VALUETYPE_UINT32:
  155. if (value == NULL) {
  156. err = cmap_get_uint32(handle, key_name, &u32);
  157. } else {
  158. u32 = *((uint32_t *)value);
  159. }
  160. break;
  161. case CMAP_VALUETYPE_UINT64:
  162. if (value == NULL) {
  163. err = cmap_get_uint64(handle, key_name, &u64);
  164. } else {
  165. u64 = *((uint64_t *)value);
  166. }
  167. break;
  168. case CMAP_VALUETYPE_FLOAT:
  169. if (value == NULL) {
  170. err = cmap_get_float(handle, key_name, &flt);
  171. } else {
  172. flt = *((float *)value);
  173. }
  174. break;
  175. case CMAP_VALUETYPE_DOUBLE:
  176. if (value == NULL) {
  177. err = cmap_get_double(handle, key_name, &dbl);
  178. } else {
  179. dbl = *((double *)value);
  180. }
  181. break;
  182. case CMAP_VALUETYPE_STRING:
  183. if (value == NULL) {
  184. err = cmap_get_string(handle, key_name, &str);
  185. } else {
  186. str = (char *)value;
  187. }
  188. break;
  189. case CMAP_VALUETYPE_BINARY: break;
  190. }
  191. if (err == CS_OK)
  192. end_loop = 1;
  193. if (err == CS_ERR_TRY_AGAIN) {
  194. sleep(1);
  195. no_retries++;
  196. }
  197. if (no_retries > MAX_TRY_AGAIN) {
  198. end_loop = 1;
  199. }
  200. };
  201. if (err != CS_OK) {
  202. fprintf(stderr, "Can't get value of %s. Error %s\n", key_name, cs_strerror(err));
  203. return ;
  204. }
  205. printf("%s (", key_name);
  206. switch (type) {
  207. case CMAP_VALUETYPE_INT8:
  208. printf("%s) = %"PRId8, "i8", i8);
  209. break;
  210. case CMAP_VALUETYPE_UINT8:
  211. printf("%s) = %"PRIu8, "u8", u8);
  212. break;
  213. case CMAP_VALUETYPE_INT16:
  214. printf("%s) = %"PRId16, "i16", i16);
  215. break;
  216. case CMAP_VALUETYPE_UINT16:
  217. printf("%s) = %"PRIu16, "u16", u16);
  218. break;
  219. case CMAP_VALUETYPE_INT32:
  220. printf("%s) = %"PRId32, "i32", i32);
  221. break;
  222. case CMAP_VALUETYPE_UINT32:
  223. printf("%s) = %"PRIu32, "u32", u32);
  224. break;
  225. case CMAP_VALUETYPE_INT64:
  226. printf("%s) = %"PRId64, "i64", i64);
  227. break;
  228. case CMAP_VALUETYPE_UINT64:
  229. printf("%s) = %"PRIu64, "u64", u64);
  230. break;
  231. case CMAP_VALUETYPE_FLOAT:
  232. printf("%s) = %f", "flt", flt);
  233. break;
  234. case CMAP_VALUETYPE_DOUBLE:
  235. printf("%s) = %lf", "dbl", dbl);
  236. break;
  237. case CMAP_VALUETYPE_STRING:
  238. printf("%s) = %s", "str", str);
  239. if (value == NULL) {
  240. free(str);
  241. }
  242. break;
  243. case CMAP_VALUETYPE_BINARY:
  244. printf("%s)", "bin");
  245. break;
  246. }
  247. printf("\n");
  248. }
  249. static void print_iter(enum user_action action, cmap_handle_t handle, const char *prefix)
  250. {
  251. cmap_iter_handle_t iter_handle;
  252. char key_name[CMAP_KEYNAME_MAXLEN + 1];
  253. size_t value_len;
  254. cmap_value_types_t type;
  255. cs_error_t err;
  256. err = cmap_iter_init(handle, prefix, &iter_handle);
  257. if (err != CS_OK) {
  258. fprintf (stderr, "Failed to initialize iteration. Error %s\n", cs_strerror(err));
  259. exit (EXIT_FAILURE);
  260. }
  261. while ((err = cmap_iter_next(handle, iter_handle, key_name, &value_len, &type)) == CS_OK) {
  262. if (action == ACTION_PRINT_DEFAULT &&
  263. strncmp(key_name, "internal_configuration", strlen("internal_configuration") == 0))
  264. continue;
  265. print_key(handle, key_name, value_len, NULL, type);
  266. }
  267. }
  268. static void cmap_notify_fn(
  269. cmap_handle_t cmap_handle,
  270. cmap_track_handle_t cmap_track_handle,
  271. int32_t event,
  272. const char *key_name,
  273. struct cmap_notify_value new_val,
  274. struct cmap_notify_value old_val,
  275. void *user_data)
  276. {
  277. switch (event) {
  278. case CMAP_TRACK_ADD:
  279. printf("create> ");
  280. print_key(cmap_handle, key_name, new_val.len, new_val.data, new_val.type);
  281. break;
  282. case CMAP_TRACK_DELETE:
  283. printf("delete> ");
  284. print_key(cmap_handle, key_name, old_val.len, old_val.data, old_val.type);
  285. break;
  286. case CMAP_TRACK_MODIFY:
  287. printf("modify> ");
  288. print_key(cmap_handle, key_name, new_val.len, new_val.data, new_val.type);
  289. break;
  290. default:
  291. printf("unknown change> ");
  292. break;
  293. }
  294. }
  295. static void add_track(cmap_handle_t handle, const char *key_name, int prefix)
  296. {
  297. cmap_track_handle_t track_handle;
  298. int32_t track_type;
  299. cs_error_t err;
  300. track_type = CMAP_TRACK_ADD | CMAP_TRACK_DELETE | CMAP_TRACK_MODIFY;
  301. if (prefix) {
  302. track_type |= CMAP_TRACK_PREFIX;
  303. }
  304. err = cmap_track_add(handle, key_name, track_type, cmap_notify_fn, NULL, &track_handle);
  305. if (err != CS_OK) {
  306. fprintf(stderr, "Failed to add tracking function. Error %s\n", cs_strerror(err));
  307. exit (EXIT_FAILURE);
  308. }
  309. }
  310. static void track_changes(cmap_handle_t handle)
  311. {
  312. struct pollfd pfd[2];
  313. int cmap_fd;
  314. cs_error_t err;
  315. int poll_res;
  316. char inbuf[3];
  317. int quit = CS_FALSE;
  318. err = cmap_fd_get(handle, &cmap_fd);
  319. if (err != CS_OK) {
  320. fprintf(stderr, "Failed to get file handle. Error %s\n", cs_strerror(err));
  321. exit (EXIT_FAILURE);
  322. }
  323. pfd[0].fd = cmap_fd;
  324. pfd[1].fd = STDIN_FILENO;
  325. pfd[0].events = pfd[1].events = POLLIN;
  326. printf("Type \"q\" to finish\n");
  327. do {
  328. pfd[0].revents = pfd[1].revents = 0;
  329. poll_res = poll(pfd, 2, INFTIM);
  330. if (poll_res == -1) {
  331. perror("poll");
  332. }
  333. if (pfd[1].revents & POLLIN) {
  334. if (fgets(inbuf, sizeof(inbuf), stdin) == NULL) {
  335. quit = CS_TRUE;
  336. } else if (strncmp(inbuf, "q", 1) == 0) {
  337. quit = CS_TRUE;
  338. }
  339. }
  340. if (pfd[0].revents & POLLIN) {
  341. err = cmap_dispatch(handle, CS_DISPATCH_ALL);
  342. if (err != CS_OK) {
  343. fprintf(stderr, "Dispatch error %s\n", cs_strerror(err));
  344. quit = CS_TRUE;
  345. }
  346. }
  347. } while (poll_res > 0 && !quit);
  348. }
  349. static void set_key(cmap_handle_t handle, const char *key_name, const char *key_type_s, const char *key_value_s)
  350. {
  351. int64_t i64;
  352. uint64_t u64;
  353. double dbl;
  354. float flt;
  355. cs_error_t err;
  356. int scanf_res;
  357. cmap_value_types_t type;
  358. if (convert_name_to_type(key_type_s) == -1) {
  359. fprintf(stderr, "Unknown type %s\n", key_type_s);
  360. exit (EXIT_FAILURE);
  361. }
  362. type = convert_name_to_type(key_type_s);
  363. switch (type) {
  364. case CMAP_VALUETYPE_INT8:
  365. case CMAP_VALUETYPE_INT16:
  366. case CMAP_VALUETYPE_INT32:
  367. case CMAP_VALUETYPE_INT64:
  368. scanf_res = sscanf(key_value_s, "%"PRId64, &i64);
  369. break;
  370. case CMAP_VALUETYPE_UINT8:
  371. case CMAP_VALUETYPE_UINT16:
  372. case CMAP_VALUETYPE_UINT32:
  373. case CMAP_VALUETYPE_UINT64:
  374. scanf_res = sscanf(key_value_s, "%"PRIu64, &u64);
  375. break;
  376. case CMAP_VALUETYPE_FLOAT:
  377. scanf_res = sscanf(key_value_s, "%f", &flt);
  378. break;
  379. case CMAP_VALUETYPE_DOUBLE:
  380. scanf_res = sscanf(key_value_s, "%lf", &dbl);
  381. break;
  382. case CMAP_VALUETYPE_STRING:
  383. case CMAP_VALUETYPE_BINARY:
  384. /*
  385. * Do nothing
  386. */
  387. scanf_res = 1;
  388. break;
  389. }
  390. if (scanf_res != 1) {
  391. fprintf(stderr, "%s is not valid %s type value\n", key_value_s, key_type_s);
  392. exit(EXIT_FAILURE);
  393. }
  394. /*
  395. * We have parsed value, so insert value
  396. */
  397. switch (type) {
  398. case CMAP_VALUETYPE_INT8:
  399. if (i64 > INT8_MAX || i64 < INT8_MIN) {
  400. fprintf(stderr, "%s is not valid i8 integer\n", key_value_s);
  401. exit(EXIT_FAILURE);
  402. }
  403. err = cmap_set_int8(handle, key_name, i64);
  404. break;
  405. case CMAP_VALUETYPE_INT16:
  406. if (i64 > INT16_MAX || i64 < INT16_MIN) {
  407. fprintf(stderr, "%s is not valid i16 integer\n", key_value_s);
  408. exit(EXIT_FAILURE);
  409. }
  410. err = cmap_set_int16(handle, key_name, i64);
  411. break;
  412. case CMAP_VALUETYPE_INT32:
  413. if (i64 > INT32_MAX || i64 < INT32_MIN) {
  414. fprintf(stderr, "%s is not valid i32 integer\n", key_value_s);
  415. exit(EXIT_FAILURE);
  416. }
  417. err = cmap_set_int32(handle, key_name, i64);
  418. break;
  419. case CMAP_VALUETYPE_INT64:
  420. err = cmap_set_int64(handle, key_name, i64);
  421. break;
  422. case CMAP_VALUETYPE_UINT8:
  423. if (u64 > UINT8_MAX) {
  424. fprintf(stderr, "%s is not valid u8 integer\n", key_value_s);
  425. exit(EXIT_FAILURE);
  426. }
  427. err = cmap_set_uint8(handle, key_name, u64);
  428. break;
  429. case CMAP_VALUETYPE_UINT16:
  430. if (u64 > UINT16_MAX) {
  431. fprintf(stderr, "%s is not valid u16 integer\n", key_value_s);
  432. exit(EXIT_FAILURE);
  433. }
  434. err = cmap_set_uint16(handle, key_name, u64);
  435. break;
  436. case CMAP_VALUETYPE_UINT32:
  437. if (u64 > UINT32_MAX) {
  438. fprintf(stderr, "%s is not valid u32 integer\n", key_value_s);
  439. exit(EXIT_FAILURE);
  440. }
  441. err = cmap_set_uint32(handle, key_name, u64);
  442. break;
  443. case CMAP_VALUETYPE_UINT64:
  444. err = cmap_set_uint64(handle, key_name, u64);
  445. break;
  446. case CMAP_VALUETYPE_FLOAT:
  447. err = cmap_set_float(handle, key_name, flt);
  448. break;
  449. case CMAP_VALUETYPE_DOUBLE:
  450. err = cmap_set_double(handle, key_name, dbl);
  451. break;
  452. case CMAP_VALUETYPE_STRING:
  453. err = cmap_set_string(handle, key_name, key_value_s);
  454. break;
  455. case CMAP_VALUETYPE_BINARY:
  456. exit(1);
  457. break;
  458. }
  459. if (err != CS_OK) {
  460. fprintf (stderr, "Failed to set key %s. Error %s\n", key_name, cs_strerror(err));
  461. exit (EXIT_FAILURE);
  462. }
  463. }
  464. int main(int argc, char *argv[])
  465. {
  466. enum user_action action;
  467. int c;
  468. cs_error_t err;
  469. cmap_handle_t handle;
  470. int i;
  471. size_t value_len;
  472. cmap_value_types_t type;
  473. int track_prefix;
  474. int no_retries;
  475. action = ACTION_PRINT_PREFIX;
  476. track_prefix = 1;
  477. while ((c = getopt(argc, argv, "hagsdtT")) != -1) {
  478. switch (c) {
  479. case 'h':
  480. return print_help();
  481. break;
  482. case 'a':
  483. action = ACTION_PRINT_ALL;
  484. break;
  485. case 'g':
  486. action = ACTION_GET;
  487. break;
  488. case 's':
  489. action = ACTION_SET;
  490. break;
  491. case 'd':
  492. action = ACTION_DELETE;
  493. break;
  494. case 't':
  495. action = ACTION_TRACK;
  496. break;
  497. case 'T':
  498. action = ACTION_TRACK;
  499. track_prefix = 0;
  500. break;
  501. case '?':
  502. return (EXIT_FAILURE);
  503. break;
  504. default:
  505. action = ACTION_PRINT_PREFIX;
  506. break;
  507. }
  508. }
  509. if (argc == 1) {
  510. action = ACTION_PRINT_DEFAULT;
  511. }
  512. argc -= optind;
  513. argv += optind;
  514. if (argc == 0 && action != ACTION_PRINT_DEFAULT && action != ACTION_PRINT_ALL) {
  515. fprintf(stderr, "Expected key after options\n");
  516. return (EXIT_FAILURE);
  517. }
  518. no_retries = 0;
  519. while ((err = cmap_initialize(&handle)) == CS_ERR_TRY_AGAIN && no_retries++ < MAX_TRY_AGAIN) {
  520. sleep(1);
  521. }
  522. if (err != CS_OK) {
  523. fprintf (stderr, "Failed to initialize the cmap API. Error %s\n", cs_strerror(err));
  524. exit (EXIT_FAILURE);
  525. }
  526. switch (action) {
  527. case ACTION_PRINT_DEFAULT:
  528. case ACTION_PRINT_ALL:
  529. print_iter(action, handle, NULL);
  530. break;
  531. case ACTION_PRINT_PREFIX:
  532. for (i = 0; i < argc; i++) {
  533. print_iter(action, handle, argv[i]);
  534. }
  535. break;
  536. case ACTION_GET:
  537. for (i = 0; i < argc; i++) {
  538. err = cmap_get(handle, argv[i], NULL, &value_len, &type);
  539. if (err == CS_OK) {
  540. print_key(handle, argv[i], value_len, NULL, type);
  541. } else {
  542. fprintf(stderr, "Can't get key %s. Error %s\n", argv[i], cs_strerror(err));
  543. }
  544. }
  545. break;
  546. case ACTION_DELETE:
  547. for (i = 0; i < argc; i++) {
  548. err = cmap_delete(handle, argv[i]);
  549. if (err != CS_OK) {
  550. fprintf(stderr, "Can't delete key %s. Error %s\n", argv[i], cs_strerror(err));
  551. }
  552. }
  553. break;
  554. case ACTION_TRACK:
  555. for (i = 0; i < argc; i++) {
  556. add_track(handle, argv[i], track_prefix);
  557. }
  558. track_changes(handle);
  559. break;
  560. case ACTION_SET:
  561. if (argc < 3) {
  562. fprintf(stderr, "At least 3 parameters are expected for set\n");
  563. return (EXIT_FAILURE);
  564. }
  565. set_key(handle, argv[0], argv[1], argv[2]);
  566. break;
  567. }
  568. err = cmap_finalize(handle);
  569. if (err != CS_OK) {
  570. fprintf (stderr, "Failed to finalize the cmap API. Error %s\n", cs_strerror(err));
  571. exit (EXIT_FAILURE);
  572. }
  573. return (0);
  574. }