s3:registry: fix seqnum race in fetch_values_internal
authorMichael Adam <obnox@samba.org>
Wed, 11 Apr 2012 13:48:02 +0000 (15:48 +0200)
committerKarolin Seeger <kseeger@samba.org>
Thu, 10 May 2012 09:15:32 +0000 (11:15 +0200)
This prevents race between fetching seqnum and key content.

Because there is currently no way to atomically fetch the
record along with the seqnum, I use a loop.
This is far from optimal and should should ideally be done
differently. But for now it fixes the race.
(cherry picked from commit 13bccba3c2f6e6fdda2b4a40dd4b1e250a98a7ef)

Conflicts:

source3/registry/reg_backend_db.c

source3/registry/reg_backend_db.c

index e0fe7c121151b23f70293f155de2c6ba788b4aa5..c2c36dde141f280e0543b9e23859f1637e6b4e06 100644 (file)
@@ -1765,6 +1765,7 @@ static int regdb_fetch_values_internal(struct db_context *db, const char* key,
        int ret = 0;
        TDB_DATA value;
        WERROR werr;
+       int seqnum[2], count;
 
        DEBUG(10,("regdb_fetch_values: Looking for values of key [%s]\n", key));
 
@@ -1777,10 +1778,27 @@ static int regdb_fetch_values_internal(struct db_context *db, const char* key,
                goto done;
        }
 
-       werr = regval_ctr_set_seqnum(values, db->get_seqnum(db));
-       W_ERROR_NOT_OK_GOTO_DONE(werr);
+       ZERO_STRUCT(value);
+       count = 0;
+       seqnum[0] = db->get_seqnum(db);
+
+       do {
+               count++;
+               TALLOC_FREE(value.dptr);
+               value = regdb_fetch_key_internal(db, ctx, keystr);
+               seqnum[count % 2] = db->get_seqnum(db);
+       } while (seqnum[0] != seqnum[1]);
+
+       if (count > 1) {
+               DEBUG(5, ("regdb_fetch_values_internal: it took %d attempts "
+                         "to fetch key '%s' with constant seqnum\n",
+                         count, key));
+       }
 
-       value = regdb_fetch_key_internal(db, ctx, keystr);
+       werr = regval_ctr_set_seqnum(values, seqnum[0]);
+       if (!W_ERROR_IS_OK(werr)) {
+               goto done;
+       }
 
        if (!value.dptr) {
                /* all keys have zero values by default */