lockf patch for qconfirm-0.14.3

Andy Bradford <[email protected]> Wed, 20 May 2009 20:50:08 -0600
Newsgroups gmane.comp.misc.pape.general
Message-ID <[email protected]>
Hello,

For those  interested in running qconfirm  on hosts that have  lockf and
not flock. It is invalid to attempt  to lockf a file that is opened with
O_RDONLY.  Attempts  to  do  so  result  in  EBADF  (or  other  errors),
which result  in the  inability to install  qconfirm because  make check
fails.  The  following  patch  attempts  to  correct  this  behavior  in
qconfirm-cdb-update and qconfirm-cdb-check.

Does this look correct?

Andy
--

diff -ur qconfirm-0.14.3.orig/src/qconfirm-cdb-check.c qconfirm-0.14.3/src/qconfirm-cdb-check.c
--- qconfirm-0.14.3.orig/src/qconfirm-cdb-check.c	Sun Dec 12 09:57:17 2004
+++ qconfirm-0.14.3/src/qconfirm-cdb-check.c	Wed May 20 15:01:16 2009
@@ -54,6 +54,7 @@
 stralloc sa ={0};
 static struct cdb c;
 int fdcdb;
+int fdcdblock;
 int fdtmp;
 
 int main(int argc, const char **argv) {
@@ -125,6 +126,7 @@
   mode =st.st_mode;
 
   /* open cdb */
+  fdcdblock =open_write(cdb.s);
   if ((fdcdb =open_read(cdb.s)) == -1) fatal("unable to open cdb: ", cdb.s);
   cdb_init(&c, fdcdb);
   /* create id */
@@ -165,6 +167,7 @@
     if (! found) {
       cdb_free(&c);
       close(fdcdb);
+      close(fdcdblock);
       if (verbose) info("unknown: ", id.s);
       _exit(100);
     }
@@ -186,9 +189,9 @@
     /* update cdb */
     tai_pack(tspack, &now);
     /* lock cdb */
-    if (lock_exnb(fdcdb) == -1)
+    if (lock_exnb(fdcdblock) == -1)
       if (verbose) warn("lock is busy, waiting...: ", cdbfn);
-    if (lock_ex(fdcdb) == -1) fatal("unable to lock: ", cdbfn);
+    if (lock_ex(fdcdblock) == -1) fatal("unable to lock: ", cdbfn);
     /* copy cdb cdb.tmp */
     if (seek_begin(fdcdb) == -1) fatal("unable to seek: ", cdb.s);
     if (! stralloc_copys(&tmp, cdb.s)) die_nomem();
@@ -212,6 +215,7 @@
     if (fsync(fdtmp) == -1) fatal("unable to write: ", tmp.s);
     if (close(fdtmp) == -1) fatal("unable to write: ", tmp.s);
     close(fdcdb);
+    close(fdcdblock);
     
     /* move cdb.tmp cdb */
     if (rename(tmp.s, cdb.s) == -1) fatal("unable to replace: ", cdb.s);
diff -ur qconfirm-0.14.3.orig/src/qconfirm-cdb-update.c qconfirm-0.14.3/src/qconfirm-cdb-update.c
--- qconfirm-0.14.3.orig/src/qconfirm-cdb-update.c	Sun Dec 12 09:57:17 2004
+++ qconfirm-0.14.3/src/qconfirm-cdb-update.c	Wed May 20 15:01:10 2009
@@ -61,6 +61,7 @@
 struct cdb_make c;
 struct cdb cdb;
 int fdcdb;
+int fdcdblock;
 int fdtmp;
 
 void get(buffer *b, char *buf, unsigned int l) {
@@ -141,6 +142,7 @@
     mode =st.st_mode;
 
   /* open cdb */
+  fdcdblock =open_write(cdbfn);
   fdcdb =open_read(cdbfn);
   if (fdcdb != -1) cdb_init(&cdb, fdcdb);
   /* scan dir */
@@ -190,11 +192,11 @@
   if (! stralloc_0(&tmp)) die_nomem();
   if ((fdtmp =open_trunc(tmp.s)) == -1) fatal("unable to create: ", tmp.s);
   if (cdb_make_start(&c, fdtmp) == -1) fatal("unable to create: ", tmp.s);
-  if (fdcdb != -1) {
+  if (fdcdblock != -1 && fdcdb != -1) {
     /* lock data.cdb */
-    if (lock_exnb(fdcdb) == -1)
+    if (lock_exnb(fdcdblock) == -1)
       if (verbose) warn("lock is busy, waiting...: ", cdbfn);
-    if (lock_ex(fdcdb) == -1) fatal("unable to lock: ", cdbfn);
+    if (lock_ex(fdcdblock) == -1) fatal("unable to lock: ", cdbfn);
     buffer_init(&bcdb, buffer_unixread, fdcdb, bcdbspace, sizeof bcdbspace);
     /* size of database */
     get(&bcdb, uintbuf, 4);