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) 2011 New Dream Network
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.
15 #ifndef CEPH_COMMON_ADMIN_SOCKET_H
16 #define CEPH_COMMON_ADMIN_SOCKET_H
18 #include <condition_variable>
21 #include <string_view>
24 #include "include/buffer.h"
25 #include "common/cmdparse.h"
30 using namespace std::literals
;
32 inline constexpr auto CEPH_ADMIN_SOCK_VERSION
= "2"sv
;
34 class AdminSocketHook
{
36 virtual bool call(std::string_view command
, const cmdmap_t
& cmdmap
,
37 std::string_view format
, bufferlist
& out
) = 0;
38 virtual ~AdminSocketHook() {}
44 AdminSocket(CephContext
*cct
);
47 AdminSocket(const AdminSocket
&) = delete;
48 AdminSocket
& operator =(const AdminSocket
&) = delete;
49 AdminSocket(AdminSocket
&&) = delete;
50 AdminSocket
& operator =(AdminSocket
&&) = delete;
53 * register an admin socket command
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'.
61 * The entire incoming command string is passed to the registered
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.
69 * @return 0 for success, -EEXIST if command already registered.
71 int register_command(std::string_view command
,
72 std::string_view cmddesc
,
73 AdminSocketHook
*hook
,
74 std::string_view help
);
77 * unregister an admin socket command.
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.
83 * @param command command string
84 * @return 0 on succest, -ENOENT if command dne.
86 int unregister_command(std::string_view command
);
89 * unregister all commands belong to hook.
91 void unregister_commands(const AdminSocketHook
*hook
);
93 bool init(const std::string
& path
);
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
);
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
);
108 void entry() noexcept
;
110 bool validate(const std::string
& command
,
111 const cmdmap_t
& cmdmap
,
112 bufferlist
& out
) const;
117 int m_shutdown_rd_fd
= -1;
118 int m_shutdown_wr_fd
= -1;
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
;
128 AdminSocketHook
* hook
;
132 hook_info(AdminSocketHook
* hook
, std::string_view desc
,
133 std::string_view help
)
134 : hook(hook
), desc(desc
), help(help
) {}
137 std::map
<std::string
, hook_info
, std::less
<>> hooks
;
139 friend class AdminSocketTest
;
140 friend class HelpHook
;
141 friend class GetdescsHook
;