~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzleadmin.cc

  • Committer: Brian Aker
  • Date: 2008-10-06 06:47:29 UTC
  • Revision ID: brian@tangent.org-20081006064729-2i9mhjkzyvow9xsm
Remove uint.

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