4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.9 2008/02/15 23:52:12 james
14 * *** empty log message ***
16 * Revision 1.8 2008/02/15 20:52:36 james
17 * *** empty log message ***
19 * Revision 1.7 2008/02/15 19:51:30 james
20 * *** empty log message ***
22 * Revision 1.6 2008/02/15 19:09:00 james
23 * *** empty log message ***
25 * Revision 1.5 2008/02/15 18:26:49 james
26 * *** empty log message ***
28 * Revision 1.4 2008/02/15 18:16:48 james
29 * *** empty log message ***
31 * Revision 1.3 2008/02/15 18:16:35 james
32 * *** empty log message ***
34 * Revision 1.2 2008/02/15 16:48:56 james
35 * *** empty log message ***
37 * Revision 1.1 2008/02/15 15:09:17 james
38 * *** empty log message ***
45 #define STALE_CHECK_INTERVAL 10
48 #include <sys/types.h>
67 Filelist *fl = (Filelist *) malloc (sizeof (Filelist));
75 filelist_remove (Filelist * fl, Filelist_ent * fle)
79 for (ep = &fl->head; *ep; ep = &((*ep)->next))
93 filelist_add (Filelist * fl, char *fn)
98 if (i >= FILE_LIST_MAX_LEN)
101 for (fle = fl->head; fle; fle = fle->next)
102 if (!strcmp (fle->name, fn))
105 fle = malloc (sizeof (Filelist_ent));
107 strcpy (fle->name, fn);
109 fle->next = fl->head;
114 filelist_free (Filelist * fl)
117 filelist_remove (fl, fl->head);
122 filelist_print (Filelist * fl, FILE * f)
127 fprintf (f, "(empty list)\n");
130 for (fle = fl->head; fle; fle = fle->next)
131 fprintf (f, "%s\n", fle->name);
141 static int uuid = -1, ugid;
146 if (pw = getpwnam ("uucp"))
156 return fchown (fd, uuid, ugid);
160 lockfile_make (char *name)
162 char buf[1024], tmpfn[1024];
167 strcpy (tmpfn, name);
169 ptr = rindex (tmpfn, '/');
175 ptr += sprintf (ptr, "LTMP.%d", getpid ());
178 i = sprintf (buf, "%10d\n", getpid ());
181 fd = open (tmpfn, O_WRONLY | O_CREAT | O_TRUNC, 0444);
199 if (link (tmpfn, name) < 0)
211 lockfile_add_places (Filelist * fl, char *leaf)
216 { "/var/lock/uucp", "/var/spool/lock", "/var/spool/uucp", "/etc/locks",
217 "/usr/spool/uucp", "/var/spool/locks", "/usr/spool/lock",
218 "/usr/spool/locks", "/usr/spool/uucp/LCK", "/var/lock"
222 for (i = 0; i < (sizeof (lock_dirs) / sizeof (char *)); ++i)
224 if (stat (lock_dirs[i], &stbuf))
226 strcpy (buf, lock_dirs[i]);
229 filelist_add (fl, buf);
235 do_tedious_mangling (Filelist * fl, char *buf, char *ptr, char inv, int lower)
239 if (lower && (*ptr >= 'A') && (*ptr <= 'Z'))
246 lockfile_add_places (fl, buf);
251 lockfile_regularize_and_add (Filelist * fl, char *leaf)
253 char buf[1024] = "LCK..";
264 do_tedious_mangling (fl, buf, ptr, '_', 0);
266 do_tedious_mangling (fl, buf, ptr, '_', 1);
268 do_tedious_mangling (fl, buf, ptr, '.', 0);
270 do_tedious_mangling (fl, buf, ptr, '.', 1);
274 lockfile_add_name_from_path (Filelist * fl, char *file)
281 lockfile_regularize_and_add (fl, ptr);
283 if (!strncmp (ptr, "dev/", 4))
286 lockfile_regularize_and_add (fl, ptr);
292 lockfile_add_name_from_dev (Filelist * fl, dev_t dev)
295 sprintf (buf, "LCK.%03d.%03d", major (dev), minor (dev));
296 lockfile_add_places (fl, buf);
300 lockfile_check_dir_for_dev (Filelist * fl, char *dir, dev_t dev)
303 struct stat ent_stat;
312 while ((de = readdir (d)))
315 strcat (buf, de->d_name);
317 if (stat (buf, &ent_stat))
319 if (!S_ISCHR (ent_stat.st_mode))
321 if (ent_stat.st_rdev != dev)
324 lockfile_add_name_from_path (fl, buf);
331 lockfile_make_list (char *device)
333 struct stat dev_stat;
334 Filelist *ret = NULL;
337 if (stat (device, &dev_stat))
339 if (!S_ISCHR (dev_stat.st_mode))
342 ret = filelist_new ();
344 lockfile_add_name_from_dev (ret, dev_stat.st_rdev);
346 lockfile_add_name_from_path (ret, device);
348 lockfile_check_dir_for_dev (ret, "/dev/", dev_stat.st_rdev);
349 lockfile_check_dir_for_dev (ret, "/dev/usb/", dev_stat.st_rdev);
350 lockfile_check_dir_for_dev (ret, "/dev/tts/", dev_stat.st_rdev);
356 remove_stale_lock (char *path)
363 fd = open (path, O_RDONLY);
367 length = read (fd, apid, sizeof (apid) - 1);
373 if (length == sizeof (pid) || sscanf (apid, "%d", &pid) != 1 || pid == 0)
375 pid = *((int *) apid);
378 "compiled with ascii locks, found binary lock file (length=%d, pid=%d)!",
386 "compiled with binary locks, found ascii lock file (length=%d, pid=%d)!",
393 if ((kill (pid, 0) < 0) && (errno == ESRCH))
395 fprintf (stderr, "removing stale lock file %s\n", path);
402 lockfile_remove_stale (Filelist * fl)
407 for (fle = fl->head; fle; fle = fle->next)
409 if (stat (fle->name, &buf))
411 remove_stale_lock (fle->name);
419 lockfile_lock (Filelist * fl)
424 ret = filelist_new ();
426 lockfile_remove_stale (fl);
428 for (fle = fl->head; fle; fle = fle->next)
430 if (lockfile_make (fle->name))
432 fprintf (stderr, "Failed to get lockfile %s\n", fle->name);
436 filelist_add (ret, fle->name);
443 lockfile_unlock (Filelist * fl)
448 unlink (fl->head->name);
449 filelist_remove (fl, fl->head);
454 /* If we have a passive lock, check noone has an */
455 /* active one, returns 1 if he does, 0 if he doesnt */
457 serial_lock_check (Serial_lock * l)
462 struct timeval now, dif;
464 if (l->mode == SERIAL_LOCK_ACTIVE)
467 for (fle = l->locks_to_check->head; fle; fle = fle->next)
469 if (!stat (fle->name, &buf))
476 gettimeofday (&now, NULL);
477 timersub (&now, &l->last_stale_purge, &dif);
479 if (dif.tv_sec > STALE_CHECK_INTERVAL)
481 lockfile_remove_stale (l->locks_to_check);
482 l->last_stale_purge = now;
489 serial_lock_free (Serial_lock * l)
496 lockfile_unlock (l->locks_held);
497 filelist_free (l->locks_held);
500 if (l->locks_to_check)
502 filelist_free (l->locks_to_check);
510 serial_lock_new (char *dev, int mode)
512 Filelist *fl = lockfile_make_list (dev);
518 l = (Serial_lock *) malloc (sizeof (Serial_lock));
521 l->locks_to_check = fl;
522 l->locks_held = NULL;
523 memset (&l->last_stale_purge, 0, sizeof (l->last_stale_purge));
525 if (mode == SERIAL_LOCK_PASSIVE)
528 l->locks_held = lockfile_lock (l->locks_to_check);
531 serial_lock_free (l);
541 main (int argc, char *argv[])
543 Filelist *fl = lockfile_make_list ("/dev/ttyS0");
547 filelist_print (fl, stdout);
549 fll = lockfile_lock (fl);
551 filelist_print (fll, stdout);