Skip to content

Commit

Permalink
log: add lxc_log_strerror_r macro
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
2xsec authored and Christian Brauner committed Dec 10, 2018
1 parent f1f839b commit 14a740a
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 5 deletions.
61 changes: 56 additions & 5 deletions src/lxc/log.h
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions src/lxc/lsm/apparmor.c
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions src/lxc/lsm/lsm.c
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions src/lxc/lsm/selinux.c
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions src/lxc/monitor.c
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions src/lxc/state.c
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions src/lxc/sync.c
Expand Up @@ -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>
Expand Down

0 comments on commit 14a740a

Please sign in to comment.