diff --git a/meson.build b/meson.build index ace66c0c8..65058e1ee 100644 --- a/meson.build +++ b/meson.build @@ -148,6 +148,7 @@ want_openssl = get_option('openssl') want_selinux = get_option('selinux') want_oss_fuzz = get_option('oss-fuzz') want_seccomp = get_option('seccomp') +want_thread_safety = get_option('thread-safety') srcconf.set_quoted('DEFAULT_CGROUP_PATTERN', cgrouppattern) if coverity @@ -403,6 +404,8 @@ if want_oss_fuzz fuzzing_engine = meson.get_compiler('cpp').find_library('FuzzingEngine') endif +srcconf.set10('ENFORCE_THREAD_SAFETY', want_thread_safety) + ## PAM. pam = cc.find_library('pam', has_headers: 'security/pam_modules.h', required: want_pam_cgroup) srcconf.set10('HAVE_PAM', pam.found()) @@ -415,6 +418,28 @@ srcconf.set10('HAVE_STRCHRNUL', have) have = cc.has_function('openpty', prefix: '#include ', args: '-D_GNU_SOURCE') srcconf.set10('HAVE_OPENPTY', have) +have_func_strerror_r = cc.has_function('strerror_r', prefix: '#include ', args: '-D_GNU_SOURCE') +srcconf.set10('HAVE_STRERROR_R', have) + +have_func_strerror_r_char_p = false + +if have_func_strerror_r + code = ''' +#define _GNU_SOURCE +#include +int func (void) { + char error_string[256]; + char *ptr = strerror_r (-2, error_string, 256); + char c = *strerror_r (-2, error_string, 256); + return c != 0 && ptr != (void*) 0L; +} +''' + +have_func_strerror_r_char_p = cc.compiles(code, name : 'strerror_r() returns char *') +endif + +srcconf.set10('STRERROR_R_CHAR_P', have_func_strerror_r_char_p) + ## Compiler attributes. foreach ccattr: [ 'fallthrough', @@ -435,6 +460,7 @@ foreach tuple: [ ['execveat'], ['faccessat'], ['strchrnul'], + ['strerror_r'], ['fgetln'], ['fsconfig'], ['fsmount'], diff --git a/meson_options.txt b/meson_options.txt index 807a6ebec..4a2da223c 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -77,3 +77,6 @@ option('usernet-db-path', type: 'string', value: 'lxc/nics', option('oss-fuzz', type : 'boolean', value : 'false', description : 'build against oss-fuzz') + +option('thread-safety', type : 'boolean', value : 'true', + description : 'whether the build fails when thread-safe logging cannot be guaranteed') diff --git a/src/lxc/log.h b/src/lxc/log.h index 6cee31886..554a2e1d4 100644 --- a/src/lxc/log.h +++ b/src/lxc/log.h @@ -304,12 +304,10 @@ __lxc_unused static inline void LXC_##LEVEL(struct lxc_log_locinfo* locinfo, \ * Helper macro to define errno string. */ #if HAVE_STRERROR_R - #if !HAVE_DECL_STRERROR_R - #ifdef STRERROR_R_CHAR_P - char *strerror_r(int errnum, char *buf, size_t buflen); - #else - int strerror_r(int errnum, char *buf, size_t buflen); - #endif + #ifdef STRERROR_R_CHAR_P + char *strerror_r(int errnum, char *buf, size_t buflen); + #else + int strerror_r(int errnum, char *buf, size_t buflen); #endif #ifdef STRERROR_R_CHAR_P diff --git a/src/tests/oss-fuzz.sh b/src/tests/oss-fuzz.sh index 665facdcd..4a3920a77 100755 --- a/src/tests/oss-fuzz.sh +++ b/src/tests/oss-fuzz.sh @@ -46,6 +46,7 @@ meson setup san_build \ -Dseccomp=false \ -Db_lto=false \ -Db_pie=false \ + -Dthread-safety=false \ -Doss-fuzz=true ninja -C san_build -v