]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
libcephfs: choose more unique nonce
authorSage Weil <sage@inktank.com>
Mon, 31 Dec 2012 23:22:23 +0000 (15:22 -0800)
committerSage Weil <sage@inktank.com>
Mon, 31 Dec 2012 23:26:54 +0000 (15:26 -0800)
We were using a per-process counter combined with the pid.  A short
running process can easily loop through and reuse the same pid later.
Instead, go for 48 bits of randomness and the pid.  This way if we get
a dup pid we'll only get a dup nonce once out of 2^48 tries.

Avoids #3630 when running a libcephfs test in a loop (so that the pid
is eventually reused).  This is a better fix than the broken
8b599083705c2495810c00f9f5fd5bb8ace7f32e.  The real solution on the MDS
side involves cleaning up the msgr/MDS interaction with session
shutdown.

Signed-off-by: Sage Weil <sage@inktank.com>
src/libcephfs.cc

index 55bcfed1ff713ca99963ea5b3b3a6ba941424870..0bb508ff91209244116a8a43369df76f28f17cc9 100644 (file)
@@ -17,6 +17,7 @@
 #include <string.h>
 #include <string>
 
+#include "auth/Crypto.h"
 #include "client/Client.h"
 #include "include/cephfs/libcephfs.h"
 #include "common/Mutex.h"
@@ -222,10 +223,13 @@ extern "C" const char *ceph_version(int *pmajor, int *pminor, int *ppatch)
 
 extern "C" int ceph_create_with_context(struct ceph_mount_info **cmount, CephContext *cct)
 {
-  // Function-static variables are thread-safe in gcc and in the forthcoming C++ standard
-  static int nonce_seed = 0;
+  uint64_t nonce = 0;
+
+  // 6 bytes of random and 2 bytes of pid
+  get_random_bytes((char*)&nonce, sizeof(nonce));
+  nonce &= ~0xffff;
+  nonce |= (uint64_t)getpid();
 
-  uint64_t nonce = (uint64_t)++nonce_seed * 1000000ull + (uint64_t)getpid();
   *cmount = new struct ceph_mount_info(nonce, cct);
   return 0;
 }