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