[CRIU] [PATCH] criu: print correct errno messages from pr_perror()

Andrey Vagin avagin at openvz.org
Thu Feb 12 10:24:13 PST 2015


"%m" can't be used to print strerror(errno), because print_on_level()
calls gettimeofday() which can overwrite errno.

For example:
13486 connect(4, {sa_family=AF_INET, sin_port=htons(8880), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 ENETUNREACH (Network is unreachable)
13486 gettimeofday({1423756664, 717423}, NULL) = 0
13486 open("/etc/localtime", O_RDONLY|O_CLOEXEC) = -1 EACCES (Permission denied)
13486 write(2, "15:57:44.717:     4: ERR: socket_udp.c:73: Can't connect (errno = 101 (Permission denied))\n", 91) = 91

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 include/criu-log.h | 13 ++++++++++++-
 include/sk-inet.h  |  2 +-
 pie/restorer.c     |  2 +-
 3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/include/criu-log.h b/include/criu-log.h
index a701349..948ab96 100644
--- a/include/criu-log.h
+++ b/include/criu-log.h
@@ -20,6 +20,13 @@
 #ifndef __CRIU_LOG_H__
 #define __CRIU_LOG_H__
 
+#ifndef CR_NOGLIBC
+
+#include <string.h>
+#include <errno.h>
+
+#endif /* CR_NOGLIBC */
+
 #define LOG_UNSET	(-1)
 #define LOG_MSG		(0) /* Print message regardless of log level */
 #define LOG_ERROR	(1) /* Errors only, when we're in trouble */
@@ -74,7 +81,11 @@ extern void print_on_level(unsigned int loglevel, const char *format, ...)
 	print_on_level(LOG_DEBUG,					\
 		       LOG_PREFIX fmt, ##__VA_ARGS__)
 
+#ifndef CR_NOGLIBC
+
 #define pr_perror(fmt, ...)						\
-	pr_err(fmt ": %m\n", ##__VA_ARGS__)
+	pr_err(fmt ": %s\n", ##__VA_ARGS__, strerror(errno))
+
+#endif /* CR_NOGLIBC */
 
 #endif /* __CR_LOG_LEVELS_H__ */
diff --git a/include/sk-inet.h b/include/sk-inet.h
index e8fc74f..aa7db16 100644
--- a/include/sk-inet.h
+++ b/include/sk-inet.h
@@ -65,7 +65,7 @@ static inline void tcp_repair_off(int fd)
 
 	ret = sys_setsockopt(fd, SOL_TCP, TCP_REPAIR, &aux, sizeof(aux));
 	if (ret < 0)
-		pr_perror("Failed to turn off repair mode on socket (%d)", ret);
+		pr_err("Failed to turn off repair mode on socket (%d)", ret);
 }
 
 extern void tcp_locked_conn_add(struct inet_sk_info *);
diff --git a/pie/restorer.c b/pie/restorer.c
index c45892e..c890360 100644
--- a/pie/restorer.c
+++ b/pie/restorer.c
@@ -462,7 +462,7 @@ static void rst_tcp_repair_off(struct rst_tcp_sock *rts)
 
 	ret = sys_setsockopt(rts->sk, SOL_SOCKET, SO_REUSEADDR, &aux, sizeof(aux));
 	if (ret < 0)
-		pr_perror("Failed to restore of SO_REUSEADDR on socket (%d)", ret);
+		pr_err("Failed to restore of SO_REUSEADDR on socket (%d)", ret);
 }
 
 static void rst_tcp_socks_all(struct task_restore_args *ta)
-- 
1.9.3



More information about the CRIU mailing list