ntdb: don't call open hook when re-opening an existing database.
authorRusty Russell <rusty@rustcorp.com.au>
Sat, 23 Mar 2013 06:57:57 +0000 (17:27 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Sat, 23 Mar 2013 08:39:50 +0000 (09:39 +0100)
In particular, the Samba dbwrap wrapper can do this for schannel_store,
with the openhook set to clear the database if it can get the lock
(which, being in the same process, it can).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Autobuild-User(master): Rusty Russell <rusty@rustcorp.com.au>
Autobuild-Date(master): Sat Mar 23 09:39:50 CET 2013 on sn-devel-104

lib/ntdb/open.c
lib/ntdb/test/api-83-openhook.c

index abec1172362df51357dbaf7bb4d41c95d28c82c2..2a265afe7d585aac6f975ab2f5837ed7645586cf 100644 (file)
@@ -679,6 +679,17 @@ _PUBLIC_ struct ntdb_context *ntdb_open(const char *name, int ntdb_flags,
 
                ntdb->file->device = st.st_dev;
                ntdb->file->inode = st.st_ino;
+
+               /* call their open hook if they gave us one. */
+               if (ntdb->openhook) {
+                       ecode = ntdb->openhook(ntdb->file->fd, ntdb->openhook_data);
+                       if (ecode != NTDB_SUCCESS) {
+                               ntdb_logerr(ntdb, ecode, NTDB_LOG_ERROR,
+                                           "ntdb_open: open hook failed");
+                               goto fail;
+                       }
+                       open_flags |= O_CREAT;
+               }
        } else {
                /* ensure there is only one process initialising at once */
                ecode = ntdb_lock_open(ntdb, openlock,
@@ -689,17 +700,6 @@ _PUBLIC_ struct ntdb_context *ntdb_open(const char *name, int ntdb_flags,
                }
        }
 
-       /* call their open hook if they gave us one. */
-       if (ntdb->openhook) {
-               ecode = ntdb->openhook(ntdb->file->fd, ntdb->openhook_data);
-               if (ecode != NTDB_SUCCESS) {
-                       ntdb_logerr(ntdb, ecode, NTDB_LOG_ERROR,
-                                  "ntdb_open: open hook failed");
-                       goto fail;
-               }
-               open_flags |= O_CREAT;
-       }
-
        /* If they used O_TRUNC, read will return 0. */
        rlen = pread(ntdb->file->fd, &hdr, sizeof(hdr), 0);
        if (rlen == 0 && (open_flags & O_CREAT)) {
index 31c789ead85b4be2a9874de32fd1e729f52b5316..3816eef209ccff24013ff8bc07273f8b4b786658 100644 (file)
@@ -44,7 +44,7 @@ static enum NTDB_ERROR clear_if_first(int fd, void *arg)
 int main(int argc, char *argv[])
 {
        unsigned int i;
-       struct ntdb_context *ntdb;
+       struct ntdb_context *ntdb, *ntdb2;
        struct agent *agent;
        union ntdb_attribute cif;
        NTDB_DATA key = ntdb_mkdata(KEY_STR, strlen(KEY_STR));
@@ -57,7 +57,7 @@ int main(int argc, char *argv[])
        cif.openhook.data = clear_if_first;
 
        agent = prepare_external_agent();
-       plan_tests(sizeof(flags) / sizeof(flags[0]) * 13);
+       plan_tests(sizeof(flags) / sizeof(flags[0]) * 16);
        for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
                /* Create it */
                ntdb = ntdb_open("run-83-openhook.ntdb", flags[i]|MAYBE_NOSYNC,
@@ -83,6 +83,15 @@ int main(int argc, char *argv[])
                /* Still exists for us too. */
                ok1(ntdb_exists(ntdb, key));
 
+               /* Nested open should not erase db. */
+               ntdb2 = ntdb_open("run-83-openhook.ntdb", flags[i]|MAYBE_NOSYNC,
+                                 O_RDWR, 0, &cif);
+               ok1(ntdb_exists(ntdb2, key));
+               ok1(ntdb_exists(ntdb, key));
+               ntdb_close(ntdb2);
+
+               ok1(ntdb_exists(ntdb, key));
+
                /* Close it, now agent should clear it. */
                ntdb_close(ntdb);