s3:lib/conn_tdb: avoid using sconn_server_id()
[mat/samba.git] / source3 / lib / conn_tdb.c
index dd0a354a85169564b01b5290bc23c55a4d84d08b..f6008714115411ea618c5647e7fcf4ecb92807f3 100644 (file)
@@ -2,46 +2,46 @@
    Unix SMB/CIFS implementation.
    Low-level connections.tdb access functions
    Copyright (C) Volker Lendecke 2007
-   
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
+#include "system/filesys.h"
+#include "smbd/globals.h"
+#include "dbwrap/dbwrap.h"
+#include "dbwrap/dbwrap_open.h"
+#include "messages.h"
 
 static struct db_context *connections_db_ctx(bool rw)
 {
        static struct db_context *db_ctx;
+       int open_flags;
 
        if (db_ctx != NULL) {
                return db_ctx;
        }
 
-       if (rw) {
-               db_ctx = db_open(NULL, lock_path("connections.tdb"), 0,
-                                TDB_CLEAR_IF_FIRST|TDB_DEFAULT, 
-                                O_RDWR | O_CREAT, 0644);
-       }
-       else {
-               db_ctx = db_open(NULL, lock_path("connections.tdb"), 0,
-                                TDB_DEFAULT, O_RDONLY, 0);
-       }
+       open_flags = rw ? (O_RDWR|O_CREAT) : O_RDONLY;
 
+       db_ctx = db_open(NULL, lock_path("connections.tdb"), 0,
+                        TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH|TDB_DEFAULT, open_flags, 0644);
        return db_ctx;
 }
 
-struct db_record *connections_fetch_record(TALLOC_CTX *mem_ctx,
-                                          TDB_DATA key)
+static struct db_record *connections_fetch_record(TALLOC_CTX *mem_ctx,
+                                                 TDB_DATA key)
 {
        struct db_context *ctx = connections_db_ctx(True);
 
@@ -49,7 +49,7 @@ struct db_record *connections_fetch_record(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       return ctx->fetch_locked(ctx, mem_ctx, key);
+       return dbwrap_fetch_locked(ctx, mem_ctx, key);
 }
 
 struct db_record *connections_fetch_entry(TALLOC_CTX *mem_ctx,
@@ -60,8 +60,8 @@ struct db_record *connections_fetch_entry(TALLOC_CTX *mem_ctx,
        TDB_DATA key;
 
        ZERO_STRUCT(ckey);
-       ckey.pid = procid_self();
-       ckey.cnum = conn ? conn->cnum : -1;
+       ckey.pid = messaging_server_id(conn->sconn->msg_ctx);
+       ckey.cnum = conn->cnum;
        strlcpy(ckey.name, name, sizeof(ckey.name));
 
        key.dsize = sizeof(ckey);
@@ -80,16 +80,21 @@ struct conn_traverse_state {
 
 static int conn_traverse_fn(struct db_record *rec, void *private_data)
 {
+       TDB_DATA key;
+       TDB_DATA value;
        struct conn_traverse_state *state =
                (struct conn_traverse_state *)private_data;
 
-       if ((rec->key.dsize != sizeof(struct connections_key))
-           || (rec->value.dsize != sizeof(struct connections_data))) {
+       key = dbwrap_record_get_key(rec);
+       value = dbwrap_record_get_value(rec);
+
+       if ((key.dsize != sizeof(struct connections_key))
+           || (value.dsize != sizeof(struct connections_data))) {
                return 0;
        }
 
-       return state->fn(rec, (const struct connections_key *)rec->key.dptr,
-                        (const struct connections_data *)rec->value.dptr,
+       return state->fn(rec, (const struct connections_key *)key.dptr,
+                        (const struct connections_data *)value.dptr,
                         state->private_data);
 }
 
@@ -97,13 +102,20 @@ int connections_traverse(int (*fn)(struct db_record *rec,
                                   void *private_data),
                         void *private_data)
 {
+       NTSTATUS status;
+       int count;
        struct db_context *ctx = connections_db_ctx(False);
 
        if (ctx == NULL) {
                return -1;
        }
 
-       return ctx->traverse(ctx, fn, private_data);
+       status = dbwrap_traverse(ctx, fn, private_data, &count);
+       if (!NT_STATUS_IS_OK(status)) {
+               return -1;
+       }
+
+       return count;
 }
 
 int connections_forall(int (*fn)(struct db_record *rec,
@@ -112,12 +124,80 @@ int connections_forall(int (*fn)(struct db_record *rec,
                                 void *private_data),
                       void *private_data)
 {
+       struct db_context *ctx;
        struct conn_traverse_state state;
+       NTSTATUS status;
+       int count;
+
+       ctx = connections_db_ctx(true);
+       if (ctx == NULL) {
+               return -1;
+       }
+
+       state.fn = fn;
+       state.private_data = private_data;
+
+       status = dbwrap_traverse(ctx, conn_traverse_fn, (void *)&state, &count);
+       if (!NT_STATUS_IS_OK(status)) {
+               return -1;
+       }
+
+       return count;
+}
+
+struct conn_traverse_read_state {
+       int (*fn)(const struct connections_key *key,
+                 const struct connections_data *data,
+                 void *private_data);
+       void *private_data;
+};
+
+static int connections_forall_read_fn(struct db_record *rec,
+                                     void *private_data)
+{
+       TDB_DATA key;
+       TDB_DATA value;
+       struct conn_traverse_read_state *state =
+               (struct conn_traverse_read_state *)private_data;
+
+       key = dbwrap_record_get_key(rec);
+       value = dbwrap_record_get_value(rec);
+
+       if ((key.dsize != sizeof(struct connections_key))
+           || (value.dsize != sizeof(struct connections_data))) {
+               return 0;
+       }
+       return state->fn((const struct connections_key *)key.dptr,
+                        (const struct connections_data *)value.dptr,
+                        state->private_data);
+}
+
+int connections_forall_read(int (*fn)(const struct connections_key *key,
+                                     const struct connections_data *data,
+                                     void *private_data),
+                           void *private_data)
+{
+       struct db_context *ctx;
+       struct conn_traverse_read_state state;
+       NTSTATUS status;
+       int count;
+
+       ctx = connections_db_ctx(false);
+       if (ctx == NULL) {
+               return -1;
+       }
 
        state.fn = fn;
        state.private_data = private_data;
 
-       return connections_traverse(conn_traverse_fn, (void *)&state);
+       status = dbwrap_traverse_read(ctx, connections_forall_read_fn,
+                                     (void *)&state, &count);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               return -1;
+       }
+
+       return count;
 }
 
 bool connections_init(bool rw)