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 3 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, see <http://www.gnu.org/licenses/>.
22 /****************************************************************************
23 Delete a connection record.
24 ****************************************************************************/
26 bool yield_connection(connection_struct *conn, const char *name)
28 struct db_record *rec;
31 DEBUG(3,("Yielding connection to %s\n",name));
33 if (!(rec = connections_fetch_entry(NULL, conn, name))) {
34 DEBUG(0, ("connections_fetch_entry failed\n"));
38 status = rec->delete_rec(rec);
39 if (!NT_STATUS_IS_OK(status)) {
40 DEBUG( NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND) ? 3 : 0,
41 ("deleting connection record returned %s\n",
46 return NT_STATUS_IS_OK(status);
56 /****************************************************************************
57 Count the entries belonging to a service in the connection db.
58 ****************************************************************************/
60 static int count_fn(struct db_record *rec,
61 const struct connections_key *ckey,
62 const struct connections_data *crec,
65 struct count_stat *cs = (struct count_stat *)udp;
67 if (crec->cnum == -1) {
71 /* If the pid was not found delete the entry from connections.tdb */
73 if (cs->Clear && !process_exists(crec->pid) && (errno == ESRCH)) {
75 DEBUG(2,("pid %s doesn't exist - deleting connections %d [%s]\n",
76 procid_str_static(&crec->pid), crec->cnum,
79 status = rec->delete_rec(rec);
80 if (!NT_STATUS_IS_OK(status)) {
81 DEBUG(0,("count_fn: tdb_delete failed with error %s\n",
87 if (strequal(crec->servicename, cs->name))
88 cs->curr_connections++;
93 /****************************************************************************
94 Claim an entry in the connections database.
95 ****************************************************************************/
97 int count_current_connections( const char *sharename, bool clear )
101 cs.mypid = sys_getpid();
102 cs.curr_connections = 0;
107 * This has a race condition, but locking the chain before hand is worse
108 * as it leads to deadlock.
111 if (connections_forall(count_fn, &cs) == -1) {
112 DEBUG(0,("count_current_connections: traverse of "
113 "connections.tdb failed\n"));
117 return cs.curr_connections;
120 /****************************************************************************
121 Claim an entry in the connections database.
122 ****************************************************************************/
124 bool claim_connection(connection_struct *conn, const char *name,
127 struct db_record *rec;
128 struct connections_data crec;
131 char addr[INET6_ADDRSTRLEN];
133 DEBUG(5,("claiming [%s]\n", name));
135 if (!(rec = connections_fetch_entry(talloc_tos(), conn, name))) {
136 DEBUG(0, ("connections_fetch_entry failed\n"));
140 /* fill in the crec */
142 crec.magic = 0x280267;
143 crec.pid = procid_self();
144 crec.cnum = conn?conn->cnum:-1;
146 crec.uid = conn->uid;
147 crec.gid = conn->gid;
148 strlcpy(crec.servicename, lp_servicename(SNUM(conn)),
149 sizeof(crec.servicename));
151 crec.start = time(NULL);
152 crec.bcast_msg_flags = msg_flags;
154 strlcpy(crec.machine,get_remote_machine_name(),sizeof(crec.machine));
155 strlcpy(crec.addr,conn?conn->client_address:
156 client_addr(get_client_fd(),addr,sizeof(addr)),
159 dbuf.dptr = (uint8 *)&crec;
160 dbuf.dsize = sizeof(crec);
162 status = rec->store(rec, dbuf, TDB_REPLACE);
166 if (!NT_STATUS_IS_OK(status)) {
167 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",
175 bool register_message_flags(bool doreg, uint32 msg_flags)
177 struct db_record *rec;
178 struct connections_data *pcrec;
181 DEBUG(10,("register_message_flags: %s flags 0x%x\n",
182 doreg ? "adding" : "removing",
183 (unsigned int)msg_flags ));
185 if (!(rec = connections_fetch_entry(NULL, NULL, ""))) {
186 DEBUG(0, ("connections_fetch_entry failed\n"));
190 if (rec->value.dsize != sizeof(struct connections_data)) {
191 DEBUG(0,("register_message_flags: Got wrong record size\n"));
196 pcrec = (struct connections_data *)rec->value.dptr;
198 pcrec->bcast_msg_flags |= msg_flags;
200 pcrec->bcast_msg_flags &= ~msg_flags;
202 status = rec->store(rec, rec->value, TDB_REPLACE);
204 if (!NT_STATUS_IS_OK(status)) {
205 DEBUG(0,("register_message_flags: tdb_store failed: %s.\n",
211 DEBUG(10,("register_message_flags: new flags 0x%x\n",
212 (unsigned int)pcrec->bcast_msg_flags ));
219 /*********************************************************************
220 *********************************************************************/
222 static TDB_DATA* make_pipe_rec_key( struct pipe_open_rec *prec )
224 TDB_DATA *kbuf = NULL;
230 if ( (kbuf = TALLOC_P(prec, TDB_DATA)) == NULL ) {
234 snprintf( key_string, sizeof(key_string), "%s/%d/%d",
235 prec->name, procid_to_pid(&prec->pid), prec->pnum );
237 *kbuf = string_term_tdb_data(talloc_strdup(prec, key_string));
238 if (kbuf->dptr == NULL )
244 /*********************************************************************
245 *********************************************************************/
247 static void fill_pipe_open_rec( struct pipe_open_rec *prec, smb_np_struct *p )
249 prec->pid = pid_to_procid(sys_getpid());
250 prec->pnum = p->pnum;
251 prec->uid = geteuid();
252 fstrcpy( prec->name, p->name );
257 /*********************************************************************
258 *********************************************************************/
260 bool store_pipe_opendb( smb_np_struct *p )
262 struct db_record *dbrec;
263 struct pipe_open_rec *prec;
268 if ( (prec = TALLOC_P( NULL, struct pipe_open_rec)) == NULL ) {
269 DEBUG(0,("store_pipe_opendb: talloc failed!\n"));
273 fill_pipe_open_rec( prec, p );
274 if ( (key = make_pipe_rec_key( prec )) == NULL ) {
278 data.dptr = (uint8 *)prec;
279 data.dsize = sizeof(struct pipe_open_rec);
281 if (!(dbrec = connections_fetch_record(prec, *key))) {
282 DEBUG(0, ("connections_fetch_record failed\n"));
286 ret = NT_STATUS_IS_OK(dbrec->store(dbrec, data, TDB_REPLACE));
293 /*********************************************************************
294 *********************************************************************/
296 bool delete_pipe_opendb( smb_np_struct *p )
298 struct db_record *dbrec;
299 struct pipe_open_rec *prec;
303 if ( (prec = TALLOC_P( NULL, struct pipe_open_rec)) == NULL ) {
304 DEBUG(0,("store_pipe_opendb: talloc failed!\n"));
308 fill_pipe_open_rec( prec, p );
309 if ( (key = make_pipe_rec_key( prec )) == NULL ) {
313 if (!(dbrec = connections_fetch_record(prec, *key))) {
314 DEBUG(0, ("connections_fetch_record failed\n"));
318 ret = NT_STATUS_IS_OK(dbrec->delete_rec(dbrec));