From 41fe13dd2eee6140c4546e7bc8569c86d9a07bb7 Mon Sep 17 00:00:00 2001 From: Richard Patel Date: Thu, 20 Jul 2023 22:58:18 +0000 Subject: [PATCH] musl support - disable utterly useless "bitwise arithmetic instead of logic" compile Clang warning - deps.sh: add linux-headers Alpine package - fdctl: switch between GCC-internal __rlimit_resource_t type and POSIX-compatible int for RLIMIT_{...} - quic: remove accidental execinfo.h import - log: only export backtraces when execinfo.h found - shmem: replace non-standard PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP with portable PTHREAD_MUTEX_RECURSIVE initializer - tile: skip non-standard pthread_attr_setaffinity_np on non-glibc --- config/x86-64-clang-flags.mk | 1 + deps.sh | 2 +- src/app/fdctl/security.c | 10 +++++----- src/app/fdctl/security.h | 24 +++++++++++++++++++----- src/tango/quic/fd_quic.c | 1 - src/util/log/fd_log.c | 27 +++++++++++++++++++++++++++ src/util/shmem/fd_shmem_admin.c | 12 +++++++++++- src/util/tile/fd_tile_threads.cxx | 6 ++++++ 8 files changed, 70 insertions(+), 13 deletions(-) diff --git a/config/x86-64-clang-flags.mk b/config/x86-64-clang-flags.mk index f651cb9436..4cf1723148 100644 --- a/config/x86-64-clang-flags.mk +++ b/config/x86-64-clang-flags.mk @@ -1 +1,2 @@ +CPPFLAGS+=-Wno-bitwise-instead-of-logical LDFLAGS+=-lrt diff --git a/deps.sh b/deps.sh index 65559bc806..c3dd923e6a 100755 --- a/deps.sh +++ b/deps.sh @@ -178,7 +178,7 @@ check_debian_pkgs () { } check_alpine_pkgs () { - local REQUIRED_APKS=( perl autoconf gettext automake flex bison build-base ) + local REQUIRED_APKS=( perl autoconf gettext automake flex bison build-base linux-headers ) echo "[~] Checking for required APK packages" diff --git a/src/app/fdctl/security.c b/src/app/fdctl/security.c index 8131bae71b..452e010b20 100644 --- a/src/app/fdctl/security.c +++ b/src/app/fdctl/security.c @@ -44,11 +44,11 @@ check_cap( security_t * security, } void -check_res( security_t * security, - const char * name, - __rlimit_resource_t resource, - ulong limit, - const char * reason ) { +check_res( security_t * security, + const char * name, + fd_rlimit_res_t resource, + ulong limit, + const char * reason ) { struct rlimit rlim; if( FD_UNLIKELY( getrlimit( resource, &rlim ) ) ) FD_LOG_ERR(( "getrlimit failed (%i-%s)", errno, strerror( errno ) )); diff --git a/src/app/fdctl/security.h b/src/app/fdctl/security.h index c10d6a5dcf..2c9351a4ee 100644 --- a/src/app/fdctl/security.h +++ b/src/app/fdctl/security.h @@ -22,6 +22,18 @@ typedef struct security { char errors[ MAX_SECURITY_ERRORS ][ 256 ]; } security_t; +/* fd_rlimit_res_t is the appropriate type for RLIMIT_{...} for the + libc flavor in use. glibc with GNU_SOURCE redefines the type of + the first arg to {get,set}rlimit(2), sigh ... */ + +#ifdef __GLIBC__ +typedef __rlimit_resource_t fd_rlimit_res_t; +#else /* non-glibc */ +typedef int fd_rlimit_res_t; +#endif /* __GLIBC__ */ + +FD_PROTOTYPES_BEGIN + /* check_root() checks if the current process is running as the root user (uid 0). If it's not, an error entry is added to the security context with the given reason. */ @@ -44,10 +56,12 @@ check_cap( security_t * security, increase the resource itself (which it will do). If it cannot, an error entry is added to the security context with the given reason. */ void -check_res( security_t * security, - const char * name, - __rlimit_resource_t resource, - ulong limit, - const char * reason ); +check_res( security_t * security, + const char * name, + fd_rlimit_res_t resource, + ulong limit, + const char * reason ); + +FD_PROTOTYPES_END #endif /* HEADER_fd_src_app_fdctl_security_h */ diff --git a/src/tango/quic/fd_quic.c b/src/tango/quic/fd_quic.c index e220ab8c41..d605fb0eef 100644 --- a/src/tango/quic/fd_quic.c +++ b/src/tango/quic/fd_quic.c @@ -9,7 +9,6 @@ #include "tls/fd_quic_tls.h" #include -#include #include #include /* for keylog open(2) */ #include /* for keylog close(2) */ diff --git a/src/util/log/fd_log.c b/src/util/log/fd_log.c index 887c098d55..e7b0c6b970 100644 --- a/src/util/log/fd_log.c +++ b/src/util/log/fd_log.c @@ -25,7 +25,13 @@ #include #include #include + +#if __has_include( ) +#define FD_HAS_BACKTRACE 1 #include +#else +#define FD_HAS_BACKTRACE 0 +#endif /* __has_include( ) */ /* TEXT_* are quick-and-dirty color terminal hacks. Probably should do something more robust longer term. */ @@ -918,6 +924,8 @@ fd_log_private_sig_abort( int sig, /* Hopefully all out streams are idle now and we have flushed out all non-logging activity ... log a backtrace */ +# if FD_HAS_BACKTRACE + void * btrace[128]; int btrace_cnt = backtrace( btrace, 128 ); @@ -940,6 +948,23 @@ fd_log_private_sig_abort( int sig, backtrace_symbols_fd( btrace, btrace_cnt, fd ); fsync( fd ); +# else /* !FD_HAS_BACKTRACE */ + + FILE * log_file = FD_VOLATILE_CONST( fd_log_private_file ); + if( log_file ) { + fprintf( log_file, "Caught signal %i.\n", sig ); +# if FD_LOG_FFLUSH_LOG_FILE + fflush( log_file ); +# endif + } + + fprintf( stderr, "\nCaught signal %i.\n", sig ); +# if FD_LOG_FFLUSH_STDERR + fflush( stderr ); +# endif + +# endif /* FD_HAS_BACKTRACE */ + /* Do final log cleanup */ fd_log_private_cleanup(); @@ -1053,6 +1078,7 @@ fd_log_private_boot( int * pargc, int log_backtrace = fd_env_strip_cmdline_int( pargc, pargv, "--log-backtrace", "FD_LOG_BACKTRACE", 1 ); if( log_backtrace ) { +# if FD_HAS_BACKTRACE /* If libgcc isn't already linked into the program when a trapped signal is received by an application, calls to backtrace and backtrace_symbols_fd within the signal handler can silently @@ -1072,6 +1098,7 @@ fd_log_private_boot( int * pargc, if( FD_UNLIKELY( close( fd ) ) ) fprintf( stderr, "close( \"/dev/null\" ) failed (%i-%s); attempting to continue\n", errno, strerror( errno ) ); } +# endif /* FD_HAS_BACKTRACE */ /* This is all overridable POSIX sigs whose default behavior is to abort the program. It will backtrace and then fallback to the diff --git a/src/util/shmem/fd_shmem_admin.c b/src/util/shmem/fd_shmem_admin.c index ad07d4aaa4..afc33a7a72 100644 --- a/src/util/shmem/fd_shmem_admin.c +++ b/src/util/shmem/fd_shmem_admin.c @@ -16,7 +16,7 @@ #include #if FD_HAS_THREADS -pthread_mutex_t fd_shmem_private_lock[1] = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP }; +pthread_mutex_t fd_shmem_private_lock[1]; #endif char fd_shmem_private_base[ FD_SHMEM_PRIVATE_BASE_MAX ]; /* "" at thread group start, initialized at boot */ @@ -652,6 +652,16 @@ fd_shmem_private_boot( int * pargc, char *** pargv ) { FD_LOG_INFO(( "fd_shmem: booting" )); + /* Initialize the phtread mutex */ + +# if FD_HAS_THREADS + pthread_mutexattr_t lockattr[1]; + pthread_mutexattr_init( lockattr ); + pthread_mutexattr_settype( lockattr, PTHREAD_MUTEX_RECURSIVE ); + pthread_mutex_init( fd_shmem_private_lock, lockattr ); + pthread_mutexattr_destroy( lockattr ); +# endif /* FD_HAS_THREADS */ + /* Cache the numa topology for this thread group's host for subsequent fast use by the application. */ diff --git a/src/util/tile/fd_tile_threads.cxx b/src/util/tile/fd_tile_threads.cxx index 1fae9c6f3e..be0a7fb47a 100644 --- a/src/util/tile/fd_tile_threads.cxx +++ b/src/util/tile/fd_tile_threads.cxx @@ -572,6 +572,11 @@ fd_tile_private_boot( int * pargc, if( FD_UNLIKELY( err ) ) FD_LOG_ERR(( "fd_tile: pthread_attr_init failed (%i-%s) for tile %lu.\n\t", err, strerror( err ), tile_idx )); + /* Set affinity ahead of time. This is a GNU-specific extension + that is not available on musl. On musl, we just skip this + step as we call sched_setaffinity(2) later on regardless. */ + +# if __GLIBC__ if( fixed ) { cpu_set_t cpu_set[1]; CPU_ZERO( cpu_set ); @@ -586,6 +591,7 @@ fd_tile_private_boot( int * pargc, "to eliminate this warning.", err, strerror( err ), tile_idx, cpu_idx )); } +# endif /* __GLIBC__ */ /* Create an optimized stack with guard regions if the build target is x86 (e.g. supports huge pages necessary to optimize TLB usage)