1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 MySQL
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
/* maintaince of drizzle databases */
23
#include "client_priv.h"
25
#include <mysys/my_pthread.h> /* because of signal() */
31
/* Added this for string translation. */
32
#include <drizzled/gettext.h>
34
#define ADMIN_VERSION "8.42"
35
#define SHUTDOWN_DEF_TIMEOUT 3600 /* Wait for shutdown */
36
#define NUM_COMMAND_NAMES 2
38
char *host= NULL, *user= NULL, *opt_password= NULL;
39
static bool interrupted= false, opt_verbose= false,tty_password= false;
40
static uint32_t tcp_port= 0, option_wait= 0, option_silent= 0;
41
static uint32_t my_end_arg;
42
static uint32_t opt_connect_timeout, opt_shutdown_timeout;
49
static void usage(void);
50
static void print_version(void);
51
extern "C" void endprog(int signal_number);
52
extern "C" bool get_one_option(int optid, const struct my_option *opt,
54
static int execute_commands(DRIZZLE *drizzle,int argc, char **argv);
55
static bool sql_connect(DRIZZLE *drizzle, uint32_t wait);
58
The order of commands must be the same as command_names,
67
static string command_names[]= {
73
command_vector(command_names, command_names + NUM_COMMAND_NAMES);
75
static struct my_option my_long_options[] =
77
{"help", '?', N_("Display this help and exit."), 0, 0, 0, GET_NO_ARG,
78
NO_ARG, 0, 0, 0, 0, 0, 0},
79
{"host", 'h', N_("Connect to host."), (char**) &host, (char**) &host, 0, GET_STR,
80
REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
82
N_("Password to use when connecting to server. If password is not given it's asked from the tty."),
83
0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
84
{"port", 'p', N_("Port number to use for connection or 0 for default to, in "
85
"order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, "
86
"built-in default (" STRINGIFY_ARG(DRIZZLE_PORT) ")."),
87
0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
88
{"silent", 's', N_("Silently exit if one can't connect to server."),
89
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
90
#ifndef DONT_ALLOW_USER_CHANGE
91
{"user", 'u', N_("User for login if not current user."), (char**) &user,
92
(char**) &user, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
94
{"verbose", 'v', N_("Write more information."), (char**) &opt_verbose,
95
(char**) &opt_verbose, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
96
{"version", 'V', N_("Output version information and exit."), 0, 0, 0, GET_NO_ARG,
97
NO_ARG, 0, 0, 0, 0, 0, 0},
98
{"wait", 'w', N_("Wait and retry if connection is down."), 0, 0, 0, GET_UINT,
99
OPT_ARG, 0, 0, 0, 0, 0, 0},
100
{"connect_timeout", OPT_CONNECT_TIMEOUT, "", (char**) &opt_connect_timeout,
101
(char**) &opt_connect_timeout, 0, GET_UINT32, REQUIRED_ARG, 3600*12, 0,
103
{"shutdown_timeout", OPT_SHUTDOWN_TIMEOUT, "", (char**) &opt_shutdown_timeout,
104
(char**) &opt_shutdown_timeout, 0, GET_UINT32, REQUIRED_ARG,
105
SHUTDOWN_DEF_TIMEOUT, 0, 3600*12, 0, 1, 0},
106
{ 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
110
static const char *load_default_groups[]= { "drizzleadmin","client",0 };
112
inline string lower_string(const char * from_string)
114
string to_string= from_string;
115
transform(to_string.begin(), to_string.end(),
116
to_string.begin(), ::tolower);
121
* Searches for the given command and determines
124
* Returns the type of the command corresponding
125
* to the commands enum defined in drizzleadmin.cc
126
* If the command is not supported, return ADMIN_ERROR.
128
* @param command name to search for
130
static int get_command_type(const char *name)
132
int type= ADMIN_ERROR;
133
string comp_string= lower_string(name);
134
vector<string>::iterator it=
135
std::find(command_vector.begin(), command_vector.end(),
137
if (it != command_vector.end())
139
/* add 1 due to the way the commands ENUM is defined */
140
type= distance(command_vector.begin(), it) + 1;
146
get_one_option(int optid, const struct my_option *, char *argument)
149
uint64_t temp_drizzle_port= 0;
154
temp_drizzle_port= (uint64_t) strtoul(argument, &endchar, 10);
155
/* if there is an alpha character this is not a valid port */
156
if (strlen(endchar) != 0)
158
fprintf(stderr, _("Non-integer value supplied for port. If you are trying to enter a password please use --password instead.\n"));
161
/* If the port number is > 65535 it is not a valid port
162
This also helps with potential data loss casting unsigned long to a
164
if ((temp_drizzle_port == 0) || (temp_drizzle_port > 65535))
166
fprintf(stderr, _("Value supplied for port is not valid.\n"));
171
tcp_port= (uint32_t) temp_drizzle_port;
177
char *start= argument;
181
opt_password= strdup(argument);
182
if (opt_password == NULL)
184
fprintf(stderr, _("Memory allocation error while copying password. "
190
/* Overwriting password with 'x' */
195
/* Cut length of argument */
212
if ((option_wait=atoi(argument)) <= 0)
216
option_wait= ~(uint32_t)0;
232
int main(int argc,char *argv[])
234
int error= 0, ho_error;
236
char **commands, **save_argv;
239
drizzleclient_create(&drizzle);
240
load_defaults("drizzle",load_default_groups,&argc,&argv);
241
save_argv= argv; /* Save for free_defaults */
242
if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
244
free_defaults(save_argv);
256
opt_password = drizzleclient_get_tty_password(NULL);
258
signal(SIGINT,endprog); /* Here if abort */
259
signal(SIGTERM,endprog); /* Here if abort */
261
if (opt_connect_timeout)
263
uint32_t tmp= opt_connect_timeout;
264
drizzleclient_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT, (char*) &tmp);
267
if (sql_connect(&drizzle, option_wait))
269
unsigned int err= drizzleclient_errno(&drizzle);
270
if (err >= CR_MIN_ERROR && err <= CR_MAX_ERROR)
274
/* Return 0 if all commands are PING */
275
for (; argc > 0; argv++, argc--)
277
int type= get_command_type(argv[0]);
278
if (type != ADMIN_PING)
288
error= execute_commands(&drizzle,argc,commands);
289
drizzleclient_close(&drizzle);
293
free_defaults(save_argv);
295
return error ? 1 : 0;
303
static bool sql_connect(DRIZZLE *drizzle, uint32_t wait)
309
if (drizzleclient_connect(drizzle,host,user,opt_password,NULL,tcp_port,NULL,0))
311
drizzle->reconnect= 1;
315
(void) fflush(stderr);
325
host= (char*) LOCAL_HOST;
327
fprintf(stderr,_("connect to server at '%s' failed\nerror: '%s'"),
328
host, drizzleclient_error(drizzle));
330
if (drizzleclient_errno(drizzle) == CR_CONN_HOST_ERROR ||
331
drizzleclient_errno(drizzle) == CR_UNKNOWN_HOST)
333
fprintf(stderr,_("Check that drizzled is running on %s"),host);
334
fprintf(stderr,_(" and that the port is %d.\n"),
335
tcp_port ? tcp_port: drizzleclient_get_default_port());
336
fprintf(stderr,_("You can check this by doing 'telnet %s %d'\n"),
337
host, tcp_port ? tcp_port: drizzleclient_get_default_port());
342
if (wait != UINT32_MAX)
343
wait--; /* One less retry */
344
if ((drizzleclient_errno(drizzle) != CR_CONN_HOST_ERROR) &&
345
(drizzleclient_errno(drizzle) != CR_CONNECTION_ERROR))
347
fprintf(stderr,_("Got error: %s\n"), drizzleclient_error(drizzle));
349
else if (!option_silent)
354
fputs(_("Waiting for Drizzle server to answer"),stderr);
355
(void) fflush(stderr);
360
(void) fflush(stderr);
370
-1 on retryable error
373
static int execute_commands(DRIZZLE *drizzle,int argc, char **argv)
377
DRIZZLE documentation relies on the fact that drizzleadmin will
378
execute commands in the order specified.
379
If this behaviour is ever changed, Docs should be notified.
381
for (; argc > 0 ; argv++,argc--)
383
int type= get_command_type(argv[0]);
388
printf(_("shutting down drizzled...\n"));
390
if (drizzleclient_shutdown(drizzle))
392
fprintf(stderr, _("shutdown failed; error: '%s'"),
393
drizzleclient_error(drizzle));
396
drizzleclient_close(drizzle); /* Close connection to avoid error messages */
401
argc= 1; /* Force SHUTDOWN to be the last command */
405
drizzle->reconnect= 0; /* We want to know of reconnects */
406
if (!drizzleclient_ping(drizzle))
408
if (option_silent < 2)
409
puts(_("drizzled is alive"));
413
if (drizzleclient_errno(drizzle) == CR_SERVER_GONE_ERROR)
415
drizzle->reconnect= 1;
416
if (!drizzleclient_ping(drizzle))
417
puts(_("connection was down, but drizzled is now alive"));
421
fprintf(stderr,_("drizzled doesn't answer to ping, error: '%s'"),
422
drizzleclient_error(drizzle));
426
drizzle->reconnect= 1; /* Automatic reconnect is default */
430
fprintf(stderr, _("Unknown command: '%-.60s'"), argv[0]);
437
static void print_version(void)
439
printf(_("%s Ver %s Distrib %s, for %s on %s\n"),my_progname,ADMIN_VERSION,
440
drizzleclient_get_client_info(),SYSTEM_TYPE,MACHINE_TYPE);
443
static void usage(void)
446
puts(_("Copyright (C) 2000-2008 MySQL AB"));
447
puts(_("This software comes with ABSOLUTELY NO WARRANTY. This is free software,\nand you are welcome to modify and redistribute it under the GPL license\n"));
448
puts(_("Administration program for the drizzled daemon."));
449
printf(_("Usage: %s [OPTIONS] command command....\n"), my_progname);
450
my_print_help(my_long_options);
452
ping Check if server is down\n\
453
shutdown Take server down\n"));