]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/admin_socket.cc
Import ceph 15.2.8
[ceph.git] / ceph / src / common / admin_socket.cc
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 #include <poll.h>
15 #include <sys/un.h>
16
17 #include "common/admin_socket.h"
18 #include "common/admin_socket_client.h"
19 #include "common/dout.h"
20 #include "common/errno.h"
21 #include "common/safe_io.h"
22 #include "common/Thread.h"
23 #include "common/version.h"
24 #include "common/ceph_mutex.h"
25
26 #ifndef WITH_SEASTAR
27 #include "common/Cond.h"
28 #endif
29
30 #include "messages/MCommand.h"
31 #include "messages/MCommandReply.h"
32 #include "messages/MMonCommand.h"
33 #include "messages/MMonCommandAck.h"
34
35 // re-include our assert to clobber the system one; fix dout:
36 #include "include/ceph_assert.h"
37 #include "include/compat.h"
38 #include "include/sock_compat.h"
39
40 #define dout_subsys ceph_subsys_asok
41 #undef dout_prefix
42 #define dout_prefix *_dout << "asok(" << (void*)m_cct << ") "
43
44
45 using std::ostringstream;
46 using namespace TOPNSPC::common;
47
48 /*
49 * UNIX domain sockets created by an application persist even after that
50 * application closes, unless they're explicitly unlinked. This is because the
51 * directory containing the socket keeps a reference to the socket.
52 *
53 * This code makes things a little nicer by unlinking those dead sockets when
54 * the application exits normally.
55 */
56
57 template<typename F, typename... Args>
58 inline int retry_sys_call(F f, Args... args) {
59 int r;
60 do {
61 r = f(args...);
62 } while (r < 0 && errno == EINTR);
63 return r;
64 };
65
66
67 static std::mutex cleanup_lock;
68 static std::vector<std::string> cleanup_files;
69 static bool cleanup_atexit = false;
70
71 static void remove_cleanup_file(std::string_view file) {
72 std::unique_lock l(cleanup_lock);
73
74 if (auto i = std::find(cleanup_files.cbegin(), cleanup_files.cend(), file);
75 i != cleanup_files.cend()) {
76 retry_sys_call(::unlink, i->c_str());
77 cleanup_files.erase(i);
78 }
79 }
80
81 void remove_all_cleanup_files() {
82 std::unique_lock l(cleanup_lock);
83 for (const auto& s : cleanup_files) {
84 retry_sys_call(::unlink, s.c_str());
85 }
86 cleanup_files.clear();
87 }
88
89 static void add_cleanup_file(std::string file) {
90 std::unique_lock l(cleanup_lock);
91 cleanup_files.push_back(std::move(file));
92 if (!cleanup_atexit) {
93 atexit(remove_all_cleanup_files);
94 cleanup_atexit = true;
95 }
96 }
97
98 AdminSocket::AdminSocket(CephContext *cct)
99 : m_cct(cct)
100 {}
101
102 AdminSocket::~AdminSocket()
103 {
104 shutdown();
105 }
106
107 /*
108 * This thread listens on the UNIX domain socket for incoming connections.
109 * It only handles one connection at a time at the moment. All I/O is nonblocking,
110 * so that we can implement sensible timeouts. [TODO: make all I/O nonblocking]
111 *
112 * This thread also listens to m_wakeup_rd_fd. If there is any data sent to this
113 * pipe, the thread wakes up. If m_shutdown is set, the thread terminates
114 * itself gracefully, allowing the AdminSocketConfigObs class to join() it.
115 */
116
117 std::string AdminSocket::create_wakeup_pipe(int *pipe_rd, int *pipe_wr)
118 {
119 int pipefd[2];
120 if (pipe_cloexec(pipefd, O_NONBLOCK) < 0) {
121 int e = errno;
122 ostringstream oss;
123 oss << "AdminSocket::create_wakeup_pipe error: " << cpp_strerror(e);
124 return oss.str();
125 }
126
127 *pipe_rd = pipefd[0];
128 *pipe_wr = pipefd[1];
129 return "";
130 }
131
132 std::string AdminSocket::destroy_wakeup_pipe()
133 {
134 // Send a byte to the wakeup pipe that the thread is listening to
135 char buf[1] = { 0x0 };
136 int ret = safe_write(m_wakeup_wr_fd, buf, sizeof(buf));
137
138 // Close write end
139 retry_sys_call(::close, m_wakeup_wr_fd);
140 m_wakeup_wr_fd = -1;
141
142 if (ret != 0) {
143 ostringstream oss;
144 oss << "AdminSocket::destroy_shutdown_pipe error: failed to write"
145 "to thread shutdown pipe: error " << ret;
146 return oss.str();
147 }
148
149 th.join();
150
151 // Close read end. Doing this before join() blocks the listenter and prevents
152 // joining.
153 retry_sys_call(::close, m_wakeup_rd_fd);
154 m_wakeup_rd_fd = -1;
155
156 return "";
157 }
158
159 std::string AdminSocket::bind_and_listen(const std::string &sock_path, int *fd)
160 {
161 ldout(m_cct, 5) << "bind_and_listen " << sock_path << dendl;
162
163 struct sockaddr_un address;
164 if (sock_path.size() > sizeof(address.sun_path) - 1) {
165 ostringstream oss;
166 oss << "AdminSocket::bind_and_listen: "
167 << "The UNIX domain socket path " << sock_path << " is too long! The "
168 << "maximum length on this system is "
169 << (sizeof(address.sun_path) - 1);
170 return oss.str();
171 }
172 int sock_fd = socket_cloexec(PF_UNIX, SOCK_STREAM, 0);
173 if (sock_fd < 0) {
174 int err = errno;
175 ostringstream oss;
176 oss << "AdminSocket::bind_and_listen: "
177 << "failed to create socket: " << cpp_strerror(err);
178 return oss.str();
179 }
180 // FIPS zeroization audit 20191115: this memset is fine.
181 memset(&address, 0, sizeof(struct sockaddr_un));
182 address.sun_family = AF_UNIX;
183 snprintf(address.sun_path, sizeof(address.sun_path),
184 "%s", sock_path.c_str());
185 if (::bind(sock_fd, (struct sockaddr*)&address,
186 sizeof(struct sockaddr_un)) != 0) {
187 int err = errno;
188 if (err == EADDRINUSE) {
189 AdminSocketClient client(sock_path);
190 bool ok;
191 client.ping(&ok);
192 if (ok) {
193 ldout(m_cct, 20) << "socket " << sock_path << " is in use" << dendl;
194 err = EEXIST;
195 } else {
196 ldout(m_cct, 20) << "unlink stale file " << sock_path << dendl;
197 retry_sys_call(::unlink, sock_path.c_str());
198 if (::bind(sock_fd, (struct sockaddr*)&address,
199 sizeof(struct sockaddr_un)) == 0) {
200 err = 0;
201 } else {
202 err = errno;
203 }
204 }
205 }
206 if (err != 0) {
207 ostringstream oss;
208 oss << "AdminSocket::bind_and_listen: "
209 << "failed to bind the UNIX domain socket to '" << sock_path
210 << "': " << cpp_strerror(err);
211 close(sock_fd);
212 return oss.str();
213 }
214 }
215 if (listen(sock_fd, 5) != 0) {
216 int err = errno;
217 ostringstream oss;
218 oss << "AdminSocket::bind_and_listen: "
219 << "failed to listen to socket: " << cpp_strerror(err);
220 close(sock_fd);
221 retry_sys_call(::unlink, sock_path.c_str());
222 return oss.str();
223 }
224 *fd = sock_fd;
225 return "";
226 }
227
228 void AdminSocket::entry() noexcept
229 {
230 ldout(m_cct, 5) << "entry start" << dendl;
231 while (true) {
232 struct pollfd fds[2];
233 // FIPS zeroization audit 20191115: this memset is fine.
234 memset(fds, 0, sizeof(fds));
235 fds[0].fd = m_sock_fd;
236 fds[0].events = POLLIN | POLLRDBAND;
237 fds[1].fd = m_wakeup_rd_fd;
238 fds[1].events = POLLIN | POLLRDBAND;
239
240 ldout(m_cct,20) << __func__ << " waiting" << dendl;
241 int ret = poll(fds, 2, -1);
242 if (ret < 0) {
243 int err = errno;
244 if (err == EINTR) {
245 continue;
246 }
247 lderr(m_cct) << "AdminSocket: poll(2) error: '"
248 << cpp_strerror(err) << dendl;
249 return;
250 }
251 ldout(m_cct,20) << __func__ << " awake" << dendl;
252
253 if (fds[0].revents & POLLIN) {
254 // Send out some data
255 do_accept();
256 }
257 if (fds[1].revents & POLLIN) {
258 // read off one byte
259 char buf;
260 auto s = ::read(m_wakeup_rd_fd, &buf, 1);
261 if (s == -1) {
262 int e = errno;
263 ldout(m_cct, 5) << "AdminSocket: (ignoring) read(2) error: '"
264 << cpp_strerror(e) << dendl;
265 }
266 do_tell_queue();
267 }
268 if (m_shutdown) {
269 // Parent wants us to shut down
270 return;
271 }
272 }
273 ldout(m_cct, 5) << "entry exit" << dendl;
274 }
275
276 void AdminSocket::chown(uid_t uid, gid_t gid)
277 {
278 if (m_sock_fd >= 0) {
279 int r = ::chown(m_path.c_str(), uid, gid);
280 if (r < 0) {
281 r = -errno;
282 lderr(m_cct) << "AdminSocket: failed to chown socket: "
283 << cpp_strerror(r) << dendl;
284 }
285 }
286 }
287
288 void AdminSocket::chmod(mode_t mode)
289 {
290 if (m_sock_fd >= 0) {
291 int r = ::chmod(m_path.c_str(), mode);
292 if (r < 0) {
293 r = -errno;
294 lderr(m_cct) << "AdminSocket: failed to chmod socket: "
295 << cpp_strerror(r) << dendl;
296 }
297 }
298 }
299
300 void AdminSocket::do_accept()
301 {
302 struct sockaddr_un address;
303 socklen_t address_length = sizeof(address);
304 ldout(m_cct, 30) << "AdminSocket: calling accept" << dendl;
305 int connection_fd = accept_cloexec(m_sock_fd, (struct sockaddr*) &address,
306 &address_length);
307 if (connection_fd < 0) {
308 int err = errno;
309 lderr(m_cct) << "AdminSocket: do_accept error: '"
310 << cpp_strerror(err) << dendl;
311 return;
312 }
313 ldout(m_cct, 30) << "AdminSocket: finished accept" << dendl;
314
315 char cmd[1024];
316 unsigned pos = 0;
317 string c;
318 while (1) {
319 int ret = safe_read(connection_fd, &cmd[pos], 1);
320 if (ret <= 0) {
321 if (ret < 0) {
322 lderr(m_cct) << "AdminSocket: error reading request code: "
323 << cpp_strerror(ret) << dendl;
324 }
325 retry_sys_call(::close, connection_fd);
326 return;
327 }
328 if (cmd[0] == '\0') {
329 // old protocol: __be32
330 if (pos == 3 && cmd[0] == '\0') {
331 switch (cmd[3]) {
332 case 0:
333 c = "0";
334 break;
335 case 1:
336 c = "perfcounters_dump";
337 break;
338 case 2:
339 c = "perfcounters_schema";
340 break;
341 default:
342 c = "foo";
343 break;
344 }
345 //wrap command with new protocol
346 c = "{\"prefix\": \"" + c + "\"}";
347 break;
348 }
349 } else {
350 // new protocol: null or \n terminated string
351 if (cmd[pos] == '\n' || cmd[pos] == '\0') {
352 cmd[pos] = '\0';
353 c = cmd;
354 break;
355 }
356 }
357 if (++pos >= sizeof(cmd)) {
358 lderr(m_cct) << "AdminSocket: error reading request too long" << dendl;
359 retry_sys_call(::close, connection_fd);
360 return;
361 }
362 }
363
364 std::vector<std::string> cmdvec = { c };
365 bufferlist empty, out;
366 ostringstream err;
367 int rval = execute_command(cmdvec, empty /* inbl */, err, &out);
368
369 // Unfortunately, the asok wire protocol does not let us pass an error code,
370 // and many asok command implementations return helpful error strings. So,
371 // let's prepend an error string to the output if there is an error code.
372 if (rval < 0) {
373 ostringstream ss;
374 ss << "ERROR: " << cpp_strerror(rval) << "\n";
375 ss << err.str() << "\n";
376 bufferlist o;
377 o.append(ss.str());
378 o.claim_append(out);
379 out.claim_append(o);
380 }
381 uint32_t len = htonl(out.length());
382 int ret = safe_write(connection_fd, &len, sizeof(len));
383 if (ret < 0) {
384 lderr(m_cct) << "AdminSocket: error writing response length "
385 << cpp_strerror(ret) << dendl;
386 } else {
387 if (out.write_fd(connection_fd) < 0) {
388 lderr(m_cct) << "AdminSocket: error writing response payload "
389 << cpp_strerror(ret) << dendl;
390 }
391 }
392 retry_sys_call(::close, connection_fd);
393 }
394
395 void AdminSocket::do_tell_queue()
396 {
397 ldout(m_cct,10) << __func__ << dendl;
398 std::list<cref_t<MCommand>> q;
399 std::list<cref_t<MMonCommand>> lq;
400 {
401 std::lock_guard l(tell_lock);
402 q.swap(tell_queue);
403 lq.swap(tell_legacy_queue);
404 }
405 for (auto& m : q) {
406 bufferlist outbl;
407 execute_command(
408 m->cmd,
409 m->get_data(),
410 [m](int r, const std::string& err, bufferlist& outbl) {
411 auto reply = new MCommandReply(r, err);
412 reply->set_tid(m->get_tid());
413 reply->set_data(outbl);
414 #ifdef WITH_SEASTAR
415 #warning "fix message send with crimson"
416 #else
417 m->get_connection()->send_message(reply);
418 #endif
419 });
420 }
421 for (auto& m : lq) {
422 bufferlist outbl;
423 execute_command(
424 m->cmd,
425 m->get_data(),
426 [m](int r, const std::string& err, bufferlist& outbl) {
427 auto reply = new MMonCommandAck(m->cmd, r, err, 0);
428 reply->set_tid(m->get_tid());
429 reply->set_data(outbl);
430 #ifdef WITH_SEASTAR
431 #warning "fix message send with crimson"
432 #else
433 m->get_connection()->send_message(reply);
434 #endif
435 });
436 }
437 }
438
439 int AdminSocket::execute_command(
440 const std::vector<std::string>& cmd,
441 const bufferlist& inbl,
442 std::ostream& errss,
443 bufferlist *outbl)
444 {
445 #ifdef WITH_SEASTAR
446 #warning "must implement admin socket blocking execute_command() for crimson"
447 return -ENOSYS;
448 #else
449 bool done = false;
450 int rval = 0;
451 ceph::mutex mylock = ceph::make_mutex("admin_socket::excute_command::mylock");
452 ceph::condition_variable mycond;
453 C_SafeCond fin(mylock, mycond, &done, &rval);
454 execute_command(
455 cmd,
456 inbl,
457 [&errss, outbl, &fin](int r, const std::string& err, bufferlist& out) {
458 errss << err;
459 outbl->claim(out);
460 fin.finish(r);
461 });
462 {
463 std::unique_lock l{mylock};
464 mycond.wait(l, [&done] { return done;});
465 }
466 return rval;
467 #endif
468 }
469
470 void AdminSocket::execute_command(
471 const std::vector<std::string>& cmdvec,
472 const bufferlist& inbl,
473 std::function<void(int,const std::string&,bufferlist&)> on_finish)
474 {
475 cmdmap_t cmdmap;
476 string format;
477 stringstream errss;
478 bufferlist empty;
479 ldout(m_cct,10) << __func__ << " cmdvec='" << cmdvec << "'" << dendl;
480 if (!cmdmap_from_json(cmdvec, &cmdmap, errss)) {
481 ldout(m_cct, 0) << "AdminSocket: " << errss.str() << dendl;
482 return on_finish(-EINVAL, "invalid json", empty);
483 }
484 string prefix;
485 try {
486 cmd_getval(cmdmap, "format", format);
487 cmd_getval(cmdmap, "prefix", prefix);
488 } catch (const bad_cmd_get& e) {
489 return on_finish(-EINVAL, "invalid json, missing format and/or prefix",
490 empty);
491 }
492
493 Formatter *f = Formatter::create(format, "json-pretty", "json-pretty");
494
495 auto [retval, hook] = find_matched_hook(prefix, cmdmap);
496 switch (retval) {
497 case ENOENT:
498 lderr(m_cct) << "AdminSocket: request '" << cmdvec
499 << "' not defined" << dendl;
500 delete f;
501 return on_finish(-EINVAL, "unknown command prefix "s + prefix, empty);
502 case EINVAL:
503 delete f;
504 return on_finish(-EINVAL, "invalid command json", empty);
505 default:
506 assert(retval == 0);
507 }
508
509 hook->call_async(
510 prefix, cmdmap, f, inbl,
511 [f, on_finish](int r, const std::string& err, bufferlist& out) {
512 // handle either existing output in bufferlist *or* via formatter
513 if (r >= 0 && out.length() == 0) {
514 f->flush(out);
515 }
516 delete f;
517 on_finish(r, err, out);
518 });
519
520 std::unique_lock l(lock);
521 in_hook = false;
522 in_hook_cond.notify_all();
523 }
524
525 std::pair<int, AdminSocketHook*>
526 AdminSocket::find_matched_hook(std::string& prefix,
527 const cmdmap_t& cmdmap)
528 {
529 std::unique_lock l(lock);
530 // Drop lock after done with the lookup to avoid cycles in cases where the
531 // hook takes the same lock that was held during calls to
532 // register/unregister, and set in_hook to allow unregister to wait for us
533 // before removing this hook.
534 auto [hooks_begin, hooks_end] = hooks.equal_range(prefix);
535 if (hooks_begin == hooks_end) {
536 return {ENOENT, nullptr};
537 }
538 // make sure one of the registered commands with this prefix validates.
539 stringstream errss;
540 for (auto hook = hooks_begin; hook != hooks_end; ++hook) {
541 if (validate_cmd(m_cct, hook->second.desc, cmdmap, errss)) {
542 in_hook = true;
543 return {0, hook->second.hook};
544 }
545 }
546 return {EINVAL, nullptr};
547 }
548
549 void AdminSocket::queue_tell_command(cref_t<MCommand> m)
550 {
551 ldout(m_cct,10) << __func__ << " " << *m << dendl;
552 std::lock_guard l(tell_lock);
553 tell_queue.push_back(std::move(m));
554 wakeup();
555 }
556 void AdminSocket::queue_tell_command(cref_t<MMonCommand> m)
557 {
558 ldout(m_cct,10) << __func__ << " " << *m << dendl;
559 std::lock_guard l(tell_lock);
560 tell_legacy_queue.push_back(std::move(m));
561 wakeup();
562 }
563
564 int AdminSocket::register_command(std::string_view cmddesc,
565 AdminSocketHook *hook,
566 std::string_view help)
567 {
568 int ret;
569 std::unique_lock l(lock);
570 string prefix = cmddesc_get_prefix(cmddesc);
571 auto i = hooks.find(prefix);
572 if (i != hooks.cend() &&
573 i->second.desc == cmddesc) {
574 ldout(m_cct, 5) << "register_command " << prefix
575 << " cmddesc " << cmddesc << " hook " << hook
576 << " EEXIST" << dendl;
577 ret = -EEXIST;
578 } else {
579 ldout(m_cct, 5) << "register_command " << prefix << " hook " << hook
580 << dendl;
581 hooks.emplace_hint(i,
582 std::piecewise_construct,
583 std::forward_as_tuple(prefix),
584 std::forward_as_tuple(hook, cmddesc, help));
585 ret = 0;
586 }
587 return ret;
588 }
589
590 void AdminSocket::unregister_commands(const AdminSocketHook *hook)
591 {
592 std::unique_lock l(lock);
593 auto i = hooks.begin();
594 while (i != hooks.end()) {
595 if (i->second.hook == hook) {
596 ldout(m_cct, 5) << __func__ << " " << i->first << dendl;
597
598 // If we are currently processing a command, wait for it to
599 // complete in case it referenced the hook that we are
600 // unregistering.
601 in_hook_cond.wait(l, [this]() { return !in_hook; });
602 hooks.erase(i++);
603 } else {
604 i++;
605 }
606 }
607 }
608
609 class VersionHook : public AdminSocketHook {
610 public:
611 int call(std::string_view command, const cmdmap_t& cmdmap,
612 Formatter *f,
613 std::ostream& errss,
614 bufferlist& out) override {
615 if (command == "0"sv) {
616 out.append(CEPH_ADMIN_SOCK_VERSION);
617 } else {
618 f->open_object_section("version");
619 if (command == "version") {
620 f->dump_string("version", ceph_version_to_str());
621 f->dump_string("release", ceph_release_to_str());
622 f->dump_string("release_type", ceph_release_type());
623 } else if (command == "git_version") {
624 f->dump_string("git_version", git_version_to_str());
625 }
626 ostringstream ss;
627 f->close_section();
628 }
629 return 0;
630 }
631 };
632
633 class HelpHook : public AdminSocketHook {
634 AdminSocket *m_as;
635 public:
636 explicit HelpHook(AdminSocket *as) : m_as(as) {}
637 int call(std::string_view command, const cmdmap_t& cmdmap,
638 Formatter *f,
639 std::ostream& errss,
640 bufferlist& out) override {
641 f->open_object_section("help");
642 for (const auto& [command, info] : m_as->hooks) {
643 if (info.help.length())
644 f->dump_string(command.c_str(), info.help);
645 }
646 f->close_section();
647 return 0;
648 }
649 };
650
651 class GetdescsHook : public AdminSocketHook {
652 AdminSocket *m_as;
653 public:
654 explicit GetdescsHook(AdminSocket *as) : m_as(as) {}
655 int call(std::string_view command, const cmdmap_t& cmdmap,
656 Formatter *f,
657 std::ostream& errss,
658 bufferlist& out) override {
659 int cmdnum = 0;
660 f->open_object_section("command_descriptions");
661 for (const auto& [command, info] : m_as->hooks) {
662 // GCC 8 actually has [[maybe_unused]] on a structured binding
663 // do what you'd expect. GCC 7 does not.
664 (void)command;
665 ostringstream secname;
666 secname << "cmd" << setfill('0') << std::setw(3) << cmdnum;
667 dump_cmd_and_help_to_json(f,
668 CEPH_FEATURES_ALL,
669 secname.str().c_str(),
670 info.desc,
671 info.help);
672 cmdnum++;
673 }
674 f->close_section(); // command_descriptions
675 return 0;
676 }
677 };
678
679 bool AdminSocket::init(const std::string& path)
680 {
681 ldout(m_cct, 5) << "init " << path << dendl;
682
683 /* Set up things for the new thread */
684 std::string err;
685 int pipe_rd = -1, pipe_wr = -1;
686 err = create_wakeup_pipe(&pipe_rd, &pipe_wr);
687 if (!err.empty()) {
688 lderr(m_cct) << "AdminSocketConfigObs::init: error: " << err << dendl;
689 return false;
690 }
691 int sock_fd;
692 err = bind_and_listen(path, &sock_fd);
693 if (!err.empty()) {
694 lderr(m_cct) << "AdminSocketConfigObs::init: failed: " << err << dendl;
695 close(pipe_rd);
696 close(pipe_wr);
697 return false;
698 }
699
700 /* Create new thread */
701 m_sock_fd = sock_fd;
702 m_wakeup_rd_fd = pipe_rd;
703 m_wakeup_wr_fd = pipe_wr;
704 m_path = path;
705
706 version_hook = std::make_unique<VersionHook>();
707 register_command("0", version_hook.get(), "");
708 register_command("version", version_hook.get(), "get ceph version");
709 register_command("git_version", version_hook.get(),
710 "get git sha1");
711 help_hook = std::make_unique<HelpHook>(this);
712 register_command("help", help_hook.get(),
713 "list available commands");
714 getdescs_hook = std::make_unique<GetdescsHook>(this);
715 register_command("get_command_descriptions",
716 getdescs_hook.get(), "list available commands");
717
718 th = make_named_thread("admin_socket", &AdminSocket::entry, this);
719 add_cleanup_file(m_path.c_str());
720 return true;
721 }
722
723 void AdminSocket::shutdown()
724 {
725 // Under normal operation this is unlikely to occur. However for some unit
726 // tests, some object members are not initialized and so cannot be deleted
727 // without fault.
728 if (m_wakeup_wr_fd < 0)
729 return;
730
731 ldout(m_cct, 5) << "shutdown" << dendl;
732 m_shutdown = true;
733
734 auto err = destroy_wakeup_pipe();
735 if (!err.empty()) {
736 lderr(m_cct) << "AdminSocket::shutdown: error: " << err << dendl;
737 }
738
739 retry_sys_call(::close, m_sock_fd);
740
741 unregister_commands(version_hook.get());
742 version_hook.reset();
743
744 unregister_commands(help_hook.get());
745 help_hook.reset();
746
747 unregister_commands(getdescs_hook.get());
748 getdescs_hook.reset();
749
750 remove_cleanup_file(m_path);
751 m_path.clear();
752 }
753
754 void AdminSocket::wakeup()
755 {
756 // Send a byte to the wakeup pipe that the thread is listening to
757 char buf[1] = { 0x0 };
758 int r = safe_write(m_wakeup_wr_fd, buf, sizeof(buf));
759 (void)r;
760 }