corosync-objctl.c 23 KB

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