]>
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 "common/cmdparse.h"
16 #include "common/Formatter.h"
17 #include "common/debug.h"
18 #include "common/strtol.h"
19 #include "json_spirit/json_spirit.h"
22 * Given a cmddesc like "foo baz name=bar,type=CephString",
23 * return the prefix "foo baz".
25 std::string
cmddesc_get_prefix(const std::string
&cmddesc
)
27 stringstream
ss(cmddesc
);
29 std::ostringstream result
;
31 while (std::getline(ss
, word
, ' ')) {
32 if (word
.find_first_of(",=") != string::npos
) {
46 using arg_desc_t
= std::map
<std::string_view
, std::string_view
>;
48 // Snarf up all the key=val,key=val pairs, put 'em in a dict.
49 template<class String
>
50 arg_desc_t
cmddesc_get_args(const String
& cmddesc
)
53 for_each_substr(cmddesc
, ",", [&](auto kv
) {
54 // key=value; key by itself implies value is bool true
55 // name="name" means arg dict will be titled 'name'
56 auto equal
= kv
.find('=');
57 if (equal
== kv
.npos
) {
58 // it should be the command
61 auto key
= kv
.substr(0, equal
);
62 auto val
= kv
.substr(equal
+ 1);
68 std::string
cmddesc_get_prenautilus_compat(const std::string
&cmddesc
)
70 std::vector
<std::string
> out
;
71 stringstream
ss(cmddesc
);
74 while (std::getline(ss
, word
, ' ')) {
75 // if no , or =, must be a plain word to put out
76 if (word
.find_first_of(",=") == string::npos
) {
80 auto desckv
= cmddesc_get_args(word
);
81 auto j
= desckv
.find("type");
82 if (j
!= desckv
.end() && j
->second
== "CephBool") {
83 // Instruct legacy clients or mons to send --foo-bar string in place
84 // of a 'true'/'false' value
85 std::ostringstream oss
;
86 oss
<< std::string("--") << desckv
["name"];
87 std::string val
= oss
.str();
88 std::replace(val
.begin(), val
.end(), '_', '-');
89 desckv
["type"] = "CephChoices";
90 desckv
["strings"] = val
;
91 std::ostringstream fss
;
92 for (auto k
= desckv
.begin(); k
!= desckv
.end(); ++k
) {
93 if (k
!= desckv
.begin()) {
96 fss
<< k
->first
<< "=" << k
->second
;
98 out
.push_back(fss
.str());
108 for (auto i
= out
.begin(); i
!= out
.end(); ++i
) {
109 if (i
!= out
.begin()) {
118 * Read a command description list out of cmd, and dump it to f.
119 * A signature description is a set of space-separated words;
120 * see MonCommands.h for more info.
124 dump_cmd_to_json(Formatter
*f
, uint64_t features
, const string
& cmd
)
126 // put whole command signature in an already-opened container
127 // elements are: "name", meaning "the typeless name that means a literal"
128 // an object {} with key:value pairs representing an argument
130 stringstream
ss(cmd
);
133 while (std::getline(ss
, word
, ' ')) {
134 // if no , or =, must be a plain word to put out
135 if (word
.find_first_of(",=") == string::npos
) {
136 f
->dump_string("arg", word
);
139 // accumulate descriptor keywords in desckv
140 auto desckv
= cmddesc_get_args(word
);
141 // name the individual desc object based on the name key
142 f
->open_object_section(string(desckv
["name"]).c_str());
144 // Compatibility for pre-nautilus clients that don't know about CephBool
146 if (!HAVE_FEATURE(features
, SERVER_NAUTILUS
)) {
147 auto i
= desckv
.find("type");
148 if (i
!= desckv
.end() && i
->second
== "CephBool") {
149 // Instruct legacy clients to send --foo-bar string in place
150 // of a 'true'/'false' value
151 std::ostringstream oss
;
152 oss
<< std::string("--") << desckv
["name"];
154 std::replace(val
.begin(), val
.end(), '_', '-');
156 desckv
["type"] = "CephChoices";
157 desckv
["strings"] = val
;
161 // dump all the keys including name into the array
162 for (auto [key
, value
] : desckv
) {
163 f
->dump_string(string(key
).c_str(), string(value
));
165 f
->close_section(); // attribute object for individual desc
170 dump_cmd_and_help_to_json(Formatter
*jf
,
172 const string
& secname
,
173 const string
& cmdsig
,
174 const string
& helptext
)
176 jf
->open_object_section(secname
.c_str());
177 jf
->open_array_section("sig");
178 dump_cmd_to_json(jf
, features
, cmdsig
);
179 jf
->close_section(); // sig array
180 jf
->dump_string("help", helptext
.c_str());
181 jf
->close_section(); // cmd
185 dump_cmddesc_to_json(Formatter
*jf
,
187 const string
& secname
,
188 const string
& cmdsig
,
189 const string
& helptext
,
190 const string
& module
,
194 jf
->open_object_section(secname
.c_str());
195 jf
->open_array_section("sig");
196 dump_cmd_to_json(jf
, features
, cmdsig
);
197 jf
->close_section(); // sig array
198 jf
->dump_string("help", helptext
.c_str());
199 jf
->dump_string("module", module
.c_str());
200 jf
->dump_string("perm", perm
.c_str());
201 jf
->dump_int("flags", flags
);
202 jf
->close_section(); // cmd
205 void cmdmap_dump(const cmdmap_t
&cmdmap
, Formatter
*f
)
207 ceph_assert(f
!= nullptr);
209 class dump_visitor
: public boost::static_visitor
<void>
212 std::string
const &key
;
214 dump_visitor(Formatter
*f_
, std::string
const &key_
)
219 void operator()(const std::string
&operand
) const
221 f
->dump_string(key
.c_str(), operand
);
224 void operator()(const bool &operand
) const
226 f
->dump_bool(key
.c_str(), operand
);
229 void operator()(const int64_t &operand
) const
231 f
->dump_int(key
.c_str(), operand
);
234 void operator()(const double &operand
) const
236 f
->dump_float(key
.c_str(), operand
);
239 void operator()(const std::vector
<std::string
> &operand
) const
241 f
->open_array_section(key
.c_str());
242 for (const auto i
: operand
) {
243 f
->dump_string("item", i
);
248 void operator()(const std::vector
<int64_t> &operand
) const
250 f
->open_array_section(key
.c_str());
251 for (const auto i
: operand
) {
252 f
->dump_int("item", i
);
257 void operator()(const std::vector
<double> &operand
) const
259 f
->open_array_section(key
.c_str());
260 for (const auto i
: operand
) {
261 f
->dump_float("item", i
);
267 //f->open_object_section("cmdmap");
268 for (const auto &i
: cmdmap
) {
269 boost::apply_visitor(dump_visitor(f
, i
.first
), i
.second
);
271 //f->close_section();
275 /** Parse JSON in vector cmd into a map from field to map of values
276 * (use mValue/mObject)
277 * 'cmd' should not disappear over lifetime of map
278 * 'mapp' points to the caller's map
279 * 'ss' captures any errors during JSON parsing; if function returns
280 * false, ss is valid */
283 cmdmap_from_json(vector
<string
> cmd
, cmdmap_t
*mapp
, stringstream
&ss
)
285 json_spirit::mValue v
;
288 // First, join all cmd strings
289 for (vector
<string
>::iterator it
= cmd
.begin();
290 it
!= cmd
.end(); ++it
)
294 if (!json_spirit::read(fullcmd
, v
))
295 throw runtime_error("unparseable JSON " + fullcmd
);
296 if (v
.type() != json_spirit::obj_type
)
297 throw(runtime_error("not JSON object " + fullcmd
));
299 // allocate new mObject (map) to return
300 // make sure all contents are simple types (not arrays or objects)
301 json_spirit::mObject o
= v
.get_obj();
302 for (map
<string
, json_spirit::mValue
>::iterator it
= o
.begin();
303 it
!= o
.end(); ++it
) {
305 // ok, marshal it into our string->cmd_vartype map, or throw an
306 // exception if it's not a simple datatype. This is kind of
307 // annoying, since json_spirit has a boost::variant inside it
308 // already, but it's not public. Oh well.
310 switch (it
->second
.type()) {
312 case json_spirit::obj_type
:
314 throw(runtime_error("JSON array/object not allowed " + fullcmd
));
317 case json_spirit::array_type
:
319 // array is a vector of values. Unpack it to a vector
320 // of strings, doubles, or int64_t, the only types we handle.
321 const vector
<json_spirit::mValue
>& spvals
= it
->second
.get_array();
322 if (spvals
.empty()) {
323 // if an empty array is acceptable, the caller should always check for
324 // vector<string> if the expected value of "vector<int64_t>" in the
325 // cmdmap is missing.
326 (*mapp
)[it
->first
] = vector
<string
>();
327 } else if (spvals
.front().type() == json_spirit::str_type
) {
329 for (const auto& sv
: spvals
) {
330 if (sv
.type() != json_spirit::str_type
) {
331 throw(runtime_error("Can't handle arrays of multiple types"));
333 outv
.push_back(sv
.get_str());
335 (*mapp
)[it
->first
] = std::move(outv
);
336 } else if (spvals
.front().type() == json_spirit::int_type
) {
337 vector
<int64_t> outv
;
338 for (const auto& sv
: spvals
) {
339 if (spvals
.front().type() != json_spirit::int_type
) {
340 throw(runtime_error("Can't handle arrays of multiple types"));
342 outv
.push_back(sv
.get_int64());
344 (*mapp
)[it
->first
] = std::move(outv
);
345 } else if (spvals
.front().type() == json_spirit::real_type
) {
347 for (const auto& sv
: spvals
) {
348 if (spvals
.front().type() != json_spirit::real_type
) {
349 throw(runtime_error("Can't handle arrays of multiple types"));
351 outv
.push_back(sv
.get_real());
353 (*mapp
)[it
->first
] = std::move(outv
);
355 throw(runtime_error("Can't handle arrays of types other than "
356 "int, string, or double"));
360 case json_spirit::str_type
:
361 (*mapp
)[it
->first
] = it
->second
.get_str();
364 case json_spirit::bool_type
:
365 (*mapp
)[it
->first
] = it
->second
.get_bool();
368 case json_spirit::int_type
:
369 (*mapp
)[it
->first
] = it
->second
.get_int64();
372 case json_spirit::real_type
:
373 (*mapp
)[it
->first
] = it
->second
.get_real();
378 } catch (runtime_error
&e
) {
384 class stringify_visitor
: public boost::static_visitor
<string
>
387 template <typename T
>
388 string
operator()(T
&operand
) const
397 cmd_vartype_stringify(const cmd_vartype
&v
)
399 return boost::apply_visitor(stringify_visitor(), v
);
404 handle_bad_get(CephContext
*cct
, const string
& k
, const char *tname
)
406 ostringstream errstr
;
408 const char *typestr
= abi::__cxa_demangle(tname
, 0, 0, &status
);
411 errstr
<< "bad boost::get: key " << k
<< " is not type " << typestr
;
412 lderr(cct
) << errstr
.str() << dendl
;
416 lderr(cct
) << oss
.str() << dendl
;
419 free((char *)typestr
);
422 long parse_pos_long(const char *s
, std::ostream
*pss
)
424 if (*s
== '-' || *s
== '+') {
426 *pss
<< "expected numerical value, got: " << s
;
431 long r
= strict_strtol(s
, 10, &err
);
432 if ((r
== 0) && !err
.empty()) {
439 *pss
<< "unable to parse positive integer '" << s
<< "'";
445 int parse_osd_id(const char *s
, std::ostream
*pss
)
448 if (strncmp(s
, "osd.", 4) == 0) {
454 long id
= parse_pos_long(s
, &ss
);
460 *pss
<< "osd id " << id
<< " is too large";
467 template <typename Func
>
468 bool find_first_in(std::string_view s
, const char *delims
, Func
&& f
)
470 auto pos
= s
.find_first_not_of(delims
);
471 while (pos
!= s
.npos
) {
472 s
.remove_prefix(pos
);
473 auto end
= s
.find_first_of(delims
);
474 if (f(s
.substr(0, end
))) {
477 pos
= s
.find_first_not_of(delims
, end
);
483 T
str_to_num(const std::string
& s
)
485 if constexpr (is_same_v
<T
, int>) {
487 } else if constexpr (is_same_v
<T
, long>) {
489 } else if constexpr (is_same_v
<T
, long long>) {
490 return std::stoll(s
);
491 } else if constexpr (is_same_v
<T
, double>) {
497 bool arg_in_range(T value
, const arg_desc_t
& desc
, std::ostream
& os
) {
498 auto range
= desc
.find("range");
499 if (range
== desc
.end()) {
502 auto min_max
= get_str_list(string(range
->second
), "|");
503 auto min
= str_to_num
<T
>(min_max
.front());
504 auto max
= numeric_limits
<T
>::max();
505 if (min_max
.size() > 1) {
506 max
= str_to_num
<T
>(min_max
.back());
508 if (value
< min
|| value
> max
) {
509 os
<< "'" << value
<< "' out of range: " << min_max
;
515 bool validate_str_arg(std::string_view value
,
516 std::string_view type
,
517 const arg_desc_t
& desc
,
520 if (type
== "CephIPAddr") {
522 if (addr
.parse(string(value
).c_str())) {
525 os
<< "failed to parse addr '" << value
<< "', should be ip:[port]";
528 } else if (type
== "CephChoices") {
529 auto choices
= desc
.find("strings");
530 ceph_assert(choices
!= end(desc
));
531 auto strings
= choices
->second
;
532 if (find_first_in(strings
, "|", [=](auto choice
) {
533 return (value
== choice
);
537 os
<< "'" << value
<< "' not belong to '" << strings
<< "'";
541 // CephString or other types like CephPgid
546 template<bool is_vector
,
548 typename Value
= conditional_t
<is_vector
,
551 bool validate_arg(CephContext
* cct
,
552 const cmdmap_t
& cmdmap
,
553 const arg_desc_t
& desc
,
554 const std::string_view name
,
555 const std::string_view type
,
560 if (!cmd_getval(cct
, cmdmap
, string(name
), v
)) {
561 if constexpr (is_vector
) {
562 // an empty list is acceptable.
565 if (auto req
= desc
.find("req");
566 req
!= end(desc
) && req
->second
== "false") {
569 os
<< "missing required parameter: '" << name
<< "'";
574 } catch (const bad_cmd_get
& e
) {
577 auto validate
= [&](const T
& value
) {
578 if constexpr (is_same_v
<std::string
, T
>) {
579 return validate_str_arg(value
, type
, desc
, os
);
580 } else if constexpr (is_same_v
<int64_t, T
> ||
581 is_same_v
<double, T
>) {
582 return arg_in_range(value
, desc
, os
);
585 if constexpr(is_vector
) {
586 return find_if_not(begin(v
), end(v
), validate
) == end(v
);
591 } // anonymous namespace
593 bool validate_cmd(CephContext
* cct
,
594 const std::string
& desc
,
595 const cmdmap_t
& cmdmap
,
598 return !find_first_in(desc
, " ", [&](auto desc
) {
599 auto arg_desc
= cmddesc_get_args(desc
);
600 if (arg_desc
.empty()) {
603 ceph_assert(arg_desc
.count("name"));
604 ceph_assert(arg_desc
.count("type"));
605 auto name
= arg_desc
["name"];
606 auto type
= arg_desc
["type"];
607 if (arg_desc
.count("n")) {
608 if (type
== "CephInt") {
609 return !validate_arg
<true, int64_t>(cct
, cmdmap
, arg_desc
,
611 } else if (type
== "CephFloat") {
612 return !validate_arg
<true, double>(cct
, cmdmap
, arg_desc
,
615 return !validate_arg
<true, string
>(cct
, cmdmap
, arg_desc
,
619 if (type
== "CephInt") {
620 return !validate_arg
<false, int64_t>(cct
, cmdmap
, arg_desc
,
622 } else if (type
== "CephFloat") {
623 return !validate_arg
<false, double>(cct
, cmdmap
, arg_desc
,
626 return !validate_arg
<false, string
>(cct
, cmdmap
, arg_desc
,
633 bool cmd_getval(CephContext
*cct
, const cmdmap_t
& cmdmap
,
634 const std::string
& k
, bool& val
)
637 * Specialized getval for booleans. CephBool didn't exist before Nautilus,
638 * so earlier clients are sent a CephChoices argdesc instead, and will
639 * send us a "--foo-bar" value string for boolean arguments.
641 if (cmdmap
.count(k
)) {
643 val
= boost::get
<bool>(cmdmap
.find(k
)->second
);
645 } catch (boost::bad_get
&) {
647 std::string expected
= "--" + k
;
648 std::replace(expected
.begin(), expected
.end(), '_', '-');
650 std::string v_str
= boost::get
<std::string
>(cmdmap
.find(k
)->second
);
651 if (v_str
== expected
) {
655 throw bad_cmd_get(k
, cmdmap
);
657 } catch (boost::bad_get
&) {
658 throw bad_cmd_get(k
, cmdmap
);