testconfdb.c 7.0 KB

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