corosync-objctl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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, sizeof (tmp_name));
  385. tmp_name[sizeof (tmp_name) - 1] = '\0';
  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, sizeof (tmp_name));
  479. tmp_name[sizeof (tmp_name) - 1] = '\0';
  480. obj_name_pt = strtok_r(tmp_name, SEPERATOR_STR, &save_pt);
  481. while (obj_name_pt != NULL) {
  482. res = confdb_object_find_start(handle, parent_object_handle);
  483. if (res != CS_OK) {
  484. fprintf (stderr, "Could not start object_find %d\n", res);
  485. exit (EXIT_FAILURE);
  486. }
  487. res = confdb_object_find(handle, parent_object_handle,
  488. obj_name_pt, strlen (obj_name_pt), &obj_handle);
  489. if (res != CS_OK) {
  490. if (validate_name(obj_name_pt) != CS_OK) {
  491. fprintf(stderr, "Incorrect object name \"%s\", \"=\" not allowed.\n",
  492. obj_name_pt);
  493. exit(EXIT_FAILURE);
  494. }
  495. if (debug)
  496. printf ("%s:%d: %s\n", __func__,__LINE__, obj_name_pt);
  497. res = confdb_object_create (handle,
  498. parent_object_handle,
  499. obj_name_pt,
  500. strlen (obj_name_pt),
  501. &obj_handle);
  502. if (res != CS_OK)
  503. fprintf(stderr, "Failed to create object \"%s\". Error %d.\n",
  504. obj_name_pt, res);
  505. }
  506. parent_object_handle = obj_handle;
  507. obj_name_pt = strtok_r (NULL, SEPERATOR_STR, &save_pt);
  508. }
  509. }
  510. static void create_object_key(confdb_handle_t handle, char *name_pt)
  511. {
  512. char * obj_name_pt;
  513. char * new_obj_name_pt;
  514. char * save_pt;
  515. hdb_handle_t obj_handle;
  516. hdb_handle_t parent_object_handle = OBJECT_PARENT_HANDLE;
  517. char tmp_name[OBJ_NAME_SIZE];
  518. cs_error_t res;
  519. char parent_name[OBJ_NAME_SIZE];
  520. char key_name[OBJ_NAME_SIZE];
  521. char key_value[OBJ_NAME_SIZE];
  522. get_parent_name(name_pt, parent_name);
  523. get_key(name_pt, key_name, key_value);
  524. strncpy (tmp_name, parent_name, sizeof (tmp_name));
  525. tmp_name[sizeof (tmp_name) - 1] = '\0';
  526. obj_name_pt = strtok_r(tmp_name, SEPERATOR_STR, &save_pt);
  527. /*
  528. * Create parent object tree
  529. */
  530. while (obj_name_pt != NULL) {
  531. res = confdb_object_find_start(handle, parent_object_handle);
  532. if (res != CS_OK) {
  533. fprintf (stderr, "Could not start object_find %d\n", res);
  534. exit (EXIT_FAILURE);
  535. }
  536. new_obj_name_pt = strtok_r (NULL, SEPERATOR_STR, &save_pt);
  537. res = confdb_object_find(handle, parent_object_handle,
  538. obj_name_pt, strlen (obj_name_pt), &obj_handle);
  539. if (res != CS_OK || new_obj_name_pt == NULL) {
  540. if (validate_name(obj_name_pt) != CS_OK) {
  541. fprintf(stderr, "Incorrect object name \"%s\", \"=\" not allowed.\n",
  542. obj_name_pt);
  543. exit(EXIT_FAILURE);
  544. }
  545. if (debug)
  546. printf ("%s:%d: %s\n", __func__,__LINE__, obj_name_pt);
  547. res = confdb_object_create (handle,
  548. parent_object_handle,
  549. obj_name_pt,
  550. strlen (obj_name_pt),
  551. &obj_handle);
  552. if (res != CS_OK) {
  553. fprintf(stderr, "Failed to create object \"%s\". Error %d.\n",
  554. obj_name_pt, res);
  555. }
  556. }
  557. parent_object_handle = obj_handle;
  558. obj_name_pt = new_obj_name_pt;
  559. }
  560. /*
  561. * Create key
  562. */
  563. res = confdb_key_create_typed (handle,
  564. obj_handle,
  565. key_name,
  566. key_value,
  567. strlen(key_value),
  568. CONFDB_VALUETYPE_STRING);
  569. if (res != CS_OK) {
  570. fprintf(stderr,
  571. "Failed to create the key %s=%s. Error %d\n",
  572. key_name, key_value, res);
  573. }
  574. }
  575. /* Print "?" in place of any non-printable byte of OBJ. */
  576. static void print_name (FILE *fp, const void *obj, size_t obj_len)
  577. {
  578. const char *p = obj;
  579. size_t i;
  580. for (i = 0; i < obj_len; i++) {
  581. int c = *p++;
  582. if (!isprint (c)) {
  583. c = '?';
  584. }
  585. fputc (c, fp);
  586. }
  587. }
  588. static void tail_key_changed(confdb_handle_t handle,
  589. confdb_change_type_t change_type,
  590. hdb_handle_t parent_object_handle,
  591. hdb_handle_t object_handle,
  592. const void *object_name_pt,
  593. size_t object_name_len,
  594. const void *key_name_pt,
  595. size_t key_name_len,
  596. const void *key_value_pt,
  597. size_t key_value_len)
  598. {
  599. /* printf("key_changed> %.*s.%.*s=%.*s\n", */
  600. fputs("key_changed> ", stdout);
  601. print_name (stdout, object_name_pt, object_name_len);
  602. fputs(".", stdout);
  603. print_name (stdout, key_name_pt, key_name_len);
  604. fputs("=", stdout);
  605. print_name (stdout, key_value_pt, key_value_len);
  606. fputs("\n", stdout);
  607. }
  608. static void tail_object_created(confdb_handle_t handle,
  609. hdb_handle_t parent_object_handle,
  610. hdb_handle_t object_handle,
  611. const void *name_pt,
  612. size_t name_len)
  613. {
  614. fputs("object_created>", stdout);
  615. print_name(stdout, name_pt, name_len);
  616. fputs("\n", stdout);
  617. }
  618. static void tail_object_deleted(confdb_handle_t handle,
  619. hdb_handle_t parent_object_handle,
  620. const void *name_pt,
  621. size_t name_len)
  622. {
  623. fputs("object_deleted>", stdout);
  624. print_name(stdout, name_pt, name_len);
  625. fputs("\n", stdout);
  626. }
  627. static void listen_for_object_changes(confdb_handle_t handle)
  628. {
  629. int result;
  630. fd_set read_fds;
  631. int select_fd;
  632. int quit = CS_FALSE;
  633. FD_ZERO (&read_fds);
  634. if (confdb_fd_get (handle, &select_fd) != CS_OK) {
  635. printf ("can't get the confdb selector object.\n");
  636. return;
  637. }
  638. printf ("Type \"q\" to finish\n");
  639. do {
  640. FD_SET (select_fd, &read_fds);
  641. FD_SET (STDIN_FILENO, &read_fds);
  642. result = select (select_fd + 1, &read_fds, 0, 0, 0);
  643. if (result == -1) {
  644. perror ("select\n");
  645. }
  646. if (FD_ISSET (STDIN_FILENO, &read_fds)) {
  647. char inbuf[3];
  648. if (fgets(inbuf, sizeof(inbuf), stdin) == NULL)
  649. quit = CS_TRUE;
  650. else if (strncmp(inbuf, "q", 1) == 0)
  651. quit = CS_TRUE;
  652. }
  653. if (FD_ISSET (select_fd, &read_fds)) {
  654. if (confdb_dispatch (handle, CONFDB_DISPATCH_ALL) != CS_OK)
  655. exit(1);
  656. }
  657. } while (result && quit == CS_FALSE);
  658. (void)confdb_stop_track_changes(handle);
  659. }
  660. static void track_object(confdb_handle_t handle, char * name_pt)
  661. {
  662. cs_error_t res;
  663. hdb_handle_t obj_handle;
  664. res = find_object (handle, name_pt, FIND_OBJECT_ONLY, &obj_handle);
  665. if (res != CS_OK) {
  666. fprintf (stderr, "Could not find object \"%s\". Error %d\n",
  667. name_pt, res);
  668. return;
  669. }
  670. res = confdb_track_changes (handle, obj_handle, CONFDB_TRACK_DEPTH_RECURSIVE);
  671. if (res != CS_OK) {
  672. fprintf (stderr, "Could not enable tracking on object \"%s\". Error %d\n",
  673. name_pt, res);
  674. return;
  675. }
  676. }
  677. static void stop_tracking(confdb_handle_t handle)
  678. {
  679. cs_error_t res;
  680. res = confdb_stop_track_changes (handle);
  681. if (res != CS_OK) {
  682. fprintf (stderr, "Could not stop tracking. Error %d\n", res);
  683. return;
  684. }
  685. }
  686. static void delete_object(confdb_handle_t handle, char * name_pt)
  687. {
  688. cs_error_t res;
  689. hdb_handle_t obj_handle;
  690. res = find_object (handle, name_pt, FIND_OBJECT_ONLY, &obj_handle);
  691. if (res == CS_OK) {
  692. res = confdb_object_destroy (handle, obj_handle);
  693. if (res != CS_OK)
  694. fprintf(stderr, "Failed to find object \"%s\" to delete. Error %d\n", name_pt, res);
  695. } else {
  696. char parent_name[OBJ_NAME_SIZE];
  697. char key_name[OBJ_NAME_SIZE];
  698. char key_value[OBJ_NAME_SIZE];
  699. /* find the parent object */
  700. get_parent_name(name_pt, parent_name);
  701. get_key(name_pt, key_name, key_value);
  702. res = find_object (handle, parent_name, FIND_OBJECT_ONLY, &obj_handle);
  703. if (res != CS_OK) {
  704. fprintf(stderr, "Failed to find the key's parent object \"%s\". Error %d\n", parent_name, res);
  705. exit (EXIT_FAILURE);
  706. }
  707. res = confdb_key_delete (handle,
  708. obj_handle,
  709. key_name,
  710. strlen(key_name),
  711. key_value,
  712. strlen(key_value));
  713. if (res != CS_OK)
  714. fprintf(stderr, "Failed to delete key \"%s=%s\" from object \"%s\". Error %d\n",
  715. key_name, key_value, parent_name, res);
  716. }
  717. }
  718. int main (int argc, char *argv[]) {
  719. confdb_handle_t handle;
  720. cs_error_t result;
  721. int c;
  722. action = ACTION_READ;
  723. for (;;){
  724. c = getopt (argc,argv,"habwncvdtp:");
  725. if (c==-1) {
  726. break;
  727. }
  728. switch (c) {
  729. case 'v':
  730. debug++;
  731. break;
  732. case 'h':
  733. return print_help();
  734. break;
  735. case 'a':
  736. action = ACTION_PRINT_ALL;
  737. break;
  738. case 'b':
  739. show_binary++;
  740. break;
  741. case 'p':
  742. return read_in_config_file (optarg);
  743. break;
  744. case 'c':
  745. action = ACTION_CREATE;
  746. break;
  747. case 'd':
  748. action = ACTION_DELETE;
  749. break;
  750. case 'w':
  751. action = ACTION_WRITE;
  752. break;
  753. case 'n':
  754. action = ACTION_CREATE_KEY;
  755. break;
  756. case 't':
  757. action = ACTION_TRACK;
  758. break;
  759. default :
  760. action = ACTION_READ;
  761. break;
  762. }
  763. }
  764. if (argc == 1) {
  765. action = ACTION_PRINT_DEFAULT;
  766. return print_all();
  767. } else if (action == ACTION_PRINT_ALL) {
  768. return print_all();
  769. } else if (optind >= argc) {
  770. fprintf(stderr, "Expected an object path after options\n");
  771. exit(EXIT_FAILURE);
  772. }
  773. result = confdb_initialize (&handle, &callbacks);
  774. if (result != CS_OK) {
  775. fprintf (stderr, "Failed to initialize the objdb API. Error %d\n", result);
  776. exit (EXIT_FAILURE);
  777. }
  778. while (optind < argc) {
  779. switch (action) {
  780. case ACTION_READ:
  781. read_object(handle, argv[optind++]);
  782. break;
  783. case ACTION_WRITE:
  784. write_key(handle, argv[optind++]);
  785. break;
  786. case ACTION_CREATE:
  787. create_object(handle, argv[optind++]);
  788. break;
  789. case ACTION_CREATE_KEY:
  790. create_object_key(handle, argv[optind++]);
  791. break;
  792. case ACTION_DELETE:
  793. delete_object(handle, argv[optind++]);
  794. break;
  795. case ACTION_TRACK:
  796. track_object(handle, argv[optind++]);
  797. break;
  798. }
  799. }
  800. if (action == ACTION_TRACK) {
  801. listen_for_object_changes(handle);
  802. stop_tracking(handle);
  803. }
  804. result = confdb_finalize (handle);
  805. if (result != CS_OK) {
  806. fprintf (stderr, "Error finalizing objdb API. Error %d\n", result);
  807. exit(EXIT_FAILURE);
  808. }
  809. return 0;
  810. }