testlck.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@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 <string.h>
  37. #include <errno.h>
  38. #include <signal.h>
  39. #include <unistd.h>
  40. #include <pthread.h>
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <sys/select.h>
  44. #include <sys/un.h>
  45. #include "saAis.h"
  46. #include "saLck.h"
  47. SaNameT resource_name_async;
  48. SaLckResourceHandleT resource_handle_async;
  49. void testLckResourceOpenCallback (
  50. SaInvocationT invocation,
  51. SaLckResourceHandleT lockResourceHandle,
  52. SaAisErrorT error)
  53. {
  54. printf ("testLckResourceOpenCallback invocation %llu error %d\n",
  55. (unsigned long long)invocation, error);
  56. resource_handle_async = lockResourceHandle;
  57. }
  58. void testLckLockGrantCallback (
  59. SaInvocationT invocation,
  60. SaLckLockStatusT lockStatus,
  61. SaAisErrorT error)
  62. {
  63. printf ("testLckLockGrantCallback invocation %llu status %d error %d\n",
  64. (unsigned long long)invocation, lockStatus, error);
  65. }
  66. SaLckLockIdT pr_lock_id;
  67. SaLckLockIdT pr_lock_async_id;
  68. void testLckLockWaiterCallback (
  69. SaLckWaiterSignalT waiterSignal,
  70. SaLckLockIdT lockId,
  71. SaLckLockModeT modeHeld,
  72. SaLckLockModeT modeRequested)
  73. {
  74. int result;
  75. printf ("waiter callback mode held %d mode requested %d lock id %llu\n",
  76. modeHeld,
  77. modeRequested,
  78. (unsigned long long)lockId);
  79. printf ("pr lock id %llu\n", (unsigned long long)pr_lock_async_id);
  80. result = saLckResourceUnlockAsync (
  81. 25,
  82. lockId);
  83. printf ("saLckResourceUnlockAsync result %d (should be 1)\n", result);
  84. }
  85. void testLckResourceUnlockCallback (
  86. SaInvocationT invocation,
  87. SaAisErrorT error)
  88. {
  89. printf ("testLckResourceUnlockCallback async invocation %llu error %d\n",
  90. (unsigned long long)invocation, error);
  91. }
  92. SaLckCallbacksT callbacks = {
  93. .saLckResourceOpenCallback = testLckResourceOpenCallback,
  94. .saLckLockGrantCallback = testLckLockGrantCallback,
  95. .saLckLockWaiterCallback = testLckLockWaiterCallback,
  96. .saLckResourceUnlockCallback = testLckResourceUnlockCallback
  97. };
  98. SaVersionT version = { 'B', 1, 1 };
  99. void setSaNameT (SaNameT *name, char *str) {
  100. strncpy ((char *)name->value, str, SA_MAX_NAME_LENGTH);
  101. if (strlen ((char *)name->value) > SA_MAX_NAME_LENGTH) {
  102. name->length = SA_MAX_NAME_LENGTH;
  103. } else {
  104. name->length = strlen (str);
  105. }
  106. }
  107. void sigintr_handler (int signum) {
  108. exit (0);
  109. }
  110. struct th_data {
  111. SaLckHandleT handle;
  112. };
  113. void *th_dispatch (void *arg)
  114. {
  115. struct th_data *th_data = (struct th_data *)arg;
  116. saLckDispatch (th_data->handle, SA_DISPATCH_BLOCKING);
  117. return (0);
  118. }
  119. int main (void) {
  120. SaLckHandleT handle;
  121. SaLckResourceHandleT resource_handle;
  122. int result;
  123. SaLckLockIdT ex_lock_id;
  124. SaLckLockStatusT status;
  125. SaNameT resource_name;
  126. pthread_t dispatch_thread;
  127. fd_set read_fds;
  128. struct th_data th_data;
  129. signal (SIGINT, sigintr_handler);
  130. result = saLckInitialize (&handle, &callbacks, &version);
  131. if (result != SA_AIS_OK) {
  132. printf ("Could not initialize Lock Service API instance error %d\n", result);
  133. exit (1);
  134. }
  135. printf ("saLckInitialize result is %d (should be 1)\n", result);
  136. th_data.handle = handle;
  137. pthread_create (&dispatch_thread, NULL, th_dispatch, &th_data);
  138. setSaNameT (&resource_name_async, "test_resource_async");
  139. setSaNameT (&resource_name, "not_existingaabb");
  140. result = saLckResourceOpen (
  141. handle,
  142. &resource_name,
  143. // SA_LCK_RESOURCE_CREATE,
  144. 0,
  145. SA_TIME_ONE_SECOND,
  146. &resource_handle);
  147. printf ("saLckResourceOpen %d (should be 12)\n", result);
  148. result = saLckResourceClose (resource_handle);
  149. printf ("saLckResourceClose %d (should be 9)\n", result);
  150. setSaNameT (&resource_name, "test_resource");
  151. result = saLckResourceOpen (
  152. handle,
  153. &resource_name,
  154. SA_LCK_RESOURCE_CREATE,
  155. SA_TIME_ONE_SECOND,
  156. &resource_handle);
  157. printf ("saLckResourceOpen %d (should be 1)\n", result);
  158. result = saLckResourceOpenAsync (
  159. handle,
  160. (SaInvocationT)0x56,
  161. &resource_name_async,
  162. SA_LCK_RESOURCE_CREATE);
  163. printf ("saLckResourceOpenAsync %d (should be 1)\n", result);
  164. result = saLckResourceLock (
  165. resource_handle,
  166. &pr_lock_id,
  167. SA_LCK_PR_LOCK_MODE,
  168. SA_LCK_LOCK_ORPHAN,
  169. 55,
  170. SA_TIME_END,
  171. &status);
  172. printf ("saLckResourceLock PR %d (should be 1)\n", result);
  173. printf ("status %d\n", status);
  174. result = saLckResourceUnlock (
  175. pr_lock_id,
  176. SA_TIME_END);
  177. printf ("saLckResourceUnlock result %d (should be 1)\n", result);
  178. result = saLckResourceLock (
  179. resource_handle,
  180. &pr_lock_id,
  181. SA_LCK_PR_LOCK_MODE,
  182. SA_LCK_LOCK_ORPHAN,
  183. 55,
  184. SA_TIME_END,
  185. &status);
  186. printf ("saLckResourceLock PR %d (should be 1)\n", result);
  187. printf ("status %d\n", status);
  188. result = saLckResourceLock (
  189. resource_handle,
  190. &ex_lock_id,
  191. SA_LCK_EX_LOCK_MODE,
  192. 0,
  193. 55,
  194. SA_TIME_END,
  195. &status);
  196. printf ("saLckResourceLock EX %d (should be 1)\n", result);
  197. printf ("status %d\n", status);
  198. result = saLckResourceLockAsync (
  199. resource_handle_async,
  200. 0x56,
  201. &pr_lock_async_id,
  202. SA_LCK_PR_LOCK_MODE,
  203. 0,
  204. 55);
  205. printf ("saLckResourceLockAsync PR %d (should be 1)\n", result);
  206. printf ("status %d\n", status);
  207. printf ("press the enter key to exit\n");
  208. FD_ZERO (&read_fds);
  209. do {
  210. FD_SET (STDIN_FILENO, &read_fds);
  211. result = select (STDIN_FILENO + 1, &read_fds, 0, 0, 0);
  212. if (FD_ISSET (STDIN_FILENO, &read_fds)) {
  213. break;
  214. }
  215. } while (result);
  216. printf ("Starting saLckResourceUnlock\n");
  217. result = saLckResourceUnlock (
  218. ex_lock_id,
  219. SA_TIME_END);
  220. printf ("saLckResourceUnlock result %d (should be 1)\n",
  221. result);
  222. result = saLckResourceClose (resource_handle);
  223. printf ("saLckResourceClose result %d (should be 1)\n", result);
  224. result = saLckFinalize (handle);
  225. printf ("saLckFinalize %d (should be 1)\n", result);
  226. return (0);
  227. }