2 Unix SMB/CIFS implementation.
3 connection claim routines
4 Copyright (C) Andrew Tridgell 1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 static TDB_CONTEXT *tdb;
25 /****************************************************************************
26 Return the connection tdb context (used for message send all).
27 ****************************************************************************/
29 TDB_CONTEXT *conn_tdb_ctx(void)
32 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
33 O_RDWR | O_CREAT, 0644);
38 /****************************************************************************
39 Delete a connection record.
40 ****************************************************************************/
42 BOOL yield_connection(connection_struct *conn,char *name)
44 struct connections_key key;
50 DEBUG(3,("Yielding connection to %s\n",name));
53 key.pid = sys_getpid();
54 key.cnum = conn?conn->cnum:-1;
55 fstrcpy(key.name, name);
57 kbuf.dptr = (char *)&key;
58 kbuf.dsize = sizeof(key);
60 if (tdb_delete(tdb, kbuf) != 0) {
61 int dbg_lvl = (!conn && (tdb_error(tdb) == TDB_ERR_NOEXIST)) ? 3 : 0;
62 DEBUG(dbg_lvl,("yield_connection: tdb_delete for name %s failed with error %s.\n",
63 name, tdb_errorstr(tdb) ));
77 /****************************************************************************
78 Count the entries belonging to a service in the connection db.
79 ****************************************************************************/
81 static int count_fn( TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *udp)
83 struct connections_data crec;
84 struct count_stat *cs = (struct count_stat *)udp;
86 if (dbuf.dsize != sizeof(crec))
89 memcpy(&crec, dbuf.dptr, sizeof(crec));
94 /* If the pid was not found delete the entry from connections.tdb */
96 if (cs->Clear && !process_exists(crec.pid) && (errno == ESRCH)) {
97 DEBUG(2,("pid %u doesn't exist - deleting connections %d [%s]\n",
98 (unsigned int)crec.pid, crec.cnum, crec.name));
99 if (tdb_delete(the_tdb, kbuf) != 0)
100 DEBUG(0,("count_fn: tdb_delete failed with error %s\n", tdb_errorstr(tdb) ));
104 if (strequal(crec.name, cs->name))
105 cs->curr_connections++;
110 /****************************************************************************
111 Claim an entry in the connections database.
112 ****************************************************************************/
114 BOOL claim_connection(connection_struct *conn,char *name,int max_connections,BOOL Clear, uint32 msg_flags)
116 struct connections_key key;
117 struct connections_data crec;
121 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
122 O_RDWR | O_CREAT, 0644);
128 * Enforce the max connections parameter.
131 if (max_connections > 0) {
132 struct count_stat cs;
134 cs.mypid = sys_getpid();
135 cs.curr_connections = 0;
136 cs.name = lp_servicename(SNUM(conn));
140 * This has a race condition, but locking the chain before hand is worse
141 * as it leads to deadlock.
144 if (tdb_traverse(tdb, count_fn, &cs) == -1) {
145 DEBUG(0,("claim_connection: traverse of connections.tdb failed with error %s.\n",
146 tdb_errorstr(tdb) ));
150 if (cs.curr_connections >= max_connections) {
151 DEBUG(1,("claim_connection: Max connections (%d) exceeded for %s\n",
152 max_connections, name ));
157 DEBUG(5,("claiming %s %d\n",name,max_connections));
160 key.pid = sys_getpid();
161 key.cnum = conn?conn->cnum:-1;
162 fstrcpy(key.name, name);
164 kbuf.dptr = (char *)&key;
165 kbuf.dsize = sizeof(key);
167 /* fill in the crec */
169 crec.magic = 0x280267;
170 crec.pid = sys_getpid();
171 crec.cnum = conn?conn->cnum:-1;
173 crec.uid = conn->uid;
174 crec.gid = conn->gid;
176 lp_servicename(SNUM(conn)),sizeof(crec.name)-1);
178 crec.start = time(NULL);
179 crec.bcast_msg_flags = msg_flags;
181 StrnCpy(crec.machine,get_remote_machine_name(),sizeof(crec.machine)-1);
182 StrnCpy(crec.addr,conn?conn->client_address:client_addr(),sizeof(crec.addr)-1);
184 dbuf.dptr = (char *)&crec;
185 dbuf.dsize = sizeof(crec);
187 if (tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) != 0) {
188 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",
189 tdb_errorstr(tdb) ));