From: cmccabe <[email protected]>

Hi all! Here is a small patch to the CLD test programs...

When doing a raw read(2) in cld_readport(), resume after EINTR. Also resume if
we read less than the requested amount.

In the test programs, call g_thread_init() before cldc_init(). cldc_init has a
stern warning to "call cldc_init after g_thread_init, if present."
---
 lib/common.c           |   37 ++++++++++++++++++++++++++++++++++---
 test/it-works.c        |    1 +
 test/load-file-event.c |    1 +
 test/lock-file-event.c |    1 +
 test/save-file-event.c |    1 +
 5 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/lib/common.c b/lib/common.c
index 68f60f8..1b20980 100644
--- a/lib/common.c
+++ b/lib/common.c
@@ -56,6 +56,37 @@ const char *cld_errstr(enum cle_err_codes ecode)
        return cld_errlist[ecode];
 }
 
+/** Read from a file descriptor, resuming after interruptions.
+ *
+ * @param fd           The file descriptor
+ * @param buf          Output buffer
+ * @param len          How many bytes to read
+ *
+ * @return             Negative error code on error, or the number of bytes
+ *                     that were read. This will only be less than the
+ *                     requested length if the file is too short.
+ */
+int safe_read(int fd, void *buf, int len)
+{
+       int rem = len;
+       while (rem) {
+               int res = read(fd, buf, rem);
+               if (res == 0) {
+                       return len - rem;
+               }
+               else if (res < 0) {
+                       int err = errno;
+                       if (err != EINTR)
+                               return -err;
+               }
+               else {
+                       rem -= res;
+                       buf += res;
+               }
+       }
+       return len;
+}
+
 /*
  * Read a port number from a port file, return the value or negative error.
  */
@@ -69,13 +100,13 @@ int cld_readport(const char *fname)
 
        if ((fd = open(fname, O_RDONLY)) == -1)
                return -errno;
-       rc = read(fd, buf, LEN);
+       memset(buf, 0, sizeof(buf));
+       rc = safe_read(fd, buf, LEN);
        close(fd);
        if (rc < 0)
-               return -errno;
+               return rc;
        if (rc == 0)
                return -EPIPE;
-       buf[rc] = 0;
 
        port = strtol(buf, NULL, 10);
        if (port <= 0 || port >= 65636)
diff --git a/test/it-works.c b/test/it-works.c
index bd2f965..0e932e7 100644
--- a/test/it-works.c
+++ b/test/it-works.c
@@ -107,6 +107,7 @@ static int init(void)
 
 int main (int argc, char *argv[])
 {
+       g_thread_init(NULL);
        cldc_init();
        event_init();
        if (init())
diff --git a/test/load-file-event.c b/test/load-file-event.c
index 1620508..23ee4e0 100644
--- a/test/load-file-event.c
+++ b/test/load-file-event.c
@@ -243,6 +243,7 @@ int main(int argc, char *argv[])
        }
 #endif
 
+       g_thread_init(NULL);
        cldc_init();
        event_init();
 #if 0
diff --git a/test/lock-file-event.c b/test/lock-file-event.c
index c69da66..6fc4f1a 100644
--- a/test/lock-file-event.c
+++ b/test/lock-file-event.c
@@ -247,6 +247,7 @@ static int init(void)
 
 int main (int argc, char *argv[])
 {
+       g_thread_init(NULL);
        cldc_init();
        event_init();
        if (init())
diff --git a/test/save-file-event.c b/test/save-file-event.c
index 1b3418a..23f1781 100644
--- a/test/save-file-event.c
+++ b/test/save-file-event.c
@@ -249,6 +249,7 @@ int main(int argc, char *argv[])
        }
 #endif
 
+       g_thread_init(NULL);
        cldc_init();
        event_init();
 #if 0
-- 
1.6.2.5

--
To unsubscribe from this list: send the line "unsubscribe hail-devel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to