~ubuntu-branches/ubuntu/precise/corosync/precise-proposed

« back to all changes in this revision

Viewing changes to tools/corosync-cfgtool.c

  • Committer: Bazaar Package Importer
  • Author(s): Ante Karamatic
  • Date: 2009-08-21 09:29:56 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090821092956-w9qxxxx3zeoh8dem
Tags: 1.0.0-4ubuntu2
* debian/control:
  - 'Ubuntu Developers' instead of 'Ubuntu Core Developers'
    as maintainer
  - Bump debhelper dependecy to 7

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (c) 2006-2007 Red Hat, Inc.
 
2
 * Copyright (c) 2006-2009 Red Hat, Inc.
3
3
 *
4
4
 * All rights reserved.
5
5
 *
32
32
 * THE POSSIBILITY OF SUCH DAMAGE.
33
33
 */
34
34
 
 
35
#include <config.h>
 
36
 
35
37
#include <stdio.h>
36
38
#include <stdlib.h>
37
39
#include <errno.h>
38
 
#include <signal.h>
39
40
#include <unistd.h>
40
41
#include <string.h>
 
42
#include <pthread.h>
41
43
#include <sys/types.h>
42
44
#include <sys/socket.h>
43
45
#include <sys/select.h>
45
47
#include <netinet/in.h>
46
48
#include <arpa/inet.h>
47
49
 
48
 
#include <corosync/saAis.h>
 
50
#include <corosync/corotypes.h>
 
51
#include <corosync/totem/totem.h>
49
52
#include <corosync/cfg.h>
50
53
 
51
54
static void ringstatusget_do (void)
52
55
{
53
 
        SaAisErrorT result;
 
56
        cs_error_t result;
54
57
        corosync_cfg_handle_t handle;
55
58
        unsigned int interface_count;
56
59
        char **interface_names;
57
60
        char **interface_status;
58
61
        unsigned int i;
 
62
        unsigned int nodeid;
59
63
 
60
64
        printf ("Printing ring status.\n");
61
65
        result = corosync_cfg_initialize (&handle, NULL);
62
 
        if (result != SA_AIS_OK) {
 
66
        if (result != CS_OK) {
63
67
                printf ("Could not initialize corosync configuration API error %d\n", result);
64
68
                exit (1);
65
69
        }
66
70
 
67
 
        corosync_cfg_ring_status_get (handle,
68
 
                &interface_names,
69
 
                &interface_status,
70
 
                &interface_count);
71
 
 
72
 
        for (i = 0; i < interface_count; i++) {
73
 
                printf ("RING ID %d\n", i);
74
 
                printf ("\tid\t= %s\n", interface_names[i]);
75
 
                printf ("\tstatus\t= %s\n", interface_status[i]);
76
 
        }
77
 
 
78
 
        corosync_cfg_finalize (handle);
 
71
        result = corosync_cfg_local_get(handle, &nodeid);
 
72
        if (result != CS_OK) {
 
73
                printf ("Could not get the local node id, the error is: %d\n", result);
 
74
        }
 
75
        else {
 
76
                printf ("Local node ID %d\n", nodeid);
 
77
        }
 
78
 
 
79
        result = corosync_cfg_ring_status_get (handle,
 
80
                                &interface_names,
 
81
                                &interface_status,
 
82
                                &interface_count);
 
83
        if (result != CS_OK) {
 
84
                printf ("Could not get the ring status, the error is: %d\n", result);
 
85
        } else {
 
86
                for (i = 0; i < interface_count; i++) {
 
87
                        printf ("RING ID %d\n", i);
 
88
                        printf ("\tid\t= %s\n", interface_names[i]);
 
89
                        printf ("\tstatus\t= %s\n", interface_status[i]);
 
90
                }
 
91
        }
 
92
        (void)corosync_cfg_finalize (handle);
79
93
}
80
94
 
81
95
static void ringreenable_do (void)
82
96
{
83
 
        SaAisErrorT result;
 
97
        cs_error_t result;
84
98
        corosync_cfg_handle_t handle;
85
99
 
86
100
        printf ("Re-enabling all failed rings.\n");
87
101
        result = corosync_cfg_initialize (&handle, NULL);
88
 
        if (result != SA_AIS_OK) {
 
102
        if (result != CS_OK) {
89
103
                printf ("Could not initialize corosync configuration API error %d\n", result);
90
104
                exit (1);
91
105
        }
92
106
 
93
107
        result = corosync_cfg_ring_reenable (handle);
94
 
        if (result != SA_AIS_OK) {
 
108
        if (result != CS_OK) {
95
109
                printf ("Could not reenable ring error %d\n", result);
96
110
        }
97
111
 
98
 
        corosync_cfg_finalize (handle);
 
112
        (void)corosync_cfg_finalize (handle);
99
113
}
100
114
 
101
 
void service_load_do (char *service, unsigned int version)
 
115
static void service_load_do (const char *service, unsigned int version)
102
116
{
103
 
        SaAisErrorT result;
 
117
        cs_error_t result;
104
118
        corosync_cfg_handle_t handle;
105
119
 
106
120
        printf ("Loading service '%s' version '%d'\n", service, version);
107
121
        result = corosync_cfg_initialize (&handle, NULL);
108
 
        if (result != SA_AIS_OK) {
 
122
        if (result != CS_OK) {
109
123
                printf ("Could not initialize corosync configuration API error %d\n", result);
110
124
                exit (1);
111
125
        }
112
126
        result = corosync_cfg_service_load (handle, service, version);
113
 
        if (result != SA_AIS_OK) {
 
127
        if (result != CS_OK) {
114
128
                printf ("Could not load service (error = %d)\n", result);
115
129
        }
116
 
        corosync_cfg_finalize (handle);
 
130
        (void)corosync_cfg_finalize (handle);
117
131
}
118
132
 
119
 
void service_unload_do (char *service, unsigned int version)
 
133
static void service_unload_do (const char *service, unsigned int version)
120
134
{
121
 
        SaAisErrorT result;
 
135
        cs_error_t result;
122
136
        corosync_cfg_handle_t handle;
123
137
 
124
138
        printf ("Unloading service '%s' version '%d'\n", service, version);
125
139
        result = corosync_cfg_initialize (&handle, NULL);
126
 
        if (result != SA_AIS_OK) {
 
140
        if (result != CS_OK) {
127
141
                printf ("Could not initialize corosync configuration API error %d\n", result);
128
142
                exit (1);
129
143
        }
130
144
        result = corosync_cfg_service_unload (handle, service, version);
131
 
        if (result != SA_AIS_OK) {
 
145
        if (result != CS_OK) {
132
146
                printf ("Could not unload service (error = %d)\n", result);
133
147
        }
134
 
        corosync_cfg_finalize (handle);
135
 
}
136
 
 
137
 
void usage_do (void)
138
 
{
139
 
        printf ("corosync-cfgtool [-s] [-r] [-l] [-u] [service_name] [-v] [version]\n\n");
 
148
        (void)corosync_cfg_finalize (handle);
 
149
}
 
150
 
 
151
static void shutdown_do(void)
 
152
{
 
153
        cs_error_t result;
 
154
        corosync_cfg_handle_t handle;
 
155
        corosync_cfg_callbacks_t callbacks;
 
156
 
 
157
        callbacks.corosync_cfg_shutdown_callback = NULL;
 
158
 
 
159
        result = corosync_cfg_initialize (&handle, &callbacks);
 
160
        if (result != CS_OK) {
 
161
                printf ("Could not initialize corosync configuration API error %d\n", result);
 
162
                exit (1);
 
163
        }
 
164
 
 
165
        printf ("Shutting down corosync\n");
 
166
        result = corosync_cfg_try_shutdown (handle, COROSYNC_CFG_SHUTDOWN_FLAG_REQUEST);
 
167
        if (result != CS_OK) {
 
168
                printf ("Could not shutdown (error = %d)\n", result);
 
169
        }
 
170
 
 
171
        (void)corosync_cfg_finalize (handle);
 
172
}
 
173
 
 
174
static void showaddrs_do(int nodeid)
 
175
{
 
176
        cs_error_t result;
 
177
        corosync_cfg_handle_t handle;
 
178
        corosync_cfg_callbacks_t callbacks;
 
179
        int numaddrs;
 
180
        int i;
 
181
        corosync_cfg_node_address_t addrs[INTERFACE_MAX];
 
182
 
 
183
 
 
184
        result = corosync_cfg_initialize (&handle, &callbacks);
 
185
        if (result != CS_OK) {
 
186
                printf ("Could not initialize corosync configuration API error %d\n", result);
 
187
                exit (1);
 
188
        }
 
189
 
 
190
        if (corosync_cfg_get_node_addrs(handle, nodeid, INTERFACE_MAX, &numaddrs, addrs) == CS_OK) {
 
191
                for (i=0; i<numaddrs; i++) {
 
192
                        char buf[INET6_ADDRSTRLEN];
 
193
                        struct sockaddr_storage *ss = (struct sockaddr_storage *)addrs[i].address;
 
194
                        struct sockaddr_in *sin = (struct sockaddr_in *)addrs[i].address;
 
195
                        struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addrs[i].address;
 
196
                        void *saddr;
 
197
 
 
198
                        if (ss->ss_family == AF_INET6)
 
199
                                saddr = &sin6->sin6_addr;
 
200
                        else
 
201
                                saddr = &sin->sin_addr;
 
202
 
 
203
                        inet_ntop(ss->ss_family, saddr, buf, sizeof(buf));
 
204
                        printf("%s", buf);
 
205
                }
 
206
                printf("\n");
 
207
        }
 
208
 
 
209
 
 
210
        (void)corosync_cfg_finalize (handle);
 
211
}
 
212
 
 
213
 
 
214
static void crypto_do(unsigned int type)
 
215
{
 
216
        cs_error_t result;
 
217
        corosync_cfg_handle_t handle;
 
218
 
 
219
        printf ("Setting crypto to mode %d\n", type);
 
220
        result = corosync_cfg_initialize (&handle, NULL);
 
221
        if (result != CS_OK) {
 
222
                printf ("Could not initialize corosync configuration API error %d\n", result);
 
223
                exit (1);
 
224
        }
 
225
        result = corosync_cfg_crypto_set (handle, type);
 
226
        if (result != CS_OK) {
 
227
                printf ("Could not set crypto mode (error = %d)\n", result);
 
228
        }
 
229
        (void)corosync_cfg_finalize (handle);
 
230
 
 
231
}
 
232
 
 
233
static void killnode_do(unsigned int nodeid)
 
234
{
 
235
        cs_error_t result;
 
236
        corosync_cfg_handle_t handle;
 
237
 
 
238
        printf ("Killing node %d\n", nodeid);
 
239
        result = corosync_cfg_initialize (&handle, NULL);
 
240
        if (result != CS_OK) {
 
241
                printf ("Could not initialize corosync configuration API error %d\n", result);
 
242
                exit (1);
 
243
        }
 
244
        result = corosync_cfg_kill_node (handle, nodeid, "Killed by corosync-cfgtool");
 
245
        if (result != CS_OK) {
 
246
                printf ("Could not kill node (error = %d)\n", result);
 
247
        }
 
248
        (void)corosync_cfg_finalize (handle);
 
249
}
 
250
 
 
251
 
 
252
static void usage_do (void)
 
253
{
 
254
        printf ("corosync-cfgtool [-s] [-r] [-l] [-u] [-H] [service_name] [-v] [version] [-k] [nodeid] [-a] [nodeid]\n\n");
140
255
        printf ("A tool for displaying and configuring active parameters within corosync.\n");
141
256
        printf ("options:\n");
142
257
        printf ("\t-s\tDisplays the status of the current rings on this node.\n");
144
259
        printf ("\t\tre-enable redundant ring operation.\n");
145
260
        printf ("\t-l\tLoad a service identified by name.\n");
146
261
        printf ("\t-u\tUnload a service identified by name.\n");
 
262
        printf ("\t-a\tDisplay the IP address(es) of a node\n");
 
263
        printf ("\t-c\tSet the cryptography mode of cluster communications\n");
 
264
        printf ("\t-k\tKill a node identified by node id.\n");
 
265
        printf ("\t-H\tShutdown corosync cleanly on this node.\n");
 
266
}
 
267
 
 
268
static char *
 
269
xstrdup (char const *s)
 
270
{
 
271
        char *p = strdup (s);
 
272
        if (p)
 
273
                return (char *) p;
 
274
 
 
275
        printf ("exhausted virtual memory\n");
 
276
        exit (1);
147
277
}
148
278
 
149
279
int main (int argc, char *argv[]) {
150
 
        const char *options = "srl:u:v:";
 
280
        const char *options = "srl:u:v:k:a:c:hH";
151
281
        int opt;
152
282
        int service_load = 0;
 
283
        unsigned int nodeid;
153
284
        int service_unload = 0;
154
 
        char *service;
155
 
        unsigned int version;
 
285
        char *service = NULL;
 
286
        unsigned int version = 0;
156
287
 
157
288
        if (argc == 1) {
158
289
                usage_do ();
167
298
                        break;
168
299
                case 'l':
169
300
                        service_load = 1;
170
 
                        service = strdup (optarg);
 
301
                        service = xstrdup (optarg);
171
302
                        break;
172
303
                case 'u':
173
304
                        service_unload = 1;
174
 
                        service = strdup (optarg);
 
305
                        service = xstrdup (optarg);
 
306
                        break;
 
307
                case 'k':
 
308
                        nodeid = atoi (optarg);
 
309
                        killnode_do(nodeid);
 
310
                        break;
 
311
                case 'H':
 
312
                        shutdown_do();
 
313
                        break;
 
314
                case 'a':
 
315
                        showaddrs_do( atoi(optarg) );
 
316
                        break;
 
317
                case 'c':
 
318
                        crypto_do( atoi(optarg) );
175
319
                        break;
176
320
                case 'v':
177
321
                        version = atoi (optarg);
 
322
                        break;
 
323
                case 'h':
 
324
                        usage_do();
 
325
                        break;
178
326
                }
179
327
        }
180
328
 
181
329
        if (service_load) {
182
330
                service_load_do (service, version);
183
 
        } else 
 
331
        } else
184
332
        if (service_unload) {
185
333
                service_unload_do (service, version);
186
334
        }
187
 
                
 
335
 
188
336
        return (0);
189
337
}