4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.5 2008/02/15 18:26:49 james
14 * *** empty log message ***
16 * Revision 1.4 2008/02/15 18:16:48 james
17 * *** empty log message ***
19 * Revision 1.3 2008/02/15 18:16:35 james
20 * *** empty log message ***
22 * Revision 1.2 2008/02/15 16:48:56 james
23 * *** empty log message ***
25 * Revision 1.1 2008/02/15 15:09:17 james
26 * *** empty log message ***
34 #include <sys/types.h>
51 Filelist *fl = (Filelist *) malloc (sizeof (Filelist));
59 filelist_remove (Filelist * fl, Filelist_ent * fle)
63 for (ep = &fl->head; *ep; ep = &((*ep)->next))
77 filelist_add (Filelist * fl, char *fn)
82 if (i >= FILE_LIST_MAX_LEN)
85 for (fle = fl->head; fle; fle = fle->next)
86 if (!strcmp (fle->name, fn))
89 fle = malloc (sizeof (Filelist_ent));
91 strcpy (fle->name, fn);
98 filelist_free (Filelist * fl)
101 filelist_remove (fl, fl->head);
106 filelist_print (Filelist * fl, FILE * f)
111 fprintf (f, "(empty list)\n");
114 for (fle = fl->head; fle; fle = fle->next)
115 fprintf (f, "%s\n", fle->name);
125 static int uuid = -1, ugid;
130 if (pw = getpwnam ("uucp"))
140 return fchown (fd, uuid, ugid);
144 lockfile_make (char *name)
146 char buf[1024], tmpfn[1024];
151 strcpy (tmpfn, name);
153 ptr = rindex (tmpfn, '/');
159 ptr += sprintf (ptr, "LTMP.%d", getpid ());
162 i = sprintf (buf, "%10d\n", getpid ());
165 fd = open (tmpfn, O_WRONLY | O_CREAT | O_TRUNC, 0444);
183 if (link (tmpfn, name) < 0)
195 lockfile_add_places (Filelist * fl, char *leaf)
200 { "/var/lock/uucp", "/var/spool/lock", "/var/spool/uucp", "/etc/locks",
201 "/usr/spool/uucp", "/var/spool/locks", "/usr/spool/lock",
203 "/usr/spool/uucp/LCK"
207 for (i = 0; i < (sizeof (lock_dirs) / sizeof (char *)); ++i)
209 if (stat (lock_dirs[i], &stbuf))
211 strcpy (buf, lock_dirs[i]);
214 filelist_add (fl, buf);
220 do_tedious_mangling (Filelist * fl, char *buf, char *ptr, char inv, int lower)
224 if (lower && (*ptr >= 'A') && (*ptr <= 'Z'))
231 lockfile_add_places (fl, buf);
236 lockfile_regularize_and_add (Filelist * fl, char *leaf)
238 char buf[1024] = "LCK..";
249 do_tedious_mangling (fl, buf, ptr, '_', 0);
251 do_tedious_mangling (fl, buf, ptr, '_', 1);
253 do_tedious_mangling (fl, buf, ptr, '.', 0);
255 do_tedious_mangling (fl, buf, ptr, '.', 1);
259 lockfile_add_name_from_path (Filelist * fl, char *file)
266 lockfile_regularize_and_add (fl, ptr);
268 if (!strncmp (ptr, "dev/", 4))
271 lockfile_regularize_and_add (fl, ptr);
277 lockfile_add_name_from_dev (Filelist * fl, dev_t dev)
280 sprintf (buf, "LCK.%03d.%03d", major (dev), minor (dev));
281 lockfile_add_places (fl, buf);
285 lockfile_check_dir_for_dev (Filelist * fl, char *dir, dev_t dev)
288 struct stat ent_stat;
297 while ((de = readdir (d)))
300 strcat (buf, de->d_name);
302 if (stat (buf, &ent_stat))
304 if (!S_ISCHR (ent_stat.st_mode))
306 if (ent_stat.st_rdev != dev)
309 lockfile_add_name_from_path (fl, buf);
316 lockfile_make_list (char *device)
318 struct stat dev_stat;
319 Filelist *ret = NULL;
322 if (stat (device, &dev_stat))
324 if (!S_ISCHR (dev_stat.st_mode))
327 ret = filelist_new ();
329 lockfile_add_name_from_dev (ret, dev_stat.st_rdev);
331 lockfile_add_name_from_path (ret, device);
333 lockfile_check_dir_for_dev (ret, "/dev/", dev_stat.st_rdev);
334 lockfile_check_dir_for_dev (ret, "/dev/usb/", dev_stat.st_rdev);
335 lockfile_check_dir_for_dev (ret, "/dev/tts/", dev_stat.st_rdev);
341 remove_stale_lock (char *path)
348 fd = open (path, O_RDONLY);
352 length = read (fd, apid, sizeof (apid) - 1);
358 if (length == sizeof (pid) || sscanf (apid, "%d", &pid) != 1 || pid == 0)
360 pid = *((int *) apid);
363 "compiled with ascii locks, found binary lock file (length=%d, pid=%d)!",
371 "compiled with binary locks, found ascii lock file (length=%d, pid=%d)!",
378 if ((kill (pid, 0) < 0) && (errno == ESRCH))
380 fprintf (stderr, "removing stale lock file %s\n", path);
387 lockfile_remove_stale (Filelist * fl)
392 for (fle = fl->head; fle; fle = fle->next)
394 if (stat (fle->name, &buf))
396 remove_stale_lock (fle->name);
404 lockfile_lock (Filelist * fl)
409 ret = filelist_new ();
411 lockfile_remove_stale (fl);
413 for (fle = fl->head; fle; fle = fle->next)
415 if (lockfile_make (fle->name))
417 fprintf (stderr, "Failed to get lockfile %s\n", fle->name);
421 filelist_add (ret, fle->name);
429 main (int argc, char *argv[])
431 Filelist *fl = lockfile_make_list ("/dev/ttyS0");
435 filelist_print (fl, stdout);
437 fll = lockfile_lock (fl);
439 filelist_print (fll, stdout);