2 Unix SMB/CIFS implementation.
3 Manage connections_struct structures
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Alexander Bokovoy 2002
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "smbd/globals.h"
24 /* The connections bitmap is expanded in increments of BITMAP_BLOCK_SZ. The
25 * maximum size of the bitmap is the largest positive integer, but you will hit
26 * the "max connections" limit, looong before that.
28 #define BITMAP_BLOCK_SZ 128
30 /****************************************************************************
31 init the conn structures
32 ****************************************************************************/
35 bmap = bitmap_allocate(BITMAP_BLOCK_SZ);
38 /****************************************************************************
39 return the number of open connections
40 ****************************************************************************/
41 int conn_num_open(void)
47 /****************************************************************************
48 check if a snum is in use
49 ****************************************************************************/
50 bool conn_snum_used(int snum)
52 connection_struct *conn;
53 for (conn=Connections;conn;conn=conn->next) {
54 if (conn->params->service == snum) {
61 /****************************************************************************
62 Find a conn given a cnum.
63 ****************************************************************************/
65 connection_struct *conn_find(unsigned cnum)
68 connection_struct *conn;
70 for (conn=Connections;conn;conn=conn->next,count++) {
71 if (conn->cnum == cnum) {
73 DLIST_PROMOTE(Connections, conn);
82 /****************************************************************************
83 find first available connection slot, starting from a random position.
84 The randomisation stops problems with the server dieing and clients
85 thinking the server is still available.
86 ****************************************************************************/
87 connection_struct *conn_new(void)
89 connection_struct *conn;
94 i = bitmap_find(bmap, find_offset);
97 /* Expand the connections bitmap. */
99 int newsz = bmap->n + BITMAP_BLOCK_SZ;
100 struct bitmap * nbmap;
102 if (newsz <= oldsz) {
104 DEBUG(0,("ERROR! Out of connection structures\n"));
108 DEBUG(4,("resizing connections bitmap from %d to %d\n",
111 nbmap = bitmap_allocate(newsz);
113 DEBUG(0,("ERROR! malloc fail.\n"));
117 bitmap_copy(nbmap, bmap);
121 find_offset = oldsz; /* Start next search in the new portion. */
126 /* The bitmap position is used below as the connection number
127 * conn->cnum). This ends up as the TID field in the SMB header,
128 * which is limited to 16 bits (we skip 0xffff which is the
132 DEBUG(0, ("Maximum connection limit reached\n"));
136 if (!(conn=TALLOC_ZERO_P(NULL, connection_struct)) ||
137 !(conn->params = TALLOC_P(conn, struct share_params))) {
138 DEBUG(0,("TALLOC_ZERO() failed!\n"));
148 string_set(&conn->dirpath,"");
149 string_set(&conn->connectpath,"");
150 string_set(&conn->origpath,"");
152 DLIST_ADD(Connections, conn);
157 /****************************************************************************
158 Close all conn structures.
159 return true if any were closed
160 ****************************************************************************/
161 bool conn_close_all(void)
163 connection_struct *conn, *next;
165 for (conn=Connections;conn;conn=next) {
167 set_current_service(conn, 0, True);
168 close_cnum(conn, conn->vuid);
174 /****************************************************************************
175 Idle inactive connections.
176 ****************************************************************************/
178 bool conn_idle_all(time_t t)
180 int deadtime = lp_deadtime()*60;
181 pipes_struct *plist = NULL;
182 connection_struct *conn;
185 deadtime = DEFAULT_SMBD_TIMEOUT;
187 for (conn=Connections;conn;conn=conn->next) {
189 time_t age = t - conn->lastused;
191 /* Update if connection wasn't idle. */
192 if (conn->lastused != conn->lastused_count) {
194 conn->lastused_count = t;
197 /* close dirptrs on connections that are idle */
198 if (age > DPTR_IDLE_TIMEOUT) {
202 if (conn->num_files_open > 0 || age < deadtime) {
208 * Check all pipes for any open handles. We cannot
209 * idle with a handle open.
212 for (plist = get_first_internal_pipe(); plist;
213 plist = get_next_internal_pipe(plist)) {
214 if (plist->pipe_handles && plist->pipe_handles->count) {
222 /****************************************************************************
223 Clear a vuid out of the validity cache, and as the 'owner' of a connection.
224 ****************************************************************************/
226 void conn_clear_vuid_caches(uint16_t vuid)
228 connection_struct *conn;
230 for (conn=Connections;conn;conn=conn->next) {
231 if (conn->vuid == vuid) {
232 conn->vuid = UID_FIELD_INVALID;
234 conn_clear_vuid_cache(conn, vuid);
238 /****************************************************************************
239 Free a conn structure - internal part.
240 ****************************************************************************/
242 void conn_free_internal(connection_struct *conn)
244 vfs_handle_struct *handle = NULL, *thandle = NULL;
245 struct trans_state *state = NULL;
247 /* Free vfs_connection_struct */
248 handle = conn->vfs_handles;
250 thandle = handle->next;
251 DLIST_REMOVE(conn->vfs_handles, handle);
252 if (handle->free_data)
253 handle->free_data(&handle->data);
257 /* Free any pending transactions stored on this conn. */
258 for (state = conn->pending_trans; state; state = state->next) {
259 /* state->setup is a talloc child of state. */
260 SAFE_FREE(state->param);
261 SAFE_FREE(state->data);
264 free_namearray(conn->veto_list);
265 free_namearray(conn->hide_list);
266 free_namearray(conn->veto_oplock_list);
267 free_namearray(conn->aio_write_behind_list);
269 string_free(&conn->dirpath);
270 string_free(&conn->connectpath);
271 string_free(&conn->origpath);
274 talloc_destroy(conn);
277 /****************************************************************************
278 Free a conn structure.
279 ****************************************************************************/
281 void conn_free(connection_struct *conn)
283 DLIST_REMOVE(Connections, conn);
285 bitmap_clear(bmap, conn->cnum);
287 SMB_ASSERT(num_open > 0);
290 conn_free_internal(conn);
293 /****************************************************************************
294 receive a smbcontrol message to forcibly unmount a share
295 the message contains just a share name and all instances of that
297 the special sharename '*' forces unmount of all shares
298 ****************************************************************************/
299 void msg_force_tdis(struct messaging_context *msg,
302 struct server_id server_id,
305 connection_struct *conn, *next;
308 fstrcpy(sharename, (const char *)data->data);
310 if (strcmp(sharename, "*") == 0) {
311 DEBUG(1,("Forcing close of all shares\n"));
316 for (conn=Connections;conn;conn=next) {
318 if (strequal(lp_servicename(SNUM(conn)), sharename)) {
319 DEBUG(1,("Forcing close of share %s cnum=%d\n",
320 sharename, conn->cnum));
321 close_cnum(conn, (uint16)-1);