From: Li Wang Date: Tue, 27 Nov 2018 03:35:53 +0000 (+0000) Subject: tools/rados: always call rados.shutdown() before exit() X-Git-Tag: v12.2.13~97^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=638abda05db7c0addd8ddeef530085a749758ae1;p=ceph.git tools/rados: always call rados.shutdown() before exit() When connected to ceph cluster, if call exit(1) directly, will cause the finisher thread segmentation fault as follows, Caught signal (Segmentation fault) **in thread 7f45377fe700 thread_name:fn_anonymous run 'rados mapext' without other arguments could easily get it. This patch fixes it by calling rados.shutdown() before exit() Fixes: http://tracker.ceph.com/issues/36732 Signed-off-by: Li Wang (cherry picked from commit 2c149262888c50beb9f480a6cd78f77fd5920d1c) Conflicts: src/tools/rados/rados.cc - removed conflicts in error conditions - no clearomap option - no set-chunk option - no tier-promote option - no touch option - no stat2 option - no unset-manifest - removed std::make_unique as not defined --- diff --git a/src/tools/rados/rados.cc b/src/tools/rados/rados.cc index a8f57b23c255..95c1f79a0315 100644 --- a/src/tools/rados/rados.cc +++ b/src/tools/rados/rados.cc @@ -1945,19 +1945,19 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, ret = rados.init_with_context(g_ceph_context); if (ret < 0) { cerr << "couldn't initialize rados: " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } ret = rados.connect(); if (ret) { cerr << "couldn't connect to cluster: " << cpp_strerror(ret) << std::endl; - ret = -1; - goto out; + return 1; } if (create_pool && !pool_name) { cerr << "--create-pool requested but pool_name was not specified!" << std::endl; - usage_exit(); + usage(cerr); + return 1; } if (create_pool) { @@ -1965,7 +1965,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { cerr << "error creating pool " << pool_name << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } } @@ -1973,8 +1973,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, boost::optional pgid(i != opts.end(), pg_t()); if (pgid && (!pgid->parse(i->second.c_str()) || (pool_name && rados.pool_lookup(pool_name) != pgid->pool()))) { cerr << "invalid pgid" << std::endl; - ret = -1; - goto out; + return 1; } // open io context. @@ -1984,7 +1983,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, cerr << "error opening pool " << (pool_name ? pool_name : std::string("with id ") + std::to_string(pgid->pool())) << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } // align op_size @@ -1994,7 +1993,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { cerr << "error checking pool alignment requirement" << cpp_strerror(ret) << std::endl; - goto out; + return 1; } if (requires) { @@ -2003,7 +2002,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { cerr << "error getting pool alignment" << cpp_strerror(ret) << std::endl; - goto out; + return 1; } const uint64_t prev_op_size = op_size; @@ -2018,9 +2017,9 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (opts.find("striper") != opts.end()) { ret = RadosStriper::striper_create(io_ctx, &striper); if (0 != ret) { - cerr << "error opening pool " << pool_name << " with striper interface: " - << cpp_strerror(ret) << std::endl; - goto out; + cerr << "error opening pool " << pool_name << " with striper interface: " + << cpp_strerror(ret) << std::endl; + return 1; } use_striper = true; } @@ -2030,20 +2029,18 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (snapname) { if (!pool_name) { cerr << "pool name must be specified with --snap" << std::endl; - ret = -1; - goto out; + return 1; } ret = io_ctx.snap_lookup(snapname, &snapid); if (ret < 0) { cerr << "error looking up snap '" << snapname << "': " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } } if (oloc.size()) { if (!pool_name) { cerr << "pool name must be specified with --object_locator" << std::endl; - ret = -1; - goto out; + return 1; } io_ctx.locator_set_key(oloc); } @@ -2051,8 +2048,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (opts.find("namespace") != opts.end()) { if (!pool_name) { cerr << "pool name must be specified with --namespace" << std::endl; - ret = -1; - goto out; + return 1; } io_ctx.set_namespace(nspace); // Use wildcard if --all specified and --default NOT specified @@ -2063,15 +2059,14 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (snapid != CEPH_NOSNAP) { if (!pool_name) { cerr << "pool name must be specified with --snapid" << std::endl; - ret = -1; - goto out; + return 1; } string name; ret = io_ctx.snap_get_name(snapid, &name); if (ret < 0) { cerr << "snapid " << snapid << " doesn't exist in pool " << io_ctx.get_pool_name() << std::endl; - goto out; + return 1; } io_ctx.snap_set_read(snapid); cout << "selected snap " << snapid << " '" << name << "'" << std::endl; @@ -2085,7 +2080,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, ret = rados.pool_list(vec); if (ret < 0) { cerr << "error listing pools: " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } for (list::iterator i = vec.begin(); i != vec.end(); ++i) cout << *i << std::endl; @@ -2098,7 +2093,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, ret = rados.pool_list(vec); if (ret < 0) { cerr << "error listing pools: " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } } else { vec.push_back(pool_name); @@ -2108,7 +2103,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, ret = rados.get_pool_stats(vec, stats); if (ret < 0) { cerr << "error fetching pool stats: " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } TextTable tab; @@ -2183,7 +2178,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, ret = rados.cluster_stat(tstats); if (ret < 0) { cerr << "error getting total cluster usage: " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } if (!formatter) { cout << std::endl; @@ -2209,8 +2204,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, else if (strcmp(nargs[0], "ls") == 0) { if (!pool_name && !pgid) { cerr << "either pool name or pg id needs to be specified" << std::endl; - ret = -1; - goto out; + return 1; } if (wildcard) @@ -2270,8 +2264,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, } catch (const std::exception& e) { cerr << e.what() << std::endl; - ret = -1; - goto out; + return 1; } } if (formatter) { @@ -2303,14 +2296,16 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, << " to " << new_auid << std::endl; } else if (strcmp(nargs[0], "mapext") == 0) { - if (!pool_name || nargs.size() < 2) - usage_exit(); + if (!pool_name || nargs.size() < 2) { + usage(cerr); + return 1; + } string oid(nargs[1]); std::map m; ret = io_ctx.mapext(oid, 0, -1, m); if (ret < 0) { cerr << "mapext error on " << pool_name << "/" << oid << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } std::map::iterator iter; for (iter = m.begin(); iter != m.end(); ++iter) { @@ -2318,8 +2313,10 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, } } else if (strcmp(nargs[0], "stat") == 0) { - if (!pool_name || nargs.size() < 2) - usage_exit(); + if (!pool_name || nargs.size() < 2) { + usage(cerr); + return 1; + } string oid(nargs[1]); uint64_t size; time_t mtime; @@ -2331,7 +2328,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { cerr << " error stat-ing " << pool_name << "/" << oid << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } else { utime_t t(mtime, 0); cout << pool_name << "/" << oid @@ -2339,35 +2336,43 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, } } else if (strcmp(nargs[0], "get") == 0) { - if (!pool_name || nargs.size() < 3) - usage_exit(); + if (!pool_name || nargs.size() < 3) { + usage(cerr); + return 1; + } ret = do_get(io_ctx, striper, nargs[1], nargs[2], op_size, use_striper); if (ret < 0) { cerr << "error getting " << pool_name << "/" << nargs[1] << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } } else if (strcmp(nargs[0], "put") == 0) { - if (!pool_name || nargs.size() < 3) - usage_exit(); + if (!pool_name || nargs.size() < 3) { + usage(cerr); + return 1; + } ret = do_put(io_ctx, striper, nargs[1], nargs[2], op_size, obj_offset, use_striper); if (ret < 0) { cerr << "error putting " << pool_name << "/" << nargs[1] << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } } else if (strcmp(nargs[0], "append") == 0) { - if (!pool_name || nargs.size() < 3) - usage_exit(); + if (!pool_name || nargs.size() < 3) { + usage(cerr); + return 1; + } ret = do_append(io_ctx, striper, nargs[1], nargs[2], op_size, use_striper); if (ret < 0) { cerr << "error appending " << pool_name << "/" << nargs[1] << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } } else if (strcmp(nargs[0], "truncate") == 0) { - if (!pool_name || nargs.size() < 3) - usage_exit(); + if (!pool_name || nargs.size() < 3) { + usage(cerr); + return 1; + } string oid(nargs[1]); char* endptr = NULL; @@ -2375,11 +2380,12 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (*endptr) { cerr << "Invalid value for size: '" << nargs[2] << "'" << std::endl; ret = -EINVAL; - goto out; + return 1; } if (size < 0) { cerr << "error, cannot truncate to negative value" << std::endl; - usage_exit(); + usage(cerr); + return 1; } if (use_striper) { ret = striper.trunc(oid, size); @@ -2395,8 +2401,10 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, } } else if (strcmp(nargs[0], "setxattr") == 0) { - if (!pool_name || nargs.size() < 3 || nargs.size() > 4) - usage_exit(); + if (!pool_name || nargs.size() < 3 || nargs.size() > 4) { + usage(cerr); + return 1; + } string oid(nargs[1]); string attr_name(nargs[2]); @@ -2408,7 +2416,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, do { ret = bl.read_fd(STDIN_FILENO, 1024); // from stdin if (ret < 0) - goto out; + return 1; } while (ret > 0); } @@ -2419,14 +2427,16 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, } if (ret < 0) { cerr << "error setting xattr " << pool_name << "/" << oid << "/" << attr_name << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } else ret = 0; } else if (strcmp(nargs[0], "getxattr") == 0) { - if (!pool_name || nargs.size() < 3) - usage_exit(); + if (!pool_name || nargs.size() < 3) { + usage(cerr); + return 1; + } string oid(nargs[1]); string attr_name(nargs[2]); @@ -2439,15 +2449,17 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, } if (ret < 0) { cerr << "error getting xattr " << pool_name << "/" << oid << "/" << attr_name << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } else ret = 0; string s(bl.c_str(), bl.length()); cout << s; } else if (strcmp(nargs[0], "rmxattr") == 0) { - if (!pool_name || nargs.size() < 3) - usage_exit(); + if (!pool_name || nargs.size() < 3) { + usage(cerr); + return 1; + } string oid(nargs[1]); string attr_name(nargs[2]); @@ -2459,11 +2471,13 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, } if (ret < 0) { cerr << "error removing xattr " << pool_name << "/" << oid << "/" << attr_name << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } } else if (strcmp(nargs[0], "listxattr") == 0) { - if (!pool_name || nargs.size() < 2) - usage_exit(); + if (!pool_name || nargs.size() < 2) { + usage(cerr); + return 1; + } string oid(nargs[1]); map attrset; @@ -2475,7 +2489,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, } if (ret < 0) { cerr << "error getting xattr set " << pool_name << "/" << oid << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } for (map::iterator iter = attrset.begin(); @@ -2483,8 +2497,10 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, cout << iter->first << std::endl; } } else if (strcmp(nargs[0], "getomapheader") == 0) { - if (!pool_name || nargs.size() < 2) - usage_exit(); + if (!pool_name || nargs.size() < 2) { + usage(cerr); + return 1; + } string oid(nargs[1]); string outfile; @@ -2497,7 +2513,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { cerr << "error getting omap header " << pool_name << "/" << oid << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } else { if (!outfile.empty()) { cerr << "Writing to " << outfile << std::endl; @@ -2510,8 +2526,10 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, ret = 0; } } else if (strcmp(nargs[0], "setomapheader") == 0) { - if (!pool_name || nargs.size() < 3) - usage_exit(); + if (!pool_name || nargs.size() < 3) { + usage(cerr); + return 1; + } string oid(nargs[1]); string val(nargs[2]); @@ -2523,14 +2541,15 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { cerr << "error setting omap value " << pool_name << "/" << oid << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } else { ret = 0; } } else if (strcmp(nargs[0], "setomapval") == 0) { uint32_t min_args = (omap_key_valid ? 2 : 3); if (!pool_name || nargs.size() < min_args || nargs.size() > min_args + 1) { - usage_exit(); + usage(cerr); + return 1; } string oid(nargs[1]); @@ -2547,7 +2566,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, do { ret = bl.read_fd(STDIN_FILENO, 1024); // from stdin if (ret < 0) { - goto out; + return 1; } } while (ret > 0); } @@ -2559,14 +2578,15 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { cerr << "error setting omap value " << pool_name << "/" << oid << "/" << omap_key_pretty << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } else { ret = 0; } } else if (strcmp(nargs[0], "getomapval") == 0) { uint32_t min_args = (omap_key_valid ? 2 : 3); if (!pool_name || nargs.size() < min_args || nargs.size() > min_args + 1) { - usage_exit(); + usage(cerr); + return 1; } string oid(nargs[1]); @@ -2588,7 +2608,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { cerr << "error getting omap value " << pool_name << "/" << oid << "/" << omap_key_pretty << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } else { ret = 0; } @@ -2606,13 +2626,13 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, } else { cout << "No such key: " << pool_name << "/" << oid << "/" << omap_key_pretty << std::endl; - ret = -1; - goto out; + return 1; } } else if (strcmp(nargs[0], "rmomapkey") == 0) { uint32_t num_args = (omap_key_valid ? 2 : 3); if (!pool_name || nargs.size() != num_args) { - usage_exit(); + usage(cerr); + return 1; } string oid(nargs[1]); @@ -2627,13 +2647,15 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { cerr << "error removing omap key " << pool_name << "/" << oid << "/" << omap_key_pretty << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } else { ret = 0; } } else if (strcmp(nargs[0], "listomapvals") == 0) { - if (!pool_name || nargs.size() < 2) - usage_exit(); + if (!pool_name || nargs.size() < 2) { + usage(cerr); + return 1; + } string oid(nargs[1]); string last_read = ""; @@ -2669,11 +2691,15 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, ret = 0; } else if (strcmp(nargs[0], "cp") == 0) { - if (!pool_name) - usage_exit(); + if (!pool_name) { + usage(cerr); + return 1; + } - if (nargs.size() < 2 || nargs.size() > 3) - usage_exit(); + if (nargs.size() < 2 || nargs.size() > 3) { + usage(cerr); + return 1; + } const char *target = target_pool_name; if (!target) @@ -2683,8 +2709,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (nargs.size() < 3) { if (strcmp(target, pool_name) == 0) { cerr << "cannot copy object into itself" << std::endl; - ret = -1; - goto out; + return 1; } target_obj = nargs[1]; } else { @@ -2697,7 +2722,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { cerr << "error opening target pool " << target << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } if (target_oloc.size()) { target_ctx.locator_set_key(target_oloc); @@ -2709,11 +2734,13 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, ret = do_copy(io_ctx, nargs[1], target_ctx, target_obj); if (ret < 0) { cerr << "error copying " << pool_name << "/" << nargs[1] << " => " << target << "/" << target_obj << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } } else if (strcmp(nargs[0], "rm") == 0) { - if (!pool_name || nargs.size() < 2) - usage_exit(); + if (!pool_name || nargs.size() < 2) { + usage(cerr); + return 1; + } vector::iterator iter = nargs.begin(); ++iter; for (; iter != nargs.end(); ++iter) { @@ -2734,18 +2761,20 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { string name = (nspace.size() ? nspace + "/" : "" ) + oid; cerr << "error removing " << pool_name << ">" << name << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } } } else if (strcmp(nargs[0], "create") == 0) { - if (!pool_name || nargs.size() < 2) - usage_exit(); + if (!pool_name || nargs.size() < 2) { + usage(cerr); + return 1; + } string oid(nargs[1]); ret = io_ctx.create(oid, true); if (ret < 0) { cerr << "error creating " << pool_name << "/" << oid << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } } @@ -2876,15 +2905,16 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, } else if (strcmp(nargs[0], "cppool") == 0) { bool force = nargs.size() == 4 && !strcmp(nargs[3], "--yes-i-really-mean-it"); - if (nargs.size() != 3 && !(nargs.size() == 4 && force)) - usage_exit(); + if (nargs.size() != 3 && !(nargs.size() == 4 && force)) { + usage(cerr); + return 1; + } const char *src_pool = nargs[1]; const char *target_pool = nargs[2]; if (strcmp(src_pool, target_pool) == 0) { cerr << "cannot copy pool into itself" << std::endl; - ret = -1; - goto out; + return 1; } cerr << "WARNING: pool copy does not preserve user_version, which some " @@ -2906,7 +2936,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { cerr << "error copying pool " << src_pool << " => " << target_pool << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } cout << "successfully copied pool " << nargs[1] << std::endl; } @@ -2933,21 +2963,22 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, } } else if (strcmp(nargs[0], "purge") == 0) { - if (nargs.size() < 2) - usage_exit(); + if (nargs.size() < 2) { + usage(cerr); + return 1; + } if (nargs.size() < 3 || strcmp(nargs[2], "--yes-i-really-really-mean-it") != 0) { cerr << "WARNING:\n" << " This will PERMANENTLY DESTROY all objects from a pool with no way back.\n" << " To confirm, follow pool with --yes-i-really-really-mean-it" << std::endl; - ret = -1; - goto out; + return 1; } ret = rados.ioctx_create(nargs[1], io_ctx); if (ret < 0) { cerr << "error pool " << nargs[1] << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } io_ctx.set_namespace(all_nspaces); io_ctx.set_osdmap_full_try(); @@ -2962,8 +2993,10 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, } } else if (strcmp(nargs[0], "lssnap") == 0) { - if (!pool_name || nargs.size() != 1) - usage_exit(); + if (!pool_name || nargs.size() != 1) { + usage(cerr); + return 1; + } vector snaps; io_ctx.snap_list(&snaps); @@ -2997,53 +3030,60 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, } else if (strcmp(nargs[0], "mksnap") == 0) { - if (!pool_name || nargs.size() < 2) - usage_exit(); + if (!pool_name || nargs.size() < 2) { + usage(cerr); + return 1; + } ret = io_ctx.snap_create(nargs[1]); if (ret < 0) { cerr << "error creating pool " << pool_name << " snapshot " << nargs[1] << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } cout << "created pool " << pool_name << " snap " << nargs[1] << std::endl; } else if (strcmp(nargs[0], "rmsnap") == 0) { - if (!pool_name || nargs.size() < 2) - usage_exit(); + if (!pool_name || nargs.size() < 2) { + usage(cerr); + return 1; + } ret = io_ctx.snap_remove(nargs[1]); if (ret < 0) { cerr << "error removing pool " << pool_name << " snapshot " << nargs[1] << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } cout << "removed pool " << pool_name << " snap " << nargs[1] << std::endl; } else if (strcmp(nargs[0], "rollback") == 0) { - if (!pool_name || nargs.size() < 3) - usage_exit(); + if (!pool_name || nargs.size() < 3) { + usage(cerr); + return 1; + } ret = io_ctx.snap_rollback(nargs[1], nargs[2]); if (ret < 0) { cerr << "error rolling back pool " << pool_name << " to snapshot " << nargs[1] << cpp_strerror(ret) << std::endl; - goto out; + return 1; } cout << "rolled back pool " << pool_name << " to snapshot " << nargs[2] << std::endl; } else if (strcmp(nargs[0], "bench") == 0) { - if (!pool_name || nargs.size() < 3) - usage_exit(); + if (!pool_name || nargs.size() < 3) { + usage(cerr); + return 1; + } char* endptr = NULL; int seconds = strtol(nargs[1], &endptr, 10); if (*endptr) { cerr << "Invalid value for seconds: '" << nargs[1] << "'" << std::endl; - ret = -EINVAL; - goto out; + return 1; } int operation = 0; if (strcmp(nargs[2], "write") == 0) @@ -3052,21 +3092,21 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, operation = OP_SEQ_READ; else if (strcmp(nargs[2], "rand") == 0) operation = OP_RAND_READ; - else - usage_exit(); + else { + usage(cerr); + return 1; + } if (operation != OP_WRITE) { if (block_size_specified) { cerr << "-b|--block_size option can be used only with 'write' bench test" << std::endl; - ret = -EINVAL; - goto out; + return 1; } if (bench_write_dest != 0) { cerr << "--write-object, --write-omap and --write-xattr options can " "only be used with the 'write' bench test" << std::endl; - ret = -EINVAL; - goto out; + return 1; } } else if (bench_write_dest == 0) { @@ -3076,8 +3116,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (!formatter && output) { cerr << "-o|--output option can only be used with '--format' option" << std::endl; - ret = -EINVAL; - goto out; + return 1; } RadosBencher bencher(g_ceph_context, rados, io_ctx); bencher.set_show_time(show_time); @@ -3106,8 +3145,10 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, delete outstream; } else if (strcmp(nargs[0], "cleanup") == 0) { - if (!pool_name) - usage_exit(); + if (!pool_name) { + usage(cerr); + return 1; + } if (wildcard) io_ctx.set_namespace(all_nspaces); RadosBencher bencher(g_ceph_context, rados, io_ctx); @@ -3116,8 +3157,10 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, cerr << "error during cleanup: " << cpp_strerror(ret) << std::endl; } else if (strcmp(nargs[0], "watch") == 0) { - if (!pool_name || nargs.size() < 2) - usage_exit(); + if (!pool_name || nargs.size() < 2) { + usage(cerr); + return 1; + } string oid(nargs[1]); RadosWatchCtx ctx(io_ctx, oid.c_str()); uint64_t cookie; @@ -3132,8 +3175,10 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, } } else if (strcmp(nargs[0], "notify") == 0) { - if (!pool_name || nargs.size() < 3) - usage_exit(); + if (!pool_name || nargs.size() < 3) { + usage(cerr); + return 1; + } string oid(nargs[1]); string msg(nargs[2]); bufferlist bl, replybl; @@ -3163,30 +3208,35 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, } } } else if (strcmp(nargs[0], "set-alloc-hint") == 0) { - if (!pool_name || nargs.size() < 4) - usage_exit(); + if (!pool_name || nargs.size() < 4) { + usage(cerr); + return 1; + } string err; string oid(nargs[1]); uint64_t expected_object_size = strict_strtoll(nargs[2], 10, &err); if (!err.empty()) { cerr << "couldn't parse expected_object_size: " << err << std::endl; - usage_exit(); + usage(cerr); + return 1; } uint64_t expected_write_size = strict_strtoll(nargs[3], 10, &err); if (!err.empty()) { cerr << "couldn't parse expected_write_size: " << err << std::endl; - usage_exit(); + usage(cerr); + return 1; } ret = io_ctx.set_alloc_hint(oid, expected_object_size, expected_write_size); if (ret < 0) { cerr << "error setting alloc-hint " << pool_name << "/" << oid << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } } else if (strcmp(nargs[0], "load-gen") == 0) { if (!pool_name) { cerr << "error: must specify pool" << std::endl; - usage_exit(); + usage(cerr); + return 1; } LoadGen lg(&rados); if (min_obj_len) @@ -3215,21 +3265,23 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, ret = lg.bootstrap(pool_name); if (ret < 0) { cerr << "load-gen bootstrap failed" << std::endl; - exit(1); + return 1; } cout << "load-gen will run " << lg.run_length << " seconds" << std::endl; lg.run(); lg.cleanup(); } else if (strcmp(nargs[0], "listomapkeys") == 0) { - if (!pool_name || nargs.size() < 2) - usage_exit(); + if (!pool_name || nargs.size() < 2) { + usage(cerr); + return 1; + } set out_keys; ret = io_ctx.omap_get_keys(nargs[1], "", LONG_MAX, &out_keys); if (ret < 0) { cerr << "error getting omap key set " << pool_name << "/" << nargs[1] << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } for (set::iterator iter = out_keys.begin(); @@ -3237,16 +3289,20 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, cout << *iter << std::endl; } } else if (strcmp(nargs[0], "lock") == 0) { - if (!pool_name) - usage_exit(); + if (!pool_name) { + usage(cerr); + return 1; + } if (!formatter) { formatter = new JSONFormatter(pretty_format); } ret = do_lock_cmd(nargs, opts, &io_ctx, formatter); } else if (strcmp(nargs[0], "listwatchers") == 0) { - if (!pool_name || nargs.size() < 2) - usage_exit(); + if (!pool_name || nargs.size() < 2) { + usage(cerr); + return 1; + } string oid(nargs[1]); std::list lw; @@ -3254,7 +3310,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, ret = io_ctx.list_watchers(oid, &lw); if (ret < 0) { cerr << "error listing watchers " << pool_name << "/" << oid << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } else ret = 0; @@ -3263,8 +3319,10 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, cout << "watcher=" << i->addr << " client." << i->watcher_id << " cookie=" << i->cookie << std::endl; } } else if (strcmp(nargs[0], "listsnaps") == 0) { - if (!pool_name || nargs.size() < 2) - usage_exit(); + if (!pool_name || nargs.size() < 2) { + usage(cerr); + return 1; + } string oid(nargs[1]); snap_set_t ls; @@ -3273,7 +3331,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, ret = io_ctx.list_snaps(oid, &ls); if (ret < 0) { cerr << "error listing snap shots " << pool_name << "/" << oid << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } else ret = 0; @@ -3402,8 +3460,10 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, } ret = do_get_inconsistent_cmd(nargs, rados, *formatter); } else if (strcmp(nargs[0], "cache-flush") == 0) { - if (!pool_name || nargs.size() < 2) - usage_exit(); + if (!pool_name || nargs.size() < 2) { + usage(cerr); + return 1; + } string oid(nargs[1]); if (with_clones) { snap_set_t ls; @@ -3412,7 +3472,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { cerr << "error listing snapshots " << pool_name << "/" << oid << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } for (std::vector::iterator ci = ls.clones.begin(); ci != ls.clones.end(); ++ci) { @@ -3423,7 +3483,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { cerr << "error from cache-flush " << oid << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } } } else { @@ -3431,12 +3491,14 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { cerr << "error from cache-flush " << oid << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } } } else if (strcmp(nargs[0], "cache-try-flush") == 0) { - if (!pool_name || nargs.size() < 2) - usage_exit(); + if (!pool_name || nargs.size() < 2) { + usage(cerr); + return 1; + } string oid(nargs[1]); if (with_clones) { snap_set_t ls; @@ -3445,7 +3507,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { cerr << "error listing snapshots " << pool_name << "/" << oid << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } for (std::vector::iterator ci = ls.clones.begin(); ci != ls.clones.end(); ++ci) { @@ -3456,7 +3518,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { cerr << "error from cache-flush " << oid << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } } } else { @@ -3464,12 +3526,14 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { cerr << "error from cache-flush " << oid << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } } } else if (strcmp(nargs[0], "cache-evict") == 0) { - if (!pool_name || nargs.size() < 2) - usage_exit(); + if (!pool_name || nargs.size() < 2) { + usage(cerr); + return 1; + } string oid(nargs[1]); if (with_clones) { snap_set_t ls; @@ -3478,7 +3542,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { cerr << "error listing snapshots " << pool_name << "/" << oid << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } for (std::vector::iterator ci = ls.clones.begin(); ci != ls.clones.end(); ++ci) { @@ -3489,7 +3553,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { cerr << "error from cache-flush " << oid << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } } } else { @@ -3497,30 +3561,36 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { cerr << "error from cache-flush " << oid << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } } } else if (strcmp(nargs[0], "cache-flush-evict-all") == 0) { - if (!pool_name) - usage_exit(); + if (!pool_name) { + usage(cerr); + return 1; + } ret = do_cache_flush_evict_all(io_ctx, true); if (ret < 0) { cerr << "error from cache-flush-evict-all: " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } } else if (strcmp(nargs[0], "cache-try-flush-evict-all") == 0) { - if (!pool_name) - usage_exit(); + if (!pool_name) { + usage(cerr); + return 1; + } ret = do_cache_flush_evict_all(io_ctx, false); if (ret < 0) { cerr << "error from cache-try-flush-evict-all: " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } } else if (strcmp(nargs[0], "set-redirect") == 0) { - if (!pool_name) - usage_exit(); + if (!pool_name) { + usage(cerr); + return 1; + } const char *target = target_pool_name; if (!target) @@ -3530,8 +3600,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (nargs.size() < 3) { if (strcmp(target, pool_name) == 0) { cerr << "cannot copy object into itself" << std::endl; - ret = -1; - goto out; + return 1; } target_obj = nargs[1]; } else { @@ -3552,12 +3621,13 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, ret = io_ctx.operate(nargs[1], &op); if (ret < 0) { cerr << "error set-redirect " << pool_name << "/" << nargs[1] << " => " << target << "/" << target_obj << ": " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } } else if (strcmp(nargs[0], "export") == 0) { // export [filename] if (!pool_name || nargs.size() > 2) { - usage_exit(); + usage(cerr); + return 1; } int file_fd; @@ -3568,8 +3638,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (file_fd < 0) { cerr << "Error opening '" << nargs[1] << "': " << cpp_strerror(file_fd) << std::endl; - ret = file_fd; - goto out; + return 1; } } @@ -3582,12 +3651,13 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { cerr << "error from export: " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } } else if (strcmp(nargs[0], "import") == 0) { // import [--no-overwrite] [--dry-run] if (!pool_name || nargs.size() > 4 || nargs.size() < 2) { - usage_exit(); + usage(cerr); + return 1; } // Last arg is the filename @@ -3605,8 +3675,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, dry_run = true; } else { std::cerr << "Invalid argument '" << arg << "'" << std::endl; - ret = -EINVAL; - goto out; + return 1; } } @@ -3618,8 +3687,7 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (file_fd < 0) { cerr << "Error opening '" << filename << "': " << cpp_strerror(file_fd) << std::endl; - ret = file_fd; - goto out; + return 1; } } @@ -3632,12 +3700,11 @@ static int rados_tool_common(const std::map < std::string, std::string > &opts, if (ret < 0) { cerr << "error from import: " << cpp_strerror(ret) << std::endl; - goto out; + return 1; } } else { cerr << "unrecognized command " << nargs[0] << "; -h or --help for usage" << std::endl; ret = -EINVAL; - goto out; } if (ret < 0)