testlck.c 6.5 KB

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