int
handle_quit (struct cli_context *ctx)
{
        cli_disconnect (ctx);

        if (ctx->argv) {
                free (ctx->argv);
        }

        free (ctx->options);
        free (ctx);

        exit (EXIT_SUCCESS);
}
Esempio n. 2
0
void cli_cleanup(cli_info_t *cli)
{
    if (0 > cli->state || CLI_TERM < cli->state) {
        TEST_ERROR(("Bad rank %d state %d", cli_rank(cli), cli->state));
        test_abort = true;
        return;
    }
    switch( cli->next_state[cli->state] ){
    case CLI_FORKED:
        break;
    case CLI_CONNECTED:
        /* error - means that process terminated w/o calling finalize */
        if (!test_abort) {
            TEST_ERROR(("rank %d with state %d unexpectedly terminated.", cli_rank(cli), cli->state));
        }
        cli->state = CLI_TERM;
        test_abort = true;
        break;
    case CLI_FIN:
        /* error - means that process terminated w/o calling finalize */
        if (!test_abort) {
            TEST_ERROR(("rank %d with state %d unexpectedly terminated.", cli_rank(cli), cli->state));
        }
        cli_finalize(cli);
        cli_cleanup(cli);
        test_abort = true;
        break;
    case CLI_DISCONN:
        cli_disconnect(cli);
        cli_cleanup(cli);
        break;
    case CLI_TERM:
        cli_terminate(cli);
        break;
    default:
        TEST_ERROR(("Bad rank %d next state %d", cli_rank(cli), cli->next_state[cli->state]));
        test_abort = true;
        return;
    }
}
int
cli_connect (struct cli_context *ctx)
{
        int argc = ctx->argc;
        char **argv = ctx->argv;
        int ret;
        int opt;
        int option_index;
        struct gluster_url *url = NULL;
        glfs_t *fs = NULL;
        uint16_t port = GLUSTER_DEFAULT_PORT;
        struct xlator_option *xlator_options = NULL;
        struct xlator_option *option;

        while ((opt = getopt_long (argc, argv, "o:p:", connect_options, &option_index)) != -1) {
                switch (opt) {
                        case 'o':
                                option = parse_xlator_option (optarg);
                                if (option == NULL) {
                                        error (0, errno, "%s", optarg);
                                        goto err;
                                }

                                if (append_xlator_option (&xlator_options, option) == -1) {
                                        error (0, errno, "append_xlator_option: %s", optarg);
                                        goto err;
                                }

                                break;
                        case 'p':
                                port = strtoport (optarg);
                                if (port == 0) {
                                        goto err;
                                }

                                break;
                        default:
                                goto err;
                }
        }

        ret = gluster_parse_url (argv[argc - 1], &url);
        if (ret == -1) {
                printf ("Usage: %s [OPTION]... URL\n"
                        "Connect to a Gluster volume for this session.\n\n"
                        "  -o, --xlator-option=OPTION   specify a translator option for the\n"
                        "                               connection. Multiple options are supported\n"
                        "                               and take the form xlator.key=value.\n"
                        "  -p, --port=PORT              specify the port on which to connect\n",
                        argv[0]);
                goto err;
        }

        url->port = port;

        ret = gluster_getfs (&fs, url);
        if (ret == -1) {
                error (0, errno, "failed to connect to %s/%s", url->host, url->volume);
                goto err;
        }

        ret = apply_xlator_options (fs, &xlator_options);
        if (ret == -1) {
                error (0, errno, "failed to apply translator options");
                goto err;
        }

        ret = cli_disconnect (ctx);
        if (ret == -1) {
                error (0, 0, "failed to terminate previous connection");
                goto err;
        }

        ctx->fs = fs;
        ctx->url = url;

        // TODO(craigcabrey): Look into using asprintf here.
        // 5 is the length of the string format: (%s/%s)
        size_t length = strlen (ctx->url->host) + strlen (ctx->url->volume) + 5;

        ctx->conn_str = malloc (length);
        if (ctx->conn_str == NULL) {
                error (0, errno, "malloc");
                handle_quit (ctx);
        }

        snprintf (ctx->conn_str,
                        length,
                        "(%s/%s)",
                        ctx->url->host,
                        ctx->url->volume);

        goto out;

err:
        ret = -1;
        gluster_url_free (url);
out:
        return ret;
}