| 245 | } |
| 246 | |
| 247 | int ActivePyModule::handle_command( |
| 248 | const ModuleCommand& module_command, |
| 249 | const MgrSession& session, |
| 250 | const cmdmap_t &cmdmap, |
| 251 | const bufferlist &inbuf, |
| 252 | std::stringstream *ds, |
| 253 | std::stringstream *ss) |
| 254 | { |
| 255 | ceph_assert(ss != nullptr); |
| 256 | ceph_assert(ds != nullptr); |
| 257 | |
| 258 | if (pClassInstance == nullptr) { |
| 259 | // Not the friendliest error string, but we could only |
| 260 | // hit this in quite niche cases, if at all. |
| 261 | *ss << "Module not instantiated"; |
| 262 | return -EINVAL; |
| 263 | } |
| 264 | |
| 265 | Gil gil(py_module->pMyThreadState, true); |
| 266 | |
| 267 | PyFormatter f; |
| 268 | TOPNSPC::common::cmdmap_dump(cmdmap, &f); |
| 269 | PyObject *py_cmd = f.get(); |
| 270 | string instr; |
| 271 | inbuf.begin().copy(inbuf.length(), instr); |
| 272 | |
| 273 | ceph_assert(m_session == nullptr); |
| 274 | m_command_perms = module_command.perm; |
| 275 | m_session = &session; |
| 276 | auto _start = ceph::mono_clock::now(); |
| 277 | auto pResult = PyObject_CallMethod(pClassInstance, |
| 278 | const_cast<char*>("_handle_command"), const_cast<char*>("s#O"), |
| 279 | instr.c_str(), instr.length(), py_cmd); |
| 280 | |
| 281 | m_command_perms.clear(); |
| 282 | m_session = nullptr; |
| 283 | Py_DECREF(py_cmd); |
| 284 | |
| 285 | int r = 0; |
| 286 | if (pResult != NULL) { |
| 287 | if (py_module->perfcounter) { |
| 288 | auto duration = ceph::mono_clock::now() - _start; |
| 289 | auto usec = std::chrono::duration_cast<std::chrono::microseconds>(duration).count(); |
| 290 | py_module->perfcounter->inc(py_module->l_pym_cmd_avg_usec, usec); |
| 291 | } |
| 292 | if (PyTuple_Size(pResult) != 3) { |
| 293 | derr << "module '" << py_module->get_name() << "' command handler " |
| 294 | "returned wrong type!" << dendl; |
| 295 | r = -EINVAL; |
| 296 | } else { |
| 297 | r = PyLong_AsLong(PyTuple_GetItem(pResult, 0)); |
| 298 | *ds << PyUnicode_AsUTF8(PyTuple_GetItem(pResult, 1)); |
| 299 | *ss << PyUnicode_AsUTF8(PyTuple_GetItem(pResult, 2)); |
| 300 | } |
| 301 | |
| 302 | Py_DECREF(pResult); |
| 303 | } else { |
| 304 | derr << "module '" << py_module->get_name() << "' command handler " |
nothing calls this directly
no test coverage detected