~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzleadmin.cc

  • Committer: Monty Taylor
  • Date: 2008-08-02 01:03:15 UTC
  • mto: (236.1.42 codestyle)
  • mto: This revision was merged to the branch mainline in revision 261.
  • Revision ID: monty@inaugust.com-20080802010315-65h5938pymg9d99z
Moved m4 macros to top-level m4 dir, per GNU standards (and where gettext wanted it :)

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
 
 */
 
1
/* Copyright (C) 2008 Drizzle Open Source Project 
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
20
15
 
21
16
/* maintaince of drizzle databases */
22
17
 
25
20
#include <mysys/my_pthread.h>                           /* because of signal()  */
26
21
#include <sys/stat.h>
27
22
 
28
 
/* Added this for string translation. */
29
 
#include <drizzled/gettext.h>
30
 
 
31
23
#define ADMIN_VERSION "8.42"
32
24
#define SHUTDOWN_DEF_TIMEOUT 3600               /* Wait for shutdown */
33
25
 
34
26
char *host= NULL, *user= NULL, *opt_password= NULL;
35
 
static bool interrupted= false, opt_verbose= false,tty_password= false;
 
27
static bool interrupted= false, opt_verbose= false,tty_password= false; 
 
28
static uint8_t opt_protocol= DRIZZLE_PROTOCOL_TCP;  
36
29
static uint32_t tcp_port= 0, option_wait= 0, option_silent= 0;
37
30
static uint32_t my_end_arg;
38
31
static uint32_t opt_connect_timeout, opt_shutdown_timeout;
39
32
static myf error_flags; /* flags to pass to my_printf_error, like ME_BELL */
40
33
 
41
 
using namespace std;
42
 
 
43
34
/*
44
 
  Forward declarations
 
35
  Forward declarations 
45
36
*/
46
37
static void usage(void);
47
38
static void print_version(void);
48
 
extern "C" void endprog(int signal_number);
 
39
extern "C" sig_handler endprog(int signal_number);
49
40
extern "C" bool get_one_option(int optid, const struct my_option *opt,
50
41
                               char *argument);
51
42
static int execute_commands(DRIZZLE *drizzle,int argc, char **argv);
64
55
static const char *command_names[]= {
65
56
  "shutdown",
66
57
  "ping",
67
 
  NULL
 
58
  NullS
68
59
};
69
60
 
70
61
static TYPELIB command_typelib=
72
63
 
73
64
static struct my_option my_long_options[] =
74
65
{
75
 
  {"help", '?', N_("Display this help and exit."), 0, 0, 0, GET_NO_ARG,
 
66
  {"help", '?', "Display this help and exit.", 0, 0, 0, GET_NO_ARG,
76
67
   NO_ARG, 0, 0, 0, 0, 0, 0},
77
 
  {"host", 'h', N_("Connect to host."), (char**) &host, (char**) &host, 0, GET_STR,
 
68
  {"host", 'h', "Connect to host.", (char**) &host, (char**) &host, 0, GET_STR,
78
69
   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."),
 
70
  {"password", 'p',
 
71
   "Password to use when connecting to server. If password is not given it's asked from the tty.",
81
72
   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
 
   0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
86
 
  {"silent", 's', N_("Silently exit if one can't connect to server."),
 
73
  {"port", 'P', "Port number to use for connection or 0 for default to, in "
 
74
   "order of preference, my.cnf, $MYSQL_TCP_PORT, "
 
75
#if MYSQL_PORT_DEFAULT == 0
 
76
   "/etc/services, "
 
77
#endif
 
78
   "built-in default (" STRINGIFY_ARG(MYSQL_PORT) ").",
 
79
   (char**) &tcp_port,
 
80
   (char**) &tcp_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
81
  {"silent", 's', "Silently exit if one can't connect to server.",
87
82
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
88
83
#ifndef DONT_ALLOW_USER_CHANGE
89
 
  {"user", 'u', N_("User for login if not current user."), (char**) &user,
 
84
  {"user", 'u', "User for login if not current user.", (char**) &user,
90
85
   (char**) &user, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
91
86
#endif
92
 
  {"verbose", 'v', N_("Write more information."), (char**) &opt_verbose,
 
87
  {"verbose", 'v', "Write more information.", (char**) &opt_verbose,
93
88
   (char**) &opt_verbose, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
94
 
  {"version", 'V', N_("Output version information and exit."), 0, 0, 0, GET_NO_ARG,
 
89
  {"version", 'V', "Output version information and exit.", 0, 0, 0, GET_NO_ARG,
95
90
   NO_ARG, 0, 0, 0, 0, 0, 0},
96
 
  {"wait", 'w', N_("Wait and retry if connection is down."), 0, 0, 0, GET_UINT,
 
91
  {"wait", 'w', "Wait and retry if connection is down.", 0, 0, 0, GET_UINT,
97
92
   OPT_ARG, 0, 0, 0, 0, 0, 0},
98
93
  {"connect_timeout", OPT_CONNECT_TIMEOUT, "", (char**) &opt_connect_timeout,
99
94
   (char**) &opt_connect_timeout, 0, GET_ULONG, REQUIRED_ARG, 3600*12, 0,
108
103
static const char *load_default_groups[]= { "drizzleadmin","client",0 };
109
104
 
110
105
bool
111
 
get_one_option(int optid, const struct my_option *, char *argument)
 
106
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
 
107
               char *argument)
112
108
{
113
 
  char *endchar= NULL;
114
 
  uint64_t temp_drizzle_port= 0;
115
109
  int error = 0;
116
110
 
117
111
  switch(optid) {
118
112
  case 'p':
119
 
    temp_drizzle_port= (uint64_t) strtoul(argument, &endchar, 10);
120
 
    /* if there is an alpha character this is not a valid port */
121
 
    if (strlen(endchar) != 0)
122
 
    {
123
 
      fprintf(stderr, _("Non-integer value supplied for port.  If you are trying to enter a password please use --password instead.\n"));
124
 
      exit(1);
125
 
    }
126
 
    /* If the port number is > 65535 it is not a valid port
127
 
       This also helps with potential data loss casting unsigned long to a
128
 
       uint32_t. */
129
 
    if ((temp_drizzle_port == 0) || (temp_drizzle_port > 65535))
130
 
    {
131
 
      fprintf(stderr, _("Value supplied for port is not valid.\n"));
132
 
      exit(1);
133
 
    }
134
 
    else
135
 
    {
136
 
      tcp_port= (uint32_t) temp_drizzle_port;
137
 
    }
138
 
    break;
139
 
  case 'P':
140
113
    if (argument)
141
114
    {
142
115
      char *start=argument;
143
 
      if (opt_password)
144
 
        free(opt_password);
145
 
 
146
 
      opt_password= strdup(argument);
147
 
      if (opt_password == NULL)
148
 
      {
149
 
        fprintf(stderr, _("Memory allocation error while copying password. "
150
 
                          "Aborting.\n"));
151
 
        exit(ENOMEM);
152
 
      }
153
 
      while (*argument)
154
 
      {
155
 
        /* Overwriting password with 'x' */
156
 
        *argument++= 'x';
157
 
      }
 
116
      my_free(opt_password,MYF(MY_ALLOW_ZERO_PTR));
 
117
      opt_password= my_strdup(argument,MYF(MY_FAE));
 
118
      while (*argument) *argument++= 'x';   /* Destroy argument */
158
119
      if (*start)
159
 
      {
160
 
        /* Cut length of argument */
161
 
        start[1]= 0;
162
 
      }
 
120
        start[1]=0; /* Cut length of argument */
163
121
      tty_password= 0;
164
122
    }
165
 
    else
166
 
      tty_password= 1;
 
123
    else 
 
124
      tty_password= 1; 
167
125
    break;
168
126
  case 's':
169
127
    option_silent++;
171
129
  case 'V':
172
130
    print_version();
173
131
    exit(0);
 
132
    break;
174
133
  case 'w':
175
134
    if (argument)
176
135
    {
202
161
 
203
162
  MY_INIT(argv[0]);
204
163
  drizzle_create(&drizzle);
205
 
  load_defaults("drizzle",load_default_groups,&argc,&argv);
 
164
  load_defaults("my",load_default_groups,&argc,&argv);
206
165
  save_argv = argv;                             /* Save for free_defaults */
207
166
  if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
208
167
  {
218
177
 
219
178
  commands = argv;
220
179
  if (tty_password)
221
 
    opt_password = get_tty_password(NULL);
 
180
    opt_password = get_tty_password(NullS);
222
181
 
223
 
  signal(SIGINT,endprog);                       /* Here if abort */
224
 
  signal(SIGTERM,endprog);              /* Here if abort */
 
182
  VOID(signal(SIGINT,endprog));                 /* Here if abort */
 
183
  VOID(signal(SIGTERM,endprog));                /* Here if abort */
225
184
 
226
185
  if (opt_connect_timeout)
227
186
  {
228
187
    uint tmp=opt_connect_timeout;
229
188
    drizzle_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT, (char*) &tmp);
230
189
  }
 
190
  /* force drizzleadmin to use TCP */
 
191
  drizzle_options(&drizzle, DRIZZLE_OPT_PROTOCOL, (char*)&opt_protocol);
231
192
 
232
193
  error_flags= (myf)0;
233
194
 
254
215
    error=execute_commands(&drizzle,argc,commands);
255
216
    drizzle_close(&drizzle);
256
217
  }
257
 
  free(opt_password);
258
 
  free(user);
 
218
  my_free(opt_password,MYF(MY_ALLOW_ZERO_PTR));
 
219
  my_free(user,MYF(MY_ALLOW_ZERO_PTR));
259
220
  free_defaults(save_argv);
260
221
  my_end(my_end_arg);
261
 
  return error ? 1 : 0;
 
222
  exit(error ? 1 : 0);
262
223
}
263
224
 
264
 
void endprog(int)
 
225
sig_handler endprog(int signal_number __attribute__((unused)))
265
226
{
266
227
  interrupted=1;
267
228
}
272
233
 
273
234
  for (;;)
274
235
  {
275
 
    if (drizzle_connect(drizzle,host,user,opt_password,NULL,tcp_port,NULL,0))
 
236
    if (drizzle_connect(drizzle,host,user,opt_password,NullS,tcp_port,NULL,0))
276
237
    {
277
238
      drizzle->reconnect= 1;
278
239
      if (info)
290
251
        if (!host)
291
252
          host= (char*) LOCAL_HOST;
292
253
 
293
 
        my_printf_error(0,_("connect to server at '%s' failed\nerror: '%s'"),
 
254
        my_printf_error(0,"connect to server at '%s' failed\nerror: '%s'",
294
255
        error_flags, host, drizzle_error(drizzle));
295
256
 
296
257
        if (drizzle_errno(drizzle) == CR_CONN_HOST_ERROR ||
297
258
          drizzle_errno(drizzle) == CR_UNKNOWN_HOST)
298
259
        {
299
 
          fprintf(stderr,_("Check that drizzled is running on %s"),host);
300
 
          fprintf(stderr,_(" and that the port is %d.\n"),
301
 
          tcp_port ? tcp_port: drizzle_get_default_port());
302
 
          fprintf(stderr,_("You can check this by doing 'telnet %s %d'\n"),
303
 
                  host, tcp_port ? tcp_port: drizzle_get_default_port());
 
260
          fprintf(stderr,"Check that drizzled is running on %s",host);
 
261
          fprintf(stderr," and that the port is %d.\n",
 
262
          tcp_port ? tcp_port: drizzle_port);
 
263
          fprintf(stderr,"You can check this by doing 'telnet %s %d'\n",
 
264
                  host, tcp_port ? tcp_port: drizzle_port);
304
265
        }
305
266
      }
306
267
      return 1;
307
268
    }
308
 
    if (wait != UINT32_MAX)
 
269
    if (wait != (uint) ~0)
309
270
      wait--;                           /* One less retry */
310
271
    if ((drizzle_errno(drizzle) != CR_CONN_HOST_ERROR) &&
311
272
        (drizzle_errno(drizzle) != CR_CONNECTION_ERROR))
312
273
    {
313
 
      fprintf(stderr,_("Got error: %s\n"), drizzle_error(drizzle));
 
274
      fprintf(stderr,"Got error: %s\n", drizzle_error(drizzle));
314
275
    }
315
276
    else if (!option_silent)
316
277
    {
317
278
      if (!info)
318
279
      {
319
280
        info=1;
320
 
        fputs(_("Waiting for Drizzle server to answer"),stderr);
 
281
        fputs("Waiting for Drizzle server to answer",stderr);
321
282
        (void) fflush(stderr);
322
283
      }
323
284
      else
350
311
    case ADMIN_SHUTDOWN:
351
312
    {
352
313
      if (opt_verbose)
353
 
        printf(_("shutting down drizzled...\n"));
 
314
        printf("shutting down drizzled...\n");
354
315
 
355
 
      if (drizzle_shutdown(drizzle))
 
316
      if (drizzle_shutdown(drizzle, SHUTDOWN_DEFAULT))
356
317
      {
357
 
        my_printf_error(0, _("shutdown failed; error: '%s'"), error_flags,
 
318
        my_printf_error(0, "shutdown failed; error: '%s'", error_flags,
358
319
                        drizzle_error(drizzle));
359
320
        return -1;
360
321
      }
361
322
      drizzle_close(drizzle);   /* Close connection to avoid error messages */
362
323
 
363
324
      if (opt_verbose)
364
 
        printf(_("done\n"));
 
325
        printf("done\n");
365
326
 
366
327
      argc=1;             /* Force SHUTDOWN to be the last command */
367
328
      break;
371
332
      if (!drizzle_ping(drizzle))
372
333
      {
373
334
        if (option_silent < 2)
374
 
          puts(_("drizzled is alive"));
 
335
          puts("drizzled is alive");
375
336
      }
376
337
      else
377
338
      {
379
340
        {
380
341
          drizzle->reconnect=1;
381
342
          if (!drizzle_ping(drizzle))
382
 
            puts(_("connection was down, but drizzled is now alive"));
 
343
            puts("connection was down, but drizzled is now alive");
383
344
        }
384
345
        else
385
346
              {
386
 
          my_printf_error(0,_("drizzled doesn't answer to ping, error: '%s'"),
 
347
          my_printf_error(0,"drizzled doesn't answer to ping, error: '%s'",
387
348
          error_flags, drizzle_error(drizzle));
388
349
          return -1;
389
350
        }
392
353
      break;
393
354
 
394
355
    default:
395
 
      my_printf_error(0, _("Unknown command: '%-.60s'"), error_flags, argv[0]);
 
356
      my_printf_error(0, "Unknown command: '%-.60s'", error_flags, argv[0]);
396
357
      return 1;
397
358
    }
398
359
  }
401
362
 
402
363
static void print_version(void)
403
364
{
404
 
  printf(_("%s  Ver %s Distrib %s, for %s on %s\n"),my_progname,ADMIN_VERSION,
405
 
         drizzle_get_client_info(),SYSTEM_TYPE,MACHINE_TYPE);
 
365
  printf("%s  Ver %s Distrib %s, for %s on %s\n",my_progname,ADMIN_VERSION,
 
366
         MYSQL_SERVER_VERSION,SYSTEM_TYPE,MACHINE_TYPE);
406
367
}
407
368
 
408
369
static void usage(void)
409
370
{
410
371
  print_version();
411
 
  puts(_("Copyright (C) 2000-2008 MySQL AB"));
412
 
  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"));
413
 
  puts(_("Administration program for the drizzled daemon."));
414
 
  printf(_("Usage: %s [OPTIONS] command command....\n"), my_progname);
 
372
  puts("Copyright (C) 2000-2006 DRIZZLE AB");
 
373
  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");
 
374
  puts("Administration program for the drizzled daemon.");
 
375
  printf("Usage: %s [OPTIONS] command command....\n", my_progname);
415
376
  my_print_help(my_long_options);
416
 
  puts(_("\
 
377
  puts("\
417
378
  ping         Check if server is down\n\
418
 
  shutdown     Take server down\n"));
 
379
  shutdown     Take server down\n");
419
380
}