]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/admin_socket.h
import ceph 14.2.5
[ceph.git] / 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 <condition_variable>
19 #include <mutex>
20 #include <string>
21 #include <string_view>
22 #include <thread>
23
24 #include "include/buffer.h"
25 #include "common/cmdparse.h"
26
27 class AdminSocket;
28 class CephContext;
29
30 using namespace std::literals;
31
32 inline constexpr auto CEPH_ADMIN_SOCK_VERSION = "2"sv;
33
34 class AdminSocketHook {
35 public:
36 virtual bool call(std::string_view command, const cmdmap_t& cmdmap,
37 std::string_view format, bufferlist& out) = 0;
38 virtual ~AdminSocketHook() {}
39 };
40
41 class AdminSocket
42 {
43 public:
44 AdminSocket(CephContext *cct);
45 ~AdminSocket();
46
47 AdminSocket(const AdminSocket&) = delete;
48 AdminSocket& operator =(const AdminSocket&) = delete;
49 AdminSocket(AdminSocket&&) = delete;
50 AdminSocket& operator =(AdminSocket&&) = delete;
51
52 /**
53 * register an admin socket command
54 *
55 * The command is registered under a command string. Incoming
56 * commands are split by space and matched against the longest
57 * registered command. For example, if 'foo' and 'foo bar' are
58 * registered, and an incoming command is 'foo bar baz', it is
59 * matched with 'foo bar', while 'foo fud' will match 'foo'.
60 *
61 * The entire incoming command string is passed to the registered
62 * hook.
63 *
64 * @param command command string
65 * @param cmddesc command syntax descriptor
66 * @param hook implementation
67 * @param help help text. if empty, command will not be included in 'help' output.
68 *
69 * @return 0 for success, -EEXIST if command already registered.
70 */
71 int register_command(std::string_view command,
72 std::string_view cmddesc,
73 AdminSocketHook *hook,
74 std::string_view help);
75
76 /**
77 * unregister an admin socket command.
78 *
79 * If a command is currently in progress, this will block until it
80 * is done. For that reason, you must not hold any locks required
81 * by your hook while you call this.
82 *
83 * @param command command string
84 * @return 0 on succest, -ENOENT if command dne.
85 */
86 int unregister_command(std::string_view command);
87
88 /*
89 * unregister all commands belong to hook.
90 */
91 void unregister_commands(const AdminSocketHook *hook);
92
93 bool init(const std::string& path);
94
95 void chown(uid_t uid, gid_t gid);
96 void chmod(mode_t mode);
97 int execute_command(const std::string& cmd, ceph::bufferlist& out);
98
99 private:
100
101 void shutdown();
102
103 std::string create_shutdown_pipe(int *pipe_rd, int *pipe_wr);
104 std::string destroy_shutdown_pipe();
105 std::string bind_and_listen(const std::string &sock_path, int *fd);
106
107 std::thread th;
108 void entry() noexcept;
109 bool do_accept();
110 bool validate(const std::string& command,
111 const cmdmap_t& cmdmap,
112 bufferlist& out) const;
113
114 CephContext *m_cct;
115 std::string m_path;
116 int m_sock_fd = -1;
117 int m_shutdown_rd_fd = -1;
118 int m_shutdown_wr_fd = -1;
119
120 bool in_hook = false;
121 std::condition_variable in_hook_cond;
122 std::mutex lock; // protects `hooks`
123 std::unique_ptr<AdminSocketHook> version_hook;
124 std::unique_ptr<AdminSocketHook> help_hook;
125 std::unique_ptr<AdminSocketHook> getdescs_hook;
126
127 struct hook_info {
128 AdminSocketHook* hook;
129 std::string desc;
130 std::string help;
131
132 hook_info(AdminSocketHook* hook, std::string_view desc,
133 std::string_view help)
134 : hook(hook), desc(desc), help(help) {}
135 };
136
137 std::map<std::string, hook_info, std::less<>> hooks;
138
139 friend class AdminSocketTest;
140 friend class HelpHook;
141 friend class GetdescsHook;
142 };
143
144 #endif