qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Michael Roth <mdroth@linux.vnet.ibm.com>
To: Jim Meyering <jim@meyering.net>
Cc: Anthony Liguori <aliguori@us.ibm.com>,
	Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>,
	Jim Meyering <meyering@redhat.com>, Stefan Weil <sw@weilnetz.de>,
	qemu-devel@nongnu.org, Luiz Capitulino <lcapitulino@redhat.com>
Subject: Re: [Qemu-devel] [PATCH 2/6] qemu-ga: avoid unconditional lockfile file descriptor leak
Date: Wed, 16 May 2012 13:15:44 -0500	[thread overview]
Message-ID: <20120516181544.GC2967@illuin> (raw)
In-Reply-To: <1337173681-25891-3-git-send-email-jim@meyering.net>

On Wed, May 16, 2012 at 03:07:57PM +0200, Jim Meyering wrote:
> From: Jim Meyering <meyering@redhat.com>
> 
> 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 <meyering@redhat.com>
> ---
>  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) {

Closing the fd gives up the lock, which is not what we want in this
case. The intention is to hold it for the life of the process. In the
case of close() failing the error msg will also be inaccurate.

So I think we can leave this chunk out. In the error case the fd will get
cleaned up with the handling you added below.

For the success case, If we want to avoid leaking it, I would suggest
assigning it to a new field in GAState which is close()'d somewhere in
main() after we exit from g_main_loop_run().

> +        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);

Since the patch wants to report close() errors above, we should do it
here too.

> +    }
>      unlink(pidfile);
>      return false;
>  }
> -- 
> 1.7.10.2.520.g6a4a482
> 

  reply	other threads:[~2012-05-16 18:15 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-16 13:07 [Qemu-devel] [PATCH 0/6] plug memory and file-descriptor leaks Jim Meyering
2012-05-16 13:07 ` [Qemu-devel] [PATCH 1/6] qcow2: don't leak buffer for unexpected qcow_version in header Jim Meyering
2012-05-21 10:57   ` Kevin Wolf
2012-05-21 11:06     ` [Qemu-devel] [PATCHv2 " Jim Meyering
2012-05-21 11:24       ` Kevin Wolf
2012-05-21 11:07     ` [Qemu-devel] [PATCH " Jim Meyering
2012-05-16 13:07 ` [Qemu-devel] [PATCH 2/6] qemu-ga: avoid unconditional lockfile file descriptor leak Jim Meyering
2012-05-16 18:15   ` Michael Roth [this message]
2012-05-16 20:17     ` Jim Meyering
2012-05-16 20:19   ` [Qemu-devel] [PATCHv2 2/6] qemu-ga: don't leak a file descriptor upon failed lockf Jim Meyering
2012-05-16 20:58     ` Michael Roth
2012-05-16 13:07 ` [Qemu-devel] [PATCH 3/6] linux-user: do_msgrcv: don't leak host_mb upon TARGET_EFAULT failure Jim Meyering
2012-05-16 13:21   ` Peter Maydell
2012-05-16 13:50     ` Jim Meyering
2012-05-16 13:52   ` [Qemu-devel] [PATCHv2 " Jim Meyering
2012-05-16 13:07 ` [Qemu-devel] [PATCH 4/6] sheepdog: don't leak socket file descriptor upon connection failure Jim Meyering
2012-05-21 11:00   ` Kevin Wolf
2012-08-17 13:30     ` Jim Meyering
2012-08-17 13:40       ` Kevin Wolf
2012-08-17 13:42         ` Jim Meyering
2012-05-16 13:08 ` [Qemu-devel] [PATCH 5/6] arm-semi: don't leak 1kb user string lock buffer upon TARGET_SYS_OPEN Jim Meyering
2012-05-16 13:15   ` Peter Maydell
2012-05-16 13:08 ` [Qemu-devel] [PATCH 6/6] softmmu-semi: fix lock_user* functions not to deref NULL upon OOM Jim Meyering
2012-05-19  7:13   ` Matthew Fernandez
2012-05-19 15:46   ` Peter Maydell
2012-05-24 14:46     ` Jim Meyering
2012-05-24 14:46   ` [Qemu-devel] [PATCH v2 " Jim Meyering

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120516181544.GC2967@illuin \
    --to=mdroth@linux.vnet.ibm.com \
    --cc=aliguori@us.ibm.com \
    --cc=jim@meyering.net \
    --cc=lcapitulino@redhat.com \
    --cc=meyering@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@linux.vnet.ibm.com \
    --cc=sw@weilnetz.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).