corosync-objctl.c 23 KB

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