X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=sympathy.git;a=blobdiff_plain;f=src%2Flockfile.c;h=ce37079bf4eff7b3b54c540f50ccb6be4d49a00c;hp=7a2849b6209ddacdf8906ac61cd7fa35c5228834;hb=3e72a1f6fc28777c26e4fb109867bd2a3c7b89b0;hpb=9bd6ab8d0e7bbb8a0a6ae35815bfc51f8e51f61f diff --git a/src/lockfile.c b/src/lockfile.c index 7a2849b..ce37079 100644 --- a/src/lockfile.c +++ b/src/lockfile.c @@ -10,6 +10,18 @@ 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 *** * @@ -30,6 +42,8 @@ static char rcsid[] = "$Id$"; #define LOCK_ASCII #undef LOCK_BINARY +#define STALE_CHECK_INTERVAL 10 + #include #include #include @@ -42,6 +56,8 @@ static char rcsid[] = "$Id$"; #include #include #include +#include +#include #include "lockfile.h" @@ -199,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; @@ -424,7 +439,104 @@ lockfile_lock (Filelist * fl) return ret; } -#if 1 +void +lockfile_unlock (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_ent *fle; + int locks_found = 0; + struct stat buf; + struct timeval now, dif; + + if (l->mode == SERIAL_LOCK_ACTIVE) + return 0; + + 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; + } + + return 1; +} + +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 0 int main (int argc, char *argv[]) {