~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/mysqladmin.cc

Merged.

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 */
 
1
/* Copyright (C) 2000-2006 MySQL AB
 
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 */
 
15
 
 
16
/* maintaince of mysql databases */
22
17
 
23
18
#include "client_priv.h"
24
19
#include <signal.h>
25
 
#include <mysys/my_pthread.h>                           /* because of signal()  */
 
20
#include <my_pthread.h>                         /* because of signal()  */
26
21
#include <sys/stat.h>
27
22
 
28
 
/* Added this for string translation. */
29
 
#include <libdrizzle/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
27
static bool interrupted= false, opt_verbose= false,tty_password= false; 
 
28
static uint8_t opt_protocol= MYSQL_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;
43
36
*/
44
37
static void usage(void);
45
38
static void print_version(void);
46
 
extern "C" RETSIGTYPE endprog(int signal_number);
 
39
extern "C" sig_handler endprog(int signal_number);
47
40
extern "C" bool get_one_option(int optid, const struct my_option *opt,
48
41
                               char *argument);
49
 
static int execute_commands(DRIZZLE *drizzle,int argc, char **argv);
50
 
static bool sql_connect(DRIZZLE *drizzle, uint wait);
 
42
static int execute_commands(MYSQL *mysql,int argc, char **argv);
 
43
static bool sql_connect(MYSQL *mysql, uint wait);
51
44
 
52
45
/*
53
46
  The order of commands must be the same as command_names,
62
55
static const char *command_names[]= {
63
56
  "shutdown",
64
57
  "ping",
65
 
  NULL
 
58
  NullS
66
59
};
67
60
 
68
61
static TYPELIB command_typelib=
70
63
 
71
64
static struct my_option my_long_options[] =
72
65
{
73
 
  {"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,
74
67
   NO_ARG, 0, 0, 0, 0, 0, 0},
75
 
  {"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,
76
69
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
77
70
  {"password", 'p',
78
 
   N_("Password to use when connecting to server. If password is not given it's asked from the tty."),
 
71
   "Password to use when connecting to server. If password is not given it's asked from the tty.",
79
72
   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) ")."),
 
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) ").",
83
79
   (char**) &tcp_port,
84
80
   (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."),
 
81
  {"silent", 's', "Silently exit if one can't connect to server.",
86
82
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
87
83
#ifndef DONT_ALLOW_USER_CHANGE
88
 
  {"user", 'u', N_("User for login if not current user."), (char**) &user,
 
84
  {"user", 'u', "User for login if not current user.", (char**) &user,
89
85
   (char**) &user, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
90
86
#endif
91
 
  {"verbose", 'v', N_("Write more information."), (char**) &opt_verbose,
 
87
  {"verbose", 'v', "Write more information.", (char**) &opt_verbose,
92
88
   (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,
 
89
  {"version", 'V', "Output version information and exit.", 0, 0, 0, GET_NO_ARG,
94
90
   NO_ARG, 0, 0, 0, 0, 0, 0},
95
 
  {"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,
96
92
   OPT_ARG, 0, 0, 0, 0, 0, 0},
97
93
  {"connect_timeout", OPT_CONNECT_TIMEOUT, "", (char**) &opt_connect_timeout,
98
94
   (char**) &opt_connect_timeout, 0, GET_ULONG, REQUIRED_ARG, 3600*12, 0,
104
100
};
105
101
 
106
102
 
107
 
static const char *load_default_groups[]= { "drizzleadmin","client",0 };
 
103
static const char *load_default_groups[]= { "mysqladmin","client",0 };
108
104
 
109
105
bool
110
106
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
117
113
    if (argument)
118
114
    {
119
115
      char *start=argument;
120
 
      free(opt_password);
 
116
      my_free(opt_password,MYF(MY_ALLOW_ZERO_PTR));
121
117
      opt_password= my_strdup(argument,MYF(MY_FAE));
122
118
      while (*argument) *argument++= 'x';   /* Destroy argument */
123
119
      if (*start)
160
156
int main(int argc,char *argv[])
161
157
{
162
158
  int error= 0, ho_error;
163
 
  DRIZZLE drizzle;
 
159
  MYSQL mysql;
164
160
  char **commands, **save_argv;
165
161
 
166
162
  MY_INIT(argv[0]);
167
 
  drizzle_create(&drizzle);
 
163
  mysql_init(&mysql);
168
164
  load_defaults("my",load_default_groups,&argc,&argv);
169
165
  save_argv = argv;                             /* Save for free_defaults */
170
166
  if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
181
177
 
182
178
  commands = argv;
183
179
  if (tty_password)
184
 
    opt_password = get_tty_password(NULL);
 
180
    opt_password = get_tty_password(NullS);
185
181
 
186
 
  signal(SIGINT,endprog);                       /* Here if abort */
187
 
  signal(SIGTERM,endprog);              /* Here if abort */
 
182
  VOID(signal(SIGINT,endprog));                 /* Here if abort */
 
183
  VOID(signal(SIGTERM,endprog));                /* Here if abort */
188
184
 
189
185
  if (opt_connect_timeout)
190
186
  {
191
187
    uint tmp=opt_connect_timeout;
192
 
    drizzle_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT, (char*) &tmp);
 
188
    mysql_options(&mysql,MYSQL_OPT_CONNECT_TIMEOUT, (char*) &tmp);
193
189
  }
 
190
  /* force mysqladmin to use TCP */
 
191
  mysql_options(&mysql, MYSQL_OPT_PROTOCOL, (char*)&opt_protocol);
194
192
 
195
193
  error_flags= (myf)0;
196
194
 
197
 
  if (sql_connect(&drizzle, option_wait))
 
195
  if (sql_connect(&mysql, option_wait))
198
196
  {
199
 
    unsigned int err= drizzle_errno(&drizzle);
 
197
    unsigned int err= mysql_errno(&mysql);
200
198
    if (err >= CR_MIN_ERROR && err <= CR_MAX_ERROR)
201
199
      error= 1;
202
200
    else
214
212
  }
215
213
  else
216
214
  {
217
 
    error=execute_commands(&drizzle,argc,commands);
218
 
    drizzle_close(&drizzle);
 
215
    error=execute_commands(&mysql,argc,commands);
 
216
    mysql_close(&mysql);
219
217
  }
220
 
  free(opt_password);
221
 
  free(user);
 
218
  my_free(opt_password,MYF(MY_ALLOW_ZERO_PTR));
 
219
  my_free(user,MYF(MY_ALLOW_ZERO_PTR));
222
220
  free_defaults(save_argv);
223
221
  my_end(my_end_arg);
224
222
  exit(error ? 1 : 0);
225
223
}
226
224
 
227
 
RETSIGTYPE endprog(int signal_number __attribute__((unused)))
 
225
sig_handler endprog(int signal_number __attribute__((unused)))
228
226
{
229
227
  interrupted=1;
230
228
}
231
229
 
232
 
static bool sql_connect(DRIZZLE *drizzle, uint wait)
 
230
static bool sql_connect(MYSQL *mysql, uint wait)
233
231
{
234
232
  bool info=0;
235
233
 
236
234
  for (;;)
237
235
  {
238
 
    if (drizzle_connect(drizzle,host,user,opt_password,NULL,tcp_port,NULL,0))
 
236
    if (mysql_real_connect(mysql,host,user,opt_password,NullS,tcp_port,NULL,0))
239
237
    {
240
 
      drizzle->reconnect= 1;
 
238
      mysql->reconnect= 1;
241
239
      if (info)
242
240
      {
243
241
        fputs("\n",stderr);
253
251
        if (!host)
254
252
          host= (char*) LOCAL_HOST;
255
253
 
256
 
        my_printf_error(0,_("connect to server at '%s' failed\nerror: '%s'"),
257
 
        error_flags, host, drizzle_error(drizzle));
 
254
        my_printf_error(0,"connect to server at '%s' failed\nerror: '%s'",
 
255
        error_flags, host, mysql_error(mysql));
258
256
 
259
 
        if (drizzle_errno(drizzle) == CR_CONN_HOST_ERROR ||
260
 
          drizzle_errno(drizzle) == CR_UNKNOWN_HOST)
 
257
        if (mysql_errno(mysql) == CR_CONN_HOST_ERROR ||
 
258
          mysql_errno(mysql) == CR_UNKNOWN_HOST)
261
259
        {
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());
 
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: mysql_port);
 
263
          fprintf(stderr,"You can check this by doing 'telnet %s %d'\n",
 
264
                  host, tcp_port ? tcp_port: mysql_port);
267
265
        }
268
266
      }
269
267
      return 1;
270
268
    }
271
 
    if (wait != UINT32_MAX)
 
269
    if (wait != (uint) ~0)
272
270
      wait--;                           /* One less retry */
273
 
    if ((drizzle_errno(drizzle) != CR_CONN_HOST_ERROR) &&
274
 
        (drizzle_errno(drizzle) != CR_CONNECTION_ERROR))
 
271
    if ((mysql_errno(mysql) != CR_CONN_HOST_ERROR) &&
 
272
        (mysql_errno(mysql) != CR_CONNECTION_ERROR))
275
273
    {
276
 
      fprintf(stderr,_("Got error: %s\n"), drizzle_error(drizzle));
 
274
      fprintf(stderr,"Got error: %s\n", mysql_error(mysql));
277
275
    }
278
276
    else if (!option_silent)
279
277
    {
280
278
      if (!info)
281
279
      {
282
280
        info=1;
283
 
        fputs(_("Waiting for Drizzle server to answer"),stderr);
 
281
        fputs("Waiting for Drizzle server to answer",stderr);
284
282
        (void) fflush(stderr);
285
283
      }
286
284
      else
299
297
         -1 on retryable error
300
298
         1 on fatal error
301
299
*/
302
 
static int execute_commands(DRIZZLE *drizzle,int argc, char **argv)
 
300
static int execute_commands(MYSQL *mysql,int argc, char **argv)
303
301
{
304
302
 
305
303
  /*
306
 
    DRIZZLE documentation relies on the fact that drizzleadmin will
 
304
    MySQL documentation relies on the fact that mysqladmin will
307
305
    execute commands in the order specified.
308
306
    If this behaviour is ever changed, Docs should be notified.
309
307
  */
313
311
    case ADMIN_SHUTDOWN:
314
312
    {
315
313
      if (opt_verbose)
316
 
        printf(_("shutting down drizzled...\n"));
 
314
        printf("shutting down drizzled...\n");
317
315
 
318
 
      if (drizzle_shutdown(drizzle))
 
316
      if (mysql_shutdown(mysql, SHUTDOWN_DEFAULT))
319
317
      {
320
 
        my_printf_error(0, _("shutdown failed; error: '%s'"), error_flags,
321
 
                        drizzle_error(drizzle));
 
318
        my_printf_error(0, "shutdown failed; error: '%s'", error_flags,
 
319
                        mysql_error(mysql));
322
320
        return -1;
323
321
      }
324
 
      drizzle_close(drizzle);   /* Close connection to avoid error messages */
 
322
      mysql_close(mysql);       /* Close connection to avoid error messages */
325
323
 
326
324
      if (opt_verbose)
327
 
        printf(_("done\n"));
 
325
        printf("done\n");
328
326
 
329
327
      argc=1;             /* Force SHUTDOWN to be the last command */
330
328
      break;
331
329
    }
332
330
    case ADMIN_PING:
333
 
      drizzle->reconnect=0;     /* We want to know of reconnects */
334
 
      if (!drizzle_ping(drizzle))
 
331
      mysql->reconnect=0;       /* We want to know of reconnects */
 
332
      if (!mysql_ping(mysql))
335
333
      {
336
334
        if (option_silent < 2)
337
 
          puts(_("drizzled is alive"));
 
335
          puts("drizzled is alive");
338
336
      }
339
337
      else
340
338
      {
341
 
        if (drizzle_errno(drizzle) == CR_SERVER_GONE_ERROR)
 
339
        if (mysql_errno(mysql) == CR_SERVER_GONE_ERROR)
342
340
        {
343
 
          drizzle->reconnect=1;
344
 
          if (!drizzle_ping(drizzle))
345
 
            puts(_("connection was down, but drizzled is now alive"));
 
341
          mysql->reconnect=1;
 
342
          if (!mysql_ping(mysql))
 
343
            puts("connection was down, but drizzled is now alive");
346
344
        }
347
345
        else
348
346
              {
349
 
          my_printf_error(0,_("drizzled doesn't answer to ping, error: '%s'"),
350
 
          error_flags, drizzle_error(drizzle));
 
347
          my_printf_error(0,"drizzled doesn't answer to ping, error: '%s'",
 
348
          error_flags, mysql_error(mysql));
351
349
          return -1;
352
350
        }
353
351
      }
354
 
      drizzle->reconnect=1;     /* Automatic reconnect is default */
 
352
      mysql->reconnect=1;       /* Automatic reconnect is default */
355
353
      break;
356
354
 
357
355
    default:
358
 
      my_printf_error(0, _("Unknown command: '%-.60s'"), error_flags, argv[0]);
 
356
      my_printf_error(0, "Unknown command: '%-.60s'", error_flags, argv[0]);
359
357
      return 1;
360
358
    }
361
359
  }
362
360
  return 0;
363
361
}
364
362
 
 
363
#include <help_start.h>
 
364
 
365
365
static void print_version(void)
366
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);
 
367
  printf("%s  Ver %s Distrib %s, for %s on %s\n",my_progname,ADMIN_VERSION,
 
368
         MYSQL_SERVER_VERSION,SYSTEM_TYPE,MACHINE_TYPE);
369
369
}
370
370
 
371
371
static void usage(void)
372
372
{
373
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);
 
374
  puts("Copyright (C) 2000-2006 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
378
  my_print_help(my_long_options);
379
 
  puts(_("\
 
379
  puts("\
380
380
  ping         Check if server is down\n\
381
 
  shutdown     Take server down\n"));
 
381
  shutdown     Take server down\n");
382
382
}
 
383
 
 
384
#include <help_end.h>
 
385