Patch to remove segfault on the exiting of a service.
[openais.git] / test / testamf5.c
blob5e61950052d114cbc5448dca8c0e2afd57c13580
1 /*
2 * Copyright (c) 2002-2003 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 <errno.h>
38 #include <signal.h>
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <sys/un.h>
43 #include <sched.h>
45 #include "ais_types.h"
46 #include "saAmf.h"
48 void printSaNameT (SaNameT *name)
50 int i;
52 for (i = 0; i < name->length; i++) {
53 printf ("%c", name->value[i]);
57 void setSanameT (SaNameT *name, char *str) {
58 name->length = strlen (str);
59 memcpy (name->value, str, name->length);
62 static int health_flag = -1;
63 static unsigned int healthcheck_count = 0;
64 static unsigned int healthcheck_no = 0;
66 #ifdef COMPILE_OUT
67 void HealthcheckCallback (SaInvocationT invocation,
68 const SaNameT *compName,
69 SaAmfHealthcheckT checkType)
71 SaErrorT res;
73 healthcheck_no ++;
74 if (health_flag == -1 || healthcheck_no%healthcheck_count == 0) {
75 printf ("%u HealthcheckCallback have occured for component: ",healthcheck_no);
76 printSaNameT ((SaNameT *)compName);
77 printf ("\n");
79 res = saAmfResponse (invocation, SA_OK);
80 if (res != SA_OK) {
81 printf ("response res is %d\n", res);
85 void ReadinessStateSetCallback (SaInvocationT invocation,
86 const SaNameT *compName,
87 SaAmfReadinessStateT readinessState)
89 switch (readinessState) {
90 case SA_AMF_IN_SERVICE:
91 printf ("ReadinessStateSetCallback: '");
92 printSaNameT ((SaNameT *)compName);
93 printf ("' requested to enter operational state SA_AMF_IN_SERVICE.\n");
94 saAmfResponse (invocation, SA_OK);
95 break;
96 case SA_AMF_OUT_OF_SERVICE:
97 printf ("ReadinessStateSetCallback: '");
98 printSaNameT ((SaNameT *)compName);
99 printf ("' requested to enter operational state SA_AMF_OUT_OF_SERVICE.\n");
100 saAmfResponse (invocation, SA_OK);
101 break;
102 case SA_AMF_STOPPING:
103 printf ("ReadinessStateSetCallback: '");
104 printSaNameT ((SaNameT *)compName);
105 printf ("' requested to enter operational state SA_AMF_STOPPING.\n");
106 saAmfStoppingComplete (invocation, SA_OK);
107 break;
111 void ComponentTerminateCallback (
112 SaInvocationT invocation,
113 const SaNameT *compName)
115 printf ("ComponentTerminateCallback\n");
118 #endif
119 void CSISetCallback (
120 SaInvocationT invocation,
121 const SaNameT *compName,
122 SaAmfHAStateT haState,
123 SaAmfCSIDescriptorT *csiDescriptor)
125 switch (haState) {
126 case SA_AMF_HA_ACTIVE:
127 printf ("CSISetCallback: '");
128 printSaNameT ((SaNameT *)&csiDescriptor->csiName);
129 printf ("' for CSI '");
130 printSaNameT ((SaNameT *)compName);
131 printf ("'");
132 printf (" requested to enter hastate SA_AMF_ACTIVE.\n");
133 // saAmfResponse (invocation, SA_OK);
134 break;
136 case SA_AMF_HA_STANDBY:
137 printf ("CSISetCallback: '");
138 printSaNameT ((SaNameT *)compName);
139 printf ("' for CSI '");
140 printSaNameT ((SaNameT *)compName);
141 printf ("'");
142 printf (" requested to enter hastate SA_AMF_STANDBY.\n");
143 // saAmfResponse (invocation, SA_OK);
144 break;
148 #ifdef COMPILE_OUT
149 void CSIRemoveCallback (
150 SaInvocationT invocation,
151 const SaNameT *compName,
152 const SaNameT *csiName,
153 const SaAmfCSIFlagsT *csiFlags)
155 printf ("CSIRemoveCallback for component '");
156 printSaNameT ((SaNameT *)compName);
157 printf ("' in CSI '");
158 printSaNameT ((SaNameT *)csiName);
159 printf ("'\n");
160 saAmfResponse (invocation, SA_OK);
163 void ProtectionGroupTrackCallback (
164 const SaNameT *csiName,
165 SaAmfProtectionGroupNotificationT *notificationBuffer,
166 SaUint32T numberOfItems,
167 SaUint32T numberOfMembers,
168 SaErrorT error)
170 int i;
172 printf ("ProtectionGroupTrackCallback items %d members %d\n", (int)numberOfItems, (int)numberOfMembers);
173 printf ("buffer is %p\n", notificationBuffer);
174 for (i = 0; i < numberOfItems; i++) {
175 printf ("component name");
176 printSaNameT (&notificationBuffer[i].member.compName);
177 printf ("\n");
178 printf ("\treadiness state is %d\n", notificationBuffer[i].member.readinessState);
179 printf ("\thastate %d\n", notificationBuffer[i].member.haState);
180 printf ("\tchange is %d\n", notificationBuffer[i].change);
185 void ExternalComponentRestartCallback (
186 const SaInvocationT invocation,
187 const SaNameT *externalCompName)
189 printf ("ExternalComponentRestartCallback\n");
192 void ExternalComponentControlCallback (
193 const SaInvocationT invocation,
194 const SaNameT *externalCompName,
195 SaAmfExternalComponentActionT controlAction)
197 printf ("ExternalComponentControlCallback\n");
200 void PendingOperationConfirmCallback (
201 const SaInvocationT invocation,
202 const SaNameT *compName,
203 SaAmfPendingOperationFlagsT pendingOperationFlags)
205 printf ("PendingOperationConfirmCallback\n");
208 void PendingOperationExpiredCallback (
209 const SaNameT *compName,
210 SaAmfPendingOperationFlagsT pendingOperationFlags)
212 printf ("PendingOperationExpiredCallback\n");
214 #endif
216 SaAmfCallbacksT amfCallbacks = {
217 .saAmfCSISetCallback = CSISetCallback,
220 SaAmfCallbacksT amfCallbacks;
222 SaVersionT version = { 'B', 1, 1 };
224 static struct sched_param sched_param = {
225 sched_priority: 99
228 void sigintr_handler (int signum) {
229 exit (0);
233 int main (int argc, char **argv) {
234 SaAmfHandleT handle;
235 int result;
236 SaSelectionObjectT select_fd;
237 fd_set read_fds;
238 SaNameT compName;
239 extern char *optarg;
240 extern int optind;
241 int c;
243 memset (&compName, 0, sizeof (SaNameT));
244 signal (SIGINT, sigintr_handler);
245 result = sched_setscheduler (0, SCHED_RR, &sched_param);
246 if (result == -1) {
247 printf ("couldn't set sched priority\n");
250 for (;;){
251 c = getopt(argc,argv,"h:n:");
252 if (c==-1) {
253 break;
255 switch (c) {
256 case 0 :
257 break;
258 case 'h':
259 health_flag = 0;
260 sscanf (optarg,"%ud" ,&healthcheck_count);
261 break;
262 case 'n':
263 setSanameT (&compName, optarg);
264 break;
265 default :
266 break;
270 result = saAmfInitialize (&handle, &amfCallbacks, &version);
271 if (result != SA_OK) {
272 printf ("initialize result is %d\n", result);
273 exit (1);
276 FD_ZERO (&read_fds);
277 saAmfSelectionObjectGet (handle, &select_fd);
278 FD_SET (select_fd, &read_fds);
279 if (compName.length <= 0) {
280 setSanameT (&compName, "comp_a_in_su_3");
283 result = saAmfComponentRegister (handle, &compName, NULL);
284 printf ("register result is %d (should be 1)\n", result);
286 do {
287 select (select_fd + 1, &read_fds, 0, 0, 0);
288 saAmfDispatch (handle, SA_DISPATCH_ALL);
289 } while (result);
291 saAmfFinalize (handle);
293 exit (0);