47
47
extern "C" void endprog(int signal_number);
48
48
extern "C" bool get_one_option(int optid, const struct my_option *opt,
50
static int execute_commands(DRIZZLE *drizzle,int argc, char **argv);
51
static bool sql_connect(DRIZZLE *drizzle, uint32_t wait);
50
static int execute_commands(drizzle_con_st *con,int argc, char **argv);
51
static bool sql_connect(drizzle_con_st *con, uint32_t wait);
54
54
The order of commands must be the same as command_names,
196
196
int main(int argc,char *argv[])
198
198
int error= 0, ho_error;
200
201
char **commands, **save_argv;
202
203
MY_INIT(argv[0]);
203
drizzleclient_create(&drizzle);
204
drizzle_create(&drizzle);
205
drizzle_con_create(&drizzle, &con);
204
206
load_defaults("drizzle",load_default_groups,&argc,&argv);
205
207
save_argv = argv; /* Save for free_defaults */
206
208
if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
219
221
if (tty_password)
220
opt_password = drizzleclient_get_tty_password(NULL);
222
opt_password = client_get_tty_password(NULL);
222
224
signal(SIGINT,endprog); /* Here if abort */
223
225
signal(SIGTERM,endprog); /* Here if abort */
225
228
if (opt_connect_timeout)
227
230
uint32_t tmp=opt_connect_timeout;
228
231
drizzleclient_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT, (char*) &tmp);
231
if (sql_connect(&drizzle, option_wait))
235
if (sql_connect(&con, option_wait))
233
unsigned int err= drizzleclient_errno(&drizzle);
234
if (err >= CR_MIN_ERROR && err <= CR_MAX_ERROR)
237
/* Return 0 if all commands are PING */
238
for (; argc > 0; argv++, argc--)
238
/* Return 0 if all commands are PING */
239
for (; argc > 0; argv++, argc--)
240
if (find_type(argv[0], &command_typelib, 2) != ADMIN_PING)
241
if (find_type(argv[0], &command_typelib, 2) != ADMIN_PING)
251
error=execute_commands(&drizzle,argc,commands);
252
drizzleclient_close(&drizzle);
249
error=execute_commands(&con,argc,commands);
251
drizzle_con_free(&con);
252
drizzle_free(&drizzle);
254
253
free(opt_password);
256
255
free_defaults(save_argv);
266
static bool sql_connect(DRIZZLE *drizzle, uint32_t wait)
265
static bool sql_connect(drizzle_con_st *con, uint32_t wait)
268
drizzle_return_t ret;
270
drizzle_con_set_tcp(con, host, tcp_port);
271
drizzle_con_set_auth(con, user, opt_password);
272
if (drizzleclient_connect(drizzle,host,user,opt_password,NULL,tcp_port,NULL,0))
275
ret= drizzle_con_connect(con);
276
if (ret == DRIZZLE_RETURN_OK)
274
drizzle->reconnect= 1;
277
280
fputs("\n",stderr);
285
288
if (!option_silent)
288
host= (char*) LOCAL_HOST;
291
host= (char *)DRIZZLE_DEFAULT_TCP_HOST;
290
293
fprintf(stderr,_("connect to server at '%s' failed\nerror: '%s'"),
291
host, drizzleclient_error(drizzle));
294
host, drizzle_con_error(con));
293
if (drizzleclient_errno(drizzle) == CR_CONN_HOST_ERROR ||
294
drizzleclient_errno(drizzle) == CR_UNKNOWN_HOST)
296
if (ret == DRIZZLE_RETURN_GETADDRINFO ||
297
ret == DRIZZLE_RETURN_COULD_NOT_CONNECT)
296
299
fprintf(stderr,_("Check that drizzled is running on %s"),host);
297
300
fprintf(stderr,_(" and that the port is %d.\n"),
298
tcp_port ? tcp_port: drizzleclient_get_default_port());
301
tcp_port ? tcp_port: DRIZZLE_DEFAULT_TCP_PORT);
299
302
fprintf(stderr,_("You can check this by doing 'telnet %s %d'\n"),
300
host, tcp_port ? tcp_port: drizzleclient_get_default_port());
303
host, tcp_port ? tcp_port: DRIZZLE_DEFAULT_TCP_PORT);
305
308
if (wait != UINT32_MAX)
306
309
wait--; /* One less retry */
307
if ((drizzleclient_errno(drizzle) != CR_CONN_HOST_ERROR) &&
308
(drizzleclient_errno(drizzle) != CR_CONNECTION_ERROR))
310
if (ret != DRIZZLE_RETURN_GETADDRINFO &&
311
ret != DRIZZLE_RETURN_COULD_NOT_CONNECT)
310
fprintf(stderr,_("Got error: %s\n"), drizzleclient_error(drizzle));
313
fprintf(stderr,_("Got error: %s\n"), drizzle_con_error(con));
312
315
else if (!option_silent)
333
336
-1 on retryable error
336
static int execute_commands(DRIZZLE *drizzle,int argc, char **argv)
339
static int execute_commands(drizzle_con_st *con,int argc, char **argv)
341
drizzle_result_st result;
342
drizzle_return_t ret;
340
345
DRIZZLE documentation relies on the fact that drizzleadmin will
350
355
printf(_("shutting down drizzled...\n"));
352
if (drizzleclient_shutdown(drizzle))
357
if (drizzle_shutdown(con, &result, DRIZZLE_SHUTDOWN_DEFAULT,
359
ret != DRIZZLE_RETURN_OK)
354
fprintf(stderr, _("shutdown failed; error: '%s'"),
355
drizzleclient_error(drizzle));
361
if (ret == DRIZZLE_RETURN_ERROR_CODE)
363
fprintf(stderr, _("shutdown failed; error: '%s'"),
364
drizzle_result_error(&result));
365
drizzle_result_free(&result);
369
fprintf(stderr, _("shutdown failed; error: '%s'"),
370
drizzle_con_error(con));
358
drizzleclient_close(drizzle); /* Close connection to avoid error messages */
375
drizzle_result_free(&result);
361
377
printf(_("done\n"));
367
drizzle->reconnect=0; /* We want to know of reconnects */
368
if (!drizzleclient_ping(drizzle))
383
if (drizzle_ping(con, &result, &ret) != NULL && ret == DRIZZLE_RETURN_OK)
370
385
if (option_silent < 2)
371
386
puts(_("drizzled is alive"));
375
if (drizzleclient_errno(drizzle) == CR_SERVER_GONE_ERROR)
390
if (ret == DRIZZLE_RETURN_SERVER_GONE)
377
drizzle->reconnect=1;
378
if (!drizzleclient_ping(drizzle))
392
if (drizzle_ping(con, &result, &ret) != NULL &&
393
ret == DRIZZLE_RETURN_OK)
379
395
puts(_("connection was down, but drizzled is now alive"));
383
fprintf(stderr,_("drizzled doesn't answer to ping, error: '%s'"),
384
drizzleclient_error(drizzle));
400
if (ret == DRIZZLE_RETURN_ERROR_CODE)
402
fprintf(stderr, _("shutdown failed; error: '%s'"),
403
drizzle_result_error(&result));
404
drizzle_result_free(&result);
408
fprintf(stderr,_("drizzled doesn't answer to ping, error: '%s'"),
409
drizzle_con_error(con));
388
drizzle->reconnect=1; /* Automatic reconnect is default */
414
drizzle_result_free(&result);
399
425
static void print_version(void)
401
427
printf(_("%s Ver %s Distrib %s, for %s on %s\n"),my_progname,ADMIN_VERSION,
402
drizzleclient_get_client_info(),SYSTEM_TYPE,MACHINE_TYPE);
428
drizzle_version(),SYSTEM_TYPE,MACHINE_TYPE);
405
431
static void usage(void)