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 void make_conn_key(connection_struct *conn, const char *name, TDB_DATA *pkbuf, struct connections_key *pkey)
26 pkey->pid = procid_self();
27 pkey->cnum = conn?conn->cnum:-1;
28 fstrcpy(pkey->name, name);
30 /* valgrind fixer... */
32 size_t sl = strlen(pkey->name);
33 if (sizeof(fstring)-sl)
34 memset(&pkey->name[sl], '\0', sizeof(fstring)-sl);
38 pkbuf->dptr = (uint8 *)pkey;
39 pkbuf->dsize = sizeof(*pkey);
42 /****************************************************************************
43 Delete a connection record.
44 ****************************************************************************/
46 BOOL yield_connection(connection_struct *conn, const char *name)
48 struct connections_key key;
50 TDB_CONTEXT *tdb = conn_tdb_ctx(True);
55 DEBUG(3,("Yielding connection to %s\n",name));
57 make_conn_key(conn, name, &kbuf, &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)) {
89 memcpy(&crec, dbuf.dptr, sizeof(crec));
91 if (crec.cnum == -1) {
95 /* If the pid was not found delete the entry from connections.tdb */
97 if (cs->Clear && !process_exists(crec.pid) && (errno == ESRCH)) {
98 DEBUG(2,("pid %s doesn't exist - deleting connections %d [%s]\n",
99 procid_str_static(&crec.pid), crec.cnum, crec.servicename));
100 if (tdb_delete(the_tdb, kbuf) != 0)
101 DEBUG(0,("count_fn: tdb_delete failed with error %s\n", tdb_errorstr(the_tdb) ));
106 /* We are counting all the connections to a given share. */
107 if (strequal(crec.servicename, cs->name)) {
108 cs->curr_connections++;
111 /* We are counting all the connections. Static registrations
112 * like the lpq backgroud process and the smbd daemon process
113 * have a cnum of -1, so won't be counted here.
115 cs->curr_connections++;
121 /****************************************************************************
122 Claim an entry in the connections database.
123 ****************************************************************************/
125 int count_current_connections( const char *sharename, BOOL clear )
127 struct count_stat cs;
128 TDB_CONTEXT *tdb = conn_tdb_ctx(True);
130 cs.mypid = sys_getpid();
131 cs.curr_connections = 0;
136 * This has a race condition, but locking the chain before hand is worse
137 * as it leads to deadlock.
140 if (tdb_traverse(tdb, count_fn, &cs) == -1) {
141 DEBUG(0,("count_current_connections: traverse of connections.tdb failed with error %s\n",
142 tdb_errorstr(tdb) ));
143 DEBUGADD(0, ("count_current_connections: connection count of %d might not be accurate",
144 cs.curr_connections));
147 /* If the traverse failed part-way through, we at least return
148 * as many connections as we had already counted. If it failed
149 * right at the start, we will return 0, which is about all we
153 return cs.curr_connections;
156 /****************************************************************************
157 Count the number of connections open across all shares.
158 ****************************************************************************/
160 int count_all_current_connections(void)
162 return count_current_connections(NULL, True /* clear stale entries */);
165 /****************************************************************************
166 Claim an entry in the connections database.
167 ****************************************************************************/
169 BOOL claim_connection(connection_struct *conn, const char *name,
170 int max_connections, uint32 msg_flags)
172 struct connections_key key;
173 struct connections_data crec;
175 TDB_CONTEXT *tdb = conn_tdb_ctx(True);
182 * Enforce the max connections parameter.
185 if (max_connections > 0) {
186 int curr_connections;
188 curr_connections = count_current_connections( lp_servicename(SNUM(conn)), True );
190 if (curr_connections >= max_connections) {
191 DEBUG(1,("claim_connection: Max connections (%d) exceeded for %s\n",
192 max_connections, name ));
197 DEBUG(5,("claiming %s %d\n",name,max_connections));
199 make_conn_key(conn, name, &kbuf, &key);
201 /* fill in the crec */
203 crec.magic = 0x280267;
204 crec.pid = procid_self();
205 crec.cnum = conn?conn->cnum:-1;
207 crec.uid = conn->uid;
208 crec.gid = conn->gid;
209 safe_strcpy(crec.servicename,
210 lp_servicename(SNUM(conn)),sizeof(crec.servicename)-1);
212 crec.start = time(NULL);
213 crec.bcast_msg_flags = msg_flags;
215 safe_strcpy(crec.machine,get_remote_machine_name(),sizeof(crec.machine)-1);
216 safe_strcpy(crec.addr,conn?conn->client_address:client_addr(),sizeof(crec.addr)-1);
218 dbuf.dptr = (uint8 *)&crec;
219 dbuf.dsize = sizeof(crec);
221 if (tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) != 0) {
222 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",
223 tdb_errorstr(tdb) ));
230 BOOL register_message_flags(BOOL doreg, uint32 msg_flags)
232 struct connections_key key;
233 struct connections_data *pcrec;
235 TDB_CONTEXT *tdb = conn_tdb_ctx(True);
240 DEBUG(10,("register_message_flags: %s flags 0x%x\n",
241 doreg ? "adding" : "removing",
242 (unsigned int)msg_flags ));
244 make_conn_key(NULL, "", &kbuf, &key);
246 dbuf = tdb_fetch(tdb, kbuf);
248 DEBUG(0,("register_message_flags: tdb_fetch failed: %s\n",
253 pcrec = (struct connections_data *)dbuf.dptr;
255 pcrec->bcast_msg_flags |= msg_flags;
257 pcrec->bcast_msg_flags &= ~msg_flags;
259 if (tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) != 0) {
260 DEBUG(0,("register_message_flags: tdb_store failed: %s.\n",
261 tdb_errorstr(tdb) ));
262 SAFE_FREE(dbuf.dptr);
266 DEBUG(10,("register_message_flags: new flags 0x%x\n",
267 (unsigned int)pcrec->bcast_msg_flags ));
269 SAFE_FREE(dbuf.dptr);
273 /*********************************************************************
274 *********************************************************************/
276 static TDB_DATA* make_pipe_rec_key( struct pipe_open_rec *prec )
278 TDB_DATA *kbuf = NULL;
284 if ( (kbuf = TALLOC_P(prec, TDB_DATA)) == NULL ) {
288 snprintf( key_string, sizeof(key_string), "%s/%d/%d",
289 prec->name, procid_to_pid(&prec->pid), prec->pnum );
291 *kbuf = string_term_tdb_data(talloc_strdup(prec, key_string));
292 if (kbuf->dptr == NULL )
298 /*********************************************************************
299 *********************************************************************/
301 static void fill_pipe_open_rec( struct pipe_open_rec *prec, smb_np_struct *p )
303 prec->pid = pid_to_procid(sys_getpid());
304 prec->pnum = p->pnum;
305 prec->uid = geteuid();
306 fstrcpy( prec->name, p->name );
311 /*********************************************************************
312 *********************************************************************/
314 BOOL store_pipe_opendb( smb_np_struct *p )
316 struct pipe_open_rec *prec;
319 TDB_CONTEXT *pipe_tdb;
322 if ( (prec = TALLOC_P( NULL, struct pipe_open_rec)) == NULL ) {
323 DEBUG(0,("store_pipe_opendb: talloc failed!\n"));
327 fill_pipe_open_rec( prec, p );
328 if ( (key = make_pipe_rec_key( prec )) == NULL ) {
332 data.dptr = (uint8 *)prec;
333 data.dsize = sizeof(struct pipe_open_rec);
335 if ( (pipe_tdb = conn_tdb_ctx(True) ) == NULL ) {
339 ret = (tdb_store( pipe_tdb, *key, data, TDB_REPLACE ) != -1);
346 /*********************************************************************
347 *********************************************************************/
349 BOOL delete_pipe_opendb( smb_np_struct *p )
351 struct pipe_open_rec *prec;
353 TDB_CONTEXT *pipe_tdb;
356 if ( (prec = TALLOC_P( NULL, struct pipe_open_rec)) == NULL ) {
357 DEBUG(0,("store_pipe_opendb: talloc failed!\n"));
361 fill_pipe_open_rec( prec, p );
362 if ( (key = make_pipe_rec_key( prec )) == NULL ) {
366 if ( (pipe_tdb = conn_tdb_ctx(True) ) == NULL ) {
370 ret = (tdb_delete( pipe_tdb, *key ) != -1 );