corosync-objctl.c 22 KB

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