]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/cmdparse.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2013 Inktank Storage, Inc.
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public
10 * License version 2, as published by the Free Software
11 * Foundation. See file COPYING.
15 #include "include/common_fwd.h"
16 #include "common/cmdparse.h"
17 #include "common/Formatter.h"
18 #include "common/debug.h"
19 #include "common/strtol.h"
20 #include "json_spirit/json_spirit.h"
23 * Given a cmddesc like "foo baz name=bar,type=CephString",
24 * return the prefix "foo baz".
26 namespace TOPNSPC::common
{
27 std::string
cmddesc_get_prefix(const std::string_view
&cmddesc
)
29 string
tmp(cmddesc
); // FIXME: stringstream ctor can't take string_view :(
32 std::ostringstream result
;
34 while (std::getline(ss
, word
, ' ')) {
35 if (word
.find_first_of(",=") != string::npos
) {
49 using arg_desc_t
= std::map
<std::string_view
, std::string_view
>;
51 // Snarf up all the key=val,key=val pairs, put 'em in a dict.
52 arg_desc_t
cmddesc_get_args(const string_view cmddesc
)
55 for_each_substr(cmddesc
, ",", [&](auto kv
) {
56 // key=value; key by itself implies value is bool true
57 // name="name" means arg dict will be titled 'name'
58 auto equal
= kv
.find('=');
59 if (equal
== kv
.npos
) {
60 // it should be the command
63 auto key
= kv
.substr(0, equal
);
64 auto val
= kv
.substr(equal
+ 1);
70 std::string
cmddesc_get_prenautilus_compat(const std::string
&cmddesc
)
72 std::vector
<std::string
> out
;
73 stringstream
ss(cmddesc
);
76 while (std::getline(ss
, word
, ' ')) {
77 // if no , or =, must be a plain word to put out
78 if (word
.find_first_of(",=") == string::npos
) {
82 auto desckv
= cmddesc_get_args(word
);
83 auto j
= desckv
.find("type");
84 if (j
!= desckv
.end() && j
->second
== "CephBool") {
85 // Instruct legacy clients or mons to send --foo-bar string in place
86 // of a 'true'/'false' value
87 std::ostringstream oss
;
88 oss
<< std::string("--") << desckv
["name"];
89 std::string val
= oss
.str();
90 std::replace(val
.begin(), val
.end(), '_', '-');
91 desckv
["type"] = "CephChoices";
92 desckv
["strings"] = val
;
93 std::ostringstream fss
;
94 for (auto k
= desckv
.begin(); k
!= desckv
.end(); ++k
) {
95 if (k
!= desckv
.begin()) {
98 fss
<< k
->first
<< "=" << k
->second
;
100 out
.push_back(fss
.str());
110 for (auto i
= out
.begin(); i
!= out
.end(); ++i
) {
111 if (i
!= out
.begin()) {
120 * Read a command description list out of cmd, and dump it to f.
121 * A signature description is a set of space-separated words;
122 * see MonCommands.h for more info.
126 dump_cmd_to_json(Formatter
*f
, uint64_t features
, const string
& cmd
)
128 // put whole command signature in an already-opened container
129 // elements are: "name", meaning "the typeless name that means a literal"
130 // an object {} with key:value pairs representing an argument
132 stringstream
ss(cmd
);
135 while (std::getline(ss
, word
, ' ')) {
136 // if no , or =, must be a plain word to put out
137 if (word
.find_first_of(",=") == string::npos
) {
138 f
->dump_string("arg", word
);
141 // accumulate descriptor keywords in desckv
142 auto desckv
= cmddesc_get_args(word
);
143 // name the individual desc object based on the name key
144 f
->open_object_section(desckv
["name"]);
146 // Compatibility for pre-nautilus clients that don't know about CephBool
148 if (!HAVE_FEATURE(features
, SERVER_NAUTILUS
)) {
149 auto i
= desckv
.find("type");
150 if (i
!= desckv
.end() && i
->second
== "CephBool") {
151 // Instruct legacy clients to send --foo-bar string in place
152 // of a 'true'/'false' value
153 std::ostringstream oss
;
154 oss
<< std::string("--") << desckv
["name"];
156 std::replace(val
.begin(), val
.end(), '_', '-');
158 desckv
["type"] = "CephChoices";
159 desckv
["strings"] = val
;
163 // dump all the keys including name into the array
164 for (auto [key
, value
] : desckv
) {
165 f
->dump_string(key
, value
);
167 f
->close_section(); // attribute object for individual desc
172 dump_cmd_and_help_to_json(Formatter
*jf
,
174 const string
& secname
,
175 const string
& cmdsig
,
176 const string
& helptext
)
178 jf
->open_object_section(secname
);
179 jf
->open_array_section("sig");
180 dump_cmd_to_json(jf
, features
, cmdsig
);
181 jf
->close_section(); // sig array
182 jf
->dump_string("help", helptext
);
183 jf
->close_section(); // cmd
187 dump_cmddesc_to_json(Formatter
*jf
,
189 const string
& secname
,
190 const string
& cmdsig
,
191 const string
& helptext
,
192 const string
& module
,
196 jf
->open_object_section(secname
);
197 jf
->open_array_section("sig");
198 dump_cmd_to_json(jf
, features
, cmdsig
);
199 jf
->close_section(); // sig array
200 jf
->dump_string("help", helptext
);
201 jf
->dump_string("module", module
);
202 jf
->dump_string("perm", perm
);
203 jf
->dump_int("flags", flags
);
204 jf
->close_section(); // cmd
207 void cmdmap_dump(const cmdmap_t
&cmdmap
, Formatter
*f
)
209 ceph_assert(f
!= nullptr);
211 class dump_visitor
: public boost::static_visitor
<void>
214 std::string
const &key
;
216 dump_visitor(Formatter
*f_
, std::string
const &key_
)
221 void operator()(const std::string
&operand
) const
223 f
->dump_string(key
, operand
);
226 void operator()(const bool &operand
) const
228 f
->dump_bool(key
, operand
);
231 void operator()(const int64_t &operand
) const
233 f
->dump_int(key
, operand
);
236 void operator()(const double &operand
) const
238 f
->dump_float(key
, operand
);
241 void operator()(const std::vector
<std::string
> &operand
) const
243 f
->open_array_section(key
);
244 for (const auto& i
: operand
) {
245 f
->dump_string("item", i
);
250 void operator()(const std::vector
<int64_t> &operand
) const
252 f
->open_array_section(key
);
253 for (const auto i
: operand
) {
254 f
->dump_int("item", i
);
259 void operator()(const std::vector
<double> &operand
) const
261 f
->open_array_section(key
);
262 for (const auto i
: operand
) {
263 f
->dump_float("item", i
);
269 //f->open_object_section("cmdmap");
270 for (const auto &i
: cmdmap
) {
271 boost::apply_visitor(dump_visitor(f
, i
.first
), i
.second
);
273 //f->close_section();
277 /** Parse JSON in vector cmd into a map from field to map of values
278 * (use mValue/mObject)
279 * 'cmd' should not disappear over lifetime of map
280 * 'mapp' points to the caller's map
281 * 'ss' captures any errors during JSON parsing; if function returns
282 * false, ss is valid */
285 cmdmap_from_json(const vector
<string
>& cmd
, cmdmap_t
*mapp
, stringstream
&ss
)
287 json_spirit::mValue v
;
290 // First, join all cmd strings
295 if (!json_spirit::read(fullcmd
, v
))
296 throw runtime_error("unparseable JSON " + fullcmd
);
297 if (v
.type() != json_spirit::obj_type
)
298 throw(runtime_error("not JSON object " + fullcmd
));
300 // allocate new mObject (map) to return
301 // make sure all contents are simple types (not arrays or objects)
302 json_spirit::mObject o
= v
.get_obj();
303 for (map
<string
, json_spirit::mValue
>::iterator it
= o
.begin();
304 it
!= o
.end(); ++it
) {
306 // ok, marshal it into our string->cmd_vartype map, or throw an
307 // exception if it's not a simple datatype. This is kind of
308 // annoying, since json_spirit has a boost::variant inside it
309 // already, but it's not public. Oh well.
311 switch (it
->second
.type()) {
313 case json_spirit::obj_type
:
315 throw(runtime_error("JSON array/object not allowed " + fullcmd
));
318 case json_spirit::array_type
:
320 // array is a vector of values. Unpack it to a vector
321 // of strings, doubles, or int64_t, the only types we handle.
322 const vector
<json_spirit::mValue
>& spvals
= it
->second
.get_array();
323 if (spvals
.empty()) {
324 // if an empty array is acceptable, the caller should always check for
325 // vector<string> if the expected value of "vector<int64_t>" in the
326 // cmdmap is missing.
327 (*mapp
)[it
->first
] = vector
<string
>();
328 } else if (spvals
.front().type() == json_spirit::str_type
) {
330 for (const auto& sv
: spvals
) {
331 if (sv
.type() != json_spirit::str_type
) {
332 throw(runtime_error("Can't handle arrays of multiple types"));
334 outv
.push_back(sv
.get_str());
336 (*mapp
)[it
->first
] = std::move(outv
);
337 } else if (spvals
.front().type() == json_spirit::int_type
) {
338 vector
<int64_t> outv
;
339 for (const auto& sv
: spvals
) {
340 if (spvals
.front().type() != json_spirit::int_type
) {
341 throw(runtime_error("Can't handle arrays of multiple types"));
343 outv
.push_back(sv
.get_int64());
345 (*mapp
)[it
->first
] = std::move(outv
);
346 } else if (spvals
.front().type() == json_spirit::real_type
) {
348 for (const auto& sv
: spvals
) {
349 if (spvals
.front().type() != json_spirit::real_type
) {
350 throw(runtime_error("Can't handle arrays of multiple types"));
352 outv
.push_back(sv
.get_real());
354 (*mapp
)[it
->first
] = std::move(outv
);
356 throw(runtime_error("Can't handle arrays of types other than "
357 "int, string, or double"));
361 case json_spirit::str_type
:
362 (*mapp
)[it
->first
] = it
->second
.get_str();
365 case json_spirit::bool_type
:
366 (*mapp
)[it
->first
] = it
->second
.get_bool();
369 case json_spirit::int_type
:
370 (*mapp
)[it
->first
] = it
->second
.get_int64();
373 case json_spirit::real_type
:
374 (*mapp
)[it
->first
] = it
->second
.get_real();
379 } catch (runtime_error
&e
) {
385 class stringify_visitor
: public boost::static_visitor
<string
>
388 template <typename T
>
389 string
operator()(T
&operand
) const
398 cmd_vartype_stringify(const cmd_vartype
&v
)
400 return boost::apply_visitor(stringify_visitor(), v
);
405 handle_bad_get(CephContext
*cct
, const string
& k
, const char *tname
)
407 ostringstream errstr
;
409 const char *typestr
= abi::__cxa_demangle(tname
, 0, 0, &status
);
412 errstr
<< "bad boost::get: key " << k
<< " is not type " << typestr
;
413 lderr(cct
) << errstr
.str() << dendl
;
417 lderr(cct
) << oss
.str() << dendl
;
420 free((char *)typestr
);
423 long parse_pos_long(const char *s
, std::ostream
*pss
)
425 if (*s
== '-' || *s
== '+') {
427 *pss
<< "expected numerical value, got: " << s
;
432 long r
= strict_strtol(s
, 10, &err
);
433 if ((r
== 0) && !err
.empty()) {
440 *pss
<< "unable to parse positive integer '" << s
<< "'";
446 int parse_osd_id(const char *s
, std::ostream
*pss
)
449 if (strncmp(s
, "osd.", 4) == 0) {
455 long id
= parse_pos_long(s
, &ss
);
461 *pss
<< "osd id " << id
<< " is too large";
468 template <typename Func
>
469 bool find_first_in(std::string_view s
, const char *delims
, Func
&& f
)
471 auto pos
= s
.find_first_not_of(delims
);
472 while (pos
!= s
.npos
) {
473 s
.remove_prefix(pos
);
474 auto end
= s
.find_first_of(delims
);
475 if (f(s
.substr(0, end
))) {
478 pos
= s
.find_first_not_of(delims
, end
);
484 T
str_to_num(const std::string
& s
)
486 if constexpr (is_same_v
<T
, int>) {
488 } else if constexpr (is_same_v
<T
, long>) {
490 } else if constexpr (is_same_v
<T
, long long>) {
491 return std::stoll(s
);
492 } else if constexpr (is_same_v
<T
, double>) {
498 bool arg_in_range(T value
, const arg_desc_t
& desc
, std::ostream
& os
) {
499 auto range
= desc
.find("range");
500 if (range
== desc
.end()) {
503 auto min_max
= get_str_list(string(range
->second
), "|");
504 auto min
= str_to_num
<T
>(min_max
.front());
505 auto max
= numeric_limits
<T
>::max();
506 if (min_max
.size() > 1) {
507 max
= str_to_num
<T
>(min_max
.back());
509 if (value
< min
|| value
> max
) {
510 os
<< "'" << value
<< "' out of range: " << min_max
;
516 bool validate_str_arg(std::string_view value
,
517 std::string_view type
,
518 const arg_desc_t
& desc
,
521 if (type
== "CephIPAddr") {
523 if (addr
.parse(string(value
).c_str())) {
526 os
<< "failed to parse addr '" << value
<< "', should be ip:[port]";
529 } else if (type
== "CephChoices") {
530 auto choices
= desc
.find("strings");
531 ceph_assert(choices
!= end(desc
));
532 auto strings
= choices
->second
;
533 if (find_first_in(strings
, "|", [=](auto choice
) {
534 return (value
== choice
);
538 os
<< "'" << value
<< "' not belong to '" << strings
<< "'";
542 // CephString or other types like CephPgid
547 template<bool is_vector
,
549 typename Value
= conditional_t
<is_vector
,
552 bool validate_arg(CephContext
* cct
,
553 const cmdmap_t
& cmdmap
,
554 const arg_desc_t
& desc
,
555 const std::string_view name
,
556 const std::string_view type
,
561 if (!cmd_getval(cmdmap
, string(name
), v
)) {
562 if constexpr (is_vector
) {
563 // an empty list is acceptable.
566 if (auto req
= desc
.find("req");
567 req
!= end(desc
) && req
->second
== "false") {
570 os
<< "missing required parameter: '" << name
<< "'";
575 } catch (const bad_cmd_get
& e
) {
578 auto validate
= [&](const T
& value
) {
579 if constexpr (is_same_v
<std::string
, T
>) {
580 return validate_str_arg(value
, type
, desc
, os
);
581 } else if constexpr (is_same_v
<int64_t, T
> ||
582 is_same_v
<double, T
>) {
583 return arg_in_range(value
, desc
, os
);
586 if constexpr(is_vector
) {
587 return find_if_not(begin(v
), end(v
), validate
) == end(v
);
592 } // anonymous namespace
594 bool validate_cmd(CephContext
* cct
,
595 const std::string
& desc
,
596 const cmdmap_t
& cmdmap
,
599 return !find_first_in(desc
, " ", [&](auto desc
) {
600 auto arg_desc
= cmddesc_get_args(desc
);
601 if (arg_desc
.empty()) {
604 ceph_assert(arg_desc
.count("name"));
605 ceph_assert(arg_desc
.count("type"));
606 auto name
= arg_desc
["name"];
607 auto type
= arg_desc
["type"];
608 if (arg_desc
.count("n")) {
609 if (type
== "CephInt") {
610 return !validate_arg
<true, int64_t>(cct
, cmdmap
, arg_desc
,
612 } else if (type
== "CephFloat") {
613 return !validate_arg
<true, double>(cct
, cmdmap
, arg_desc
,
616 return !validate_arg
<true, string
>(cct
, cmdmap
, arg_desc
,
620 if (type
== "CephInt") {
621 return !validate_arg
<false, int64_t>(cct
, cmdmap
, arg_desc
,
623 } else if (type
== "CephFloat") {
624 return !validate_arg
<false, double>(cct
, cmdmap
, arg_desc
,
627 return !validate_arg
<false, string
>(cct
, cmdmap
, arg_desc
,
634 bool cmd_getval(const cmdmap_t
& cmdmap
,
635 const std::string
& k
, bool& val
)
638 * Specialized getval for booleans. CephBool didn't exist before Nautilus,
639 * so earlier clients are sent a CephChoices argdesc instead, and will
640 * send us a "--foo-bar" value string for boolean arguments.
642 if (cmdmap
.count(k
)) {
644 val
= boost::get
<bool>(cmdmap
.find(k
)->second
);
646 } catch (boost::bad_get
&) {
648 std::string expected
= "--" + k
;
649 std::replace(expected
.begin(), expected
.end(), '_', '-');
651 std::string v_str
= boost::get
<std::string
>(cmdmap
.find(k
)->second
);
652 if (v_str
== expected
) {
656 throw bad_cmd_get(k
, cmdmap
);
658 } catch (boost::bad_get
&) {
659 throw bad_cmd_get(k
, cmdmap
);