From: Pan Liu Date: Tue, 28 Mar 2017 08:33:25 +0000 (+0800) Subject: rbd-nbd: support signal handle for SIGHUP, SIGINT, and SIGTERM. X-Git-Tag: v11.2.1~101^2~3 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=e6a01244e6ae2862b1c6e78be67b3c2be6899364;p=ceph.git rbd-nbd: support signal handle for SIGHUP, SIGINT, and SIGTERM. Fixes: http://tracker.ceph.com/issues/19349 Signed-off-by: Pan Liu (cherry picked from commit 3ba01aa6ce052d1afa42132feffc2353d73caae6) --- diff --git a/src/tools/rbd_nbd/rbd-nbd.cc b/src/tools/rbd_nbd/rbd-nbd.cc index 04a747cbf9a..047f3e5c0ed 100644 --- a/src/tools/rbd_nbd/rbd-nbd.cc +++ b/src/tools/rbd_nbd/rbd-nbd.cc @@ -46,6 +46,7 @@ #include "common/ceph_argparse.h" #include "common/Preforker.h" #include "global/global_init.h" +#include "global/signal_handler.h" #include "include/rados/librados.hpp" #include "include/rbd/librbd.hpp" @@ -75,6 +76,7 @@ static bool readonly = false; static int nbds_max = 0; static int max_part = 255; static bool exclusive = false; +static int nbd = -1; #ifdef CEPH_BIG_ENDIAN #define ntohll(a) (a) @@ -85,6 +87,13 @@ static bool exclusive = false; #endif #define htonll(a) ntohll(a) +static void handle_signal(int signum) +{ + assert(signum == SIGINT || signum == SIGTERM); + derr << "*** Got signal " << sig_str(signum) << " ***" << dendl; + ioctl(nbd, NBD_DISCONNECT); +} + class NBDServer { private: @@ -480,7 +489,6 @@ static int do_map() unsigned long size; int fd[2]; - int nbd; uint8_t old_format; librbd::image_info_t info; @@ -645,7 +653,19 @@ static int do_map() NBDServer server(fd[1], image); server.start(); + + init_async_signal_handler(); + register_async_signal_handler(SIGHUP, sighup_handler); + register_async_signal_handler_oneshot(SIGINT, handle_signal); + register_async_signal_handler_oneshot(SIGTERM, handle_signal); + ioctl(nbd, NBD_DO_IT); + + unregister_async_signal_handler(SIGHUP, sighup_handler); + unregister_async_signal_handler(SIGINT, handle_signal); + unregister_async_signal_handler(SIGTERM, handle_signal); + shutdown_async_signal_handler(); + server.stop(); }