1996-05-04 07:50:46 +00:00
/*
2002-01-30 06:08:46 +00:00
Unix SMB / CIFS implementation .
1996-05-04 07:50:46 +00:00
status reporting
1998-01-22 13:27:43 +00:00
Copyright ( C ) Andrew Tridgell 1994 - 1998
1996-05-04 07:50:46 +00:00
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 .
1996-08-15 15:11:34 +00:00
Revision History :
12 aug 96 : Erik . Devriendt @ te6 . siemens . be
added support for shared memory implementation of share mode locking
1998-07-24 01:08:31 +00:00
21 - Jul - 1998 : rsharpe @ ns . aus . com ( Richard Sharpe )
Added - L ( locks only ) - S ( shares only ) flags and code
1996-05-04 07:50:46 +00:00
*/
/*
* This program reports current SMB connections
*/
# include "includes.h"
2003-11-12 22:35:50 +00:00
# define SMB_MAXPIDS 2048
2004-12-17 08:51:23 +00:00
static uid_t Ucrit_uid = 0 ; /* added by OH */
2007-05-07 15:31:12 +00:00
static struct server_id Ucrit_pid [ SMB_MAXPIDS ] ; /* Ugly !!! */ /* added by OH */
2003-11-12 22:35:50 +00:00
static int Ucrit_MaxPid = 0 ; /* added by OH */
static unsigned int Ucrit_IsActive = 0 ; /* added by OH */
1999-12-21 04:54:30 +00:00
static int verbose , brief ;
static int shares_only = 0 ; /* Added by RJS */
static int locks_only = 0 ; /* Added by RJS */
static BOOL processes_only = False ;
2000-01-16 11:14:44 +00:00
static int show_brl ;
2004-12-17 08:51:23 +00:00
static BOOL numeric_only = False ;
1998-07-24 01:08:31 +00:00
2004-11-11 23:30:32 +00:00
const char * username = NULL ;
2006-07-11 18:01:26 +00:00
extern BOOL status_profile_dump ( BOOL be_verbose ) ;
extern BOOL status_profile_rates ( BOOL be_verbose ) ;
1998-08-10 01:25:32 +00:00
/* added by OH */
2004-12-17 08:51:23 +00:00
static void Ucrit_addUid ( uid_t uid )
1998-08-10 01:25:32 +00:00
{
2004-12-17 08:51:23 +00:00
Ucrit_uid = uid ;
Ucrit_IsActive = 1 ;
1998-08-10 01:25:32 +00:00
}
2004-12-17 08:51:23 +00:00
static unsigned int Ucrit_checkUid ( uid_t uid )
1998-08-10 01:25:32 +00:00
{
2003-11-12 22:35:50 +00:00
if ( ! Ucrit_IsActive )
return 1 ;
2004-12-17 08:51:23 +00:00
if ( uid = = Ucrit_uid )
2003-11-12 22:35:50 +00:00
return 1 ;
1998-08-10 01:25:32 +00:00
return 0 ;
}
2007-05-07 15:31:12 +00:00
static unsigned int Ucrit_checkPid ( struct server_id pid )
1998-08-10 01:25:32 +00:00
{
int i ;
2003-11-12 22:35:50 +00:00
if ( ! Ucrit_IsActive )
return 1 ;
for ( i = 0 ; i < Ucrit_MaxPid ; i + + ) {
2007-05-07 15:31:12 +00:00
if ( cluster_id_equal ( & pid , & Ucrit_pid [ i ] ) )
2003-11-12 22:35:50 +00:00
return 1 ;
}
1998-08-10 01:25:32 +00:00
return 0 ;
}
2007-05-07 15:31:12 +00:00
static BOOL Ucrit_addPid ( struct server_id pid )
2003-11-12 22:35:50 +00:00
{
if ( ! Ucrit_IsActive )
return True ;
if ( Ucrit_MaxPid > = SMB_MAXPIDS ) {
d_printf ( " ERROR: More than %d pids for user %s! \n " ,
2004-12-17 08:51:23 +00:00
SMB_MAXPIDS , uidtoname ( Ucrit_uid ) ) ;
2003-11-12 22:35:50 +00:00
return False ;
}
Ucrit_pid [ Ucrit_MaxPid + + ] = pid ;
return True ;
}
1998-08-10 01:25:32 +00:00
2006-07-21 14:13:30 +00:00
static void print_share_mode ( const struct share_mode_entry * e ,
const char * sharepath ,
const char * fname ,
void * dummy )
1997-10-20 08:46:00 +00:00
{
static int count ;
2006-01-31 21:54:24 +00:00
if ( ! is_valid_share_mode_entry ( e ) ) {
return ;
}
1997-10-20 08:46:00 +00:00
if ( count = = 0 ) {
2001-09-08 02:59:23 +00:00
d_printf ( " Locked files: \n " ) ;
2006-06-21 02:31:12 +00:00
d_printf ( " Pid Uid DenyMode Access R/W Oplock SharePath Name Time \n " ) ;
d_printf ( " -------------------------------------------------------------------------------------------------- \n " ) ;
1997-10-20 08:46:00 +00:00
}
count + + ;
2007-05-07 15:31:12 +00:00
if ( Ucrit_checkPid ( e - > pid ) ) {
2006-01-23 14:26:48 +00:00
d_printf ( " %-11s " , procid_str_static ( & e - > pid ) ) ;
2006-06-21 02:31:12 +00:00
d_printf ( " %-9u " , ( unsigned int ) e - > uid ) ;
2005-07-08 04:51:27 +00:00
switch ( map_share_mode_to_deny_mode ( e - > share_access ,
e - > private_options ) ) {
case DENY_NONE : d_printf ( " DENY_NONE " ) ; break ;
case DENY_ALL : d_printf ( " DENY_ALL " ) ; break ;
case DENY_DOS : d_printf ( " DENY_DOS " ) ; break ;
case DENY_READ : d_printf ( " DENY_READ " ) ; break ;
case DENY_WRITE : printf ( " DENY_WRITE " ) ; break ;
case DENY_FCB : d_printf ( " DENY_FCB " ) ; break ;
default : {
d_printf ( " unknown-please report ! "
" e->share_access = 0x%x, "
" e->private_options = 0x%x \n " ,
( unsigned int ) e - > share_access ,
( unsigned int ) e - > private_options ) ;
break ;
}
}
d_printf ( " 0x%-8x " , ( unsigned int ) e - > access_mask ) ;
2005-09-13 20:13:33 +00:00
if ( ( e - > access_mask & ( FILE_READ_DATA | FILE_WRITE_DATA ) ) = =
( FILE_READ_DATA | FILE_WRITE_DATA ) ) {
2005-07-08 04:51:27 +00:00
d_printf ( " RDWR " ) ;
} else if ( e - > access_mask & FILE_WRITE_DATA ) {
d_printf ( " WRONLY " ) ;
} else {
d_printf ( " RDONLY " ) ;
}
if ( ( e - > op_type & ( EXCLUSIVE_OPLOCK | BATCH_OPLOCK ) ) = =
( EXCLUSIVE_OPLOCK | BATCH_OPLOCK ) ) {
d_printf ( " EXCLUSIVE+BATCH " ) ;
} else if ( e - > op_type & EXCLUSIVE_OPLOCK ) {
d_printf ( " EXCLUSIVE " ) ;
} else if ( e - > op_type & BATCH_OPLOCK ) {
d_printf ( " BATCH " ) ;
} else if ( e - > op_type & LEVEL_II_OPLOCK ) {
d_printf ( " LEVEL_II " ) ;
} else {
d_printf ( " NONE " ) ;
}
2007-03-05 23:40:03 +00:00
d_printf ( " %s %s %s " , sharepath , fname , time_to_asc ( ( time_t ) e - > time . tv_sec ) ) ;
1998-05-30 02:25:11 +00:00
}
1997-10-20 08:46:00 +00:00
}
1997-05-20 00:32:51 +00:00
2006-04-10 15:33:04 +00:00
static void print_brl ( SMB_DEV_T dev ,
SMB_INO_T ino ,
2007-05-07 09:35:35 +00:00
struct server_id pid ,
2006-04-10 15:33:04 +00:00
enum brl_type lock_type ,
enum brl_flavour lock_flav ,
br_off start ,
br_off size )
2000-01-16 11:14:44 +00:00
{
static int count ;
if ( count = = 0 ) {
2001-09-08 02:59:23 +00:00
d_printf ( " Byte range locks: \n " ) ;
d_printf ( " Pid dev:inode R/W start size \n " ) ;
d_printf ( " ------------------------------------------------ \n " ) ;
2000-01-16 11:14:44 +00:00
}
count + + ;
2006-01-23 21:57:58 +00:00
d_printf ( " %8s %05x:%05x %s %9.0f %9.0f \n " ,
2005-09-30 17:13:37 +00:00
procid_str_static ( & pid ) , ( int ) dev , ( int ) ino ,
2000-01-16 11:14:44 +00:00
lock_type = = READ_LOCK ? " R " : " W " ,
( double ) start , ( double ) size ) ;
}
2007-05-08 13:44:36 +00:00
static int traverse_fn1 ( TDB_CONTEXT * tdb ,
const struct connections_key * key ,
const struct connections_data * crec ,
void * state )
1999-12-21 04:54:30 +00:00
{
2007-05-08 13:44:36 +00:00
if ( crec - > cnum = = - 1 )
2001-05-15 18:12:02 +00:00
return 0 ;
1999-12-21 04:54:30 +00:00
2007-05-08 13:44:36 +00:00
if ( ! process_exists ( crec - > pid ) | | ! Ucrit_checkUid ( crec - > uid ) ) {
1999-12-21 04:54:30 +00:00
return 0 ;
}
2005-09-30 17:13:37 +00:00
d_printf ( " %-10s %s %-12s %s " ,
2007-05-08 13:44:36 +00:00
crec - > servicename , procid_str_static ( & crec - > pid ) ,
crec - > machine ,
time_to_asc ( crec - > start ) ) ;
2001-08-22 19:11:55 +00:00
return 0 ;
}
static int traverse_sessionid ( TDB_CONTEXT * tdb , TDB_DATA kbuf , TDB_DATA dbuf , void * state )
{
struct sessionid sessionid ;
2004-12-17 08:51:23 +00:00
fstring uid_str , gid_str ;
2001-08-22 19:11:55 +00:00
if ( dbuf . dsize ! = sizeof ( sessionid ) )
return 0 ;
memcpy ( & sessionid , dbuf . dptr , sizeof ( sessionid ) ) ;
2007-05-07 15:31:12 +00:00
if ( ! process_exists ( sessionid . pid ) | | ! Ucrit_checkUid ( sessionid . uid ) ) {
2001-08-22 19:11:55 +00:00
return 0 ;
1999-12-21 04:54:30 +00:00
}
2003-11-12 22:35:50 +00:00
Ucrit_addPid ( sessionid . pid ) ;
2004-12-17 08:51:23 +00:00
fstr_sprintf ( uid_str , " %d " , sessionid . uid ) ;
fstr_sprintf ( gid_str , " %d " , sessionid . gid ) ;
2007-05-07 15:31:12 +00:00
d_printf ( " %s %-12s %-12s %-12s (%s) \n " ,
procid_str_static ( & sessionid . pid ) ,
2004-12-17 08:51:23 +00:00
numeric_only ? uid_str : uidtoname ( sessionid . uid ) ,
numeric_only ? gid_str : gidtoname ( sessionid . gid ) ,
sessionid . remote_machine , sessionid . hostname ) ;
2001-08-22 19:11:55 +00:00
1999-12-21 04:54:30 +00:00
return 0 ;
}
1997-05-20 00:32:51 +00:00
1996-06-04 06:42:03 +00:00
int main ( int argc , char * argv [ ] )
1996-05-04 07:50:46 +00:00
{
1999-12-21 04:54:30 +00:00
int c ;
2006-07-11 18:01:26 +00:00
int profile_only = 0 ;
1999-12-21 04:54:30 +00:00
TDB_CONTEXT * tdb ;
2004-11-11 23:30:32 +00:00
BOOL show_processes , show_locks , show_shares ;
2002-03-11 21:37:01 +00:00
poptContext pc ;
struct poptOption long_options [ ] = {
2002-10-28 19:50:06 +00:00
POPT_AUTOHELP
{ " processes " , ' p ' , POPT_ARG_NONE , & processes_only , ' p ' , " Show processes only " } ,
{ " verbose " , ' v ' , POPT_ARG_NONE , & verbose , ' v ' , " Be verbose " } ,
{ " locks " , ' L ' , POPT_ARG_NONE , & locks_only , ' L ' , " Show locks only " } ,
{ " shares " , ' S ' , POPT_ARG_NONE , & shares_only , ' S ' , " Show shares only " } ,
2004-11-11 23:30:32 +00:00
{ " user " , ' u ' , POPT_ARG_STRING , & username , ' u ' , " Switch to user " } ,
2002-10-28 19:50:06 +00:00
{ " brief " , ' b ' , POPT_ARG_NONE , & brief , ' b ' , " Be brief " } ,
2006-07-11 18:01:26 +00:00
{ " profile " , ' P ' , POPT_ARG_NONE , NULL , ' P ' , " Do profiling " } ,
{ " profile-rates " , ' R ' , POPT_ARG_NONE , NULL , ' R ' , " Show call rates " } ,
2002-10-28 19:50:06 +00:00
{ " byterange " , ' B ' , POPT_ARG_NONE , & show_brl , ' B ' , " Include byte range locks " } ,
2004-12-17 08:51:23 +00:00
{ " numeric " , ' n ' , POPT_ARG_NONE , & numeric_only , ' n ' , " Numeric uid/gid " } ,
2003-04-14 03:30:20 +00:00
POPT_COMMON_SAMBA
POPT_TABLEEND
2002-03-11 21:37:01 +00:00
} ;
2006-05-10 22:33:10 +00:00
sec_init ( ) ;
2005-12-28 22:48:54 +00:00
load_case_tables ( ) ;
1999-12-21 04:54:30 +00:00
setup_logging ( argv [ 0 ] , True ) ;
2001-09-10 11:08:57 +00:00
dbf = x_stderr ;
1999-12-21 04:54:30 +00:00
if ( getuid ( ) ! = geteuid ( ) ) {
2001-09-08 02:59:23 +00:00
d_printf ( " smbstatus should not be run setuid \n " ) ;
1999-12-21 04:54:30 +00:00
return ( 1 ) ;
1998-07-24 01:08:31 +00:00
}
2002-03-11 21:37:01 +00:00
pc = poptGetContext ( NULL , argc , ( const char * * ) argv , long_options ,
POPT_CONTEXT_KEEP_FIRST ) ;
1999-12-21 04:54:30 +00:00
2003-04-16 14:31:10 +00:00
while ( ( c = poptGetNextOpt ( pc ) ) ! = - 1 ) {
1999-12-21 04:54:30 +00:00
switch ( c ) {
2000-01-02 23:00:27 +00:00
case ' u ' :
2004-12-17 08:51:23 +00:00
Ucrit_addUid ( nametouid ( poptGetOptArg ( pc ) ) ) ;
1999-12-21 04:54:30 +00:00
break ;
2006-07-11 18:01:26 +00:00
case ' P ' :
case ' R ' :
profile_only = c ;
1999-12-13 13:27:58 +00:00
}
1999-12-21 04:54:30 +00:00
}
2001-06-13 05:36:28 +00:00
2004-11-11 23:30:32 +00:00
/* setup the flags based on the possible combincations */
show_processes = ! ( shares_only | | locks_only | | profile_only ) | | processes_only ;
show_locks = ! ( shares_only | | processes_only | | profile_only ) | | locks_only ;
show_shares = ! ( processes_only | | locks_only | | profile_only ) | | shares_only ;
if ( username )
2004-12-17 08:51:23 +00:00
Ucrit_addUid ( nametouid ( username ) ) ;
2004-11-11 23:30:32 +00:00
1999-12-21 04:54:30 +00:00
if ( verbose ) {
2001-11-19 02:49:53 +00:00
d_printf ( " using configfile = %s \n " , dyn_CONFIGFILE ) ;
1999-12-21 04:54:30 +00:00
}
2002-10-28 19:50:06 +00:00
2006-01-28 22:53:04 +00:00
if ( ! lp_load ( dyn_CONFIGFILE , False , False , False , True ) ) {
2002-10-28 19:50:06 +00:00
fprintf ( stderr , " Can't load %s - run testparm to debug it \n " , dyn_CONFIGFILE ) ;
return ( - 1 ) ;
}
2006-07-11 18:01:26 +00:00
switch ( profile_only ) {
case ' P ' :
/* Dump profile data */
return status_profile_dump ( verbose ) ;
case ' R ' :
/* Continuously display rate-converted data */
return status_profile_rates ( verbose ) ;
default :
break ;
1999-12-21 04:54:30 +00:00
}
2006-07-11 18:01:26 +00:00
2004-11-11 23:30:32 +00:00
if ( show_processes ) {
tdb = tdb_open_log ( lock_path ( " sessionid.tdb " ) , 0 , TDB_DEFAULT , O_RDONLY , 0 ) ;
if ( ! tdb ) {
d_printf ( " sessionid.tdb not initialised \n " ) ;
} else {
d_printf ( " \n Samba version %s \n " , SAMBA_VERSION_STRING ) ;
d_printf ( " PID Username Group Machine \n " ) ;
d_printf ( " ------------------------------------------------------------------- \n " ) ;
tdb_traverse ( tdb , traverse_sessionid , NULL ) ;
tdb_close ( tdb ) ;
}
if ( processes_only )
exit ( 0 ) ;
2001-09-10 22:31:59 +00:00
}
2001-08-22 19:11:55 +00:00
2004-11-11 23:30:32 +00:00
if ( show_shares ) {
2007-05-08 13:44:36 +00:00
if ( verbose ) {
d_printf ( " Opened %s \n " , lock_path ( " connections.tdb " ) ) ;
}
2004-11-11 23:30:32 +00:00
2007-05-08 13:44:36 +00:00
if ( brief )
exit ( 0 ) ;
2004-11-11 23:30:32 +00:00
2007-05-08 13:44:36 +00:00
d_printf ( " \n Service pid machine Connected at \n " ) ;
d_printf ( " ------------------------------------------------------- \n " ) ;
2004-11-11 23:30:32 +00:00
2007-05-08 13:44:36 +00:00
connections_forall ( traverse_fn1 , NULL ) ;
2004-11-11 23:30:32 +00:00
2007-05-08 13:44:36 +00:00
d_printf ( " \n " ) ;
1999-12-21 04:54:30 +00:00
2004-11-11 23:30:32 +00:00
if ( shares_only )
2001-09-10 22:31:59 +00:00
exit ( 0 ) ;
}
1999-12-21 04:54:30 +00:00
2004-11-11 23:30:32 +00:00
if ( show_locks ) {
1999-12-22 01:36:27 +00:00
int ret ;
1999-12-21 04:54:30 +00:00
if ( ! locking_init ( 1 ) ) {
2001-09-08 02:59:23 +00:00
d_printf ( " Can't initialise locking module - exiting \n " ) ;
1999-12-21 04:54:30 +00:00
exit ( 1 ) ;
}
2006-07-21 14:13:30 +00:00
ret = share_mode_forall ( print_share_mode , NULL ) ;
1999-12-22 01:36:27 +00:00
if ( ret = = 0 ) {
2001-09-08 02:59:23 +00:00
d_printf ( " No locked files \n " ) ;
1999-12-22 01:36:27 +00:00
} else if ( ret = = - 1 ) {
2001-09-08 02:59:23 +00:00
d_printf ( " locked file list truncated \n " ) ;
1999-12-22 01:36:27 +00:00
}
1999-12-21 04:54:30 +00:00
2001-09-08 02:59:23 +00:00
d_printf ( " \n " ) ;
2000-01-16 11:14:44 +00:00
if ( show_brl ) {
brl_forall ( print_brl ) ;
}
1999-12-21 04:54:30 +00:00
locking_end ( ) ;
}
1997-05-20 00:32:51 +00:00
1999-12-21 04:54:30 +00:00
return ( 0 ) ;
1996-05-04 07:50:46 +00:00
}