4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.6 2008/02/15 19:09:00 james
14 * *** empty log message ***
16 * Revision 1.5 2008/02/15 18:26:49 james
17 * *** empty log message ***
19 * Revision 1.4 2008/02/15 18:16:48 james
20 * *** empty log message ***
22 * Revision 1.3 2008/02/15 18:16:35 james
23 * *** empty log message ***
25 * Revision 1.2 2008/02/15 16:48:56 james
26 * *** empty log message ***
28 * Revision 1.1 2008/02/15 15:09:17 james
29 * *** empty log message ***
37 #include <sys/types.h>
54 Filelist *fl = (Filelist *) malloc (sizeof (Filelist));
62 filelist_remove (Filelist * fl, Filelist_ent * fle)
66 for (ep = &fl->head; *ep; ep = &((*ep)->next))
80 filelist_add (Filelist * fl, char *fn)
85 if (i >= FILE_LIST_MAX_LEN)
88 for (fle = fl->head; fle; fle = fle->next)
89 if (!strcmp (fle->name, fn))
92 fle = malloc (sizeof (Filelist_ent));
94 strcpy (fle->name, fn);
101 filelist_free (Filelist * fl)
104 filelist_remove (fl, fl->head);
109 filelist_print (Filelist * fl, FILE * f)
114 fprintf (f, "(empty list)\n");
117 for (fle = fl->head; fle; fle = fle->next)
118 fprintf (f, "%s\n", fle->name);
128 static int uuid = -1, ugid;
133 if (pw = getpwnam ("uucp"))
143 return fchown (fd, uuid, ugid);
147 lockfile_make (char *name)
149 char buf[1024], tmpfn[1024];
154 strcpy (tmpfn, name);
156 ptr = rindex (tmpfn, '/');
162 ptr += sprintf (ptr, "LTMP.%d", getpid ());
165 i = sprintf (buf, "%10d\n", getpid ());
168 fd = open (tmpfn, O_WRONLY | O_CREAT | O_TRUNC, 0444);
186 if (link (tmpfn, name) < 0)
198 lockfile_add_places (Filelist * fl, char *leaf)
203 { "/var/lock/uucp", "/var/spool/lock", "/var/spool/uucp", "/etc/locks",
204 "/usr/spool/uucp", "/var/spool/locks", "/usr/spool/lock",
205 "/usr/spool/locks", "/usr/spool/uucp/LCK"
209 for (i = 0; i < (sizeof (lock_dirs) / sizeof (char *)); ++i)
211 if (stat (lock_dirs[i], &stbuf))
213 strcpy (buf, lock_dirs[i]);
216 filelist_add (fl, buf);
222 do_tedious_mangling (Filelist * fl, char *buf, char *ptr, char inv, int lower)
226 if (lower && (*ptr >= 'A') && (*ptr <= 'Z'))
233 lockfile_add_places (fl, buf);
238 lockfile_regularize_and_add (Filelist * fl, char *leaf)
240 char buf[1024] = "LCK..";
251 do_tedious_mangling (fl, buf, ptr, '_', 0);
253 do_tedious_mangling (fl, buf, ptr, '_', 1);
255 do_tedious_mangling (fl, buf, ptr, '.', 0);
257 do_tedious_mangling (fl, buf, ptr, '.', 1);
261 lockfile_add_name_from_path (Filelist * fl, char *file)
268 lockfile_regularize_and_add (fl, ptr);
270 if (!strncmp (ptr, "dev/", 4))
273 lockfile_regularize_and_add (fl, ptr);
279 lockfile_add_name_from_dev (Filelist * fl, dev_t dev)
282 sprintf (buf, "LCK.%03d.%03d", major (dev), minor (dev));
283 lockfile_add_places (fl, buf);
287 lockfile_check_dir_for_dev (Filelist * fl, char *dir, dev_t dev)
290 struct stat ent_stat;
299 while ((de = readdir (d)))
302 strcat (buf, de->d_name);
304 if (stat (buf, &ent_stat))
306 if (!S_ISCHR (ent_stat.st_mode))
308 if (ent_stat.st_rdev != dev)
311 lockfile_add_name_from_path (fl, buf);
318 lockfile_make_list (char *device)
320 struct stat dev_stat;
321 Filelist *ret = NULL;
324 if (stat (device, &dev_stat))
326 if (!S_ISCHR (dev_stat.st_mode))
329 ret = filelist_new ();
331 lockfile_add_name_from_dev (ret, dev_stat.st_rdev);
333 lockfile_add_name_from_path (ret, device);
335 lockfile_check_dir_for_dev (ret, "/dev/", dev_stat.st_rdev);
336 lockfile_check_dir_for_dev (ret, "/dev/usb/", dev_stat.st_rdev);
337 lockfile_check_dir_for_dev (ret, "/dev/tts/", dev_stat.st_rdev);
343 remove_stale_lock (char *path)
350 fd = open (path, O_RDONLY);
354 length = read (fd, apid, sizeof (apid) - 1);
360 if (length == sizeof (pid) || sscanf (apid, "%d", &pid) != 1 || pid == 0)
362 pid = *((int *) apid);
365 "compiled with ascii locks, found binary lock file (length=%d, pid=%d)!",
373 "compiled with binary locks, found ascii lock file (length=%d, pid=%d)!",
380 if ((kill (pid, 0) < 0) && (errno == ESRCH))
382 fprintf (stderr, "removing stale lock file %s\n", path);
389 lockfile_remove_stale (Filelist * fl)
394 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)
417 if (lockfile_make (fle->name))
419 fprintf (stderr, "Failed to get lockfile %s\n", fle->name);
423 filelist_add (ret, fle->name);
431 main (int argc, char *argv[])
433 Filelist *fl = lockfile_make_list ("/dev/ttyS0");
437 filelist_print (fl, stdout);
439 fll = lockfile_lock (fl);
441 filelist_print (fll, stdout);