2003-12-01 16:58:43 +03:00
/*
Samba Unix / Linux SMB client library
net status command - - possible replacement for smbstatus
Copyright ( C ) 2003 Volker Lendecke ( vl @ samba . org )
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
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
2003-12-01 16:58:43 +03:00
( 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
2007-07-10 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>. */
2003-12-01 16:58:43 +03:00
# include "includes.h"
2004-10-07 08:01:18 +04:00
# include "utils/net.h"
2003-12-01 16:58:43 +03:00
static int show_session ( TDB_CONTEXT * tdb , TDB_DATA kbuf , TDB_DATA dbuf ,
void * state )
{
2007-10-19 04:40:25 +04:00
bool * parseable = ( bool * ) state ;
2003-12-01 16:58:43 +03:00
struct sessionid sessionid ;
if ( dbuf . dsize ! = sizeof ( sessionid ) )
return 0 ;
memcpy ( & sessionid , dbuf . dptr , sizeof ( sessionid ) ) ;
2007-05-07 19:31:12 +04:00
if ( ! process_exists ( sessionid . pid ) ) {
2003-12-01 16:58:43 +03:00
return 0 ;
}
if ( * parseable ) {
2007-05-07 19:31:12 +04:00
d_printf ( " %s \\ %s \\ %s \\ %s \\ %s \n " ,
procid_str_static ( & sessionid . pid ) , uidtoname ( sessionid . uid ) ,
2003-12-01 16:58:43 +03:00
gidtoname ( sessionid . gid ) ,
sessionid . remote_machine , sessionid . hostname ) ;
} else {
2007-05-07 19:31:12 +04:00
d_printf ( " %7s %-12s %-12s %-12s (%s) \n " ,
procid_str_static ( & sessionid . pid ) , uidtoname ( sessionid . uid ) ,
2003-12-01 16:58:43 +03:00
gidtoname ( sessionid . gid ) ,
sessionid . remote_machine , sessionid . hostname ) ;
}
return 0 ;
}
static int net_status_sessions ( int argc , const char * * argv )
{
TDB_CONTEXT * tdb ;
2007-10-19 04:40:25 +04:00
bool parseable ;
2003-12-01 16:58:43 +03:00
if ( argc = = 0 ) {
parseable = False ;
} else if ( ( argc = = 1 ) & & strequal ( argv [ 0 ] , " parseable " ) ) {
parseable = True ;
} else {
return net_help_status ( argc , argv ) ;
}
if ( ! parseable ) {
d_printf ( " PID Username Group Machine "
" \n " ) ;
d_printf ( " ------------------------------------------- "
" ------------------------ \n " ) ;
}
tdb = tdb_open_log ( lock_path ( " sessionid.tdb " ) , 0 ,
TDB_DEFAULT , O_RDONLY , 0 ) ;
if ( tdb = = NULL ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " %s not initialised \n " , lock_path ( " sessionid.tdb " ) ) ;
2003-12-01 16:58:43 +03:00
return - 1 ;
}
tdb_traverse ( tdb , show_session , & parseable ) ;
tdb_close ( tdb ) ;
return 0 ;
}
2007-05-28 15:38:42 +04:00
static int show_share ( struct db_record * rec ,
2007-05-08 17:44:36 +04:00
const struct connections_key * key ,
const struct connections_data * crec ,
2003-12-01 16:58:43 +03:00
void * state )
{
2007-05-08 17:44:36 +04:00
if ( crec - > cnum = = - 1 )
2003-12-01 16:58:43 +03:00
return 0 ;
2007-05-08 17:44:36 +04:00
if ( ! process_exists ( crec - > pid ) ) {
2003-12-01 16:58:43 +03:00
return 0 ;
}
2005-09-30 21:13:37 +04:00
d_printf ( " %-10.10s %s %-12s %s " ,
2007-05-08 17:44:36 +04:00
crec - > servicename , procid_str_static ( & crec - > pid ) ,
crec - > machine ,
time_to_asc ( crec - > start ) ) ;
2003-12-01 16:58:43 +03:00
return 0 ;
}
struct sessionids {
int num_entries ;
struct sessionid * entries ;
} ;
static int collect_pid ( TDB_CONTEXT * tdb , TDB_DATA kbuf , TDB_DATA dbuf ,
void * state )
{
struct sessionids * ids = ( struct sessionids * ) state ;
struct sessionid sessionid ;
if ( dbuf . dsize ! = sizeof ( sessionid ) )
return 0 ;
memcpy ( & sessionid , dbuf . dptr , sizeof ( sessionid ) ) ;
2007-05-07 19:31:12 +04:00
if ( ! process_exists ( sessionid . pid ) )
2003-12-01 16:58:43 +03:00
return 0 ;
ids - > num_entries + = 1 ;
2004-12-07 21:25:53 +03:00
ids - > entries = SMB_REALLOC_ARRAY ( ids - > entries , struct sessionid , ids - > num_entries ) ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if ( ! ids - > entries ) {
ids - > num_entries = 0 ;
return 0 ;
}
2003-12-01 16:58:43 +03:00
ids - > entries [ ids - > num_entries - 1 ] = sessionid ;
return 0 ;
}
2007-05-28 15:38:42 +04:00
static int show_share_parseable ( struct db_record * rec ,
2007-05-08 17:44:36 +04:00
const struct connections_key * key ,
const struct connections_data * crec ,
2003-12-01 16:58:43 +03:00
void * state )
{
struct sessionids * ids = ( struct sessionids * ) state ;
int i ;
2007-10-19 04:40:25 +04:00
bool guest = True ;
2003-12-01 16:58:43 +03:00
2007-05-08 17:44:36 +04:00
if ( crec - > cnum = = - 1 )
2003-12-01 16:58:43 +03:00
return 0 ;
2007-05-08 17:44:36 +04:00
if ( ! process_exists ( crec - > pid ) ) {
2003-12-01 16:58:43 +03:00
return 0 ;
}
for ( i = 0 ; i < ids - > num_entries ; i + + ) {
2007-05-07 19:31:12 +04:00
struct server_id id = ids - > entries [ i ] . pid ;
2007-05-08 17:44:36 +04:00
if ( procid_equal ( & id , & crec - > pid ) ) {
2003-12-01 16:58:43 +03:00
guest = False ;
break ;
}
}
2005-09-30 21:13:37 +04:00
d_printf ( " %s \\ %s \\ %s \\ %s \\ %s \\ %s \\ %s " ,
2007-05-08 17:44:36 +04:00
crec - > servicename , procid_str_static ( & crec - > pid ) ,
2003-12-01 16:58:43 +03:00
guest ? " " : uidtoname ( ids - > entries [ i ] . uid ) ,
guest ? " " : gidtoname ( ids - > entries [ i ] . gid ) ,
2007-05-08 17:44:36 +04:00
crec - > machine ,
2003-12-01 16:58:43 +03:00
guest ? " " : ids - > entries [ i ] . hostname ,
2007-05-08 17:44:36 +04:00
time_to_asc ( crec - > start ) ) ;
2003-12-01 16:58:43 +03:00
return 0 ;
}
static int net_status_shares_parseable ( int argc , const char * * argv )
{
struct sessionids ids ;
TDB_CONTEXT * tdb ;
ids . num_entries = 0 ;
ids . entries = NULL ;
tdb = tdb_open_log ( lock_path ( " sessionid.tdb " ) , 0 ,
TDB_DEFAULT , O_RDONLY , 0 ) ;
if ( tdb = = NULL ) {
2006-01-18 00:22:00 +03:00
d_fprintf ( stderr , " %s not initialised \n " , lock_path ( " sessionid.tdb " ) ) ;
2003-12-01 16:58:43 +03:00
return - 1 ;
}
tdb_traverse ( tdb , collect_pid , & ids ) ;
tdb_close ( tdb ) ;
2007-05-08 17:44:36 +04:00
connections_forall ( show_share_parseable , & ids ) ;
2003-12-01 16:58:43 +03:00
SAFE_FREE ( ids . entries ) ;
return 0 ;
}
static int net_status_shares ( int argc , const char * * argv )
{
if ( argc = = 0 ) {
d_printf ( " \n Service pid machine "
" Connected at \n " ) ;
d_printf ( " ------------------------------------- "
" ------------------ \n " ) ;
2007-05-08 17:44:36 +04:00
connections_forall ( show_share , NULL ) ;
2003-12-01 16:58:43 +03:00
return 0 ;
}
if ( ( argc ! = 1 ) | | ! strequal ( argv [ 0 ] , " parseable " ) ) {
return net_help_status ( argc , argv ) ;
}
return net_status_shares_parseable ( argc , argv ) ;
}
int net_status ( int argc , const char * * argv )
{
struct functable func [ ] = {
{ " sessions " , net_status_sessions } ,
{ " shares " , net_status_shares } ,
{ NULL , NULL }
} ;
return net_run_function ( argc , argv , func , net_help_status ) ;
}