summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMathias Stearn <redbeard0531@gmail.com>2009-11-23 11:30:01 -0500
committerMathias Stearn <redbeard0531@gmail.com>2009-11-23 11:33:27 -0500
commit498d87fe5b85c8bd3db9ecfb663e8ee53feebc71 (patch)
treec0855fa7a1f2fb04bdd90faa6b9f7c091b9b8e00
parent33c85f0d84bbb72833e3f5f0ffe1612ae8243bdf (diff)
downloadmongo-498d87fe5b85c8bd3db9ecfb663e8ee53feebc71.tar.gz
OID::newState to generate fresh "machine" bits
-rw-r--r--db/jsobj.cpp11
-rw-r--r--db/jsobj.h6
2 files changed, 14 insertions, 3 deletions
diff --git a/db/jsobj.cpp b/db/jsobj.cpp
index 95650aeb15f..efadfc16406 100644
--- a/db/jsobj.cpp
+++ b/db/jsobj.cpp
@@ -1423,10 +1423,14 @@ namespace mongo {
}
*/
+ unsigned OID::_machine = (unsigned) security.getNonce();
+ void OID::newState(){
+ // using fresh Security object to avoid buffered devrandom
+ _machine = (unsigned) Security().getNonce();
+ }
+
void OID::init() {
- static unsigned machine = (unsigned) security.getNonce();
static unsigned inc = (unsigned) security.getNonce();
-
unsigned t = (unsigned) time(0);
char *T = (char *) &t;
data[0] = T[3];
@@ -1434,7 +1438,8 @@ namespace mongo {
data[2] = T[1];
data[3] = T[0];
- (unsigned&) data[4] = machine;
+ (unsigned&) data[4] = _machine;
+
// TODO: use compiler intrinsic atomic increment instead of inc++
int old_inc = inc++;
T = (char *) &old_inc;
diff --git a/db/jsobj.h b/db/jsobj.h
index 2a3030f9ffa..c329613e01c 100644
--- a/db/jsobj.h
+++ b/db/jsobj.h
@@ -109,6 +109,8 @@ namespace mongo {
is likely to be unique to the system. You may also use other types for _id's.
When _id field is missing from a BSON object, on an insert the database may insert one
automatically in certain circumstances.
+
+ Warning: You must call OID::newState() after a fork().
*/
class OID {
union {
@@ -118,7 +120,11 @@ namespace mongo {
};
unsigned char data[12];
};
+ static unsigned _machine;
public:
+ /** call this after a fork */
+ static void newState();
+
/** initialize to 'null' */
void clear() { a = 0; b = 0; }