~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzleadmin.cc

  • Committer: Brian Aker
  • Date: 2009-02-20 22:48:37 UTC
  • Revision ID: brian@tangent.org-20090220224837-fw5wrf46n4ru3e6a
First pass of stripping uint

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
#include <sys/stat.h>
27
27
 
28
28
/* Added this for string translation. */
29
 
#include <libdrizzle/gettext.h>
 
29
#include <drizzled/gettext.h>
30
30
 
31
31
#define ADMIN_VERSION "8.42"
32
32
#define SHUTDOWN_DEF_TIMEOUT 3600               /* Wait for shutdown */
33
33
 
34
34
char *host= NULL, *user= NULL, *opt_password= NULL;
35
 
static bool interrupted= false, opt_verbose= false,tty_password= false; 
 
35
static bool interrupted= false, opt_verbose= false,tty_password= false;
36
36
static uint32_t tcp_port= 0, option_wait= 0, option_silent= 0;
37
37
static uint32_t my_end_arg;
38
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 */
 
39
 
 
40
using namespace std;
40
41
 
41
42
/*
42
 
  Forward declarations 
 
43
  Forward declarations
43
44
*/
44
45
static void usage(void);
45
46
static void print_version(void);
46
 
extern "C" RETSIGTYPE endprog(int signal_number);
 
47
extern "C" void endprog(int signal_number);
47
48
extern "C" bool get_one_option(int optid, const struct my_option *opt,
48
49
                               char *argument);
49
50
static int execute_commands(DRIZZLE *drizzle,int argc, char **argv);
50
 
static bool sql_connect(DRIZZLE *drizzle, uint wait);
 
51
static bool sql_connect(DRIZZLE *drizzle, uint32_t wait);
51
52
 
52
53
/*
53
54
  The order of commands must be the same as command_names,
74
75
   NO_ARG, 0, 0, 0, 0, 0, 0},
75
76
  {"host", 'h', N_("Connect to host."), (char**) &host, (char**) &host, 0, GET_STR,
76
77
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
77
 
  {"password", 'p',
 
78
  {"password", 'P',
78
79
   N_("Password to use when connecting to server. If password is not given it's asked from the tty."),
79
80
   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, "
 
81
  {"port", 'p', N_("Port number to use for connection or 0 for default to, in "
 
82
   "order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, "
82
83
   "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},
 
84
   0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
85
85
  {"silent", 's', N_("Silently exit if one can't connect to server."),
86
86
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
87
87
#ifndef DONT_ALLOW_USER_CHANGE
107
107
static const char *load_default_groups[]= { "drizzleadmin","client",0 };
108
108
 
109
109
bool
110
 
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
111
 
               char *argument)
 
110
get_one_option(int optid, const struct my_option *, char *argument)
112
111
{
 
112
  char *endchar= NULL;
 
113
  uint64_t temp_drizzle_port= 0;
113
114
  int error = 0;
114
115
 
115
116
  switch(optid) {
116
117
  case 'p':
 
118
    temp_drizzle_port= (uint64_t) strtoul(argument, &endchar, 10);
 
119
    /* if there is an alpha character this is not a valid port */
 
120
    if (strlen(endchar) != 0)
 
121
    {
 
122
      fprintf(stderr, _("Non-integer value supplied for port.  If you are trying to enter a password please use --password instead.\n"));
 
123
      exit(1);
 
124
    }
 
125
    /* If the port number is > 65535 it is not a valid port
 
126
       This also helps with potential data loss casting unsigned long to a
 
127
       uint32_t. */
 
128
    if ((temp_drizzle_port == 0) || (temp_drizzle_port > 65535))
 
129
    {
 
130
      fprintf(stderr, _("Value supplied for port is not valid.\n"));
 
131
      exit(1);
 
132
    }
 
133
    else
 
134
    {
 
135
      tcp_port= (uint32_t) temp_drizzle_port;
 
136
    }
 
137
    break;
 
138
  case 'P':
117
139
    if (argument)
118
140
    {
119
141
      char *start=argument;
120
 
      free(opt_password);
121
 
      opt_password= my_strdup(argument,MYF(MY_FAE));
122
 
      while (*argument) *argument++= 'x';   /* Destroy argument */
 
142
      if (opt_password)
 
143
        free(opt_password);
 
144
 
 
145
      opt_password= strdup(argument);
 
146
      if (opt_password == NULL)
 
147
      {
 
148
        fprintf(stderr, _("Memory allocation error while copying password. "
 
149
                          "Aborting.\n"));
 
150
        exit(ENOMEM);
 
151
      }
 
152
      while (*argument)
 
153
      {
 
154
        /* Overwriting password with 'x' */
 
155
        *argument++= 'x';
 
156
      }
123
157
      if (*start)
124
 
        start[1]=0; /* Cut length of argument */
 
158
      {
 
159
        /* Cut length of argument */
 
160
        start[1]= 0;
 
161
      }
125
162
      tty_password= 0;
126
163
    }
127
 
    else 
128
 
      tty_password= 1; 
 
164
    else
 
165
      tty_password= 1;
129
166
    break;
130
167
  case 's':
131
168
    option_silent++;
133
170
  case 'V':
134
171
    print_version();
135
172
    exit(0);
136
 
    break;
137
173
  case 'w':
138
174
    if (argument)
139
175
    {
164
200
  char **commands, **save_argv;
165
201
 
166
202
  MY_INIT(argv[0]);
167
 
  drizzle_create(&drizzle);
168
 
  load_defaults("my",load_default_groups,&argc,&argv);
 
203
  drizzleclient_create(&drizzle);
 
204
  load_defaults("drizzle",load_default_groups,&argc,&argv);
169
205
  save_argv = argv;                             /* Save for free_defaults */
170
206
  if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
171
207
  {
181
217
 
182
218
  commands = argv;
183
219
  if (tty_password)
184
 
    opt_password = get_tty_password(NULL);
 
220
    opt_password = drizzleclient_get_tty_password(NULL);
185
221
 
186
222
  signal(SIGINT,endprog);                       /* Here if abort */
187
223
  signal(SIGTERM,endprog);              /* Here if abort */
188
224
 
189
225
  if (opt_connect_timeout)
190
226
  {
191
 
    uint tmp=opt_connect_timeout;
192
 
    drizzle_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT, (char*) &tmp);
 
227
    uint32_t tmp=opt_connect_timeout;
 
228
    drizzleclient_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT, (char*) &tmp);
193
229
  }
194
230
 
195
 
  error_flags= (myf)0;
196
 
 
197
231
  if (sql_connect(&drizzle, option_wait))
198
232
  {
199
 
    unsigned int err= drizzle_errno(&drizzle);
 
233
    unsigned int err= drizzleclient_errno(&drizzle);
200
234
    if (err >= CR_MIN_ERROR && err <= CR_MAX_ERROR)
201
235
      error= 1;
202
236
    else
215
249
  else
216
250
  {
217
251
    error=execute_commands(&drizzle,argc,commands);
218
 
    drizzle_close(&drizzle);
 
252
    drizzleclient_close(&drizzle);
219
253
  }
220
254
  free(opt_password);
221
255
  free(user);
222
256
  free_defaults(save_argv);
223
257
  my_end(my_end_arg);
224
 
  exit(error ? 1 : 0);
 
258
  return error ? 1 : 0;
225
259
}
226
260
 
227
 
RETSIGTYPE endprog(int signal_number __attribute__((unused)))
 
261
void endprog(int)
228
262
{
229
263
  interrupted=1;
230
264
}
231
265
 
232
 
static bool sql_connect(DRIZZLE *drizzle, uint wait)
 
266
static bool sql_connect(DRIZZLE *drizzle, uint32_t wait)
233
267
{
234
268
  bool info=0;
235
269
 
236
270
  for (;;)
237
271
  {
238
 
    if (drizzle_connect(drizzle,host,user,opt_password,NULL,tcp_port,NULL,0))
 
272
    if (drizzleclient_connect(drizzle,host,user,opt_password,NULL,tcp_port,NULL,0))
239
273
    {
240
274
      drizzle->reconnect= 1;
241
275
      if (info)
253
287
        if (!host)
254
288
          host= (char*) LOCAL_HOST;
255
289
 
256
 
        my_printf_error(0,_("connect to server at '%s' failed\nerror: '%s'"),
257
 
        error_flags, host, drizzle_error(drizzle));
 
290
        fprintf(stderr,_("connect to server at '%s' failed\nerror: '%s'"),
 
291
                host, drizzleclient_error(drizzle));
258
292
 
259
 
        if (drizzle_errno(drizzle) == CR_CONN_HOST_ERROR ||
260
 
          drizzle_errno(drizzle) == CR_UNKNOWN_HOST)
 
293
        if (drizzleclient_errno(drizzle) == CR_CONN_HOST_ERROR ||
 
294
          drizzleclient_errno(drizzle) == CR_UNKNOWN_HOST)
261
295
        {
262
296
          fprintf(stderr,_("Check that drizzled is running on %s"),host);
263
297
          fprintf(stderr,_(" and that the port is %d.\n"),
264
 
          tcp_port ? tcp_port: drizzle_get_default_port());
 
298
          tcp_port ? tcp_port: drizzleclient_get_default_port());
265
299
          fprintf(stderr,_("You can check this by doing 'telnet %s %d'\n"),
266
 
                  host, tcp_port ? tcp_port: drizzle_get_default_port());
 
300
                  host, tcp_port ? tcp_port: drizzleclient_get_default_port());
267
301
        }
268
302
      }
269
303
      return 1;
270
304
    }
271
305
    if (wait != UINT32_MAX)
272
306
      wait--;                           /* One less retry */
273
 
    if ((drizzle_errno(drizzle) != CR_CONN_HOST_ERROR) &&
274
 
        (drizzle_errno(drizzle) != CR_CONNECTION_ERROR))
 
307
    if ((drizzleclient_errno(drizzle) != CR_CONN_HOST_ERROR) &&
 
308
        (drizzleclient_errno(drizzle) != CR_CONNECTION_ERROR))
275
309
    {
276
 
      fprintf(stderr,_("Got error: %s\n"), drizzle_error(drizzle));
 
310
      fprintf(stderr,_("Got error: %s\n"), drizzleclient_error(drizzle));
277
311
    }
278
312
    else if (!option_silent)
279
313
    {
315
349
      if (opt_verbose)
316
350
        printf(_("shutting down drizzled...\n"));
317
351
 
318
 
      if (drizzle_shutdown(drizzle))
 
352
      if (drizzleclient_shutdown(drizzle))
319
353
      {
320
 
        my_printf_error(0, _("shutdown failed; error: '%s'"), error_flags,
321
 
                        drizzle_error(drizzle));
 
354
        fprintf(stderr, _("shutdown failed; error: '%s'"),
 
355
                drizzleclient_error(drizzle));
322
356
        return -1;
323
357
      }
324
 
      drizzle_close(drizzle);   /* Close connection to avoid error messages */
 
358
      drizzleclient_close(drizzle);     /* Close connection to avoid error messages */
325
359
 
326
360
      if (opt_verbose)
327
361
        printf(_("done\n"));
331
365
    }
332
366
    case ADMIN_PING:
333
367
      drizzle->reconnect=0;     /* We want to know of reconnects */
334
 
      if (!drizzle_ping(drizzle))
 
368
      if (!drizzleclient_ping(drizzle))
335
369
      {
336
370
        if (option_silent < 2)
337
371
          puts(_("drizzled is alive"));
338
372
      }
339
373
      else
340
374
      {
341
 
        if (drizzle_errno(drizzle) == CR_SERVER_GONE_ERROR)
 
375
        if (drizzleclient_errno(drizzle) == CR_SERVER_GONE_ERROR)
342
376
        {
343
377
          drizzle->reconnect=1;
344
 
          if (!drizzle_ping(drizzle))
 
378
          if (!drizzleclient_ping(drizzle))
345
379
            puts(_("connection was down, but drizzled is now alive"));
346
380
        }
347
381
        else
348
382
              {
349
 
          my_printf_error(0,_("drizzled doesn't answer to ping, error: '%s'"),
350
 
          error_flags, drizzle_error(drizzle));
 
383
          fprintf(stderr,_("drizzled doesn't answer to ping, error: '%s'"),
 
384
                  drizzleclient_error(drizzle));
351
385
          return -1;
352
386
        }
353
387
      }
355
389
      break;
356
390
 
357
391
    default:
358
 
      my_printf_error(0, _("Unknown command: '%-.60s'"), error_flags, argv[0]);
 
392
      fprintf(stderr, _("Unknown command: '%-.60s'"), argv[0]);
359
393
      return 1;
360
394
    }
361
395
  }
365
399
static void print_version(void)
366
400
{
367
401
  printf(_("%s  Ver %s Distrib %s, for %s on %s\n"),my_progname,ADMIN_VERSION,
368
 
         drizzle_get_client_info(),SYSTEM_TYPE,MACHINE_TYPE);
 
402
         drizzleclient_get_client_info(),SYSTEM_TYPE,MACHINE_TYPE);
369
403
}
370
404
 
371
405
static void usage(void)