corosync-objctl.c 19 KB

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