chiark / gitweb /
*** empty log message ***
[sympathy.git] / src / lockfile.c
index cc0ad4276b164e7c35fdaabc0be04b61dc73f6d5..ce37079bf4eff7b3b54c540f50ccb6be4d49a00c 100644 (file)
@@ -10,6 +10,24 @@ static char rcsid[] = "$Id$";
 
 /*
  * $Log$
+ * Revision 1.9  2008/02/15 23:52:12  james
+ * *** empty log message ***
+ *
+ * Revision 1.8  2008/02/15 20:52:36  james
+ * *** empty log message ***
+ *
+ * Revision 1.7  2008/02/15 19:51:30  james
+ * *** empty log message ***
+ *
+ * Revision 1.6  2008/02/15 19:09:00  james
+ * *** empty log message ***
+ *
+ * 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 ***
+ *
  * Revision 1.3  2008/02/15 18:16:35  james
  * *** empty log message ***
  *
@@ -24,6 +42,8 @@ static char rcsid[] = "$Id$";
 #define LOCK_ASCII
 #undef LOCK_BINARY
 
+#define STALE_CHECK_INTERVAL 10
+
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -35,6 +55,9 @@ static char rcsid[] = "$Id$";
 #include <pwd.h>
 #include <fcntl.h>
 #include <dirent.h>
+#include <errno.h>
+#include <time.h>
+#include <sys/time.h>
 
 #include "lockfile.h"
 
@@ -75,8 +98,9 @@ filelist_add (Filelist * fl, char *fn)
   if (i >= FILE_LIST_MAX_LEN)
     return;
 
-  for (fle=fl->head;fle;fle=fle->next) 
-       if (!strcmp(fle->name,fn)) return;
+  for (fle = fl->head; fle; fle = fle->next)
+    if (!strcmp (fle->name, fn))
+      return;
 
   fle = malloc (sizeof (Filelist_ent));
 
@@ -94,15 +118,17 @@ filelist_free (Filelist * fl)
   free (fl);
 }
 
-void filelist_print(Filelist *fl,FILE *f)
+void
+filelist_print (Filelist * fl, FILE * f)
 {
-Filelist_ent *fle;
-if (!fl) {
-  fprintf(f,"(empty list)\n");
-  return;
-}
-  for (fle=fl->head;fle;fle=fle->next) 
-  fprintf(f,"%s\n",fle->name);
+  Filelist_ent *fle;
+  if (!fl)
+    {
+      fprintf (f, "(empty list)\n");
+      return;
+    }
+  for (fle = fl->head; fle; fle = fle->next)
+    fprintf (f, "%s\n", fle->name);
 }
 
 
@@ -189,8 +215,7 @@ lockfile_add_places (Filelist * fl, char *leaf)
   char *lock_dirs[] =
     { "/var/lock/uucp", "/var/spool/lock", "/var/spool/uucp", "/etc/locks",
     "/usr/spool/uucp", "/var/spool/locks", "/usr/spool/lock",
-      "/usr/spool/locks",
-    "/usr/spool/uucp/LCK"
+    "/usr/spool/locks", "/usr/spool/uucp/LCK", "/var/lock"
   };
   int i;
 
@@ -281,9 +306,10 @@ lockfile_check_dir_for_dev (Filelist * fl, char *dir, dev_t dev)
 
   d = opendir (dir);
 
-  if (!d) return;
+  if (!d)
+    return;
 
-   while ((de = readdir (d)))
+  while ((de = readdir (d)))
     {
       strcpy (buf, dir);
       strcat (buf, de->d_name);
@@ -302,7 +328,7 @@ lockfile_check_dir_for_dev (Filelist * fl, char *dir, dev_t dev)
 }
 
 Filelist *
-lockfile_make_list(char *device)
+lockfile_make_list (char *device)
 {
   struct stat dev_stat;
   Filelist *ret = NULL;
@@ -326,46 +352,202 @@ lockfile_make_list(char *device)
   return ret;
 }
 
-void lockfile_remove_stale(Filelist *fl)
+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);
+    }
+
+
+}
+
+
+Filelist *
+lockfile_lock (Filelist * fl)
 {
+  Filelist *ret;
+  Filelist_ent *fle;
+
+  ret = filelist_new ();
 
+  lockfile_remove_stale (fl);
+
+  for (fle = fl->head; fle; fle = fle->next)
+    {
+      if (lockfile_make (fle->name))
+        {
+          fprintf (stderr, "Failed to get lockfile %s\n", fle->name);
+          filelist_free (ret);
+          return NULL;
+        }
+      filelist_add (ret, fle->name);
+    }
 
+  return ret;
 }
 
+void
+lockfile_unlock (Filelist * fl)
+{
 
-Filelist *lockfile_lock(Filelist *fl)
+  while (fl->head)
+    {
+      unlink (fl->head->name);
+      filelist_remove (fl, fl->head);
+    }
+}
+
+
+/* If we have a passive lock, check noone has an */
+/* active one, returns 1 if he does, 0 if he doesnt */
+int
+serial_lock_check (Serial_lock * l)
 {
-Filelist *ret;
-Filelist_ent *fle;
+  Filelist_ent *fle;
+  int locks_found = 0;
+  struct stat buf;
+  struct timeval now, dif;
 
-ret=filelist_new();
+  if (l->mode == SERIAL_LOCK_ACTIVE)
+    return 0;
 
-lockfile_remove_stale(fl);
+  for (fle = l->locks_to_check->head; fle; fle = fle->next)
+    {
+      if (!stat (fle->name, &buf))
+        locks_found++;
+    }
+
+  if (!locks_found)
+    return 0;
+
+  gettimeofday (&now, NULL);
+  timersub (&now, &l->last_stale_purge, &dif);
+
+  if (dif.tv_sec > STALE_CHECK_INTERVAL)
+    {
+      lockfile_remove_stale (l->locks_to_check);
+      l->last_stale_purge = now;
+    }
 
-for (fle=fl->head;fle;fle=fle->next) {
-       if (lockfile_make(fle->name)) {
-               fprintf(stderr,"Failed to get lockfile %s\n",fle->name);
-               filelist_free(ret);
-               return NULL;
-       }
-       filelist_add(ret,fle->name);
+  return 1;
 }
 
-return ret;
+void
+serial_lock_free (Serial_lock * l)
+{
+  if (!l)
+    return;
+
+  if (l->locks_held)
+    {
+      lockfile_unlock (l->locks_held);
+      filelist_free (l->locks_held);
+    }
+
+  if (l->locks_to_check)
+    {
+      filelist_free (l->locks_to_check);
+    }
+
+  free (l);
+}
+
+
+Serial_lock *
+serial_lock_new (char *dev, int mode)
+{
+  Filelist *fl = lockfile_make_list (dev);
+  Serial_lock *l;
+
+  if (!fl)
+    return NULL;
+
+  l = (Serial_lock *) malloc (sizeof (Serial_lock));
+
+  l->mode = mode;
+  l->locks_to_check = fl;
+  l->locks_held = NULL;
+  memset (&l->last_stale_purge, 0, sizeof (l->last_stale_purge));
+
+  if (mode == SERIAL_LOCK_PASSIVE)
+    return l;
+
+  l->locks_held = lockfile_lock (l->locks_to_check);
+  if (!l->locks_held)
+    {
+      serial_lock_free (l);
+      return NULL;
+    }
+
+  return l;
 }
 
-#if 1
-int main(int argc,char *argv[])
+
+#if 0
+int
+main (int argc, char *argv[])
 {
-Filelist *fl=lockfile_make_list("/dev/ttyS0");
-Filelist *fll;
-Filelist_ent *fle;
+  Filelist *fl = lockfile_make_list ("/dev/ttyS0");
+  Filelist *fll;
+  Filelist_ent *fle;
 
-filelist_print(fl,stdout);
+  filelist_print (fl, stdout);
 
-fll=lockfile_lock(fl);
+  fll = lockfile_lock (fl);
 
-filelist_print(fll,stdout);
+  filelist_print (fll, stdout);
 }
 #endif
-