2 Unix SMB/Netbios implementation.
4 connection claim routines
5 Copyright (C) Andrew Tridgell 1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 extern fstring remote_machine;
26 static TDB_CONTEXT *tdb;
28 extern int DEBUGLEVEL;
30 /****************************************************************************
31 Return the connection tdb context (used for message send all).
32 ****************************************************************************/
34 TDB_CONTEXT *conn_tdb_ctx(void)
39 /****************************************************************************
40 Delete a connection record.
41 ****************************************************************************/
43 BOOL yield_connection(connection_struct *conn,char *name,int max_connections)
45 struct connections_key key;
48 if (!tdb) return False;
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 DEBUG(0,("yield_connection: tdb_delete failed with error %s.\n", tdb_errorstr(tdb) ));
75 /****************************************************************************
76 Count the entries belonging to a service in the connection db.
77 ****************************************************************************/
79 static int count_fn( TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *udp)
81 struct connections_data crec;
82 struct count_stat *cs = (struct count_stat *)udp;
84 if (dbuf.dsize != sizeof(crec))
87 memcpy(&crec, dbuf.dptr, sizeof(crec));
92 /* If the pid was not found delete the entry from connections.tdb */
94 if (cs->Clear && !process_exists(crec.pid) && (errno == ESRCH)) {
95 DEBUG(2,("pid %u doesn't exist - deleting connections %d [%s]\n",
96 (unsigned int)crec.pid, crec.cnum, crec.name));
97 if (tdb_delete(the_tdb, kbuf) != 0)
98 DEBUG(0,("count_fn: tdb_delete failed with error %s\n", tdb_errorstr(tdb) ));
102 if (strequal(crec.name, cs->name))
103 cs->curr_connections++;
108 /****************************************************************************
109 Claim an entry in the connections database.
110 ****************************************************************************/
112 BOOL claim_connection(connection_struct *conn,char *name,int max_connections,BOOL Clear)
114 struct connections_key key;
115 struct connections_data crec;
116 TDB_DATA kbuf, dbuf, lockkey;
117 BOOL rec_locked = False;
121 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_CLEAR_IF_FIRST,
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));
139 lockkey.dptr = cs.name;
140 lockkey.dsize = strlen(cs.name)+1;
143 * Go through and count the connections with hash chain representing the service name
144 * locked. This is slow but removes race conditions. JRA.
147 if (tdb_chainlock(tdb, lockkey)) {
148 DEBUG(0,("claim_connection: tdb_chainlock failed %s\n",
149 tdb_errorstr(tdb) ));
155 if (tdb_traverse(tdb, count_fn, &cs) == -1) {
156 DEBUG(0,("claim_connection: traverse of connections.tdb failed with error %s.\n",
157 tdb_errorstr(tdb) ));
162 if (cs.curr_connections >= max_connections) {
163 DEBUG(1,("claim_connection: Max connections (%d) exceeded for %s\n",
164 max_connections, name ));
170 DEBUG(5,("claiming %s %d\n",name,max_connections));
173 key.pid = sys_getpid();
174 key.cnum = conn?conn->cnum:-1;
175 fstrcpy(key.name, name);
177 kbuf.dptr = (char *)&key;
178 kbuf.dsize = sizeof(key);
180 /* fill in the crec */
182 crec.magic = 0x280267;
183 crec.pid = sys_getpid();
184 crec.cnum = conn?conn->cnum:-1;
186 crec.uid = conn->uid;
187 crec.gid = conn->gid;
189 lp_servicename(SNUM(conn)),sizeof(crec.name)-1);
191 crec.start = time(NULL);
193 StrnCpy(crec.machine,remote_machine,sizeof(crec.machine)-1);
194 StrnCpy(crec.addr,conn?conn->client_address:client_addr(),sizeof(crec.addr)-1);
196 dbuf.dptr = (char *)&crec;
197 dbuf.dsize = sizeof(crec);
199 if (tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) != 0) {
200 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",
201 tdb_errorstr(tdb) ));
208 tdb_chainunlock(tdb, lockkey);