corosync-objctl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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, OBJ_NAME_SIZE);
  361. obj_name_pt = strtok_r(tmp_name, SEPERATOR_STR, &save_pt);
  362. while (obj_name_pt != NULL) {
  363. res = confdb_object_find_start(handle, parent_object_handle);
  364. if (res != CS_OK) {
  365. fprintf (stderr, "Could not start object_find %d\n", res);
  366. exit (EXIT_FAILURE);
  367. }
  368. res = confdb_object_find(handle, parent_object_handle,
  369. obj_name_pt, strlen (obj_name_pt), &obj_handle);
  370. if (res != CS_OK) {
  371. return res;
  372. }
  373. parent_object_handle = obj_handle;
  374. obj_name_pt = strtok_r (NULL, SEPERATOR_STR, &save_pt);
  375. }
  376. *out_handle = parent_object_handle;
  377. return res;
  378. }
  379. static void read_object(confdb_handle_t handle, char * name_pt)
  380. {
  381. char parent_name[OBJ_NAME_SIZE];
  382. hdb_handle_t obj_handle;
  383. cs_error_t res;
  384. get_parent_name(name_pt, parent_name);
  385. res = find_object (handle, name_pt, FIND_OBJECT_OR_KEY, &obj_handle);
  386. if (res == CS_OK) {
  387. print_config_tree(handle, obj_handle, parent_name);
  388. }
  389. }
  390. static void write_key(confdb_handle_t handle, char * path_pt)
  391. {
  392. hdb_handle_t obj_handle;
  393. char parent_name[OBJ_NAME_SIZE];
  394. char key_name[OBJ_NAME_SIZE];
  395. char key_value[OBJ_NAME_SIZE];
  396. char old_key_value[OBJ_NAME_SIZE];
  397. size_t old_key_value_len;
  398. cs_error_t res;
  399. confdb_value_types_t type;
  400. /* find the parent object */
  401. get_parent_name(path_pt, parent_name);
  402. get_key(path_pt, key_name, key_value);
  403. if (debug == 1)
  404. printf ("%d: key:\"%s\", value:\"%s\"\n",
  405. __LINE__, key_name, key_value);
  406. if (validate_name(key_name) != CS_OK) {
  407. fprintf(stderr, "Incorrect key name, can not have \"=\" or \"%c\"\n", SEPERATOR);
  408. exit(EXIT_FAILURE);
  409. }
  410. res = find_object (handle, parent_name, FIND_OBJECT_ONLY, &obj_handle);
  411. if (res != CS_OK) {
  412. fprintf(stderr, "Can't find parent object of \"%s\"\n", path_pt);
  413. exit(EXIT_FAILURE);
  414. }
  415. /* get the current key */
  416. res = confdb_key_get_typed (handle,
  417. obj_handle,
  418. key_name,
  419. old_key_value,
  420. &old_key_value_len, &type);
  421. if (res == CS_OK) {
  422. /* replace the current value */
  423. res = confdb_key_replace (handle,
  424. obj_handle,
  425. key_name,
  426. strlen(key_name),
  427. old_key_value,
  428. old_key_value_len,
  429. key_value,
  430. strlen(key_value));
  431. if (res != CS_OK)
  432. fprintf(stderr, "Failed to replace the key %s=%s. Error %d\n", key_name, key_value, res);
  433. } else {
  434. /* not there, create a new key */
  435. res = confdb_key_create_typed (handle,
  436. obj_handle,
  437. key_name,
  438. key_value,
  439. strlen(key_value),
  440. CONFDB_VALUETYPE_STRING);
  441. if (res != CS_OK)
  442. fprintf(stderr, "Failed to create the key %s=%s. Error %d\n", key_name, key_value, res);
  443. }
  444. }
  445. static void create_object(confdb_handle_t handle, char * name_pt)
  446. {
  447. char * obj_name_pt;
  448. char * save_pt;
  449. hdb_handle_t obj_handle;
  450. hdb_handle_t parent_object_handle = OBJECT_PARENT_HANDLE;
  451. char tmp_name[OBJ_NAME_SIZE];
  452. cs_error_t res;
  453. strncpy (tmp_name, name_pt, OBJ_NAME_SIZE);
  454. obj_name_pt = strtok_r(tmp_name, SEPERATOR_STR, &save_pt);
  455. while (obj_name_pt != NULL) {
  456. res = confdb_object_find_start(handle, parent_object_handle);
  457. if (res != CS_OK) {
  458. fprintf (stderr, "Could not start object_find %d\n", res);
  459. exit (EXIT_FAILURE);
  460. }
  461. res = confdb_object_find(handle, parent_object_handle,
  462. obj_name_pt, strlen (obj_name_pt), &obj_handle);
  463. if (res != CS_OK) {
  464. if (validate_name(obj_name_pt) != CS_OK) {
  465. fprintf(stderr, "Incorrect object name \"%s\", \"=\" not allowed.\n",
  466. obj_name_pt);
  467. exit(EXIT_FAILURE);
  468. }
  469. if (debug)
  470. printf ("%s:%d: %s\n", __func__,__LINE__, obj_name_pt);
  471. res = confdb_object_create (handle,
  472. parent_object_handle,
  473. obj_name_pt,
  474. strlen (obj_name_pt),
  475. &obj_handle);
  476. if (res != CS_OK)
  477. fprintf(stderr, "Failed to create object \"%s\". Error %d.\n",
  478. obj_name_pt, res);
  479. }
  480. parent_object_handle = obj_handle;
  481. obj_name_pt = strtok_r (NULL, SEPERATOR_STR, &save_pt);
  482. }
  483. }
  484. static void create_object_key(confdb_handle_t handle, char *name_pt)
  485. {
  486. char * obj_name_pt;
  487. char * new_obj_name_pt;
  488. char * save_pt;
  489. hdb_handle_t obj_handle;
  490. hdb_handle_t parent_object_handle = OBJECT_PARENT_HANDLE;
  491. char tmp_name[OBJ_NAME_SIZE];
  492. cs_error_t res;
  493. char parent_name[OBJ_NAME_SIZE];
  494. char key_name[OBJ_NAME_SIZE];
  495. char key_value[OBJ_NAME_SIZE];
  496. get_parent_name(name_pt, parent_name);
  497. get_key(name_pt, key_name, key_value);
  498. strncpy (tmp_name, parent_name, OBJ_NAME_SIZE);
  499. obj_name_pt = strtok_r(tmp_name, SEPERATOR_STR, &save_pt);
  500. /*
  501. * Create parent object tree
  502. */
  503. while (obj_name_pt != NULL) {
  504. res = confdb_object_find_start(handle, parent_object_handle);
  505. if (res != CS_OK) {
  506. fprintf (stderr, "Could not start object_find %d\n", res);
  507. exit (EXIT_FAILURE);
  508. }
  509. new_obj_name_pt = strtok_r (NULL, SEPERATOR_STR, &save_pt);
  510. res = confdb_object_find(handle, parent_object_handle,
  511. obj_name_pt, strlen (obj_name_pt), &obj_handle);
  512. if (res != CS_OK || new_obj_name_pt == NULL) {
  513. if (validate_name(obj_name_pt) != CS_OK) {
  514. fprintf(stderr, "Incorrect object name \"%s\", \"=\" not allowed.\n",
  515. obj_name_pt);
  516. exit(EXIT_FAILURE);
  517. }
  518. if (debug)
  519. printf ("%s:%d: %s\n", __func__,__LINE__, obj_name_pt);
  520. res = confdb_object_create (handle,
  521. parent_object_handle,
  522. obj_name_pt,
  523. strlen (obj_name_pt),
  524. &obj_handle);
  525. if (res != CS_OK) {
  526. fprintf(stderr, "Failed to create object \"%s\". Error %d.\n",
  527. obj_name_pt, res);
  528. }
  529. }
  530. parent_object_handle = obj_handle;
  531. obj_name_pt = new_obj_name_pt;
  532. }
  533. /*
  534. * Create key
  535. */
  536. res = confdb_key_create_typed (handle,
  537. obj_handle,
  538. key_name,
  539. key_value,
  540. strlen(key_value),
  541. CONFDB_VALUETYPE_STRING);
  542. if (res != CS_OK) {
  543. fprintf(stderr,
  544. "Failed to create the key %s=%s. Error %d\n",
  545. key_name, key_value, res);
  546. }
  547. }
  548. /* Print "?" in place of any non-printable byte of OBJ. */
  549. static void print_name (FILE *fp, const void *obj, size_t obj_len)
  550. {
  551. const char *p = obj;
  552. size_t i;
  553. for (i = 0; i < obj_len; i++) {
  554. int c = *p++;
  555. if (!isprint (c)) {
  556. c = '?';
  557. }
  558. fputc (c, fp);
  559. }
  560. }
  561. static void tail_key_changed(confdb_handle_t handle,
  562. confdb_change_type_t change_type,
  563. hdb_handle_t parent_object_handle,
  564. hdb_handle_t object_handle,
  565. const void *object_name_pt,
  566. size_t object_name_len,
  567. const void *key_name_pt,
  568. size_t key_name_len,
  569. const void *key_value_pt,
  570. size_t key_value_len)
  571. {
  572. /* printf("key_changed> %.*s.%.*s=%.*s\n", */
  573. fputs("key_changed> ", stdout);
  574. print_name (stdout, object_name_pt, object_name_len);
  575. fputs(".", stdout);
  576. print_name (stdout, key_name_pt, key_name_len);
  577. fputs("=", stdout);
  578. print_name (stdout, key_value_pt, key_value_len);
  579. fputs("\n", stdout);
  580. }
  581. static void tail_object_created(confdb_handle_t handle,
  582. hdb_handle_t parent_object_handle,
  583. hdb_handle_t object_handle,
  584. const void *name_pt,
  585. size_t name_len)
  586. {
  587. fputs("object_created>", stdout);
  588. print_name(stdout, name_pt, name_len);
  589. fputs("\n", stdout);
  590. }
  591. static void tail_object_deleted(confdb_handle_t handle,
  592. hdb_handle_t parent_object_handle,
  593. const void *name_pt,
  594. size_t name_len)
  595. {
  596. fputs("object_deleted>", stdout);
  597. print_name(stdout, name_pt, name_len);
  598. fputs("\n", stdout);
  599. }
  600. static void listen_for_object_changes(confdb_handle_t handle)
  601. {
  602. int result;
  603. fd_set read_fds;
  604. int select_fd;
  605. int quit = CS_FALSE;
  606. FD_ZERO (&read_fds);
  607. if (confdb_fd_get (handle, &select_fd) != CS_OK) {
  608. printf ("can't get the confdb selector object.\n");
  609. return;
  610. }
  611. printf ("Type \"q\" to finish\n");
  612. do {
  613. FD_SET (select_fd, &read_fds);
  614. FD_SET (STDIN_FILENO, &read_fds);
  615. result = select (select_fd + 1, &read_fds, 0, 0, 0);
  616. if (result == -1) {
  617. perror ("select\n");
  618. }
  619. if (FD_ISSET (STDIN_FILENO, &read_fds)) {
  620. char inbuf[3];
  621. if (fgets(inbuf, sizeof(inbuf), stdin) == NULL)
  622. quit = CS_TRUE;
  623. else if (strncmp(inbuf, "q", 1) == 0)
  624. quit = CS_TRUE;
  625. }
  626. if (FD_ISSET (select_fd, &read_fds)) {
  627. if (confdb_dispatch (handle, CONFDB_DISPATCH_ALL) != CS_OK)
  628. exit(1);
  629. }
  630. } while (result && quit == CS_FALSE);
  631. (void)confdb_stop_track_changes(handle);
  632. }
  633. static void track_object(confdb_handle_t handle, char * name_pt)
  634. {
  635. cs_error_t res;
  636. hdb_handle_t obj_handle;
  637. res = find_object (handle, name_pt, FIND_OBJECT_ONLY, &obj_handle);
  638. if (res != CS_OK) {
  639. fprintf (stderr, "Could not find object \"%s\". Error %d\n",
  640. name_pt, res);
  641. return;
  642. }
  643. res = confdb_track_changes (handle, obj_handle, CONFDB_TRACK_DEPTH_RECURSIVE);
  644. if (res != CS_OK) {
  645. fprintf (stderr, "Could not enable tracking on object \"%s\". Error %d\n",
  646. name_pt, res);
  647. return;
  648. }
  649. }
  650. static void stop_tracking(confdb_handle_t handle)
  651. {
  652. cs_error_t res;
  653. res = confdb_stop_track_changes (handle);
  654. if (res != CS_OK) {
  655. fprintf (stderr, "Could not stop tracking. Error %d\n", res);
  656. return;
  657. }
  658. }
  659. static void delete_object(confdb_handle_t handle, char * name_pt)
  660. {
  661. cs_error_t res;
  662. hdb_handle_t obj_handle;
  663. res = find_object (handle, name_pt, FIND_OBJECT_ONLY, &obj_handle);
  664. if (res == CS_OK) {
  665. res = confdb_object_destroy (handle, obj_handle);
  666. if (res != CS_OK)
  667. fprintf(stderr, "Failed to find object \"%s\" to delete. Error %d\n", name_pt, res);
  668. } else {
  669. char parent_name[OBJ_NAME_SIZE];
  670. char key_name[OBJ_NAME_SIZE];
  671. char key_value[OBJ_NAME_SIZE];
  672. /* find the parent object */
  673. get_parent_name(name_pt, parent_name);
  674. get_key(name_pt, key_name, key_value);
  675. res = find_object (handle, parent_name, FIND_OBJECT_ONLY, &obj_handle);
  676. if (res != CS_OK) {
  677. fprintf(stderr, "Failed to find the key's parent object \"%s\". Error %d\n", parent_name, res);
  678. exit (EXIT_FAILURE);
  679. }
  680. res = confdb_key_delete (handle,
  681. obj_handle,
  682. key_name,
  683. strlen(key_name),
  684. key_value,
  685. strlen(key_value));
  686. if (res != CS_OK)
  687. fprintf(stderr, "Failed to delete key \"%s=%s\" from object \"%s\". Error %d\n",
  688. key_name, key_value, parent_name, res);
  689. }
  690. }
  691. int main (int argc, char *argv[]) {
  692. confdb_handle_t handle;
  693. cs_error_t result;
  694. int c;
  695. action = ACTION_READ;
  696. for (;;){
  697. c = getopt (argc,argv,"hawncvdtp:");
  698. if (c==-1) {
  699. break;
  700. }
  701. switch (c) {
  702. case 'v':
  703. debug++;
  704. break;
  705. case 'h':
  706. return print_help();
  707. break;
  708. case 'a':
  709. action = ACTION_PRINT_ALL;
  710. break;
  711. case 'p':
  712. return read_in_config_file (optarg);
  713. break;
  714. case 'c':
  715. action = ACTION_CREATE;
  716. break;
  717. case 'd':
  718. action = ACTION_DELETE;
  719. break;
  720. case 'w':
  721. action = ACTION_WRITE;
  722. break;
  723. case 'n':
  724. action = ACTION_CREATE_KEY;
  725. break;
  726. case 't':
  727. action = ACTION_TRACK;
  728. break;
  729. default :
  730. action = ACTION_READ;
  731. break;
  732. }
  733. }
  734. if (argc == 1) {
  735. action = ACTION_PRINT_DEFAULT;
  736. return print_all();
  737. } else if (action == ACTION_PRINT_ALL) {
  738. return print_all();
  739. } else if (optind >= argc) {
  740. fprintf(stderr, "Expected an object path after options\n");
  741. exit(EXIT_FAILURE);
  742. }
  743. result = confdb_initialize (&handle, &callbacks);
  744. if (result != CS_OK) {
  745. fprintf (stderr, "Failed to initialize the objdb API. Error %d\n", result);
  746. exit (EXIT_FAILURE);
  747. }
  748. while (optind < argc) {
  749. switch (action) {
  750. case ACTION_READ:
  751. read_object(handle, argv[optind++]);
  752. break;
  753. case ACTION_WRITE:
  754. write_key(handle, argv[optind++]);
  755. break;
  756. case ACTION_CREATE:
  757. create_object(handle, argv[optind++]);
  758. break;
  759. case ACTION_CREATE_KEY:
  760. create_object_key(handle, argv[optind++]);
  761. break;
  762. case ACTION_DELETE:
  763. delete_object(handle, argv[optind++]);
  764. break;
  765. case ACTION_TRACK:
  766. track_object(handle, argv[optind++]);
  767. break;
  768. }
  769. }
  770. if (action == ACTION_TRACK) {
  771. listen_for_object_changes(handle);
  772. stop_tracking(handle);
  773. }
  774. result = confdb_finalize (handle);
  775. if (result != CS_OK) {
  776. fprintf (stderr, "Error finalizing objdb API. Error %d\n", result);
  777. exit(EXIT_FAILURE);
  778. }
  779. return 0;
  780. }