]> 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)
committerPan Liu <liupan1111@gmail.com>
Tue, 28 Mar 2017 08:33:25 +0000 (16:33 +0800)
Fixes: http://tracker.ceph.com/issues/19349
Signed-off-by: Pan Liu <liupan1111@gmail.com>
src/tools/rbd_nbd/rbd-nbd.cc

index ebb143f69fee56409b024413c4dcec8127f8b0de..3a55cf2b16373a1b3210fa83d9ad9e34931915f0 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"
@@ -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();
     }