Commit f274f160 authored by Andrey Vagin's avatar Andrey Vagin Committed by Cyrill Gorcunov

log: close unused descriptor

This code opens a log file and duplicates the descriptor to logfd,
but forgets to close the first one.
Signed-off-by: 's avatarAndrey Vagin <avagin@openvz.org>
Signed-off-by: 's avatarCyrill Gorcunov <gorcunov@openvz.org>
parent 5d5e8b80
...@@ -32,6 +32,11 @@ int init_log(const char *name) ...@@ -32,6 +32,11 @@ int init_log(const char *name)
struct rlimit rlimit; struct rlimit rlimit;
int fd = STDERR_FILENO; int fd = STDERR_FILENO;
if (getrlimit(RLIMIT_NOFILE, &rlimit)) {
pr_err("can't get rlimit: %m\n");
return -1;
}
if (name) { if (name) {
fd = open(name, O_CREAT | O_WRONLY); fd = open(name, O_CREAT | O_WRONLY);
if (fd == -1) { if (fd == -1) {
...@@ -40,20 +45,19 @@ int init_log(const char *name) ...@@ -40,20 +45,19 @@ int init_log(const char *name)
} }
} }
if (getrlimit(RLIMIT_NOFILE, &rlimit)) {
pr_err("can't get rlimit: %m\n");
return -1;
}
logfd = rlimit.rlim_cur - 1; logfd = rlimit.rlim_cur - 1;
if (dup2(fd, logfd) < 0) { if (reopen_fd_as(logfd, fd) < 0) {
pr_err("can't duplicate descriptor %d->%d: %m\n", pr_err("can't duplicate descriptor %d->%d: %m\n",
fd, logfd); fd, logfd);
logfd = STDERR_FILENO; logfd = STDERR_FILENO;
return -1; goto err;
} }
return 0; return 0;
err:
if (name)
close(fd);
return -1;
} }
void fini_log(void) void fini_log(void)
......
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