summaryrefslogtreecommitdiffstats
path: root/polyp/util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2004-11-21 13:18:56 +0000
committerLennart Poettering <lennart@poettering.net>2004-11-21 13:18:56 +0000
commitf2b11dbef8bb638ff57baf3225864ed732164fe7 (patch)
tree6e3646203db735ab2e603b03d7000aba6eb017be /polyp/util.c
parentfa751e537db75108f9a1597d83a4e1093173fc28 (diff)
* PID and lock file fixes
git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@299 fefdeb5f-60dc-0310-8127-8f9354f1896f
Diffstat (limited to 'polyp/util.c')
-rw-r--r--polyp/util.c46
1 files changed, 35 insertions, 11 deletions
diff --git a/polyp/util.c b/polyp/util.c
index b01b80a1..87beafb3 100644
--- a/polyp/util.c
+++ b/polyp/util.c
@@ -646,26 +646,50 @@ char* pa_strip_nl(char *s) {
/* Create a temporary lock file and lock it. */
int pa_lock_lockfile(const char *fn) {
- int fd;
+ int fd = -1;
assert(fn);
- if ((fd = open(fn, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR)) < 0) {
- pa_log(__FILE__": failed to create lock file '%s': %s\n", fn, strerror(errno));
- goto fail;
- }
+ for (;;) {
+ struct stat st;
+
+ if ((fd = open(fn, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR)) < 0) {
+ pa_log(__FILE__": failed to create lock file '%s': %s\n", fn, strerror(errno));
+ goto fail;
+ }
+
+ if (pa_lock_fd(fd, 1) < 0) {
+ pa_log(__FILE__": failed to lock file '%s'.\n", fn);
+ goto fail;
+ }
+
+ if (fstat(fd, &st) < 0) {
+ pa_log(__FILE__": failed to fstat() file '%s'.\n", fn);
+ goto fail;
+ }
- if (pa_lock_fd(fd, 1) < 0)
- pa_log(__FILE__": failed to lock file '%s'.\n", fn);
- goto fail;
+ /* Check wheter the file has been removed meanwhile. When yes, restart this loop, otherwise, we're done */
+ if (st.st_nlink >= 1)
+ break;
+
+ if (pa_lock_fd(fd, 0) < 0) {
+ pa_log(__FILE__": failed to unlock file '%s'.\n", fn);
+ goto fail;
+ }
+
+ if (close(fd) < 0) {
+ pa_log(__FILE__": failed to close file '%s'.\n", fn);
+ goto fail;
+ }
+ fd = -1;
+ }
+
return fd;
fail:
- if (fd >= 0) {
- unlink(fn);
+ if (fd >= 0)
close(fd);
- }
return -1;
}