summaryrefslogtreecommitdiffstats
path: root/agents/kdump/fence_kdump.c
blob: eda1559f2d86376f9dfef526b2b66d9682d7cd09 (plain)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/* -*- 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); \
        syslog (LOG_INFO, fmt, ##args);            \
    }                                              \
} while (0);

#define log_error(lvl, fmt, args...)               \
do {                                               \
    if (lvl <= verbose) {                          \
        fprintf (stderr, "[error]: " fmt, ##args); \
        syslog (LOG_ERR, fmt, ##args);             \
    }                                              \
} while (0);

static int
trim (char *str)
{
    char *p;
    int len;

    if (!str) return (0);

    len = strlen (str);

    while (len--) {
        if (isspace (str[len])) {
            str[len] = 0;
        } else {
            break;
        }
    }

    for (p = str; *p && isspace (*p); p++);

    memmove (str, p, strlen (p) + 1);

    return (strlen (str));
}

static int
do_action_monitor (void)
{
    const char cmdline_path[] = "/proc/cmdline";
    FILE *procFile;
    size_t sz = 0;
    char *lines = NULL;
    int result = 1;

    procFile = fopen(cmdline_path, "r");

    if (procFile == NULL) {
        log_error (0, "Unable to open file %s (%s)\n", cmdline_path, strerror (errno));
        return 1;
    }

    while (!feof (procFile)) {
        ssize_t rv = getline (&lines, &sz, procFile);
        if ((rv != -1) && (strstr(lines, "crashkernel=") != NULL)) {
            result = 0;
        }
    }

    free (lines);
    fclose (procFile);

    return result;
}

static int
do_action_off (const fence_kdump_opts_t *opts)
{
    int error;
    fd_set rfds;
    fence_kdump_msg_t msg;
    fence_kdump_node_t *node;
    struct timeval timeout;
    struct addrinfo hints;
    fence_kdump_node_t *check_node;
    char addr[NI_MAXHOST];
    char port[NI_MAXSERV];
    struct sockaddr_storage ss;
    socklen_t size = sizeof (ss);

    if (list_empty (&opts->nodes)) {
        return (1);
    } else {
        node = list_first_entry (&opts->nodes, fence_kdump_node_t, list);
    }

    timeout.tv_sec = opts->timeout;
    timeout.tv_usec = 0;

    FD_ZERO (&rfds);
    FD_SET (node->socket, &rfds);

    // create listening socket
    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;

    hints.ai_family = node->info->ai_family;
    hints.ai_flags |= AI_PASSIVE;

    freeaddrinfo (node->info);

    node->info = NULL;
    error = getaddrinfo (NULL, node->port, &hints, &node->info);
    if (error != 0) {
        log_error (2, "getaddrinfo (%s)\n", gai_strerror (error));
        free_node (node);
        return (1);
    }

    error = bind (node->socket, node->info->ai_addr, node->info->ai_addrlen);
    if (error != 0) {
        log_error (2, "bind (%s)\n", strerror (errno));
        free_node (node);
        return (1);
    }

    list_for_each_entry (check_node, &opts->nodes, list) {
        log_debug (0, "waiting for message from '%s'\n", check_node->addr);
	if (node->info->ai_family != check_node->info->ai_family) {
            log_error (0, "mixing IPv4 and IPv6 nodes is not supported\n");
            return (1);
	}
    }

    for (;;) {
        error = select (node->socket + 1, &rfds, NULL, NULL, &timeout);
        if (error < 0) {
            log_error (2, "select (%s)\n", strerror (errno));
            break;
        }
        if (error == 0) {
            log_debug (0, "timeout after %d seconds\n", opts->timeout);
            break;
        }

        error = recvfrom (node->socket, &msg, sizeof (msg), 0, (struct sockaddr *) &ss, &size);
        if (error < 0) {
            log_error (2, "recvfrom (%s)\n", strerror (errno));
            continue;
        }

        error = getnameinfo ((struct sockaddr *) &ss, size,
                             addr, sizeof (addr),
                             port, sizeof (port),
                             NI_NUMERICHOST | NI_NUMERICSERV);
        if (error != 0) {
            log_error (2, "getnameinfo (%s)\n", gai_strerror (error));
            continue;
        }

        if (msg.magic != FENCE_KDUMP_MAGIC) {
            log_debug (1, "invalid magic number '0x%X'\n", msg.magic);
            continue;
        }

        // check if we have matched messages from any known node
        list_for_each_entry (check_node, &opts->nodes, list) {
            error = strcasecmp (check_node->addr, addr);
            if (error == 0 ) {
                switch (msg.version) {
                case FENCE_KDUMP_MSGV1:
                    log_debug (0, "received valid message from '%s'\n", addr);
                    return (0);
                default:
                    log_debug (1, "invalid message version '0x%X'\n", msg.version);
                    continue;
                }
            }
        }
    log_debug (1, "discard message from '%s'\n", addr);

    }

    return (1);
}

static int
do_action_metadata (const char *self)
{
    fprintf (stdout, "<?xml version=\"1.0\" ?>\n");
    fprintf (stdout, "<resource-agent name=\"%s\"", basename (self));
    fprintf (stdout, " shortdesc=\"fencing agent for use with kdump crash recovery service\">\n");
    fprintf (stdout, "<longdesc>");
    fprintf (stdout, "fence_kdump is an I/O fencing agent to be used with the kdump\n"
                     "crash recovery service. When the fence_kdump agent is invoked,\n"
                     "it will listen for a message from the failed node that acknowledges\n"
                     "that the failed node is executing the kdump crash kernel.\n"
                     "Note that fence_kdump is not a replacement for traditional\n"
                     "fencing methods. The fence_kdump agent can only detect that a\n"
                     "node has entered the kdump crash recovery service. This allows the\n"
                     "kdump crash recovery service complete without being preempted by\n"
                     "traditional power fencing methods.\n\n"
                     "Note: the \"off\" action listen for message from failed node that\n"
                     "acknowledges node has entered kdump crash recovery service. If a valid\n"
                     "message is received from the failed node, the node is considered to be\n"
                     "fenced and the agent returns success. Failure to receive a valid\n"
                     "message from the failed node in the given timeout period results in\n"
                     "fencing failure. When multiple node names/IP addresses are specified\n"
                     "a single valid message is sufficient for success. This is useful when\n"
                     "single node can send message via several different IP addresses.\n");
    fprintf (stdout, "</longdesc>\n");
    fprintf (stdout, "<vendor-url>http://www.kernel.org/pub/linux/utils/kernel/kexec/</vendor-url>\n");

    fprintf (stdout, "<parameters>\n");

    fprintf (stdout, "\t<parameter name=\"nodename\" unique=\"0\" required=\"0\">\n");
    fprintf (stdout, "\t\t<getopt mixed=\"-n, --nodename=NODE[,NODE...]\" />\n");
    fprintf (stdout, "\t\t<content type=\"string\" />\n");
    fprintf (stdout, "\t\t<shortdesc lang=\"en\">%s</shortdesc>\n",
             "List of names or IP addresses of node to be fenced. This option is\n"
	     "required for the \"off\" action. Multiple values separated by commas\n"
	     "can be specified. All values must be of same IP network family." );
    fprintf (stdout, "\t</parameter>\n");

    fprintf (stdout, "\t<parameter name=\"ipport\" unique=\"0\" required=\"0\">\n");
    fprintf (stdout, "\t\t<getopt mixed=\"-p, --ipport=PORT\" />\n");
    fprintf (stdout, "\t\t<content type=\"string\" default=\"7410\" />\n");
    fprintf (stdout, "\t\t<shortdesc lang=\"en\">%s</shortdesc>\n",
             "IP port number that the fence_kdump agent will use to listen for\n"
             "messages.");
    fprintf (stdout, "\t</parameter>\n");

    fprintf (stdout, "\t<parameter name=\"family\" unique=\"0\" required=\"0\">\n");
    fprintf (stdout, "\t\t<getopt mixed=\"-f, --family=FAMILY\" />\n");
    fprintf (stdout, "\t\t<content type=\"string\" default=\"auto\" />\n");
    fprintf (stdout, "\t\t<shortdesc lang=\"en\">%s</shortdesc>\n",
             "IP network family. Force the fence_kdump agent to use a specific\n"
             "family. The value for FAMILY can be \"auto\", \"ipv4\", or\n"
             "\"ipv6\".");
    fprintf (stdout, "\t</parameter>\n");

    fprintf (stdout, "\t<parameter name=\"action\" unique=\"0\" required=\"0\">\n");
    fprintf (stdout, "\t\t<getopt mixed=\"-o, --action=ACTION\" />\n");
    fprintf (stdout, "\t\t<content type=\"string\" default=\"off\" />\n");
    fprintf (stdout, "\t\t<shortdesc lang=\"en\">%s</shortdesc>\n",
             "Fencing action to perform. The value for ACTION can be either\n"
             "\"off\" or \"metadata\".");
    fprintf (stdout, "\t</parameter>\n");

    fprintf (stdout, "\t<parameter name=\"timeout\" unique=\"0\" required=\"0\">\n");
    fprintf (stdout, "\t\t<getopt mixed=\"-t, --timeout=TIMEOUT\" />\n");
    fprintf (stdout, "\t\t<content type=\"string\" default=\"60\" />\n");
    fprintf (stdout, "\t\t<shortdesc lang=\"en\">%s</shortdesc>\n",
             "Number of seconds to wait for message from failed node. If no message\n"
             "is received within TIMEOUT seconds, the fence_kdump agent\n"
             "returns failure.");
    fprintf (stdout, "\t</parameter>\n");

    fprintf (stdout, "\t<parameter name=\"verbose\" unique=\"0\" required=\"0\">\n");
    fprintf (stdout, "\t\t<getopt mixed=\"-v, --verbose\" />\n");
    fprintf (stdout, "\t\t<content type=\"boolean\" />\n");
    fprintf (stdout, "\t\t<shortdesc lang=\"en\">%s</shortdesc>\n",
             "Print verbose output");
    fprintf (stdout, "\t</parameter>\n");

    fprintf (stdout, "\t<parameter name=\"version\" unique=\"0\" required=\"0\">\n");
    fprintf (stdout, "\t\t<getopt mixed=\"-V, --version\" />\n");
    fprintf (stdout, "\t\t<content type=\"boolean\" />\n");
    fprintf (stdout, "\t\t<shortdesc lang=\"en\">%s</shortdesc>\n",
             "Print version");
    fprintf (stdout, "\t</parameter>\n");

    fprintf (stdout, "\t<parameter name=\"usage\" unique=\"0\" required=\"0\">\n");
    fprintf (stdout, "\t\t<getopt mixed=\"-h, --help\" />\n");
    fprintf (stdout, "\t\t<content type=\"boolean\" />\n");
    fprintf (stdout, "\t\t<shortdesc lang=\"en\">%s</shortdesc>\n",
             "Print usage");
    fprintf (stdout, "\t</parameter>\n");

    fprintf (stdout, "</parameters>\n");

    fprintf (stdout, "<actions>\n");
    fprintf (stdout, "\t<action name=\"off\" />\n");
    fprintf (stdout, "\t<action name=\"monitor\" />\n");
    fprintf (stdout, "\t<action name=\"metadata\" />\n");
    fprintf (stdout, "\t<action name=\"validate-all\" />\n");
    fprintf (stdout, "</actions>\n");

    fprintf (stdout, "</resource-agent>\n");

    return (0);
}

static void
print_usage (const char *self)
{
    fprintf (stdout, "Usage: %s [options]\n", basename (self));
    fprintf (stdout, "\n");
    fprintf (stdout, "Options:\n");
    fprintf (stdout, "\n");
    fprintf (stdout, "%s\n",
             "  -n, --nodename=NODE[,NODE...]List of names or IP addresses of node to be fenced");
    fprintf (stdout, "%s\n",
             "  -p, --ipport=PORT            IP port number (default: 7410)");
    fprintf (stdout, "%s\n",
             "  -f, --family=FAMILY          Network family: ([auto], ipv4, ipv6)");
    fprintf (stdout, "%s\n",
             "  -o, --action=ACTION          Fencing action: ([off], monitor, metadata, validate-all)");
    fprintf (stdout, "%s\n",
             "  -t, --timeout=TIMEOUT        Timeout in seconds (default: 60)");
    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[] = {
        { "nodename", required_argument, NULL, 'n' },
        { "ipport",   required_argument, NULL, 'p' },
        { "family",   required_argument, NULL, 'f' },
        { "action",   required_argument, NULL, 'o' },
        { "timeout",  required_argument, NULL, 't' },
        { "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, "n:p:f:o:t:v::Vh", options, NULL)) != EOF) {
        switch (opt) {
        case 'n':
            set_option_nodename (opts, optarg);
            break;
        case 'p':
            set_option_ipport (opts, optarg);
            break;
        case 'f':
            set_option_family (opts, optarg);
            break;
        case 'o':
            set_option_action (opts, optarg);
            break;
        case 't':
            set_option_timeout (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;
}

static void
get_options_stdin (fence_kdump_opts_t *opts)
{
    char buf[1024];
    char *opt;
    char *arg;

    while (fgets (buf, sizeof (buf), stdin) != 0) {
        if (trim (buf) == 0) {
            continue;
        }
        if (buf[0] == '#') {
            continue;
        }

        opt = buf;

        if ((arg = strchr (opt, '=')) != 0) {
            *arg = 0;
            arg += 1;
        } else {
            continue;
        }

        if (!strcasecmp (opt, "nodename")) {
            set_option_nodename (opts, arg);
            continue;
        }
        if (!strcasecmp (opt, "ipport")) {
            set_option_ipport (opts, arg);
            continue;
        }
        if (!strcasecmp (opt, "family")) {
            set_option_family (opts, arg);
            continue;
        }
        if (!strcasecmp (opt, "action")) {
            set_option_action (opts, arg);
            continue;
        }
        if (!strcasecmp (opt, "timeout")) {
            set_option_timeout (opts, arg);
            continue;
        }
        if (!strcasecmp (opt, "verbose")) {
            set_option_verbose (opts, arg);
            continue;
        }
    }

    verbose = opts->verbose;

    return;
}

int
main (int argc, char **argv)
{
    int error = 1;
    fence_kdump_opts_t opts;
    char *ptr;
    char *node_list;

    init_options (&opts);

    if (argc > 1) {
        get_options (argc, argv, &opts);
    } else {
        get_options_stdin (&opts);
    }

    openlog ("fence_kdump", LOG_CONS|LOG_PID, LOG_DAEMON);

    if (opts.action == FENCE_KDUMP_ACTION_OFF) {
        if (opts.nodename == NULL) {
            log_error (0, "action 'off' requires nodename\n");
            exit (1);
        }
        node_list = (char *)malloc(strlen(opts.nodename)+1);

        strcpy(node_list, opts.nodename); //make local copy of nodename on which we can safely iterate
        // iterate through node_list
        for (ptr = strtok(node_list, ","); ptr != NULL; ptr = strtok(NULL, ",")) {
            set_option_nodename (&opts, ptr); //overwrite nodename for next function
            if (get_options_node (&opts) != 0) {
                log_error (0, "failed to get node '%s'\n", opts.nodename);
                exit (1);
            }
        }
        free(node_list);
    }

    if (verbose != 0) {
        //clear nodename to avoid showing just last nodename here
        free(opts.nodename);
        opts.nodename = NULL;
        print_options (&opts);
    }

    switch (opts.action) {
    case FENCE_KDUMP_ACTION_OFF:
        error = do_action_off (&opts);
        break;
    case FENCE_KDUMP_ACTION_METADATA:
        error = do_action_metadata (argv[0]);
        break;
    case FENCE_KDUMP_ACTION_MONITOR:
        error = do_action_monitor ();
        break;
    case FENCE_KDUMP_ACTION_VALIDATE:
	error = 0;
	break;
    default:
        break;
    }

    free_options (&opts);

    return (error);
}