aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lockfile.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/lockfile.c b/src/lockfile.c
index aa39f7b..7a2849b 100644
--- a/src/lockfile.c
+++ b/src/lockfile.c
@@ -10,6 +10,9 @@ static char rcsid[] = "$Id$";
/*
* $Log$
+ * Revision 1.5 2008/02/15 18:26:49 james
+ * *** empty log message ***
+ *
* Revision 1.4 2008/02/15 18:16:48 james
* *** empty log message ***
*
@@ -38,6 +41,7 @@ static char rcsid[] = "$Id$";
#include <pwd.h>
#include <fcntl.h>
#include <dirent.h>
+#include <errno.h>
#include "lockfile.h"
@@ -333,9 +337,64 @@ lockfile_make_list (char *device)
return ret;
}
+static void
+remove_stale_lock (char *path)
+{
+ int fd;
+ int pid;
+ char apid[20];
+ int length;
+
+ fd = open (path, O_RDONLY);
+ if (fd < 0)
+ return;
+
+ length = read (fd, apid, sizeof (apid) - 1);
+ if (length < 0)
+ length = 0;
+ apid[length] = 0;
+
+ pid = 0;
+ if (length == sizeof (pid) || sscanf (apid, "%d", &pid) != 1 || pid == 0)
+ {
+ pid = *((int *) apid);
+#ifdef LOCK_ASCII
+ fprintf (stderr,
+ "compiled with ascii locks, found binary lock file (length=%d, pid=%d)!",
+ length, pid);
+#endif
+ }
+#ifdef LOCK_BINARY
+ else
+ {
+ fprintf (stderr,
+ "compiled with binary locks, found ascii lock file (length=%d, pid=%d)!",
+ length, pid);
+ }
+#endif
+
+ close (fd);
+
+ if ((kill (pid, 0) < 0) && (errno == ESRCH))
+ {
+ fprintf (stderr, "removing stale lock file %s\n", path);
+ unlink (path);
+ }
+
+}
+
void
lockfile_remove_stale (Filelist * fl)
{
+ Filelist_ent *fle;
+ struct stat buf;
+
+ for (fle = fl->head; fle; fle = fle->next)
+ {
+ if (stat (fle->name, &buf))
+ continue;
+ remove_stale_lock (fle->name);
+ }
}