* [Qemu-devel] bug writing pidfile under unix (and fix)
@ 2008-11-07 17:02 Jim Bailey
2008-11-07 17:56 ` Paul Brook
2008-11-11 21:05 ` Anthony Liguori
0 siblings, 2 replies; 6+ messages in thread
From: Jim Bailey @ 2008-11-07 17:02 UTC (permalink / raw)
To: qemu-devel
Hello,
In qemu_create_pidfile (osdep.c:229) the current pid and a newline is
written to the pidfile. However, the pidfile isn't truncated, so if it
is longer than the length of the pid and the newline character you get
trailing junk that can really mess up scripts.
I noticed this when going from a 5 digit pid to a 3 digit pid, so it
can happen in regular operation, especially if the OS randomizes pids.
Truncating the file fixes the bug.
dgym
*** osdep.c.orig Fri Nov 7 16:56:12 2008
--- osdep.c Fri Nov 7 16:58:49 2008
***************
*** 236,241 ****
--- 236,243 ----
len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
if (write(fd, buffer, len) != len)
return -1;
+
+ ftruncate(fd, len);
#else
HANDLE file;
DWORD flags;
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Qemu-devel] bug writing pidfile under unix (and fix)
2008-11-07 17:02 [Qemu-devel] bug writing pidfile under unix (and fix) Jim Bailey
@ 2008-11-07 17:56 ` Paul Brook
2008-11-07 18:08 ` Jim Bailey
2008-11-07 20:15 ` Anthony Liguori
2008-11-11 21:05 ` Anthony Liguori
1 sibling, 2 replies; 6+ messages in thread
From: Paul Brook @ 2008-11-07 17:56 UTC (permalink / raw)
To: qemu-devel; +Cc: Jim Bailey
On Friday 07 November 2008, Jim Bailey wrote:
> Hello,
>
> In qemu_create_pidfile (osdep.c:229) the current pid and a newline is
> written to the pidfile. However, the pidfile isn't truncated, so if it
> is longer than the length of the pid and the newline character you get
> trailing junk that can really mess up scripts.
Shouldn't we just open with O_TRUNC ?
I also notice that we're not removing the file when we exit.
Paul
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Qemu-devel] bug writing pidfile under unix (and fix)
2008-11-07 17:56 ` Paul Brook
@ 2008-11-07 18:08 ` Jim Bailey
2008-11-07 20:15 ` Anthony Liguori
1 sibling, 0 replies; 6+ messages in thread
From: Jim Bailey @ 2008-11-07 18:08 UTC (permalink / raw)
To: qemu-devel; +Cc: Paul Brook
Hi Paul,
Opening with O_TRUNC causes a problem. If you try and start qemu again
(by accident presumably) it opens the pidfile and then fails to acquire
a lock on it. It then exits with a useful message "Could not acquire
pidfile" and you know that is has just saved you from trashing your
images.
If we open the file with O_TRUNC the contents get discarded before the
lock is checked, so although a second instance wouldn't run, it would
still leave you with an empty pidfile for your still-running first
instance.
I hope that makes sense.
Removing the file on exit would also be a great feature.
dgym
On Fri, 7 Nov 2008 18:56:11 +0100
Paul Brook <paul@codesourcery.com> wrote:
> On Friday 07 November 2008, Jim Bailey wrote:
> > Hello,
> >
> > In qemu_create_pidfile (osdep.c:229) the current pid and a newline is
> > written to the pidfile. However, the pidfile isn't truncated, so if it
> > is longer than the length of the pid and the newline character you get
> > trailing junk that can really mess up scripts.
>
> Shouldn't we just open with O_TRUNC ?
>
> I also notice that we're not removing the file when we exit.
>
> Paul
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Qemu-devel] bug writing pidfile under unix (and fix)
2008-11-07 17:56 ` Paul Brook
2008-11-07 18:08 ` Jim Bailey
@ 2008-11-07 20:15 ` Anthony Liguori
2008-11-07 20:33 ` Jim Bailey
1 sibling, 1 reply; 6+ messages in thread
From: Anthony Liguori @ 2008-11-07 20:15 UTC (permalink / raw)
To: qemu-devel; +Cc: Jim Bailey
Paul Brook wrote:
> On Friday 07 November 2008, Jim Bailey wrote:
>
>> Hello,
>>
>> In qemu_create_pidfile (osdep.c:229) the current pid and a newline is
>> written to the pidfile. However, the pidfile isn't truncated, so if it
>> is longer than the length of the pid and the newline character you get
>> trailing junk that can really mess up scripts.
>>
>
> Shouldn't we just open with O_TRUNC ?
>
Definitely.
> I also notice that we're not removing the file when we exit.
>
It's not terribly useful to remove the file but it couldn't hurt. An
advisory lock is used to ensure that only one process is running at a
given time.
Regards,
Anthony Liguori
> Paul
>
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Qemu-devel] bug writing pidfile under unix (and fix)
2008-11-07 20:15 ` Anthony Liguori
@ 2008-11-07 20:33 ` Jim Bailey
0 siblings, 0 replies; 6+ messages in thread
From: Jim Bailey @ 2008-11-07 20:33 UTC (permalink / raw)
To: qemu-devel
On Fri, 07 Nov 2008 14:15:54 -0600
Anthony Liguori <anthony@codemonkey.ws> wrote:
> Paul Brook wrote:
> > On Friday 07 November 2008, Jim Bailey wrote:
> >
> >> Hello,
> >>
> >> In qemu_create_pidfile (osdep.c:229) the current pid and a newline is
> >> written to the pidfile. However, the pidfile isn't truncated, so if it
> >> is longer than the length of the pid and the newline character you get
> >> trailing junk that can really mess up scripts.
> >>
> >
> > Shouldn't we just open with O_TRUNC ?
> >
>
> Definitely.
No no no, definitely not, please see my earlier comment.
We don't want to wipe the contents of the file until AFTER checking the
advisory lock.
Regards,
dgym
>
> > I also notice that we're not removing the file when we exit.
> >
>
> It's not terribly useful to remove the file but it couldn't hurt. An
> advisory lock is used to ensure that only one process is running at a
> given time.
>
> Regards,
>
> Anthony Liguori
>
> > Paul
> >
> >
> >
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Qemu-devel] bug writing pidfile under unix (and fix)
2008-11-07 17:02 [Qemu-devel] bug writing pidfile under unix (and fix) Jim Bailey
2008-11-07 17:56 ` Paul Brook
@ 2008-11-11 21:05 ` Anthony Liguori
1 sibling, 0 replies; 6+ messages in thread
From: Anthony Liguori @ 2008-11-11 21:05 UTC (permalink / raw)
To: qemu-devel
Jim Bailey wrote:
> Hello,
>
> In qemu_create_pidfile (osdep.c:229) the current pid and a newline is
> written to the pidfile. However, the pidfile isn't truncated, so if it
> is longer than the length of the pid and the newline character you get
> trailing junk that can really mess up scripts.
>
> I noticed this when going from a 5 digit pid to a 3 digit pid, so it
> can happen in regular operation, especially if the OS randomizes pids.
>
> Truncating the file fixes the bug.
>
Needs a Signed-off-by.
Regards,
Anthony Liguori
> dgym
>
>
> *** osdep.c.orig Fri Nov 7 16:56:12 2008
> --- osdep.c Fri Nov 7 16:58:49 2008
> ***************
> *** 236,241 ****
> --- 236,243 ----
> len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
> if (write(fd, buffer, len) != len)
> return -1;
> +
> + ftruncate(fd, len);
> #else
> HANDLE file;
> DWORD flags;
>
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2008-11-11 21:05 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-11-07 17:02 [Qemu-devel] bug writing pidfile under unix (and fix) Jim Bailey
2008-11-07 17:56 ` Paul Brook
2008-11-07 18:08 ` Jim Bailey
2008-11-07 20:15 ` Anthony Liguori
2008-11-07 20:33 ` Jim Bailey
2008-11-11 21:05 ` Anthony Liguori
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).