Patch to remove segfault on the exiting of a service.
[openais.git] / test / testamf6.c
blob46aedf54dd92057a8687c05e68df4f7a5b362079
1 /*
2 * Copyright (c) 2002-2003 MontaVista Software, Inc.
3 * Copyright (c) 2006 Sun Microsystems, Inc.
5 * All rights reserved.
7 * Author: Steven Dake (sdake@mvista.com)
9 * This software licensed under BSD license, the text of which follows:
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
14 * - Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * - Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * - Neither the name of the MontaVista Software, Inc. nor the names of its
20 * contributors may be used to endorse or promote products derived from this
21 * software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33 * THE POSSIBILITY OF SUCH DAMAGE.
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <errno.h>
39 #include <signal.h>
40 #include <unistd.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/un.h>
44 #include <sched.h>
46 #include "ais_types.h"
47 #include "saAmf.h"
49 void printSaNameT (SaNameT *name)
51 int i;
53 for (i = 0; i < name->length; i++) {
54 printf ("%c", name->value[i]);
58 void setSanameT (SaNameT *name, char *str) {
59 name->length = strlen (str);
60 memcpy (name->value, str, name->length);
63 static int health_flag = -1;
64 static unsigned int healthcheck_count = 0;
65 static unsigned int healthcheck_no = 0;
67 #ifdef COMPILE_OUT
68 void HealthcheckCallback (SaInvocationT invocation,
69 const SaNameT *compName,
70 SaAmfHealthcheckT checkType)
72 SaErrorT res;
74 healthcheck_no ++;
75 if (health_flag == -1 || healthcheck_no%healthcheck_count == 0) {
76 printf ("%u HealthcheckCallback have occured for component: ",healthcheck_no);
77 printSaNameT ((SaNameT *)compName);
78 printf ("\n");
80 res = saAmfResponse (invocation, SA_OK);
81 if (res != SA_OK) {
82 printf ("response res is %d\n", res);
86 void ReadinessStateSetCallback (SaInvocationT invocation,
87 const SaNameT *compName,
88 SaAmfReadinessStateT readinessState)
90 switch (readinessState) {
91 case SA_AMF_IN_SERVICE:
92 printf ("ReadinessStateSetCallback: '");
93 printSaNameT ((SaNameT *)compName);
94 printf ("' requested to enter operational state SA_AMF_IN_SERVICE.\n");
95 saAmfResponse (invocation, SA_OK);
96 break;
97 case SA_AMF_OUT_OF_SERVICE:
98 printf ("ReadinessStateSetCallback: '");
99 printSaNameT ((SaNameT *)compName);
100 printf ("' requested to enter operational state SA_AMF_OUT_OF_SERVICE.\n");
101 saAmfResponse (invocation, SA_OK);
102 break;
103 case SA_AMF_STOPPING:
104 printf ("ReadinessStateSetCallback: '");
105 printSaNameT ((SaNameT *)compName);
106 printf ("' requested to enter operational state SA_AMF_STOPPING.\n");
107 saAmfStoppingComplete (invocation, SA_OK);
108 break;
112 void ComponentTerminateCallback (
113 SaInvocationT invocation,
114 const SaNameT *compName)
116 printf ("ComponentTerminateCallback\n");
119 #endif
120 void CSISetCallback (
121 SaInvocationT invocation,
122 const SaNameT *compName,
123 SaAmfHAStateT haState,
124 SaAmfCSIDescriptorT *csiDescriptor)
126 switch (haState) {
127 case SA_AMF_HA_ACTIVE:
128 printf ("CSISetCallback: '");
129 printSaNameT ((SaNameT *)&csiDescriptor->csiName);
130 printf ("' for CSI '");
131 printSaNameT ((SaNameT *)compName);
132 printf ("'");
133 printf (" requested to enter hastate SA_AMF_ACTIVE.\n");
134 // saAmfResponse (invocation, SA_OK);
135 break;
137 case SA_AMF_HA_STANDBY:
138 printf ("CSISetCallback: '");
139 printSaNameT ((SaNameT *)compName);
140 printf ("' for CSI '");
141 printSaNameT ((SaNameT *)compName);
142 printf ("'");
143 printf (" requested to enter hastate SA_AMF_STANDBY.\n");
144 // saAmfResponse (invocation, SA_OK);
145 break;
149 #ifdef COMPILE_OUT
150 void CSIRemoveCallback (
151 SaInvocationT invocation,
152 const SaNameT *compName,
153 const SaNameT *csiName,
154 const SaAmfCSIFlagsT *csiFlags)
156 printf ("CSIRemoveCallback for component '");
157 printSaNameT ((SaNameT *)compName);
158 printf ("' in CSI '");
159 printSaNameT ((SaNameT *)csiName);
160 printf ("'\n");
161 saAmfResponse (invocation, SA_OK);
164 void ProtectionGroupTrackCallback (
165 const SaNameT *csiName,
166 SaAmfProtectionGroupNotificationT *notificationBuffer,
167 SaUint32T numberOfItems,
168 SaUint32T numberOfMembers,
169 SaErrorT error)
171 int i;
173 printf ("ProtectionGroupTrackCallback items %d members %d\n", (int)numberOfItems, (int)numberOfMembers);
174 printf ("buffer is %p\n", notificationBuffer);
175 for (i = 0; i < numberOfItems; i++) {
176 printf ("component name");
177 printSaNameT (&notificationBuffer[i].member.compName);
178 printf ("\n");
179 printf ("\treadiness state is %d\n", notificationBuffer[i].member.readinessState);
180 printf ("\thastate %d\n", notificationBuffer[i].member.haState);
181 printf ("\tchange is %d\n", notificationBuffer[i].change);
186 void ExternalComponentRestartCallback (
187 const SaInvocationT invocation,
188 const SaNameT *externalCompName)
190 printf ("ExternalComponentRestartCallback\n");
193 void ExternalComponentControlCallback (
194 const SaInvocationT invocation,
195 const SaNameT *externalCompName,
196 SaAmfExternalComponentActionT controlAction)
198 printf ("ExternalComponentControlCallback\n");
201 void PendingOperationConfirmCallback (
202 const SaInvocationT invocation,
203 const SaNameT *compName,
204 SaAmfPendingOperationFlagsT pendingOperationFlags)
206 printf ("PendingOperationConfirmCallback\n");
209 void PendingOperationExpiredCallback (
210 const SaNameT *compName,
211 SaAmfPendingOperationFlagsT pendingOperationFlags)
213 printf ("PendingOperationExpiredCallback\n");
215 #endif
217 SaAmfCallbacksT amfCallbacks = {
218 .saAmfCSISetCallback = CSISetCallback,
221 SaAmfCallbacksT amfCallbacks;
223 SaVersionT version = { 'B', 1, 1 };
225 static struct sched_param sched_param = {
226 sched_priority: 99
229 void sigintr_handler (int signum) {
230 exit (0);
234 int main (int argc, char **argv) {
235 SaAmfHandleT handle;
236 int result;
237 SaSelectionObjectT select_fd;
238 fd_set read_fds;
239 SaNameT compName;
240 extern char *optarg;
241 extern int optind;
242 int c;
244 memset (&compName, 0, sizeof (SaNameT));
245 signal (SIGINT, sigintr_handler);
246 result = sched_setscheduler (0, SCHED_RR, &sched_param);
247 if (result == -1) {
248 printf ("couldn't set sched priority\n");
251 for (;;){
252 c = getopt(argc,argv,"h:n:");
253 if (c==-1) {
254 break;
256 switch (c) {
257 case 0 :
258 break;
259 case 'h':
260 health_flag = 0;
261 sscanf (optarg,"%ud" ,&healthcheck_count);
262 break;
263 case 'n':
264 setSanameT (&compName, optarg);
265 break;
266 default :
267 break;
271 result = saAmfInitialize (&handle, &amfCallbacks, &version);
272 if (result != SA_OK) {
273 printf ("initialize result is %d\n", result);
274 exit (1);
277 FD_ZERO (&read_fds);
278 saAmfSelectionObjectGet (handle, &select_fd);
279 FD_SET (select_fd, &read_fds);
280 if (compName.length <= 0) {
281 setSanameT (&compName, "comp_b_in_su_3");
284 result = saAmfComponentRegister (handle, &compName, NULL);
285 printf ("register result is %d (should be 1)\n", result);
287 do {
288 select (select_fd + 1, &read_fds, 0, 0, 0);
289 saAmfDispatch (handle, SA_DISPATCH_ALL);
290 } while (result);
292 saAmfFinalize (handle);
294 exit (0);