Commit 6e7c9ea8 authored by Cyrill Gorcunov's avatar Cyrill Gorcunov Committed by Pavel Emelyanov

util: Extend reopen_fd_as_safe

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: 's avatarCyrill Gorcunov <gorcunov@openvz.org>
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 231db3f8
...@@ -164,9 +164,9 @@ extern void pr_vma(unsigned int loglevel, const struct vma_area *vma_area); ...@@ -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 move_img_fd(int *img_fd, int want_fd);
extern int close_safe(int *fd); extern int close_safe(int *fd);
extern int reopen_fd_as_safe(int new_fd, int old_fd, bool allow_reuse_fd); 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(new_fd, old_fd, false) #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(new_fd, old_fd, true) #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 open_pid_proc(pid_t pid);
int close_pid_proc(void); int close_pid_proc(void);
......
...@@ -77,7 +77,7 @@ int close_safe(int *fd) ...@@ -77,7 +77,7 @@ int close_safe(int *fd)
return ret; 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; int tmp;
...@@ -89,9 +89,11 @@ int reopen_fd_as_safe(int new_fd, int old_fd, bool allow_reuse_fd) ...@@ -89,9 +89,11 @@ int reopen_fd_as_safe(int new_fd, int old_fd, bool allow_reuse_fd)
/* /*
* Standard descriptors. * 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 { } 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; return -1;
} }
} }
...@@ -99,7 +101,8 @@ int reopen_fd_as_safe(int new_fd, int old_fd, bool allow_reuse_fd) ...@@ -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); tmp = dup2(old_fd, new_fd);
if (tmp < 0) { 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; return tmp;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment