Skip to contentSkip to conversation

keep the PID file locked for the lifetime of the process

PATCH Superseded Laszlo Ersek 5 participants 8 messages Jan 26, 2012 – Feb 3, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 keep the PID file locked for the lifetime of the process

The lockf() call in qemu_create_pidfile() aims at ensuring mutual exclusion. We shouldn't close the pidfile on success, because that drops the lock as well [1]:

"File locks shall be released on first close by the locking process of any file descriptor for the file."

Coverity may complain again about the leaked file descriptor; let's worry about that later.

[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/lockf.html

---
Please keep me CC'd, I'm not subscribed. Thanks!

 os-posix.c |    1 -
 1 files changed, 0 insertions(+), 1 deletions(-)
os-posix.c +0 -1
index 5c437ca..f4940c8 100644
@@ -348,6 +348,5 @@ int qemu_create_pidfile(const char *filename)
         return -1;
     }
 
-    close(fd);
     return 0;
 }
Signature · 1 line
1.7.1
#1 lore raw
Quoted 7 lines · Laszlo Ersek

The lockf() call in qemu_create_pidfile() aims at ensuring mutual exclusion. We shouldn't close the pidfile on success, because that drops the lock as well [1]:

"File locks shall be released on first close by the locking process of any file descriptor for the file."

Quoted 7 lines

Coverity may complain again about the leaked file descriptor; let's worry about that later.

[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/lockf.html

---

Isn't the normal way to do pidfiles O_CREAT | O_EXCL? It may not work on all NFS versions but putting the pidfile on NFS doesn't really make sense.

Then we can drop the lockf(3) completely.

Stefan

#2 lore raw
Quoted 11 lines · Stefan Hajnoczi
Quoted 10 lines · Laszlo Ersek

The lockf() call in qemu_create_pidfile() aims at ensuring mutual exclusion. We shouldn't close the pidfile on success, because that drops the lock as well [1]:

"File locks shall be released on first close by the locking process of any file descriptor for the file."

Coverity may complain again about the leaked file descriptor; let's worry about that later.

Quoted 6 lines
Quoted 4 lines

[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/lockf.html

---

Isn't the normal way to do pidfiles O_CREAT | O_EXCL?

Yes, it is.

Quoted 5 lines

It may not work
on all NFS versions but putting the pidfile on NFS doesn't really make sense.

Then we can drop the lockf(3) completely.

When you rely on O_EXCL to ensure mutual exclusion, and an abruptly terminated process leaves the lockfile lying around, then the user has to clean it up manually before starting the next instance (and double check if the pid file is in fact stale or not). I'm personally OK with that, but I reckoned the qemu code tried to avoid that intentionally. Record locks can't remain stale when the process dies.

Laszlo

#3 lore raw
In reply to Stefan Hajnoczi
Quoted 13 lines · Stefan Hajnoczi
Quoted 12 lines · Laszlo Ersek

The lockf() call in qemu_create_pidfile() aims at ensuring mutual exclusion. We shouldn't close the pidfile on success, because that drops the lock as well [1]:

"File locks shall be released on first close by the locking process of any file descriptor for the file."

Coverity may complain again about the leaked file descriptor; let's worry about that later.

[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/lockf.html

Quoted 8 lines
Quoted 2 lines

---

Isn't the normal way to do pidfiles O_CREAT | O_EXCL? It may not work on all NFS versions but putting the pidfile on NFS doesn't really make sense.

Then we can drop the lockf(3) completely.

IMHO it is preferable to use lockf because that makes sure you are crash safe, so you don't get later bogus startup failures due to stale pidfiles

Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
Reviewed-by on v1 1/1
#4 lore raw
In reply to Laszlo Ersek
Quoted 11 lines · Laszlo Ersek

The lockf() call in qemu_create_pidfile() aims at ensuring mutual exclusion. We shouldn't close the pidfile on success, because that drops the lock as well [1]:

"File locks shall be released on first close by the locking process of any file descriptor for the file."

Coverity may complain again about the leaked file descriptor; let's worry about that later.

[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/lockf.html

Broken in commit 1bbd1592 by yours truly %-} Suitable pointer could be added to the commit message.

Quoted 12 lines

---
Please keep me CC'd, I'm not subscribed. Thanks!

 os-posix.c |    1 -
 1 files changed, 0 insertions(+), 1 deletions(-)
os-posix.c +0 -0
index 5c437ca..f4940c8 100644

os-posix.c int qemu_create_pidfile(const char *filename) · line 352

Quoted 6 lines
os-posix.c +0 -1
@@ -348,6 +348,5 @@ int qemu_create_pidfile(const char *filename)
         return -1;
     }
 
-    close(fd);
     return 0;
 }

We intentionally leak fd here. A comment would be nice.

#5 lore raw

PATCH v2 1/1 keep the PID file locked for the lifetime of the process

The lockf() call in qemu_create_pidfile() aims at ensuring mutual exclusion. We shouldn't close the pidfile on success (as introduced by commit 1bbd1592), because that drops the lock as well [1]:

"File locks shall be released on first close by the locking process of any file descriptor for the file."

Coverity may complain again about the leaked file descriptor; let's worry about that later.

v1->v2:
- add reference to 1bbd1592
- explain the intentional fd leak in the source

[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/lockf.html

---
 os-posix.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
os-posix.c +1 -1
index 5c437ca..e3ed497 100644
@@ -348,6 +348,6 @@ int qemu_create_pidfile(const char *filename)
         return -1;
     }
 
-    close(fd);
+    /* keep pidfile open & locked forever */
     return 0;
 }
Signature · 1 line
1.7.1
Reviewed-by on v2 1/1
#6 lore raw
Quoted 10 lines · Laszlo Ersek

The lockf() call in qemu_create_pidfile() aims at ensuring mutual exclusion. We shouldn't close the pidfile on success (as introduced by commit 1bbd1592), because that drops the lock as well [1]:

"File locks shall be released on first close by the locking process of any file descriptor for the file."

Coverity may complain again about the leaked file descriptor; let's worry about that later.

Quoted 7 lines

v1->v2:
- add reference to 1bbd1592
- explain the intentional fd leak in the source

[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/lockf.html

Thanks for cleaning up the mess I made!

#7 lore raw
In reply to Laszlo Ersek
Quoted 7 lines · Laszlo Ersek

The lockf() call in qemu_create_pidfile() aims at ensuring mutual exclusion. We shouldn't close the pidfile on success (as introduced by commit 1bbd1592), because that drops the lock as well [1]:

"File locks shall be released on first close by the locking process of any file descriptor for the file."

Quoted 8 lines

Coverity may complain again about the leaked file descriptor; let's worry about that later.

v1->v2:
- add reference to 1bbd1592
- explain the intentional fd leak in the source

[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/lockf.html

Applied. Thanks.

Regards,

Anthony Liguori

Quoted 18 lines
---
  os-posix.c |    2 +-
  1 files changed, 1 insertions(+), 1 deletions(-)
os-posix.c +1 -1
index 5c437ca..e3ed497 100644
@@ -348,6 +348,6 @@ int qemu_create_pidfile(const char *filename)
          return -1;
      }
 
-    close(fd);
+    /* keep pidfile open&  locked forever */
      return 0;
  }