corosync-objctl.c 20 KB

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