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
|
/*
Copyright Red Hat, Inc. 2010
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, 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; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
MA 02139, USA.
*/
/*
* Author: Lon Hohberger <lhh at redhat.com>
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/time.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <nss.h>
#include <libgen.h>
/* Local includes */
#include "debug.h"
#include "fdops.h"
#include "serial.h"
#include "list.h"
#include "simpleconfig.h"
#include "static_map.h"
#include "server_plugin.h"
#include "history.h"
#include "xvm.h"
#define NAME "serial"
#define SERIAL_VERSION "0.5"
#define SERIAL_PLUG_MAGIC 0x1227a000
#define VALIDATE(info) \
do {\
if (!info || info->magic != SERIAL_PLUG_MAGIC)\
return -EINVAL;\
} while(0)
typedef struct _serial_info {
uint64_t magic;
const fence_callbacks_t *cb;
void *priv;
char *uri;
char *path;
history_info_t *history;
map_object_t *maps;
int mode;
int wake_fd;
} serial_info;
struct serial_hostlist_arg {
map_object_t *map;
const char *src;
int fd;
};
/*
* See if we fenced this node recently (successfully)
* If so, ignore the request for a few seconds.
*
* We purge our history when the entries time out.
*/
static int
check_history(void *a, void *b) {
serial_req_t *old = a, *current = b;
if (old->request == current->request &&
old->seqno == current->seqno &&
!strcasecmp((const char *)old->domain,
(const char *)current->domain)) {
return 1;
}
return 0;
}
static int
serial_hostlist(const char *vm_name, const char *vm_uuid,
int state, void *priv)
{
struct serial_hostlist_arg *arg = (struct serial_hostlist_arg *)priv;
host_state_t hinfo;
struct timeval tv;
int ret;
if (map_check2(arg->map, arg->src, vm_uuid, vm_name) == 0) {
/* if we don't have access to fence this VM,
* we should not see it in a hostlist either */
return 0;
}
strncpy((char *)hinfo.domain, vm_name, sizeof(hinfo.domain) - 1);
strncpy((char *)hinfo.uuid, vm_uuid, sizeof(hinfo.uuid) - 1);
hinfo.state = state;
tv.tv_sec = 1;
tv.tv_usec = 0;
ret = _write_retry(arg->fd, &hinfo, sizeof(hinfo), &tv);
if (ret == sizeof(hinfo))
return 0;
return 1;
}
static int
serial_hostlist_begin(int fd)
{
struct timeval tv;
serial_resp_t resp;
resp.magic = SERIAL_MAGIC;
resp.response = RESP_HOSTLIST;
tv.tv_sec = 1;
tv.tv_usec = 0;
return _write_retry(fd, &resp, sizeof(resp), &tv);
}
static int
serial_hostlist_end(int fd)
{
host_state_t hinfo;
struct timeval tv;
int ret;
//printf("Sending terminator packet\n");
memset(&hinfo, 0, sizeof(hinfo));
tv.tv_sec = 1;
tv.tv_usec = 0;
ret = _write_retry(fd, &hinfo, sizeof(hinfo), &tv);
if (ret == sizeof(hinfo))
return 0;
return 1;
}
static int
do_fence_request(int fd, const char *src, serial_req_t *req, serial_info *info)
{
char response = RESP_FAIL;
struct serial_hostlist_arg arg;
serial_resp_t resp;
arg.fd = fd;
switch(req->request) {
case FENCE_NULL:
response = info->cb->null((char *)req->domain, info->priv);
break;
case FENCE_ON:
if (map_check(info->maps, src,
(const char *)req->domain) == 0) {
response = RESP_PERM;
break;
}
response = info->cb->on((char *)req->domain, src,
req->seqno, info->priv);
break;
case FENCE_OFF:
if (map_check(info->maps, src,
(const char *)req->domain) == 0) {
response = RESP_PERM;
break;
}
response = info->cb->off((char *)req->domain, src,
req->seqno, info->priv);
break;
case FENCE_REBOOT:
if (map_check(info->maps, src,
(const char *)req->domain) == 0) {
response = RESP_PERM;
break;
}
response = info->cb->reboot((char *)req->domain, src,
req->seqno, info->priv);
break;
case FENCE_STATUS:
if (map_check(info->maps, src,
(const char *)req->domain) == 0) {
response = RESP_PERM;
break;
}
response = info->cb->status((char *)req->domain, info->priv);
break;
case FENCE_DEVSTATUS:
response = info->cb->devstatus(info->priv);
break;
case FENCE_HOSTLIST:
arg.map = info->maps;
arg.src = src;
arg.fd = fd;
serial_hostlist_begin(arg.fd);
response = info->cb->hostlist(serial_hostlist, &arg,
info->priv);
serial_hostlist_end(arg.fd);
break;
}
resp.magic = SERIAL_MAGIC;
resp.response = response;
swab_serial_resp_t(&resp);
dbg_printf(3, "Sending response to caller...\n");
if (_write_retry(fd, &resp, sizeof(resp), NULL) < 0)
perror("write");
/* XVM shotguns multicast packets, so we want to avoid
* acting on the same request multiple times if the first
* attempt was successful.
*/
history_record(info->history, req);
return 1;
}
static int
serial_dispatch(listener_context_t c, struct timeval *timeout)
{
char src_domain[MAX_DOMAINNAME_LENGTH];
serial_info *info;
serial_req_t data;
fd_set rfds;
struct timeval tv;
int max;
int n, x, ret;
info = (serial_info *)c;
VALIDATE(info);
FD_ZERO(&rfds);
domain_sock_fdset(&rfds, &max);
FD_SET(info->wake_fd, &rfds);
if (info->wake_fd > max)
max = info->wake_fd;
n = select(max+1, &rfds, NULL, NULL, timeout);
if (n < 0) {
if (errno == EINTR || errno == EAGAIN)
n = 0;
else
dbg_printf(2, "select: %s\n", strerror(errno));
return n;
}
/*
* See if the goal was just to be woken up in order to refill our
* file descriptor set. For example, if multiple domains were
* created simultaneously, we would have to refill our fd_set
*/
if (FD_ISSET(info->wake_fd, &rfds)) {
tv.tv_sec = 0;
tv.tv_usec = 10000;
_read_retry(info->wake_fd, &c, 1, &tv);
return 0;
}
/*
* If no requests, we're done
*/
if (n == 0)
return 0;
/* find & read request */
for (x = 0; x <= max; x++) {
if (FD_ISSET(x, &rfds)) {
tv.tv_sec = 1;
tv.tv_usec = 0;
ret = _read_retry(x, &data, sizeof(data), &tv);
if (ret != sizeof(data)) {
if (--n > 0)
continue;
else
return 0;
} else {
swab_serial_req_t(&data);
break;
}
}
}
src_domain[0] = 0;
domain_sock_name(x, src_domain, sizeof(src_domain));
dbg_printf(2, "Sock %d Request %d seqno %d src %s target %s\n", x,
data.request, data.seqno, src_domain, data.domain);
if (history_check(info->history, &data) == 1) {
dbg_printf(3, "We just did this request; dropping packet\n");
return 0;
}
do_fence_request(x, src_domain[0] == 0 ? NULL : src_domain,
&data, info);
return 0;
}
static int
serial_config(config_object_t *config, serial_info *args)
{
char value[1024];
int errors = 0;
if (sc_get(config, "fence_virtd/@debug", value, sizeof(value))==0)
dset(atoi(value));
if (sc_get(config, "listeners/serial/@uri",
value, sizeof(value)-1) == 0) {
dbg_printf(1, "Got %s for uri\n", value);
args->uri = strdup(value);
}
if (sc_get(config, "listeners/serial/@path",
value, sizeof(value)-1) == 0) {
dbg_printf(1, "Got %s for path\n", value);
args->path = strdup(value);
}
if (sc_get(config, "listeners/serial/@mode",
value, sizeof(value)-1) == 0) {
if (!strcasecmp(value, "vmchannel")) {
args->mode = 1;
} else if (!strcasecmp(value, "serial")) {
args->mode = 0;
} else {
args->mode = atoi(value);
if (args->mode < 0)
args->mode = 0;
}
dbg_printf(1, "Got %s for mode\n",
args->mode?"VMChannel":"serial");
}
return errors;
}
static int
serial_init(listener_context_t *c, const fence_callbacks_t *cb,
config_object_t *config, map_object_t *map, void *priv)
{
serial_info *info;
int ret;
info = malloc(sizeof(*info));
if (!info)
return -1;
memset(info, 0, sizeof(*info));
info->priv = priv;
info->cb = cb;
ret = serial_config(config, info);
if (ret < 0) {
perror("serial_config");
return -1;
} else if (ret > 0) {
printf("%d errors found during configuration\n",ret);
return -1;
}
info->maps = map;
info->magic = SERIAL_PLUG_MAGIC;
info->history = history_init(check_history, 10, sizeof(fence_req_t));
*c = (listener_context_t)info;
start_event_listener(info->uri, info->path, info->mode, &info->wake_fd);
sleep(1);
return 0;
}
static int
serial_shutdown(listener_context_t c)
{
serial_info *info = (serial_info *)c;
dbg_printf(3, "Shutting down serial\n");
VALIDATE(info);
info->magic = 0;
stop_event_listener();
domain_sock_cleanup();
history_wipe(info->history);
free(info->history);
free(info->uri);
free(info->path);
free(info);
return 0;
}
static listener_plugin_t serial_plugin = {
.name = NAME,
.version = SERIAL_VERSION,
.init = serial_init,
.dispatch = serial_dispatch,
.cleanup = serial_shutdown,
};
double
LISTENER_VER_SYM(void)
{
return PLUGIN_VERSION_LISTENER;
}
const listener_plugin_t *
LISTENER_INFO_SYM(void)
{
return &serial_plugin;
}
|