]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
rbd-nbd: support signal handle for SIGHUP, SIGINT, and SIGTERM.
authorPan Liu <liupan1111@gmail.com>
Tue, 28 Mar 2017 08:33:25 +0000 (16:33 +0800)
committerNathan Cutler <ncutler@suse.com>
Tue, 4 Jul 2017 08:56:34 +0000 (10:56 +0200)
Fixes: http://tracker.ceph.com/issues/19349
Signed-off-by: Pan Liu <liupan1111@gmail.com>
(cherry picked from commit 3ba01aa6ce052d1afa42132feffc2353d73caae6)

src/tools/rbd_nbd/rbd-nbd.cc

index 04a747cbf9a67ff310a8ddea0e39a620bcd73ab4..047f3e5c0edd1ccf79578a6c55841f525502d8fb 100644 (file)
@@ -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();
     }