From: 2xsec Date: Mon, 25 Jun 2018 13:00:43 +0000 (+0900) Subject: log: add lxc_log_strerror_r macro X-Git-Tag: lxc-2.0.10~60 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=14a740a620558daf092790b82ef6aaed515eca63;p=thirdparty%2Flxc.git log: add lxc_log_strerror_r macro 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 [christian.brauner@ubuntu.com: simplify lxc_log_strerror_r macro] Signed-off-by: Christian Brauner --- diff --git a/src/lxc/log.h b/src/lxc/log.h index 9dffef48b..f1cffcef2 100644 --- a/src/lxc/log.h +++ b/src/lxc/log.h @@ -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; diff --git a/src/lxc/lsm/apparmor.c b/src/lxc/lsm/apparmor.c index d21b3d224..0b13e172b 100644 --- a/src/lxc/lsm/apparmor.c +++ b/src/lxc/lsm/apparmor.c @@ -18,6 +18,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#define _GNU_SOURCE #include #include #include diff --git a/src/lxc/lsm/lsm.c b/src/lxc/lsm/lsm.c index 5186fe902..e404002d6 100644 --- a/src/lxc/lsm/lsm.c +++ b/src/lxc/lsm/lsm.c @@ -21,6 +21,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#define _GNU_SOURCE #include #include #include diff --git a/src/lxc/lsm/selinux.c b/src/lxc/lsm/selinux.c index bd6541cfa..1d8ce4cba 100644 --- a/src/lxc/lsm/selinux.c +++ b/src/lxc/lsm/selinux.c @@ -21,6 +21,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#define _GNU_SOURCE #include #include #include diff --git a/src/lxc/monitor.c b/src/lxc/monitor.c index 9c5da3680..646b42af5 100644 --- a/src/lxc/monitor.c +++ b/src/lxc/monitor.c @@ -22,6 +22,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#define _GNU_SOURCE #include #include #include diff --git a/src/lxc/state.c b/src/lxc/state.c index 47e671249..86236b67f 100644 --- a/src/lxc/state.c +++ b/src/lxc/state.c @@ -21,6 +21,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#define _GNU_SOURCE #include #include #include diff --git a/src/lxc/sync.c b/src/lxc/sync.c index 9c20c1d69..adc6fb2c3 100644 --- a/src/lxc/sync.c +++ b/src/lxc/sync.c @@ -21,6 +21,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#define _GNU_SOURCE #include #include #include