s3: async cli_list
[Samba/gbeck.git] / source3 / smbd / connection.c
blob9b766d586e19750634bf9dca009f018d54db47fa
1 /*
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/>.
20 #include "includes.h"
21 #include "smbd/globals.h"
23 /****************************************************************************
24 Delete a connection record.
25 ****************************************************************************/
27 bool yield_connection(connection_struct *conn, const char *name)
29 struct db_record *rec;
30 NTSTATUS status;
32 DEBUG(3,("Yielding connection to %s\n",name));
34 rec = connections_fetch_entry(talloc_tos(), conn, name);
35 if (rec == NULL) {
36 DEBUG(0, ("connections_fetch_entry failed\n"));
37 return False;
40 status = rec->delete_rec(rec);
41 if (!NT_STATUS_IS_OK(status)) {
42 DEBUG( NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND) ? 3 : 0,
43 ("deleting connection record returned %s\n",
44 nt_errstr(status)));
47 TALLOC_FREE(rec);
48 return NT_STATUS_IS_OK(status);
51 struct count_stat {
52 int curr_connections;
53 const char *name;
54 bool Clear;
57 /****************************************************************************
58 Count the entries belonging to a service in the connection db.
59 ****************************************************************************/
61 static int count_fn(struct db_record *rec,
62 const struct connections_key *ckey,
63 const struct connections_data *crec,
64 void *udp)
66 struct count_stat *cs = (struct count_stat *)udp;
68 if (crec->cnum == -1) {
69 return 0;
72 /* If the pid was not found delete the entry from connections.tdb */
74 if (cs->Clear && !process_exists(crec->pid) && (errno == ESRCH)) {
75 NTSTATUS status;
76 DEBUG(2,("pid %s doesn't exist - deleting connections %d [%s]\n",
77 procid_str_static(&crec->pid), crec->cnum,
78 crec->servicename));
80 status = rec->delete_rec(rec);
81 if (!NT_STATUS_IS_OK(status)) {
82 DEBUG(0,("count_fn: tdb_delete failed with error %s\n",
83 nt_errstr(status)));
85 return 0;
88 if (strequal(crec->servicename, cs->name))
89 cs->curr_connections++;
91 return 0;
94 /****************************************************************************
95 Claim an entry in the connections database.
96 ****************************************************************************/
98 int count_current_connections( const char *sharename, bool clear )
100 struct count_stat cs;
102 cs.curr_connections = 0;
103 cs.name = sharename;
104 cs.Clear = clear;
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"));
114 return False;
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)
126 struct db_record *rec;
127 struct connections_data crec;
128 TDB_DATA dbuf;
129 NTSTATUS status;
131 DEBUG(5,("claiming [%s]\n", name));
133 if (!(rec = connections_fetch_entry(talloc_tos(), conn, name))) {
134 DEBUG(0, ("connections_fetch_entry failed\n"));
135 return False;
138 /* fill in the crec */
139 ZERO_STRUCT(crec);
140 crec.magic = 0x280267;
141 crec.pid = sconn_server_id(conn->sconn);
142 crec.cnum = conn->cnum;
143 crec.uid = conn->server_info->utok.uid;
144 crec.gid = conn->server_info->utok.gid;
145 strlcpy(crec.servicename, lp_servicename(SNUM(conn)),
146 sizeof(crec.servicename));
147 crec.start = time(NULL);
149 strlcpy(crec.machine,get_remote_machine_name(),sizeof(crec.machine));
150 strlcpy(crec.addr, conn->sconn->client_id.addr, sizeof(crec.addr));
152 dbuf.dptr = (uint8 *)&crec;
153 dbuf.dsize = sizeof(crec);
155 status = rec->store(rec, dbuf, TDB_REPLACE);
157 TALLOC_FREE(rec);
159 if (!NT_STATUS_IS_OK(status)) {
160 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",
161 nt_errstr(status)));
162 return False;
165 return True;