corosync-objctl.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. /*
  2. * Copyright (c) 2008 Allied Telesis Labs NZ
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Angus Salkeld <angus.salkeld@alliedtelesis.co.nz>
  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 MontaVista Software, 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 <sys/select.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <errno.h>
  39. #include <unistd.h>
  40. #include <string.h>
  41. #include <ctype.h>
  42. #include <sys/types.h>
  43. #include <sys/un.h>
  44. #include <corosync/corotypes.h>
  45. #include <corosync/confdb.h>
  46. #define SEPERATOR '.'
  47. #define SEPERATOR_STR "."
  48. #define OBJ_NAME_SIZE 512
  49. typedef enum {
  50. ACTION_READ,
  51. ACTION_WRITE,
  52. ACTION_CREATE,
  53. ACTION_DELETE,
  54. ACTION_PRINT_ALL,
  55. ACTION_PRINT_DEFAULT,
  56. ACTION_TRACK,
  57. } action_types_t;
  58. typedef enum {
  59. FIND_OBJECT_ONLY,
  60. FIND_OBJECT_OR_KEY,
  61. FIND_KEY_ONLY
  62. } find_object_of_type_t;
  63. static void tail_key_changed(confdb_handle_t handle,
  64. confdb_change_type_t change_type,
  65. hdb_handle_t parent_object_handle,
  66. hdb_handle_t object_handle,
  67. const void *object_name,
  68. size_t object_name_len,
  69. const void *key_name,
  70. size_t key_name_len,
  71. const void *key_value,
  72. size_t key_value_len);
  73. static void tail_object_created(confdb_handle_t handle,
  74. hdb_handle_t parent_object_handle,
  75. hdb_handle_t object_handle,
  76. const void *name_pt,
  77. size_t name_len);
  78. static void tail_object_deleted(confdb_handle_t handle,
  79. hdb_handle_t parent_object_handle,
  80. const void *name_pt,
  81. size_t name_len);
  82. static void create_object(confdb_handle_t handle, char * name_pt);
  83. static void write_key(confdb_handle_t handle, char * path_pt);
  84. static void get_parent_name(const char * name_pt, char * parent_name);
  85. static confdb_callbacks_t callbacks = {
  86. .confdb_key_change_notify_fn = tail_key_changed,
  87. .confdb_object_create_change_notify_fn = tail_object_created,
  88. .confdb_object_delete_change_notify_fn = tail_object_deleted,
  89. };
  90. static int debug = 0;
  91. static int action;
  92. static void print_key (char *key_name, void *value, size_t value_len, confdb_value_types_t type)
  93. {
  94. switch (type) {
  95. case CONFDB_VALUETYPE_INT16:
  96. printf ("%s=%hd\n", key_name,
  97. *(int16_t*)value);
  98. break;
  99. case CONFDB_VALUETYPE_UINT16:
  100. printf ("%s=%hu\n", key_name,
  101. *(uint16_t*)value);
  102. break;
  103. case CONFDB_VALUETYPE_INT32:
  104. printf ("%s=%d\n", key_name,
  105. *(int32_t*)value);
  106. break;
  107. case CONFDB_VALUETYPE_UINT32:
  108. printf ("%s=%u\n", key_name,
  109. *(uint32_t*)value);
  110. break;
  111. case CONFDB_VALUETYPE_INT64:
  112. printf ("%s=%"PRIi64"\n", key_name,
  113. *(int64_t*)value);
  114. break;
  115. case CONFDB_VALUETYPE_UINT64:
  116. printf ("%s=%"PRIu64"\n", key_name,
  117. *(uint64_t*)value);
  118. break;
  119. case CONFDB_VALUETYPE_FLOAT:
  120. printf ("%s=%f\n", key_name,
  121. *(float*)value);
  122. break;
  123. case CONFDB_VALUETYPE_DOUBLE:
  124. printf ("%s=%f\n", key_name,
  125. *(double*)value);
  126. break;
  127. case CONFDB_VALUETYPE_STRING:
  128. printf ("%s=%s\n", key_name, (char*)value);
  129. break;
  130. default:
  131. case CONFDB_VALUETYPE_ANY:
  132. printf ("%s=**binary**(%d)\n", key_name, type);
  133. break;
  134. }
  135. }
  136. /* Recursively dump the object tree */
  137. static void print_config_tree(confdb_handle_t handle, hdb_handle_t parent_object_handle, char * parent_name)
  138. {
  139. hdb_handle_t object_handle;
  140. char object_name[OBJ_NAME_SIZE];
  141. size_t object_name_len;
  142. char key_name[OBJ_NAME_SIZE];
  143. char key_value[OBJ_NAME_SIZE];
  144. size_t key_value_len;
  145. cs_error_t res;
  146. int children_printed;
  147. confdb_value_types_t type;
  148. /* Show the keys */
  149. res = confdb_key_iter_start(handle, parent_object_handle);
  150. if (res != CS_OK) {
  151. fprintf(stderr, "error resetting key iterator for object "HDB_X_FORMAT" %d\n", parent_object_handle, res);
  152. exit(EXIT_FAILURE);
  153. }
  154. children_printed = 0;
  155. while ( (res = confdb_key_iter_typed(handle,
  156. parent_object_handle,
  157. key_name,
  158. key_value,
  159. &key_value_len,
  160. &type)) == CS_OK) {
  161. key_value[key_value_len] = '\0';
  162. if (parent_name != NULL)
  163. printf("%s%c", parent_name, SEPERATOR);
  164. print_key(key_name, key_value, key_value_len, type);
  165. children_printed++;
  166. }
  167. /* Show sub-objects */
  168. res = confdb_object_iter_start(handle, parent_object_handle);
  169. if (res != CS_OK) {
  170. fprintf(stderr, "error resetting object iterator for object "HDB_X_FORMAT" %d\n", parent_object_handle, res);
  171. exit(EXIT_FAILURE);
  172. }
  173. while ( (res = confdb_object_iter(handle,
  174. parent_object_handle,
  175. &object_handle,
  176. object_name,
  177. &object_name_len)) == CS_OK) {
  178. object_name[object_name_len] = '\0';
  179. if (parent_name != NULL) {
  180. snprintf(key_value, OBJ_NAME_SIZE, "%s%c%s", parent_name, SEPERATOR, object_name);
  181. } else {
  182. if ((action == ACTION_PRINT_DEFAULT) && strcmp(object_name, "internal_configuration") == 0) continue;
  183. snprintf(key_value, OBJ_NAME_SIZE, "%s", object_name);
  184. }
  185. print_config_tree(handle, object_handle, key_value);
  186. children_printed++;
  187. }
  188. if (children_printed == 0 && parent_name != NULL) {
  189. printf("%s\n", parent_name);
  190. }
  191. }
  192. static int read_in_config_file (char * filename)
  193. {
  194. confdb_handle_t handle;
  195. int result;
  196. int ignore;
  197. int c;
  198. FILE* fh;
  199. char buf[1024];
  200. char * line;
  201. char * end;
  202. char parent_name[OBJ_NAME_SIZE];
  203. if (access (filename, R_OK) != 0) {
  204. perror ("Couldn't access file.");
  205. return -1;
  206. }
  207. fh = fopen(filename, "r");
  208. if (fh == NULL) {
  209. perror ("Couldn't open file.");
  210. return -1;
  211. }
  212. result = confdb_initialize (&handle, &callbacks);
  213. if (result != CONFDB_OK) {
  214. fprintf (stderr, "Could not initialize objdb library. Error %d\n", result);
  215. fclose (fh);
  216. return -1;
  217. }
  218. while (fgets (buf, 1024, fh) != NULL) {
  219. /* find the first real character, if it is
  220. * a '#' then ignore this line.
  221. * else process.
  222. * if no real characters then also ignore.
  223. */
  224. ignore = 1;
  225. for (c = 0; c < 1024; c++) {
  226. if (isblank (buf[c]))
  227. continue;
  228. if (buf[c] == '#' || buf[c] == '\n') {
  229. ignore = 1;
  230. break;
  231. }
  232. ignore = 0;
  233. line = &buf[c];
  234. break;
  235. }
  236. if (ignore == 1)
  237. continue;
  238. /* kill the \n */
  239. end = strchr (line, '\n');
  240. if (end != NULL)
  241. *end = '\0';
  242. if (debug == 2)
  243. printf ("%d: %s\n", __LINE__, line);
  244. /* find the parent object */
  245. get_parent_name(line, parent_name);
  246. if (debug == 2)
  247. printf ("%d: %s\n", __LINE__, parent_name);
  248. /* create the object */
  249. create_object (handle, parent_name);
  250. /* write the attribute */
  251. write_key (handle, line);
  252. }
  253. confdb_finalize (handle);
  254. fclose (fh);
  255. return 0;
  256. }
  257. static int print_all(void)
  258. {
  259. confdb_handle_t handle;
  260. int result;
  261. result = confdb_initialize (&handle, &callbacks);
  262. if (result != CS_OK) {
  263. fprintf (stderr, "Could not initialize objdb library. Error %d\n", result);
  264. return 1;
  265. }
  266. print_config_tree(handle, OBJECT_PARENT_HANDLE, NULL);
  267. result = confdb_finalize (handle);
  268. return 0;
  269. }
  270. static int print_help(void)
  271. {
  272. printf("\n");
  273. printf ("usage: corosync-objctl object%ckey ... Print an object\n", SEPERATOR);
  274. printf (" corosync-objctl -c object%cchild_obj ... Create Object\n", SEPERATOR);
  275. printf (" corosync-objctl -d object%cchild_obj ... Delete object\n", SEPERATOR);
  276. printf (" corosync-objctl -w object%cchild_obj.key=value ... Create a key\n", SEPERATOR);
  277. printf (" corosync-objctl -t object%cchild_obj ... Track changes\n", SEPERATOR);
  278. printf (" corosync-objctl -a Print all objects\n");
  279. printf (" corosync-objctl -p <filename> Load in config from the specified file.\n");
  280. printf("\n");
  281. return 0;
  282. }
  283. static cs_error_t validate_name(char * obj_name_pt)
  284. {
  285. if ((strchr (obj_name_pt, SEPERATOR) == NULL) &&
  286. (strchr (obj_name_pt, '=') == NULL))
  287. return CS_OK;
  288. else
  289. return CS_ERR_INVALID_PARAM;
  290. }
  291. static void get_parent_name(const char * name_pt, char * parent_name)
  292. {
  293. char * tmp;
  294. strcpy(parent_name, name_pt);
  295. /* first remove the value (it could be a file path */
  296. tmp = strchr(parent_name, '=');
  297. if (tmp != NULL) {
  298. *tmp = '\0';
  299. tmp--;
  300. while (isblank (*tmp)) {
  301. *tmp = '\0';
  302. tmp--;
  303. }
  304. }
  305. /* then truncate the child name */
  306. tmp = strrchr(parent_name, SEPERATOR);
  307. if (tmp != NULL) *tmp = '\0';
  308. }
  309. static void get_key(const char * name_pt, char * key_name, char * key_value)
  310. {
  311. char * tmp = (char*)name_pt;
  312. char str_copy[OBJ_NAME_SIZE];
  313. char * copy_tmp = str_copy;
  314. int equals_seen = 0;
  315. int in_quotes = 0;
  316. /* strip out spaces when not in quotes */
  317. while (*tmp != '\0') {
  318. if (*tmp == '=')
  319. equals_seen = 1;
  320. if (equals_seen && *tmp == '"') {
  321. if (in_quotes)
  322. in_quotes = 0;
  323. else
  324. in_quotes = 1;
  325. }
  326. if (*tmp != ' ' || in_quotes) {
  327. *copy_tmp = *tmp;
  328. copy_tmp++;
  329. }
  330. tmp++;
  331. }
  332. *copy_tmp = '\0';
  333. /* first remove the value (it could have a SEPERATOR in it */
  334. tmp = strchr(str_copy, '=');
  335. if (tmp != NULL && strlen(tmp) > 0) {
  336. strcpy(key_value, tmp+1);
  337. *tmp = '\0';
  338. } else {
  339. key_value[0] = '\0';
  340. }
  341. /* then remove the name */
  342. tmp = strrchr(str_copy, SEPERATOR);
  343. if (tmp == NULL) tmp = str_copy;
  344. strcpy(key_name, tmp+1);
  345. }
  346. static cs_error_t find_object (confdb_handle_t handle,
  347. char * name_pt,
  348. find_object_of_type_t type,
  349. hdb_handle_t * out_handle)
  350. {
  351. char * obj_name_pt;
  352. char * save_pt;
  353. hdb_handle_t obj_handle;
  354. confdb_handle_t parent_object_handle = OBJECT_PARENT_HANDLE;
  355. char tmp_name[OBJ_NAME_SIZE];
  356. cs_error_t res = CS_OK;
  357. strncpy (tmp_name, name_pt, OBJ_NAME_SIZE);
  358. obj_name_pt = strtok_r(tmp_name, SEPERATOR_STR, &save_pt);
  359. while (obj_name_pt != NULL) {
  360. res = confdb_object_find_start(handle, parent_object_handle);
  361. if (res != CS_OK) {
  362. fprintf (stderr, "Could not start object_find %d\n", res);
  363. exit (EXIT_FAILURE);
  364. }
  365. res = confdb_object_find(handle, parent_object_handle,
  366. obj_name_pt, strlen (obj_name_pt), &obj_handle);
  367. if (res != CS_OK) {
  368. return res;
  369. }
  370. parent_object_handle = obj_handle;
  371. obj_name_pt = strtok_r (NULL, SEPERATOR_STR, &save_pt);
  372. }
  373. *out_handle = parent_object_handle;
  374. return res;
  375. }
  376. static void read_object(confdb_handle_t handle, char * name_pt)
  377. {
  378. char parent_name[OBJ_NAME_SIZE];
  379. hdb_handle_t obj_handle;
  380. cs_error_t res;
  381. get_parent_name(name_pt, parent_name);
  382. res = find_object (handle, name_pt, FIND_OBJECT_OR_KEY, &obj_handle);
  383. if (res == CS_OK) {
  384. print_config_tree(handle, obj_handle, parent_name);
  385. }
  386. }
  387. static void write_key(confdb_handle_t handle, char * path_pt)
  388. {
  389. hdb_handle_t obj_handle;
  390. char parent_name[OBJ_NAME_SIZE];
  391. char key_name[OBJ_NAME_SIZE];
  392. char key_value[OBJ_NAME_SIZE];
  393. char old_key_value[OBJ_NAME_SIZE];
  394. size_t old_key_value_len;
  395. cs_error_t res;
  396. confdb_value_types_t type;
  397. /* find the parent object */
  398. get_parent_name(path_pt, parent_name);
  399. get_key(path_pt, key_name, key_value);
  400. if (debug == 1)
  401. printf ("%d: key:\"%s\", value:\"%s\"\n",
  402. __LINE__, key_name, key_value);
  403. if (validate_name(key_name) != CS_OK) {
  404. fprintf(stderr, "Incorrect key name, can not have \"=\" or \"%c\"\n", SEPERATOR);
  405. exit(EXIT_FAILURE);
  406. }
  407. res = find_object (handle, parent_name, FIND_OBJECT_ONLY, &obj_handle);
  408. if (res != CS_OK) {
  409. fprintf(stderr, "Can't find parent object of \"%s\"\n", path_pt);
  410. exit(EXIT_FAILURE);
  411. }
  412. /* get the current key */
  413. res = confdb_key_get_typed (handle,
  414. obj_handle,
  415. key_name,
  416. old_key_value,
  417. &old_key_value_len, &type);
  418. if (res == CS_OK) {
  419. /* replace the current value */
  420. res = confdb_key_replace (handle,
  421. obj_handle,
  422. key_name,
  423. strlen(key_name),
  424. old_key_value,
  425. old_key_value_len,
  426. key_value,
  427. strlen(key_value));
  428. if (res != CS_OK)
  429. fprintf(stderr, "Failed to replace the key %s=%s. Error %d\n", key_name, key_value, res);
  430. } else {
  431. /* not there, create a new key */
  432. res = confdb_key_create_typed (handle,
  433. obj_handle,
  434. key_name,
  435. key_value,
  436. strlen(key_value),
  437. CONFDB_VALUETYPE_STRING);
  438. if (res != CS_OK)
  439. fprintf(stderr, "Failed to create the key %s=%s. Error %d\n", key_name, key_value, res);
  440. }
  441. }
  442. static void create_object(confdb_handle_t handle, char * name_pt)
  443. {
  444. char * obj_name_pt;
  445. char * save_pt;
  446. hdb_handle_t obj_handle;
  447. hdb_handle_t parent_object_handle = OBJECT_PARENT_HANDLE;
  448. char tmp_name[OBJ_NAME_SIZE];
  449. cs_error_t res;
  450. strncpy (tmp_name, name_pt, OBJ_NAME_SIZE);
  451. obj_name_pt = strtok_r(tmp_name, SEPERATOR_STR, &save_pt);
  452. while (obj_name_pt != NULL) {
  453. res = confdb_object_find_start(handle, parent_object_handle);
  454. if (res != CS_OK) {
  455. fprintf (stderr, "Could not start object_find %d\n", res);
  456. exit (EXIT_FAILURE);
  457. }
  458. res = confdb_object_find(handle, parent_object_handle,
  459. obj_name_pt, strlen (obj_name_pt), &obj_handle);
  460. if (res != CS_OK) {
  461. if (validate_name(obj_name_pt) != CS_OK) {
  462. fprintf(stderr, "Incorrect object name \"%s\", \"=\" not allowed.\n",
  463. obj_name_pt);
  464. exit(EXIT_FAILURE);
  465. }
  466. if (debug)
  467. printf ("%s:%d: %s\n", __func__,__LINE__, obj_name_pt);
  468. res = confdb_object_create (handle,
  469. parent_object_handle,
  470. obj_name_pt,
  471. strlen (obj_name_pt),
  472. &obj_handle);
  473. if (res != CS_OK)
  474. fprintf(stderr, "Failed to create object \"%s\". Error %d.\n",
  475. obj_name_pt, res);
  476. }
  477. parent_object_handle = obj_handle;
  478. obj_name_pt = strtok_r (NULL, SEPERATOR_STR, &save_pt);
  479. }
  480. }
  481. /* Print "?" in place of any non-printable byte of OBJ. */
  482. static void print_name (FILE *fp, const void *obj, size_t obj_len)
  483. {
  484. const char *p = obj;
  485. size_t i;
  486. for (i = 0; i < obj_len; i++) {
  487. int c = *p++;
  488. if (!isprint (c)) {
  489. c = '?';
  490. }
  491. fputc (c, fp);
  492. }
  493. }
  494. static void tail_key_changed(confdb_handle_t handle,
  495. confdb_change_type_t change_type,
  496. hdb_handle_t parent_object_handle,
  497. hdb_handle_t object_handle,
  498. const void *object_name_pt,
  499. size_t object_name_len,
  500. const void *key_name_pt,
  501. size_t key_name_len,
  502. const void *key_value_pt,
  503. size_t key_value_len)
  504. {
  505. /* printf("key_changed> %.*s.%.*s=%.*s\n", */
  506. fputs("key_changed> ", stdout);
  507. print_name (stdout, object_name_pt, object_name_len);
  508. fputs(".", stdout);
  509. print_name (stdout, key_name_pt, key_name_len);
  510. fputs("=", stdout);
  511. print_name (stdout, key_value_pt, key_value_len);
  512. fputs("\n", stdout);
  513. }
  514. static void tail_object_created(confdb_handle_t handle,
  515. hdb_handle_t parent_object_handle,
  516. hdb_handle_t object_handle,
  517. const void *name_pt,
  518. size_t name_len)
  519. {
  520. fputs("object_created>", stdout);
  521. print_name(stdout, name_pt, name_len);
  522. fputs("\n", stdout);
  523. }
  524. static void tail_object_deleted(confdb_handle_t handle,
  525. hdb_handle_t parent_object_handle,
  526. const void *name_pt,
  527. size_t name_len)
  528. {
  529. fputs("object_deleted>", stdout);
  530. print_name(stdout, name_pt, name_len);
  531. fputs("\n", stdout);
  532. }
  533. static void listen_for_object_changes(confdb_handle_t handle)
  534. {
  535. int result;
  536. fd_set read_fds;
  537. int select_fd;
  538. int quit = CS_FALSE;
  539. FD_ZERO (&read_fds);
  540. if (confdb_fd_get (handle, &select_fd) != CS_OK) {
  541. printf ("can't get the confdb selector object.\n");
  542. return;
  543. }
  544. printf ("Type \"q\" to finish\n");
  545. do {
  546. FD_SET (select_fd, &read_fds);
  547. FD_SET (STDIN_FILENO, &read_fds);
  548. result = select (select_fd + 1, &read_fds, 0, 0, 0);
  549. if (result == -1) {
  550. perror ("select\n");
  551. }
  552. if (FD_ISSET (STDIN_FILENO, &read_fds)) {
  553. char inbuf[3];
  554. if (fgets(inbuf, sizeof(inbuf), stdin) == NULL)
  555. quit = CS_TRUE;
  556. else if (strncmp(inbuf, "q", 1) == 0)
  557. quit = CS_TRUE;
  558. }
  559. if (FD_ISSET (select_fd, &read_fds)) {
  560. if (confdb_dispatch (handle, CONFDB_DISPATCH_ALL) != CS_OK)
  561. exit(1);
  562. }
  563. } while (result && quit == CS_FALSE);
  564. (void)confdb_stop_track_changes(handle);
  565. }
  566. static void track_object(confdb_handle_t handle, char * name_pt)
  567. {
  568. cs_error_t res;
  569. hdb_handle_t obj_handle;
  570. res = find_object (handle, name_pt, FIND_OBJECT_ONLY, &obj_handle);
  571. if (res != CS_OK) {
  572. fprintf (stderr, "Could not find object \"%s\". Error %d\n",
  573. name_pt, res);
  574. return;
  575. }
  576. res = confdb_track_changes (handle, obj_handle, CONFDB_TRACK_DEPTH_RECURSIVE);
  577. if (res != CS_OK) {
  578. fprintf (stderr, "Could not enable tracking on object \"%s\". Error %d\n",
  579. name_pt, res);
  580. return;
  581. }
  582. }
  583. static void stop_tracking(confdb_handle_t handle)
  584. {
  585. cs_error_t res;
  586. res = confdb_stop_track_changes (handle);
  587. if (res != CS_OK) {
  588. fprintf (stderr, "Could not stop tracking. Error %d\n", res);
  589. return;
  590. }
  591. }
  592. static void delete_object(confdb_handle_t handle, char * name_pt)
  593. {
  594. cs_error_t res;
  595. hdb_handle_t obj_handle;
  596. res = find_object (handle, name_pt, FIND_OBJECT_ONLY, &obj_handle);
  597. if (res == CS_OK) {
  598. res = confdb_object_destroy (handle, obj_handle);
  599. if (res != CS_OK)
  600. fprintf(stderr, "Failed to find object \"%s\" to delete. Error %d\n", name_pt, res);
  601. } else {
  602. char parent_name[OBJ_NAME_SIZE];
  603. char key_name[OBJ_NAME_SIZE];
  604. char key_value[OBJ_NAME_SIZE];
  605. /* find the parent object */
  606. get_parent_name(name_pt, parent_name);
  607. get_key(name_pt, key_name, key_value);
  608. res = find_object (handle, parent_name, FIND_OBJECT_ONLY, &obj_handle);
  609. if (res != CS_OK) {
  610. fprintf(stderr, "Failed to find the key's parent object \"%s\". Error %d\n", parent_name, res);
  611. exit (EXIT_FAILURE);
  612. }
  613. res = confdb_key_delete (handle,
  614. obj_handle,
  615. key_name,
  616. strlen(key_name),
  617. key_value,
  618. strlen(key_value));
  619. if (res != CS_OK)
  620. fprintf(stderr, "Failed to delete key \"%s=%s\" from object \"%s\". Error %d\n",
  621. key_name, key_value, parent_name, res);
  622. }
  623. }
  624. int main (int argc, char *argv[]) {
  625. confdb_handle_t handle;
  626. cs_error_t result;
  627. int c;
  628. action = ACTION_READ;
  629. for (;;){
  630. c = getopt (argc,argv,"hawcvdtp:");
  631. if (c==-1) {
  632. break;
  633. }
  634. switch (c) {
  635. case 'v':
  636. debug++;
  637. break;
  638. case 'h':
  639. return print_help();
  640. break;
  641. case 'a':
  642. action = ACTION_PRINT_ALL;
  643. break;
  644. case 'p':
  645. return read_in_config_file (optarg);
  646. break;
  647. case 'c':
  648. action = ACTION_CREATE;
  649. break;
  650. case 'd':
  651. action = ACTION_DELETE;
  652. break;
  653. case 'w':
  654. action = ACTION_WRITE;
  655. break;
  656. case 't':
  657. action = ACTION_TRACK;
  658. break;
  659. default :
  660. action = ACTION_READ;
  661. break;
  662. }
  663. }
  664. if (argc == 1) {
  665. action = ACTION_PRINT_DEFAULT;
  666. return print_all();
  667. } else if (action == ACTION_PRINT_ALL) {
  668. return print_all();
  669. } else if (optind >= argc) {
  670. fprintf(stderr, "Expected an object path after options\n");
  671. exit(EXIT_FAILURE);
  672. }
  673. result = confdb_initialize (&handle, &callbacks);
  674. if (result != CS_OK) {
  675. fprintf (stderr, "Failed to initialize the objdb API. Error %d\n", result);
  676. exit (EXIT_FAILURE);
  677. }
  678. while (optind < argc) {
  679. switch (action) {
  680. case ACTION_READ:
  681. read_object(handle, argv[optind++]);
  682. break;
  683. case ACTION_WRITE:
  684. write_key(handle, argv[optind++]);
  685. break;
  686. case ACTION_CREATE:
  687. create_object(handle, argv[optind++]);
  688. break;
  689. case ACTION_DELETE:
  690. delete_object(handle, argv[optind++]);
  691. break;
  692. case ACTION_TRACK:
  693. track_object(handle, argv[optind++]);
  694. break;
  695. }
  696. }
  697. if (action == ACTION_TRACK) {
  698. listen_for_object_changes(handle);
  699. stop_tracking(handle);
  700. }
  701. result = confdb_finalize (handle);
  702. if (result != CS_OK) {
  703. fprintf (stderr, "Error finalizing objdb API. Error %d\n", result);
  704. exit(EXIT_FAILURE);
  705. }
  706. return 0;
  707. }