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 /****************************************************************************
29 Return the connection tdb context (used for message send all).
30 ****************************************************************************/
32 TDB_CONTEXT
*conn_tdb_ctx(void)
37 /****************************************************************************
38 Delete a connection record.
39 ****************************************************************************/
41 BOOL
yield_connection(connection_struct
*conn
,const char *name
)
43 struct connections_key key
;
46 if (!tdb
) return False
;
48 DEBUG(3,("Yielding connection to %s\n",name
));
51 key
.pid
= sys_getpid();
52 key
.cnum
= conn
?conn
->cnum
:-1;
53 fstrcpy(key
.name
, name
);
54 dos_to_unix(key
.name
); /* Convert key to unix-codepage */
56 kbuf
.dptr
= (char *)&key
;
57 kbuf
.dsize
= sizeof(key
);
59 if (tdb_delete(tdb
, kbuf
) != 0) {
60 int dbg_lvl
= (!conn
&& (tdb_error(tdb
) == TDB_ERR_NOEXIST
)) ? 3 : 0;
61 DEBUG(dbg_lvl
,("yield_connection: tdb_delete for name %s failed with error %s.\n",
62 name
, tdb_errorstr(tdb
) ));
76 /****************************************************************************
77 Count the entries belonging to a service in the connection db.
78 ****************************************************************************/
80 static int count_fn( TDB_CONTEXT
*the_tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
, void *udp
)
82 struct connections_data crec
;
83 struct count_stat
*cs
= (struct count_stat
*)udp
;
85 if (dbuf
.dsize
!= sizeof(crec
))
88 memcpy(&crec
, dbuf
.dptr
, sizeof(crec
));
93 /* If the pid was not found delete the entry from connections.tdb */
95 if (cs
->Clear
&& !process_exists(crec
.pid
) && (errno
== ESRCH
)) {
96 DEBUG(2,("pid %u doesn't exist - deleting connections %d [%s]\n",
97 (unsigned int)crec
.pid
, crec
.cnum
, crec
.name
));
98 if (tdb_delete(the_tdb
, kbuf
) != 0)
99 DEBUG(0,("count_fn: tdb_delete failed with error %s\n", tdb_errorstr(tdb
) ));
103 if (strequal(crec
.name
, cs
->name
))
104 cs
->curr_connections
++;
109 /****************************************************************************
110 Claim an entry in the connections database.
111 ****************************************************************************/
113 BOOL
claim_connection(connection_struct
*conn
,const char *name
,int max_connections
,BOOL Clear
)
115 struct connections_key key
;
116 struct connections_data crec
;
120 tdb
= tdb_open_log(lock_path("connections.tdb"), 0, TDB_CLEAR_IF_FIRST
|TDB_DEFAULT
,
121 O_RDWR
| O_CREAT
, 0644);
127 * Enforce the max connections parameter.
130 if (max_connections
> 0) {
131 struct count_stat cs
;
133 cs
.mypid
= sys_getpid();
134 cs
.curr_connections
= 0;
135 cs
.name
= lp_servicename(SNUM(conn
));
139 * This has a race condition, but locking the chain before hand is worse
140 * as it leads to deadlock.
143 if (tdb_traverse(tdb
, count_fn
, &cs
) == -1) {
144 DEBUG(0,("claim_connection: traverse of connections.tdb failed with error %s.\n",
145 tdb_errorstr(tdb
) ));
149 if (cs
.curr_connections
>= max_connections
) {
150 DEBUG(1,("claim_connection: Max connections (%d) exceeded for %s\n",
151 max_connections
, name
));
156 DEBUG(5,("claiming %s %d\n",name
,max_connections
));
159 key
.pid
= sys_getpid();
160 key
.cnum
= conn
?conn
->cnum
:-1;
161 fstrcpy(key
.name
, name
);
162 dos_to_unix(key
.name
); /* Convert key to unix-codepage */
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
);
180 StrnCpy(crec
.machine
,remote_machine
,sizeof(crec
.machine
)-1);
181 StrnCpy(crec
.addr
,conn
?conn
->client_address
:client_addr(),sizeof(crec
.addr
)-1);
183 dbuf
.dptr
= (char *)&crec
;
184 dbuf
.dsize
= sizeof(crec
);
186 if (tdb_store(tdb
, kbuf
, dbuf
, TDB_REPLACE
) != 0) {
187 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",
188 tdb_errorstr(tdb
) ));