Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / common / admin_socket.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2011 New Dream Network
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software 
11  * Foundation.  See file COPYING.
12  * 
13  */
14
15 #ifndef CEPH_COMMON_ADMIN_SOCKET_H
16 #define CEPH_COMMON_ADMIN_SOCKET_H
17
18 #include "common/Cond.h"
19
20 class AdminSocket;
21 class CephContext;
22
23 #define CEPH_ADMIN_SOCK_VERSION "2"
24
25 class AdminSocketHook {
26 public:
27   virtual bool call(std::string command, cmdmap_t &cmdmap, std::string format,
28                     bufferlist& out) = 0;
29   virtual ~AdminSocketHook() {}
30 };
31
32 class AdminSocket : public Thread
33 {
34 public:
35   AdminSocket(CephContext *cct);
36   ~AdminSocket() override;
37
38   /**
39    * register an admin socket command
40    *
41    * The command is registered under a command string.  Incoming
42    * commands are split by space and matched against the longest
43    * registered command.  For example, if 'foo' and 'foo bar' are
44    * registered, and an incoming command is 'foo bar baz', it is
45    * matched with 'foo bar', while 'foo fud' will match 'foo'.
46    *
47    * The entire incoming command string is passed to the registred
48    * hook.
49    *
50    * @param command command string
51    * @param cmddesc command syntax descriptor
52    * @param hook implementaiton
53    * @param help help text.  if empty, command will not be included in 'help' output.
54    *
55    * @return 0 for success, -EEXIST if command already registered.
56    */
57   int register_command(std::string command, std::string cmddesc, AdminSocketHook *hook, std::string help);
58
59   /**
60    * unregister an admin socket command.
61    *
62    * If a command is currently in progress, this will block until it
63    * is done.  For that reason, you must not hold any locks required
64    * by your hook while you call this.
65    *
66    * @param command command string
67    * @return 0 on succest, -ENOENT if command dne.
68    */
69   int unregister_command(std::string command);
70
71   bool init(const std::string &path);
72
73   void chown(uid_t uid, gid_t gid);
74   void chmod(mode_t mode);
75
76 private:
77   AdminSocket(const AdminSocket& rhs);
78   AdminSocket& operator=(const AdminSocket &rhs);
79
80   void shutdown();
81
82   std::string create_shutdown_pipe(int *pipe_rd, int *pipe_wr);
83   std::string destroy_shutdown_pipe();
84   std::string bind_and_listen(const std::string &sock_path, int *fd);
85
86   void *entry() override;
87   bool do_accept();
88
89   CephContext *m_cct;
90   std::string m_path;
91   int m_sock_fd;
92   int m_shutdown_rd_fd;
93   int m_shutdown_wr_fd;
94
95   bool in_hook;
96   Cond in_hook_cond;
97   Mutex m_lock;    // protects m_hooks, m_descs, m_help
98   AdminSocketHook *m_version_hook, *m_help_hook, *m_getdescs_hook;
99
100   std::map<std::string,AdminSocketHook*> m_hooks;
101   std::map<std::string,std::string> m_descs;
102   std::map<std::string,std::string> m_help;
103
104   friend class AdminSocketTest;
105   friend class HelpHook;
106   friend class GetdescsHook;
107 };
108
109 #endif