r22761: This introduces lib/conn_tdb.c with two main functions: connections_traverse
authorVolker Lendecke <vlendec@samba.org>
Tue, 8 May 2007 13:44:36 +0000 (13:44 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 17:21:55 +0000 (12:21 -0500)
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)

15 files changed:
source3/Makefile.in
source3/lib/conn_tdb.c [new file with mode: 0644]
source3/lib/messages.c
source3/nmbd/nmbd_processlogon.c
source3/rpc_server/srv_netlog_nt.c
source3/rpc_server/srv_spoolss_nt.c
source3/rpc_server/srv_srvsvc_nt.c
source3/smbd/connection.c
source3/smbd/lanman.c
source3/smbd/server.c
source3/smbd/statcache.c
source3/utils/net_status.c
source3/utils/smbcontrol.c
source3/utils/status.c
source3/web/statuspage.c

index 5ddcaa14014fc9694e2c8d42f39a7d7634101dc7..0346217ff7fd613307823869ab1e7e53bf387245 100644 (file)
@@ -272,7 +272,7 @@ LIB_WITH_PROTO_OBJ = $(VERSION_OBJ) lib/charcnv.o lib/debug.o lib/fault.o \
          lib/tallocmsg.o lib/dmallocmsg.o libsmb/smb_signing.o \
          lib/md5.o lib/hmacmd5.o lib/arc4.o lib/iconv.o \
          nsswitch/wb_client.o $(WBCOMMON_OBJ) \
-         lib/pam_errors.o intl/lang_tdb.o \
+         lib/pam_errors.o intl/lang_tdb.o lib/conn_tdb.o \
          lib/adt_tree.o lib/gencache.o $(TDB_OBJ) \
          lib/module.o lib/events.o lib/ldap_escape.o @CHARSET_STATIC@ \
          lib/secdesc.o lib/util_seaccess.o lib/secace.o lib/secacl.o \
diff --git a/source3/lib/conn_tdb.c b/source3/lib/conn_tdb.c
new file mode 100644 (file)
index 0000000..e6f491b
--- /dev/null
@@ -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);
+}
index 6ecb89571b29579d1911ecd565a83837e04518ca..10bf5dfcf7f2480183ef50dcc3a783af09dbd138 100644 (file)
@@ -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;
index ee7d732ae48751200d5bed06dc3852e205fb4bc4..15be7f59c081ec92094a69f322cf7ca5f0eec6a9 100644 (file)
@@ -38,24 +38,10 @@ Send a message to smbd to do a sam delta sync
 
 static void send_repl_message(uint32 low_serial)
 {
-        TDB_CONTEXT *tdb;
-
-        tdb = tdb_open_log(lock_path("connections.tdb"), 0,
-                           TDB_DEFAULT, O_RDONLY, 0);
-
-        if (!tdb) {
-                DEBUG(3, ("send_repl_message(): failed to open connections "
-                          "database\n"));
-                return;
-        }
-
         DEBUG(3, ("sending replication message, serial = 0x%04x\n", 
                   low_serial));
-        
-        message_send_all(tdb, MSG_SMB_SAM_REPL, &low_serial,
+        message_send_all(MSG_SMB_SAM_REPL, &low_serial,
                          sizeof(low_serial), False, NULL);
-
-        tdb_close(tdb);
 }
 
 /****************************************************************************
index 0c12cb3b7c5e6466013a2fbba62eb6f2050af386..e110f39289aa7ec425f66d9e9ed6015a6dc2ccc2 100644 (file)
@@ -75,22 +75,8 @@ Send a message to smbd to do a sam synchronisation
 
 static void send_sync_message(void)
 {
-        TDB_CONTEXT *tdb;
-
-        tdb = tdb_open_log(lock_path("connections.tdb"), 0,
-                           TDB_DEFAULT, O_RDONLY, 0);
-
-        if (!tdb) {
-                DEBUG(3, ("send_sync_message(): failed to open connections "
-                          "database\n"));
-                return;
-        }
-
         DEBUG(3, ("sending sam synchronisation message\n"));
-        
-        message_send_all(tdb, MSG_SMB_SAM_SYNC, NULL, 0, False, NULL);
-
-        tdb_close(tdb);
+        message_send_all(MSG_SMB_SAM_SYNC, NULL, 0, False, NULL);
 }
 
 /*************************************************************************
index 7e46541b942c0b1620b2fa1c514ef706dca1f4ad..a4edeb2cfdd815cb717020c6100bf5af31a1b94a 100644 (file)
@@ -311,7 +311,7 @@ WERROR delete_printer_hook( NT_USER_TOKEN *token, const char *sharename )
                
        if ( (ret = smbrun(command, NULL)) == 0 ) {
                /* Tell everyone we updated smb.conf. */
-               message_send_all(conn_tdb_ctx(), MSG_SMB_CONF_UPDATED, NULL, 0, False, NULL);
+               message_send_all(MSG_SMB_CONF_UPDATED, NULL, 0, False, NULL);
        }
                
        if ( is_print_op )
@@ -6253,7 +6253,7 @@ BOOL add_printer_hook(NT_USER_TOKEN *token, NT_PRINTER_INFO_LEVEL *printer)
        
        if ( (ret = smbrun(command, &fd)) == 0 ) {
                /* Tell everyone we updated smb.conf. */
-               message_send_all(conn_tdb_ctx(), MSG_SMB_CONF_UPDATED, NULL, 0, False, NULL);
+               message_send_all(MSG_SMB_CONF_UPDATED, NULL, 0, False, NULL);
        }
 
        if ( is_print_op )
index 2365f7ece37c558dee5686ae92c2ac5e6d94e436..59e86e491232775892e25654d0be737cc9eb6508 100644 (file)
@@ -104,27 +104,22 @@ static WERROR net_enum_pipes( TALLOC_CTX *ctx, struct srvsvc_NetFileInfo3 **info
                               uint32 *count, uint32 *resume )
 {
        struct file_enum_count fenum;
-       TDB_CONTEXT *conn_tdb = conn_tdb_ctx();
 
-       if ( !conn_tdb ) {
-               DEBUG(0,("net_enum_pipes: Failed to retrieve the connections tdb handle!\n"));
-               return WERR_ACCESS_DENIED;
-       }
-       
        fenum.ctx = ctx;
        fenum.info = *info;
        fenum.count = *count;
 
-       if (tdb_traverse(conn_tdb, pipe_enum_fn, &fenum) == -1) {
-               DEBUG(0,("net_enum_pipes: traverse of connections.tdb failed with error %s.\n",
-                       tdb_errorstr(conn_tdb) ));
+       if (connections_traverse(pipe_enum_fn, &fenum) == -1) {
+               DEBUG(0,("net_enum_pipes: traverse of connections.tdb "
+                        "failed\n"));
                return WERR_NOMEM;
        }
        
        *info  = fenum.info;
        *count = fenum.count;
        
-       return WERR_OK;}
+       return WERR_OK;
+}
 
 /*******************************************************************
 ********************************************************************/
@@ -1421,7 +1416,7 @@ static WERROR add_share(const char *share_name, const char *path,
 
                if ( (ret = smbrun(command, NULL)) == 0 ) {
                        /* Tell everyone we updated smb.conf. */
-                       message_send_all(conn_tdb_ctx(), MSG_SMB_CONF_UPDATED,
+                       message_send_all(MSG_SMB_CONF_UPDATED,
                                         NULL, 0, False, NULL);
                }
 
@@ -1517,7 +1512,7 @@ static WERROR delete_share(const char *sharename,
 
                if ( (ret = smbrun(command, NULL)) == 0 ) {
                        /* Tell everyone we updated smb.conf. */
-                       message_send_all(conn_tdb_ctx(), MSG_SMB_CONF_UPDATED,
+                       message_send_all(MSG_SMB_CONF_UPDATED,
                                         NULL, 0, False, NULL);
                }
 
@@ -1575,7 +1570,7 @@ static WERROR change_share(const char *share_name, const char *path,
                        
                if ( (ret = smbrun(command, NULL)) == 0 ) {
                        /* Tell everyone we updated smb.conf. */
-                       message_send_all(conn_tdb_ctx(), MSG_SMB_CONF_UPDATED,
+                       message_send_all(MSG_SMB_CONF_UPDATED,
                                         NULL, 0, False, NULL);
                }
                
index e609b90a507fc7296de5b0a96a79df6b59b4ff03..b9cdede69ed019435088cfb0711c84b5ad3ed555 100644 (file)
 
 #include "includes.h"
 
-static TDB_CONTEXT *tdb;
-
-/****************************************************************************
- Return the connection tdb context (used for message send all).
-****************************************************************************/
-
-TDB_CONTEXT *conn_tdb_ctx(void)
-{
-       if (!tdb)
-               tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT, 
-                              O_RDWR | O_CREAT, 0644);
-
-       return tdb;
-}
-
 static void make_conn_key(connection_struct *conn, const char *name, TDB_DATA *pkbuf, struct connections_key *pkey)
 {
        ZERO_STRUCTP(pkey);
@@ -62,6 +47,7 @@ BOOL yield_connection(connection_struct *conn, const char *name)
 {
        struct connections_key key;
        TDB_DATA kbuf;
+       TDB_CONTEXT *tdb = conn_tdb_ctx(True);
 
        if (!tdb)
                return False;
@@ -112,7 +98,7 @@ static int count_fn( TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *u
                DEBUG(2,("pid %s doesn't exist - deleting connections %d [%s]\n",
                        procid_str_static(&crec.pid), crec.cnum, crec.servicename));
                if (tdb_delete(the_tdb, kbuf) != 0)
-                       DEBUG(0,("count_fn: tdb_delete failed with error %s\n", tdb_errorstr(tdb) ));
+                       DEBUG(0,("count_fn: tdb_delete failed with error %s\n", tdb_errorstr(the_tdb) ));
                return 0;
        }
  
@@ -139,6 +125,7 @@ static int count_fn( TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *u
 int count_current_connections( const char *sharename, BOOL clear  )
 {
        struct count_stat cs;
+       TDB_CONTEXT *tdb = conn_tdb_ctx(True);
 
        cs.mypid = sys_getpid();
        cs.curr_connections = 0;
@@ -184,11 +171,10 @@ BOOL claim_connection(connection_struct *conn, const char *name,int max_connecti
        struct connections_key key;
        struct connections_data crec;
        TDB_DATA kbuf, dbuf;
+       TDB_CONTEXT *tdb = conn_tdb_ctx(True);
 
        if (!tdb) {
-               if ( (tdb =conn_tdb_ctx()) == NULL ) {
-                       return False;
-               }
+               return False;
        }
        
        /*
@@ -245,6 +231,7 @@ BOOL register_message_flags(BOOL doreg, uint32 msg_flags)
        struct connections_key key;
        struct connections_data *pcrec;
        TDB_DATA kbuf, dbuf;
+       TDB_CONTEXT *tdb = conn_tdb_ctx(True);
 
        if (!tdb)
                return False;
@@ -344,7 +331,7 @@ BOOL store_pipe_opendb( smb_np_struct *p )
        data.dptr = (uint8 *)prec;
        data.dsize = sizeof(struct pipe_open_rec);
        
-       if ( (pipe_tdb = conn_tdb_ctx() ) == NULL ) {
+       if ( (pipe_tdb = conn_tdb_ctx(True) ) == NULL ) {
                goto done;
        }
        
@@ -375,7 +362,7 @@ BOOL delete_pipe_opendb( smb_np_struct *p )
                goto done;
        }
        
-       if ( (pipe_tdb = conn_tdb_ctx() ) == NULL ) {
+       if ( (pipe_tdb = conn_tdb_ctx(True) ) == NULL ) {
                goto done;
        }
 
index 05b1e812b2579fd710d899967287227d5ef067a3..b235fd16986fe6df1bcb131b5f5ecc8a31a010f3 100644 (file)
@@ -1894,7 +1894,8 @@ static BOOL api_RNetShareAdd(connection_struct *conn,uint16 vuid,
                        goto error_exit;
                } else {
                        SAFE_FREE(command);
-                       message_send_all(conn_tdb_ctx(), MSG_SMB_CONF_UPDATED, NULL, 0, False, NULL);
+                       message_send_all(MSG_SMB_CONF_UPDATED, NULL, 0,
+                                        False, NULL);
                }
        } else {
                return False;
index 1020ad3aca570232d9ea730cf8ba34ed4a54568f..b869a1a48e8e772503d043cab941a0a45a40a6f1 100644 (file)
@@ -987,7 +987,7 @@ extern void build_options(BOOL screen);
        if (!session_init())
                exit(1);
 
-       if (conn_tdb_ctx() == NULL)
+       if (!connections_init(True))
                exit(1);
 
        if (!locking_init(0))
index e257483f818ad330444f913fa18da8a831780af2..7c2c968d8dca4a27e387e3165c469555ffc475b2 100644 (file)
@@ -292,8 +292,7 @@ BOOL stat_cache_lookup(connection_struct *conn, pstring name, pstring dirpath,
 void send_stat_cache_delete_message(const char *name)
 {
 #ifdef DEVELOPER
-       message_send_all(conn_tdb_ctx(),
-                       MSG_SMB_STAT_CACHE_DELETE,
+       message_send_all(MSG_SMB_STAT_CACHE_DELETE,
                        name,
                        strlen(name)+1,
                        True,
index af6952389cbdf584b541ce27fdd575773c4f2369..88c1789f71b76813db683b16e6e900fc8916c653 100644 (file)
@@ -84,27 +84,22 @@ static int net_status_sessions(int argc, const char **argv)
        return 0;
 }
 
-static int show_share(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
+static int show_share(TDB_CONTEXT *tdb, 
+                     const struct connections_key *key,
+                     const struct connections_data *crec,
                      void *state)
 {
-       struct connections_data crec;
-
-       if (dbuf.dsize != sizeof(crec))
+       if (crec->cnum == -1)
                return 0;
 
-       memcpy(&crec, dbuf.dptr, sizeof(crec));
-
-       if (crec.cnum == -1)
-               return 0;
-
-       if (!process_exists(crec.pid)) {
+       if (!process_exists(crec->pid)) {
                return 0;
        }
 
        d_printf("%-10.10s   %s   %-12s  %s",
-              crec.servicename, procid_str_static(&crec.pid),
-              crec.machine,
-              time_to_asc(crec.start));
+              crec->servicename, procid_str_static(&crec->pid),
+              crec->machine,
+              time_to_asc(crec->start));
 
        return 0;
 }
@@ -139,41 +134,37 @@ static int collect_pid(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
        return 0;
 }
 
-static int show_share_parseable(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
+static int show_share_parseable(TDB_CONTEXT *tdb,
+                               const struct connections_key *key,
+                               const struct connections_data *crec,
                                void *state)
 {
        struct sessionids *ids = (struct sessionids *)state;
-       struct connections_data crec;
        int i;
        BOOL guest = True;
 
-       if (dbuf.dsize != sizeof(crec))
-               return 0;
-
-       memcpy(&crec, dbuf.dptr, sizeof(crec));
-
-       if (crec.cnum == -1)
+       if (crec->cnum == -1)
                return 0;
 
-       if (!process_exists(crec.pid)) {
+       if (!process_exists(crec->pid)) {
                return 0;
        }
 
        for (i=0; i<ids->num_entries; i++) {
                struct server_id id = ids->entries[i].pid;
-               if (procid_equal(&id, &crec.pid)) {
+               if (procid_equal(&id, &crec->pid)) {
                        guest = False;
                        break;
                }
        }
 
        d_printf("%s\\%s\\%s\\%s\\%s\\%s\\%s",
-                crec.servicename,procid_str_static(&crec.pid),
+                crec->servicename,procid_str_static(&crec->pid),
                 guest ? "" : uidtoname(ids->entries[i].uid),
                 guest ? "" : gidtoname(ids->entries[i].gid),
-                crec.machine, 
+                crec->machine, 
                 guest ? "" : ids->entries[i].hostname,
-                time_to_asc(crec.start));
+                time_to_asc(crec->start));
 
        return 0;
 }
@@ -197,18 +188,7 @@ static int net_status_shares_parseable(int argc, const char **argv)
        tdb_traverse(tdb, collect_pid, &ids);
        tdb_close(tdb);
 
-       tdb = tdb_open_log(lock_path("connections.tdb"), 0,
-                          TDB_DEFAULT, O_RDONLY, 0);
-
-       if (tdb == NULL) {
-               d_fprintf(stderr, "%s not initialised\n", lock_path("connections.tdb"));
-               d_fprintf(stderr, "This is normal if no SMB client has ever "
-                        "connected to your server.\n");
-               return -1;
-       }
-
-       tdb_traverse(tdb, show_share_parseable, &ids);
-       tdb_close(tdb);
+       connections_forall(show_share_parseable, &ids);
 
        SAFE_FREE(ids.entries);
 
@@ -217,8 +197,6 @@ static int net_status_shares_parseable(int argc, const char **argv)
 
 static int net_status_shares(int argc, const char **argv)
 {
-       TDB_CONTEXT *tdb;
-
        if (argc == 0) {
 
                d_printf("\nService      pid     machine       "
@@ -226,19 +204,7 @@ static int net_status_shares(int argc, const char **argv)
                d_printf("-------------------------------------"
                         "------------------\n");
 
-               tdb = tdb_open_log(lock_path("connections.tdb"), 0,
-                                  TDB_DEFAULT, O_RDONLY, 0);
-
-               if (tdb == NULL) {
-                       d_fprintf(stderr, "%s not initialised\n",
-                                lock_path("connections.tdb"));
-                       d_fprintf(stderr, "This is normal if no SMB client has "
-                                "ever connected to your server.\n");
-                       return -1;
-               }
-
-               tdb_traverse(tdb, show_share, NULL);
-               tdb_close(tdb);
+               connections_forall(show_share, NULL);
 
                return 0;
        }
index d9eb5b78eaacd5027823527de4e25b8e7922b9a5..ab46cbce65a4ea9f0c38e3930f3e2721f1a2c2dd 100644 (file)
@@ -51,7 +51,6 @@ static BOOL send_message(struct server_id pid, int msg_type,
                         const void *buf, int len,
                         BOOL duplicates)
 {
-       TDB_CONTEXT *tdb;
        BOOL ret;
        int n_sent = 0;
 
@@ -62,21 +61,11 @@ static BOOL send_message(struct server_id pid, int msg_type,
                return NT_STATUS_IS_OK(message_send_pid(pid, msg_type, buf, len,
                                                        duplicates));
 
-       tdb = tdb_open_log(lock_path("connections.tdb"), 0, 
-                          TDB_DEFAULT, O_RDWR, 0);
-       if (!tdb) {
-               fprintf(stderr,"Failed to open connections database"
-                       ": %s\n", strerror(errno));
-               return False;
-       }
-       
-       ret = message_send_all(tdb,msg_type, buf, len, duplicates,
+       ret = message_send_all(msg_type, buf, len, duplicates,
                               &n_sent);
        DEBUG(10,("smbcontrol/send_message: broadcast message to "
                  "%d processes\n", n_sent));
        
-       tdb_close(tdb);
-       
        return ret;
 }
 
@@ -247,16 +236,11 @@ cleanup:
        ptrace(PTRACE_DETACH, pid, NULL, NULL);
 }
 
-static int stack_trace_connection(TDB_CONTEXT * tdb, TDB_DATA key,
-       TDB_DATA data, void * priv)
+static int stack_trace_connection(TDB_CONTEXT * tdb, 
+                                 const struct connections_key *key,
+                                 const struct connections_data *conn,
 {
-       struct connections_data conn;
-
-       if (data.dsize != sizeof(conn))
-               return 0;
-
-       memcpy(&conn, data.dptr, sizeof(conn));
-       print_stack_trace(procid_to_pid(&conn.pid), (int *)priv);
+       print_stack_trace(procid_to_pid(&conn->pid), (int *)priv);
 
        return 0;
 }
@@ -286,19 +270,7 @@ static BOOL do_daemon_stack_trace(const struct server_id pid,
                 */
                print_stack_trace(dest, &count);
        } else {
-               TDB_CONTEXT * tdb;
-
-               tdb = tdb_open_log(lock_path("connections.tdb"), 0, 
-                                  TDB_DEFAULT, O_RDONLY, 0);
-               if (!tdb) {
-                       fprintf(stderr,
-                               "Failed to open connections database: %s\n",
-                               strerror(errno));
-                       return False;
-               }
-
-               tdb_traverse(tdb, stack_trace_connection, &count);
-               tdb_close(tdb);
+               connections_traverse(stack_trace_connection, &count);
        }
 
        return True;
index d3bb79dd016b0d4f398408aaf505fe0ec2e73e91..aa014c8410fa4d8f922423dd35fc8f3495364e8c 100644 (file)
@@ -188,26 +188,22 @@ static void print_brl(SMB_DEV_T dev,
               (double)start, (double)size);
 }
 
-static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
+static int traverse_fn1(TDB_CONTEXT *tdb,
+                       const struct connections_key *key,
+                       const struct connections_data *crec,
+                       void *state)
 {
-       struct connections_data crec;
-
-       if (dbuf.dsize != sizeof(crec))
-               return 0;
-
-       memcpy(&crec, dbuf.dptr, sizeof(crec));
-
-       if (crec.cnum == -1)
+       if (crec->cnum == -1)
                return 0;
 
-       if (!process_exists(crec.pid) || !Ucrit_checkUid(crec.uid)) {
+       if (!process_exists(crec->pid) || !Ucrit_checkUid(crec->uid)) {
                return 0;
        }
 
        d_printf("%-10s   %s   %-12s  %s",
-              crec.servicename,procid_str_static(&crec.pid),
-              crec.machine,
-              time_to_asc(crec.start));
+              crec->servicename,procid_str_static(&crec->pid),
+              crec->machine,
+              time_to_asc(crec->start));
 
        return 0;
 }
@@ -339,26 +335,19 @@ static int traverse_sessionid(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, vo
        }
   
        if ( show_shares ) {
-               tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
-               if (!tdb) {
-                       d_printf("%s not initialised\n", lock_path("connections.tdb"));
-                       d_printf("This is normal if an SMB client has never connected to your server.\n");
-               }  else  {
-                       if (verbose) {
-                               d_printf("Opened %s\n", lock_path("connections.tdb"));
-                       }
+               if (verbose) {
+                       d_printf("Opened %s\n", lock_path("connections.tdb"));
+               }
 
-                       if (brief) 
-                               exit(0);
+               if (brief) 
+                       exit(0);
                
-                       d_printf("\nService      pid     machine       Connected at\n");
-                       d_printf("-------------------------------------------------------\n");
+               d_printf("\nService      pid     machine       Connected at\n");
+               d_printf("-------------------------------------------------------\n");
        
-                       tdb_traverse(tdb, traverse_fn1, NULL);
-                       tdb_close(tdb);
+               connections_forall(traverse_fn1, NULL);
 
-                       d_printf("\n");
-               }
+               d_printf("\n");
 
                if ( shares_only )
                        exit(0);
index 7b5c528a7de39ef850889b090c5fb7f1227c1326..2071561e2ede329ef981ca99077d49f66a03c7f9 100644 (file)
@@ -167,20 +167,17 @@ static void print_share_mode(const struct share_mode_entry *e,
 
 
 /* kill off any connections chosen by the user */
-static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
+static int traverse_fn1(TDB_CONTEXT *tdb,
+                       const struct connections_key *key,
+                       const struct connections_data *crec,
+                       void* state)
 {
-       struct connections_data crec;
-
-       if (dbuf.dsize != sizeof(crec))
-               return 0;
-
-       memcpy(&crec, dbuf.dptr, sizeof(crec));
-
-       if (crec.cnum == -1 && process_exists(crec.pid)) {
+       if (crec->cnum == -1 && process_exists(crec->pid)) {
                char buf[30];
-               slprintf(buf,sizeof(buf)-1,"kill_%s", procid_str_static(&crec.pid));
+               slprintf(buf, sizeof(buf)-1,"kill_%s",
+                        procid_str_static(&crec->pid));
                if (cgi_variable(buf)) {
-                       kill_pid(crec.pid);
+                       kill_pid(crec->pid);
                        sleep(SLEEP_TIME);
                }
        }
@@ -188,28 +185,24 @@ static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* st
 }
 
 /* traversal fn for showing machine connections */
-static int traverse_fn2(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
+static int traverse_fn2(TDB_CONTEXT *tdb,
+                       const struct connections_key *key,
+                       const struct connections_data *crec,
+                       void* state)
 {
-       struct connections_data crec;
-
-       if (dbuf.dsize != sizeof(crec))
-               return 0;
-
-       memcpy(&crec, dbuf.dptr, sizeof(crec));
-       
-       if (crec.cnum == -1 || !process_exists(crec.pid) ||
-           procid_equal(&crec.pid, &smbd_pid))
+       if (crec->cnum == -1 || !process_exists(crec->pid) ||
+           procid_equal(&crec->pid, &smbd_pid))
                return 0;
 
-       addPid2Machine (crec.pid, crec.machine);
+       addPid2Machine (crec->pid, crec->machine);
 
        printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td>\n",
-              procid_str_static(&crec.pid),
-              crec.machine,crec.addr,
-              tstring(crec.start));
+              procid_str_static(&crec->pid),
+              crec->machine, crec->addr,
+              tstring(crec->start));
        if (geteuid() == 0) {
                printf("<td><input type=submit value=\"X\" name=\"kill_%s\"></td>\n",
-                      procid_str_static(&crec.pid));
+                      procid_str_static(&crec->pid));
        }
        printf("</tr>\n");
 
@@ -217,23 +210,19 @@ static int traverse_fn2(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* st
 }
 
 /* traversal fn for showing share connections */
-static int traverse_fn3(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void* state)
+static int traverse_fn3(TDB_CONTEXT *tdb,
+                       const struct connections_key *key,
+                       const struct connections_data *crec,
+                       void* state)
 {
-       struct connections_data crec;
-
-       if (dbuf.dsize != sizeof(crec))
-               return 0;
-
-       memcpy(&crec, dbuf.dptr, sizeof(crec));
-
-       if (crec.cnum == -1 || !process_exists(crec.pid))
+       if (crec->cnum == -1 || !process_exists(crec->pid))
                return 0;
 
        printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n",
-              crec.servicename,uidtoname(crec.uid),
-              gidtoname(crec.gid),procid_str_static(&crec.pid),
-              crec.machine,
-              tstring(crec.start));
+              crec->servicename, uidtoname(crec->uid),
+              gidtoname(crec->gid),procid_str_static(&crec->pid),
+              crec->machine,
+              tstring(crec->start));
        return 0;
 }
 
@@ -244,7 +233,6 @@ void status_page(void)
        const char *v;
        int autorefresh=0;
        int refresh_interval=30;
-       TDB_CONTEXT *tdb;
        int nr_running=0;
        BOOL waitup = False;
 
@@ -322,8 +310,7 @@ void status_page(void)
                PID_or_Machine = 0;
        }
 
-       tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
-       if (tdb) tdb_traverse(tdb, traverse_fn1, NULL);
+       connections_forall(traverse_fn1, NULL);
  
        initPid2Machine ();
 
@@ -344,12 +331,6 @@ void status_page(void)
 
        printf("<p>\n");
 
-       if (!tdb) {
-               /* open failure either means no connections have been
-                   made */
-       }
-
-
        printf("<table>\n");
 
        printf("<tr><td>%s</td><td>%s</td></tr>", _("version:"), SAMBA_VERSION_STRING);
@@ -419,7 +400,7 @@ void status_page(void)
        }
        printf("</tr>\n");
 
-       if (tdb) tdb_traverse(tdb, traverse_fn2, NULL);
+       connections_forall(traverse_fn2, NULL);
 
        printf("</table><p>\n");
 
@@ -428,7 +409,7 @@ void status_page(void)
        printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n\n",
                _("Share"), _("User"), _("Group"), _("PID"), _("Client"), _("Date"));
 
-       if (tdb) tdb_traverse(tdb, traverse_fn3, NULL);
+       connections_forall(traverse_fn3, NULL);
 
        printf("</table><p>\n");
 
@@ -441,8 +422,6 @@ void status_page(void)
        locking_end();
        printf("</table>\n");
 
-       if (tdb) tdb_close(tdb);
-
        printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"%s\">\n", _("Show Client in col 1"));
        printf("<input type=submit name=\"show_pid_in_col_1\" value=\"%s\">\n", _("Show PID in col 1"));