corosync-cmapctl.c 19 KB

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