From f274f16081278d6c560d2e5f8086cc7569b0a3a1 Mon Sep 17 00:00:00 2001 From: Andrey Vagin Date: Tue, 10 Jan 2012 16:39:00 +0400 Subject: [PATCH] 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: Andrey Vagin Signed-off-by: Cyrill Gorcunov --- log.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/log.c b/log.c index 7454b1fc8..a080cb00b 100644 --- a/log.c +++ b/log.c @@ -32,6 +32,11 @@ int init_log(const char *name) struct rlimit rlimit; int fd = STDERR_FILENO; + if (getrlimit(RLIMIT_NOFILE, &rlimit)) { + pr_err("can't get rlimit: %m\n"); + return -1; + } + if (name) { fd = open(name, O_CREAT | O_WRONLY); if (fd == -1) { @@ -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; - if (dup2(fd, logfd) < 0) { + if (reopen_fd_as(logfd, fd) < 0) { pr_err("can't duplicate descriptor %d->%d: %m\n", fd, logfd); logfd = STDERR_FILENO; - return -1; + goto err; } return 0; +err: + if (name) + close(fd); + return -1; } void fini_log(void)