Skip to content

Commit

Permalink
musl support
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
riptl authored and ripatel-fd committed Jul 20, 2023
1 parent 517e015 commit 41fe13d
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 13 deletions.
1 change: 1 addition & 0 deletions config/x86-64-clang-flags.mk
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
CPPFLAGS+=-Wno-bitwise-instead-of-logical
LDFLAGS+=-lrt
2 changes: 1 addition & 1 deletion deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
10 changes: 5 additions & 5 deletions src/app/fdctl/security.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ));
Expand Down
24 changes: 19 additions & 5 deletions src/app/fdctl/security.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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 */
1 change: 0 additions & 1 deletion src/tango/quic/fd_quic.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "tls/fd_quic_tls.h"

#include <errno.h>
#include <execinfo.h>
#include <string.h>
#include <fcntl.h> /* for keylog open(2) */
#include <unistd.h> /* for keylog close(2) */
Expand Down
27 changes: 27 additions & 0 deletions src/util/log/fd_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@
#include <sched.h>
#include <time.h>
#include <syscall.h>

#if __has_include( <execinfo.h> )
#define FD_HAS_BACKTRACE 1
#include <execinfo.h>
#else
#define FD_HAS_BACKTRACE 0
#endif /* __has_include( <execinfo.h> ) */

/* TEXT_* are quick-and-dirty color terminal hacks. Probably should
do something more robust longer term. */
Expand Down Expand Up @@ -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 );

Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 11 additions & 1 deletion src/util/shmem/fd_shmem_admin.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <linux/mman.h>

#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 */
Expand Down Expand Up @@ -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. */

Expand Down
6 changes: 6 additions & 0 deletions src/util/tile/fd_tile_threads.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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)
Expand Down

0 comments on commit 41fe13d

Please sign in to comment.