r1899: this is 3.0.6 -- will release tomorrow
[Samba.git] / source / smbd / connection.c
blobfc5fe9d7418df7c0c7193c23f2da54a9f4972406
1 /*
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.
21 #include "includes.h"
23 static TDB_CONTEXT *tdb;
25 /****************************************************************************
26 Return the connection tdb context (used for message send all).
27 ****************************************************************************/
29 TDB_CONTEXT *conn_tdb_ctx(void)
31 if (!tdb)
32 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
33 O_RDWR | O_CREAT, 0644);
35 return tdb;
38 static void make_conn_key(connection_struct *conn, const char *name, TDB_DATA *pkbuf, struct connections_key *pkey)
40 ZERO_STRUCTP(pkey);
41 pkey->pid = sys_getpid();
42 pkey->cnum = conn?conn->cnum:-1;
43 fstrcpy(pkey->name, name);
44 #ifdef DEVELOPER
45 /* valgrind fixer... */
47 size_t sl = strlen(pkey->name);
48 if (sizeof(fstring)-sl)
49 memset(&pkey->name[sl], '\0', sizeof(fstring)-sl);
51 #endif
53 pkbuf->dptr = (char *)pkey;
54 pkbuf->dsize = sizeof(*pkey);
57 /****************************************************************************
58 Delete a connection record.
59 ****************************************************************************/
61 BOOL yield_connection(connection_struct *conn, const char *name)
63 struct connections_key key;
64 TDB_DATA kbuf;
66 if (!tdb)
67 return False;
69 DEBUG(3,("Yielding connection to %s\n",name));
71 make_conn_key(conn, name, &kbuf, &key);
73 if (tdb_delete(tdb, kbuf) != 0) {
74 int dbg_lvl = (!conn && (tdb_error(tdb) == TDB_ERR_NOEXIST)) ? 3 : 0;
75 DEBUG(dbg_lvl,("yield_connection: tdb_delete for name %s failed with error %s.\n",
76 name, tdb_errorstr(tdb) ));
77 return (False);
80 return(True);
83 struct count_stat {
84 pid_t mypid;
85 int curr_connections;
86 char *name;
87 BOOL Clear;
90 /****************************************************************************
91 Count the entries belonging to a service in the connection db.
92 ****************************************************************************/
94 static int count_fn( TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *udp)
96 struct connections_data crec;
97 struct count_stat *cs = (struct count_stat *)udp;
99 if (dbuf.dsize != sizeof(crec))
100 return 0;
102 memcpy(&crec, dbuf.dptr, sizeof(crec));
104 if (crec.cnum == -1)
105 return 0;
107 /* If the pid was not found delete the entry from connections.tdb */
109 if (cs->Clear && !process_exists(crec.pid) && (errno == ESRCH)) {
110 DEBUG(2,("pid %u doesn't exist - deleting connections %d [%s]\n",
111 (unsigned int)crec.pid, crec.cnum, crec.name));
112 if (tdb_delete(the_tdb, kbuf) != 0)
113 DEBUG(0,("count_fn: tdb_delete failed with error %s\n", tdb_errorstr(tdb) ));
114 return 0;
117 if (strequal(crec.name, cs->name))
118 cs->curr_connections++;
120 return 0;
123 /****************************************************************************
124 Claim an entry in the connections database.
125 ****************************************************************************/
127 BOOL claim_connection(connection_struct *conn, const char *name,int max_connections,BOOL Clear, uint32 msg_flags)
129 struct connections_key key;
130 struct connections_data crec;
131 TDB_DATA kbuf, dbuf;
133 if (!tdb)
134 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
135 O_RDWR | O_CREAT, 0644);
137 if (!tdb)
138 return False;
141 * Enforce the max connections parameter.
144 if (max_connections > 0) {
145 struct count_stat cs;
147 cs.mypid = sys_getpid();
148 cs.curr_connections = 0;
149 cs.name = lp_servicename(SNUM(conn));
150 cs.Clear = Clear;
153 * This has a race condition, but locking the chain before hand is worse
154 * as it leads to deadlock.
157 if (tdb_traverse(tdb, count_fn, &cs) == -1) {
158 DEBUG(0,("claim_connection: traverse of connections.tdb failed with error %s.\n",
159 tdb_errorstr(tdb) ));
160 return False;
163 if (cs.curr_connections >= max_connections) {
164 DEBUG(1,("claim_connection: Max connections (%d) exceeded for %s\n",
165 max_connections, name ));
166 return False;
170 DEBUG(5,("claiming %s %d\n",name,max_connections));
172 make_conn_key(conn, name, &kbuf, &key);
174 /* fill in the crec */
175 ZERO_STRUCT(crec);
176 crec.magic = 0x280267;
177 crec.pid = sys_getpid();
178 crec.cnum = conn?conn->cnum:-1;
179 if (conn) {
180 crec.uid = conn->uid;
181 crec.gid = conn->gid;
182 safe_strcpy(crec.name,
183 lp_servicename(SNUM(conn)),sizeof(crec.name)-1);
185 crec.start = time(NULL);
186 crec.bcast_msg_flags = msg_flags;
188 safe_strcpy(crec.machine,get_remote_machine_name(),sizeof(crec.machine)-1);
189 safe_strcpy(crec.addr,conn?conn->client_address:client_addr(),sizeof(crec.addr)-1);
191 dbuf.dptr = (char *)&crec;
192 dbuf.dsize = sizeof(crec);
194 if (tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) != 0) {
195 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",
196 tdb_errorstr(tdb) ));
197 return False;
200 return True;
203 BOOL register_message_flags(BOOL doreg, uint32 msg_flags)
205 struct connections_key key;
206 struct connections_data *pcrec;
207 TDB_DATA kbuf, dbuf;
209 if (!tdb)
210 return False;
212 DEBUG(10,("register_message_flags: %s flags 0x%x\n",
213 doreg ? "adding" : "removing",
214 (unsigned int)msg_flags ));
216 make_conn_key(NULL, "", &kbuf, &key);
218 dbuf = tdb_fetch(tdb, kbuf);
219 if (!dbuf.dptr) {
220 DEBUG(0,("register_message_flags: tdb_fetch failed\n"));
221 return False;
224 pcrec = (struct connections_data *)dbuf.dptr;
225 if (doreg)
226 pcrec->bcast_msg_flags |= msg_flags;
227 else
228 pcrec->bcast_msg_flags &= ~msg_flags;
230 if (tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) != 0) {
231 DEBUG(0,("register_message_flags: tdb_store failed with error %s.\n",
232 tdb_errorstr(tdb) ));
233 SAFE_FREE(dbuf.dptr);
234 return False;
237 DEBUG(10,("register_message_flags: new flags 0x%x\n",
238 (unsigned int)pcrec->bcast_msg_flags ));
240 SAFE_FREE(dbuf.dptr);
241 return True;