]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
log: add lxc_log_strerror_r macro
author2xsec <dh48.jeong@samsung.com>
Mon, 25 Jun 2018 13:00:43 +0000 (22:00 +0900)
committerChristian Brauner <christian.brauner@ubuntu.com>
Mon, 10 Dec 2018 13:33:12 +0000 (14:33 +0100)
Let's ensure that we always use the thread-safe strerror_r() function and add
an approriate macro.
Additionally, define SYS*() macros for all log levels. They will use the new
macro and ensure thread-safe retrieval of errno values.

Signed-off-by: 2xsec <dh48.jeong@samsung.com>
[christian.brauner@ubuntu.com: simplify lxc_log_strerror_r macro]
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/log.h
src/lxc/lsm/apparmor.c
src/lxc/lsm/lsm.c
src/lxc/lsm/selinux.c
src/lxc/monitor.c
src/lxc/state.c
src/lxc/sync.c

index 9dffef48b92e259fff53b314d64ad6c45afa90fb..f1cffcef2b4218444b09e9b52e56c1d3e9f1a74b 100644 (file)
@@ -273,6 +273,27 @@ ATTR_UNUSED static inline void LXC_##LEVEL(struct lxc_log_locinfo* locinfo,        \
 #define lxc_log_category_priority(name)                                \
        (lxc_log_priority_to_string(lxc_log_category_##name.priority))
 
+/*
+ * Helper macro to define errno string.
+ */
+#if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE)
+#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));   \
+       }
+#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;                                 \
+       }
+#endif
+
 /*
  * top categories
  */
@@ -321,11 +342,41 @@ ATTR_UNUSED static inline void LXC_##LEVEL(struct lxc_log_locinfo* locinfo,       \
        LXC_FATAL(&locinfo, format, ##__VA_ARGS__);                     \
 } while (0)
 
-
-
-#define SYSERROR(format, ...) do {                                     \
-       ERROR("%s - " format, strerror(errno), ##__VA_ARGS__);          \
-} while (0)
+#define SYSTRACE(format, ...)                              \
+       do {                                               \
+               lxc_log_strerror_r;                        \
+               TRACE("%s - " format, ptr, ##__VA_ARGS__); \
+       } while (0)
+
+#define SYSDEBUG(format, ...)                              \
+       do {                                               \
+               lxc_log_strerror_r;                        \
+               DEBUG("%s - " format, ptr, ##__VA_ARGS__); \
+       } while (0)
+
+#define SYSINFO(format, ...)                              \
+       do {                                              \
+               lxc_log_strerror_r;                       \
+               INFO("%s - " format, ptr, ##__VA_ARGS__); \
+       } while (0)
+
+#define SYSNOTICE(format, ...)                              \
+       do {                                                \
+               lxc_log_strerror_r;                         \
+               NOTICE("%s - " format, ptr, ##__VA_ARGS__); \
+       } while (0)
+
+#define SYSWARN(format, ...)                              \
+       do {                                              \
+               lxc_log_strerror_r;                       \
+               WARN("%s - " format, ptr, ##__VA_ARGS__); \
+       } while (0)
+
+#define SYSERROR(format, ...)                              \
+       do {                                               \
+               lxc_log_strerror_r;                        \
+               ERROR("%s - " format, ptr, ##__VA_ARGS__); \
+       } while (0)
 
 extern int lxc_log_fd;
 
index d21b3d224d5f39fb5f11a6bd076b7749ba47701d..0b13e172bc62b22c6cf1bb9bea71f8dcffcb17aa 100644 (file)
@@ -18,6 +18,7 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
+#define _GNU_SOURCE
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
index 5186fe90257e36196bd4bbd534382683cbea4a54..e404002d685b9d23b96bb9cfb05e90ef3ff9d1b8 100644 (file)
@@ -21,6 +21,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#define _GNU_SOURCE
 #include <errno.h>
 #include <stdlib.h>
 #include <unistd.h>
index bd6541cfaec8c1766341099dd10f23e220ff4870..1d8ce4cbadd19a262251c5a18b3b76f2bb824b6c 100644 (file)
@@ -21,6 +21,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#define _GNU_SOURCE
 #include <errno.h>
 #include <stdlib.h>
 #include <stdbool.h>
index 9c5da368055ccf6345be244f033491f02b3832b1..646b42af5734cfd5e97a4eeffeeae1da5c8f9946 100644 (file)
@@ -22,6 +22,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#define _GNU_SOURCE
 #include <errno.h>
 #include <fcntl.h>
 #include <inttypes.h>
index 47e6712498299275655103cb8735f422101ef8b0..86236b67f22f3bf2227c9310615f7e88eb2507fc 100644 (file)
@@ -21,6 +21,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#define _GNU_SOURCE
 #include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
index 9c20c1d69c93ecc10c04da557d75b68e737b0773..adc6fb2c3e77c9f595d9f5c7b6c725f0d9952aaa 100644 (file)
@@ -21,6 +21,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#define _GNU_SOURCE
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <unistd.h>