~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzleadmin.cc

  • Committer: Monty Taylor
  • Date: 2009-02-05 21:07:57 UTC
  • mto: This revision was merged to the branch mainline in revision 840.
  • Revision ID: mordred@inaugust.com-20090205210757-6487lf69y3mndcds
Fixed warnings badness in csv_alter_table test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 */
 
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
 */
15
20
 
16
21
/* maintaince of drizzle databases */
17
22
 
21
26
#include <sys/stat.h>
22
27
 
23
28
/* Added this for string translation. */
24
 
#include <libdrizzle/gettext.h>
 
29
#include <drizzled/gettext.h>
25
30
 
26
31
#define ADMIN_VERSION "8.42"
27
32
#define SHUTDOWN_DEF_TIMEOUT 3600               /* Wait for shutdown */
28
33
 
29
34
char *host= NULL, *user= NULL, *opt_password= NULL;
30
 
static bool interrupted= false, opt_verbose= false,tty_password= false; 
31
 
static uint8_t opt_protocol= DRIZZLE_PROTOCOL_TCP;  
 
35
static bool interrupted= false, opt_verbose= false,tty_password= false;
32
36
static uint32_t tcp_port= 0, option_wait= 0, option_silent= 0;
33
37
static uint32_t my_end_arg;
34
38
static uint32_t opt_connect_timeout, opt_shutdown_timeout;
35
39
static myf error_flags; /* flags to pass to my_printf_error, like ME_BELL */
36
40
 
 
41
using namespace std;
 
42
 
37
43
/*
38
 
  Forward declarations 
 
44
  Forward declarations
39
45
*/
40
46
static void usage(void);
41
47
static void print_version(void);
42
 
extern "C" sig_handler endprog(int signal_number);
 
48
extern "C" void endprog(int signal_number);
43
49
extern "C" bool get_one_option(int optid, const struct my_option *opt,
44
50
                               char *argument);
45
51
static int execute_commands(DRIZZLE *drizzle,int argc, char **argv);
58
64
static const char *command_names[]= {
59
65
  "shutdown",
60
66
  "ping",
61
 
  NullS
 
67
  NULL
62
68
};
63
69
 
64
70
static TYPELIB command_typelib=
70
76
   NO_ARG, 0, 0, 0, 0, 0, 0},
71
77
  {"host", 'h', N_("Connect to host."), (char**) &host, (char**) &host, 0, GET_STR,
72
78
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
73
 
  {"password", 'p',
 
79
  {"password", 'P',
74
80
   N_("Password to use when connecting to server. If password is not given it's asked from the tty."),
75
81
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
76
 
  {"port", 'P', N_("Port number to use for connection or 0 for default to, in "
77
 
   "order of preference, my.cnf, $DRIZZLE_TCP_PORT, "
 
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, "
78
84
   "built-in default (" STRINGIFY_ARG(DRIZZLE_PORT) ")."),
79
 
   (char**) &tcp_port,
80
 
   (char**) &tcp_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
85
   0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
81
86
  {"silent", 's', N_("Silently exit if one can't connect to server."),
82
87
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
83
88
#ifndef DONT_ALLOW_USER_CHANGE
103
108
static const char *load_default_groups[]= { "drizzleadmin","client",0 };
104
109
 
105
110
bool
106
 
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
107
 
               char *argument)
 
111
get_one_option(int optid, const struct my_option *, char *argument)
108
112
{
 
113
  char *endchar= NULL;
 
114
  uint64_t temp_drizzle_port= 0;
109
115
  int error = 0;
110
116
 
111
117
  switch(optid) {
112
118
  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':
113
140
    if (argument)
114
141
    {
115
142
      char *start=argument;
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 */
 
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
      }
119
158
      if (*start)
120
 
        start[1]=0; /* Cut length of argument */
 
159
      {
 
160
        /* Cut length of argument */
 
161
        start[1]= 0;
 
162
      }
121
163
      tty_password= 0;
122
164
    }
123
 
    else 
124
 
      tty_password= 1; 
 
165
    else
 
166
      tty_password= 1;
125
167
    break;
126
168
  case 's':
127
169
    option_silent++;
129
171
  case 'V':
130
172
    print_version();
131
173
    exit(0);
132
 
    break;
133
174
  case 'w':
134
175
    if (argument)
135
176
    {
161
202
 
162
203
  MY_INIT(argv[0]);
163
204
  drizzle_create(&drizzle);
164
 
  load_defaults("my",load_default_groups,&argc,&argv);
 
205
  load_defaults("drizzle",load_default_groups,&argc,&argv);
165
206
  save_argv = argv;                             /* Save for free_defaults */
166
207
  if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
167
208
  {
177
218
 
178
219
  commands = argv;
179
220
  if (tty_password)
180
 
    opt_password = get_tty_password(NullS);
 
221
    opt_password = get_tty_password(NULL);
181
222
 
182
 
  VOID(signal(SIGINT,endprog));                 /* Here if abort */
183
 
  VOID(signal(SIGTERM,endprog));                /* Here if abort */
 
223
  signal(SIGINT,endprog);                       /* Here if abort */
 
224
  signal(SIGTERM,endprog);              /* Here if abort */
184
225
 
185
226
  if (opt_connect_timeout)
186
227
  {
187
228
    uint tmp=opt_connect_timeout;
188
229
    drizzle_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT, (char*) &tmp);
189
230
  }
190
 
  /* force drizzleadmin to use TCP */
191
 
  drizzle_options(&drizzle, DRIZZLE_OPT_PROTOCOL, (char*)&opt_protocol);
192
231
 
193
232
  error_flags= (myf)0;
194
233
 
215
254
    error=execute_commands(&drizzle,argc,commands);
216
255
    drizzle_close(&drizzle);
217
256
  }
218
 
  my_free(opt_password,MYF(MY_ALLOW_ZERO_PTR));
219
 
  my_free(user,MYF(MY_ALLOW_ZERO_PTR));
 
257
  free(opt_password);
 
258
  free(user);
220
259
  free_defaults(save_argv);
221
260
  my_end(my_end_arg);
222
 
  exit(error ? 1 : 0);
 
261
  return error ? 1 : 0;
223
262
}
224
263
 
225
 
sig_handler endprog(int signal_number __attribute__((unused)))
 
264
void endprog(int)
226
265
{
227
266
  interrupted=1;
228
267
}
233
272
 
234
273
  for (;;)
235
274
  {
236
 
    if (drizzle_connect(drizzle,host,user,opt_password,NullS,tcp_port,NULL,0))
 
275
    if (drizzle_connect(drizzle,host,user,opt_password,NULL,tcp_port,NULL,0))
237
276
    {
238
277
      drizzle->reconnect= 1;
239
278
      if (info)
259
298
        {
260
299
          fprintf(stderr,_("Check that drizzled is running on %s"),host);
261
300
          fprintf(stderr,_(" and that the port is %d.\n"),
262
 
          tcp_port ? tcp_port: drizzle_port);
 
301
          tcp_port ? tcp_port: drizzle_get_default_port());
263
302
          fprintf(stderr,_("You can check this by doing 'telnet %s %d'\n"),
264
 
                  host, tcp_port ? tcp_port: drizzle_port);
 
303
                  host, tcp_port ? tcp_port: drizzle_get_default_port());
265
304
        }
266
305
      }
267
306
      return 1;
268
307
    }
269
 
    if (wait != (uint) ~0)
 
308
    if (wait != UINT32_MAX)
270
309
      wait--;                           /* One less retry */
271
310
    if ((drizzle_errno(drizzle) != CR_CONN_HOST_ERROR) &&
272
311
        (drizzle_errno(drizzle) != CR_CONNECTION_ERROR))
313
352
      if (opt_verbose)
314
353
        printf(_("shutting down drizzled...\n"));
315
354
 
316
 
      if (drizzle_shutdown(drizzle, SHUTDOWN_DEFAULT))
 
355
      if (drizzle_shutdown(drizzle))
317
356
      {
318
357
        my_printf_error(0, _("shutdown failed; error: '%s'"), error_flags,
319
358
                        drizzle_error(drizzle));