Index: /trunk/Ohana/src/elixir/src/DumpStatus.c
===================================================================
--- /trunk/Ohana/src/elixir/src/DumpStatus.c	(revision 3647)
+++ /trunk/Ohana/src/elixir/src/DumpStatus.c	(revision 3648)
@@ -4,5 +4,5 @@
 
   FILE *f;
-  int i, fd, state, total, mode;
+  int i, state, total, mode;
   Process *global, **process;
   int Nprocess, Nobject, Dynamic;
@@ -16,13 +16,6 @@
   } else {
     /* lock file */
-    fd = setlockfile2 (filename, 0.1, LCK_XCLD, &state);
-    if (fd == -1) return (2);
-    f = fdopen (fd, "r+");
-    if (f == (FILE *) NULL) {
-      mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
-      fchmod (fd, mode);
-      clearlockfile2 (filename, fd, LCK_XCLD, &state);
-      return (2);
-    }
+    f = fsetlockfile (filename, 0.1, LCK_XCLD, &state);
+    if (f == NULL) return (2);
     fseek (f, 0, SEEK_END);
   }  
@@ -51,7 +44,6 @@
   if (f != stderr) {
     mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
-    fchmod (fd, mode);
-    clearlockfile2 (filename, fd, LCK_XCLD, &state);
-    fclose (f);
+    chmod (filename, mode);
+    fclearlockfile (filename, f, LCK_XCLD, &state);
   }
   return (TRUE);
Index: /trunk/Ohana/src/elixir/src/LoadPending.c
===================================================================
--- /trunk/Ohana/src/elixir/src/LoadPending.c	(revision 3647)
+++ /trunk/Ohana/src/elixir/src/LoadPending.c	(revision 3648)
@@ -15,5 +15,5 @@
 
   int i, status, depend, Nobjects;
-  int fd, lockstate, mode;
+  int lockstate, mode;
   FILE *f;
   char name[7][256], line[1024];
@@ -33,12 +33,6 @@
     
     /* check lockfile - don't remove list if locked */
-    fd = setlockfile2 (filename, 1.0, LCK_XCLD, &lockstate);
-    if (fd == -1) { 
-      *state = 1;
-      return (Nobjects);
-    }
-    f = fdopen (fd, "r+");
-    if (f == (FILE *) NULL) {
-      clearlockfile2 (filename, fd, LCK_XCLD, &lockstate);
+    f = fsetlockfile (filename, 1.0, LCK_XCLD, &lockstate);
+    if (f == NULL) { 
       *state = 1;
       return (Nobjects);
@@ -113,5 +107,5 @@
   escape:
   if (*dynamic) {
-    if (ftruncate (fd, 0)) {
+    if (truncate (filename, 0)) {
       fprintf (stderr, "failed to clear source %s (errno %d)\n", filename, errno);
       Shutdown (1);
@@ -120,8 +114,9 @@
     /* clean up fifo - set mode to 666, unlock */
     mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
-    fchmod (fd, mode);
-    clearlockfile2 (filename, fd, LCK_XCLD, &lockstate);
+    chmod (filename, mode);
+    fclearlockfile (filename, f, LCK_XCLD, &lockstate);
+  } else {
+    fclose (f);
   }
-  fclose (f);
 
   if ((*state == 2) && *dynamic) {
Index: /trunk/Ohana/src/elixir/src/MachineOps.c
===================================================================
--- /trunk/Ohana/src/elixir/src/MachineOps.c	(revision 3647)
+++ /trunk/Ohana/src/elixir/src/MachineOps.c	(revision 3648)
@@ -443,24 +443,15 @@
 int DumpProcessTimes (char *filename) {
   
-  int i, j, fd, state, mode;
+  int i, j, state, mode;
   FILE *f;
   double dt;
 
-  fd = 0;
   if (filename == (char *) NULL) {
     system ("tput clear");
     f = stderr;
   } else {
-
     /* check lockfile */
-    fd = setlockfile2 (filename, 0.1, LCK_XCLD, &state);
-    if (fd == -1) return (2);
-    f = fdopen (fd, "r+");
-    if (f == (FILE *) NULL) {
-      mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
-      fchmod (fd, mode);
-      clearlockfile2 (filename, fd, LCK_XCLD, &state);
-      return (2);
-    }
+    f = fsetlockfile (filename, 0.1, LCK_XCLD, &state);
+    if (f == NULL) return (2);
     fseek (f, 0, SEEK_END);
   }  
@@ -486,7 +477,6 @@
   if (f != stderr) {
     mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
-    fchmod (fd, mode);
-    clearlockfile2 (filename, fd, LCK_XCLD, &state);
-    fclose (f);
+    chmod (filename, mode);
+    fclearlockfile (filename, f, LCK_XCLD, &state);
   }
   return (1);
Index: /trunk/Ohana/src/elixir/src/MsgOps.c
===================================================================
--- /trunk/Ohana/src/elixir/src/MsgOps.c	(revision 3647)
+++ /trunk/Ohana/src/elixir/src/MsgOps.c	(revision 3648)
@@ -1,5 +1,3 @@
 # include "controller.h"
-
-/*** this file should be updated to use fsetlockfile / fclearlockfile ***/
 
 int WaitMsg (char *fifo, char **message, double maxdelay) {
@@ -42,20 +40,12 @@
 
   /* check lockfile */
-  fd = setlockfile2 (fifo, 0.1, LCK_XCLD, &state);
-  if (fd == -1) return (2);
-  f = fdopen (fd, "r+");
-  if (f == (FILE *) NULL) {
-    mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
-    fchmod (fd, mode);
-    clearlockfile2 (fifo, fd, LCK_XCLD, &state);
-    return (2);
-  }
+  f = fsetlockfile (fifo, 0.1, LCK_XCLD, &state);
+  if (f == NULL) return (2);
   
   /* if file is empty, return 0 */
   if (state == LCK_EMPTY) {
     mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
-    fchmod (fd, mode);
-    clearlockfile2 (fifo, fd, LCK_XCLD, &state);
-    fclose (f);
+    chmod (fifo, mode);
+    fclearlockfile (fifo, f, LCK_XCLD, &state);
     return (0);
   }  
@@ -80,5 +70,5 @@
   /* file will remain until unlocked and fclosed below, 
      but cannot be written to because it is locked */
-  if (ftruncate (fd, 0)) {
+  if (truncate (fifo, 0)) {
     fprintf (stderr, "failed to clear fifo file %s (errno: %d)\n", fifo, errno);
     Shutdown (1);
@@ -86,7 +76,6 @@
   
   mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
-  fchmod (fd, mode);
-  clearlockfile2 (fifo, fd, LCK_XCLD, &state);
-  fclose (f);
+  chmod (f, mode);
+  fclearlockfile (fifo, f, LCK_XCLD, &state);
   
   if (Nbytes == 0) {
@@ -106,13 +95,6 @@
 
   /* check lockfile */
-  fd = setlockfile2 (fifo, 0.1, LCK_XCLD, &state);
-  if (fd == -1) return (2);
-  f = fdopen (fd, "r+");
-  if (f == (FILE *) NULL) {
-    mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
-    fchmod (fd, mode);
-    clearlockfile2 (fifo, fd, LCK_XCLD, &state);
-    return (2);
-  }
+  f = fsetlockfile (fifo, 0.1, LCK_XCLD, &state);
+  if (f == NULL) return (2);
 
   /* write message to end of file */
@@ -121,18 +103,12 @@
 
   mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
-  fchmod (fd, mode);
-  clearlockfile2 (fifo, fd, LCK_XCLD, &state);
-  fclose (f);
-
+  chmod (fifo, mode);
+  fclearlockfile (fifo, f, LCK_XCLD, &state);
   return (1);
-
 }
 
-
 /*  possible return states:
-
     0 - no message (fifo file empty or non-existent) 
     1 - message received
     2 - busy (fifo file locked)
-
 */
Index: /trunk/Ohana/src/imregister/base/WriteFIFO.c
===================================================================
--- /trunk/Ohana/src/imregister/base/WriteFIFO.c	(revision 3647)
+++ /trunk/Ohana/src/imregister/base/WriteFIFO.c	(revision 3648)
@@ -1,26 +1,23 @@
 # include "imregister.h"
-
-static char retchar[] = "\n";
 
 int WriteFIFO (char *filename, char *line) {
 
   int state, mode;
-  int fd;
+  FILE *f;
 
-  fd = setlockfile2 (filename, 20.0, LCK_XCLD, &state);
-  if (fd == -1) {
-      fprintf (stderr, "ERROR: can't lock fifo %s\n", filename);
-      return (FALSE);
+  f = fsetlockfile (filename, 20.0, LCK_XCLD, &state);
+  if (f == NULL) {
+    fprintf (stderr, "ERROR: can't lock fifo %s\n", filename);
+    return (FALSE);
   }
 
-  lseek (fd, 0, SEEK_END);
-  write (fd, line, strlen (line));
-  write (fd, retchar, 1);
+  fseek (f, 0, SEEK_END);
+  fprintf (f, "%s\n", line);
 
   mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
-  fchmod (fd, mode);
-  clearlockfile2 (filename, fd, LCK_XCLD, &state);
+  chmod (filename, mode);
+  fclearlockfile (filename, f, LCK_XCLD, &state);
 
   return (TRUE);
+}
 
-}
Index: /trunk/Ohana/src/imregister/detrend/entry.c
===================================================================
--- /trunk/Ohana/src/imregister/detrend/entry.c	(revision 3647)
+++ /trunk/Ohana/src/imregister/detrend/entry.c	(revision 3648)
@@ -30,9 +30,10 @@
 
   int status;
-  int found, Ntry, fd, filestate;
+  int found, Ntry, filestate;
   char path[256], fullpath[256], filename[256], rootname[256], fullname[256], line[512];
   char *dBPath;
   char *filter_basename;
   struct stat statbuf;
+  FILE *f;
 
   filter_basename = filterhash[newdata[0].filter];
@@ -75,5 +76,5 @@
     sprintf (filename, "%s/%s.%02d.fits", path, rootname, Ntry);
     sprintf (fullname, "%s/%s", dBPath, filename);
-    fd = setlockfile2 (fullname, 2.0, LCK_XCLD, &filestate);
+    f = fsetlockfile (fullname, 2.0, LCK_XCLD, &filestate);
     if (filestate == LCK_EMPTY) {
 	found = TRUE;
@@ -105,5 +106,5 @@
     exit (1);
   }
-  clearlockfile2 (fullname, fd, LCK_XCLD, &filestate);
+  fclearlockfile2 (fullname, f, LCK_XCLD, &filestate);
 
   return (TRUE);
Index: /trunk/Ohana/src/imregister/src/imstatreg.c
===================================================================
--- /trunk/Ohana/src/imregister/src/imstatreg.c	(revision 3647)
+++ /trunk/Ohana/src/imregister/src/imstatreg.c	(revision 3648)
@@ -1,5 +1,5 @@
 # include "imregister.h"
 # include "imreg.h"
-static char *version = "imstatreg $Revision: 3.10 $";
+static char *version = "imstatreg $Revision: 3.11 $";
 
 int main (int argc, char **argv) {
@@ -12,9 +12,7 @@
   get_version (argc, argv, version);
   args (argc, argv);
-  init_db ();
+  SetSignals ();
 
   if (CLIENT) imregclient (argv[1], argv[2], argv[3]);
-
-  SetSignals ();
 
   /* fork in background */
Index: /trunk/Ohana/src/libohana/src/glockfile.c
===================================================================
--- /trunk/Ohana/src/libohana/src/glockfile.c	(revision 3647)
+++ /trunk/Ohana/src/libohana/src/glockfile.c	(revision 3648)
@@ -1,49 +1,11 @@
 # include <ohana.h>
 
-/* set a lock on the given file */
 FILE *fsetlockfile (char *filename, double timeout, int type, int *state) {
-  
-  int fd;
-  FILE *f;
-
-  fd = setlockfile2 (filename, timeout, type, state);
-  if (fd == -1) return ((FILE *) NULL);
-
-  /* setlockfile2 returns a pointer to a vile open for update */
-  f = fdopen (fd, "r+");
-  if (f == (FILE *) NULL) {
-    clearlockfile2 (filename, fd, type, state);
-    fprintf (stderr, "can't make stream from descriptor\n");
-    return ((FILE *) NULL);
-  }
-  return (f);
-}
-
-/* clears lock. removes hardlock even if file pointer is not supplied */
-int fclearlockfile (char *filename, FILE *f, int type, int *state) {
-
-  int fd, status;
-
-  fd = -1;
-  if (f != NULL) {
-    fflush (f);
-    fd = fileno (f);
-  }
-  status = clearlockfile2 (filename, fd, type, state);
-  if (f != NULL) fclose (f);
-  return (status);
-}
-
-/* should eventually rewrite these two to use a stream as the basic
-   element and only grab the fileno when needed by fcntl
-   (fileno cannot fail) */
-
-int setlockfile2 (char *filename, double timeout, int type, int *state) {
   
   int i, done, nbytes, status;
   char *lockname, buffer[64];
   char *file, *path;
-  int fd, flock;
-  int filemode;
+  int fd;
+  FILE *f, flock;
   struct stat filestat;
   struct flock filelock;
@@ -51,8 +13,5 @@
 
   /* initial values at -1, close if the are set to another value */
-  fd = flock = -1;
-  filemode = S_IRUSR | S_IRGRP | S_IROTH;
   file = path = lockname = (char *) NULL;
-  gettimeofday (&then, (void *) NULL);
 
   /* define lock type */
@@ -65,9 +24,7 @@
   case LCK_XCLD:
     filelock.l_type   = F_WRLCK;  /* set an exclusive lock */
-    filemode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
     break;
   case LCK_SOFT:
     filelock.l_type   = F_RDLCK;  /* set a shared lock */
-    filemode = S_IRUSR | S_IRGRP | S_IROTH;
     break;
   default:
@@ -85,13 +42,17 @@
   }
   
-  /* try to open file */
-  fd = open (filename, O_RDWR | O_CREAT, filemode);
-  if (fd == -1) {
+  /* try to open file (create if it does not exist) */
+  f = fopen (filename, "w+");
+  if (f == NULL) {
     *state = LCK_ACCESS;
     goto failure;
   }
+  fd = fileno (f);
 
   /* we first try to set a FS level lock on the file */
-  for (done = 0; !done; ) {
+  gettimeofday (&then, (void *) NULL);
+  while (1) {
+    /* try to lock file */
+    if (fcntl (fd, F_SETLK, &filelock) != -1) goto got_lock;
 
     /* check for timeout */
@@ -101,16 +62,11 @@
       goto failure;
     }
-    usleep (10000); /* 10 ms is min */
-
-    /* try to lock file */
-    if (fcntl (fd, F_SETLK, &filelock) != -1) done = 1;
-  }
+    usleep (10000); /* 10 ms is min utime */
+  }
+got_lock:
 
   if (type == LCK_HARD) {
-    /* we've locked filename, now check lockname */ 
-
-    /* locking the lockfile before checking / setting contents will 
-       ensure the data is synced across NFS */
-    
+    /* we've locked filename, now check lockfile */ 
+
     /* set up name to lockfile */
     path = pathname (filename);
@@ -120,21 +76,23 @@
 
     /* try to open lockfile */
-    flock = open (lockname, O_RDWR | O_CREAT, filemode);
-    if (flock == -1) {
+    flock = fopen (lockname, "w+");
+    if (flock == NULL) {
       *state = LCK_HARDLCK;
       goto failure;
-    }
-    
-    /* try a few times to lock lockfile */
-    for (i = 0; (i < 20) && (fcntl (flock, F_SETLK, &filelock) == -1); i++) {
-      usleep (10000);
-    }
+      /***** this one has left the file locked ****/
+    }
+    fd = fileno (flock);
+    
+    /* try a few times to lock lockfile (locking the lockfile before checking
+       the contents will ensure the data is synced across NFS) */
+    for (i = 0; (i < 20) && (fcntl (fd, F_SETLK, &filelock) == -1); i++) usleep (10000);
     if (i == 20) {
       *state = LCK_HARDLCK + 10;
       goto failure;
-    }
-    
-    /* we've locked the file, now read the contents */
-    nbytes = read (flock, buffer, 4);
+      /***** this one has left the file locked ****/
+    }
+    
+    /* we've locked the lockfile, now read the contents */
+    nbytes = fread (buffer, 1, 4, flock);
     if (nbytes == 4) { /* lock file has a word in it */
       buffer[4] = 0;
@@ -142,5 +100,7 @@
 	*state = LCK_HARDLCK + 20;
 	goto failure;
+	/*** lock file is still 
       }
+      /* note that we don't care if the lockfile has random garbage */
     }
     
@@ -187,4 +147,19 @@
 }
   
+/* clears lock. removes hardlock even if file pointer is not supplied */
+int fclearlockfile (char *filename, FILE *f, int type, int *state) {
+
+  int fd, status;
+
+  fd = -1;
+  if (f != NULL) {
+    fflush (f);
+    fd = fileno (f);
+  }
+  status = clearlockfile2 (filename, fd, type, state);
+  if (f != NULL) fclose (f);
+  return (status);
+}
+
 /* if we pass in -1, all we can do is clear the hardlock */
 int clearlockfile2 (char *filename, int fd, int type, int *state) {
Index: /trunk/Ohana/src/misc/Makefile
===================================================================
--- /trunk/Ohana/src/misc/Makefile	(revision 3647)
+++ /trunk/Ohana/src/misc/Makefile	(revision 3648)
@@ -44,8 +44,4 @@
 	for i in $(PROGRAMS); do make $$i.install; done
 
-# special rules for specific targets
-dbtest: src/dbtest.$(ARCH).o src/glockfile2.$(ARCH).o
-	$(CC) -o dbtest src/dbtest.$(ARCH).o src/glockfile2.$(ARCH).o $(LIBS) $(CCFLAGS)
-
 FAKESRC = $(SRC)/fakestars.$(ARCH).o $(SRC)/random.$(ARCH).o
 
Index: unk/Ohana/src/misc/src/dbtest.c
===================================================================
--- /trunk/Ohana/src/misc/src/dbtest.c	(revision 3647)
+++ 	(revision )
@@ -1,142 +1,0 @@
-# include <ohana.h>
-# define DTIME(A,B) ((A.tv_sec - B.tv_sec) + 1e-6*(A.tv_usec - B.tv_usec))
-
-# define LCK_ACCESS  1   /* can't get access to file */
-# define LCK_TIMEOUT 2   /* timeout setting lock */
-# define LCK_HARDLCK 3   /* error setting hard lockfile */
-# define LCK_HUNLOCK 4   /* error clearing hard lockfile */
-
-# define LCK_EMPTY   5   /* locked file is empty */
-# define LCK_FULL    6   /* locked file is not empty */
-# define LCK_UNKNOWN 7   /* can't stat file to get size */
-
-# define LCK_SOFT    10  /* soft lock */
-# define LCK_HARD    11  /* hard lock */
-# define LCK_XCLD    12  /* exclusive soft lock */
-
-int clearlockfile2 (char *filename, int fd, int hard, int *state);
-int setlockfile2 (char *filename, double timeout, int hard, int *state);
-
-static char dbfile[] = "/h/eugene/kiawe/test.db";
-
-void main (int argc, char **argv) {
-
-  char buffer[10000], hostname[1025];
-  int vbuffer[10000];
-  FILE *f;
-  int i, j, Nrun, status, fd, pid, Remote;
-  int dbstate;
-  struct timeval now, then;  
-  double dtime;
-  
-  /* check cmd line args */
-  if (argc > 1) {
-    if (!strcmp (argv[1], "-check")) {
-      f = fopen (dbfile, "r");
-      if (f == (FILE *) NULL) {
-	fprintf (stderr, "ERROR: can't open file\n");
-	exit (0);
-      }
-      status = fread (buffer, 1, 5000, f);
-      if (status != 5000) {
-	fprintf (stderr, "ERROR: can't read header\n");
-	exit (0);
-      }
-      sscanf (&buffer[500], "%*s %*s %*s %d", &Nrun);
-      for (j = 0 ; j < Nrun; j++) {
-	status = fread (vbuffer, 4, 1000, f);
-	if (status != 1000) {
-	  fprintf (stderr, "ERROR: can't read data\n");
-	  exit (0);
-	}
-	for (i = 0; i < 1000; i++) {
-	  if (vbuffer[i] != j) {
-	    fprintf (stderr, "ERROR: invalid data %d/%d,  %d - %d\n", j, Nrun, i, vbuffer[i]);
-	    exit (0);
-	  }
-	}
-      }
-      fprintf (stderr, "Data is good, %d entries\n", Nrun);
-      exit (0);
-    }
-  }	  
-
-  /* set hard lock on dbfile */
-  if ((fd = setlockfile2 (dbfile, 60.0, LCK_HARD, &dbstate)) == -1) {
-    fprintf (stderr, "ERROR: can't set lock on %s, state is %d\n", dbfile, dbstate);
-    exit (1);
-  }
-
-  /* setlock will return an fd to an open file ready for read/write access */
-
-  /* first pass test, use only fd */
-  /* read the header block, change one line, write the header block */
-  /* write a block to EOF */
-
-  /* for new file, create header block with value of 0 */
-  if (dbstate == LCK_EMPTY) {
-    for (i = 0; i < 50; i++) {
-      memset (&buffer[100*i], 65+i, 99);
-      buffer[100*i + 99] = '\n';
-    }
-    sprintf (&buffer[500], "THIS IS TEST    0   \n");
-    Nrun = 0;
-  } else {
-    status = read (fd, buffer, 5000);
-    /* status = fread (buffer, 1, 5000, f); */
-    if (status != 5000) {
-      fprintf (stderr, "ERROR: can't read data\n");
-      exit (0);
-    }
-    /* check on current number of entries */
-    sscanf (&buffer[500], "%*s %*s %*s %d", &Nrun);
-    Nrun ++;
-    sprintf (&buffer[500], "THIS IS TEST    %d   \n", Nrun);
-  }
-
-
-# define STREAMS 1
-
-  /* position to begining of file to write header */
-# if (STREAMS)
-  f = fdopen (fd, "r+"); 
-
-  if (Nrun == 20) {
-    freopen (f, 
-  fseek (f, 0, SEEK_SET);
-  status = fwrite (buffer, 1, 5000, f);
-# else 
-  lseek (fd, 0, SEEK_SET);
-  status = write (fd, buffer, 5000);
-# endif
-
-  if (status != 5000) {
-    fprintf (stderr, "ERROR: failed writing data to header\n");
-    exit (0);
-  }
-
-  for (i = 0; i < 1000; i++) vbuffer[i] = Nrun;
-
-# if (STREAMS)
-  fseek (f, 0, SEEK_END);
-  status = fwrite (vbuffer, 1, 4000, f);
-# else 
-  lseek (fd, 0, SEEK_END);
-  status = write (fd, vbuffer, 4000);
-# endif
-
-  if (status != 4000) {
-    fprintf (stderr, "ERROR: failed writing data to file\n");
-    exit (0);
-  }
-
-  clearlockfile2 (dbfile, fd, LCK_HARD, &dbstate);
-# if (STREAMS)
-  fclose (f);
-# else 
-  close (fd);
-# endif
-
-
-}
-
Index: unk/Ohana/src/misc/src/glock.c
===================================================================
--- /trunk/Ohana/src/misc/src/glock.c	(revision 3647)
+++ 	(revision )
@@ -1,301 +1,0 @@
-# include <stdio.h>
-# include <sys/types.h>
-# include <sys/stat.h>
-# include <errno.h>
-# include <fcntl.h>
-# include <sys/time.h>
-# include <unistd.h>
-# include <stdlib.h>
-
-# define LCK_ACCESS  1   /* can't get access to file */
-# define LCK_TIMEOUT 2   /* timeout setting lock */
-# define LCK_HARDLCK 3   /* error setting hard lockfile */
-# define LCK_HUNLOCK 4   /* error clearing hard lockfile */
-
-# define LCK_EMPTY   5   /* locked file is empty */
-# define LCK_FULL    6   /* locked file is not empty */
-# define LCK_UNKNOWN 7   /* can't stat file to get size */
-
-# define LCK_SOFT    10  /* soft lock */
-# define LCK_HARD    11  /* hard lock */
-# define LCK_XCLD    12  /* exclusive soft lock */
-
-# define DTIME(A,B) ((A.tv_sec - B.tv_sec) + 1e-6*(A.tv_usec - B.tv_usec))
-
-extern int errno;
-extern char *pathname (), *filebasename ();
-
-# ifndef ALLOCATE
-# define ALLOCATE(X,T,S)  \
-  X=(T *)malloc((unsigned) ((S)*sizeof(T)));\
-  if(X==NULL) \
-    { \
-      fprintf(stderr,"failed to malloc X\n");\
-        exit(0);\
-    } 
-# define REALLOCATE(X,T,S) \
-  X=(T *)realloc(X,(unsigned) ((S)*sizeof(T))); \
-  if(X==NULL) \
-    { \
-       fprintf(stderr,"failed to realloc X\n"); \
-       exit(0); \
-    }
-# endif /* ALLOCATE */
-
-main (int argc, char **argv) {
-
-  int i, status, done, nbytes, type;
-  char *lockname, *filename, buffer[64];
-  char *file, *path;
-  int fd, flock;
-  int filemode, state, holdtime;
-  double timeout;
-  struct stat filestat;
-  struct flock filelock;
-  struct timeval now, then;
-
-  if (argc != 4) {
-    fprintf (stderr, "USAGE: glock (filename) (type) (holdtime)\n");
-    exit (1);
-  }
-
-  filename = argv[1];
-  timeout = 1.0;
-
-  holdtime = atoi (argv[3]);
-
-  type = 0;
-  if (!strcasecmp (argv[2], "hard")) {
-    type = LCK_HARD;
-  }
-  if (!strcasecmp (argv[2], "soft")) {
-    type = LCK_SOFT;
-  }
-  if (!strcasecmp (argv[2], "xcld")) {
-    type = LCK_XCLD;
-  }
-
-  file = path = lockname = (char *) NULL;
-  gettimeofday (&then, (void *) NULL);
-
-  /* define lock type */
-  filelock.l_start  = 0;
-  filelock.l_whence = SEEK_SET;
-  filelock.l_len    = 0;
-  filelock.l_pid    = getpid ();
-  switch (type) {
-  case LCK_HARD:
-  case LCK_XCLD:
-    filelock.l_type   = F_WRLCK;  /* set an exclusive lock */
-    break;
-  case LCK_SOFT:
-    filelock.l_type   = F_RDLCK;  /* set a shared lock */
-    break;
-  default:
-    fprintf (stderr, "invalid lock type %d\n", type);
-  }
-  filemode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
-
-  /* try to open file */
-  fd = open (filename, O_RDWR | O_CREAT, filemode);
-  if (fd == -1) {
-    state = LCK_ACCESS;
-    goto failure;
-  }
-
-  /* we first try to set a FS level lock on the file */
-  for (done = 0; !done; ) {
-
-    /* check for timeout */
-    gettimeofday (&now, (void *) NULL);
-    if (DTIME (now, then) > timeout) {
-      state = LCK_TIMEOUT;
-      fcntl (fd, F_GETLK, &filelock);
-      fprintf (stderr, "lock held:\n");
-      fprintf (stderr, "pid: %d\n", filelock.l_pid);
-      fprintf (stderr, "lock stats: %d %d %d\n", filelock.l_start, filelock.l_whence, filelock.l_len);
-      goto failure;
-    }
-    usleep (1000);
-
-    /* try to lock file */
-    if (fcntl (fd, F_SETLK, &filelock) != -1) done = 1;
-  }
-
-  if (type == LCK_HARD) {
-    /* we've locked filename, now check lockname */ 
-
-    /* locking the lockfile before checking / setting contents will 
-       ensure the data is synced across NFS */
-    
-    /* set up name to lockfile */
-    path = pathname (filename);
-    file = filebasename (filename);
-    ALLOCATE (lockname, char, strlen (path) + strlen (file) + 10);
-    sprintf (lockname, "%s/.%s.lck", path, file);
-
-    /* try to open lockfile */
-    flock = open (lockname, O_RDWR | O_CREAT, filemode);
-    if (flock == -1) {
-      state = LCK_HARDLCK;
-      goto failure;
-    }
-    
-    /* try a few times to lock lockfile */
-    for (i = 0; (i < 20) && (fcntl (flock, F_SETLK, &filelock) == -1); i++) {
-      usleep (10000);
-    }
-    if (i == 20) {
-      state = LCK_HARDLCK + 10;
-      goto failure;
-    }
-    
-    /* we've locked the file, now read the contents */
-    nbytes = read (flock, buffer, 4);
-    if (nbytes == 4) { /* lock file has a word in it */
-      buffer[4] = 0;
-      if (!strcmp (buffer, "BUSY")) { 
-	state = LCK_HARDLCK + 20;
-	goto failure;
-      }
-    }
-    
-    /* we've really got the lock, write BUSY to protect it */
-    lseek (flock, 0, SEEK_SET);
-    write (flock, "BUSY", 4);
-    
-    /* now unlock lockfile */
-    filelock.l_type = F_UNLCK;
-    if (fcntl (flock, F_SETLK, &filelock) == -1) {
-      state = LCK_HARDLCK + 30;
-      goto failure;
-    }
-    if (close (flock)) {
-      fprintf (stderr, "error closing lock\n");
-      goto failure;
-    }
-  }
-
-  /* check if file is empty or not */
-  if (fstat (fd, &filestat)) {
-    state = LCK_UNKNOWN;
-  } else {
-    if (filestat.st_size == 0) {
-      state = LCK_EMPTY;
-    } else {
-      state = LCK_FULL;
-    }
-  }
-
-  fprintf (stderr, "file is locked...\n");
-  for (i = holdtime; i > 0; i--) {
-    fprintf (stderr, "%d ", i);
-    usleep (1000000);
-  }
-  
-  clearlockfile2 (filename, fd, type, &state);
-  fprintf (stderr, "file unlocked...\n");
-   
-
-  if (path != NULL) free (path);
-  if (file != NULL) free (file);
-  if (lockname != NULL) free (lockname);
-  exit (0);
-
-failure:
-  if (path != NULL) free (path);
-  if (file != NULL) free (file);
-  if (lockname != NULL) free (lockname);
-  exit (1);
-}
-  
-
-/* if we pass in -1, all we can do is clear the hardlock */
-int clearlockfile2 (char *filename, int fd, int type, int *state) {
-  
-  char *lockname;
-  int i, status;
-  char *path, *file;
-  int flock, filemode;
-  struct stat filestat;
-  struct flock filelock;
-
-  file = path = lockname = (char *) NULL;
-
-  /* define lock */
-  filelock.l_type   = F_UNLCK;
-  filelock.l_start  = 0;
-  filelock.l_whence = SEEK_SET;
-  filelock.l_len    = 0;
-  filelock.l_pid    = getpid ();
-  filemode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
-
-  /* first clear hard lockfile */
-  if (type == LCK_HARD) {
-
-    /* define lockfile */
-    path = pathname (filename);
-    file = filebasename (filename);
-    ALLOCATE (lockname, char, strlen (path) + strlen (file) + 10);
-    sprintf (lockname, "%s/.%s.lck", path, file);
-    
-    /* check for lockfile existence */
-    status = stat (lockname, &filestat);
-    if (status == -1) {
-      *state = LCK_HUNLOCK;
-      goto failure;
-    } 
-  
-    /* open lockfile */
-    flock = open (lockname, O_RDWR | O_CREAT, filemode);
-    if (flock == -1) {
-      *state = LCK_HUNLOCK;
-      goto failure;
-    }
-
-    /* try a few times to lock lockfile */
-    filelock.l_type = F_WRLCK;
-    for (i = 0; (i < 20) && (fcntl (flock, F_SETLK, &filelock) == -1); i++) {
-      usleep (10000);
-    }
-    if (i == 20) {
-      *state = LCK_HUNLOCK;
-      goto failure;
-    }
-
-    /* set value to IDLE */
-    lseek (flock, 0, SEEK_SET);
-    write (flock, "IDLE", 4);
-
-    /* unlock lockfile */
-    filelock.l_type = F_UNLCK;
-    if (fcntl (flock, F_SETLK, &filelock) == -1) {
-      *state = LCK_HUNLOCK;
-      goto failure;
-    }
-    if (close (flock)) {
-      fprintf (stderr, "error closing lock\n");
-      goto failure;
-    }
-  }
-  
-  /* now unlock the file */
-  if (fd != -1) {
-    if (fcntl (fd, F_SETLK, &filelock) == -1) {
-      *state = LCK_HUNLOCK;
-      goto failure;
-    }
-  }
-
-  if (path != NULL) free (path);
-  if (file != NULL) free (file);
-  if (lockname != NULL) free (lockname);
-  return (1);
-
-failure:
-  if (path != NULL) free (path);
-  if (file != NULL) free (file);
-  if (lockname != NULL) free (lockname);
-  return (0);
-
-}
Index: /trunk/Ohana/src/misc/src/glockfile.c
===================================================================
--- /trunk/Ohana/src/misc/src/glockfile.c	(revision 3647)
+++ /trunk/Ohana/src/misc/src/glockfile.c	(revision 3648)
@@ -5,5 +5,6 @@
   char *filename;
   double timeout;
-  int holdtime, state, type, fd;
+  int holdtime, state, type;
+  FILE *f;
 
   if (argc != 4) {
@@ -28,6 +29,6 @@
   }
 
-  fd = setlockfile2 (filename, timeout, type, &state);
-  if (fd == -1) {
+  f = fsetlockfile (filename, timeout, type, &state);
+  if (fd == NULL) {
     fprintf (stderr, "ERROR: can't lock file %s\n", filename);
     exit (1);
@@ -35,7 +36,5 @@
 
   sleep (holdtime);
-  
-  clearlockfile2 (filename, fd, type, &state);
-
+  fclearlockfile (filename, f, type, &state);
   exit (0);
 }
Index: unk/Ohana/src/misc/src/glockfile2.c
===================================================================
--- /trunk/Ohana/src/misc/src/glockfile2.c	(revision 3647)
+++ 	(revision )
@@ -1,286 +1,0 @@
-# include <stdio.h>
-# include <sys/types.h>
-# include <sys/stat.h>
-# include <errno.h>
-# include <fcntl.h>
-# include <sys/time.h>
-# include <unistd.h>
-# include <stdlib.h>
-
-# define LCK_ACCESS  1   /* can't get access to file */
-# define LCK_TIMEOUT 2   /* timeout setting lock */
-# define LCK_HARDLCK 3   /* error setting hard lockfile */
-# define LCK_HUNLOCK 4   /* error clearing hard lockfile */
-
-# define LCK_EMPTY   5   /* locked file is empty */
-# define LCK_FULL    6   /* locked file is not empty */
-# define LCK_UNKNOWN 7   /* can't stat file to get size */
-
-# define LCK_SOFT    10  /* soft lock */
-# define LCK_HARD    11  /* hard lock */
-# define LCK_XCLD    12  /* exclusive soft lock */
-
-# define DTIME(A,B) ((A.tv_sec - B.tv_sec) + 1e-6*(A.tv_usec - B.tv_usec))
-
-extern int errno;
-extern char *pathname (), *filebasename ();
-
-# ifndef ALLOCATE
-# define ALLOCATE(X,T,S)  \
-  X=(T *)malloc((unsigned) ((S)*sizeof(T)));\
-  if(X==NULL) \
-    { \
-      fprintf(stderr,"failed to malloc X\n");\
-        exit(0);\
-    } 
-# define REALLOCATE(X,T,S) \
-  X=(T *)realloc(X,(unsigned) ((S)*sizeof(T))); \
-  if(X==NULL) \
-    { \
-       fprintf(stderr,"failed to realloc X\n"); \
-       exit(0); \
-    }
-# endif /* ALLOCATE */
-
-/* 
-   set a lockfile on [path]/filename.  
-   we allow two types of locks: hard & soft
-
-   both lock types set a filesystem-level lock on the given file, 
-    using exclusive for hard and shared for soft locks
-
-   a hard lock also uses a lockfile to make the lock permanent:
-    the lockfile must be actively removed and will not vanish 
-    even if the locking program dies 
-
-   a soft lock will vanish if the locking program dies.
-
-   if a hard lock is set, both hard & soft locks will block
-   if a soft lock is set, a hard lock will block, but not a soft lock
-
-   the hard lockfile uses the file [path]/.filename.lck
-
-   setlockfile returns a file descriptor to an open READ/WRITE access file,
-     or -1 on failure, and sets state.
-*/
-
-int setlockfile2 (char *filename, double timeout, int type, int *state) {
-  
-  int i, status, done, nbytes;
-  char *lockname, buffer[64];
-  char *file, *path;
-  int fd, flock;
-  int filemode;
-  struct stat filestat;
-  struct flock filelock;
-  struct timeval now, then;
-
-  file = path = lockname = (char *) NULL;
-  gettimeofday (&then, (void *) NULL);
-
-  /* define lock type */
-  filelock.l_start  = 0;
-  filelock.l_whence = SEEK_SET;
-  filelock.l_len    = 0;
-  filelock.l_pid    = getpid ();
-  switch (type) {
-  case LCK_HARD:
-  case LCK_XCLD:
-    filelock.l_type   = F_WRLCK;  /* set an exclusive lock */
-    break;
-  case LCK_SOFT:
-    filelock.l_type   = F_RDLCK;  /* set a shared lock */
-    break;
-  default:
-    fprintf (stderr, "invalid lock type %d\n", type);
-  }
-  filemode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
-
-  /* try to open file */
-  fd = open (filename, O_RDWR | O_CREAT, filemode);
-  if (fd == -1) {
-    *state = LCK_ACCESS;
-    goto failure;
-  }
-
-  /* we first try to set a FS level lock on the file */
-  for (done = 0; !done; ) {
-
-    /* check for timeout */
-    gettimeofday (&now, (void *) NULL);
-    if (DTIME (now, then) > timeout) {
-      *state = LCK_TIMEOUT;
-      goto failure;
-    }
-    usleep (1000);
-
-    /* try to lock file */
-    if (fcntl (fd, F_SETLK, &filelock) != -1) done = 1;
-  }
-
-  if (type == LCK_HARD) {
-    /* we've locked filename, now check lockname */ 
-
-    /* locking the lockfile before checking / setting contents will 
-       ensure the data is synced across NFS */
-    
-    /* set up name to lockfile */
-    path = pathname (filename);
-    file = filebasename (filename);
-    ALLOCATE (lockname, char, strlen (path) + strlen (file) + 10);
-    sprintf (lockname, "%s/.%s.lck", path, file);
-
-    /* try to open lockfile */
-    flock = open (lockname, O_RDWR | O_CREAT, filemode);
-    if (flock == -1) {
-      *state = LCK_HARDLCK;
-      goto failure;
-    }
-    
-    /* try a few times to lock lockfile */
-    for (i = 0; (i < 20) && (fcntl (flock, F_SETLK, &filelock) == -1); i++) {
-      usleep (10000);
-    }
-    if (i == 20) {
-      *state = LCK_HARDLCK + 10;
-      goto failure;
-    }
-    
-    /* we've locked the file, now read the contents */
-    nbytes = read (flock, buffer, 4);
-    if (nbytes == 4) { /* lock file has a word in it */
-      buffer[4] = 0;
-      if (!strcmp (buffer, "BUSY")) { 
-	*state = LCK_HARDLCK + 20;
-	goto failure;
-      }
-    }
-    
-    /* we've really got the lock, write BUSY to protect it */
-    lseek (flock, 0, SEEK_SET);
-    write (flock, "BUSY", 4);
-    
-    /* now unlock lockfile */
-    filelock.l_type = F_UNLCK;
-    if (fcntl (flock, F_SETLK, &filelock) == -1) {
-      *state = LCK_HARDLCK + 30;
-      goto failure;
-    }
-    if (close (flock)) {
-      fprintf (stderr, "error closing lock\n");
-      goto failure;
-    }
-  }
-
-  /* check if file is empty or not */
-  if (fstat (fd, &filestat)) {
-    *state = LCK_UNKNOWN;
-  } else {
-    if (filestat.st_size == 0) {
-      *state = LCK_EMPTY;
-    } else {
-      *state = LCK_FULL;
-    }
-  }
-
-  if (path != NULL) free (path);
-  if (file != NULL) free (file);
-  if (lockname != NULL) free (lockname);
-  return (fd);
-
-failure:
-  if (path != NULL) free (path);
-  if (file != NULL) free (file);
-  if (lockname != NULL) free (lockname);
-  return (-1);
-}
-  
-/* if we pass in -1, all we can do is clear the hardlock */
-int clearlockfile2 (char *filename, int fd, int type, int *state) {
-  
-  char *lockname;
-  int i, status;
-  char *path, *file;
-  int flock, filemode;
-  struct stat filestat;
-  struct flock filelock;
-
-  file = path = lockname = (char *) NULL;
-
-  /* define lock */
-  filelock.l_type   = F_UNLCK;
-  filelock.l_start  = 0;
-  filelock.l_whence = SEEK_SET;
-  filelock.l_len    = 0;
-  filelock.l_pid    = getpid ();
-  filemode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
-
-  /* first clear hard lockfile */
-  if (type == LCK_HARD) {
-
-    /* define lockfile */
-    path = pathname (filename);
-    file = filebasename (filename);
-    ALLOCATE (lockname, char, strlen (path) + strlen (file) + 10);
-    sprintf (lockname, "%s/.%s.lck", path, file);
-    
-    /* check for lockfile existence */
-    status = stat (lockname, &filestat);
-    if (status == -1) {
-      *state = LCK_HUNLOCK;
-      goto failure;
-    } 
-  
-    /* open lockfile */
-    flock = open (lockname, O_RDWR | O_CREAT, filemode);
-    if (flock == -1) {
-      *state = LCK_HUNLOCK;
-      goto failure;
-    }
-
-    /* try a few times to lock lockfile */
-    filelock.l_type = F_WRLCK;
-    for (i = 0; (i < 20) && (fcntl (flock, F_SETLK, &filelock) == -1); i++) {
-      usleep (10000);
-    }
-    if (i == 20) {
-      *state = LCK_HUNLOCK;
-      goto failure;
-    }
-
-    /* set value to IDLE */
-    lseek (flock, 0, SEEK_SET);
-    write (flock, "IDLE", 4);
-
-    /* unlock lockfile */
-    filelock.l_type = F_UNLCK;
-    if (fcntl (flock, F_SETLK, &filelock) == -1) {
-      *state = LCK_HUNLOCK;
-      goto failure;
-    }
-    if (close (flock)) {
-      fprintf (stderr, "error closing lock\n");
-      goto failure;
-    }
-  }
-  
-  /* now unlock the file */
-  if (fd != -1) {
-    if (fcntl (fd, F_SETLK, &filelock) == -1) {
-      *state = LCK_HUNLOCK;
-      goto failure;
-    }
-  }
-
-  if (path != NULL) free (path);
-  if (file != NULL) free (file);
-  if (lockname != NULL) free (lockname);
-  return (1);
-
-failure:
-  if (path != NULL) free (path);
-  if (file != NULL) free (file);
-  if (lockname != NULL) free (lockname);
-  return (0);
-
-}
Index: /trunk/Ohana/src/opihi/dvo/elixir.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/elixir.c	(revision 3647)
+++ /trunk/Ohana/src/opihi/dvo/elixir.c	(revision 3648)
@@ -62,17 +62,10 @@
 int WriteMsg (char *fifo, char *message) {
 
-  int fd, state, mode;
+  int state, mode;
   FILE *f;
 
   /* check lockfile */
-  fd = setlockfile2 (fifo, 2.0, LCK_XCLD, &state);
-  if (fd == -1) return (0);
-  f = fdopen (fd, "r+");
-  if (f == (FILE *) NULL) {
-    mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
-    fchmod (fd, mode);
-    clearlockfile2 (fifo, fd, LCK_XCLD, &state);
-    return (0);
-  }
+  f = fsetlockfile (fifo, 2.0, LCK_XCLD, &state);
+  if (f == NULL) return (0);
 
   /* write message to end of file */
@@ -81,11 +74,8 @@
 
   mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
-  fchmod (fd, mode);
+  chmod (fifo, mode);
 
-  clearlockfile2 (fifo, fd, LCK_XCLD, &state);
-  fclose (f);
-
+  fclearlockfile (fifo, f, LCK_XCLD, &state);
   return (1);
-
 }
 
@@ -94,5 +84,5 @@
   int i, nbytes, Nbytes, NBYTES;
   char *buffer;
-  int fd, state, mode;
+  int state, mode;
   FILE *f;
   struct stat filestat;
@@ -108,15 +98,7 @@
 
   /* check lockfile */
-  fd = setlockfile2 (fifo, 2.0, LCK_XCLD, &state);
-  if (fd == -1) {
+  f = fsetlockfile (fifo, 2.0, LCK_XCLD, &state);
+  if (f == NULL) {
     fprintf (stderr, "message locked (%d)\n", state);
-    return (0);
-  }
-  f = fdopen (fd, "r+");
-  if (f == (FILE *) NULL) {
-    fprintf (stderr, "message blocked\n");
-    mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
-    fchmod (fd, mode);
-    clearlockfile2 (fifo, fd, LCK_XCLD, &state);
     return (0);
   }
@@ -125,7 +107,6 @@
   if (state == LCK_EMPTY) {
     mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
-    fchmod (fd, mode);
-    clearlockfile2 (fifo, fd, LCK_XCLD, &state);
-    fclose (f);
+    chmod (fifo, mode);
+    fclearlockfile (fifo, f, LCK_XCLD, &state);
     return (0);
   }  
@@ -148,7 +129,6 @@
 
   mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
-  fchmod (fd, mode);
-  clearlockfile2 (fifo, fd, LCK_XCLD, &state);
-  fclose (f);
+  chmod (f, mode);
+  fclearlockfile (fifo, f, LCK_XCLD, &state);
 
   if (Nbytes == 0) {
