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
|
#include <config.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <limits.h>
#include <sys/mman.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <syslog.h>
#include "sfex.h"
#include "sfex_lib.h"
#if HAVE_GLUE_CONFIG_H
#include <glue_config.h> /* for HA_LOG_FACILITY */
#endif
static int sysrq_fd;
static int lock_index = 1; /* default 1st lock */
static time_t collision_timeout = 1; /* default 1 sec */
static time_t lock_timeout = 60; /* default 60 sec */
time_t unlock_timeout = 60;
static time_t monitor_interval = 10;
static sfex_controldata cdata;
static sfex_lockdata ldata;
static sfex_lockdata ldata_new;
static const char *device;
const char *progname;
char *nodename;
static const char *rsc_id = "sfex";
static void usage(FILE *dist) {
fprintf(dist, "usage: %s [-i <index>] [-c <collision_timeout>] [-t <lock_timeout>] <device>\n", progname);
}
static int lock_index_check(void)
{
if (read_controldata(&cdata) == -1) {
cl_log(LOG_ERR, "%s\n", "read_controldata failed in lock_index_check");
return -1;
}
#ifdef SFEX_DEBUG
cl_log(LOG_INFO, "version: %d\n", cdata.version);
cl_log(LOG_INFO, "revision: %d\n", cdata.revision);
cl_log(LOG_INFO, "blocksize: %d\n", cdata.blocksize);
cl_log(LOG_INFO, "numlocks: %d\n", cdata.numlocks);
#endif
if (lock_index > cdata.numlocks) {
cl_log(LOG_ERR, "index %d is too large. %d locks are stored.\n",
lock_index, cdata.numlocks);
return -1;
/*exit(EXIT_FAILURE);*/
}
if (cdata.blocksize != sector_size) {
cl_log(LOG_ERR, "sector_size is not the same as the blocksize.\n");
return -1;
}
return 0;
}
static void acquire_lock(void)
{
if (read_lockdata(&cdata, &ldata, lock_index) == -1) {
cl_log(LOG_ERR, "read_lockdata failed in acquire_lock\n");
exit(EXIT_FAILURE);
}
if ((ldata.status == SFEX_STATUS_LOCK) && (strncmp(nodename, (const char*)(ldata.nodename), sizeof(ldata.nodename)))) {
unsigned int t = lock_timeout;
while (t > 0)
t = sleep(t);
read_lockdata(&cdata, &ldata_new, lock_index);
if (ldata.count != ldata_new.count) {
cl_log(LOG_ERR, "can\'t acquire lock: the lock's already hold by some other node.\n");
exit(2);
}
}
/* The lock acquisition is possible because it was not updated. */
ldata.status = SFEX_STATUS_LOCK;
ldata.count = SFEX_NEXT_COUNT(ldata.count);
strncpy((char*)(ldata.nodename), nodename, sizeof(ldata.nodename));
if (write_lockdata(&cdata, &ldata, lock_index) == -1) {
cl_log(LOG_ERR, "write_lockdata failed\n");
exit(EXIT_FAILURE);
}
/* detect the collision of lock */
/* The collision occurs when two or more nodes do the reservation
processing of the lock at the same time. It waits for collision_timeout
seconds to detect this,and whether the superscription of lock data by
another node is done is checked. If the superscription was done by
another node, the lock acquisition with the own node is given up.
*/
{
unsigned int t = collision_timeout;
while (t > 0)
t = sleep(t);
if (read_lockdata(&cdata, &ldata_new, lock_index) == -1) {
cl_log(LOG_ERR, "read_lockdata failed in collision detection\n");
}
if (strncmp((char*)(ldata.nodename), (const char*)(ldata_new.nodename), sizeof(ldata.nodename))) {
cl_log(LOG_ERR, "can\'t acquire lock: collision detected in the air.\n");
exit(2);
}
}
/* extension of lock */
/* Validly time of the lock is extended. It is because of spending at
the collision_timeout seconds to detect the collision. */
ldata.count = SFEX_NEXT_COUNT(ldata.count);
if (write_lockdata(&cdata, &ldata, lock_index) == -1) {
cl_log(LOG_ERR, "write_lockdata failed in extension of lock\n");
exit(EXIT_FAILURE);
}
cl_log(LOG_INFO, "lock acquired\n");
}
static void error_todo (void)
{
if (fork() == 0) {
cl_log(LOG_INFO, "Execute \"crm_resource -F -r %s -H %s\" command\n", rsc_id, nodename);
execl("/usr/sbin/crm_resource", "crm_resource", "-F", "-r", rsc_id, "-H", nodename, NULL);
} else {
exit(EXIT_FAILURE);
}
}
static void failure_todo(void)
{
#ifdef SFEX_TESTING
exit(EXIT_FAILURE);
#else
/*execl("/usr/sbin/crm_resource", "crm_resource", "-F", "-r", rsc_id, "-H", nodename, NULL); */
int ret;
cl_log(LOG_INFO, "Force reboot node %s\n", nodename);
ret = write(sysrq_fd, "b\n", 2);
if (ret == -1) {
cl_log(LOG_ERR, "%s\n", strerror(errno));
}
close(sysrq_fd);
exit(EXIT_FAILURE);
#endif
}
static void update_lock(void)
{
/* read lock data */
if (read_lockdata(&cdata, &ldata, lock_index) == -1) {
cl_log(LOG_ERR, "read_lockdata failed in update_lock\n");
error_todo();
exit(EXIT_FAILURE);
}
/* check current lock status */
/* if own node is not locking, lock update is failed */
if (ldata.status != SFEX_STATUS_LOCK || strncmp((const char*)(ldata.nodename), nodename, sizeof(ldata.nodename))) {
cl_log(LOG_ERR, "can't update lock.\n");
failure_todo();
exit(EXIT_FAILURE);
}
/* lock update */
ldata.count = SFEX_NEXT_COUNT(ldata.count);
if (write_lockdata(&cdata, &ldata, lock_index) == -1) {
cl_log(LOG_ERR, "write_lockdata failed in update_lock\n");
error_todo();
exit(EXIT_FAILURE);
}
}
static void release_lock(void)
{
/* The only thing I care about in release_lock(), is to terminate the process */
/* read lock data */
if (read_lockdata(&cdata, &ldata, lock_index) == -1) {
cl_log(LOG_ERR, "read_lockdata failed in release_lock\n");
exit(EXIT_FAILURE);
}
/* check current lock status */
/* if own node is not locking, we judge that lock has been released already */
if (ldata.status != SFEX_STATUS_LOCK || strncmp((const char*)(ldata.nodename), nodename, sizeof(ldata.nodename))) {
cl_log(LOG_ERR, "lock was already released.\n");
exit(EXIT_FAILURE);
}
/* lock release */
ldata.status = SFEX_STATUS_UNLOCK;
if (write_lockdata(&cdata, &ldata, lock_index) == -1) {
/*FIXME: We are going to self-stop */
cl_log(LOG_ERR, "write_lockdata failed in release_lock\n");
exit(EXIT_FAILURE);
}
cl_log(LOG_INFO, "lock released\n");
}
static void quit_handler(int signo, siginfo_t *info, void *context)
{
cl_log(LOG_INFO, "quit_handler called. now releasing lock\n");
release_lock();
cl_log(LOG_INFO, "Shutdown sfex_daemon with EXIT_SUCCESS\n");
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[])
{
int ret;
progname = get_progname(argv[0]);
nodename = get_nodename();
cl_log_set_entity(progname);
cl_log_set_facility(HA_LOG_FACILITY);
cl_inherit_logging_environment(0);
/* read command line option */
opterr = 0;
while (1) {
int c = getopt(argc, argv, "hi:c:t:m:n:r:");
if (c == -1)
break;
switch (c) {
case 'h': /* help*/
usage(stdout);
exit(EXIT_SUCCESS);
case 'i': /* -i <index> */
{
unsigned long l = strtoul(optarg, NULL, 10);
if (l < SFEX_MIN_NUMLOCKS || l > SFEX_MAX_NUMLOCKS) {
cl_log(LOG_ERR,
"index %s is out of range or invalid. it must be integer value between %lu and %lu.\n",
optarg,
(unsigned long)SFEX_MIN_NUMLOCKS,
(unsigned long)SFEX_MAX_NUMLOCKS);
exit(4);
}
lock_index = l;
}
break;
case 'c': /* -c <collision_timeout> */
{
unsigned long l = strtoul(optarg, NULL, 10);
if (l < 1 || l > INT_MAX) {
cl_log(LOG_ERR,
"collision_timeout %s is out of range or invalid. it must be integer value between %lu and %lu.\n",
optarg,
(unsigned long)1,
(unsigned long)INT_MAX);
exit(4);
}
collision_timeout = l;
}
break;
case 'm': /* -m <monitor_interval> */
{
unsigned long l = strtoul(optarg, NULL, 10);
if (l < 1 || l > INT_MAX) {
cl_log(LOG_ERR,
"monitor_interval %s is out of range or invalid. it must be integer value between %lu and %lu.\n",
optarg,
(unsigned long)1,
(unsigned long)INT_MAX);
exit(4);
}
monitor_interval = l;
}
break;
case 't': /* -t <lock_timeout> */
{
unsigned long l = strtoul(optarg, NULL, 10);
if (l < 1 || l > INT_MAX) {
cl_log(LOG_ERR,
"lock_timeout %s is out of range or invalid. it must be integer value between %lu and %lu.\n",
optarg,
(unsigned long)1,
(unsigned long)INT_MAX);
exit(4);
}
lock_timeout = l;
}
break;
case 'n':
{
free(nodename);
if (strlen(optarg) > SFEX_MAX_NODENAME) {
cl_log(LOG_ERR, "nodename %s is too long. must be less than %d byte.\n",
optarg,
(unsigned int)SFEX_MAX_NODENAME);
exit(EXIT_FAILURE);
}
nodename = strdup(optarg);
}
break;
case 'r':
{
rsc_id = strdup(optarg);
}
break;
case '?': /* error */
usage(stderr);
exit(4);
}
}
/* check parameter except the option */
if (optind >= argc) {
cl_log(LOG_ERR, "no device specified.\n");
usage(stderr);
exit(EXIT_FAILURE);
} else if (optind + 1 < argc) {
cl_log(LOG_ERR, "too many arguments.\n");
usage(stderr);
exit(EXIT_FAILURE);
}
device = argv[optind];
prepare_lock(device);
#if !SFEX_TESTING
sysrq_fd = open("/proc/sysrq-trigger", O_WRONLY);
if (sysrq_fd == -1) {
cl_log(LOG_ERR, "failed to open /proc/sysrq-trigger due to %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
#endif
ret = lock_index_check();
if (ret == -1)
exit(EXIT_FAILURE);
{
struct sigaction sig_act;
sigemptyset (&sig_act.sa_mask);
sig_act.sa_flags = SA_SIGINFO;
sig_act.sa_sigaction = quit_handler;
ret = sigaction(SIGTERM, &sig_act, NULL);
if (ret == -1) {
cl_log(LOG_ERR, "sigaction failed\n");
exit(EXIT_FAILURE);
}
}
cl_log(LOG_INFO, "Starting SFeX Daemon...\n");
/* acquire lock first.*/
acquire_lock();
if (daemon(0, 1) != 0) {
cl_perror("%s::%d: daemon() failed.", __FUNCTION__, __LINE__);
release_lock();
exit(EXIT_FAILURE);
}
cl_make_realtime(-1, -1, 128, 128);
cl_log(LOG_INFO, "SFeX Daemon started.\n");
while (1) {
sleep (monitor_interval);
update_lock();
}
}
|