Skip to content

Commit

Permalink
Only enable 'unclean fd_log exit' for fdctl
Browse files Browse the repository at this point in the history
Allows unit tests to use atexit(3) with FD_LOG_ERR
  • Loading branch information
riptl authored and ripatel-fd committed Dec 18, 2024
1 parent ee6633e commit d34f504
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 33 deletions.
2 changes: 1 addition & 1 deletion config/base.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ BASEDIR?=build

OPT?=opt
SHELL:=bash
CPPFLAGS:=-isystem ./$(OPT)/include -DFD_LOG_UNCLEAN_EXIT=1 -DFD_HAS_BACKTRACE=0
CPPFLAGS:=-isystem ./$(OPT)/include -DFD_HAS_BACKTRACE=0
RUSTFLAGS:=-C force-frame-pointers=yes
CC:=gcc
CFLAGS=-std=c17 -fwrapv
Expand Down
1 change: 1 addition & 0 deletions src/app/fdctl/main1.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ fdctl_boot( int * pargc,
char *** pargv,
config_t * config,
char const * log_path ) {
fd_log_enable_unclean_exit(); /* Don't call atexit handlers on FD_LOG_ERR */
fd_log_level_core_set( 5 ); /* Don't dump core for FD_LOG_ERR during boot */
fd_log_colorize_set( should_colorize() ); /* Colorize during boot until we can determine from config */
fd_log_level_stderr_set( 2 ); /* Only NOTICE and above will be logged during boot until fd_log is initialized */
Expand Down
68 changes: 36 additions & 32 deletions src/util/log/fd_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ static int fd_log_private_level_logfile; /* 0 outside boot/halt, init at boot */
static int fd_log_private_level_stderr; /* 0 outside boot/halt, init at boot */
static int fd_log_private_level_flush; /* 0 outside boot/halt, init at boot */
static int fd_log_private_level_core; /* 0 outside boot/halt, init at boot */
static int fd_log_private_unclean_exit;

int fd_log_colorize ( void ) { return FD_VOLATILE_CONST( fd_log_private_colorize ); }
int fd_log_level_logfile( void ) { return FD_VOLATILE_CONST( fd_log_private_level_logfile ); }
Expand All @@ -517,6 +518,8 @@ void fd_log_level_core_set ( int level ) { FD_VOLATILE( fd_log_private_level_c

int fd_log_private_logfile_fd( void ) { return FD_VOLATILE_CONST( fd_log_private_fileno ); }

void fd_log_enable_unclean_exit( void ) { fd_log_private_unclean_exit = 1; }

/* Buffer size used for vsnprintf calls (this is also one more than the
maximum size that this can passed to fd_io_write) */

Expand Down Expand Up @@ -876,11 +879,14 @@ fd_log_private_2( int level,
char const * msg ) {
fd_log_private_1( level, now, file, line, func, msg );

# if FD_LOG_UNCLEAN_EXIT && defined(__linux__)
if( level<fd_log_level_core() ) syscall(SYS_exit_group, 1);
# else
if( level<fd_log_level_core() ) exit(1); /* atexit will call fd_log_private_cleanup implicitly */
# endif
if( level<fd_log_level_core() ) {
# if defined(__linux__)
if( fd_log_private_unclean_exit ) {
syscall( SYS_exit_group, 1 );
}
# endif /* defined(__linux__) */
exit(1); /* atexit will call fd_log_private_cleanup implicitly */
}

abort();
}
Expand Down Expand Up @@ -957,7 +963,6 @@ fd_log_private_cleanup( void ) {
} FD_ONCE_END;
}

#ifndef FD_LOG_UNCLEAN_EXIT
static void
fd_log_private_sig_abort( int sig,
siginfo_t * info,
Expand Down Expand Up @@ -1011,7 +1016,6 @@ fd_log_private_sig_trap( int sig ) {
act->sa_flags = (int)(SA_SIGINFO | SA_RESETHAND);
if( sigaction( sig, act, NULL ) ) FD_LOG_ERR(( "unable to override signal %i", sig ));
}
#endif

static int
fd_log_private_open_path( int cmdline,
Expand Down Expand Up @@ -1183,37 +1187,37 @@ fd_log_private_boot( int * pargc,
/* This is all overridable POSIX sigs whose default behavior is to
abort the program. It will backtrace and then fallback to the
default behavior. */
#ifndef FD_LOG_UNCLEAN_EXIT
fd_log_private_sig_trap( SIGABRT );
fd_log_private_sig_trap( SIGALRM );
fd_log_private_sig_trap( SIGFPE );
fd_log_private_sig_trap( SIGHUP );
fd_log_private_sig_trap( SIGILL );
fd_log_private_sig_trap( SIGINT );
fd_log_private_sig_trap( SIGQUIT );
fd_log_private_sig_trap( SIGPIPE );
fd_log_private_sig_trap( SIGSEGV );
fd_log_private_sig_trap( SIGTERM );
fd_log_private_sig_trap( SIGUSR1 );
fd_log_private_sig_trap( SIGUSR2 );
fd_log_private_sig_trap( SIGBUS );
fd_log_private_sig_trap( SIGPOLL );
fd_log_private_sig_trap( SIGPROF );
fd_log_private_sig_trap( SIGSYS );
fd_log_private_sig_trap( SIGTRAP );
fd_log_private_sig_trap( SIGVTALRM );
fd_log_private_sig_trap( SIGXCPU );
fd_log_private_sig_trap( SIGXFSZ );
#endif
if( !fd_log_private_unclean_exit ) {
fd_log_private_sig_trap( SIGABRT );
fd_log_private_sig_trap( SIGALRM );
fd_log_private_sig_trap( SIGFPE );
fd_log_private_sig_trap( SIGHUP );
fd_log_private_sig_trap( SIGILL );
fd_log_private_sig_trap( SIGINT );
fd_log_private_sig_trap( SIGQUIT );
fd_log_private_sig_trap( SIGPIPE );
fd_log_private_sig_trap( SIGSEGV );
fd_log_private_sig_trap( SIGTERM );
fd_log_private_sig_trap( SIGUSR1 );
fd_log_private_sig_trap( SIGUSR2 );
fd_log_private_sig_trap( SIGBUS );
fd_log_private_sig_trap( SIGPOLL );
fd_log_private_sig_trap( SIGPROF );
fd_log_private_sig_trap( SIGSYS );
fd_log_private_sig_trap( SIGTRAP );
fd_log_private_sig_trap( SIGVTALRM );
fd_log_private_sig_trap( SIGXCPU );
fd_log_private_sig_trap( SIGXFSZ );
}
}

/* Hook up the permanent log */
char const * log_path = fd_env_strip_cmdline_cstr( pargc, pargv, "--log-path", "FD_LOG_PATH", NULL );
FD_VOLATILE( fd_log_private_fileno ) = fd_log_private_open_path( 1, log_path );

#if !FD_LOG_UNCLEAN_EXIT
if( atexit( fd_log_private_cleanup ) ) { fd_log_private_fprintf_0( STDERR_FILENO, "atexit failed; unable to boot\n" ); exit(1); }
#endif
if( !fd_log_private_unclean_exit ) {
if( atexit( fd_log_private_cleanup ) ) { fd_log_private_fprintf_0( STDERR_FILENO, "atexit failed; unable to boot\n" ); exit(1); }
}

/* At this point, logging online */
if( fd_log_build_info_sz>1UL ) FD_LOG_INFO(( "fd_log: build info:\n%s", fd_log_build_info ));
Expand Down
2 changes: 2 additions & 0 deletions src/util/log/fd_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,8 @@ void fd_log_level_stderr_set ( int level );
void fd_log_level_flush_set ( int level );
void fd_log_level_core_set ( int level );

void fd_log_enable_unclean_exit( void );

/* These functions are for fd_log internal use only. */

void
Expand Down
6 changes: 6 additions & 0 deletions src/waltz/quic/tests/fd_quic_test_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ fd_quic_test_now( void * context ) {
return (ulong)fd_log_wallclock();
}

static void
flush_pcap( void ) {
fflush( fd_quic_test_pcap );
}

/* Test runtime */

void
Expand All @@ -90,6 +95,7 @@ fd_quic_test_boot( int * pargc,
FD_LOG_NOTICE(( "Logging to --pcap %s", _pcap ));
fd_quic_test_pcap = fopen( _pcap, "ab" );
FD_TEST( fd_quic_test_pcap );
atexit( flush_pcap );
}
}

Expand Down

0 comments on commit d34f504

Please sign in to comment.