2011-03-23 02:25:01 +03:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
1996-05-04 11:50:46 +04:00
Critical Fault handling
1998-01-22 16:27:43 +03:00
Copyright ( C ) Andrew Tridgell 1992 - 1998
2009-02-25 03:42:18 +03:00
Copyright ( C ) Tim Prouty 2009
2018-04-10 07:35:07 +03:00
Copyright ( C ) James Peach 2006
2011-03-23 02:25:01 +03:00
1996-05-04 11:50:46 +04: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
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
1996-05-04 11:50:46 +04:00
( at your option ) any later version .
2011-03-23 02:25:01 +03:00
1996-05-04 11:50:46 +04:00
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 .
2011-03-23 02:25:01 +03:00
1996-05-04 11:50:46 +04:00
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/>.
1996-05-04 11:50:46 +04:00
*/
2014-09-22 13:43:27 +04:00
# include "replace.h"
2011-03-22 08:17:39 +03:00
# include "system/filesys.h"
2014-09-22 13:43:27 +04:00
# include "system/wait.h"
2011-03-22 08:17:39 +03:00
# include "version.h"
1996-05-04 11:50:46 +04:00
2009-09-15 08:48:49 +04:00
# ifdef HAVE_SYS_SYSCTL_H
# include <sys/sysctl.h>
# endif
2006-04-04 04:27:50 +04:00
# ifdef HAVE_SYS_PRCTL_H
# include <sys/prctl.h>
# endif
2014-09-22 13:43:27 +04:00
# include "debug.h"
# include "lib/util/signal.h" /* Avoid /usr/include/signal.h */
# include "fault.h"
2011-03-22 08:17:39 +03:00
static struct {
bool disabled ;
smb_panic_handler_t panic_handler ;
} fault_state ;
/*******************************************************************
setup variables used for fault handling
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void fault_configure ( smb_panic_handler_t panic_handler )
{
fault_state . panic_handler = panic_handler ;
}
/**
disable setting up fault handlers
This is used for the bind9 dlz module , as we
don ' t want a Samba module in bind9 to override the bind
fault handling
* */
_PUBLIC_ void fault_setup_disable ( void )
{
fault_state . disabled = true ;
}
1996-05-04 11:50:46 +04:00
/*******************************************************************
report a fault
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void fault_report ( int sig )
{
1998-08-11 17:55:16 +04:00
static int counter ;
2020-02-26 01:58:48 +03:00
char signal_string [ 128 ] ;
1998-08-11 17:55:16 +04:00
if ( counter ) _exit ( 1 ) ;
counter + + ;
2020-02-26 01:58:48 +03:00
snprintf ( signal_string , sizeof ( signal_string ) ,
" Signal %d: %s " , sig , strsignal ( sig ) ) ;
smb_panic ( signal_string ) ;
1996-05-04 11:50:46 +04:00
2013-02-18 12:59:52 +04:00
/* smb_panic() never returns, so this is really redundant */
1998-08-11 17:55:16 +04:00
exit ( 1 ) ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
catch serious errors
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void sig_fault ( int sig )
{
1998-08-11 17:55:16 +04:00
fault_report ( sig ) ;
1996-05-04 11:50:46 +04:00
}
/*******************************************************************
setup our fault handlers
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2011-03-22 06:05:23 +03:00
void fault_setup ( void )
1996-05-04 11:50:46 +04:00
{
2011-03-22 08:17:39 +03:00
if ( fault_state . disabled ) {
return ;
}
2012-03-14 20:56:02 +04:00
# if !defined(HAVE_DISABLE_FAULT_HANDLING)
1996-05-04 11:50:46 +04:00
# ifdef SIGSEGV
2010-02-19 17:32:45 +03:00
CatchSignal ( SIGSEGV , sig_fault ) ;
1996-05-04 11:50:46 +04:00
# endif
# ifdef SIGBUS
2010-02-19 17:32:45 +03:00
CatchSignal ( SIGBUS , sig_fault ) ;
1996-05-04 11:50:46 +04:00
# endif
2004-01-22 04:50:58 +03:00
# ifdef SIGABRT
2010-02-19 17:32:45 +03:00
CatchSignal ( SIGABRT , sig_fault ) ;
2004-01-22 04:50:58 +03:00
# endif
2012-03-14 20:56:02 +04:00
# endif
1996-05-04 11:50:46 +04:00
}
2006-04-04 04:27:50 +04:00
2011-03-22 08:17:39 +03:00
_PUBLIC_ const char * panic_action = NULL ;
2009-02-25 01:45:46 +03:00
2011-03-22 08:17:39 +03:00
/*
default smb_panic ( ) implementation
*/
2014-02-26 10:37:13 +04:00
static void smb_panic_default ( const char * why ) _NORETURN_ ;
2011-03-22 08:17:39 +03:00
static void smb_panic_default ( const char * why )
2009-02-25 03:42:18 +03:00
{
2011-10-04 12:47:46 +04:00
# if defined(HAVE_PRCTL) && defined(PR_SET_PTRACER)
/*
* Make sure all children can attach a debugger .
*/
prctl ( PR_SET_PTRACER , getpid ( ) , 0 , 0 , 0 ) ;
# endif
2011-03-22 08:17:39 +03:00
if ( panic_action & & * panic_action ) {
char cmdstring [ 200 ] ;
2012-03-29 03:49:30 +04:00
if ( strlcpy ( cmdstring , panic_action , sizeof ( cmdstring ) ) < sizeof ( cmdstring ) ) {
int result ;
char pidstr [ 20 ] ;
2021-01-11 15:55:38 +03:00
char subst [ 200 ] ;
char * p = NULL ;
2012-03-29 03:49:30 +04:00
snprintf ( pidstr , sizeof ( pidstr ) , " %d " , ( int ) getpid ( ) ) ;
2021-01-11 15:55:38 +03:00
p = strstr ( cmdstring , " %d " ) ;
if ( p ! = NULL ) {
snprintf ( subst ,
sizeof ( subst ) ,
" %.*s%s%s " ,
( int ) ( p - cmdstring ) ,
cmdstring ,
pidstr ,
p + 2 ) ;
strlcpy ( cmdstring , subst , sizeof ( cmdstring ) ) ;
}
2012-03-29 03:49:30 +04:00
DEBUG ( 0 , ( " smb_panic(): calling panic action [%s] \n " , cmdstring ) ) ;
result = system ( cmdstring ) ;
if ( result = = - 1 )
DEBUG ( 0 , ( " smb_panic(): fork failed in panic action: %s \n " ,
strerror ( errno ) ) ) ;
else
DEBUG ( 0 , ( " smb_panic(): action returned status %d \n " ,
WEXITSTATUS ( result ) ) ) ;
}
2011-03-22 08:17:39 +03:00
}
2009-02-25 03:42:18 +03:00
2011-03-22 08:17:39 +03:00
# ifdef SIGABRT
CatchSignal ( SIGABRT , SIG_DFL ) ;
2009-02-25 03:42:18 +03:00
# endif
2011-03-22 08:17:39 +03:00
abort ( ) ;
2010-07-23 14:22:23 +04:00
}
2020-02-26 02:02:36 +03:00
_PUBLIC_ void smb_panic_log ( const char * why )
2006-04-04 04:27:50 +04:00
{
2020-02-26 01:58:48 +03:00
DEBUGSEP ( 0 ) ;
2020-02-26 02:02:36 +03:00
DEBUG ( 0 , ( " INTERNAL ERROR: %s in pid %lld (%s) \n " ,
why ,
( unsigned long long ) getpid ( ) ,
SAMBA_VERSION_STRING ) ) ;
DEBUG ( 0 , ( " If you are running a recent Samba version, and "
2020-02-26 01:58:48 +03:00
" if you think this problem is not yet fixed in the "
" latest versions, please consider reporting this "
" bug, see "
" https://wiki.samba.org/index.php/Bug_Reporting \n " ) ) ;
DEBUGSEP ( 0 ) ;
2020-02-26 02:02:36 +03:00
DEBUG ( 0 , ( " PANIC (pid %llu): %s in " SAMBA_VERSION_STRING " \n " ,
( unsigned long long ) getpid ( ) , why ) ) ;
log_stack_trace ( ) ;
}
/**
Something really nasty happened - panic !
* */
_PUBLIC_ void smb_panic ( const char * why )
{
smb_panic_log ( why ) ;
2020-02-26 01:58:48 +03:00
2011-03-22 08:17:39 +03:00
if ( fault_state . panic_handler ) {
fault_state . panic_handler ( why ) ;
_exit ( 1 ) ;
2006-04-04 04:27:50 +04:00
}
2011-03-22 08:17:39 +03:00
smb_panic_default ( why ) ;
2006-04-04 04:27:50 +04:00
}
2018-04-10 07:35:07 +03:00
/*******************************************************************
Print a backtrace of the stack to the debug log . This function
DELIBERATELY LEAKS MEMORY . The expectation is that you should
exit shortly after calling it .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Buffer size to use when printing backtraces */
# define BACKTRACE_STACK_SIZE 64
# ifdef HAVE_LIBUNWIND_H
# include <libunwind.h>
# endif
# ifdef HAVE_EXECINFO_H
# include <execinfo.h>
# endif
void log_stack_trace ( void )
{
# ifdef HAVE_LIBUNWIND
/* Try to use libunwind before any other technique since on ia64
* libunwind correctly walks the stack in more circumstances than
* backtrace .
*/
unw_cursor_t cursor ;
unw_context_t uc ;
unsigned i = 0 ;
char procname [ 256 ] ;
unw_word_t ip , sp , off ;
procname [ sizeof ( procname ) - 1 ] = ' \0 ' ;
if ( unw_getcontext ( & uc ) ! = 0 ) {
goto libunwind_failed ;
}
if ( unw_init_local ( & cursor , & uc ) ! = 0 ) {
goto libunwind_failed ;
}
DEBUG ( 0 , ( " BACKTRACE: \n " ) ) ;
do {
ip = sp = 0 ;
unw_get_reg ( & cursor , UNW_REG_IP , & ip ) ;
unw_get_reg ( & cursor , UNW_REG_SP , & sp ) ;
switch ( unw_get_proc_name ( & cursor ,
procname , sizeof ( procname ) - 1 , & off ) ) {
case 0 :
/* Name found. */
case - UNW_ENOMEM :
/* Name truncated. */
DEBUGADD ( 0 , ( " #%u %s + %#llx [ip=%#llx] [sp=%#llx] \n " ,
i , procname , ( long long ) off ,
( long long ) ip , ( long long ) sp ) ) ;
break ;
default :
/* case -UNW_ENOINFO: */
/* case -UNW_EUNSPEC: */
/* No symbol name found. */
DEBUGADD ( 0 , ( " #%u %s [ip=%#llx] [sp=%#llx] \n " ,
i , " <unknown symbol> " ,
( long long ) ip , ( long long ) sp ) ) ;
}
+ + i ;
} while ( unw_step ( & cursor ) > 0 ) ;
return ;
libunwind_failed :
DEBUG ( 0 , ( " unable to produce a stack trace with libunwind \n " ) ) ;
2018-12-13 20:48:03 +03:00
# elif defined(HAVE_BACKTRACE_SYMBOLS)
2018-04-10 07:35:07 +03:00
void * backtrace_stack [ BACKTRACE_STACK_SIZE ] ;
size_t backtrace_size ;
char * * backtrace_strings ;
/* get the backtrace (stack frames) */
backtrace_size = backtrace ( backtrace_stack , BACKTRACE_STACK_SIZE ) ;
backtrace_strings = backtrace_symbols ( backtrace_stack , backtrace_size ) ;
DEBUG ( 0 , ( " BACKTRACE: %lu stack frames: \n " ,
( unsigned long ) backtrace_size ) ) ;
if ( backtrace_strings ) {
2019-06-21 08:10:19 +03:00
size_t i ;
2018-04-10 07:35:07 +03:00
for ( i = 0 ; i < backtrace_size ; i + + )
2019-06-21 08:10:19 +03:00
DEBUGADD ( 0 , ( " #%zu %s \n " , i , backtrace_strings [ i ] ) ) ;
2018-04-10 07:35:07 +03:00
/* Leak the backtrace_strings, rather than risk what free() might do */
}
# else
DEBUG ( 0 , ( " unable to produce a stack trace on this platform \n " ) ) ;
# endif
}