chiark / gitweb /
*** empty log message ***
authorjames <james>
Fri, 15 Feb 2008 18:26:49 +0000 (18:26 +0000)
committerjames <james>
Fri, 15 Feb 2008 18:26:49 +0000 (18:26 +0000)
src/lockfile.c

index aa39f7b3c2f18104b3c30e4dec647cb39cd1411e..7a2849b6209ddacdf8906ac61cd7fa35c5228834 100644 (file)
@@ -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);
+    }
 
 
 }