diff options
Diffstat (limited to 'cli/src/cli.c')
| -rw-r--r-- | cli/src/cli.c | 277 |
1 files changed, 135 insertions, 142 deletions
diff --git a/cli/src/cli.c b/cli/src/cli.c index f20ea1f17..91b315ff1 100644 --- a/cli/src/cli.c +++ b/cli/src/cli.c @@ -1,22 +1,12 @@ /* - Copyright (c) 2010-2011 Gluster, Inc. <http://www.gluster.com> - This file is part of GlusterFS. - - GlusterFS is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published - by the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - GlusterFS is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see - <http://www.gnu.org/licenses/>. -*/ + Copyright (c) 2010-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ #include <stdio.h> #include <string.h> #include <stdlib.h> @@ -99,43 +89,6 @@ rpc_clnt_prog_t *cli_rpc_prog; extern struct rpc_clnt_program cli_prog; - - - -static char * -generate_uuid () -{ - char tmp_str[1024] = {0,}; - char hostname[256] = {0,}; - struct timeval tv = {0,}; - struct tm now = {0, }; - char now_str[32]; - - if (gettimeofday (&tv, NULL) == -1) { - gf_log ("glusterfsd", GF_LOG_ERROR, - "gettimeofday: failed %s", - strerror (errno)); - } - - if (gethostname (hostname, 256) == -1) { - gf_log ("glusterfsd", GF_LOG_ERROR, - "gethostname: failed %s", - strerror (errno)); - } - - localtime_r (&tv.tv_sec, &now); - strftime (now_str, 32, "%Y/%m/%d-%H:%M:%S", &now); - snprintf (tmp_str, 1024, "%s-%d-%s:%" -#ifdef GF_DARWIN_HOST_OS - PRId32, -#else - "ld", -#endif - hostname, getpid(), now_str, tv.tv_usec); - - return gf_strdup (tmp_str); -} - static int glusterfs_ctx_defaults_init (glusterfs_ctx_t *ctx) { @@ -145,7 +98,7 @@ glusterfs_ctx_defaults_init (glusterfs_ctx_t *ctx) xlator_mem_acct_init (THIS, cli_mt_end); - ctx->process_uuid = generate_uuid (); + ctx->process_uuid = generate_glusterfs_ctx_id (); if (!ctx->process_uuid) return -1; @@ -179,6 +132,18 @@ glusterfs_ctx_defaults_init (glusterfs_ctx_t *ctx) if (!ctx->stub_mem_pool) return -1; + ctx->dict_pool = mem_pool_new (dict_t, 32); + if (!ctx->dict_pool) + return -1; + + ctx->dict_pair_pool = mem_pool_new (data_pair_t, 512); + if (!ctx->dict_pair_pool) + return -1; + + ctx->dict_data_pool = mem_pool_new (data_t, 512); + if (!ctx->dict_data_pool) + return -1; + INIT_LIST_HEAD (&pool->all_frames); LOCK_INIT (&pool->lock); ctx->pool = pool; @@ -198,12 +163,13 @@ glusterfs_ctx_defaults_init (glusterfs_ctx_t *ctx) static int -logging_init (struct cli_state *state) +logging_init (glusterfs_ctx_t *ctx, struct cli_state *state) { char *log_file = state->log_file ? state->log_file : DEFAULT_CLI_LOG_FILE_DIRECTORY "/cli.log"; - if (gf_log_init (log_file) == -1) { + /* passing ident as NULL means to use default ident for syslog */ + if (gf_log_init (ctx, log_file, NULL) == -1) { fprintf (stderr, "ERROR: failed to open logfile %s\n", log_file); return -1; @@ -318,18 +284,41 @@ cli_rpc_notify (struct rpc_clnt *rpc, void *mydata, rpc_clnt_event_t event, return ret; } + +/* + * ret: 0: option successfully processed + * 1: signalling end of option list + * -1: unknown option or other issue + */ int cli_opt_parse (char *opt, struct cli_state *state) { char *oarg; + if (strcmp (opt, "") == 0) + return 1; + if (strcmp (opt, "version") == 0) { - puts (argp_program_version); + cli_out ("%s", argp_program_version); + exit (0); + } + + if (strcmp (opt, "print-logdir") == 0) { + cli_out ("%s", DEFAULT_LOG_FILE_DIRECTORY); + exit (0); + } + + if (strcmp (opt, "print-statedumpdir") == 0) { + cli_out ("%s", DEFAULT_VAR_RUN_DIRECTORY); exit (0); } if (strcmp (opt, "xml") == 0) { +#if (HAVE_LIB_XML) state->mode |= GLUSTER_MODE_XML; +#else + cli_err ("XML output not supported. Ignoring '--xml' option"); +#endif return 0; } @@ -364,6 +353,12 @@ cli_opt_parse (char *opt, struct cli_state *state) return 0; } + oarg = strtail (opt, "glusterd-sock="); + if (oarg) { + state->glusterd_sock = oarg; + return 0; + } + return -1; } @@ -391,9 +386,16 @@ parse_cmdline (int argc, char *argv[], struct cli_state *state) state->argc--; /* argv shifted, next check should be at i again */ i--; + if (ret == 1) { + /* end of cli options */ + ret = 0; + break; + } } } + state->argv[state->argc] = NULL; + return ret; } @@ -418,7 +420,6 @@ cli_state_init (struct cli_state *state) int ret = 0; - state->remote_host = "localhost"; state->log_level = -1; tree = &state->tree; @@ -438,11 +439,35 @@ cli_usage_out (const char *usage) if (!usage || usage[0] == '\0') return -1; - cli_out ("Usage: %s", usage); + cli_err ("Usage: %s", usage); return 0; } int +_cli_err (const char *fmt, ...) +{ + struct cli_state *state = NULL; + va_list ap; + int ret = 0; + + state = global_state; + + va_start (ap, fmt); + +#ifdef HAVE_READLINE + if (state->rl_enabled && !state->rl_processing) + return cli_rl_err(state, fmt, ap); +#endif + + ret = vfprintf (stderr, fmt, ap); + fprintf (stderr, "\n"); + va_end (ap); + + return ret; +} + + +int _cli_out (const char *fmt, ...) { struct cli_state *state = NULL; @@ -481,23 +506,46 @@ cli_rpc_init (struct cli_state *state) if (!options) goto out; - ret = dict_set_str (options, "remote-host", state->remote_host); - if (ret) - goto out; + /* Connect using to glusterd using the specified method, giving + * preference to unix socket connection. If nothing is specified connect + * to the default glusterd socket + */ + if (state->glusterd_sock) { + gf_log ("cli", GF_LOG_INFO, "Connecting to glusterd using " + "sockfile %s", state->glusterd_sock); + ret = rpc_transport_unix_options_build (&options, + state->glusterd_sock, + 0); + if (ret) + goto out; + } else if (state->remote_host) { + gf_log ("cli", GF_LOG_INFO, "Connecting to remote glusterd at " + "%s", state->remote_host); + ret = dict_set_str (options, "remote-host", state->remote_host); + if (ret) + goto out; - if (state->remote_port) - port = state->remote_port; + if (state->remote_port) + port = state->remote_port; - ret = dict_set_int32 (options, "remote-port", port); - if (ret) - goto out; + ret = dict_set_int32 (options, "remote-port", port); + if (ret) + goto out; - ret = dict_set_str (options, "transport.address-family", "inet/inet6"); - if (ret) - goto out; + ret = dict_set_str (options, "transport.address-family", + "inet"); + if (ret) + goto out; + } else { + gf_log ("cli", GF_LOG_DEBUG, "Connecting to glusterd using " + "default socket"); + ret = rpc_transport_unix_options_build + (&options, DEFAULT_GLUSTERD_SOCKFILE, 0); + if (ret) + goto out; + } rpc = rpc_clnt_new (options, this->ctx, this->name, 16); - if (!rpc) goto out; @@ -507,7 +555,7 @@ cli_rpc_init (struct cli_state *state) goto out; } - rpc_clnt_start (rpc); + ret = rpc_clnt_start (rpc); out: if (ret) { if (rpc) @@ -531,76 +579,15 @@ void cli_local_wipe (cli_local_t *local) { if (local) { + GF_FREE (local->get_vol.volname); + if (local->dict) + dict_unref (local->dict); GF_FREE (local); } return; } -/* If the path exists use realpath(3) to handle extra slashes and to resolve - * symlinks else strip the extra slashes in the path and return */ - -int -cli_canonicalize_path (char *path) -{ - struct stat sb = {0}; - int ret = -1; - char *tmppath = NULL; - char *dir = NULL; - char *tmpstr = NULL; - int path_len = 0; - - if (!path) - return ret; - - ret = stat (path, &sb); - if (ret == -1) { - /* Strip the extra slashes and return */ - tmppath = gf_strdup (path); - if (tmppath == NULL) { - ret = -1; - gf_log ("cli", GF_LOG_ERROR, "Out of memory."); - goto out; - } - bzero (path, strlen(path)); - path[0] = '/'; - dir = strtok_r(tmppath, "/", &tmpstr); - while (dir) { - strncpy ((path + path_len + 1), dir, strlen(dir)); - path_len = strlen (path); - dir = strtok_r(NULL, "/", &tmpstr); - if (dir) - strncpy((path + path_len), "/", 1); - } - if (path_len == 0) - path[1] = '\0'; - else - path[path_len] = '\0'; - ret = 0; - goto out; - } else { - tmppath = gf_strdup(path); - if (tmppath == NULL) { - ret = -1; - gf_log ("cli", GF_LOG_ERROR, "Out of memory."); - goto out; - } - if (realpath (tmppath, path) == NULL) { - cli_out ("Path manipulation failed: %s", - strerror(errno)); - gf_log ("cli", GF_LOG_ERROR, "Path manipulation " - "failed: %s", strerror(errno)); - ret = -1; - goto out; - } - ret = 0; - } -out: - if (tmppath) - GF_FREE(tmppath); - return ret; -} - struct cli_state *global_state; int @@ -610,13 +597,19 @@ main (int argc, char *argv[]) int ret = -1; glusterfs_ctx_t *ctx = NULL; - ret = glusterfs_globals_init (); + ctx = glusterfs_ctx_new (); + if (!ctx) + return ENOMEM; + +#ifdef DEBUG + gf_mem_acct_enable_set (ctx); +#endif + + ret = glusterfs_globals_init (ctx); if (ret) return ret; - ctx = glusterfs_ctx_get (); - if (!ctx) - return ENOMEM; + THIS->ctx = ctx; ret = glusterfs_ctx_defaults_init (ctx); if (ret) @@ -633,7 +626,7 @@ main (int argc, char *argv[]) if (ret) goto out; - ret = logging_init (&state); + ret = logging_init (ctx, &state); if (ret) goto out; |
