]> git.proxmox.com Git - mirror_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, 25 Jun 2018 14:04:25 +0000 (16:04 +0200)
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
src/lxc/terminal.c

index 960209777dcfed98ac7d283c23899c32d30ce653..e183a3a79bddb1ebc68ace09e3d86ac2cb434998 100644 (file)
@@ -292,6 +292,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
  */
@@ -340,11 +361,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 ec41688472eaa1ccfd48b825ed45e39980a6b70e..793d13f76341e1200e2759e7f50333aecfdd7f2e 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 ebf2122c0544088a25f15f9203a512a8b827e99d..ed223a5b29da4e95241d1e0ab7e3e3beb574ad85 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 06aa3208a4f0b6a0a35e498acec822add9d0922f..12fd65386ba4dbf2cbed5fd7438134aa9fab7763 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>
index eb745c5ff52bc211fec4bfdbd3cedf8a32e08095..e1089acf3eb4bf5fa4bbd855bddc910a126fab21 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 <lxc/lxccontainer.h>