qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: Shaun Reitan <shaun.reitan@ndchost.com>
Cc: qemu-devel@nongnu.org, pbonzini@redhat.com
Subject: Re: [Qemu-devel] QEMU leaves pidfile behind on exit
Date: Tue, 13 Feb 2018 16:28:57 +0000	[thread overview]
Message-ID: <20180213162857.GV573@redhat.com> (raw)
In-Reply-To: <embe7de3dd-331d-44fe-9c32-75916c902fb6@ndc-wkst-2>

On Fri, Feb 09, 2018 at 07:12:59PM +0000, Shaun Reitan wrote:
> QEMU leaves the pidfile behind on a clean exit when using the option
> -pidfile /var/run/qemu.pid.
> 
> Should QEMU leave it behind or should it clean up after itself?
> 
> I'm willing to take a crack at a patch to fix the issue, but before I do, I
> want to make sure that leaving the pidfile behind was not intentional?

If QEMU deletes the pidfile on exit then, with the current pidfile
acquisition logic, there's a race condition possible:

To acquire we do

 1. fd = open()
 2. lockf(fd)

If the first QEMU that currently owns the pidfile unlinks in, while
a second qemu is in betweeen steps 1 & 2, the second QEMU will
acquire the pidfile successfully (which is fine) but the pidfile
is now unlinked. This is not fine, because a 3rd qemu can now come
and try to acquire the pidfile (by creating a new one) and succeed,
despite the second qemu still owning the (now unlinked) pidfile.

It is possible to deal with this race by making qemu_create_pidfile
more intelligent [1]. It would have todo

  1. fd = open(filename)
  2. fstat(fd)
  3. lockf(fd)
  4. stat(filename)

It must then compare the results of 2 + 4 to ensure the pidfile it
acquired is the same as the one on disk. With this change, it would
be safe for QEMU to delete the pidfile on exit.

Regards,
Daniel

[1] See the equiv libvirt logic for pidfile acquisition in
     https://libvirt.org/git/?p=libvirt.git;a=blob;f=src/util/virpidfile.c;h=58ab29f77f2cfb8583447112dae77a07446bc627;hb=HEAD#l384
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

  reply	other threads:[~2018-02-13 16:29 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-09 19:12 [Qemu-devel] QEMU leaves pidfile behind on exit Shaun Reitan
2018-02-13 16:28 ` Daniel P. Berrangé [this message]
2018-02-13 19:35   ` Laszlo Ersek
2018-02-14  8:46     ` Daniel P. Berrangé

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=20180213162857.GV573@redhat.com \
    --to=berrange@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=shaun.reitan@ndchost.com \
    /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).