Skip to contentSkip to conversation

Some minor CLD test program fixes

PATCH In review [email protected] 3 participants 8 messages Nov 27, 2009 – Nov 28, 2009 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Some minor CLD test program fixes

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(-)
lib/common.c +34 -3
index 68f60f8..1b20980 100644
@@ -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)
test/it-works.c +1 -0
index bd2f965..0e932e7 100644
@@ -107,6 +107,7 @@ static int init(void)
 
 int main (int argc, char *argv[])
 {
+	g_thread_init(NULL);
 	cldc_init();
 	event_init();
 	if (init())
test/load-file-event.c +1 -0
index 1620508..23ee4e0 100644
@@ -243,6 +243,7 @@ int main(int argc, char *argv[])
 	}
 #endif
 
+	g_thread_init(NULL);
 	cldc_init();
 	event_init();
 #if 0
test/lock-file-event.c +1 -0
index c69da66..6fc4f1a 100644
@@ -247,6 +247,7 @@ static int init(void)
 
 int main (int argc, char *argv[])
 {
+	g_thread_init(NULL);
 	cldc_init();
 	event_init();
 	if (init())
test/save-file-event.c +1 -0
index 1b3418a..23f1781 100644
@@ -249,6 +249,7 @@ int main(int argc, char *argv[])
 	}
 #endif
 
+	g_thread_init(NULL);
 	cldc_init();
 	event_init();
 #if 0
Signature · 1 line
1.6.2.5
#1 lore raw
Quoted 9 lines · On 11/27/2009 06:20 PM, [email protected] wrote:

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."

Quoted 7 lines
---
  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(-)

Oh, and two procedural notes:

1) As noted in doc/contributions.txt (and like the Linux kernel), all patches must include a Signed-off-by line. This normally is the last line in the extended patch description, found in the email body.

2) Adding "From: ..." is rather pointless in this case, because the From: feature is used to denote an author other than yourself. The default is taken from the email RFC822 headers, which appear to be fine in your case. In the kernel, Andrew Morton uses this feature to indicate the original patch author, when he mails a huge swath of patches to Linus for application.

Jeff

#2 lore raw

PATCH v2 1/2 cld: in tests, call g_thread_init before cld_init

From: cmccabe <[email protected]>

---
 test/it-works.c        |    1 +
 test/load-file-event.c |    1 +
 test/lock-file-event.c |    1 +
 test/save-file-event.c |    1 +
 4 files changed, 4 insertions(+), 0 deletions(-)
test/it-works.c +1 -0
index bd2f965..0e932e7 100644
@@ -107,6 +107,7 @@ static int init(void)
 
 int main (int argc, char *argv[])
 {
+	g_thread_init(NULL);
 	cldc_init();
 	event_init();
 	if (init())
test/load-file-event.c +1 -0
index 1620508..23ee4e0 100644
@@ -243,6 +243,7 @@ int main(int argc, char *argv[])
 	}
 #endif
 
+	g_thread_init(NULL);
 	cldc_init();
 	event_init();
 #if 0
test/lock-file-event.c +1 -0
index c69da66..6fc4f1a 100644
@@ -247,6 +247,7 @@ static int init(void)
 
 int main (int argc, char *argv[])
 {
+	g_thread_init(NULL);
 	cldc_init();
 	event_init();
 	if (init())
test/save-file-event.c +1 -0
index 1b3418a..23f1781 100644
@@ -249,6 +249,7 @@ int main(int argc, char *argv[])
 	}
 #endif
 
+	g_thread_init(NULL);
 	cldc_init();
 	event_init();
 #if 0
Signature · 1 line
1.6.2.5
#3 lore raw

Re: [PATCH v2 1/2] cld: in tests, call g_thread_init before cld_init

Oops... please ignore the From: line.

C.

Quoted 63 lines · Colin McCabe

From: cmccabe <[email protected]>

---
 test/it-works.c        |    1 +
 test/load-file-event.c |    1 +
 test/lock-file-event.c |    1 +
 test/save-file-event.c |    1 +
 4 files changed, 4 insertions(+), 0 deletions(-)
test/it-works.c +1 -0
index bd2f965..0e932e7 100644
@@ -107,6 +107,7 @@ static int init(void)
 
 int main (int argc, char *argv[])
 {
+       g_thread_init(NULL);
       cldc_init();
       event_init();
       if (init())
test/load-file-event.c +1 -0
index 1620508..23ee4e0 100644
@@ -243,6 +243,7 @@ int main(int argc, char *argv[])
       }
 #endif
 
+       g_thread_init(NULL);
       cldc_init();
       event_init();
 #if 0
test/lock-file-event.c +1 -0
index c69da66..6fc4f1a 100644
@@ -247,6 +247,7 @@ static int init(void)
 
 int main (int argc, char *argv[])
 {
+       g_thread_init(NULL);
       cldc_init();
       event_init();
       if (init())
test/save-file-event.c +1 -1
index 1b3418a..23f1781 100644
@@ -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

Applied
#4 lore raw

Re: [PATCH v2 1/2] cld: in tests, call g_thread_init before cld_init

In reply to Colin McCabe
Quoted 8 lines · Colin McCabe
---
  test/it-works.c        |    1 +
  test/load-file-event.c |    1 +
  test/lock-file-event.c |    1 +
  test/save-file-event.c |    1 +
  4 files changed, 4 insertions(+), 0 deletions(-)

applied

#5 lore raw
In reply to [email protected]

lib/common.c int cld_readport(const char *fname) · line 105

Quoted 9 lines · On Fri, 27 Nov 2009 15:20:36 -0800 [email protected] wrote:

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

lib/common.c +2 -1
 	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);

I don't think it's necessary when reading from a file.

Quoted 3 lines

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."

This may be a prudent thing to do, I just forgot about the tests when I planned this.

-- Pete

#6 lore raw
In reply to [email protected]
Quoted 45 lines · On 11/27/2009 06:20 PM, [email protected] wrote:
lib/common.c +31 -1
@@ -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);

lib/common.c int cld_readport(const char *fname) · line 110

Quoted 10 lines
lib/common.c +3 -2
+	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;
 

Well, while technically correct, nobody really bothers with EINTR or loops when reading from a file, because all Unix/Linux/BSD kernels have historically given you exactly the amount you needed. Any stray EINTR is often something serious that indicates the program should abort -- Ctrl-C at terminal by an impatient user, or a kill signal from an admin.

Also, for reading whole files into memory, GLib's g_file_get_contents() already exists for that. It does full error checking.

I do wonder what cygwin does, and if a read(2) loop matters on Windows...? Eventually I would like to see these programs portable enough to run under mingw or cygwin.

Quoted 41 lines
test/it-works.c +1 -0
index bd2f965..0e932e7 100644
@@ -107,6 +107,7 @@ static int init(void)
 
  int main (int argc, char *argv[])
  {
+	g_thread_init(NULL);
  	cldc_init();
  	event_init();
  	if (init())
test/load-file-event.c +1 -0
index 1620508..23ee4e0 100644
@@ -243,6 +243,7 @@ int main(int argc, char *argv[])
  	}
  #endif
 
+	g_thread_init(NULL);
  	cldc_init();
  	event_init();
  #if 0
test/lock-file-event.c +1 -0
index c69da66..6fc4f1a 100644
@@ -247,6 +247,7 @@ static int init(void)
 
  int main (int argc, char *argv[])
  {
+	g_thread_init(NULL);
  	cldc_init();
  	event_init();
  	if (init())
test/save-file-event.c +0 -0
index 1b3418a..23f1781 100644

test/save-file-event.c int main(int argc, char *argv[]) · line 254

Quoted 6 lines
test/save-file-event.c +1 -0
@@ -249,6 +249,7 @@ int main(int argc, char *argv[])
  	}
  #endif
 
+	g_thread_init(NULL);
  	cldc_init();
  	event_init();

This is OK, but should be in a separate patch.

Similar to Linux kernel changes, logically separate changes should be in distinct, separate patches.

That allows bisection ('git bisect') to better isolate problematic changes, and helps reviewers. It also means I can immediately apply one patch, while requesting revisions of another patch. Introduces fewer "stalls" in the programming pipeline.

Jeff

#7 lore raw
Quoted 66 lines · Jeff Garzik
Quoted 48 lines · On 11/27/2009 06:20 PM, [email protected] wrote:
lib/common.c +7 -0
@@ -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.
+ */

lib/common.c +21 -0
+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.
*/

lib/common.c +0 -1
@@ -69,13 +100,13 @@ int cld_readport(const char *fname)
 
       if ((fd = open(fname, O_RDONLY)) == -1)
               return -errno;
-       rc = read(fd, buf, LEN);

lib/common.c int cld_readport(const char *fname) · line 110

Quoted 10 lines
lib/common.c +3 -2
+       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;
 

Well, while technically correct, nobody really bothers with EINTR or loops when reading from a file, because all Unix/Linux/BSD kernels have historically given you exactly the amount you needed. Any stray EINTR is often something serious that indicates the program should abort -- Ctrl-C at terminal by an impatient user, or a kill signal from an admin.

Quoted 6 lines

Also, for reading whole files into memory, GLib's g_file_get_contents() already exists for that. It does full error checking.

I do wonder what cygwin does, and if a read(2) loop matters on Windows...?
Eventually I would like to see these programs portable enough to run under
mingw or cygwin.

Good to know.

The manpages list EINTR in some surprising places... for example, close(2). Apparently Alan Cox commented that "while the standard permits" EINTR on close, "sanity suggests otherwise". :)

Quoted 52 lines
Quoted 43 lines
test/it-works.c +1 -0
index bd2f965..0e932e7 100644
@@ -107,6 +107,7 @@ static int init(void)
 
 int main (int argc, char *argv[])
 {
+       g_thread_init(NULL);
       cldc_init();
       event_init();
       if (init())
test/load-file-event.c +1 -0
index 1620508..23ee4e0 100644
@@ -243,6 +243,7 @@ int main(int argc, char *argv[])
       }
 #endif
 
+       g_thread_init(NULL);
       cldc_init();
       event_init();
 #if 0
test/lock-file-event.c +1 -0
index c69da66..6fc4f1a 100644
@@ -247,6 +247,7 @@ static int init(void)
 
 int main (int argc, char *argv[])
 {
+       g_thread_init(NULL);
       cldc_init();
       event_init();
       if (init())
test/save-file-event.c +0 -0
index 1b3418a..23f1781 100644

test/save-file-event.c int main(int argc, char *argv[]) · line 254

Quoted 6 lines
test/save-file-event.c +1 -0
@@ -249,6 +249,7 @@ int main(int argc, char *argv[])
       }
 #endif
 
+       g_thread_init(NULL);
       cldc_init();
       event_init();

This is OK, but should be in a separate patch.

Quoted 7 lines

Similar to Linux kernel changes, logically separate changes should be in distinct, separate patches.

That allows bisection ('git bisect') to better isolate problematic changes, and helps reviewers. It also means I can immediately apply one patch, while requesting revisions of another patch. Introduces fewer "stalls" in the programming pipeline.

Ah, ok

Colin

Quoted 6 lines

Jeff