4
0

corosync-objctl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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 <signal.h>
  40. #include <unistd.h>
  41. #include <string.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_DELETE,
  54. ACTION_PRINT_ALL,
  55. ACTION_PRINT_DEFAULT,
  56. ACTION_TRACK,
  57. } action_types_t;
  58. typedef enum {
  59. FIND_OBJECT_ONLY,
  60. FIND_OBJECT_OR_KEY,
  61. FIND_KEY_ONLY
  62. } find_object_of_type_t;
  63. static void tail_key_changed(confdb_handle_t handle,
  64. confdb_change_type_t change_type,
  65. hdb_handle_t parent_object_handle,
  66. hdb_handle_t object_handle,
  67. void *object_name,
  68. int object_name_len,
  69. void *key_name,
  70. int key_name_len,
  71. void *key_value,
  72. int key_value_len);
  73. static void tail_object_created(confdb_handle_t handle,
  74. hdb_handle_t parent_object_handle,
  75. hdb_handle_t object_handle,
  76. uint8_t *name_pt,
  77. int name_len);
  78. static void tail_object_deleted(confdb_handle_t handle,
  79. hdb_handle_t parent_object_handle,
  80. uint8_t *name_pt,
  81. int name_len);
  82. confdb_callbacks_t callbacks = {
  83. .confdb_key_change_notify_fn = tail_key_changed,
  84. .confdb_object_create_change_notify_fn = tail_object_created,
  85. .confdb_object_delete_change_notify_fn = tail_object_deleted,
  86. };
  87. static int action;
  88. /* Recursively dump the object tree */
  89. static void print_config_tree(confdb_handle_t handle, hdb_handle_t parent_object_handle, char * parent_name)
  90. {
  91. hdb_handle_t object_handle;
  92. char object_name[OBJ_NAME_SIZE];
  93. int object_name_len;
  94. char key_name[OBJ_NAME_SIZE];
  95. int key_name_len;
  96. char key_value[OBJ_NAME_SIZE];
  97. int key_value_len;
  98. cs_error_t res;
  99. int children_printed;
  100. /* Show the keys */
  101. res = confdb_key_iter_start(handle, parent_object_handle);
  102. if (res != CS_OK) {
  103. fprintf(stderr, "error resetting key iterator for object %llu: %d\n", parent_object_handle, res);
  104. exit(EXIT_FAILURE);
  105. }
  106. children_printed = 0;
  107. while ( (res = confdb_key_iter(handle,
  108. parent_object_handle,
  109. key_name,
  110. &key_name_len,
  111. key_value,
  112. &key_value_len)) == CS_OK) {
  113. key_name[key_name_len] = '\0';
  114. key_value[key_value_len] = '\0';
  115. if (parent_name != NULL)
  116. printf("%s%c%s=%s\n", parent_name, SEPERATOR,key_name, key_value);
  117. else
  118. printf("%s=%s\n", key_name, key_value);
  119. children_printed++;
  120. }
  121. /* Show sub-objects */
  122. res = confdb_object_iter_start(handle, parent_object_handle);
  123. if (res != CS_OK) {
  124. fprintf(stderr, "error resetting object iterator for object %llu: %d\n", parent_object_handle, res);
  125. exit(EXIT_FAILURE);
  126. }
  127. while ( (res = confdb_object_iter(handle,
  128. parent_object_handle,
  129. &object_handle,
  130. object_name,
  131. &object_name_len)) == CS_OK) {
  132. object_name[object_name_len] = '\0';
  133. if (parent_name != NULL) {
  134. snprintf(key_value, OBJ_NAME_SIZE, "%s%c%s", parent_name, SEPERATOR, object_name);
  135. } else {
  136. if ((action == ACTION_PRINT_DEFAULT) && strcmp(object_name, "internal_configuration") == 0) continue;
  137. snprintf(key_value, OBJ_NAME_SIZE, "%s", object_name);
  138. }
  139. print_config_tree(handle, object_handle, key_value);
  140. children_printed++;
  141. }
  142. if (children_printed == 0 && parent_name != NULL) {
  143. printf("%s\n", parent_name);
  144. }
  145. }
  146. static int print_all(void)
  147. {
  148. confdb_handle_t handle;
  149. int result;
  150. result = confdb_initialize (&handle, &callbacks);
  151. if (result != CS_OK) {
  152. fprintf (stderr, "Could not initialize objdb library. Error %d\n", result);
  153. return 1;
  154. }
  155. print_config_tree(handle, OBJECT_PARENT_HANDLE, NULL);
  156. result = confdb_finalize (handle);
  157. return 0;
  158. }
  159. static int print_help(void)
  160. {
  161. printf("\n");
  162. printf ("usage: corosync-objctl object%ckey ... Print an object\n", SEPERATOR);
  163. printf (" corosync-objctl -c object%cchild_obj ... Create Object\n", SEPERATOR);
  164. printf (" corosync-objctl -d object%cchild_obj ... Delete object\n", SEPERATOR);
  165. printf (" corosync-objctl -w object%cchild_obj.key=value ... Create a key\n", SEPERATOR);
  166. printf (" corosync-objctl -t object%cchild_obj ... Track changes\n", SEPERATOR);
  167. printf (" corosync-objctl -a Print all objects\n");
  168. printf("\n");
  169. return 0;
  170. }
  171. static cs_error_t validate_name(char * obj_name_pt)
  172. {
  173. if ((strchr (obj_name_pt, SEPERATOR) == NULL) &&
  174. (strchr (obj_name_pt, '=') == NULL))
  175. return CS_OK;
  176. else
  177. return CS_ERR_INVALID_PARAM;
  178. }
  179. static void get_parent_name(const char * name_pt, char * parent_name)
  180. {
  181. char * tmp;
  182. strcpy(parent_name, name_pt);
  183. /* first remove the value (it could be a file path */
  184. tmp = strchr(parent_name, '=');
  185. if (tmp != NULL) *tmp = '\0';
  186. /* then truncate the child name */
  187. tmp = strrchr(parent_name, SEPERATOR);
  188. if (tmp != NULL) *tmp = '\0';
  189. }
  190. static void get_key(const char * name_pt, char * key_name, char * key_value)
  191. {
  192. char * tmp;
  193. char str_copy[OBJ_NAME_SIZE];
  194. strcpy(str_copy, name_pt);
  195. /* first remove the value (it could have a SEPERATOR in it */
  196. tmp = strchr(str_copy, '=');
  197. if (tmp != NULL && strlen(tmp) > 0) {
  198. strcpy(key_value, tmp+1);
  199. *tmp = '\0';
  200. } else {
  201. key_value[0] = '\0';
  202. }
  203. /* then remove the name */
  204. tmp = strrchr(str_copy, SEPERATOR);
  205. if (tmp == NULL) tmp = str_copy;
  206. strcpy(key_name, tmp+1);
  207. }
  208. static cs_error_t find_object (confdb_handle_t handle,
  209. char * name_pt,
  210. find_object_of_type_t type,
  211. hdb_handle_t * out_handle)
  212. {
  213. char * obj_name_pt;
  214. char * save_pt;
  215. hdb_handle_t obj_handle;
  216. confdb_handle_t parent_object_handle = OBJECT_PARENT_HANDLE;
  217. char tmp_name[OBJ_NAME_SIZE];
  218. cs_error_t res;
  219. strncpy (tmp_name, name_pt, OBJ_NAME_SIZE);
  220. obj_name_pt = strtok_r(tmp_name, SEPERATOR_STR, &save_pt);
  221. while (obj_name_pt != NULL) {
  222. res = confdb_object_find_start(handle, parent_object_handle);
  223. if (res != CS_OK) {
  224. fprintf (stderr, "Could not start object_find %d\n", res);
  225. exit (EXIT_FAILURE);
  226. }
  227. res = confdb_object_find(handle, parent_object_handle,
  228. obj_name_pt, strlen (obj_name_pt), &obj_handle);
  229. if (res != CS_OK) {
  230. return res;
  231. }
  232. parent_object_handle = obj_handle;
  233. obj_name_pt = strtok_r (NULL, SEPERATOR_STR, &save_pt);
  234. }
  235. *out_handle = parent_object_handle;
  236. return res;
  237. }
  238. static void read_object(confdb_handle_t handle, char * name_pt)
  239. {
  240. char parent_name[OBJ_NAME_SIZE];
  241. hdb_handle_t obj_handle;
  242. cs_error_t res;
  243. get_parent_name(name_pt, parent_name);
  244. res = find_object (handle, name_pt, FIND_OBJECT_OR_KEY, &obj_handle);
  245. if (res == CS_OK) {
  246. print_config_tree(handle, obj_handle, parent_name);
  247. }
  248. }
  249. static void write_key(confdb_handle_t handle, char * path_pt)
  250. {
  251. hdb_handle_t obj_handle;
  252. char parent_name[OBJ_NAME_SIZE];
  253. char key_name[OBJ_NAME_SIZE];
  254. char key_value[OBJ_NAME_SIZE];
  255. char old_key_value[OBJ_NAME_SIZE];
  256. int old_key_value_len;
  257. cs_error_t res;
  258. /* find the parent object */
  259. get_parent_name(path_pt, parent_name);
  260. get_key(path_pt, key_name, key_value);
  261. if (validate_name(key_name) != CS_OK) {
  262. fprintf(stderr, "Incorrect key name, can not have \"=\" or \"%c\"\n", SEPERATOR);
  263. exit(EXIT_FAILURE);
  264. }
  265. res = find_object (handle, parent_name, FIND_OBJECT_ONLY, &obj_handle);
  266. if (res != CS_OK) {
  267. fprintf(stderr, "Can't find parent object of \"%s\"\n", path_pt);
  268. exit(EXIT_FAILURE);
  269. }
  270. /* get the current key */
  271. res = confdb_key_get (handle,
  272. obj_handle,
  273. key_name,
  274. strlen(key_name),
  275. old_key_value,
  276. &old_key_value_len);
  277. if (res == CS_OK) {
  278. /* replace the current value */
  279. res = confdb_key_replace (handle,
  280. obj_handle,
  281. key_name,
  282. strlen(key_name),
  283. old_key_value,
  284. old_key_value_len,
  285. key_value,
  286. strlen(key_value));
  287. if (res != CS_OK)
  288. fprintf(stderr, "Failed to replace the key %s=%s. Error %d\n", key_name, key_value, res);
  289. } else {
  290. /* not there, create a new key */
  291. res = confdb_key_create (handle,
  292. obj_handle,
  293. key_name,
  294. strlen(key_name),
  295. key_value,
  296. strlen(key_value));
  297. if (res != CS_OK)
  298. fprintf(stderr, "Failed to create the key %s=%s. Error %d\n", key_name, key_value, res);
  299. }
  300. }
  301. static void create_object(confdb_handle_t handle, char * name_pt)
  302. {
  303. char * obj_name_pt;
  304. char * save_pt;
  305. hdb_handle_t obj_handle;
  306. hdb_handle_t parent_object_handle = OBJECT_PARENT_HANDLE;
  307. char tmp_name[OBJ_NAME_SIZE];
  308. cs_error_t res;
  309. strncpy (tmp_name, name_pt, OBJ_NAME_SIZE);
  310. obj_name_pt = strtok_r(tmp_name, SEPERATOR_STR, &save_pt);
  311. while (obj_name_pt != NULL) {
  312. res = confdb_object_find_start(handle, parent_object_handle);
  313. if (res != CS_OK) {
  314. fprintf (stderr, "Could not start object_find %d\n", res);
  315. exit (EXIT_FAILURE);
  316. }
  317. res = confdb_object_find(handle, parent_object_handle,
  318. obj_name_pt, strlen (obj_name_pt), &obj_handle);
  319. if (res != CS_OK) {
  320. if (validate_name(obj_name_pt) != CS_OK) {
  321. fprintf(stderr, "Incorrect object name \"%s\", \"=\" not allowed.\n",
  322. obj_name_pt);
  323. exit(EXIT_FAILURE);
  324. }
  325. res = confdb_object_create (handle,
  326. parent_object_handle,
  327. obj_name_pt,
  328. strlen (obj_name_pt),
  329. &obj_handle);
  330. if (res != CS_OK)
  331. fprintf(stderr, "Failed to create object \"%s\". Error %d.\n",
  332. obj_name_pt, res);
  333. }
  334. parent_object_handle = obj_handle;
  335. obj_name_pt = strtok_r (NULL, SEPERATOR_STR, &save_pt);
  336. }
  337. }
  338. static void tail_key_changed(confdb_handle_t handle,
  339. confdb_change_type_t change_type,
  340. hdb_handle_t parent_object_handle,
  341. hdb_handle_t object_handle,
  342. void *object_name_pt,
  343. int object_name_len,
  344. void *key_name_pt,
  345. int key_name_len,
  346. void *key_value_pt,
  347. int key_value_len)
  348. {
  349. char * on = (char*)object_name_pt;
  350. char * kn = (char*)key_name_pt;
  351. char * kv = (char*)key_value_pt;
  352. on[object_name_len] = '\0';
  353. kv[key_value_len] = '\0';
  354. kn[key_name_len] = '\0';
  355. printf("key_changed> %s.%s=%s\n", on, kn, kv);
  356. }
  357. static void tail_object_created(confdb_handle_t handle,
  358. hdb_handle_t parent_object_handle,
  359. hdb_handle_t object_handle,
  360. uint8_t *name_pt,
  361. int name_len)
  362. {
  363. name_pt[name_len] = '\0';
  364. printf("object_created> %s\n", name_pt);
  365. }
  366. static void tail_object_deleted(confdb_handle_t handle,
  367. hdb_handle_t parent_object_handle,
  368. uint8_t *name_pt,
  369. int name_len)
  370. {
  371. name_pt[name_len] = '\0';
  372. printf("object_deleted> %s\n", name_pt);
  373. }
  374. static void listen_for_object_changes(confdb_handle_t handle)
  375. {
  376. int result;
  377. fd_set read_fds;
  378. int select_fd;
  379. int quit = CS_FALSE;
  380. FD_ZERO (&read_fds);
  381. if (confdb_fd_get (handle, &select_fd) != CS_OK) {
  382. printf ("can't get the confdb selector object.\n");
  383. return;
  384. }
  385. printf ("Type \"q\" to finish\n");
  386. do {
  387. FD_SET (select_fd, &read_fds);
  388. FD_SET (STDIN_FILENO, &read_fds);
  389. result = select (select_fd + 1, &read_fds, 0, 0, 0);
  390. if (result == -1) {
  391. perror ("select\n");
  392. }
  393. if (FD_ISSET (STDIN_FILENO, &read_fds)) {
  394. char inbuf[3];
  395. if (fgets(inbuf, sizeof(inbuf), stdin) == NULL)
  396. quit = CS_TRUE;
  397. else if (strncmp(inbuf, "q", 1) == 0)
  398. quit = CS_TRUE;
  399. }
  400. if (FD_ISSET (select_fd, &read_fds)) {
  401. if (confdb_dispatch (handle, CONFDB_DISPATCH_ALL) != CS_OK)
  402. exit(1);
  403. }
  404. } while (result && quit == CS_FALSE);
  405. (void)confdb_stop_track_changes(handle);
  406. }
  407. static void track_object(confdb_handle_t handle, char * name_pt)
  408. {
  409. cs_error_t res;
  410. hdb_handle_t obj_handle;
  411. res = find_object (handle, name_pt, FIND_OBJECT_ONLY, &obj_handle);
  412. if (res != CS_OK) {
  413. fprintf (stderr, "Could not find object \"%s\". Error %d\n",
  414. name_pt, res);
  415. return;
  416. }
  417. res = confdb_track_changes (handle, obj_handle, CONFDB_TRACK_DEPTH_RECURSIVE);
  418. if (res != CS_OK) {
  419. fprintf (stderr, "Could not enable tracking on object \"%s\". Error %d\n",
  420. name_pt, res);
  421. return;
  422. }
  423. }
  424. static void stop_tracking(confdb_handle_t handle)
  425. {
  426. cs_error_t res;
  427. res = confdb_stop_track_changes (handle);
  428. if (res != CS_OK) {
  429. fprintf (stderr, "Could not stop tracking. Error %d\n", res);
  430. return;
  431. }
  432. }
  433. static void delete_object(confdb_handle_t handle, char * name_pt)
  434. {
  435. cs_error_t res;
  436. hdb_handle_t obj_handle;
  437. res = find_object (handle, name_pt, FIND_OBJECT_ONLY, &obj_handle);
  438. if (res == CS_OK) {
  439. res = confdb_object_destroy (handle, obj_handle);
  440. if (res != CS_OK)
  441. fprintf(stderr, "Failed to find object \"%s\" to delete. Error %d\n", name_pt, res);
  442. } else {
  443. char parent_name[OBJ_NAME_SIZE];
  444. char key_name[OBJ_NAME_SIZE];
  445. char key_value[OBJ_NAME_SIZE];
  446. /* find the parent object */
  447. get_parent_name(name_pt, parent_name);
  448. get_key(name_pt, key_name, key_value);
  449. res = find_object (handle, parent_name, FIND_OBJECT_ONLY, &obj_handle);
  450. if (res != CS_OK) {
  451. fprintf(stderr, "Failed to find the key's parent object \"%s\". Error %d\n", parent_name, res);
  452. exit (EXIT_FAILURE);
  453. }
  454. res = confdb_key_delete (handle,
  455. obj_handle,
  456. key_name,
  457. strlen(key_name),
  458. key_value,
  459. strlen(key_value));
  460. if (res != CS_OK)
  461. fprintf(stderr, "Failed to delete key \"%s=%s\" from object \"%s\". Error %d\n",
  462. key_name, key_value, parent_name, res);
  463. }
  464. }
  465. int main (int argc, char *argv[]) {
  466. confdb_handle_t handle;
  467. cs_error_t result;
  468. int c;
  469. action = ACTION_READ;
  470. for (;;){
  471. c = getopt (argc,argv,"hawcdtp:");
  472. if (c==-1) {
  473. break;
  474. }
  475. switch (c) {
  476. case 'h':
  477. return print_help();
  478. break;
  479. case 'a':
  480. action = ACTION_PRINT_ALL;
  481. break;
  482. case 'p':
  483. printf("%s:%d NOT Implemented yet.\n", __FUNCTION__, __LINE__);
  484. return -1;
  485. //return read_in_config_file();
  486. break;
  487. case 'c':
  488. action = ACTION_CREATE;
  489. break;
  490. case 'd':
  491. action = ACTION_DELETE;
  492. break;
  493. case 'w':
  494. action = ACTION_WRITE;
  495. break;
  496. case 't':
  497. action = ACTION_TRACK;
  498. break;
  499. default :
  500. action = ACTION_READ;
  501. break;
  502. }
  503. }
  504. if (argc == 1) {
  505. action = ACTION_PRINT_DEFAULT;
  506. return print_all();
  507. } else if (action == ACTION_PRINT_ALL) {
  508. return print_all();
  509. } else if (optind >= argc) {
  510. fprintf(stderr, "Expected an object path after options\n");
  511. exit(EXIT_FAILURE);
  512. }
  513. result = confdb_initialize (&handle, &callbacks);
  514. if (result != CS_OK) {
  515. fprintf (stderr, "Failed to initialize the objdb API. Error %d\n", result);
  516. exit (EXIT_FAILURE);
  517. }
  518. while (optind < argc) {
  519. switch (action) {
  520. case ACTION_READ:
  521. read_object(handle, argv[optind++]);
  522. break;
  523. case ACTION_WRITE:
  524. write_key(handle, argv[optind++]);
  525. break;
  526. case ACTION_CREATE:
  527. create_object(handle, argv[optind++]);
  528. break;
  529. case ACTION_DELETE:
  530. delete_object(handle, argv[optind++]);
  531. break;
  532. case ACTION_TRACK:
  533. track_object(handle, argv[optind++]);
  534. break;
  535. }
  536. }
  537. if (action == ACTION_TRACK) {
  538. listen_for_object_changes(handle);
  539. stop_tracking(handle);
  540. }
  541. result = confdb_finalize (handle);
  542. if (result != CS_OK) {
  543. fprintf (stderr, "Error finalizing objdb API. Error %d\n", result);
  544. exit(EXIT_FAILURE);
  545. }
  546. return 0;
  547. }