4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.10 2008/03/02 10:37:56 james
14 * *** empty log message ***
16 * Revision 1.9 2008/02/15 23:52:12 james
17 * *** empty log message ***
19 * Revision 1.8 2008/02/15 20:52:36 james
20 * *** empty log message ***
22 * Revision 1.7 2008/02/15 19:51:30 james
23 * *** empty log message ***
25 * Revision 1.6 2008/02/15 19:09:00 james
26 * *** empty log message ***
28 * Revision 1.5 2008/02/15 18:26:49 james
29 * *** empty log message ***
31 * Revision 1.4 2008/02/15 18:16:48 james
32 * *** empty log message ***
34 * Revision 1.3 2008/02/15 18:16:35 james
35 * *** empty log message ***
37 * Revision 1.2 2008/02/15 16:48:56 james
38 * *** empty log message ***
40 * Revision 1.1 2008/02/15 15:09:17 james
41 * *** empty log message ***
48 #define STALE_CHECK_INTERVAL 10
51 #include <sys/types.h>
70 Filelist *fl = (Filelist *) malloc (sizeof (Filelist));
78 filelist_remove (Filelist * fl, Filelist_ent * fle)
82 for (ep = &fl->head; *ep; ep = &((*ep)->next))
96 filelist_add (Filelist * fl, char *fn)
101 if (i >= FILE_LIST_MAX_LEN)
104 for (fle = fl->head; fle; fle = fle->next)
105 if (!strcmp (fle->name, fn))
108 fle = malloc (sizeof (Filelist_ent));
110 strcpy (fle->name, fn);
112 fle->next = fl->head;
117 filelist_free (Filelist * fl)
120 filelist_remove (fl, fl->head);
125 filelist_print (Filelist * fl, FILE * f)
129 fprintf (f, "(empty list)\n");
132 for (fle = fl->head; fle; fle = fle->next)
133 fprintf (f, "%s\n", fle->name);
143 static int uuid = -1, ugid;
147 if (pw = getpwnam ("uucp")) {
154 return fchown (fd, uuid, ugid);
158 lockfile_make (char *name)
160 char buf[1024], tmpfn[1024];
165 strcpy (tmpfn, name);
167 ptr = rindex (tmpfn, '/');
173 ptr += sprintf (ptr, "LTMP.%d", getpid ());
176 i = sprintf (buf, "%10d\n", getpid ());
179 fd = open (tmpfn, O_WRONLY | O_CREAT | O_TRUNC, 0444);
187 if (chown_uucp (fd)) {
195 if (link (tmpfn, name) < 0) {
206 lockfile_add_places (Filelist * fl, char *leaf)
211 { "/var/lock/uucp", "/var/spool/lock", "/var/spool/uucp", "/etc/locks",
212 "/usr/spool/uucp", "/var/spool/locks", "/usr/spool/lock",
213 "/usr/spool/locks", "/usr/spool/uucp/LCK", "/var/lock"
217 for (i = 0; i < (sizeof (lock_dirs) / sizeof (char *)); ++i) {
218 if (stat (lock_dirs[i], &stbuf))
220 strcpy (buf, lock_dirs[i]);
223 filelist_add (fl, buf);
229 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)) {
278 lockfile_regularize_and_add (fl, ptr);
284 lockfile_add_name_from_dev (Filelist * fl, dev_t dev)
287 sprintf (buf, "LCK.%03d.%03d", major (dev), minor (dev));
288 lockfile_add_places (fl, buf);
292 lockfile_check_dir_for_dev (Filelist * fl, char *dir, dev_t dev)
295 struct stat ent_stat;
304 while ((de = readdir (d))) {
306 strcat (buf, de->d_name);
308 if (stat (buf, &ent_stat))
310 if (!S_ISCHR (ent_stat.st_mode))
312 if (ent_stat.st_rdev != dev)
315 lockfile_add_name_from_path (fl, buf);
322 lockfile_make_list (char *device)
324 struct stat dev_stat;
325 Filelist *ret = NULL;
328 if (stat (device, &dev_stat))
330 if (!S_ISCHR (dev_stat.st_mode))
333 ret = filelist_new ();
335 lockfile_add_name_from_dev (ret, dev_stat.st_rdev);
337 lockfile_add_name_from_path (ret, device);
339 lockfile_check_dir_for_dev (ret, "/dev/", dev_stat.st_rdev);
340 lockfile_check_dir_for_dev (ret, "/dev/usb/", dev_stat.st_rdev);
341 lockfile_check_dir_for_dev (ret, "/dev/tts/", dev_stat.st_rdev);
347 remove_stale_lock (char *path)
354 fd = open (path, O_RDONLY);
358 length = read (fd, apid, sizeof (apid) - 1);
364 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)!",
375 "compiled with binary locks, found ascii lock file (length=%d, pid=%d)!",
382 if ((kill (pid, 0) < 0) && (errno == ESRCH)) {
383 fprintf (stderr, "removing stale lock file %s\n", path);
390 lockfile_remove_stale (Filelist * fl)
395 for (fle = fl->head; fle; fle = fle->next) {
396 if (stat (fle->name, &buf))
398 remove_stale_lock (fle->name);
406 lockfile_lock (Filelist * fl)
411 ret = filelist_new ();
413 lockfile_remove_stale (fl);
415 for (fle = fl->head; fle; fle = fle->next) {
416 if (lockfile_make (fle->name)) {
417 fprintf (stderr, "Failed to get lockfile %s\n", fle->name);
421 filelist_add (ret, fle->name);
428 lockfile_unlock (Filelist * fl)
432 unlink (fl->head->name);
433 filelist_remove (fl, fl->head);
438 /* If we have a passive lock, check noone has an */
439 /* active one, returns 1 if he does, 0 if he doesnt */
441 serial_lock_check (Serial_lock * l)
446 struct timeval now, dif;
448 if (l->mode == SERIAL_LOCK_ACTIVE)
451 for (fle = l->locks_to_check->head; fle; fle = fle->next) {
452 if (!stat (fle->name, &buf))
459 gettimeofday (&now, NULL);
460 timersub (&now, &l->last_stale_purge, &dif);
462 if (dif.tv_sec > STALE_CHECK_INTERVAL) {
463 lockfile_remove_stale (l->locks_to_check);
464 l->last_stale_purge = now;
471 serial_lock_free (Serial_lock * l)
477 lockfile_unlock (l->locks_held);
478 filelist_free (l->locks_held);
481 if (l->locks_to_check) {
482 filelist_free (l->locks_to_check);
490 serial_lock_new (char *dev, int mode)
492 Filelist *fl = lockfile_make_list (dev);
498 l = (Serial_lock *) malloc (sizeof (Serial_lock));
501 l->locks_to_check = fl;
502 l->locks_held = NULL;
503 memset (&l->last_stale_purge, 0, sizeof (l->last_stale_purge));
505 if (mode == SERIAL_LOCK_PASSIVE)
508 l->locks_held = lockfile_lock (l->locks_to_check);
509 if (!l->locks_held) {
510 serial_lock_free (l);
520 main (int argc, char *argv[])
522 Filelist *fl = lockfile_make_list ("/dev/ttyS0");
526 filelist_print (fl, stdout);
528 fll = lockfile_lock (fl);
530 filelist_print (fll, stdout);