Patch to remove segfault on the exiting of a service.
[openais.git] / test / testlck.c
blob99bc25ed095497d7bb3bff42cde4e2783dedab93
1 /*
2 * Copyright (c) 2005 MontaVista Software, Inc.
4 * All rights reserved.
6 * Author: Steven Dake (sdake@mvista.com)
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:
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.
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.
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <signal.h>
40 #include <unistd.h>
41 #include <pthread.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <sys/select.h>
45 #include <sys/un.h>
47 #include "saAis.h"
48 #include "saLck.h"
50 SaNameT resource_name_async;
51 SaLckResourceHandleT resource_handle_async;
53 void testLckResourceOpenCallback (
54 SaInvocationT invocation,
55 SaLckResourceHandleT lockResourceHandle,
56 SaAisErrorT error)
58 printf ("testLckResourceOpenCallback invocation %llu error %d\n",
59 (unsigned long long)invocation, error);
60 resource_handle_async = lockResourceHandle;
63 void testLckLockGrantCallback (
64 SaInvocationT invocation,
65 SaLckLockStatusT lockStatus,
66 SaAisErrorT error)
68 printf ("testLckLockGrantCallback invocation %llu status %d error %d\n",
69 (unsigned long long)invocation, lockStatus, error);
72 SaLckLockIdT pr_lock_id;
74 void testLckLockWaiterCallback (
75 SaLckWaiterSignalT waiterSignal,
76 SaLckLockIdT lockId,
77 SaLckLockModeT modeHeld,
78 SaLckLockModeT modeRequested)
80 int result;
81 printf ("waiter callback mode held %d mode requested %d lock id %llu\n",
82 modeHeld,
83 modeRequested,
84 (unsigned long long)lockId);
85 printf ("pr lock id %llu\n", (unsigned long long)pr_lock_id);
86 result = saLckResourceUnlockAsync (
87 25,
88 lockId);
89 printf ("saLckResourceUnlockAsync result %d (should be 1)\n", result);
92 void testLckResourceUnlockCallback (
93 SaInvocationT invocation,
94 SaAisErrorT error)
96 printf ("testLckResourceUnlockCallback async invocation %llu error %d\n",
97 (unsigned long long)invocation, error);
100 SaLckCallbacksT callbacks = {
101 .saLckResourceOpenCallback = testLckResourceOpenCallback,
102 .saLckLockGrantCallback = testLckLockGrantCallback,
103 .saLckLockWaiterCallback = testLckLockWaiterCallback,
104 .saLckResourceUnlockCallback = testLckResourceUnlockCallback
107 SaVersionT version = { 'B', 1, 1 };
109 void setSaNameT (SaNameT *name, char *str) {
110 name->length = strlen (str);
111 memcpy (name->value, str, name->length);
114 void sigintr_handler (int signum) {
115 exit (0);
118 struct th_data {
119 SaLckHandleT handle;
122 void *th_dispatch (void *arg)
124 struct th_data *th_data = (struct th_data *)arg;
126 saLckDispatch (th_data->handle, SA_DISPATCH_BLOCKING);
127 return (0);
130 int main (void) {
131 SaLckHandleT handle;
132 SaLckResourceHandleT resource_handle;
133 int result;
134 SaLckLockIdT ex_lock_id;
135 SaLckLockStatusT status;
136 SaNameT resource_name;
137 pthread_t dispatch_thread;
138 fd_set read_fds;
139 struct th_data th_data;
141 signal (SIGINT, sigintr_handler);
143 result = saLckInitialize (&handle, &callbacks, &version);
144 if (result != SA_AIS_OK) {
145 printf ("Could not initialize Lock Service API instance error %d\n", result);
146 exit (1);
148 printf ("saLckInitialize result is %d (should be 1)\n", result);
150 th_data.handle = handle;
151 pthread_create (&dispatch_thread, NULL, th_dispatch, &th_data);
153 setSaNameT (&resource_name, "test_resource");
154 setSaNameT (&resource_name_async, "test_resource_async");
156 result = saLckResourceOpen (
157 handle,
158 &resource_name,
159 SA_LCK_RESOURCE_CREATE,
160 // 0,
161 SA_TIME_ONE_SECOND,
162 &resource_handle);
163 printf ("saLckResourceOpen %d (should be 12)\n", result);
165 result = saLckResourceClose (resource_handle);
166 printf ("saLckResourceClose %d (should be 9)\n", result);
168 result = saLckResourceOpen (
169 handle,
170 &resource_name,
171 SA_LCK_RESOURCE_CREATE,
172 SA_TIME_ONE_SECOND,
173 &resource_handle);
174 printf ("saLckResourceOpen %d (should be 1)\n", result);
176 result = saLckResourceOpenAsync (
177 handle,
178 0x56,
179 &resource_name_async,
180 SA_LCK_RESOURCE_CREATE);
181 printf ("saLckResourceOpenAsync %d (should be 1)\n", result);
183 result = saLckResourceLock (
184 resource_handle,
185 &pr_lock_id,
186 SA_LCK_PR_LOCK_MODE,
187 SA_LCK_LOCK_ORPHAN,
189 SA_TIME_END,
190 &status);
191 printf ("saLckResourceLock PR %d (should be 1)\n", result);
192 printf ("status %d\n", status);
194 result = saLckResourceLock (
195 resource_handle,
196 &ex_lock_id,
197 SA_LCK_EX_LOCK_MODE,
200 SA_TIME_END,
201 &status);
202 printf ("saLckResourceLock EX %d (should be 1)\n", result);
203 printf ("status %d\n", status);
205 result = saLckResourceLockAsync (
206 resource_handle,
207 0x56,
208 &pr_lock_id,
209 SA_LCK_PR_LOCK_MODE,
211 55);
212 printf ("saLckResourceLockAsync PR %d (should be 1)\n", result);
213 printf ("status %d\n", status);
214 printf ("press the enter key to exit\n");
215 FD_ZERO (&read_fds);
216 do {
217 FD_SET (STDIN_FILENO, &read_fds);
218 result = select (STDIN_FILENO + 1, &read_fds, 0, 0, 0);
219 if (FD_ISSET (STDIN_FILENO, &read_fds)) {
220 break;
222 } while (result);
224 result = saLckResourceUnlock (
225 ex_lock_id,
226 SA_TIME_END);
227 printf ("saLckResourceUnlock result %d (should be 1)\n",
228 result);
230 result = saLckResourceClose (resource_handle);
231 printf ("saLckResourceClose result %d (should be 1)\n", result);
233 result = saLckFinalize (handle);
234 printf ("saLckFinalize %d (should be 1)\n", result);
235 return (0);