corosync-objctl.c 22 KB

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