1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*-
*
* Copyright (c) Ryan O'Hara (rohara@redhat.com)
* Copyright (c) Red Hat, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <getopt.h>
#include <unistd.h>
#include <syslog.h>
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "options.h"
#include "message.h"
#include "version.h"
static int verbose = 0;
#define log_debug(lvl, fmt, args...) \
do { \
if (lvl <= verbose) \
fprintf (stdout, "[debug]: " fmt, ##args); \
} while (0);
#define log_error(lvl, fmt, args...) \
do { \
if (lvl <= verbose) \
fprintf (stderr, "[error]: " fmt, ##args); \
} while (0);
static int
send_message (const fence_kdump_node_t *node, void *msg, int len)
{
int error;
error = sendto (node->socket, msg, len, 0, node->info->ai_addr, node->info->ai_addrlen);
if (error < 0) {
log_error (2, "sendto (%s)\n", strerror (errno));
goto out;
}
log_debug (1, "message sent to node '%s'\n", node->addr);
out:
return (error);
}
static void
print_usage (const char *self)
{
fprintf (stdout, "Usage: %s [options] [nodes]\n", basename (self));
fprintf (stdout, "\n");
fprintf (stdout, "Options:\n");
fprintf (stdout, "\n");
fprintf (stdout, "%s\n",
" -p, --ipport=PORT Port number (default: 7410)");
fprintf (stdout, "%s\n",
" -f, --family=FAMILY Network family ([auto], ipv4, ipv6)");
fprintf (stdout, "%s\n",
" -c, --count=COUNT Number of messages to send (default: 0)");
fprintf (stdout, "%s\n",
" -i, --interval=INTERVAL Interval in seconds (default: 10)");
fprintf (stdout, "%s\n",
" -v, --verbose Print verbose output");
fprintf (stdout, "%s\n",
" -V, --version Print version");
fprintf (stdout, "%s\n",
" -h, --help Print usage");
fprintf (stdout, "\n");
return;
}
static int
get_options_node (fence_kdump_opts_t *opts)
{
int error;
struct addrinfo hints;
fence_kdump_node_t *node;
node = malloc (sizeof (fence_kdump_node_t));
if (!node) {
log_error (2, "malloc (%s)\n", strerror (errno));
return (1);
}
memset (node, 0, sizeof (fence_kdump_node_t));
memset (&hints, 0, sizeof (hints));
hints.ai_family = opts->family;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
hints.ai_flags = AI_NUMERICSERV;
strncpy (node->name, opts->nodename, sizeof (node->name) - 1);
snprintf (node->port, sizeof (node->port), "%d", opts->ipport);
node->info = NULL;
error = getaddrinfo (node->name, node->port, &hints, &node->info);
if (error != 0) {
log_error (2, "getaddrinfo (%s)\n", gai_strerror (error));
free_node (node);
return (1);
}
error = getnameinfo (node->info->ai_addr, node->info->ai_addrlen,
node->addr, sizeof (node->addr),
node->port, sizeof (node->port),
NI_NUMERICHOST | NI_NUMERICSERV);
if (error != 0) {
log_error (2, "getnameinfo (%s)\n", gai_strerror (error));
free_node (node);
return (1);
}
node->socket = socket (node->info->ai_family,
node->info->ai_socktype,
node->info->ai_protocol);
if (node->socket < 0) {
log_error (2, "socket (%s)\n", strerror (errno));
free_node (node);
return (1);
}
list_add_tail (&node->list, &opts->nodes);
return (0);
}
static void
get_options (int argc, char **argv, fence_kdump_opts_t *opts)
{
int opt;
struct option options[] = {
{ "ipport", required_argument, NULL, 'p' },
{ "family", required_argument, NULL, 'f' },
{ "count", required_argument, NULL, 'c' },
{ "interval", required_argument, NULL, 'i' },
{ "verbose", optional_argument, NULL, 'v' },
{ "version", no_argument, NULL, 'V' },
{ "help", no_argument, NULL, 'h' },
{ 0, 0, 0, 0 }
};
while ((opt = getopt_long (argc, argv, "p:f:c:i:v::Vh", options, NULL)) != EOF) {
switch (opt) {
case 'p':
set_option_ipport (opts, optarg);
break;
case 'f':
set_option_family (opts, optarg);
break;
case 'c':
set_option_count (opts, optarg);
break;
case 'i':
set_option_interval (opts, optarg);
break;
case 'v':
set_option_verbose (opts, optarg);
break;
case 'V':
print_version (argv[0]);
exit (0);
case 'h':
print_usage (argv[0]);
exit (0);
default:
print_usage (argv[0]);
exit (1);
}
}
verbose = opts->verbose;
return;
}
int
main (int argc, char **argv)
{
int count = 1;
fence_kdump_msg_t msg;
fence_kdump_opts_t opts;
fence_kdump_node_t *node;
init_options (&opts);
if (argc > 1) {
get_options (argc, argv, &opts);
} else {
print_usage (argv[0]);
exit (1);
}
for (; optind < argc; optind++) {
opts.nodename = argv[optind];
if (get_options_node (&opts) != 0) {
log_error (1, "failed to get node '%s'\n", opts.nodename);
}
opts.nodename = NULL;
}
if (list_empty (&opts.nodes)) {
print_usage (argv[0]);
exit (1);
}
if (verbose != 0) {
print_options (&opts);
}
init_message (&msg);
for (;;) {
list_for_each_entry (node, &opts.nodes, list) {
send_message (node, &msg, sizeof (msg));
}
if ((opts.count != 0) && (++count > opts.count)) {
break;
}
sleep (opts.interval);
}
free_options (&opts);
return (0);
}
|