/*
* $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 ***
+ *
* Revision 1.3 2008/02/15 18:16:35 james
* *** empty log message ***
*
#include <pwd.h>
#include <fcntl.h>
#include <dirent.h>
+#include <errno.h>
#include "lockfile.h"
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));
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);
}
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/locks",
"/usr/spool/uucp/LCK"
};
int i;
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);
}
Filelist *
-lockfile_make_list(char *device)
+lockfile_make_list (char *device)
{
struct stat dev_stat;
Filelist *ret = NULL;
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);
+ }
+}
-Filelist *lockfile_lock(Filelist *fl)
+void
+lockfile_remove_stale (Filelist * fl)
{
-Filelist *ret;
-Filelist_ent *fle;
+ Filelist_ent *fle;
+ struct stat buf;
-ret=filelist_new();
+ for (fle = fl->head; fle; fle = fle->next)
+ {
+ if (stat (fle->name, &buf))
+ continue;
+ remove_stale_lock (fle->name);
+ }
-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;
+
+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;
}
#if 1
-int main(int argc,char *argv[])
+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
-