log: thread-safety backports

parent 535ceb0b
...@@ -654,6 +654,12 @@ AC_HEADER_MAJOR ...@@ -654,6 +654,12 @@ AC_HEADER_MAJOR
# Check for some syscalls functions # Check for some syscalls functions
AC_CHECK_FUNCS([setns pivot_root sethostname unshare rand_r confstr faccessat gettid memfd_create]) AC_CHECK_FUNCS([setns pivot_root sethostname unshare rand_r confstr faccessat gettid memfd_create])
# Check for strerror_r() support. Defines:
# - HAVE_STRERROR_R if available
# - HAVE_DECL_STRERROR_R if defined
# - STRERROR_R_CHAR_P if it returns char *
AC_FUNC_STRERROR_R
# Check for some functions # Check for some functions
AC_CHECK_LIB(pthread, main) AC_CHECK_LIB(pthread, main)
AC_CHECK_FUNCS(pthread_atfork) AC_CHECK_FUNCS(pthread_atfork)
......
...@@ -21,15 +21,17 @@ ...@@ -21,15 +21,17 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#include "config.h"
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <sys/epoll.h> #include <sys/epoll.h>
#include <sys/types.h> #include <sys/types.h>
#include <termios.h>
#include <unistd.h>
#include <lxc/lxccontainer.h> #include <lxc/lxccontainer.h>
...@@ -37,12 +39,11 @@ ...@@ -37,12 +39,11 @@
#include "caps.h" #include "caps.h"
#include "commands.h" #include "commands.h"
#include "conf.h" #include "conf.h"
#include "config.h"
#include "console.h" #include "console.h"
#include "log.h" #include "log.h"
#include "lxclock.h" #include "lxclock.h"
#include "mainloop.h" #include "mainloop.h"
#include "start.h" /* for struct lxc_handler */ #include "start.h" /* for struct lxc_handler */
#include "utils.h" #include "utils.h"
#if HAVE_PTY_H #if HAVE_PTY_H
......
...@@ -276,22 +276,44 @@ ATTR_UNUSED static inline void LXC_##LEVEL(struct lxc_log_locinfo* locinfo, \ ...@@ -276,22 +276,44 @@ ATTR_UNUSED static inline void LXC_##LEVEL(struct lxc_log_locinfo* locinfo, \
/* /*
* Helper macro to define errno string. * Helper macro to define errno string.
*/ */
#if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE) || IS_BIONIC #if HAVE_STRERROR_R
#define lxc_log_strerror_r \ #ifndef HAVE_DECL_STRERROR_R
char errno_buf[MAXPATHLEN / 2] = {"Failed to get errno string"}; \ #ifdef STRERROR_R_CHAR_P
char *ptr = errno_buf; \ char *strerror_r(int errnum, char *buf, size_t buflen);
{ \ #else
(void)strerror_r(errno, errno_buf, sizeof(errno_buf)); \ int strerror_r(int errnum, char *buf, size_t buflen);
} #endif
#endif
#ifdef STRERROR_R_CHAR_P
#define lxc_log_strerror_r \
char errno_buf[PATH_MAX / 2] = {"Failed to get errno string"}; \
char *ptr = NULL; \
{ \
int saved_errno = errno; \
ptr = strerror_r(errno, errno_buf, sizeof(errno_buf)); \
errno = saved_errno; \
if (!ptr) \
ptr = errno_buf; \
}
#else
#define lxc_log_strerror_r \
char errno_buf[PATH_MAX / 2] = {"Failed to get errno string"}; \
char *ptr = errno_buf; \
{ \
int saved_errno = errno; \
(void)strerror_r(errno, errno_buf, sizeof(errno_buf)); \
errno = saved_errno; \
}
#endif
#elif ENFORCE_THREAD_SAFETY
#error ENFORCE_THREAD_SAFETY was set but cannot be guaranteed
#else #else
#define lxc_log_strerror_r \ #define lxc_log_strerror_r \
char errno_buf[MAXPATHLEN / 2] = {"Failed to get errno string"}; \ char *ptr = NULL; \
char *ptr; \ { \
{ \ ptr = strerror(errno); \
ptr = strerror_r(errno, errno_buf, sizeof(errno_buf)); \ }
if (!ptr) \
ptr = errno_buf; \
}
#endif #endif
/* /*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment