corosync-objctl.c 20 KB

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