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
|
/* Copyright (C) 2013-2015 Codership Oy <info@codership.com>
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; version 2 of the License.
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-1335 USA. */
#ifndef WSREP_UTILS_H
#define WSREP_UTILS_H
#include "wsrep_priv.h"
#include "wsrep_mysqld.h"
unsigned int wsrep_check_ip (const char* const addr, bool *is_ipv6);
size_t wsrep_guess_ip (char* buf, size_t buf_len);
namespace wsp {
class node_status
{
public:
node_status() : status(wsrep::server_state::s_disconnected) {}
void set(enum wsrep::server_state::state new_status,
const wsrep::view* view= 0)
{
if (status != new_status || 0 != view)
{
wsrep_notify_status(new_status, view);
status= new_status;
}
}
enum wsrep::server_state::state get() const { return status; }
private:
enum wsrep::server_state::state status;
};
} /* namespace wsp */
extern wsp::node_status local_status;
/* returns the length of the host part of the address string */
size_t wsrep_host_len(const char* addr, size_t addr_len);
namespace wsp {
class Address {
public:
Address()
: m_address_len(0), m_family(UNSPEC), m_port(0), m_valid(false)
{
memset(m_address, 0, sizeof(m_address));
}
Address(const char *addr_in)
: m_address_len(0), m_family(UNSPEC), m_port(0), m_valid(false)
{
memset(m_address, 0, sizeof(m_address));
parse_addr(addr_in);
}
bool is_valid() { return m_valid; }
bool is_ipv6() { return (m_family == INET6); }
const char* get_address() { return m_address; }
size_t get_address_len() { return m_address_len; }
int get_port() { return m_port; }
void set_port(int port) { m_port= port; }
private:
enum family {
UNSPEC= 0,
INET, /* IPv4 */
INET6, /* IPv6 */
};
char m_address[256];
size_t m_address_len;
family m_family;
int m_port;
bool m_valid;
void parse_addr(const char *addr_in) {
const char *start;
const char *end;
const char *port;
const char* open_bracket= strchr(const_cast<char *>(addr_in), '[');
const char* close_bracket= strchr(const_cast<char *>(addr_in), ']');
const char* colon= strchr(const_cast<char *>(addr_in), ':');
const char* dot= strchr(const_cast<char *>(addr_in), '.');
int cc= colon_count(addr_in);
if (open_bracket != NULL ||
dot == NULL ||
(colon != NULL && (dot == NULL || colon < dot)))
{
// This could be an IPv6 address or a hostname
if (open_bracket != NULL) {
/* Sanity check: Address with '[' must include ']' */
if (close_bracket == NULL &&
open_bracket < close_bracket) /* Error: malformed address */
{
m_valid= false;
return;
}
start= open_bracket + 1;
end= close_bracket;
/* Check for port */
port= strchr(close_bracket, ':');
if ((port != NULL) && parse_port(port + 1))
{
return; /* Error: invalid port */
}
m_family= INET6;
}
else
{
switch (cc) {
case 0:
/* Hostname with no port */
start= addr_in;
end= addr_in + strlen(addr_in);
break;
case 1:
/* Hostname with port (host:port) */
start= addr_in;
end= colon;
if (parse_port(colon + 1))
return; /* Error: invalid port */
break;
default:
/* IPv6 address */
start= addr_in;
end= addr_in + strlen(addr_in);
m_family= INET6;
break;
}
}
} else { /* IPv4 address or hostname */
start= addr_in;
if (colon != NULL) { /* Port */
end= colon;
if (parse_port(colon + 1))
return; /* Error: invalid port */
} else {
end= addr_in + strlen(addr_in);
}
}
size_t len= end - start;
/* Safety */
if (len >= sizeof(m_address))
{
// The supplied address is too large to fit into the internal buffer.
m_valid= false;
return;
}
memcpy(m_address, start, len);
m_address[len]= '\0';
m_address_len= ++ len;
m_valid= true;
return;
}
int colon_count(const char *addr) {
int count= 0, i= 0;
while(addr[i] != '\0')
{
if (addr[i] == ':') ++count;
++ i;
}
return count;
}
bool parse_port(const char *port) {
errno= 0; /* Reset the errno */
m_port= strtol(port, NULL, 10);
if (errno == EINVAL || errno == ERANGE)
{
m_port= 0; /* Error: invalid port */
m_valid= false;
return true;
}
return false;
}
};
class Config_state
{
public:
Config_state() : view_(), status_(wsrep::server_state::s_disconnected)
{}
void set(const wsrep::view& view)
{
wsrep_notify_status(status_, &view);
lock();
view_= view;
unlock();
}
void set(enum wsrep::server_state::state status)
{
if (status == wsrep::server_state::s_donor ||
status == wsrep::server_state::s_synced)
wsrep_notify_status(status, &view_);
else
wsrep_notify_status(status);
lock();
status_= status;
unlock();
}
const wsrep::view& get_view_info() const
{
return view_;
}
enum wsrep::server_state::state get_status() const
{
return status_;
}
int lock()
{
return mysql_mutex_lock(&LOCK_wsrep_config_state);
}
int unlock()
{
return mysql_mutex_unlock(&LOCK_wsrep_config_state);
}
private:
wsrep::view view_;
enum wsrep::server_state::state status_;
};
} /* namespace wsp */
extern wsp::Config_state *wsrep_config_state;
namespace wsp {
/* a class to manage env vars array */
class env
{
private:
size_t len_;
char** env_;
int errno_;
bool ctor_common(char** e);
void dtor();
env& operator =(env);
public:
explicit env(char** env);
explicit env(const env&);
~env();
int append(const char* var); /* add a new env. var */
int error() const { return errno_; }
char** operator()() { return env_; }
};
/* A small class to run external programs. */
class process
{
private:
const char* const str_;
FILE* io_[2];
int err_;
pid_t pid_;
enum io_direction { READ, WRITE };
void setup_parent_pipe_end(io_direction direction,
int pipe_fds[],
int const pipe_end,
const char* const mode);
void close_io(io_direction direction, bool warn = false);
public:
/*! @arg type is a pointer to a null-terminated string which must be
either "r", "w" or "rw"
@arg env optional null-terminated vector of environment variables
*/
process (const char* cmd, const char* type, char** env);
~process ();
FILE* from () { return io_[READ]; }
FILE* to () { return io_[WRITE]; }
void close_to() { close_io(WRITE, false); }
int error() { return err_; }
int wait ();
const char* cmd() { return str_; }
};
class thd
{
/* Helper class to init/deinit current thread for use with THD */
class my_init
{
public:
my_bool const init_;
int const err_;
my_init(my_bool const init) :
init_(init),
err_(init_ ? my_thread_init() : 0)
{}
~my_init() {
if (init_ && !err_) my_thread_end();
}
}
init;
thd (const thd&);
thd& operator= (const thd&);
public:
/*
@param[in] init Should be set to true if called in a freshly forked
thread to initialize MySQL-specific thread context
and likewise deinitialize on object destruction.
Should be set to false if the thread already has
initialized the context, but original THD* is not
available.
*/
explicit thd(my_bool init=true, bool system_thread=false);
~thd();
int err() const { return init.err_; }
THD* const ptr;
};
/* local server connection */
class mysql
{
MYSQL* mysql_;
public:
mysql();
~mysql();
int execute(const std::string& query) {
if (mysql_real_query(mysql_, query.c_str(), query.length())) {
return mysql_errno(mysql_);
}
return 0;
}
int errnum() {
return (mysql_errno(mysql_));
}
const char* errstr() {
return mysql_error(mysql_);
}
int disable_replication();
};
class string
{
public:
string() : string_(0) {}
explicit string(size_t s) : string_(static_cast<char*>(malloc(s))) {}
char* operator()() { return string_; }
void set(char* str) { if (string_) free (string_); string_= str; }
~string() { set (0); }
private:
char* string_;
};
/* scope level lock */
class auto_lock
{
public:
auto_lock(mysql_mutex_t* m) : m_(m) { mysql_mutex_lock(m_); }
~auto_lock() { mysql_mutex_unlock(m_); }
private:
mysql_mutex_t& operator =(mysql_mutex_t&);
mysql_mutex_t* const m_;
};
#ifdef REMOVED
class lock
{
pthread_mutex_t* const mtx_;
public:
lock (pthread_mutex_t* mtx) : mtx_(mtx)
{
int err= pthread_mutex_lock (mtx_);
if (err)
{
WSREP_ERROR("Mutex lock failed: %s", strerror(err));
abort();
}
}
virtual ~lock ()
{
int err= pthread_mutex_unlock (mtx_);
if (err)
{
WSREP_ERROR("Mutex unlock failed: %s", strerror(err));
abort();
}
}
inline void wait (pthread_cond_t* cond)
{
pthread_cond_wait (cond, mtx_);
}
private:
lock (const lock&);
lock& operator=(const lock&);
};
class monitor
{
int mutable refcnt;
pthread_mutex_t mutable mtx;
pthread_cond_t mutable cond;
public:
monitor() : refcnt(0)
{
pthread_mutex_init (&mtx, NULL);
pthread_cond_init (&cond, NULL);
}
~monitor()
{
pthread_mutex_destroy (&mtx);
pthread_cond_destroy (&cond);
}
void enter() const
{
lock l(&mtx);
while (refcnt)
{
l.wait(&cond);
}
refcnt++;
}
void leave() const
{
lock l(&mtx);
refcnt--;
if (refcnt == 0)
{
pthread_cond_signal (&cond);
}
}
private:
monitor (const monitor&);
monitor& operator= (const monitor&);
};
class critical
{
const monitor& mon;
public:
critical(const monitor& m) : mon(m) { mon.enter(); }
~critical() { mon.leave(); }
private:
critical (const critical&);
critical& operator= (const critical&);
};
#endif
} // namespace wsrep
#endif /* WSREP_UTILS_H */
|