corosync-objctl.c 17 KB

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