4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.8 2008/02/15 20:52:36 james
14 * *** empty log message ***
16 * Revision 1.7 2008/02/15 19:51:30 james
17 * *** empty log message ***
19 * Revision 1.6 2008/02/15 19:09:00 james
20 * *** empty log message ***
22 * Revision 1.5 2008/02/15 18:26:49 james
23 * *** empty log message ***
25 * Revision 1.4 2008/02/15 18:16:48 james
26 * *** empty log message ***
28 * Revision 1.3 2008/02/15 18:16:35 james
29 * *** empty log message ***
31 * Revision 1.2 2008/02/15 16:48:56 james
32 * *** empty log message ***
34 * Revision 1.1 2008/02/15 15:09:17 james
35 * *** empty log message ***
43 #include <sys/types.h>
60 Filelist *fl = (Filelist *) malloc (sizeof (Filelist));
68 filelist_remove (Filelist * fl, Filelist_ent * fle)
72 for (ep = &fl->head; *ep; ep = &((*ep)->next))
86 filelist_add (Filelist * fl, char *fn)
91 if (i >= FILE_LIST_MAX_LEN)
94 for (fle = fl->head; fle; fle = fle->next)
95 if (!strcmp (fle->name, fn))
98 fle = malloc (sizeof (Filelist_ent));
100 strcpy (fle->name, fn);
102 fle->next = fl->head;
107 filelist_free (Filelist * fl)
110 filelist_remove (fl, fl->head);
115 filelist_print (Filelist * fl, FILE * f)
120 fprintf (f, "(empty list)\n");
123 for (fle = fl->head; fle; fle = fle->next)
124 fprintf (f, "%s\n", fle->name);
134 static int uuid = -1, ugid;
139 if (pw = getpwnam ("uucp"))
149 return fchown (fd, uuid, ugid);
153 lockfile_make (char *name)
155 char buf[1024], tmpfn[1024];
160 strcpy (tmpfn, name);
162 ptr = rindex (tmpfn, '/');
168 ptr += sprintf (ptr, "LTMP.%d", getpid ());
171 i = sprintf (buf, "%10d\n", getpid ());
174 fd = open (tmpfn, O_WRONLY | O_CREAT | O_TRUNC, 0444);
192 if (link (tmpfn, name) < 0)
204 lockfile_add_places (Filelist * fl, char *leaf)
209 { "/var/lock/uucp", "/var/spool/lock", "/var/spool/uucp", "/etc/locks",
210 "/usr/spool/uucp", "/var/spool/locks", "/usr/spool/lock",
211 "/usr/spool/locks", "/usr/spool/uucp/LCK"
215 for (i = 0; i < (sizeof (lock_dirs) / sizeof (char *)); ++i)
217 if (stat (lock_dirs[i], &stbuf))
219 strcpy (buf, lock_dirs[i]);
222 filelist_add (fl, buf);
228 do_tedious_mangling (Filelist * fl, char *buf, char *ptr, char inv, int lower)
232 if (lower && (*ptr >= 'A') && (*ptr <= 'Z'))
239 lockfile_add_places (fl, buf);
244 lockfile_regularize_and_add (Filelist * fl, char *leaf)
246 char buf[1024] = "LCK..";
257 do_tedious_mangling (fl, buf, ptr, '_', 0);
259 do_tedious_mangling (fl, buf, ptr, '_', 1);
261 do_tedious_mangling (fl, buf, ptr, '.', 0);
263 do_tedious_mangling (fl, buf, ptr, '.', 1);
267 lockfile_add_name_from_path (Filelist * fl, char *file)
274 lockfile_regularize_and_add (fl, ptr);
276 if (!strncmp (ptr, "dev/", 4))
279 lockfile_regularize_and_add (fl, ptr);
285 lockfile_add_name_from_dev (Filelist * fl, dev_t dev)
288 sprintf (buf, "LCK.%03d.%03d", major (dev), minor (dev));
289 lockfile_add_places (fl, buf);
293 lockfile_check_dir_for_dev (Filelist * fl, char *dir, dev_t dev)
296 struct stat ent_stat;
305 while ((de = readdir (d)))
308 strcat (buf, de->d_name);
310 if (stat (buf, &ent_stat))
312 if (!S_ISCHR (ent_stat.st_mode))
314 if (ent_stat.st_rdev != dev)
317 lockfile_add_name_from_path (fl, buf);
324 lockfile_make_list (char *device)
326 struct stat dev_stat;
327 Filelist *ret = NULL;
330 if (stat (device, &dev_stat))
332 if (!S_ISCHR (dev_stat.st_mode))
335 ret = filelist_new ();
337 lockfile_add_name_from_dev (ret, dev_stat.st_rdev);
339 lockfile_add_name_from_path (ret, device);
341 lockfile_check_dir_for_dev (ret, "/dev/", dev_stat.st_rdev);
342 lockfile_check_dir_for_dev (ret, "/dev/usb/", dev_stat.st_rdev);
343 lockfile_check_dir_for_dev (ret, "/dev/tts/", dev_stat.st_rdev);
349 remove_stale_lock (char *path)
356 fd = open (path, O_RDONLY);
360 length = read (fd, apid, sizeof (apid) - 1);
366 if (length == sizeof (pid) || sscanf (apid, "%d", &pid) != 1 || pid == 0)
368 pid = *((int *) apid);
371 "compiled with ascii locks, found binary lock file (length=%d, pid=%d)!",
379 "compiled with binary locks, found ascii lock file (length=%d, pid=%d)!",
386 if ((kill (pid, 0) < 0) && (errno == ESRCH))
388 fprintf (stderr, "removing stale lock file %s\n", path);
395 lockfile_remove_stale (Filelist * fl)
400 for (fle = fl->head; fle; fle = fle->next)
402 if (stat (fle->name, &buf))
404 remove_stale_lock (fle->name);
412 lockfile_lock (Filelist * fl)
417 ret = filelist_new ();
419 lockfile_remove_stale (fl);
421 for (fle = fl->head; fle; fle = fle->next)
423 if (lockfile_make (fle->name))
425 fprintf (stderr, "Failed to get lockfile %s\n", fle->name);
429 filelist_add (ret, fle->name);
436 lockfile_unlock (Filelist * fl)
441 unlink (fl->head->name);
442 filelist_remove (fl, fl->head);
447 /* If we have a passive lock, check noone has an */
448 /* active one, returns 1 if he does, 0 if he doesnt */
450 serial_lock_check (Serial_lock * l)
455 struct timeval now, dif;
457 if (l->mode == SERIAL_LOCK_ACTIVE)
460 for (fle = l->locks_to_check; fle; fle = fle->next)
462 if (!stat (fle->name, &buf))
469 getimeofday (&now, NULL);
470 timersub (&now, &l->last_stale_purge, &dif);
472 if (dif.tv_sec > STALE_CHECK_INTERVAL)
474 lockfile_remove_stale (l->locks_to_check);
475 l->last_stale_purge = now;
482 serial_lock_free (Serial_lock * l)
489 lockfile_unlock (l->locks_held);
490 filelist_free (l->locks_held);
493 if (l->locks_to_check)
495 filelist_free (l->locks_to_check);
503 serial_lock_new (char *dev, int mode)
505 Filelist *fl = lockfile_make_list (dev);
511 l->locks_to_check = fl;
512 l->locks_held = NULL;
513 memset (&l->last_stale_purge, 0, sizeof (l->last_stale_purge));
515 if (mode == SERIAL_LOCK_PASSIVE)
518 l->locks_held = lockfile_lock (l->locks_to_check);
521 serial_lock_free (l);
531 main (int argc, char *argv[])
533 Filelist *fl = lockfile_make_list ("/dev/ttyS0");
537 filelist_print (fl, stdout);
539 fll = lockfile_lock (fl);
541 filelist_print (fll, stdout);