testlck.c 6.5 KB

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