corosync-cmapctl.c 20 KB

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