4
0

testconfdb.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) 2008 Red Hat Inc
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield <ccaulfie@redhat.com>
  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 "saAis.h"
  43. #include "confdb.h"
  44. /* Callbacks are not supported yet */
  45. confdb_callbacks_t callbacks = {
  46. .confdb_key_change_notify_fn = NULL,
  47. .confdb_object_create_change_notify_fn = NULL,
  48. .confdb_object_delete_change_notify_fn = NULL
  49. };
  50. /* Recursively dump the object tree */
  51. static void print_config_tree(confdb_handle_t handle, unsigned int parent_object_handle, int depth)
  52. {
  53. unsigned int object_handle;
  54. char object_name[1024];
  55. int object_name_len;
  56. char key_name[1024];
  57. int key_name_len;
  58. char key_value[1024];
  59. int key_value_len;
  60. int res;
  61. int i;
  62. /* Show the keys */
  63. res = confdb_key_iter_start(handle, parent_object_handle);
  64. if (res != SA_AIS_OK) {
  65. printf( "error resetting key iterator for object %d: %d\n", parent_object_handle, res);
  66. return;
  67. }
  68. while ( (res = confdb_key_iter(handle, parent_object_handle, key_name, &key_name_len,
  69. key_value, &key_value_len)) == SA_AIS_OK) {
  70. key_name[key_name_len] = '\0';
  71. key_value[key_value_len] = '\0';
  72. for (i=0; i<depth; i++) printf(" ");
  73. printf(" KEY %s=%s\n", key_name, key_value);
  74. }
  75. /* Show sub-objects */
  76. res = confdb_object_iter_start(handle, parent_object_handle);
  77. if (res != SA_AIS_OK) {
  78. printf( "error resetting object iterator for object %d: %d\n", parent_object_handle, res);
  79. return;
  80. }
  81. while ( (res = confdb_object_iter(handle, parent_object_handle, &object_handle, object_name, &object_name_len)) == SA_AIS_OK) {
  82. unsigned int parent;
  83. res = confdb_object_parent_get(handle, object_handle, &parent);
  84. if (res != SA_AIS_OK) {
  85. printf( "error getting parent for object %d: %d\n", object_handle, res);
  86. return;
  87. }
  88. for (i=0; i<depth; i++) printf(" ");
  89. object_name[object_name_len] = '\0';
  90. printf("OBJECT: %s (%u, parent: %u)\n", object_name, object_handle, parent);
  91. /* Down we go ... */
  92. print_config_tree(handle, object_handle, depth+1);
  93. }
  94. }
  95. static void do_write_tests(confdb_handle_t handle)
  96. {
  97. int res;
  98. unsigned int object_handle;
  99. char error_string[1024];
  100. /* Add a scratch object and put some keys into it */
  101. res = confdb_object_create(handle, OBJECT_PARENT_HANDLE, (void *)"testconfdb", strlen("testconfdb"), &object_handle);
  102. if (res != SA_AIS_OK) {
  103. printf( "error creating 'testconfdb' object: %d\n", res);
  104. return;
  105. }
  106. res = confdb_key_create(handle, object_handle, "testkey", strlen("testkey"), "one", strlen("one"));
  107. if (res != SA_AIS_OK) {
  108. printf( "error creating 'testconfdb' key 1: %d\n", res);
  109. return;
  110. }
  111. res = confdb_key_create(handle, object_handle, "testkey", strlen("testkey"), "two", strlen("two"));
  112. if (res != SA_AIS_OK) {
  113. printf( "error creating 'testconfdb' key 2: %d\n", res);
  114. return;
  115. }
  116. res = confdb_key_create(handle, object_handle, "grot", strlen("grot"), "perrins", strlen("perrins"));
  117. if (res != SA_AIS_OK) {
  118. printf( "error creating 'testconfdb' key 3: %d\n", res);
  119. return;
  120. }
  121. res = confdb_key_replace(handle, object_handle, "testkey", strlen("testkey"), "two", strlen("two"),
  122. "newtwo", strlen("newtwo"));
  123. if (res != SA_AIS_OK) {
  124. printf( "error replace 'testconfdb' key 2: %d\n", res);
  125. return;
  126. }
  127. /* Print it for verification */
  128. print_config_tree(handle, object_handle, 0);
  129. printf("-------------------------\n");
  130. /* Remove it.
  131. Check that it doesn't exist when the full tree dump runs next */
  132. res = confdb_object_destroy(handle, object_handle);
  133. if (res != SA_AIS_OK) {
  134. printf( "error destroying 'testconfdb' object: %d\n", res);
  135. return;
  136. }
  137. res = confdb_write(handle, error_string);
  138. printf("confdb_write returned %d: %s\n", res, error_string);
  139. }
  140. int main (int argc, char *argv[]) {
  141. confdb_handle_t handle;
  142. int result;
  143. unsigned int totem_handle;
  144. char key_value[256];
  145. int value_len;
  146. result = confdb_initialize (&handle, &callbacks);
  147. if (result != SA_AIS_OK) {
  148. printf ("Could not initialize Cluster Configuration Database API instance error %d\n", result);
  149. exit (1);
  150. }
  151. if (argv[1] && strcmp(argv[1], "write")==0)
  152. do_write_tests(handle);
  153. /* Test iterators */
  154. print_config_tree(handle, OBJECT_PARENT_HANDLE, 0);
  155. /* Find "totem" and dump bits of it again, to test the direct APIs */
  156. result = confdb_object_find_start(handle, OBJECT_PARENT_HANDLE);
  157. if (result != SA_AIS_OK) {
  158. printf ("Could not start object_find %d\n", result);
  159. exit (1);
  160. }
  161. result = confdb_object_find(handle, OBJECT_PARENT_HANDLE, "totem", strlen("totem"), &totem_handle);
  162. if (result != SA_AIS_OK) {
  163. printf ("Could not object_find \"totem\": %d\n", result);
  164. exit (1);
  165. }
  166. result = confdb_key_get(handle, totem_handle, "version", strlen("version"), key_value, &value_len);
  167. if (result != SA_AIS_OK) {
  168. printf ("Could not get \"version\" key: %d\n", result);
  169. exit (1);
  170. }
  171. key_value[value_len] = '\0';
  172. printf("totem/version = '%s'\n", key_value);
  173. result = confdb_key_get(handle, totem_handle, "secauth", strlen("secauth"), key_value, &value_len);
  174. if (result != SA_AIS_OK) {
  175. printf ("Could not get \"secauth\" key: %d\n", result);
  176. exit (1);
  177. }
  178. key_value[value_len] = '\0';
  179. printf("totem/secauth = '%s'\n", key_value);
  180. /* Try a call that fails */
  181. result = confdb_key_get(handle, totem_handle, "grot", strlen("grot"), key_value, &value_len);
  182. printf ("Get \"grot\" key returned: %d (should fail)\n", result);
  183. result = confdb_finalize (handle);
  184. printf ("Finalize result is %d (should be 1)\n", result);
  185. return (0);
  186. }