2 Unix SMB/CIFS implementation.
3 Manage connections_struct structures
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /* set these to define the limits of the server. NOTE These are on a
24 per-client basis. Thus any one machine can't connect to more than
25 MAX_CONNECTIONS services, but any number of machines may connect at
27 #define MAX_CONNECTIONS 128
29 static connection_struct *Connections;
31 /* number of open connections */
32 static struct bitmap *bmap;
35 /****************************************************************************
36 init the conn structures
37 ****************************************************************************/
40 bmap = bitmap_allocate(MAX_CONNECTIONS);
43 /****************************************************************************
44 return the number of open connections
45 ****************************************************************************/
46 int conn_num_open(void)
52 /****************************************************************************
53 check if a snum is in use
54 ****************************************************************************/
55 BOOL conn_snum_used(int snum)
57 connection_struct *conn;
58 for (conn=Connections;conn;conn=conn->next) {
59 if (conn->service == snum) {
67 /****************************************************************************
68 find a conn given a cnum
69 ****************************************************************************/
70 connection_struct *conn_find(int cnum)
73 connection_struct *conn;
75 for (conn=Connections;conn;conn=conn->next,count++) {
76 if (conn->cnum == cnum) {
78 DLIST_PROMOTE(Connections, conn);
88 /****************************************************************************
89 find first available connection slot, starting from a random position.
90 The randomisation stops problems with the server dieing and clients
91 thinking the server is still available.
92 ****************************************************************************/
93 connection_struct *conn_new(void)
95 connection_struct *conn;
98 i = bitmap_find(bmap, 1);
101 DEBUG(1,("ERROR! Out of connection structures\n"));
105 conn = (connection_struct *)malloc(sizeof(*conn));
106 if (!conn) return NULL;
115 string_set(&conn->user,"");
116 string_set(&conn->dirpath,"");
117 string_set(&conn->connectpath,"");
118 string_set(&conn->origpath,"");
120 DLIST_ADD(Connections, conn);
125 /****************************************************************************
126 close all conn structures
127 ****************************************************************************/
128 void conn_close_all(void)
130 connection_struct *conn, *next;
131 for (conn=Connections;conn;conn=next) {
133 close_cnum(conn, (uint16)-1);
137 /****************************************************************************
138 idle inactive connections
139 ****************************************************************************/
140 BOOL conn_idle_all(time_t t, int deadtime)
143 connection_struct *conn, *next;
145 for (conn=Connections;conn;conn=next) {
147 /* close dirptrs on connections that are idle */
148 if ((t-conn->lastused) > DPTR_IDLE_TIMEOUT)
151 if (conn->num_files_open > 0 ||
152 (t-conn->lastused)<deadtime)
159 /****************************************************************************
160 Free a conn structure.
161 ****************************************************************************/
163 void conn_free(connection_struct *conn)
165 /* Free vfs_connection_struct */
168 if (conn->dl_handle != NULL) {
169 /* Close dlopen() handle */
170 dlclose(conn->dl_handle);
172 #endif /* HAVE_LIBDL */
174 DLIST_REMOVE(Connections, conn);
176 if (conn->ngroups && conn->groups) {
177 SAFE_FREE(conn->groups);
181 delete_nt_token(&conn->nt_user_token);
182 free_namearray(conn->veto_list);
183 free_namearray(conn->hide_list);
184 free_namearray(conn->veto_oplock_list);
186 string_free(&conn->user);
187 string_free(&conn->dirpath);
188 string_free(&conn->connectpath);
189 string_free(&conn->origpath);
191 bitmap_clear(bmap, conn->cnum);
199 /****************************************************************************
200 receive a smbcontrol message to forcibly unmount a share
201 the message contains just a share name and all instances of that
203 the special sharename '*' forces unmount of all shares
204 ****************************************************************************/
205 void msg_force_tdis(int msg_type, pid_t pid, void *buf, size_t len)
207 connection_struct *conn, *next;
210 fstrcpy(sharename, buf);
212 if (strcmp(sharename, "*") == 0) {
213 DEBUG(1,("Forcing close of all shares\n"));
218 for (conn=Connections;conn;conn=next) {
220 if (strequal(lp_servicename(conn->service), sharename)) {
221 DEBUG(1,("Forcing close of share %s cnum=%d\n",
222 sharename, conn->cnum));
223 close_cnum(conn, (uint16)-1);