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);
56 dos_to_unix(key.name, True); /* Convert key to unix-codepage */
58 kbuf.dptr = (char *)&key;
59 kbuf.dsize = sizeof(key);
61 tdb_delete(tdb, kbuf);
73 /****************************************************************************
74 Count the entries belonging to a service in the connection db.
75 ****************************************************************************/
77 static int count_fn( TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *udp)
79 struct connections_data crec;
80 struct count_stat *cs = (struct count_stat *)udp;
82 memcpy(&crec, dbuf.dptr, sizeof(crec));
87 /* if the pid was not found delete the entry from connections.tdb */
88 if (cs->Clear && !process_exists(crec.pid)) {
89 DEBUG(2,("pid %u doesn't exist - deleting connections %d [%s]\n",
90 (unsigned int)crec.pid, crec.cnum, crec.name));
91 tdb_delete(the_tdb, kbuf);
95 if (cs && strequal(crec.name, cs->name))
96 cs->curr_connections++;
101 /****************************************************************************
102 Claim an entry in the connections database.
103 ****************************************************************************/
105 BOOL claim_connection(connection_struct *conn,char *name,int max_connections,BOOL Clear)
107 struct connections_key key;
108 struct connections_data crec;
112 tdb = tdb_open(lock_path("connections.tdb"), 0, TDB_CLEAR_IF_FIRST,
113 O_RDWR | O_CREAT, 0644);
119 * Enforce the max connections parameter.
122 if (max_connections > 0) {
123 struct count_stat cs;
125 cs.mypid = sys_getpid();
126 cs.curr_connections = 0;
127 cs.name = lp_servicename(SNUM(conn));
131 * Go through and count the connections
133 if (tdb_traverse(tdb, count_fn, &cs) == -1) {
134 DEBUG(0,("claim_connection: traverse of connections.tdb failed.\n"));
138 if (cs.curr_connections >= max_connections) {
139 DEBUG(1,("claim_connection: Max connections (%d) exceeded for %s\n",
140 max_connections, name ));
145 DEBUG(5,("claiming %s %d\n",name,max_connections));
148 key.pid = sys_getpid();
149 key.cnum = conn?conn->cnum:-1;
150 fstrcpy(key.name, name);
151 dos_to_unix(key.name, True); /* Convert key to unix-codepage */
153 kbuf.dptr = (char *)&key;
154 kbuf.dsize = sizeof(key);
156 /* fill in the crec */
158 crec.magic = 0x280267;
159 crec.pid = sys_getpid();
160 crec.cnum = conn?conn->cnum:-1;
162 crec.uid = conn->uid;
163 crec.gid = conn->gid;
165 lp_servicename(SNUM(conn)),sizeof(crec.name)-1);
167 crec.start = time(NULL);
169 StrnCpy(crec.machine,remote_machine,sizeof(crec.machine)-1);
170 StrnCpy(crec.addr,conn?conn->client_address:client_addr(),sizeof(crec.addr)-1);
172 dbuf.dptr = (char *)&crec;
173 dbuf.dsize = sizeof(crec);
175 if (tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) != 0)
182 /****************************************************************************
183 Use the count function to clean any dead records. Shouldn't be needed...
184 ****************************************************************************/
186 void clean_connection_db(void)
188 if (tdb_traverse(tdb, count_fn, NULL) == -1)
189 DEBUG(0,("clean_connection_db: traverse of connections.tdb failed.\n"));