converted all our existing shared memory code to use a tdb database
[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
27 extern int DEBUGLEVEL;
28
29 /****************************************************************************
30 delete a connection record
31 ****************************************************************************/
32 BOOL yield_connection(connection_struct *conn,char *name,int max_connections)
33 {
34         struct connections_key key;
35         TDB_DATA kbuf;
36         TDB_CONTEXT *tdb;
37
38         tdb = tdb_open(lock_path("connections.tdb"), 0, O_RDWR | O_CREAT, 0644);
39         if (!tdb) return False;
40
41         DEBUG(3,("Yielding connection to %s\n",name));
42
43         ZERO_STRUCT(key);
44         key.pid = getpid();
45         if (conn) key.cnum = conn->cnum;
46         fstrcpy(key.name, name);
47
48         kbuf.dptr = (char *)&key;
49         kbuf.dsize = sizeof(key);
50
51         tdb_delete(tdb, kbuf);
52         tdb_close(tdb);
53         return(True);
54 }
55
56
57 /****************************************************************************
58 claim an entry in the connections database
59 ****************************************************************************/
60 int delete_dead(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf)
61 {
62         struct connections_key key;
63         memcpy(&key, kbuf.dptr, sizeof(key));
64         if (!process_exists(key.pid)) tdb_delete(tdb, kbuf);
65         return 0;
66 }
67
68
69 /****************************************************************************
70 claim an entry in the connections database
71 ****************************************************************************/
72 BOOL claim_connection(connection_struct *conn,char *name,int max_connections,BOOL Clear)
73 {
74         struct connections_key key;
75         struct connections_data crec;
76         TDB_DATA kbuf, dbuf;
77         TDB_CONTEXT *tdb;
78         extern int Client;
79
80         if (max_connections <= 0)
81                 return(True);
82         
83         tdb = tdb_open(lock_path("connections.tdb"), 0, O_RDWR | O_CREAT, 0644);
84         if (!tdb) return False;
85
86         DEBUG(5,("claiming %s %d\n",name,max_connections));
87
88         ZERO_STRUCT(key);
89         key.pid = getpid();
90         key.cnum = conn?conn->cnum:-1;
91         fstrcpy(key.name, name);
92
93         kbuf.dptr = (char *)&key;
94         kbuf.dsize = sizeof(key);
95
96         if (Clear) {
97                 tdb_traverse(tdb, delete_dead);
98         }
99
100         /* fill in the crec */
101         ZERO_STRUCT(crec);
102         crec.magic = 0x280267;
103         crec.pid = getpid();
104         crec.cnum = conn?conn->cnum:-1;
105         if (conn) {
106                 crec.uid = conn->uid;
107                 crec.gid = conn->gid;
108                 StrnCpy(crec.name,
109                         lp_servicename(SNUM(conn)),sizeof(crec.name)-1);
110         }
111         crec.start = time(NULL);
112         
113         StrnCpy(crec.machine,remote_machine,sizeof(crec.machine)-1);
114         StrnCpy(crec.addr,conn?conn->client_address:client_addr(Client),sizeof(crec.addr)-1);
115
116         dbuf.dptr = (char *)&crec;
117         dbuf.dsize = sizeof(crec);
118
119         if (tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) != 0) return False;
120
121         tdb_close(tdb);
122
123         return True;
124 }