corosync-objctl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. /*
  2. * Copyright (c) 2008 Allied Telesis Labs NZ
  3. * (c) 2012 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Angus Salkeld <angus.salkeld@alliedtelesis.co.nz>
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <config.h>
  36. #include <sys/select.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <errno.h>
  40. #include <unistd.h>
  41. #include <string.h>
  42. #include <ctype.h>
  43. #include <sys/types.h>
  44. #include <sys/un.h>
  45. #include <corosync/corotypes.h>
  46. #include <corosync/confdb.h>
  47. #define SEPERATOR '.'
  48. #define SEPERATOR_STR "."
  49. #define OBJ_NAME_SIZE 4096
  50. typedef enum {
  51. ACTION_READ,
  52. ACTION_WRITE,
  53. ACTION_CREATE,
  54. ACTION_CREATE_KEY,
  55. ACTION_DELETE,
  56. ACTION_PRINT_ALL,
  57. ACTION_PRINT_DEFAULT,
  58. ACTION_TRACK,
  59. } action_types_t;
  60. typedef enum {
  61. FIND_OBJECT_ONLY,
  62. FIND_OBJECT_OR_KEY,
  63. FIND_KEY_ONLY
  64. } find_object_of_type_t;
  65. static void tail_key_changed(confdb_handle_t handle,
  66. confdb_change_type_t change_type,
  67. hdb_handle_t parent_object_handle,
  68. hdb_handle_t object_handle,
  69. const void *object_name,
  70. size_t object_name_len,
  71. const void *key_name,
  72. size_t key_name_len,
  73. const void *key_value,
  74. size_t key_value_len);
  75. static void tail_object_created(confdb_handle_t handle,
  76. hdb_handle_t parent_object_handle,
  77. hdb_handle_t object_handle,
  78. const void *name_pt,
  79. size_t name_len);
  80. static void tail_object_deleted(confdb_handle_t handle,
  81. hdb_handle_t parent_object_handle,
  82. const void *name_pt,
  83. size_t name_len);
  84. static void create_object(confdb_handle_t handle, char * name_pt);
  85. static void create_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 show_binary = 0;
  95. static int action;
  96. static void print_binary_key (char *value, size_t value_len)
  97. {
  98. size_t i;
  99. char c;
  100. for (i = 0; i < value_len; i++) {
  101. c = value[i];
  102. if (c >= ' ' && c < 0x7f && c != '\\') {
  103. fputc (c, stdout);
  104. } else {
  105. if (c == '\\') {
  106. printf ("\\\\");
  107. } else {
  108. printf ("\\x%02X", c);
  109. }
  110. }
  111. }
  112. printf ("\n");
  113. }
  114. static void print_key (char *key_name, void *value, size_t value_len, confdb_value_types_t type)
  115. {
  116. switch (type) {
  117. case CONFDB_VALUETYPE_INT16:
  118. printf ("%s=%hd\n", key_name,
  119. *(int16_t*)value);
  120. break;
  121. case CONFDB_VALUETYPE_UINT16:
  122. printf ("%s=%hu\n", key_name,
  123. *(uint16_t*)value);
  124. break;
  125. case CONFDB_VALUETYPE_INT32:
  126. printf ("%s=%d\n", key_name,
  127. *(int32_t*)value);
  128. break;
  129. case CONFDB_VALUETYPE_UINT32:
  130. printf ("%s=%u\n", key_name,
  131. *(uint32_t*)value);
  132. break;
  133. case CONFDB_VALUETYPE_INT64:
  134. printf ("%s=%"PRIi64"\n", key_name,
  135. *(int64_t*)value);
  136. break;
  137. case CONFDB_VALUETYPE_UINT64:
  138. printf ("%s=%"PRIu64"\n", key_name,
  139. *(uint64_t*)value);
  140. break;
  141. case CONFDB_VALUETYPE_FLOAT:
  142. printf ("%s=%f\n", key_name,
  143. *(float*)value);
  144. break;
  145. case CONFDB_VALUETYPE_DOUBLE:
  146. printf ("%s=%f\n", key_name,
  147. *(double*)value);
  148. break;
  149. case CONFDB_VALUETYPE_STRING:
  150. printf ("%s=%s\n", key_name, (char*)value);
  151. break;
  152. default:
  153. case CONFDB_VALUETYPE_ANY:
  154. if (!show_binary) {
  155. printf ("%s=**binary**(%d)\n", key_name, type);
  156. } else {
  157. printf ("%s=", key_name);
  158. print_binary_key ((char *)value, value_len);
  159. }
  160. break;
  161. }
  162. }
  163. /* Recursively dump the object tree */
  164. static void print_config_tree(confdb_handle_t handle, hdb_handle_t parent_object_handle, char * parent_name)
  165. {
  166. hdb_handle_t object_handle;
  167. char object_name[OBJ_NAME_SIZE];
  168. size_t object_name_len;
  169. char key_name[OBJ_NAME_SIZE];
  170. char output_string[OBJ_NAME_SIZE];
  171. char *key_value=NULL;/* Dynamically allocated value */
  172. size_t key_value_len;
  173. cs_error_t res;
  174. int children_printed;
  175. confdb_value_types_t type;
  176. /* Show the keys */
  177. res = confdb_key_iter_start(handle, parent_object_handle);
  178. if (res != CS_OK) {
  179. fprintf(stderr, "error resetting key iterator for object "HDB_X_FORMAT" %d\n", parent_object_handle, res);
  180. exit(EXIT_FAILURE);
  181. }
  182. children_printed = 0;
  183. while ( (res = confdb_key_iter_typed2(handle,
  184. parent_object_handle,
  185. key_name,
  186. (void**)&key_value,
  187. &key_value_len,
  188. &type)) == CS_OK) {
  189. key_value[key_value_len] = '\0';
  190. if (parent_name != NULL)
  191. printf("%s%c", parent_name, SEPERATOR);
  192. print_key(key_name, key_value, key_value_len, type);
  193. free(key_value);
  194. key_value = NULL;
  195. children_printed++;
  196. }
  197. /* Show sub-objects */
  198. res = confdb_object_iter_start(handle, parent_object_handle);
  199. if (res != CS_OK) {
  200. fprintf(stderr, "error resetting object iterator for object "HDB_X_FORMAT" %d\n", parent_object_handle, res);
  201. exit(EXIT_FAILURE);
  202. }
  203. while ( (res = confdb_object_iter(handle,
  204. parent_object_handle,
  205. &object_handle,
  206. object_name,
  207. &object_name_len)) == CS_OK) {
  208. object_name[object_name_len] = '\0';
  209. if (parent_name != NULL) {
  210. snprintf(output_string, OBJ_NAME_SIZE, "%s%c%s", parent_name, SEPERATOR, object_name);
  211. } else {
  212. if ((action == ACTION_PRINT_DEFAULT) && strcmp(object_name, "internal_configuration") == 0) continue;
  213. snprintf(output_string, OBJ_NAME_SIZE, "%s", object_name);
  214. }
  215. print_config_tree(handle, object_handle, output_string);
  216. children_printed++;
  217. }
  218. if (children_printed == 0 && parent_name != NULL) {
  219. printf("%s\n", parent_name);
  220. }
  221. }
  222. static cs_error_t reload_db(confdb_handle_t handle)
  223. {
  224. char error[1024];
  225. cs_error_t ret;
  226. ret = confdb_reload(handle, 0, error, sizeof(error) - 1);
  227. if (ret != CONFDB_OK) {
  228. fprintf (stderr, "Error reloading DB %d\n", ret);
  229. }
  230. return ret;
  231. }
  232. static int read_in_config_file (char * filename)
  233. {
  234. confdb_handle_t handle;
  235. int result;
  236. int ignore;
  237. int c;
  238. FILE* fh;
  239. char buf[1024];
  240. char * line;
  241. char * end;
  242. char parent_name[OBJ_NAME_SIZE];
  243. if (access (filename, R_OK) != 0) {
  244. perror ("Couldn't access file.");
  245. return -1;
  246. }
  247. fh = fopen(filename, "r");
  248. if (fh == NULL) {
  249. perror ("Couldn't open file.");
  250. return -1;
  251. }
  252. result = confdb_initialize (&handle, &callbacks);
  253. if (result != CONFDB_OK) {
  254. fprintf (stderr, "Could not initialize objdb library. Error %d\n", result);
  255. fclose (fh);
  256. return -1;
  257. }
  258. while (fgets (buf, 1024, fh) != NULL) {
  259. /* find the first real character, if it is
  260. * a '#' then ignore this line.
  261. * else process.
  262. * if no real characters then also ignore.
  263. */
  264. ignore = 1;
  265. for (c = 0; c < 1024; c++) {
  266. if (isblank (buf[c]))
  267. continue;
  268. if (buf[c] == '#' || buf[c] == '\n') {
  269. ignore = 1;
  270. break;
  271. }
  272. ignore = 0;
  273. line = &buf[c];
  274. break;
  275. }
  276. if (ignore == 1)
  277. continue;
  278. /* kill the \n */
  279. end = strchr (line, '\n');
  280. if (end != NULL)
  281. *end = '\0';
  282. if (debug == 2)
  283. printf ("%d: %s\n", __LINE__, line);
  284. /* find the parent object */
  285. get_parent_name(line, parent_name);
  286. if (debug == 2)
  287. printf ("%d: %s\n", __LINE__, parent_name);
  288. /* create the object */
  289. create_object (handle, parent_name);
  290. /* write the attribute */
  291. write_key (handle, line);
  292. }
  293. reload_db(handle);
  294. confdb_finalize (handle);
  295. fclose (fh);
  296. return 0;
  297. }
  298. static int print_all(void)
  299. {
  300. confdb_handle_t handle;
  301. int result;
  302. result = confdb_initialize (&handle, &callbacks);
  303. if (result != CS_OK) {
  304. fprintf (stderr, "Could not initialize objdb library. Error %d\n", result);
  305. return 1;
  306. }
  307. print_config_tree(handle, OBJECT_PARENT_HANDLE, NULL);
  308. result = confdb_finalize (handle);
  309. return 0;
  310. }
  311. static int print_help(void)
  312. {
  313. printf("\n");
  314. printf ("usage: corosync-objctl [-b] object%ckey ... Print an object\n", SEPERATOR);
  315. printf (" corosync-objctl -c object%cchild_obj ... Create Object\n", SEPERATOR);
  316. printf (" corosync-objctl -d object%cchild_obj ... Delete object\n", SEPERATOR);
  317. printf (" corosync-objctl -w object%cchild_obj.key=value ... Create a key\n", SEPERATOR);
  318. printf (" corosync-objctl -n object%cchild_obj.key=value ... Create a new object with the key\n", SEPERATOR);
  319. printf (" corosync-objctl -t object%cchild_obj ... Track changes\n", SEPERATOR);
  320. printf (" corosync-objctl [-b] -a Print all objects\n");
  321. printf (" corosync-objctl -p <filename> Load in config from the specified file.\n");
  322. printf("\n");
  323. return 0;
  324. }
  325. static cs_error_t validate_name(char * obj_name_pt)
  326. {
  327. if ((strchr (obj_name_pt, SEPERATOR) == NULL) &&
  328. (strchr (obj_name_pt, '=') == NULL))
  329. return CS_OK;
  330. else
  331. return CS_ERR_INVALID_PARAM;
  332. }
  333. static void get_parent_name(const char * name_pt, char * parent_name)
  334. {
  335. char * tmp;
  336. strcpy(parent_name, name_pt);
  337. /* first remove the value (it could be a file path */
  338. tmp = strchr(parent_name, '=');
  339. if (tmp != NULL) {
  340. *tmp = '\0';
  341. tmp--;
  342. while (isblank (*tmp)) {
  343. *tmp = '\0';
  344. tmp--;
  345. }
  346. }
  347. /* then truncate the child name */
  348. tmp = strrchr(parent_name, SEPERATOR);
  349. if (tmp != NULL) *tmp = '\0';
  350. }
  351. static void get_key(const char * name_pt, char * key_name, char * key_value)
  352. {
  353. char * tmp = (char*)name_pt;
  354. char str_copy[OBJ_NAME_SIZE];
  355. char * copy_tmp = str_copy;
  356. int equals_seen = 0;
  357. int in_quotes = 0;
  358. /* strip out spaces when not in quotes */
  359. while (*tmp != '\0') {
  360. if (*tmp == '=')
  361. equals_seen = 1;
  362. if (equals_seen && *tmp == '"') {
  363. if (in_quotes)
  364. in_quotes = 0;
  365. else
  366. in_quotes = 1;
  367. }
  368. if (*tmp != ' ' || in_quotes) {
  369. *copy_tmp = *tmp;
  370. copy_tmp++;
  371. }
  372. tmp++;
  373. }
  374. *copy_tmp = '\0';
  375. /* first remove the value (it could have a SEPERATOR in it */
  376. tmp = strchr(str_copy, '=');
  377. if (tmp != NULL && strlen(tmp) > 0) {
  378. strcpy(key_value, tmp+1);
  379. *tmp = '\0';
  380. } else {
  381. key_value[0] = '\0';
  382. }
  383. /* then remove the name */
  384. tmp = strrchr(str_copy, SEPERATOR);
  385. if (tmp == NULL) tmp = str_copy;
  386. strcpy(key_name, tmp+1);
  387. }
  388. static cs_error_t find_object (confdb_handle_t handle,
  389. char * name_pt,
  390. find_object_of_type_t type,
  391. hdb_handle_t * out_handle)
  392. {
  393. char * obj_name_pt;
  394. char * save_pt;
  395. hdb_handle_t obj_handle;
  396. confdb_handle_t parent_object_handle = OBJECT_PARENT_HANDLE;
  397. char tmp_name[OBJ_NAME_SIZE];
  398. cs_error_t res = CS_OK;
  399. strncpy (tmp_name, name_pt, sizeof (tmp_name));
  400. tmp_name[sizeof (tmp_name) - 1] = '\0';
  401. obj_name_pt = strtok_r(tmp_name, SEPERATOR_STR, &save_pt);
  402. while (obj_name_pt != NULL) {
  403. res = confdb_object_find_start(handle, parent_object_handle);
  404. if (res != CS_OK) {
  405. fprintf (stderr, "Could not start object_find %d\n", res);
  406. exit (EXIT_FAILURE);
  407. }
  408. res = confdb_object_find(handle, parent_object_handle,
  409. obj_name_pt, strlen (obj_name_pt), &obj_handle);
  410. if (res != CS_OK) {
  411. return res;
  412. }
  413. parent_object_handle = obj_handle;
  414. obj_name_pt = strtok_r (NULL, SEPERATOR_STR, &save_pt);
  415. }
  416. *out_handle = parent_object_handle;
  417. return res;
  418. }
  419. static void read_object(confdb_handle_t handle, char * name_pt)
  420. {
  421. char parent_name[OBJ_NAME_SIZE];
  422. hdb_handle_t obj_handle;
  423. cs_error_t res;
  424. get_parent_name(name_pt, parent_name);
  425. res = find_object (handle, name_pt, FIND_OBJECT_OR_KEY, &obj_handle);
  426. if (res == CS_OK) {
  427. print_config_tree(handle, obj_handle, parent_name);
  428. }
  429. }
  430. static void write_key(confdb_handle_t handle, char * path_pt)
  431. {
  432. hdb_handle_t obj_handle;
  433. char parent_name[OBJ_NAME_SIZE];
  434. char key_name[OBJ_NAME_SIZE];
  435. char key_value[OBJ_NAME_SIZE];
  436. char old_key_value[OBJ_NAME_SIZE];
  437. char *old_key_value_ptr = old_key_value;
  438. size_t old_key_value_len;
  439. cs_error_t res;
  440. confdb_value_types_t type;
  441. /* find the parent object */
  442. get_parent_name(path_pt, parent_name);
  443. get_key(path_pt, key_name, key_value);
  444. if (debug == 1)
  445. printf ("%d: key:\"%s\", value:\"%s\"\n",
  446. __LINE__, key_name, key_value);
  447. if (validate_name(key_name) != CS_OK) {
  448. fprintf(stderr, "Incorrect key name, can not have \"=\" or \"%c\"\n", SEPERATOR);
  449. exit(EXIT_FAILURE);
  450. }
  451. res = find_object (handle, parent_name, FIND_OBJECT_ONLY, &obj_handle);
  452. if (res != CS_OK) {
  453. fprintf(stderr, "Can't find parent object of \"%s\"\n", path_pt);
  454. exit(EXIT_FAILURE);
  455. }
  456. /* get the current key */
  457. res = confdb_key_get_typed2 (handle,
  458. obj_handle,
  459. key_name,
  460. (void**)&old_key_value_ptr,
  461. &old_key_value_len, &type);
  462. if (debug == 1 && res==CS_OK)
  463. printf ("%d: key:\"%s\", old value:\"%s\"\n",
  464. __LINE__, key_name, old_key_value);
  465. if (res == CS_OK) {
  466. /* replace the current value */
  467. res = confdb_key_replace (handle,
  468. obj_handle,
  469. key_name,
  470. strlen(key_name),
  471. old_key_value,
  472. old_key_value_len,
  473. key_value,
  474. strlen(key_value)+1);
  475. if (res != CS_OK)
  476. fprintf(stderr, "Failed to replace the key %s=%s. Error %d\n", key_name, key_value, res);
  477. } else {
  478. /* not there, create a new key */
  479. res = confdb_key_create_typed (handle,
  480. obj_handle,
  481. key_name,
  482. key_value,
  483. strlen(key_value)+1,
  484. CONFDB_VALUETYPE_STRING);
  485. if (res != CS_OK)
  486. fprintf(stderr, "Failed to create the key %s=%s. Error %d\n", key_name, key_value, res);
  487. }
  488. }
  489. static void create_object(confdb_handle_t handle, char * name_pt)
  490. {
  491. char * obj_name_pt;
  492. char * save_pt;
  493. hdb_handle_t obj_handle;
  494. hdb_handle_t parent_object_handle = OBJECT_PARENT_HANDLE;
  495. char tmp_name[OBJ_NAME_SIZE];
  496. cs_error_t res;
  497. strncpy (tmp_name, name_pt, sizeof (tmp_name));
  498. tmp_name[sizeof (tmp_name) - 1] = '\0';
  499. obj_name_pt = strtok_r(tmp_name, SEPERATOR_STR, &save_pt);
  500. while (obj_name_pt != NULL) {
  501. res = confdb_object_find_start(handle, parent_object_handle);
  502. if (res != CS_OK) {
  503. fprintf (stderr, "Could not start object_find %d\n", res);
  504. exit (EXIT_FAILURE);
  505. }
  506. res = confdb_object_find(handle, parent_object_handle,
  507. obj_name_pt, strlen (obj_name_pt), &obj_handle);
  508. if (res != CS_OK) {
  509. if (validate_name(obj_name_pt) != CS_OK) {
  510. fprintf(stderr, "Incorrect object name \"%s\", \"=\" not allowed.\n",
  511. obj_name_pt);
  512. exit(EXIT_FAILURE);
  513. }
  514. if (debug)
  515. printf ("%s:%d: %s\n", __func__,__LINE__, obj_name_pt);
  516. res = confdb_object_create (handle,
  517. parent_object_handle,
  518. obj_name_pt,
  519. strlen (obj_name_pt),
  520. &obj_handle);
  521. if (res != CS_OK)
  522. fprintf(stderr, "Failed to create object \"%s\". Error %d.\n",
  523. obj_name_pt, res);
  524. }
  525. parent_object_handle = obj_handle;
  526. obj_name_pt = strtok_r (NULL, SEPERATOR_STR, &save_pt);
  527. }
  528. }
  529. static void create_object_key(confdb_handle_t handle, char *name_pt)
  530. {
  531. char * obj_name_pt;
  532. char * new_obj_name_pt;
  533. char * save_pt;
  534. hdb_handle_t obj_handle;
  535. hdb_handle_t parent_object_handle = OBJECT_PARENT_HANDLE;
  536. char tmp_name[OBJ_NAME_SIZE];
  537. cs_error_t res;
  538. char parent_name[OBJ_NAME_SIZE];
  539. char key_name[OBJ_NAME_SIZE];
  540. char key_value[OBJ_NAME_SIZE];
  541. get_parent_name(name_pt, parent_name);
  542. get_key(name_pt, key_name, key_value);
  543. strncpy (tmp_name, parent_name, sizeof (tmp_name));
  544. tmp_name[sizeof (tmp_name) - 1] = '\0';
  545. obj_name_pt = strtok_r(tmp_name, SEPERATOR_STR, &save_pt);
  546. /*
  547. * Create parent object tree
  548. */
  549. while (obj_name_pt != NULL) {
  550. res = confdb_object_find_start(handle, parent_object_handle);
  551. if (res != CS_OK) {
  552. fprintf (stderr, "Could not start object_find %d\n", res);
  553. exit (EXIT_FAILURE);
  554. }
  555. new_obj_name_pt = strtok_r (NULL, SEPERATOR_STR, &save_pt);
  556. res = confdb_object_find(handle, parent_object_handle,
  557. obj_name_pt, strlen (obj_name_pt), &obj_handle);
  558. if (res != CS_OK || new_obj_name_pt == NULL) {
  559. if (validate_name(obj_name_pt) != CS_OK) {
  560. fprintf(stderr, "Incorrect object name \"%s\", \"=\" not allowed.\n",
  561. obj_name_pt);
  562. exit(EXIT_FAILURE);
  563. }
  564. if (debug)
  565. printf ("%s:%d: %s\n", __func__,__LINE__, obj_name_pt);
  566. res = confdb_object_create (handle,
  567. parent_object_handle,
  568. obj_name_pt,
  569. strlen (obj_name_pt),
  570. &obj_handle);
  571. if (res != CS_OK) {
  572. fprintf(stderr, "Failed to create object \"%s\". Error %d.\n",
  573. obj_name_pt, res);
  574. }
  575. }
  576. parent_object_handle = obj_handle;
  577. obj_name_pt = new_obj_name_pt;
  578. }
  579. /*
  580. * Create key
  581. */
  582. res = confdb_key_create_typed (handle,
  583. obj_handle,
  584. key_name,
  585. key_value,
  586. strlen(key_value)+1,
  587. CONFDB_VALUETYPE_STRING);
  588. if (res != CS_OK) {
  589. fprintf(stderr,
  590. "Failed to create the key %s=%s. Error %d\n",
  591. key_name, key_value, res);
  592. }
  593. }
  594. /* Print "?" in place of any non-printable byte of OBJ. */
  595. static void print_name (FILE *fp, const void *obj, size_t obj_len)
  596. {
  597. const char *p = obj;
  598. size_t i;
  599. for (i = 0; i < obj_len; i++) {
  600. int c = *p++;
  601. if (!isprint (c)) {
  602. c = '?';
  603. }
  604. fputc (c, fp);
  605. }
  606. }
  607. static void tail_key_changed(confdb_handle_t handle,
  608. confdb_change_type_t change_type,
  609. hdb_handle_t parent_object_handle,
  610. hdb_handle_t object_handle,
  611. const void *object_name_pt,
  612. size_t object_name_len,
  613. const void *key_name_pt,
  614. size_t key_name_len,
  615. const void *key_value_pt,
  616. size_t key_value_len)
  617. {
  618. /* printf("key_changed> %.*s.%.*s=%.*s\n", */
  619. fputs("key_changed> ", stdout);
  620. print_name (stdout, object_name_pt, object_name_len);
  621. fputs(".", stdout);
  622. print_name (stdout, key_name_pt, key_name_len);
  623. fputs("=", stdout);
  624. print_name (stdout, key_value_pt, key_value_len);
  625. fputs("\n", stdout);
  626. }
  627. static void tail_object_created(confdb_handle_t handle,
  628. hdb_handle_t parent_object_handle,
  629. hdb_handle_t object_handle,
  630. const void *name_pt,
  631. size_t name_len)
  632. {
  633. fputs("object_created>", stdout);
  634. print_name(stdout, name_pt, name_len);
  635. fputs("\n", stdout);
  636. }
  637. static void tail_object_deleted(confdb_handle_t handle,
  638. hdb_handle_t parent_object_handle,
  639. const void *name_pt,
  640. size_t name_len)
  641. {
  642. fputs("object_deleted>", stdout);
  643. print_name(stdout, name_pt, name_len);
  644. fputs("\n", stdout);
  645. }
  646. static void listen_for_object_changes(confdb_handle_t handle)
  647. {
  648. int result;
  649. fd_set read_fds;
  650. int select_fd;
  651. int quit = CS_FALSE;
  652. FD_ZERO (&read_fds);
  653. if (confdb_fd_get (handle, &select_fd) != CS_OK) {
  654. printf ("can't get the confdb selector object.\n");
  655. return;
  656. }
  657. printf ("Type \"q\" to finish\n");
  658. do {
  659. FD_SET (select_fd, &read_fds);
  660. FD_SET (STDIN_FILENO, &read_fds);
  661. result = select (select_fd + 1, &read_fds, 0, 0, 0);
  662. if (result == -1) {
  663. perror ("select\n");
  664. }
  665. if (FD_ISSET (STDIN_FILENO, &read_fds)) {
  666. char inbuf[3];
  667. if (fgets(inbuf, sizeof(inbuf), stdin) == NULL)
  668. quit = CS_TRUE;
  669. else if (strncmp(inbuf, "q", 1) == 0)
  670. quit = CS_TRUE;
  671. }
  672. if (FD_ISSET (select_fd, &read_fds)) {
  673. if (confdb_dispatch (handle, CONFDB_DISPATCH_ALL) != CS_OK)
  674. exit(1);
  675. }
  676. } while (result && quit == CS_FALSE);
  677. (void)confdb_stop_track_changes(handle);
  678. }
  679. static void track_object(confdb_handle_t handle, char * name_pt)
  680. {
  681. cs_error_t res;
  682. hdb_handle_t obj_handle;
  683. res = find_object (handle, name_pt, FIND_OBJECT_ONLY, &obj_handle);
  684. if (res != CS_OK) {
  685. fprintf (stderr, "Could not find object \"%s\". Error %d\n",
  686. name_pt, res);
  687. return;
  688. }
  689. res = confdb_track_changes (handle, obj_handle, CONFDB_TRACK_DEPTH_RECURSIVE);
  690. if (res != CS_OK) {
  691. fprintf (stderr, "Could not enable tracking on object \"%s\". Error %d\n",
  692. name_pt, res);
  693. return;
  694. }
  695. }
  696. static void stop_tracking(confdb_handle_t handle)
  697. {
  698. cs_error_t res;
  699. res = confdb_stop_track_changes (handle);
  700. if (res != CS_OK) {
  701. fprintf (stderr, "Could not stop tracking. Error %d\n", res);
  702. return;
  703. }
  704. }
  705. static void delete_object(confdb_handle_t handle, char * name_pt)
  706. {
  707. cs_error_t res;
  708. hdb_handle_t obj_handle;
  709. res = find_object (handle, name_pt, FIND_OBJECT_ONLY, &obj_handle);
  710. if (res == CS_OK) {
  711. res = confdb_object_destroy (handle, obj_handle);
  712. if (res != CS_OK)
  713. fprintf(stderr, "Failed to find object \"%s\" to delete. Error %d\n", name_pt, res);
  714. } else {
  715. char parent_name[OBJ_NAME_SIZE];
  716. char key_name[OBJ_NAME_SIZE];
  717. char key_value[OBJ_NAME_SIZE];
  718. /* find the parent object */
  719. get_parent_name(name_pt, parent_name);
  720. get_key(name_pt, key_name, key_value);
  721. res = find_object (handle, parent_name, FIND_OBJECT_ONLY, &obj_handle);
  722. if (res != CS_OK) {
  723. fprintf(stderr, "Failed to find the key's parent object \"%s\". Error %d\n", parent_name, res);
  724. exit (EXIT_FAILURE);
  725. }
  726. res = confdb_key_delete (handle,
  727. obj_handle,
  728. key_name,
  729. strlen(key_name),
  730. key_value,
  731. strlen(key_value));
  732. if (res != CS_OK)
  733. fprintf(stderr, "Failed to delete key \"%s=%s\" from object \"%s\". Error %d\n",
  734. key_name, key_value, parent_name, res);
  735. }
  736. }
  737. int main (int argc, char *argv[]) {
  738. confdb_handle_t handle;
  739. cs_error_t result;
  740. int c;
  741. action = ACTION_READ;
  742. for (;;){
  743. c = getopt (argc,argv,"habwncvdtp:");
  744. if (c==-1) {
  745. break;
  746. }
  747. switch (c) {
  748. case 'v':
  749. debug++;
  750. break;
  751. case 'h':
  752. return print_help();
  753. break;
  754. case 'a':
  755. action = ACTION_PRINT_ALL;
  756. break;
  757. case 'b':
  758. show_binary++;
  759. break;
  760. case 'p':
  761. return read_in_config_file (optarg);
  762. break;
  763. case 'c':
  764. action = ACTION_CREATE;
  765. break;
  766. case 'd':
  767. action = ACTION_DELETE;
  768. break;
  769. case 'w':
  770. action = ACTION_WRITE;
  771. break;
  772. case 'n':
  773. action = ACTION_CREATE_KEY;
  774. break;
  775. case 't':
  776. action = ACTION_TRACK;
  777. break;
  778. default :
  779. action = ACTION_READ;
  780. break;
  781. }
  782. }
  783. if (argc == 1) {
  784. action = ACTION_PRINT_DEFAULT;
  785. return print_all();
  786. } else if (action == ACTION_PRINT_ALL) {
  787. return print_all();
  788. } else if (optind >= argc) {
  789. fprintf(stderr, "Expected an object path after options\n");
  790. exit(EXIT_FAILURE);
  791. }
  792. result = confdb_initialize (&handle, &callbacks);
  793. if (result != CS_OK) {
  794. fprintf (stderr, "Failed to initialize the objdb API. Error %d\n", result);
  795. exit (EXIT_FAILURE);
  796. }
  797. while (optind < argc) {
  798. switch (action) {
  799. case ACTION_READ:
  800. read_object(handle, argv[optind++]);
  801. break;
  802. case ACTION_WRITE:
  803. write_key(handle, argv[optind++]);
  804. break;
  805. case ACTION_CREATE:
  806. create_object(handle, argv[optind++]);
  807. break;
  808. case ACTION_CREATE_KEY:
  809. create_object_key(handle, argv[optind++]);
  810. break;
  811. case ACTION_DELETE:
  812. delete_object(handle, argv[optind++]);
  813. break;
  814. case ACTION_TRACK:
  815. track_object(handle, argv[optind++]);
  816. break;
  817. }
  818. }
  819. if (action == ACTION_TRACK) {
  820. listen_for_object_changes(handle);
  821. stop_tracking(handle);
  822. } else {
  823. reload_db(handle);
  824. }
  825. result = confdb_finalize (handle);
  826. if (result != CS_OK) {
  827. fprintf (stderr, "Error finalizing objdb API. Error %d\n", result);
  828. exit(EXIT_FAILURE);
  829. }
  830. return 0;
  831. }