[CRIU] [PATCH] util: Extend reopen_fd_as_safe

Cyrill Gorcunov gorcunov at openvz.org
Thu Apr 12 10:03:44 EDT 2012


Our loggin engine prints file:line only at invoke point,
so it's unable to see where exactly reopen_fd_as_safe is
failed.

With this patch the output is more human readable

 | Error (util.c:96): fd 7 already in use (called at files.c:359)

Ideally we need full backtrace here, but it's different task.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---

My inotify series started to fail and I found it's impossible
to figure out where the problem is coming from. This patch helps
a lot.

 include/util.h |    6 +++---
 util.c         |   11 +++++++----
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/include/util.h b/include/util.h
index e5ae7ab..b15b445 100644
--- a/include/util.h
+++ b/include/util.h
@@ -164,9 +164,9 @@ extern void pr_vma(unsigned int loglevel, const struct vma_area *vma_area);
 extern int move_img_fd(int *img_fd, int want_fd);
 extern int close_safe(int *fd);
 
-extern int reopen_fd_as_safe(int new_fd, int old_fd, bool allow_reuse_fd);
-#define reopen_fd_as(new_fd, old_fd)		reopen_fd_as_safe(new_fd, old_fd, false)
-#define reopen_fd_as_nocheck(new_fd, old_fd)	reopen_fd_as_safe(new_fd, old_fd, true)
+extern int reopen_fd_as_safe(char *file, int line, int new_fd, int old_fd, bool allow_reuse_fd);
+#define reopen_fd_as(new_fd, old_fd)		reopen_fd_as_safe(__FILE__, __LINE__, new_fd, old_fd, false)
+#define reopen_fd_as_nocheck(new_fd, old_fd)	reopen_fd_as_safe(__FILE__, __LINE__, new_fd, old_fd, true)
 
 int open_pid_proc(pid_t pid);
 int close_pid_proc(void);
diff --git a/util.c b/util.c
index f2626d9..9fb5833 100644
--- a/util.c
+++ b/util.c
@@ -77,7 +77,7 @@ int close_safe(int *fd)
 	return ret;
 }
 
-int reopen_fd_as_safe(int new_fd, int old_fd, bool allow_reuse_fd)
+int reopen_fd_as_safe(char *file, int line, int new_fd, int old_fd, bool allow_reuse_fd)
 {
 	int tmp;
 
@@ -89,9 +89,11 @@ int reopen_fd_as_safe(int new_fd, int old_fd, bool allow_reuse_fd)
 					/*
 					 * Standard descriptors.
 					 */
-					pr_warn("fd %d already in use\n", new_fd);
+					pr_warn("fd %d already in use (called at %s:%d)\n",
+						new_fd, file, line);
 				} else {
-					pr_err("fd %d already in use\n", new_fd);
+					pr_err("fd %d already in use (called at %s:%d)\n",
+						new_fd, file, line);
 					return -1;
 				}
 			}
@@ -99,7 +101,8 @@ int reopen_fd_as_safe(int new_fd, int old_fd, bool allow_reuse_fd)
 
 		tmp = dup2(old_fd, new_fd);
 		if (tmp < 0) {
-			pr_perror("Dup %d -> %d failed", old_fd, new_fd);
+			pr_perror("Dup %d -> %d failed (called at %s:%d)",
+				  old_fd, new_fd, file, line);
 			return tmp;
 		}
 
-- 
1.7.7.6



More information about the CRIU mailing list