testconfdb.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. /* Add a scratch object and put some keys into it */
  98. res = confdb_object_create(handle, OBJECT_PARENT_HANDLE, (void *)"testconfdb", strlen("testconfdb"), &object_handle);
  99. if (res != SA_AIS_OK) {
  100. printf( "error creating 'testconfdb' object: %d\n", res);
  101. return;
  102. }
  103. res = confdb_key_create(handle, object_handle, "testkey", strlen("testkey"), "one", strlen("one"));
  104. if (res != SA_AIS_OK) {
  105. printf( "error creating 'testconfdb' key 1: %d\n", res);
  106. return;
  107. }
  108. res = confdb_key_create(handle, object_handle, "testkey", strlen("testkey"), "two", strlen("two"));
  109. if (res != SA_AIS_OK) {
  110. printf( "error creating 'testconfdb' key 2: %d\n", res);
  111. return;
  112. }
  113. res = confdb_key_create(handle, object_handle, "grot", strlen("grot"), "perrins", strlen("perrins"));
  114. if (res != SA_AIS_OK) {
  115. printf( "error creating 'testconfdb' key 3: %d\n", res);
  116. return;
  117. }
  118. res = confdb_key_replace(handle, object_handle, "testkey", strlen("testkey"), "two", strlen("two"),
  119. "newtwo", strlen("newtwo"));
  120. if (res != SA_AIS_OK) {
  121. printf( "error replace 'testconfdb' key 2: %d\n", res);
  122. return;
  123. }
  124. /* Print it for verification */
  125. print_config_tree(handle, object_handle, 0);
  126. printf("-------------------------\n");
  127. /* Remove it.
  128. Check that it doesn't exist when the full tree dump runs next */
  129. res = confdb_object_destroy(handle, object_handle);
  130. if (res != SA_AIS_OK) {
  131. printf( "error destroying 'testconfdb' object: %d\n", res);
  132. return;
  133. }
  134. }
  135. int main (int argc, char *argv[]) {
  136. confdb_handle_t handle;
  137. int result;
  138. unsigned int totem_handle;
  139. char key_value[256];
  140. int value_len;
  141. result = confdb_initialize (&handle, &callbacks);
  142. if (result != SA_AIS_OK) {
  143. printf ("Could not initialize Cluster Configuration Database API instance error %d\n", result);
  144. exit (1);
  145. }
  146. if (argv[1] && strcmp(argv[1], "write")==0)
  147. do_write_tests(handle);
  148. /* Test iterators */
  149. print_config_tree(handle, OBJECT_PARENT_HANDLE, 0);
  150. /* Find "totem" and dump bits of it again, to test the direct APIs */
  151. result = confdb_object_find_start(handle, OBJECT_PARENT_HANDLE);
  152. if (result != SA_AIS_OK) {
  153. printf ("Could not start object_find %d\n", result);
  154. exit (1);
  155. }
  156. result = confdb_object_find(handle, OBJECT_PARENT_HANDLE, "totem", strlen("totem"), &totem_handle);
  157. if (result != SA_AIS_OK) {
  158. printf ("Could not object_find \"totem\": %d\n", result);
  159. exit (1);
  160. }
  161. result = confdb_key_get(handle, totem_handle, "version", strlen("version"), key_value, &value_len);
  162. if (result != SA_AIS_OK) {
  163. printf ("Could not get \"version\" key: %d\n", result);
  164. exit (1);
  165. }
  166. key_value[value_len] = '\0';
  167. printf("totem/version = '%s'\n", key_value);
  168. result = confdb_key_get(handle, totem_handle, "secauth", strlen("secauth"), key_value, &value_len);
  169. if (result != SA_AIS_OK) {
  170. printf ("Could not get \"secauth\" key: %d\n", result);
  171. exit (1);
  172. }
  173. key_value[value_len] = '\0';
  174. printf("totem/secauth = '%s'\n", key_value);
  175. /* Try a call that fails */
  176. result = confdb_key_get(handle, totem_handle, "grot", strlen("grot"), key_value, &value_len);
  177. printf ("Get \"grot\" key returned: %d (should fail)\n", result);
  178. result = confdb_finalize (handle);
  179. printf ("Finalize result is %d (should be 1)\n", result);
  180. return (0);
  181. }