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"
25 /****************************************************************************
26 Delete a connection record.
27 ****************************************************************************/
29 bool yield_connection(connection_struct *conn, const char *name)
31 struct db_record *rec;
34 DEBUG(3,("Yielding connection to %s\n",name));
36 rec = connections_fetch_entry(talloc_tos(), conn, name);
38 DEBUG(0, ("connections_fetch_entry failed\n"));
42 status = rec->delete_rec(rec);
43 if (!NT_STATUS_IS_OK(status)) {
44 DEBUG( NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND) ? 3 : 0,
45 ("deleting connection record returned %s\n",
50 return NT_STATUS_IS_OK(status);
59 /****************************************************************************
60 Count the entries belonging to a service in the connection db.
61 ****************************************************************************/
63 static int count_fn(struct db_record *rec,
64 const struct connections_key *ckey,
65 const struct connections_data *crec,
68 struct count_stat *cs = (struct count_stat *)udp;
70 if (crec->cnum == -1) {
74 /* If the pid was not found delete the entry from connections.tdb */
76 if (cs->Clear && !process_exists(crec->pid) && (errno == ESRCH)) {
78 DEBUG(2,("pid %s doesn't exist - deleting connections %d [%s]\n",
79 procid_str_static(&crec->pid), crec->cnum,
82 status = rec->delete_rec(rec);
83 if (!NT_STATUS_IS_OK(status)) {
84 DEBUG(0,("count_fn: tdb_delete failed with error %s\n",
90 if (strequal(crec->servicename, cs->name))
91 cs->curr_connections++;
96 /****************************************************************************
97 Claim an entry in the connections database.
98 ****************************************************************************/
100 int count_current_connections( const char *sharename, bool clear )
102 struct count_stat cs;
104 cs.curr_connections = 0;
109 * This has a race condition, but locking the chain before hand is worse
110 * as it leads to deadlock.
113 if (connections_forall(count_fn, &cs) == -1) {
114 DEBUG(0,("count_current_connections: traverse of "
115 "connections.tdb failed\n"));
119 return cs.curr_connections;
122 /****************************************************************************
123 Claim an entry in the connections database.
124 ****************************************************************************/
126 bool claim_connection(connection_struct *conn, const char *name)
128 struct db_record *rec;
129 struct connections_data crec;
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 = sconn_server_id(conn->sconn);
144 crec.cnum = conn->cnum;
145 crec.uid = conn->session_info->utok.uid;
146 crec.gid = conn->session_info->utok.gid;
147 strlcpy(crec.servicename, lp_servicename(SNUM(conn)),
148 sizeof(crec.servicename));
149 crec.start = time(NULL);
151 strlcpy(crec.machine,get_remote_machine_name(),sizeof(crec.machine));
152 strlcpy(crec.addr, conn->sconn->client_id.addr, sizeof(crec.addr));
154 dbuf.dptr = (uint8 *)&crec;
155 dbuf.dsize = sizeof(crec);
157 status = rec->store(rec, dbuf, TDB_REPLACE);
161 if (!NT_STATUS_IS_OK(status)) {
162 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",