1998-08-17 06:13:32 +00:00
/*
Unix SMB / Netbios implementation .
Version 1.9 .
Manage connections_struct structures
Copyright ( C ) Andrew Tridgell 1998
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation ; either version 2 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program ; if not , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
# include "includes.h"
/* set these to define the limits of the server. NOTE These are on a
per - client basis . Thus any one machine can ' t connect to more than
MAX_CONNECTIONS services , but any number of machines may connect at
one time . */
# define MAX_CONNECTIONS 128
static connection_struct * Connections ;
/* number of open connections */
static struct bitmap * bmap ;
static int num_open ;
/****************************************************************************
init the conn structures
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void conn_init ( void )
{
bmap = bitmap_allocate ( MAX_CONNECTIONS ) ;
}
/****************************************************************************
return the number of open connections
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int conn_num_open ( void )
{
return num_open ;
}
/****************************************************************************
check if a snum is in use
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
BOOL conn_snum_used ( int snum )
{
connection_struct * conn ;
for ( conn = Connections ; conn ; conn = conn - > next ) {
if ( conn - > service = = snum ) {
return ( True ) ;
}
}
return ( False ) ;
}
/****************************************************************************
find a conn given a cnum
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
connection_struct * conn_find ( int cnum )
{
1998-08-17 06:47:53 +00:00
int count = 0 ;
1998-08-17 06:13:32 +00:00
connection_struct * conn ;
1998-08-17 06:47:53 +00:00
for ( conn = Connections ; conn ; conn = conn - > next , count + + ) {
if ( conn - > cnum = = cnum ) {
if ( count > 10 ) {
DLIST_PROMOTE ( Connections , conn ) ;
}
return conn ;
}
1998-08-17 06:13:32 +00:00
}
return NULL ;
}
/****************************************************************************
find first available connection slot , starting from a random position .
The randomisation stops problems with the server dieing and clients
thinking the server is still available .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
connection_struct * conn_new ( void )
{
connection_struct * conn ;
int i ;
i = bitmap_find ( bmap , 1 ) ;
if ( i = = - 1 ) {
DEBUG ( 1 , ( " ERROR! Out of connection structures \n " ) ) ;
return NULL ;
}
conn = ( connection_struct * ) malloc ( sizeof ( * conn ) ) ;
if ( ! conn ) return NULL ;
1998-09-05 13:24:20 +00:00
ZERO_STRUCTP ( conn ) ;
1998-08-17 06:13:32 +00:00
conn - > cnum = i ;
bitmap_set ( bmap , i ) ;
num_open + + ;
2000-01-16 11:18:04 +00:00
string_set ( & conn - > user , " " ) ;
string_set ( & conn - > dirpath , " " ) ;
string_set ( & conn - > connectpath , " " ) ;
string_set ( & conn - > origpath , " " ) ;
1998-08-17 06:13:32 +00:00
1998-08-17 06:47:53 +00:00
DLIST_ADD ( Connections , conn ) ;
1998-08-17 06:13:32 +00:00
return conn ;
}
/****************************************************************************
close all conn structures
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void conn_close_all ( void )
{
connection_struct * conn , * next ;
for ( conn = Connections ; conn ; conn = next ) {
next = conn - > next ;
close_cnum ( conn , ( uint16 ) - 1 ) ;
}
}
/****************************************************************************
idle inactive connections
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
BOOL conn_idle_all ( time_t t , int deadtime )
{
BOOL allidle = True ;
connection_struct * conn , * next ;
for ( conn = Connections ; conn ; conn = next ) {
next = conn - > next ;
/* close dirptrs on connections that are idle */
if ( ( t - conn - > lastused ) > DPTR_IDLE_TIMEOUT )
dptr_idlecnum ( conn ) ;
if ( conn - > num_files_open > 0 | |
( t - conn - > lastused ) < deadtime )
allidle = False ;
}
return allidle ;
}
/****************************************************************************
2000-08-03 22:38:43 +00:00
Free a conn structure .
1998-08-17 06:13:32 +00:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-08-03 22:38:43 +00:00
1998-08-17 06:13:32 +00:00
void conn_free ( connection_struct * conn )
{
2000-04-28 21:09:26 +00:00
/* Free vfs_connection_struct */
2000-06-01 17:01:34 +00:00
# ifdef HAVE_LIBDL
2000-10-06 03:21:49 +00:00
if ( conn - > dl_handle ! = NULL ) {
2000-05-06 15:00:49 +00:00
/* Close dlopen() handle */
2000-10-06 03:21:49 +00:00
dlclose ( conn - > dl_handle ) ;
2000-04-28 21:09:26 +00:00
}
2000-10-06 03:21:49 +00:00
# endif /* HAVE_LIBDL */
2000-04-28 21:09:26 +00:00
1998-08-17 06:47:53 +00:00
DLIST_REMOVE ( Connections , conn ) ;
1998-08-17 06:13:32 +00:00
if ( conn - > ngroups & & conn - > groups ) {
2001-09-17 11:25:41 +00:00
SAFE_FREE ( conn - > groups ) ;
1998-08-17 06:13:32 +00:00
conn - > ngroups = 0 ;
}
2000-08-03 22:38:43 +00:00
delete_nt_token ( & conn - > nt_user_token ) ;
1998-08-17 06:13:32 +00:00
free_namearray ( conn - > veto_list ) ;
free_namearray ( conn - > hide_list ) ;
free_namearray ( conn - > veto_oplock_list ) ;
string_free ( & conn - > user ) ;
string_free ( & conn - > dirpath ) ;
string_free ( & conn - > connectpath ) ;
string_free ( & conn - > origpath ) ;
bitmap_clear ( bmap , conn - > cnum ) ;
num_open - - ;
1998-09-05 13:24:20 +00:00
ZERO_STRUCTP ( conn ) ;
2001-09-17 11:25:41 +00:00
SAFE_FREE ( conn ) ;
1998-08-17 06:13:32 +00:00
}
2001-06-20 03:05:09 +00:00
/****************************************************************************
receive a smbcontrol message to forcibly unmount a share
the message contains just a share name and all instances of that
share are unmounted
the special sharename ' * ' forces unmount of all shares
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void msg_force_tdis ( int msg_type , pid_t pid , void * buf , size_t len )
{
connection_struct * conn , * next ;
fstring sharename ;
fstrcpy ( sharename , buf ) ;
if ( strcmp ( sharename , " * " ) = = 0 ) {
DEBUG ( 1 , ( " Forcing close of all shares \n " ) ) ;
conn_close_all ( ) ;
return ;
}
for ( conn = Connections ; conn ; conn = next ) {
next = conn - > next ;
if ( strequal ( lp_servicename ( conn - > service ) , sharename ) ) {
DEBUG ( 1 , ( " Forcing close of share %s cnum=%d \n " ,
sharename , conn - > cnum ) ) ;
close_cnum ( conn , ( uint16 ) - 1 ) ;
}
}
}