From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:48188) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SUdyT-0007hR-2e for qemu-devel@nongnu.org; Wed, 16 May 2012 09:09:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SUdyM-0003eW-Py for qemu-devel@nongnu.org; Wed, 16 May 2012 09:09:04 -0400 Received: from mx.meyering.net ([88.168.87.75]:35776 helo=hx.meyering.net) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SUdyM-0003eF-E0 for qemu-devel@nongnu.org; Wed, 16 May 2012 09:08:58 -0400 From: Jim Meyering Date: Wed, 16 May 2012 15:07:57 +0200 Message-Id: <1337173681-25891-3-git-send-email-jim@meyering.net> In-Reply-To: <1337173681-25891-1-git-send-email-jim@meyering.net> References: <1337173681-25891-1-git-send-email-jim@meyering.net> Subject: [Qemu-devel] [PATCH 2/6] qemu-ga: avoid unconditional lockfile file descriptor leak List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Anthony Liguori , Stefan Hajnoczi , Jim Meyering , Stefan Weil , Michael Roth , Luiz Capitulino From: Jim Meyering Do not leak a file descriptor. Also, do not forget to unlink the lockfile upon failed lockf. Always close the lockfile file descriptor, taking care to diagnose close, as well as open and write, failure. Signed-off-by: Jim Meyering --- qemu-ga.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/qemu-ga.c b/qemu-ga.c index 680997e..6c6de55 100644 --- a/qemu-ga.c +++ b/qemu-ga.c @@ -241,12 +241,13 @@ void ga_set_response_delimited(GAState *s) static bool ga_open_pidfile(const char *pidfile) { int pidfd; + int write_err; char pidstr[32]; pidfd = open(pidfile, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR); if (pidfd == -1 || lockf(pidfd, F_TLOCK, 0)) { g_critical("Cannot lock pid file, %s", strerror(errno)); - return false; + goto fail; } if (ftruncate(pidfd, 0) || lseek(pidfd, 0, SEEK_SET)) { @@ -254,7 +255,9 @@ static bool ga_open_pidfile(const char *pidfile) goto fail; } sprintf(pidstr, "%d", getpid()); - if (write(pidfd, pidstr, strlen(pidstr)) != strlen(pidstr)) { + write_err = write(pidfd, pidstr, strlen(pidstr)) != strlen(pidstr); + if (close(pidfd) || write_err) { + pidfd = -1; g_critical("Failed to write pid file"); goto fail; } @@ -262,6 +265,9 @@ static bool ga_open_pidfile(const char *pidfile) return true; fail: + if (pidfd != -1) { + close(pidfd); + } unlink(pidfile); return false; } -- 1.7.10.2.520.g6a4a482