summaryrefslogtreecommitdiff
path: root/source3/lib
diff options
context:
space:
mode:
authorVolker Lendecke <vlendec@samba.org>2007-05-08 13:44:36 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 12:21:55 -0500
commit4aa44f7475e03dcc596f6a13fffffda7268074a1 (patch)
tree2b0529c892a64a3c2283df64d6ea14433056d04a /source3/lib
parentdcc2fe7cb61937aa191bf8b5f353d0308a8011a3 (diff)
downloadsamba-4aa44f7475e03dcc596f6a13fffffda7268074a1.tar.gz
samba-4aa44f7475e03dcc596f6a13fffffda7268074a1.tar.bz2
samba-4aa44f7475e03dcc596f6a13fffffda7268074a1.zip
r22761: This introduces lib/conn_tdb.c with two main functions: connections_traverse
and connections_forall. This centralizes all the routines that did individual tdb_open("connections.tdb") and direct tdb_traverse. Volker (This used to be commit e43e94cda1ad8876b3cb5d1129080b57fa6ec214)
Diffstat (limited to 'source3/lib')
-rw-r--r--source3/lib/conn_tdb.c109
-rw-r--r--source3/lib/messages.c4
2 files changed, 111 insertions, 2 deletions
diff --git a/source3/lib/conn_tdb.c b/source3/lib/conn_tdb.c
new file mode 100644
index 0000000000..e6f491bbfb
--- /dev/null
+++ b/source3/lib/conn_tdb.c
@@ -0,0 +1,109 @@
+/*
+ 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 2 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, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include "includes.h"
+
+TDB_CONTEXT *conn_tdb_ctx(BOOL rw)
+{
+ static TDB_CONTEXT *tdb;
+
+ if (tdb != NULL) {
+ return tdb;
+ }
+
+ if (rw) {
+ tdb = tdb_open_log(lock_path("connections.tdb"), 0,
+ TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
+ O_RDWR | O_CREAT, 0644);
+ }
+ else {
+ tdb = tdb_open_log(lock_path("connections.tdb"), 0,
+ TDB_DEFAULT, O_RDONLY, 0);
+ }
+
+ if (tdb == NULL) {
+ DEBUG(0, ("Could not open connections.tdb: %s\n",
+ strerror(errno)));
+ }
+
+ return tdb;
+}
+
+struct conn_traverse_state {
+ int (*fn)(TDB_CONTEXT *tdb,
+ const struct connections_key *key,
+ const struct connections_data *data,
+ void *private_data);
+ void *private_data;
+};
+
+static int conn_traverse_fn(TDB_CONTEXT *tdb, TDB_DATA key,
+ TDB_DATA data, void *private_data)
+{
+ struct conn_traverse_state *state =
+ (struct conn_traverse_state *)private_data;
+
+ if ((key.dsize != sizeof(struct connections_key))
+ || (data.dsize != sizeof(struct connections_data))) {
+ return 0;
+ }
+
+ return state->fn(
+ tdb, (const struct connections_key *)key.dptr,
+ (const struct connections_data *)data.dptr,
+ state->private_data);
+}
+
+int connections_traverse(int (*fn)(TDB_CONTEXT *tdb, TDB_DATA key,
+ TDB_DATA data, void *private_data),
+ void *private_data)
+{
+ TDB_CONTEXT *tdb = conn_tdb_ctx(True);
+
+ if (tdb == NULL) {
+ DEBUG(5, ("Could not open connections.tdb r/w, trying r/o\n"));
+ tdb = conn_tdb_ctx(False);
+ }
+
+ if (tdb == NULL) {
+ return -1;
+ }
+
+ return tdb_traverse(tdb, fn, private_data);
+}
+
+int connections_forall(int (*fn)(TDB_CONTEXT *tdb,
+ const struct connections_key *key,
+ const struct connections_data *data,
+ void *private_data),
+ void *private_data)
+{
+ struct conn_traverse_state state;
+
+ state.fn = fn;
+ state.private_data = private_data;
+
+ return connections_traverse(conn_traverse_fn, (void *)&state);
+}
+
+BOOL connections_init(BOOL rw)
+{
+ return (conn_tdb_ctx(rw) != NULL);
+}
diff --git a/source3/lib/messages.c b/source3/lib/messages.c
index 6ecb89571b..10bf5dfcf7 100644
--- a/source3/lib/messages.c
+++ b/source3/lib/messages.c
@@ -665,7 +665,7 @@ static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void
*
* @retval True for success.
**/
-BOOL message_send_all(TDB_CONTEXT *conn_tdb, int msg_type,
+BOOL message_send_all(int msg_type,
const void *buf, size_t len,
BOOL duplicates_allowed,
int *n_sent)
@@ -691,7 +691,7 @@ BOOL message_send_all(TDB_CONTEXT *conn_tdb, int msg_type,
msg_all.duplicates = duplicates_allowed;
msg_all.n_sent = 0;
- tdb_traverse(conn_tdb, traverse_fn, &msg_all);
+ connections_traverse(traverse_fn, &msg_all);
if (n_sent)
*n_sent = msg_all.n_sent;
return True;