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/>.
21 #include "smbd/smbd.h"
22 #include "smbd/globals.h"
26 /****************************************************************************
27 Delete a connection record.
28 ****************************************************************************/
30 bool yield_connection(connection_struct
*conn
, const char *name
)
32 struct db_record
*rec
;
35 DEBUG(3,("Yielding connection to %s\n",name
));
37 rec
= connections_fetch_entry(talloc_tos(), conn
, name
);
39 DEBUG(0, ("connections_fetch_entry failed\n"));
43 status
= rec
->delete_rec(rec
);
44 if (!NT_STATUS_IS_OK(status
)) {
45 DEBUG( NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
) ? 3 : 0,
46 ("deleting connection record returned %s\n",
51 return NT_STATUS_IS_OK(status
);
60 /****************************************************************************
61 Count the entries belonging to a service in the connection db.
62 ****************************************************************************/
64 static int count_fn(struct db_record
*rec
,
65 const struct connections_key
*ckey
,
66 const struct connections_data
*crec
,
69 struct count_stat
*cs
= (struct count_stat
*)udp
;
71 if (crec
->cnum
== -1) {
75 /* If the pid was not found delete the entry from connections.tdb */
77 if (cs
->Clear
&& !process_exists(crec
->pid
) && (errno
== ESRCH
)) {
79 DEBUG(2,("pid %s doesn't exist - deleting connections %d [%s]\n",
80 procid_str_static(&crec
->pid
), crec
->cnum
,
83 status
= rec
->delete_rec(rec
);
84 if (!NT_STATUS_IS_OK(status
)) {
85 DEBUG(0,("count_fn: tdb_delete failed with error %s\n",
91 if (strequal(crec
->servicename
, cs
->name
))
92 cs
->curr_connections
++;
97 /****************************************************************************
98 Claim an entry in the connections database.
99 ****************************************************************************/
101 int count_current_connections( const char *sharename
, bool clear
)
103 struct count_stat cs
;
106 cs
.curr_connections
= 0;
111 * This has a race condition, but locking the chain before hand is worse
112 * as it leads to deadlock.
116 * become_root() because we might have to open connections.tdb
117 * via ctdb, which is not possible without root.
120 ret
= connections_forall(count_fn
, &cs
);
124 DEBUG(0,("count_current_connections: traverse of "
125 "connections.tdb failed\n"));
129 return cs
.curr_connections
;
132 /****************************************************************************
133 Claim an entry in the connections database.
134 ****************************************************************************/
136 bool claim_connection(connection_struct
*conn
, const char *name
)
138 struct db_record
*rec
;
139 struct connections_data crec
;
143 DEBUG(5,("claiming [%s]\n", name
));
145 if (!(rec
= connections_fetch_entry(talloc_tos(), conn
, name
))) {
146 DEBUG(0, ("connections_fetch_entry failed\n"));
150 /* fill in the crec */
152 crec
.magic
= 0x280267;
153 crec
.pid
= sconn_server_id(conn
->sconn
);
154 crec
.cnum
= conn
->cnum
;
155 crec
.uid
= conn
->session_info
->utok
.uid
;
156 crec
.gid
= conn
->session_info
->utok
.gid
;
157 strlcpy(crec
.servicename
, lp_servicename(SNUM(conn
)),
158 sizeof(crec
.servicename
));
159 crec
.start
= time(NULL
);
161 strlcpy(crec
.machine
,get_remote_machine_name(),sizeof(crec
.machine
));
162 strlcpy(crec
.addr
, conn
->sconn
->client_id
.addr
, sizeof(crec
.addr
));
164 dbuf
.dptr
= (uint8
*)&crec
;
165 dbuf
.dsize
= sizeof(crec
);
167 status
= rec
->store(rec
, dbuf
, TDB_REPLACE
);
171 if (!NT_STATUS_IS_OK(status
)) {
172 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",