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;
27 extern int DEBUGLEVEL;
29 /****************************************************************************
30 open the connections database
31 ****************************************************************************/
32 TDB_CONTEXT *open_db(char *name)
36 pstrcpy(fname,lp_lockdir());
37 trim_string(fname,"","/");
39 if (!directory_exist(fname,NULL)) {
43 pstrcat(fname,"/connections.tdb");
45 return tdb_open(fname, 0, O_RDWR | O_CREAT, 0644);
50 /****************************************************************************
51 delete a connection record
52 ****************************************************************************/
53 BOOL yield_connection(connection_struct *conn,char *name,int max_connections)
55 struct connections_key key;
59 if (!(tdb = open_db(name))) return False;
61 DEBUG(3,("Yielding connection to %s\n",name));
65 if (conn) key.cnum = conn->cnum;
66 fstrcpy(key.name, name);
68 kbuf.dptr = (char *)&key;
69 kbuf.dsize = sizeof(key);
71 tdb_delete(tdb, kbuf);
77 /****************************************************************************
78 claim an entry in the connections database
79 ****************************************************************************/
80 int delete_dead(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf)
82 struct connections_key key;
83 memcpy(&key, kbuf.dptr, sizeof(key));
84 if (!process_exists(key.pid)) tdb_delete(tdb, kbuf);
89 /****************************************************************************
90 claim an entry in the connections database
91 ****************************************************************************/
92 BOOL claim_connection(connection_struct *conn,char *name,int max_connections,BOOL Clear)
94 struct connections_key key;
95 struct connections_data crec;
100 if (max_connections <= 0)
103 if (!(tdb = open_db(name))) return False;
105 DEBUG(5,("claiming %s %d\n",name,max_connections));
109 key.cnum = conn?conn->cnum:-1;
110 fstrcpy(key.name, name);
112 kbuf.dptr = (char *)&key;
113 kbuf.dsize = sizeof(key);
116 tdb_traverse(tdb, delete_dead);
119 /* fill in the crec */
121 crec.magic = 0x280267;
123 crec.cnum = conn?conn->cnum:-1;
125 crec.uid = conn->uid;
126 crec.gid = conn->gid;
128 lp_servicename(SNUM(conn)),sizeof(crec.name)-1);
130 crec.start = time(NULL);
132 StrnCpy(crec.machine,remote_machine,sizeof(crec.machine)-1);
133 StrnCpy(crec.addr,conn?conn->client_address:client_addr(Client),sizeof(crec.addr)-1);
135 dbuf.dptr = (char *)&crec;
136 dbuf.dsize = sizeof(crec);
138 if (tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) != 0) return False;