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: v12.0.2~267^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=3ba01aa6ce052d1afa42132feffc2353d73caae6;p=ceph-ci.git rbd-nbd: support signal handle for SIGHUP, SIGINT, and SIGTERM. Fixes: http://tracker.ceph.com/issues/19349 Signed-off-by: Pan Liu --- diff --git a/src/tools/rbd_nbd/rbd-nbd.cc b/src/tools/rbd_nbd/rbd-nbd.cc index ebb143f69fe..3a55cf2b163 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" @@ -78,6 +79,7 @@ static int nbds_max = 0; static int max_part = 255; static bool set_max_part = false; static bool exclusive = false; +static int nbd = -1; #define RBD_NBD_BLKSIZE 512UL @@ -90,6 +92,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: @@ -522,7 +531,6 @@ static int do_map(int argc, const char *argv[]) int index = 0; int fd[2]; - int nbd; librbd::image_info_t info; @@ -704,7 +712,19 @@ static int do_map(int argc, const char *argv[]) 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(); }