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
|
/* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#if defined(__GNUC__) && defined(USE_PRAGMA_IMPLEMENTATION)
#pragma implementation
#endif
#include "options.h"
#include <my_global.h>
#include <my_sys.h>
#include <my_getopt.h>
#include <mysql_com.h>
#include "exit_codes.h"
#include "log.h"
#include "portability.h"
#include "priv.h"
#include "user_management_commands.h"
#define QUOTE2(x) #x
#define QUOTE(x) QUOTE2(x)
#ifdef __WIN__
/* Define holders for default values. */
static char win_dflt_config_file_name[FN_REFLEN];
static char win_dflt_password_file_name[FN_REFLEN];
static char win_dflt_pid_file_name[FN_REFLEN];
static char win_dflt_mysqld_path[FN_REFLEN];
/* Define and initialize Windows-specific options. */
my_bool Options::Service::install_as_service;
my_bool Options::Service::remove_service;
my_bool Options::Service::stand_alone;
const char *Options::Main::config_file= win_dflt_config_file_name;
const char *Options::Main::password_file_name= win_dflt_password_file_name;
const char *Options::Main::pid_file_name= win_dflt_pid_file_name;
const char *Options::Main::default_mysqld_path= win_dflt_mysqld_path;
static int setup_windows_defaults();
#else /* UNIX */
/* Define and initialize UNIX-specific options. */
my_bool Options::Daemon::run_as_service= FALSE;
const char *Options::Daemon::log_file_name= QUOTE(DEFAULT_LOG_FILE_NAME);
const char *Options::Daemon::user= NULL; /* No default value */
const char *Options::Daemon::angel_pid_file_name= NULL;
const char *Options::Main::config_file= QUOTE(DEFAULT_CONFIG_FILE);
const char *
Options::Main::password_file_name= QUOTE(DEFAULT_PASSWORD_FILE_NAME);
const char *Options::Main::pid_file_name= QUOTE(DEFAULT_PID_FILE_NAME);
const char *Options::Main::socket_file_name= QUOTE(DEFAULT_SOCKET_FILE_NAME);
const char *Options::Main::default_mysqld_path= QUOTE(DEFAULT_MYSQLD_PATH);
#endif
/* Remember if the config file was forced. */
bool Options::Main::is_forced_default_file= FALSE;
/* Define and initialize common options. */
const char *Options::Main::bind_address= NULL; /* No default value */
uint Options::Main::monitoring_interval= DEFAULT_MONITORING_INTERVAL;
uint Options::Main::port_number= DEFAULT_PORT;
my_bool Options::Main::mysqld_safe_compatible= FALSE;
const char **Options::default_directories= NULL;
/* Options::User_management */
char *Options::User_management::user_name= NULL;
char *Options::User_management::password= NULL;
User_management_cmd *Options::User_management::cmd= NULL;
/* Private members. */
char **Options::saved_argv= NULL;
#ifndef DBUG_OFF
const char *Options::Debug::config_str= "d:t:i:O,im.trace";
#endif
static const char * const ANGEL_PID_FILE_SUFFIX= ".angel.pid";
static const int ANGEL_PID_FILE_SUFFIX_LEN= (uint) strlen(ANGEL_PID_FILE_SUFFIX);
/*
List of options, accepted by the instance manager.
List must be closed with empty option.
*/
enum options {
OPT_USERNAME= 'u',
OPT_PASSWORD= 'p',
OPT_LOG= 256,
OPT_PID_FILE,
OPT_SOCKET,
OPT_PASSWORD_FILE,
OPT_MYSQLD_PATH,
#ifdef __WIN__
OPT_INSTALL_SERVICE,
OPT_REMOVE_SERVICE,
OPT_STAND_ALONE,
#else
OPT_RUN_AS_SERVICE,
OPT_USER,
OPT_ANGEL_PID_FILE,
#endif
OPT_MONITORING_INTERVAL,
OPT_PORT,
OPT_WAIT_TIMEOUT,
OPT_BIND_ADDRESS,
OPT_PRINT_PASSWORD_LINE,
OPT_ADD_USER,
OPT_DROP_USER,
OPT_EDIT_USER,
OPT_CLEAN_PASSWORD_FILE,
OPT_CHECK_PASSWORD_FILE,
OPT_LIST_USERS,
OPT_MYSQLD_SAFE_COMPATIBLE
};
static struct my_option my_long_options[] =
{
{ "help", '?', "Display this help and exit.",
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "add-user", OPT_ADD_USER,
"Add a user to the password file.",
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0 },
#ifndef __WIN__
{ "angel-pid-file", OPT_ANGEL_PID_FILE, "Pid file for angel process.",
&Options::Daemon::angel_pid_file_name,
&Options::Daemon::angel_pid_file_name,
0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
#endif
{ "bind-address", OPT_BIND_ADDRESS, "Bind address to use for connection.",
&Options::Main::bind_address,
&Options::Main::bind_address,
0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "check-password-file", OPT_CHECK_PASSWORD_FILE,
"Check the password file for consistency.",
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "clean-password-file", OPT_CLEAN_PASSWORD_FILE,
"Clean the password file.",
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0 },
#ifndef DBUG_OFF
{"debug", '#', "Debug log.",
&Options::Debug::config_str,
&Options::Debug::config_str,
0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
#endif
{ "default-mysqld-path", OPT_MYSQLD_PATH, "Where to look for MySQL"
" Server binary.",
&Options::Main::default_mysqld_path,
&Options::Main::default_mysqld_path,
0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0 },
{ "drop-user", OPT_DROP_USER,
"Drop existing user from the password file.",
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "edit-user", OPT_EDIT_USER,
"Edit existing user in the password file.",
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0 },
#ifdef __WIN__
{ "install", OPT_INSTALL_SERVICE, "Install as system service.",
&Options::Service::install_as_service,
&Options::Service::install_as_service,
0, GET_BOOL, NO_ARG, 0, 0, 1, 0, 0, 0 },
#endif
{ "list-users", OPT_LIST_USERS,
"Print out a list of registered users.",
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0 },
#ifndef __WIN__
{ "log", OPT_LOG, "Path to log file. Used only with --run-as-service.",
&Options::Daemon::log_file_name,
&Options::Daemon::log_file_name,
0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
#endif
{ "monitoring-interval", OPT_MONITORING_INTERVAL, "Interval to monitor"
" instances in seconds.",
&Options::Main::monitoring_interval,
&Options::Main::monitoring_interval,
0, GET_UINT, REQUIRED_ARG, DEFAULT_MONITORING_INTERVAL,
0, 0, 0, 0, 0 },
{ "mysqld-safe-compatible", OPT_MYSQLD_SAFE_COMPATIBLE,
"Start Instance Manager in mysqld_safe-compatible manner.",
&Options::Main::mysqld_safe_compatible,
&Options::Main::mysqld_safe_compatible,
0, GET_BOOL, NO_ARG, 0, 0, 1, 0, 0, 0 },
{ "print-password-line", OPT_PRINT_PASSWORD_LINE,
"Print out a user entry as a line for the password file and exit.",
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "password", OPT_PASSWORD, "Password to update the password file.",
&Options::User_management::password,
&Options::User_management::password,
0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "password-file", OPT_PASSWORD_FILE,
"Look for Instance Manager users and passwords here.",
&Options::Main::password_file_name,
&Options::Main::password_file_name,
0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "pid-file", OPT_PID_FILE, "Pid file to use.",
&Options::Main::pid_file_name,
&Options::Main::pid_file_name,
0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "port", OPT_PORT, "Port number to use for connections.",
&Options::Main::port_number,
&Options::Main::port_number,
0, GET_UINT, REQUIRED_ARG, DEFAULT_PORT, 0, 0, 0, 0, 0 },
#ifdef __WIN__
{ "remove", OPT_REMOVE_SERVICE, "Remove system service.",
&Options::Service::remove_service,
&Options::Service::remove_service,
0, GET_BOOL, NO_ARG, 0, 0, 1, 0, 0, 0},
#else
{ "run-as-service", OPT_RUN_AS_SERVICE,
"Daemonize and start angel process.",
&Options::Daemon::run_as_service,
0, 0, GET_BOOL, NO_ARG, 0, 0, 1, 0, 0, 0 },
#endif
#ifndef __WIN__
{ "socket", OPT_SOCKET, "Socket file to use for connection.",
&Options::Main::socket_file_name,
&Options::Main::socket_file_name,
0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
#endif
#ifdef __WIN__
{ "standalone", OPT_STAND_ALONE, "Run the application in stand alone mode.",
&Options::Service::stand_alone,
&Options::Service::stand_alone,
0, GET_BOOL, NO_ARG, 0, 0, 1, 0, 0, 0},
#else
{ "user", OPT_USER, "Username to start mysqlmanager.",
&Options::Daemon::user,
&Options::Daemon::user,
0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
#endif
{ "username", OPT_USERNAME,
"Username to update the password file.",
&Options::User_management::user_name,
&Options::User_management::user_name,
0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "version", 'V', "Output version information and exit.", 0, 0, 0,
GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "wait-timeout", OPT_WAIT_TIMEOUT, "The number of seconds IM waits "
"for activity on a connection before closing it.",
&net_read_timeout,
&net_read_timeout,
0, GET_ULONG, REQUIRED_ARG, NET_WAIT_TIMEOUT, 1, LONG_TIMEOUT, 0, 1, 0 },
{ 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0 }
};
static void version()
{
printf("%s Ver %s for %s on %s\n", my_progname,
(const char *) mysqlmanager_version.str,
SYSTEM_TYPE, MACHINE_TYPE);
}
static const char *default_groups[]= { "manager", 0 };
static void usage()
{
version();
printf("Copyright (C) 2003-2007 MySQL AB, 2009 Sun Microsystems, Inc.\n"
"This software comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
"and you are welcome to modify and redistribute it under the GPL license.\n");
printf("Usage: %s [OPTIONS] \n", my_progname);
my_print_help(my_long_options);
printf("\nThe following options may be given as the first argument:\n"
"--print-defaults Print the program argument list and exit.\n"
"--defaults-file=# Only read manager configuration and instance\n"
" settings from the given file #. The same file\n"
" will be used to modify configuration of instances\n"
" with SET commands.\n");
my_print_variables(my_long_options);
}
C_MODE_START
static my_bool
get_one_option(int optid,
const struct my_option *opt __attribute__((unused)),
char *argument)
{
switch(optid) {
case 'V':
version();
exit(0);
case OPT_PRINT_PASSWORD_LINE:
case OPT_ADD_USER:
case OPT_DROP_USER:
case OPT_EDIT_USER:
case OPT_CLEAN_PASSWORD_FILE:
case OPT_CHECK_PASSWORD_FILE:
case OPT_LIST_USERS:
if (Options::User_management::cmd)
{
fprintf(stderr, "Error: only one password-management command "
"can be specified at a time.\n");
exit(ERR_INVALID_USAGE);
}
switch (optid) {
case OPT_PRINT_PASSWORD_LINE:
Options::User_management::cmd= new Print_password_line_cmd();
break;
case OPT_ADD_USER:
Options::User_management::cmd= new Add_user_cmd();
break;
case OPT_DROP_USER:
Options::User_management::cmd= new Drop_user_cmd();
break;
case OPT_EDIT_USER:
Options::User_management::cmd= new Edit_user_cmd();
break;
case OPT_CLEAN_PASSWORD_FILE:
Options::User_management::cmd= new Clean_db_cmd();
break;
case OPT_CHECK_PASSWORD_FILE:
Options::User_management::cmd= new Check_db_cmd();
break;
case OPT_LIST_USERS:
Options::User_management::cmd= new List_users_cmd();
break;
}
break;
case '?':
usage();
exit(0);
case '#':
#ifndef DBUG_OFF
DBUG_SET(argument ? argument : Options::Debug::config_str);
DBUG_SET_INITIAL(argument ? argument : Options::Debug::config_str);
#endif
break;
}
return 0;
}
C_MODE_END
/*
- Process argv of original program: get tid of --defaults-extra-file
and print a message if met there.
- call load_defaults to load configuration file section and save the pointer
for free_defaults.
- call handle_options to assign defaults and command-line arguments
to the class members.
if either of these function fail, return the error code.
*/
int Options::load(int argc, char **argv)
{
if (argc >= 2)
{
if (is_prefix(argv[1], "--defaults-file="))
{
Main::config_file= strchr(argv[1], '=') + 1;
Main::is_forced_default_file= TRUE;
}
if (is_prefix(argv[1], "--defaults-extra-file=") ||
is_prefix(argv[1], "--no-defaults"))
{
/* the log is not enabled yet */
fprintf(stderr, "The --defaults-extra-file and --no-defaults options"
" are not supported by\n"
"Instance Manager. Program aborted.\n");
return ERR_INVALID_USAGE;
}
}
#ifdef __WIN__
if (setup_windows_defaults())
{
fprintf(stderr, "Internal error: could not setup default values.\n");
return ERR_OUT_OF_MEMORY;
}
#endif
/* load_defaults will reset saved_argv with a new allocated list */
saved_argv= argv;
/* config-file options are prepended to command-line ones */
log_info("Loading config file '%s'...",
(const char *) Main::config_file);
my_load_defaults(Main::config_file, default_groups, &argc,
&saved_argv, &default_directories);
if ((handle_options(&argc, &saved_argv, my_long_options, get_one_option)))
return ERR_INVALID_USAGE;
if (!User_management::cmd &&
(User_management::user_name || User_management::password))
{
fprintf(stderr,
"--username and/or --password options have been specified, "
"but no password-management command has been given.\n");
return ERR_INVALID_USAGE;
}
#ifndef __WIN__
if (Options::Daemon::run_as_service)
{
if (Options::Daemon::angel_pid_file_name == NULL)
{
/*
Calculate angel pid file on the IM pid file basis: replace the
extension (everything after the last dot) of the pid file basename to
'.angel.pid'.
*/
char *local_angel_pid_file_name;
char *base_name_ptr;
char *ext_ptr;
local_angel_pid_file_name=
(char *) malloc(strlen(Options::Main::pid_file_name) +
ANGEL_PID_FILE_SUFFIX_LEN);
strcpy(local_angel_pid_file_name, Options::Main::pid_file_name);
base_name_ptr= strrchr(local_angel_pid_file_name, '/');
if (!base_name_ptr)
base_name_ptr= local_angel_pid_file_name + 1;
ext_ptr= strrchr(base_name_ptr, '.');
if (ext_ptr)
*ext_ptr= 0;
strcat(local_angel_pid_file_name, ANGEL_PID_FILE_SUFFIX);
Options::Daemon::angel_pid_file_name= local_angel_pid_file_name;
}
else
{
Options::Daemon::angel_pid_file_name=
strdup(Options::Daemon::angel_pid_file_name);
}
}
#endif
return 0;
}
void Options::cleanup()
{
if (saved_argv)
free_defaults(saved_argv);
delete User_management::cmd;
#ifndef __WIN__
if (Options::Daemon::run_as_service)
free((void *) Options::Daemon::angel_pid_file_name);
#endif
}
#ifdef __WIN__
static int setup_windows_defaults()
{
char module_full_name[FN_REFLEN];
char dir_name[FN_REFLEN];
char base_name[FN_REFLEN];
char im_name[FN_REFLEN];
char *base_name_ptr;
char *ptr;
/* Determine dirname and basename. */
if (!GetModuleFileName(NULL, module_full_name, sizeof (module_full_name)) ||
!GetFullPathName(module_full_name, sizeof (dir_name), dir_name,
&base_name_ptr))
{
return 1;
}
strmake(base_name, base_name_ptr, FN_REFLEN - 1);
*base_name_ptr= 0;
strmake(im_name, base_name, FN_REFLEN - 1);
ptr= strrchr(im_name, '.');
if (!ptr)
return 1;
*ptr= 0;
/* Initialize the defaults. */
strxmov(win_dflt_config_file_name, dir_name, DFLT_CONFIG_FILE_NAME, NullS);
strxmov(win_dflt_mysqld_path, dir_name, DFLT_MYSQLD_PATH, NullS);
strxmov(win_dflt_password_file_name, dir_name, im_name, DFLT_PASSWD_FILE_EXT,
NullS);
strxmov(win_dflt_pid_file_name, dir_name, im_name, DFLT_PID_FILE_EXT, NullS);
return 0;
}
#endif
|