~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzleadmin.cc

  • Committer: Monty Taylor
  • Date: 2008-12-17 04:54:32 UTC
  • mto: (685.1.38 devel) (713.1.1 devel)
  • mto: This revision was merged to the branch mainline in revision 713.
  • Revision ID: monty@inaugust.com-20081217045432-dw4425razy7do46i
Fixed bool. No more sql_mode.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 MySQL
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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
 
19
 */
 
20
 
 
21
/* maintaince of drizzle databases */
 
22
 
 
23
#include "client_priv.h"
 
24
#include <signal.h>
 
25
#include <mysys/my_pthread.h>                           /* because of signal()  */
 
26
#include <sys/stat.h>
 
27
 
 
28
/* Added this for string translation. */
 
29
#include <drizzled/gettext.h>
 
30
 
 
31
#define ADMIN_VERSION "8.42"
 
32
#define SHUTDOWN_DEF_TIMEOUT 3600               /* Wait for shutdown */
 
33
 
 
34
char *host= NULL, *user= NULL, *opt_password= NULL;
 
35
static bool interrupted= false, opt_verbose= false,tty_password= false;
 
36
static uint32_t tcp_port= 0, option_wait= 0, option_silent= 0;
 
37
static uint32_t my_end_arg;
 
38
static uint32_t opt_connect_timeout, opt_shutdown_timeout;
 
39
static myf error_flags; /* flags to pass to my_printf_error, like ME_BELL */
 
40
 
 
41
using namespace std;
 
42
 
 
43
/*
 
44
  Forward declarations
 
45
*/
 
46
static void usage(void);
 
47
static void print_version(void);
 
48
extern "C" RETSIGTYPE endprog(int signal_number);
 
49
extern "C" bool get_one_option(int optid, const struct my_option *opt,
 
50
                               char *argument);
 
51
static int execute_commands(DRIZZLE *drizzle,int argc, char **argv);
 
52
static bool sql_connect(DRIZZLE *drizzle, uint wait);
 
53
 
 
54
/*
 
55
  The order of commands must be the same as command_names,
 
56
  except ADMIN_ERROR
 
57
*/
 
58
enum commands {
 
59
  ADMIN_ERROR,
 
60
  ADMIN_SHUTDOWN,
 
61
  ADMIN_PING
 
62
};
 
63
 
 
64
static const char *command_names[]= {
 
65
  "shutdown",
 
66
  "ping",
 
67
  NULL
 
68
};
 
69
 
 
70
static TYPELIB command_typelib=
 
71
{ array_elements(command_names)-1,"commands", command_names, NULL };
 
72
 
 
73
static struct my_option my_long_options[] =
 
74
{
 
75
  {"help", '?', N_("Display this help and exit."), 0, 0, 0, GET_NO_ARG,
 
76
   NO_ARG, 0, 0, 0, 0, 0, 0},
 
77
  {"host", 'h', N_("Connect to host."), (char**) &host, (char**) &host, 0, GET_STR,
 
78
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
79
  {"password", 'p',
 
80
   N_("Password to use when connecting to server. If password is not given it's asked from the tty."),
 
81
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
82
  {"port", 'P', N_("Port number to use for connection or 0 for default to, in "
 
83
   "order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, "
 
84
   "built-in default (" STRINGIFY_ARG(DRIZZLE_PORT) ")."),
 
85
   (char**) &tcp_port,
 
86
   (char**) &tcp_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
87
  {"silent", 's', N_("Silently exit if one can't connect to server."),
 
88
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
89
#ifndef DONT_ALLOW_USER_CHANGE
 
90
  {"user", 'u', N_("User for login if not current user."), (char**) &user,
 
91
   (char**) &user, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
92
#endif
 
93
  {"verbose", 'v', N_("Write more information."), (char**) &opt_verbose,
 
94
   (char**) &opt_verbose, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
95
  {"version", 'V', N_("Output version information and exit."), 0, 0, 0, GET_NO_ARG,
 
96
   NO_ARG, 0, 0, 0, 0, 0, 0},
 
97
  {"wait", 'w', N_("Wait and retry if connection is down."), 0, 0, 0, GET_UINT,
 
98
   OPT_ARG, 0, 0, 0, 0, 0, 0},
 
99
  {"connect_timeout", OPT_CONNECT_TIMEOUT, "", (char**) &opt_connect_timeout,
 
100
   (char**) &opt_connect_timeout, 0, GET_ULONG, REQUIRED_ARG, 3600*12, 0,
 
101
   3600*12, 0, 1, 0},
 
102
  {"shutdown_timeout", OPT_SHUTDOWN_TIMEOUT, "", (char**) &opt_shutdown_timeout,
 
103
   (char**) &opt_shutdown_timeout, 0, GET_ULONG, REQUIRED_ARG,
 
104
   SHUTDOWN_DEF_TIMEOUT, 0, 3600*12, 0, 1, 0},
 
105
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
 
106
};
 
107
 
 
108
 
 
109
static const char *load_default_groups[]= { "drizzleadmin","client",0 };
 
110
 
 
111
bool
 
112
get_one_option(int optid, const struct my_option *, char *argument)
 
113
{
 
114
  int error = 0;
 
115
 
 
116
  switch(optid) {
 
117
  case 'p':
 
118
    if (argument)
 
119
    {
 
120
      char *start=argument;
 
121
      if (opt_password)
 
122
        free(opt_password);
 
123
 
 
124
      opt_password= strdup(argument);
 
125
      if (opt_password == NULL)
 
126
      {
 
127
        fprintf(stderr, _("Memory allocation error while copying password. "
 
128
                          "Aborting.\n"));
 
129
        exit(ENOMEM);
 
130
      }
 
131
      while (*argument) *argument++= 'x';   /* Destroy argument */
 
132
      if (*start)
 
133
        start[1]=0; /* Cut length of argument */
 
134
      tty_password= 0;
 
135
    }
 
136
    else
 
137
      tty_password= 1;
 
138
    break;
 
139
  case 's':
 
140
    option_silent++;
 
141
    break;
 
142
  case 'V':
 
143
    print_version();
 
144
    exit(0);
 
145
  case 'w':
 
146
    if (argument)
 
147
    {
 
148
      if ((option_wait=atoi(argument)) <= 0)
 
149
        option_wait=1;
 
150
    }
 
151
    else
 
152
      option_wait= ~(uint)0;
 
153
    break;
 
154
  case '?':
 
155
  case 'I':                                     /* Info */
 
156
    error++;
 
157
    break;
 
158
  }
 
159
 
 
160
  if (error)
 
161
  {
 
162
    usage();
 
163
    exit(1);
 
164
  }
 
165
  return 0;
 
166
}
 
167
 
 
168
int main(int argc,char *argv[])
 
169
{
 
170
  int error= 0, ho_error;
 
171
  DRIZZLE drizzle;
 
172
  char **commands, **save_argv;
 
173
 
 
174
  MY_INIT(argv[0]);
 
175
  drizzle_create(&drizzle);
 
176
  load_defaults("drizzle",load_default_groups,&argc,&argv);
 
177
  save_argv = argv;                             /* Save for free_defaults */
 
178
  if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
 
179
  {
 
180
    free_defaults(save_argv);
 
181
    exit(ho_error);
 
182
  }
 
183
 
 
184
  if (argc == 0)
 
185
  {
 
186
    usage();
 
187
    exit(1);
 
188
  }
 
189
 
 
190
  commands = argv;
 
191
  if (tty_password)
 
192
    opt_password = get_tty_password(NULL);
 
193
 
 
194
  signal(SIGINT,endprog);                       /* Here if abort */
 
195
  signal(SIGTERM,endprog);              /* Here if abort */
 
196
 
 
197
  if (opt_connect_timeout)
 
198
  {
 
199
    uint tmp=opt_connect_timeout;
 
200
    drizzle_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT, (char*) &tmp);
 
201
  }
 
202
 
 
203
  error_flags= (myf)0;
 
204
 
 
205
  if (sql_connect(&drizzle, option_wait))
 
206
  {
 
207
    unsigned int err= drizzle_errno(&drizzle);
 
208
    if (err >= CR_MIN_ERROR && err <= CR_MAX_ERROR)
 
209
      error= 1;
 
210
    else
 
211
    {
 
212
      /* Return 0 if all commands are PING */
 
213
      for (; argc > 0; argv++, argc--)
 
214
      {
 
215
        if (find_type(argv[0], &command_typelib, 2) != ADMIN_PING)
 
216
        {
 
217
          error= 1;
 
218
          break;
 
219
        }
 
220
      }
 
221
    }
 
222
  }
 
223
  else
 
224
  {
 
225
    error=execute_commands(&drizzle,argc,commands);
 
226
    drizzle_close(&drizzle);
 
227
  }
 
228
  free(opt_password);
 
229
  free(user);
 
230
  free_defaults(save_argv);
 
231
  my_end(my_end_arg);
 
232
  return error ? 1 : 0;
 
233
}
 
234
 
 
235
RETSIGTYPE endprog(int)
 
236
{
 
237
  interrupted=1;
 
238
}
 
239
 
 
240
static bool sql_connect(DRIZZLE *drizzle, uint wait)
 
241
{
 
242
  bool info=0;
 
243
 
 
244
  for (;;)
 
245
  {
 
246
    if (drizzle_connect(drizzle,host,user,opt_password,NULL,tcp_port,NULL,0))
 
247
    {
 
248
      drizzle->reconnect= 1;
 
249
      if (info)
 
250
      {
 
251
        fputs("\n",stderr);
 
252
        (void) fflush(stderr);
 
253
      }
 
254
      return 0;
 
255
    }
 
256
 
 
257
    if (!wait)
 
258
    {
 
259
      if (!option_silent)
 
260
      {
 
261
        if (!host)
 
262
          host= (char*) LOCAL_HOST;
 
263
 
 
264
        my_printf_error(0,_("connect to server at '%s' failed\nerror: '%s'"),
 
265
        error_flags, host, drizzle_error(drizzle));
 
266
 
 
267
        if (drizzle_errno(drizzle) == CR_CONN_HOST_ERROR ||
 
268
          drizzle_errno(drizzle) == CR_UNKNOWN_HOST)
 
269
        {
 
270
          fprintf(stderr,_("Check that drizzled is running on %s"),host);
 
271
          fprintf(stderr,_(" and that the port is %d.\n"),
 
272
          tcp_port ? tcp_port: drizzle_get_default_port());
 
273
          fprintf(stderr,_("You can check this by doing 'telnet %s %d'\n"),
 
274
                  host, tcp_port ? tcp_port: drizzle_get_default_port());
 
275
        }
 
276
      }
 
277
      return 1;
 
278
    }
 
279
    if (wait != UINT32_MAX)
 
280
      wait--;                           /* One less retry */
 
281
    if ((drizzle_errno(drizzle) != CR_CONN_HOST_ERROR) &&
 
282
        (drizzle_errno(drizzle) != CR_CONNECTION_ERROR))
 
283
    {
 
284
      fprintf(stderr,_("Got error: %s\n"), drizzle_error(drizzle));
 
285
    }
 
286
    else if (!option_silent)
 
287
    {
 
288
      if (!info)
 
289
      {
 
290
        info=1;
 
291
        fputs(_("Waiting for Drizzle server to answer"),stderr);
 
292
        (void) fflush(stderr);
 
293
      }
 
294
      else
 
295
      {
 
296
        putc('.',stderr);
 
297
        (void) fflush(stderr);
 
298
      }
 
299
    }
 
300
    sleep(5);
 
301
  }
 
302
}
 
303
 
 
304
/*
 
305
  Execute a command.
 
306
  Return 0 on ok
 
307
         -1 on retryable error
 
308
         1 on fatal error
 
309
*/
 
310
static int execute_commands(DRIZZLE *drizzle,int argc, char **argv)
 
311
{
 
312
 
 
313
  /*
 
314
    DRIZZLE documentation relies on the fact that drizzleadmin will
 
315
    execute commands in the order specified.
 
316
    If this behaviour is ever changed, Docs should be notified.
 
317
  */
 
318
  for (; argc > 0 ; argv++,argc--)
 
319
  {
 
320
    switch (find_type(argv[0],&command_typelib,2)) {
 
321
    case ADMIN_SHUTDOWN:
 
322
    {
 
323
      if (opt_verbose)
 
324
        printf(_("shutting down drizzled...\n"));
 
325
 
 
326
      if (drizzle_shutdown(drizzle))
 
327
      {
 
328
        my_printf_error(0, _("shutdown failed; error: '%s'"), error_flags,
 
329
                        drizzle_error(drizzle));
 
330
        return -1;
 
331
      }
 
332
      drizzle_close(drizzle);   /* Close connection to avoid error messages */
 
333
 
 
334
      if (opt_verbose)
 
335
        printf(_("done\n"));
 
336
 
 
337
      argc=1;             /* Force SHUTDOWN to be the last command */
 
338
      break;
 
339
    }
 
340
    case ADMIN_PING:
 
341
      drizzle->reconnect=0;     /* We want to know of reconnects */
 
342
      if (!drizzle_ping(drizzle))
 
343
      {
 
344
        if (option_silent < 2)
 
345
          puts(_("drizzled is alive"));
 
346
      }
 
347
      else
 
348
      {
 
349
        if (drizzle_errno(drizzle) == CR_SERVER_GONE_ERROR)
 
350
        {
 
351
          drizzle->reconnect=1;
 
352
          if (!drizzle_ping(drizzle))
 
353
            puts(_("connection was down, but drizzled is now alive"));
 
354
        }
 
355
        else
 
356
              {
 
357
          my_printf_error(0,_("drizzled doesn't answer to ping, error: '%s'"),
 
358
          error_flags, drizzle_error(drizzle));
 
359
          return -1;
 
360
        }
 
361
      }
 
362
      drizzle->reconnect=1;     /* Automatic reconnect is default */
 
363
      break;
 
364
 
 
365
    default:
 
366
      my_printf_error(0, _("Unknown command: '%-.60s'"), error_flags, argv[0]);
 
367
      return 1;
 
368
    }
 
369
  }
 
370
  return 0;
 
371
}
 
372
 
 
373
static void print_version(void)
 
374
{
 
375
  printf(_("%s  Ver %s Distrib %s, for %s on %s\n"),my_progname,ADMIN_VERSION,
 
376
         drizzle_get_client_info(),SYSTEM_TYPE,MACHINE_TYPE);
 
377
}
 
378
 
 
379
static void usage(void)
 
380
{
 
381
  print_version();
 
382
  puts(_("Copyright (C) 2000-2008 MySQL AB"));
 
383
  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"));
 
384
  puts(_("Administration program for the drizzled daemon."));
 
385
  printf(_("Usage: %s [OPTIONS] command command....\n"), my_progname);
 
386
  my_print_help(my_long_options);
 
387
  puts(_("\
 
388
  ping         Check if server is down\n\
 
389
  shutdown     Take server down\n"));
 
390
}