corosync-objctl.c 23 KB

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