Log tdb failures.
[samba.git] / source / smbd / connection.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    connection claim routines
5    Copyright (C) Andrew Tridgell 1998
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24
25 extern fstring remote_machine;
26 static TDB_CONTEXT *tdb;
27
28 extern int DEBUGLEVEL;
29
30 /****************************************************************************
31  Return the connection tdb context (used for message send all).
32 ****************************************************************************/
33
34 TDB_CONTEXT *conn_tdb_ctx(void)
35 {
36         return tdb;
37 }
38
39 /****************************************************************************
40  Delete a connection record.
41 ****************************************************************************/
42
43 BOOL yield_connection(connection_struct *conn,char *name,int max_connections)
44 {
45         struct connections_key key;
46         TDB_DATA kbuf;
47
48         if (!tdb) return False;
49
50         DEBUG(3,("Yielding connection to %s\n",name));
51
52         ZERO_STRUCT(key);
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 */
57
58         kbuf.dptr = (char *)&key;
59         kbuf.dsize = sizeof(key);
60
61         if (tdb_delete(tdb, kbuf) != 0) {
62                 DEBUG(0,("yield_connection: tdb_delete failed with error %s.\n", tdb_errorstr(tdb) ));
63                 return (False);
64         }
65
66         return(True);
67 }
68
69 struct count_stat {
70         pid_t mypid;
71         int curr_connections;
72         char *name;
73         BOOL Clear;
74 };
75
76 /****************************************************************************
77  Count the entries belonging to a service in the connection db.
78 ****************************************************************************/
79
80 static int count_fn( TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *udp)
81 {
82         struct connections_data crec;
83         struct count_stat *cs = (struct count_stat *)udp;
84  
85         if (dbuf.dsize != sizeof(crec))
86                 return 0;
87
88         memcpy(&crec, dbuf.dptr, sizeof(crec));
89  
90     if (crec.cnum == -1)
91                 return 0;
92
93         /* If the pid was not found delete the entry from connections.tdb */
94
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) ));
100                 return 0;
101         }
102
103         if (strequal(crec.name, cs->name))
104                 cs->curr_connections++;
105
106         return 0;
107 }
108
109 /****************************************************************************
110  Claim an entry in the connections database.
111 ****************************************************************************/
112
113 BOOL claim_connection(connection_struct *conn,char *name,int max_connections,BOOL Clear)
114 {
115         struct connections_key key;
116         struct connections_data crec;
117         TDB_DATA kbuf, dbuf, lockkey;
118         BOOL rec_locked = False;
119         BOOL ret = True;
120
121         if (!tdb) {
122                 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_CLEAR_IF_FIRST, 
123                                O_RDWR | O_CREAT, 0644);
124         }
125         if (!tdb)
126                 return False;
127
128         /*
129          * Enforce the max connections parameter.
130          */
131
132         if (max_connections > 0) {
133                 struct count_stat cs;
134
135                 cs.mypid = sys_getpid();
136                 cs.curr_connections = 0;
137                 cs.name = lp_servicename(SNUM(conn));
138                 cs.Clear = Clear;
139
140                 lockkey.dptr = cs.name;
141                 lockkey.dsize = strlen(cs.name)+1;
142
143                 /*
144                  * Go through and count the connections with hash chain representing the service name
145                  * locked. This is slow but removes race conditions. JRA.
146                  */
147
148                 if (tdb_chainlock(tdb, lockkey)) {
149                         DEBUG(0,("claim_connection: tdb_chainlock failed %s\n",
150                                 tdb_errorstr(tdb) ));
151                         return False;
152                 }
153
154                 rec_locked = True;
155
156                 if (tdb_traverse(tdb, count_fn, &cs) == -1) {
157                         DEBUG(0,("claim_connection: traverse of connections.tdb failed with error %s.\n",
158                                 tdb_errorstr(tdb) ));
159                         ret = False;
160                         goto out;
161                 }
162
163                 if (cs.curr_connections >= max_connections) {
164                         DEBUG(1,("claim_connection: Max connections (%d) exceeded for %s\n",
165                                 max_connections, name ));
166                         ret = False;
167                         goto out;
168                 }
169         }
170
171         DEBUG(5,("claiming %s %d\n",name,max_connections));
172
173         ZERO_STRUCT(key);
174         key.pid = sys_getpid();
175         key.cnum = conn?conn->cnum:-1;
176         fstrcpy(key.name, name);
177         dos_to_unix(key.name, True);           /* Convert key to unix-codepage */
178
179         kbuf.dptr = (char *)&key;
180         kbuf.dsize = sizeof(key);
181
182         /* fill in the crec */
183         ZERO_STRUCT(crec);
184         crec.magic = 0x280267;
185         crec.pid = sys_getpid();
186         crec.cnum = conn?conn->cnum:-1;
187         if (conn) {
188                 crec.uid = conn->uid;
189                 crec.gid = conn->gid;
190                 StrnCpy(crec.name,
191                         lp_servicename(SNUM(conn)),sizeof(crec.name)-1);
192         }
193         crec.start = time(NULL);
194         
195         StrnCpy(crec.machine,remote_machine,sizeof(crec.machine)-1);
196         StrnCpy(crec.addr,conn?conn->client_address:client_addr(),sizeof(crec.addr)-1);
197
198         dbuf.dptr = (char *)&crec;
199         dbuf.dsize = sizeof(crec);
200
201         if (tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) != 0) {
202                 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",
203                         tdb_errorstr(tdb) ));
204                 ret = False;
205         }
206
207   out:
208
209         if (rec_locked)
210                 tdb_chainunlock(tdb, lockkey);
211
212         return ret;
213 }