corosync-objctl.c 23 KB

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