]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
log: thread-safety backports
authorChristian Brauner <christian.brauner@ubuntu.com>
Mon, 10 Dec 2018 14:14:07 +0000 (15:14 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Mon, 10 Dec 2018 14:14:07 +0000 (15:14 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
configure.ac
src/lxc/console.c
src/lxc/log.h

index 649b57afd134cf110203933ea0b70045787b8881..3cbaddf26bf4538532bbe29d9fe1970d7f28b9d9 100644 (file)
@@ -654,6 +654,12 @@ AC_HEADER_MAJOR
 # Check for some syscalls functions
 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
 AC_CHECK_LIB(pthread, main)
 AC_CHECK_FUNCS(pthread_atfork)
index 6cc3aad0871309c67231123291aa281a2db3d1b5..d9f5b7eccf832606d7a737e3ebabb2c459e76085 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "config.h"
+
 #include <errno.h>
 #include <fcntl.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <termios.h>
-#include <unistd.h>
 #include <sys/epoll.h>
 #include <sys/types.h>
+#include <termios.h>
+#include <unistd.h>
 
 #include <lxc/lxccontainer.h>
 
 #include "caps.h"
 #include "commands.h"
 #include "conf.h"
-#include "config.h"
 #include "console.h"
 #include "log.h"
 #include "lxclock.h"
 #include "mainloop.h"
-#include "start.h"     /* for struct lxc_handler */
+#include "start.h" /* for struct lxc_handler */
 #include "utils.h"
 
 #if HAVE_PTY_H
index 6030809f736030d3e9f4a387e7e3b7ab37c37a40..d114f1f4405623ca20a055dee32b552033a813ae 100644 (file)
@@ -276,22 +276,44 @@ ATTR_UNUSED static inline void LXC_##LEVEL(struct lxc_log_locinfo* locinfo,       \
 /*
  * Helper macro to define errno string.
  */
-#if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE) || IS_BIONIC
-#define lxc_log_strerror_r                                               \
-       char errno_buf[MAXPATHLEN / 2] = {"Failed to get errno string"}; \
-       char *ptr = errno_buf;                                           \
-       {                                                                \
-               (void)strerror_r(errno, errno_buf, sizeof(errno_buf));   \
-       }
+#if HAVE_STRERROR_R
+       #ifndef 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
+       #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
-#define lxc_log_strerror_r                                               \
-       char errno_buf[MAXPATHLEN / 2] = {"Failed to get errno string"}; \
-       char *ptr;                                                       \
-       {                                                                \
-               ptr = strerror_r(errno, errno_buf, sizeof(errno_buf));   \
-               if (!ptr)                                                \
-                       ptr = errno_buf;                                 \
-       }
+       #define lxc_log_strerror_r                                                       \
+               char *ptr = NULL;                                                        \
+               {                                                                        \
+                       ptr = strerror(errno);                                           \
+               }
 #endif
 
 /*