4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.7 2008/02/15 19:51:30 james
14 * *** empty log message ***
16 * Revision 1.6 2008/02/15 19:09:00 james
17 * *** empty log message ***
19 * Revision 1.5 2008/02/15 18:26:49 james
20 * *** empty log message ***
22 * Revision 1.4 2008/02/15 18:16:48 james
23 * *** empty log message ***
25 * Revision 1.3 2008/02/15 18:16:35 james
26 * *** empty log message ***
28 * Revision 1.2 2008/02/15 16:48:56 james
29 * *** empty log message ***
31 * Revision 1.1 2008/02/15 15:09:17 james
32 * *** empty log message ***
40 #include <sys/types.h>
57 Filelist *fl = (Filelist *) malloc (sizeof (Filelist));
65 filelist_remove (Filelist * fl, Filelist_ent * fle)
69 for (ep = &fl->head; *ep; ep = &((*ep)->next))
83 filelist_add (Filelist * fl, char *fn)
88 if (i >= FILE_LIST_MAX_LEN)
91 for (fle = fl->head; fle; fle = fle->next)
92 if (!strcmp (fle->name, fn))
95 fle = malloc (sizeof (Filelist_ent));
97 strcpy (fle->name, fn);
104 filelist_free (Filelist * fl)
107 filelist_remove (fl, fl->head);
112 filelist_print (Filelist * fl, FILE * f)
117 fprintf (f, "(empty list)\n");
120 for (fle = fl->head; fle; fle = fle->next)
121 fprintf (f, "%s\n", fle->name);
131 static int uuid = -1, ugid;
136 if (pw = getpwnam ("uucp"))
146 return fchown (fd, uuid, ugid);
150 lockfile_make (char *name)
152 char buf[1024], tmpfn[1024];
157 strcpy (tmpfn, name);
159 ptr = rindex (tmpfn, '/');
165 ptr += sprintf (ptr, "LTMP.%d", getpid ());
168 i = sprintf (buf, "%10d\n", getpid ());
171 fd = open (tmpfn, O_WRONLY | O_CREAT | O_TRUNC, 0444);
189 if (link (tmpfn, name) < 0)
201 lockfile_add_places (Filelist * fl, char *leaf)
206 { "/var/lock/uucp", "/var/spool/lock", "/var/spool/uucp", "/etc/locks",
207 "/usr/spool/uucp", "/var/spool/locks", "/usr/spool/lock",
208 "/usr/spool/locks", "/usr/spool/uucp/LCK"
212 for (i = 0; i < (sizeof (lock_dirs) / sizeof (char *)); ++i)
214 if (stat (lock_dirs[i], &stbuf))
216 strcpy (buf, lock_dirs[i]);
219 filelist_add (fl, buf);
225 do_tedious_mangling (Filelist * fl, char *buf, char *ptr, char inv, int lower)
229 if (lower && (*ptr >= 'A') && (*ptr <= 'Z'))
236 lockfile_add_places (fl, buf);
241 lockfile_regularize_and_add (Filelist * fl, char *leaf)
243 char buf[1024] = "LCK..";
254 do_tedious_mangling (fl, buf, ptr, '_', 0);
256 do_tedious_mangling (fl, buf, ptr, '_', 1);
258 do_tedious_mangling (fl, buf, ptr, '.', 0);
260 do_tedious_mangling (fl, buf, ptr, '.', 1);
264 lockfile_add_name_from_path (Filelist * fl, char *file)
271 lockfile_regularize_and_add (fl, ptr);
273 if (!strncmp (ptr, "dev/", 4))
276 lockfile_regularize_and_add (fl, ptr);
282 lockfile_add_name_from_dev (Filelist * fl, dev_t dev)
285 sprintf (buf, "LCK.%03d.%03d", major (dev), minor (dev));
286 lockfile_add_places (fl, buf);
290 lockfile_check_dir_for_dev (Filelist * fl, char *dir, dev_t dev)
293 struct stat ent_stat;
302 while ((de = readdir (d)))
305 strcat (buf, de->d_name);
307 if (stat (buf, &ent_stat))
309 if (!S_ISCHR (ent_stat.st_mode))
311 if (ent_stat.st_rdev != dev)
314 lockfile_add_name_from_path (fl, buf);
321 lockfile_make_list (char *device)
323 struct stat dev_stat;
324 Filelist *ret = NULL;
327 if (stat (device, &dev_stat))
329 if (!S_ISCHR (dev_stat.st_mode))
332 ret = filelist_new ();
334 lockfile_add_name_from_dev (ret, dev_stat.st_rdev);
336 lockfile_add_name_from_path (ret, device);
338 lockfile_check_dir_for_dev (ret, "/dev/", dev_stat.st_rdev);
339 lockfile_check_dir_for_dev (ret, "/dev/usb/", dev_stat.st_rdev);
340 lockfile_check_dir_for_dev (ret, "/dev/tts/", dev_stat.st_rdev);
346 remove_stale_lock (char *path)
353 fd = open (path, O_RDONLY);
357 length = read (fd, apid, sizeof (apid) - 1);
363 if (length == sizeof (pid) || sscanf (apid, "%d", &pid) != 1 || pid == 0)
365 pid = *((int *) apid);
368 "compiled with ascii locks, found binary lock file (length=%d, pid=%d)!",
376 "compiled with binary locks, found ascii lock file (length=%d, pid=%d)!",
383 if ((kill (pid, 0) < 0) && (errno == ESRCH))
385 fprintf (stderr, "removing stale lock file %s\n", path);
392 lockfile_remove_stale (Filelist * fl)
397 for (fle = fl->head; fle; fle = fle->next)
399 if (stat (fle->name, &buf))
401 remove_stale_lock (fle->name);
409 lockfile_lock (Filelist * fl)
414 ret = filelist_new ();
416 lockfile_remove_stale (fl);
418 for (fle = fl->head; fle; fle = fle->next)
420 if (lockfile_make (fle->name))
422 fprintf (stderr, "Failed to get lockfile %s\n", fle->name);
426 filelist_add (ret, fle->name);
433 lockfile_unlock (Filelist * fl)
438 unlink (fl->head->name);
439 filelist_remove (fl, fl->head);
444 /* If we have a passive lock, check noone has an */
445 /* active one, returns 1 if he does, 0 if he doesnt */
447 serial_lock_check (Serial_lock * l)
452 struct timeval now, dif;
454 if (l->mode == SERIAL_LOCK_ACTIVE)
457 for (fle = l->locks_to_check; fle; fle = fle->next)
459 if (!stat (fle->name, &buf))
466 getimeofday (&now, NULL);
467 timersub (&now, &l->last_stale_purge, &dif);
469 if (dif.tv_sec > STALE_CHECK_INTERVAL)
471 lockfile_remove_stale (l->locks_to_check);
472 l->last_stale_purge = now;
479 serial_lock_free (Serial_lock * l)
486 lockfile_unlock (l->locks_held);
487 filelist_free (l->locks_held);
490 if (l->locks_to_check)
492 filelist_free (l->locks_to_check);
500 serial_lock_new (char *dev, int mode)
502 Filelist *fl = lockfile_make_list (dev);
508 l->locks_to_check = fl;
509 l->locks_held = NULL;
510 memset (&l->last_stale_purge, 0, sizeof (l->last_stale_purge));
512 if (mode == SERIAL_LOCK_PASSIVE)
515 l->locks_held = lockfile_lock (l->locks_to_check);
518 serial_lock_free (l);
528 main (int argc, char *argv[])
530 Filelist *fl = lockfile_make_list ("/dev/ttyS0");
534 filelist_print (fl, stdout);
536 fll = lockfile_lock (fl);
538 filelist_print (fll, stdout);