fsrvp: add tdb path arg to state read/write functions
authorDavid Disseldorp <ddiss@samba.org>
Wed, 10 Oct 2012 12:55:52 +0000 (14:55 +0200)
committerDavid Disseldorp <ddiss@samba.org>
Mon, 15 Apr 2013 16:15:21 +0000 (18:15 +0200)
This allows the local.fsrvp_state tests to run under selftest, where a
writeable lock dir path is not configured for smbtorture.

source3/rpc_server/fss/srv_fss_agent.c
source3/rpc_server/fss/srv_fss_private.h
source3/rpc_server/fss/srv_fss_state.c
source4/torture/local/fsrvp_state.c

index e12412ec82552867a368cf5ffad9c27b86871b63..6f3038b5eff62e9aeccb44a1772af05626a904cb 100644 (file)
@@ -182,6 +182,7 @@ static struct fss_sc_smap *sc_smap_lookup(struct fss_sc_smap *smaps_head,
 
 void srv_fssa_cleanup(void)
 {
+       talloc_free(fss_global.db_path);
        talloc_free(fss_global.mem_ctx);
        ZERO_STRUCT(fss_global);
 }
@@ -196,6 +197,12 @@ NTSTATUS srv_fssa_start(void)
                return NT_STATUS_NO_MEMORY;
        }
 
+       fss_global.db_path = lock_path(FSS_DB_NAME);
+       if (fss_global.db_path == NULL) {
+               talloc_free(fss_global.mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
+
        fss_global.min_vers = FSRVP_RPC_VERSION_1;
        fss_global.max_vers = FSRVP_RPC_VERSION_1;
        /*
@@ -204,7 +211,8 @@ NTSTATUS srv_fssa_start(void)
         */
        become_root();
        status = fss_state_retrieve(fss_global.mem_ctx, &fss_global.sc_sets,
-                                   &fss_global.sc_sets_count);
+                                   &fss_global.sc_sets_count,
+                                   fss_global.db_path);
        unbecome_root();
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(1, ("failed to retrieve fss server state: %s\n",
@@ -760,7 +768,8 @@ static void fss_commit_sc_set_done(struct tevent_req *subreq)
                sc->sc_set->state = FSS_SC_COMMITED;
                become_root();
                status = fss_state_store(fss_global.mem_ctx, fss_global.sc_sets,
-                                        fss_global.sc_sets_count);
+                                        fss_global.sc_sets_count,
+                                        fss_global.db_path);
                unbecome_root();
                if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(1, ("failed to store fss server state: %s\n",
@@ -992,7 +1001,7 @@ uint32_t _fss_ExposeShadowCopySet(struct pipes_struct *p,
        sc_set->state = FSS_SC_EXPOSED;
        become_root();
        status = fss_state_store(fss_global.mem_ctx, fss_global.sc_sets,
-                                fss_global.sc_sets_count);
+                                fss_global.sc_sets_count, fss_global.db_path);
        unbecome_root();
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(1, ("failed to store fss server state: %s\n",
@@ -1036,7 +1045,7 @@ uint32_t _fss_RecoveryCompleteShadowCopySet(struct pipes_struct *p,
        fss_global.cur_ctx = 0;
        become_root();
        status = fss_state_store(fss_global.mem_ctx, fss_global.sc_sets,
-                                fss_global.sc_sets_count);
+                                fss_global.sc_sets_count, fss_global.db_path);
        unbecome_root();
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(1, ("failed to store fss server state: %s\n",
@@ -1435,7 +1444,7 @@ static void fss_delete_vfs_done(struct tevent_req *subreq)
 
        become_root();
        status = fss_state_store(fss_global.mem_ctx, fss_global.sc_sets,
-                                fss_global.sc_sets_count);
+                                fss_global.sc_sets_count, fss_global.db_path);
        unbecome_root();
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(1, ("failed to store fss server state: %s\n",
index 7802b070a65d25ac3976611266e5baef3fe21fd2..b54a41b2d9aa33ea67174e3847f57522b8815190 100644 (file)
@@ -70,6 +70,7 @@ struct fss_sc_set {
 
 struct fss_global {
        TALLOC_CTX *mem_ctx;            /* parent mem ctx for sc sets */
+       char *db_path;
        uint32_t min_vers;
        uint32_t max_vers;
        uint32_t cur_ctx;
@@ -79,10 +80,12 @@ struct fss_global {
 
 NTSTATUS fss_state_store(TALLOC_CTX *mem_ctx,
                         struct fss_sc_set *sc_sets,
-                        uint32_t sc_sets_count);
+                        uint32_t sc_sets_count,
+                        const char *db_path);
 
 NTSTATUS fss_state_retrieve(TALLOC_CTX *mem_ctx,
                            struct fss_sc_set **sc_sets,
-                           uint32_t *sc_sets_count);
+                           uint32_t *sc_sets_count,
+                           const char *db_path);
 
 #endif /*_SRV_FSS_PRIVATE_H_ */
index 32543a1e9d2849e19772f7301786588837144e35..29212f9aec00d7443063bc87c77817b12d6fe4b1 100644 (file)
@@ -187,29 +187,22 @@ static NTSTATUS fss_state_sc_set_store(TALLOC_CTX *mem_ctx,
  */
 NTSTATUS fss_state_store(TALLOC_CTX *mem_ctx,
                         struct fss_sc_set *sc_sets,
-                        uint32_t sc_sets_count)
+                        uint32_t sc_sets_count,
+                        const char *db_path)
 {
        TALLOC_CTX *tmp_ctx;
        struct db_context *db;
        NTSTATUS status;
        int ret;
        struct fss_sc_set *sc_set;
-       char *db_path;
 
        tmp_ctx = talloc_new(mem_ctx);
        if (tmp_ctx == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       db_path = lock_path(FSS_DB_NAME);
-       if (db_path == NULL) {
-               status = NT_STATUS_NO_MEMORY;
-               goto err_ctx_free;
-       }
-
        db = db_open(tmp_ctx, db_path, 0, TDB_DEFAULT,  O_RDWR | O_CREAT,
                     0600, DBWRAP_LOCK_ORDER_1);
-       talloc_free(db_path);
        if (db == NULL) {
                DEBUG(0, ("Failed to open fss state TDB\n"));
                status = NT_STATUS_ACCESS_DENIED;
@@ -612,9 +605,9 @@ err_out:
 
 NTSTATUS fss_state_retrieve(TALLOC_CTX *mem_ctx,
                            struct fss_sc_set **sc_sets,
-                           uint32_t *sc_sets_count)
+                           uint32_t *sc_sets_count,
+                           const char *db_path)
 {
-       char *db_path;
        struct db_context *db;
        NTSTATUS status;
        struct fss_traverse_state trv_state;
@@ -631,16 +624,9 @@ NTSTATUS fss_state_retrieve(TALLOC_CTX *mem_ctx,
                goto err_out;
        }
 
-       db_path = lock_path(FSS_DB_NAME);
-       if (db_path == NULL) {
-               status = NT_STATUS_NO_MEMORY;
-               goto err_ts_free;
-       }
-
        db = db_open(trv_state.mem_ctx, db_path, 0, TDB_DEFAULT,
                     O_RDONLY, 0600, DBWRAP_LOCK_ORDER_1);
        err = errno;
-       talloc_free(db_path);
        if ((db == NULL) && (err == ENOENT)) {
                DEBUG(4, ("fss state TDB does not exist for retrieval\n"));
                status = NT_STATUS_OK;
index 6233fb312c92c7360fc146194b03d837a8044406..484221528dbd71c04094e93a9e9f835dd62656f6 100644 (file)
@@ -40,30 +40,37 @@ static bool test_fsrvp_state_empty(struct torture_context *tctx)
        NTSTATUS status;
        struct fss_global fss_global;
        struct stat sbuf;
-       char *db_path = lock_path(FSS_DB_NAME);
+       char db_dir[] = "fsrvp_torture_XXXXXX";
+       char *db_path = talloc_asprintf(NULL, "%s/%s",
+                                       mkdtemp(db_dir), FSS_DB_NAME);
 
        memset(&fss_global, 0, sizeof(fss_global));
        fss_global.mem_ctx = talloc_new(NULL);
+       fss_global.db_path = db_path;
 
        status = fss_state_store(fss_global.mem_ctx, fss_global.sc_sets,
-                                fss_global.sc_sets_count);
+                                fss_global.sc_sets_count, fss_global.db_path);
        torture_assert_ntstatus_ok(tctx, status,
                                   "failed to store empty fss state");
 
-       torture_assert_int_equal(tctx, stat(db_path, &sbuf), 0,
+       torture_assert_int_equal(tctx, stat(fss_global.db_path, &sbuf), 0,
                        "failed to stat fss state tdb");
        talloc_free(fss_global.mem_ctx);
 
        memset(&fss_global, 0, sizeof(fss_global));
        fss_global.mem_ctx = talloc_new(NULL);
+       fss_global.db_path = db_path;
 
        status = fss_state_retrieve(fss_global.mem_ctx, &fss_global.sc_sets,
-                                   &fss_global.sc_sets_count);
+                                   &fss_global.sc_sets_count,
+                                   fss_global.db_path);
        torture_assert_ntstatus_ok(tctx, status,
                                   "failed to retrieve empty fss state");
        torture_assert_int_equal(tctx, fss_global.sc_sets_count, 0,
                                 "sc_sets_count set when it should be zero");
        talloc_free(fss_global.mem_ctx);
+       unlink(db_path);
+       rmdir(db_dir);
        talloc_free(db_path);
 
        return true;
@@ -282,9 +289,13 @@ static bool test_fsrvp_state_single(struct torture_context *tctx)
        struct fss_sc_set *sc_set;
        struct fss_sc *sc;
        struct fss_sc_smap *smap;
+       char db_dir[] = "fsrvp_torture_XXXXXX";
+       char *db_path = talloc_asprintf(NULL, "%s/%s",
+                                       mkdtemp(db_dir), FSS_DB_NAME);
 
        memset(&fss_gs, 0, sizeof(fss_gs));
        fss_gs.mem_ctx = talloc_new(NULL);
+       fss_gs.db_path = db_path;
 
        ok = test_fsrvp_state_sc_set(tctx, fss_gs.mem_ctx, &sc_set);
        torture_assert(tctx, ok, "failed to create sc set");
@@ -305,15 +316,16 @@ static bool test_fsrvp_state_single(struct torture_context *tctx)
        sc->smaps_count++;
 
        status = fss_state_store(fss_gs.mem_ctx, fss_gs.sc_sets,
-                                fss_gs.sc_sets_count);
+                                fss_gs.sc_sets_count, fss_gs.db_path);
        torture_assert_ntstatus_ok(tctx, status,
                                   "failed to store fss state");
 
        memset(&fss_gr, 0, sizeof(fss_gr));
        fss_gr.mem_ctx = talloc_new(NULL);
+       fss_gr.db_path = db_path;
 
        status = fss_state_retrieve(fss_gr.mem_ctx, &fss_gr.sc_sets,
-                                   &fss_gr.sc_sets_count);
+                                   &fss_gr.sc_sets_count, fss_gr.db_path);
        torture_assert_ntstatus_ok(tctx, status,
                                   "failed to retrieve fss state");
 
@@ -323,6 +335,9 @@ static bool test_fsrvp_state_single(struct torture_context *tctx)
 
        talloc_free(fss_gs.mem_ctx);
        talloc_free(fss_gr.mem_ctx);
+       unlink(db_path);
+       rmdir(db_dir);
+       talloc_free(db_path);
 
        return true;
 }
@@ -353,9 +368,13 @@ static bool test_fsrvp_state_multi(struct torture_context *tctx)
        struct fss_sc_smap *smap_aaa;
        struct fss_sc_smap *smap_aba;
        struct fss_sc_smap *smap_abb;
+       char db_dir[] = "fsrvp_torture_XXXXXX";
+       char *db_path = talloc_asprintf(NULL, "%s/%s",
+                                       mkdtemp(db_dir), FSS_DB_NAME);
 
        memset(&fss_gs, 0, sizeof(fss_gs));
        fss_gs.mem_ctx = talloc_new(NULL);
+       fss_gs.db_path = db_path;
 
        ok = test_fsrvp_state_sc_set(tctx, fss_gs.mem_ctx, &sc_set_a);
        torture_assert(tctx, ok, "failed to create sc set");
@@ -402,14 +421,15 @@ static bool test_fsrvp_state_multi(struct torture_context *tctx)
        sc_ab->smaps_count++;
 
        status = fss_state_store(fss_gs.mem_ctx, fss_gs.sc_sets,
-                                fss_gs.sc_sets_count);
+                                fss_gs.sc_sets_count, fss_gs.db_path);
        torture_assert_ntstatus_ok(tctx, status,
                                   "failed to store fss state");
 
        memset(&fss_gr, 0, sizeof(fss_gr));
        fss_gr.mem_ctx = talloc_new(NULL);
+       fss_gr.db_path = db_path;
        status = fss_state_retrieve(fss_gr.mem_ctx, &fss_gr.sc_sets,
-                                   &fss_gr.sc_sets_count);
+                                   &fss_gr.sc_sets_count, fss_gr.db_path);
        torture_assert_ntstatus_ok(tctx, status,
                                   "failed to retrieve fss state");
 
@@ -419,6 +439,9 @@ static bool test_fsrvp_state_multi(struct torture_context *tctx)
 
        talloc_free(fss_gs.mem_ctx);
        talloc_free(fss_gr.mem_ctx);
+       unlink(db_path);
+       rmdir(db_dir);
+       talloc_free(db_path);
 
        return true;
 }
@@ -427,21 +450,24 @@ static bool test_fsrvp_state_none(struct torture_context *tctx)
 {
        NTSTATUS status;
        struct fss_global fss_global;
-       char *db_path = lock_path(FSS_DB_NAME);
-
-       /* ensure start with an empty db */
-       unlink(db_path);
+       char db_dir[] = "fsrvp_torture_XXXXXX";
+       char *db_path = talloc_asprintf(NULL, "%s/%s",
+                                       mkdtemp(db_dir), FSS_DB_NAME);
 
        memset(&fss_global, 0, sizeof(fss_global));
        fss_global.mem_ctx = talloc_new(NULL);
+       fss_global.db_path = db_path;
 
        status = fss_state_retrieve(fss_global.mem_ctx, &fss_global.sc_sets,
-                                   &fss_global.sc_sets_count);
+                                   &fss_global.sc_sets_count,
+                                   fss_global.db_path);
        torture_assert_ntstatus_ok(tctx, status,
                                   "failed to retrieve fss state");
        torture_assert_int_equal(tctx, fss_global.sc_sets_count, 0,
                                 "sc_sets_count set when it should be zero");
        talloc_free(fss_global.mem_ctx);
+       unlink(db_path);
+       rmdir(db_dir);
        talloc_free(db_path);
 
        return true;
@@ -467,7 +493,6 @@ struct torture_suite *torture_local_fsrvp(TALLOC_CTX *mem_ctx)
                                      "state_multi",
                                      test_fsrvp_state_multi);
 
-       /* keep this last so that the tdb is cleaned up */
        torture_suite_add_simple_test(suite,
                                      "state_none",
                                      test_fsrvp_state_none);