aboutsummaryrefslogtreecommitdiffstats
path: root/src/lockfile.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lockfile.c')
-rw-r--r--src/lockfile.c269
1 files changed, 154 insertions, 115 deletions
diff --git a/src/lockfile.c b/src/lockfile.c
index 1a77815..d176325 100644
--- a/src/lockfile.c
+++ b/src/lockfile.c
@@ -11,6 +11,9 @@ static char rcsid[] =
/*
* $Log$
+ * Revision 1.18 2012/06/22 10:22:24 james
+ * *** empty log message ***
+ *
* Revision 1.17 2010/07/16 11:04:10 james
* ignore tedious return values
*
@@ -64,6 +67,12 @@ static char rcsid[] =
*
*/
+static inline char *
+stop_wno_unused_on_rcsid (void)
+{
+ return rcsid;
+}
+
#define LOCK_ASCII
#undef LOCK_BINARY
@@ -83,6 +92,7 @@ static char rcsid[] =
#include <errno.h>
#include <time.h>
#include <sys/time.h>
+#include <signal.h>
#include "lockfile.h"
@@ -149,10 +159,11 @@ void
filelist_print (Filelist * fl, FILE * f)
{
Filelist_ent *fle;
- if (!fl) {
- fprintf (f, "(empty list)\n");
- return;
- }
+ if (!fl)
+ {
+ fprintf (f, "(empty list)\n");
+ return;
+ }
for (fle = fl->head; fle; fle = fle->next)
fprintf (f, "%s\n", fle->name);
}
@@ -167,14 +178,18 @@ chown_uucp (fd)
static int uuid = -1, ugid;
struct passwd *pw;
- if (uuid < 0) {
- if (pw = getpwnam ("uucp")) {
- uuid = pw->pw_uid;
- ugid = pw->pw_gid;
- } else {
- return -1;
+ if (uuid < 0)
+ {
+ if ((pw = getpwnam ("uucp")))
+ {
+ uuid = pw->pw_uid;
+ ugid = pw->pw_gid;
+ }
+ else
+ {
+ return -1;
+ }
}
- }
return fchown (fd, uuid, ugid);
}
@@ -201,30 +216,33 @@ lockfile_make (char *name)
unlink (tmpfn);
fd = open (tmpfn, O_WRONLY | O_CREAT | O_TRUNC, 0444);
- if (fd < 0) {
- unlink (tmpfn);
- return -1;
- }
+ if (fd < 0)
+ {
+ unlink (tmpfn);
+ return -1;
+ }
int result;
result = write (fd, buf, i);
fchmod (fd, 044);
#if 0
- if (chown_uucp (fd)) {
- close (fd);
- unlink (tmpfn);
- return -1;
- }
+ if (chown_uucp (fd))
+ {
+ close (fd);
+ unlink (tmpfn);
+ return -1;
+ }
#else
chown_uucp (fd);
#endif
close (fd);
- if (link (tmpfn, name) < 0) {
- unlink (tmpfn);
- return -1;
- }
+ if (link (tmpfn, name) < 0)
+ {
+ unlink (tmpfn);
+ return -1;
+ }
unlink (tmpfn);
return 0;
@@ -243,27 +261,29 @@ lockfile_add_places (Filelist * fl, char *leaf)
};
int i;
- for (i = 0; i < (sizeof (lock_dirs) / sizeof (char *)); ++i) {
- if (stat (lock_dirs[i], &stbuf))
- continue;
- strcpy (buf, lock_dirs[i]);
- strcat (buf, "/");
- strcat (buf, leaf);
- filelist_add (fl, buf);
- }
+ for (i = 0; i < (sizeof (lock_dirs) / sizeof (char *)); ++i)
+ {
+ if (stat (lock_dirs[i], &stbuf))
+ continue;
+ strcpy (buf, lock_dirs[i]);
+ strcat (buf, "/");
+ strcat (buf, leaf);
+ filelist_add (fl, buf);
+ }
}
static void
do_tedious_mangling (Filelist * fl, char *buf, char *ptr, char inv, int lower)
{
- while (*ptr) {
- if (lower && (*ptr >= 'A') && (*ptr <= 'Z'))
- *ptr |= 32;
- if (*ptr == '/')
- *ptr = inv;
- ptr++;
- }
+ while (*ptr)
+ {
+ if (lower && (*ptr >= 'A') && (*ptr <= 'Z'))
+ *ptr |= 32;
+ if (*ptr == '/')
+ *ptr = inv;
+ ptr++;
+ }
lockfile_add_places (fl, buf);
}
@@ -302,10 +322,11 @@ lockfile_add_name_from_path (Filelist * fl, char *file)
ptr++;
lockfile_regularize_and_add (fl, ptr);
- if (!strncmp (ptr, "dev/", 4)) {
- ptr += 4;
- lockfile_regularize_and_add (fl, ptr);
- }
+ if (!strncmp (ptr, "dev/", 4))
+ {
+ ptr += 4;
+ lockfile_regularize_and_add (fl, ptr);
+ }
}
@@ -330,20 +351,21 @@ lockfile_check_dir_for_dev (Filelist * fl, char *dir, dev_t dev)
if (!d)
return;
- while ((de = readdir (d))) {
- strcpy (buf, dir);
- strcat (buf, de->d_name);
+ while ((de = readdir (d)))
+ {
+ strcpy (buf, dir);
+ strcat (buf, de->d_name);
- if (stat (buf, &ent_stat))
- continue;
- if (!S_ISCHR (ent_stat.st_mode))
- continue;
- if (ent_stat.st_rdev != dev)
- continue;
+ if (stat (buf, &ent_stat))
+ continue;
+ if (!S_ISCHR (ent_stat.st_mode))
+ continue;
+ if (ent_stat.st_rdev != dev)
+ continue;
- lockfile_add_name_from_path (fl, buf);
+ lockfile_add_name_from_path (fl, buf);
- }
+ }
closedir (d);
}
@@ -361,15 +383,16 @@ lockfile_make_list (char *device)
ret = filelist_new ();
- if (ret) {
- lockfile_add_name_from_dev (ret, dev_stat.st_rdev);
+ if (ret)
+ {
+ lockfile_add_name_from_dev (ret, dev_stat.st_rdev);
- lockfile_add_name_from_path (ret, device);
+ lockfile_add_name_from_path (ret, device);
- lockfile_check_dir_for_dev (ret, "/dev/", dev_stat.st_rdev);
- lockfile_check_dir_for_dev (ret, "/dev/usb/", dev_stat.st_rdev);
- lockfile_check_dir_for_dev (ret, "/dev/tts/", dev_stat.st_rdev);
- }
+ lockfile_check_dir_for_dev (ret, "/dev/", dev_stat.st_rdev);
+ lockfile_check_dir_for_dev (ret, "/dev/usb/", dev_stat.st_rdev);
+ lockfile_check_dir_for_dev (ret, "/dev/tts/", dev_stat.st_rdev);
+ }
return ret;
}
@@ -392,28 +415,34 @@ remove_stale_lock (char *path)
apid[length] = 0;
pid = 0;
- if (length == sizeof (pid) || sscanf (apid, "%d", &pid) != 1 || pid == 0) {
- pid = *((int *) apid);
+ if (length == sizeof (pid) || sscanf (apid, "%d", &pid) != 1 || pid == 0)
+ {
+
+ //pid=*(int *)(void *)(char *) apid;
+ memcpy (&pid, apid, sizeof (pid));
+
#ifdef LOCK_ASCII
- fprintf (stderr,
- "compiled with ascii locks, found binary lock file (length=%d, pid=%d)!",
- length, pid);
+ fprintf (stderr,
+ "compiled with ascii locks, found binary lock file (length=%d, pid=%d)!",
+ length, pid);
#endif
- }
+ }
#ifdef LOCK_BINARY
- else {
- fprintf (stderr,
- "compiled with binary locks, found ascii lock file (length=%d, pid=%d)!",
- length, pid);
- }
+ else
+ {
+ fprintf (stderr,
+ "compiled with binary locks, found ascii lock file (length=%d, pid=%d)!",
+ length, pid);
+ }
#endif
close (fd);
- if ((kill (pid, 0) < 0) && (errno == ESRCH)) {
- fprintf (stderr, "removing stale lock file %s\n", path);
- unlink (path);
- }
+ if ((kill (pid, 0) < 0) && (errno == ESRCH))
+ {
+ fprintf (stderr, "removing stale lock file %s\n", path);
+ unlink (path);
+ }
}
@@ -423,11 +452,12 @@ lockfile_remove_stale (Filelist * fl)
Filelist_ent *fle;
struct stat buf;
- for (fle = fl->head; fle; fle = fle->next) {
- if (stat (fle->name, &buf))
- continue;
- remove_stale_lock (fle->name);
- }
+ for (fle = fl->head; fle; fle = fle->next)
+ {
+ if (stat (fle->name, &buf))
+ continue;
+ remove_stale_lock (fle->name);
+ }
}
@@ -441,18 +471,21 @@ lockfile_lock (Filelist * fl)
ret = filelist_new ();
- if (ret) {
- lockfile_remove_stale (fl);
-
- for (fle = fl->head; fle; fle = fle->next) {
- if (lockfile_make (fle->name)) {
- fprintf (stderr, "Failed to get lockfile %s\n", fle->name);
- filelist_free (ret);
- return NULL;
- }
- filelist_add (ret, fle->name);
+ if (ret)
+ {
+ lockfile_remove_stale (fl);
+
+ for (fle = fl->head; fle; fle = fle->next)
+ {
+ if (lockfile_make (fle->name))
+ {
+ fprintf (stderr, "Failed to get lockfile %s\n", fle->name);
+ filelist_free (ret);
+ return NULL;
+ }
+ filelist_add (ret, fle->name);
+ }
}
- }
return ret;
}
@@ -461,10 +494,11 @@ void
lockfile_unlock (Filelist * fl)
{
- while (fl->head) {
- unlink (fl->head->name);
- filelist_remove (fl, fl->head);
- }
+ while (fl->head)
+ {
+ unlink (fl->head->name);
+ filelist_remove (fl, fl->head);
+ }
}
@@ -481,10 +515,11 @@ serial_lock_check (Serial_lock * l)
if (l->mode == SERIAL_LOCK_ACTIVE)
return 0;
- for (fle = l->locks_to_check->head; fle; fle = fle->next) {
- if (!stat (fle->name, &buf))
- locks_found++;
- }
+ for (fle = l->locks_to_check->head; fle; fle = fle->next)
+ {
+ if (!stat (fle->name, &buf))
+ locks_found++;
+ }
if (!locks_found)
return 0;
@@ -492,10 +527,11 @@ serial_lock_check (Serial_lock * l)
gettimeofday (&now, NULL);
timersub (&now, &l->last_stale_purge, &dif);
- if (dif.tv_sec > STALE_CHECK_INTERVAL) {
- lockfile_remove_stale (l->locks_to_check);
- l->last_stale_purge = now;
- }
+ if (dif.tv_sec > STALE_CHECK_INTERVAL)
+ {
+ lockfile_remove_stale (l->locks_to_check);
+ l->last_stale_purge = now;
+ }
return 1;
}
@@ -506,14 +542,16 @@ serial_lock_free (Serial_lock * l)
if (!l)
return;
- if (l->locks_held) {
- lockfile_unlock (l->locks_held);
- filelist_free (l->locks_held);
- }
+ if (l->locks_held)
+ {
+ lockfile_unlock (l->locks_held);
+ filelist_free (l->locks_held);
+ }
- if (l->locks_to_check) {
- filelist_free (l->locks_to_check);
- }
+ if (l->locks_to_check)
+ {
+ filelist_free (l->locks_to_check);
+ }
free (l);
}
@@ -539,10 +577,11 @@ serial_lock_new (char *dev, int mode)
return l;
l->locks_held = lockfile_lock (l->locks_to_check);
- if (!l->locks_held) {
- serial_lock_free (l);
- return NULL;
- }
+ if (!l->locks_held)
+ {
+ serial_lock_free (l);
+ return NULL;
+ }
return l;
}