File: fence_node.c

package info (click to toggle)
redhat-cluster 3.1.8-1.2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,608 kB
  • ctags: 6,306
  • sloc: ansic: 62,895; sh: 1,626; makefile: 1,143; perl: 765
file content (251 lines) | stat: -rw-r--r-- 5,190 bytes parent folder | download | duplicates (3)
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <liblogthread.h>
#include <libcman.h>

#include "libfence.h"
#include "libfenced.h"
#include "copyright.cf"

static char *prog_name;
static char our_name[CMAN_MAX_NODENAME_LEN+1];
static int verbose;
static int unfence;

#define FL_SIZE 32
static struct fence_log flog[FL_SIZE];
static int flog_count;
static const char *action = "fence";

#define OPTION_STRING "UvhV"

#define die(fmt, args...) \
do \
{ \
  fprintf(stderr, "%s: ", prog_name); \
  fprintf(stderr, fmt "\n", ##args); \
  exit(EXIT_FAILURE); \
} \
while (0)

static void print_usage(void)
{
	printf("Usage:\n");
	printf("\n");
	printf("%s [options] node_name\n", prog_name);
	printf("\n");
	printf("Options:\n");
	printf("\n");
	printf("  -U    Unfence the node, default local node name\n");
	printf("  -v    Show fence agent results, -vv for agent args\n");
	printf("  -h    Print this help, then exit\n");
	printf("  -V    Print program version information, then exit\n");
	printf("\n");
}

static int setup_cman(void)
{
	cman_handle_t ch;
	cman_node_t node;
	int active = 0;
	int rv;

	ch = cman_init(NULL);
	if (!ch)
		return -1;

 retry_active:
	rv = cman_is_active(ch);
	if (!rv) {
		if (active++ < 2) {
			sleep(1);
			goto retry_active;
		}
		cman_finish(ch);
		return -1;
	}

	memset(&node, 0, sizeof(node));
	rv = cman_get_node(ch, CMAN_NODEID_US, &node);
	if (rv < 0) {
		cman_finish(ch);
		return -1;
	}

	memset(our_name, 0, sizeof(our_name));
	strncpy(our_name, node.cn_name, CMAN_MAX_NODENAME_LEN);
	cman_finish(ch);
	return 0;
}

static const char *fe_str(int r)
{
	switch (r) {
	case FE_AGENT_SUCCESS:
		return "success";
	case FE_AGENT_ERROR:
		return "error from agent";
	case FE_AGENT_FORK:
		return "error from fork";
	case FE_NO_CONFIG:
		return "error from ccs";
	case FE_NO_METHOD:
		return "error no method";
	case FE_NO_DEVICE:
		return "error no device";
	case FE_READ_AGENT:
		return "error config agent";
	case FE_READ_ARGS:
		return "error config args";
	case FE_READ_METHOD:
		return "error config method";
	case FE_READ_DEVICE:
		return "error config device";
	default:
		return "error unknown";
	}
}

int main(int argc, char *argv[])
{
	char *victim = NULL;
	int cont = 1, optchar, error, rv, i, c;

	prog_name = argv[0];

	while (cont) {
		optchar = getopt(argc, argv, OPTION_STRING);

		switch (optchar) {

		case 'U':
			unfence = 1;
			action = "unfence";
			break;

		case 'v':
			verbose++;
			break;

		case 'h':
			print_usage();
			exit(EXIT_SUCCESS);
			break;

		case 'V':
			printf("%s %s (built %s %s)\n", prog_name,
				RELEASE_VERSION, __DATE__, __TIME__);
			printf("%s\n", REDHAT_COPYRIGHT);
			exit(EXIT_SUCCESS);
			break;

		case ':':
		case '?':
			fprintf(stderr, "Please use '-h' for usage.\n");
			exit(EXIT_FAILURE);
			break;

		case EOF:
			cont = 0;
			break;

		default:
			die("unknown option: %c", optchar);
			break;
		};
	}

	while (optind < argc) {
		if (victim)
			die("unknown option %s", argv[optind]);
		victim = argv[optind];
		optind++;
	}

	if (!victim && !unfence)
		die("no node name specified");

	error = setup_cman();
	if (error)
		die("cannot connect to cman");

	if (!victim && unfence)
		victim = our_name;

	memset(&flog, 0, sizeof(flog));
	flog_count = 0;

	if (unfence)
		error = unfence_node(victim, flog, FL_SIZE, &flog_count);
	else
		error = fence_node(victim, flog, FL_SIZE, &flog_count);

	if (!verbose)
		goto skip;

	if (flog_count > FL_SIZE) {
		fprintf(stderr, "%s_node log overflow %d", action, flog_count);
		flog_count = FL_SIZE;
	}

	for (i = 0; i < flog_count; i++) {
		fprintf(stderr, "%s %s dev %d.%d agent %s result: %s\n",
			action, victim, flog[i].method_num, flog[i].device_num,
			flog[i].agent_name[0] ? flog[i].agent_name : "none",
			fe_str(flog[i].error));

		if (verbose < 2)
			continue;

		for (c = 0; c < strlen(flog[i].agent_args); c++) {
			if (flog[i].agent_args[c] == '\n')
				flog[i].agent_args[c] = ' ';
		}
		fprintf(stderr, "agent args: %s\n", flog[i].agent_args);
	}

 skip:
	logt_init("fence_node", LOG_MODE_OUTPUT_SYSLOG, SYSLOGFACILITY,
		  SYSLOGLEVEL, 0, NULL);

	if (unfence) {
		if (error == -2) {
			fprintf(stderr, "unfence %s undefined\n", victim);
			rv = 2;
		} else if (error) {
			fprintf(stderr, "unfence %s failed\n", victim);
			logt_print(LOG_ERR, "unfence %s failed\n", victim);
			rv = EXIT_FAILURE;
		} else {
			fprintf(stderr, "unfence %s success\n", victim);
			logt_print(LOG_ERR, "unfence %s success\n", victim);
			rv = EXIT_SUCCESS;
		}
	} else {
		if (error == -2) {
			fprintf(stderr, "fence %s undefined\n", victim);
			logt_print(LOG_ERR, "fence %s undefined\n", victim);
			rv = 2;
		} else if (error) {
			fprintf(stderr, "fence %s failed\n", victim);
			logt_print(LOG_ERR, "fence %s failed\n", victim);
			rv = EXIT_FAILURE;
		} else {
			fprintf(stderr, "fence %s success\n", victim);
			logt_print(LOG_ERR, "fence %s success\n", victim);
			rv = EXIT_SUCCESS;

			/* Tell fenced what we've done so that it can avoid
			   fencing this node again if the fence_node() rebooted
			   it. */
			fenced_external(victim);
		}
	}

	logt_exit();
	exit(rv);
}