Skip to contentSkip to conversation

cld: read the cld.port file using g_file_get_contents

PATCH Applied Colin McCabe 3 participants 6 messages Nov 28, 2009 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v2 2/2 cld: read the cld.port file using g_file_get_contents

---
 lib/common.c |   29 +++++++++++++++--------------
 1 files changed, 15 insertions(+), 14 deletions(-)
lib/common.c +15 -14
index 68f60f8..db20e2a 100644
@@ -1,4 +1,5 @@
 
+#include <stdio.h>
 #include <stdlib.h>
 #include <fcntl.h>
 #include <unistd.h>
@@ -61,26 +62,26 @@ const char *cld_errstr(enum cle_err_codes ecode)
  */
 int cld_readport(const char *fname)
 {
-	enum { LEN = 11 };
-	char buf[LEN+1];
 	long port;
-	int fd;
-	int rc;
+	gchar *buf;
+	GError *err = NULL;
+	gsize len;
 
-	if ((fd = open(fname, O_RDONLY)) == -1)
-		return -errno;
-	rc = read(fd, buf, LEN);
-	close(fd);
-	if (rc < 0)
-		return -errno;
-	if (rc == 0)
-		return -EPIPE;
-	buf[rc] = 0;
+	if (!g_file_get_contents(fname, &buf, &len, &err)) {
+		fprintf(stderr, "Unable to read port file: %s\n",
+			 err->message);
+		g_error_free(err);
+		return -EIO;
+	}
 
+	if (len == 0) {
+		g_free(buf);
+		return -EPIPE;
+	}
 	port = strtol(buf, NULL, 10);
+	g_free(buf);
 	if (port <= 0 || port >= 65636)
 		return -EDOM;
 
 	return (int)port;
 }
-
Signature · 1 line
1.6.2.5
#1 lore raw
Quoted 41 lines · Colin McCabe
---
  lib/common.c |   29 +++++++++++++++--------------
  1 files changed, 15 insertions(+), 14 deletions(-)
lib/common.c +7 -13
index 68f60f8..db20e2a 100644
@@ -1,4 +1,5 @@
 
+#include<stdio.h>
  #include<stdlib.h>
  #include<fcntl.h>
  #include<unistd.h>
@@ -61,26 +62,26 @@ const char *cld_errstr(enum cle_err_codes ecode)
   */
  int cld_readport(const char *fname)
  {
-	enum { LEN = 11 };
-	char buf[LEN+1];
  	long port;
-	int fd;
-	int rc;
+	gchar *buf;
+	GError *err = NULL;
+	gsize len;
 
-	if ((fd = open(fname, O_RDONLY)) == -1)
-		return -errno;
-	rc = read(fd, buf, LEN);
-	close(fd);
-	if (rc<  0)
-		return -errno;
-	if (rc == 0)
-		return -EPIPE;
-	buf[rc] = 0;
+	if (!g_file_get_contents(fname,&buf,&len,&err)) {
+		fprintf(stderr, "Unable to read port file: %s\n",
+			 err->message);

lib/common.c const char *cld_errstr(enum cle_err_codes ecode) · line 82

Quoted 10 lines
lib/common.c +8 -0
+		g_error_free(err);
+		return -EIO;
+	}
 
+	if (len == 0) {
+		g_free(buf);
+		return -EPIPE;
+	}
  	port = strtol(buf, NULL, 10);
+	g_free(buf);

Two added wrinkles, unfortunately:

1) 'buf' is no longer nul-terminated, which means strtol() has become a buffer overrun.

2) this is used in daemons, so we cannot make assumptions about error output. The best you can do is return a useful numeric error code, because fprintf(stderr,...) does nothing in a daemon that redirected stderr to null.

#3 lore raw
In reply to Colin McCabe

Review

Quoted 7 lines · On Fri, 27 Nov 2009 18:23:49 -0800 Colin McCabe <[email protected]> wrote:
(quoted diff) +6 -0
+	if (!g_file_get_contents(fname, &buf, &len, &err)) {
+		fprintf(stderr, "Unable to read port file: %s\n",
+			 err->message);
+		g_error_free(err);
+		return -EIO;
+	}

We should not print errors from library routines, because it leads to double-printing (so even if you swap the fprintf to applog, it's still not a good thing to do).

Otherwise seems fine at first blush.

BTW, speaking of double-something, here's a funny buglet:

server/server.c +1 -1
@@ -722,7 +741,7 @@ static int net_open_any(void)
 
 	if (cld_srv.port_file) {
 		char portstr[7];
-		snprintf(portstr, sizeof(portstr), "%u\n", port);
+		snprintf(portstr, sizeof(portstr), "%u", port);
 		return net_write_port(cld_srv.port_file, portstr);
 	}
 	return 0;

Our PID files contain a linefeed, as a courtesy to those who examine them from a shell. But CLD's contains two.

-- Pete

#4 lore raw
Quoted 32 lines · Pete Zaitcev

Review

Quoted 7 lines · On Fri, 27 Nov 2009 18:23:49 -0800 Colin McCabe<[email protected]> wrote:
(quoted diff) +6 -0
+	if (!g_file_get_contents(fname,&buf,&len,&err)) {
+		fprintf(stderr, "Unable to read port file: %s\n",
+			 err->message);
+		g_error_free(err);
+		return -EIO;
+	}

We should not print errors from library routines, because it leads to double-printing (so even if you swap the fprintf to applog, it's still not a good thing to do).

Otherwise seems fine at first blush.

BTW, speaking of double-something, here's a funny buglet:

server/server.c +1 -1
@@ -722,7 +741,7 @@ static int net_open_any(void)
 
  	if (cld_srv.port_file) {
  		char portstr[7];
-		snprintf(portstr, sizeof(portstr), "%u\n", port);
+		snprintf(portstr, sizeof(portstr), "%u", port);
  		return net_write_port(cld_srv.port_file, portstr);
  	}
  	return 0;
Quoted 3 lines

Our PID files contain a linefeed, as a courtesy to those who examine them from a shell. But CLD's contains two.

Agreed... wanna wrap that in a signed-off-by? :)

Jeff

#5 lore raw
Quoted 11 lines · On Sat, 28 Nov 2009 16:23:29 -0500 Jeff Garzik <[email protected]> wrote:
Quoted 11 lines

+++ cld-tip/server/server.c

server/server.c +1 -1
@@ -722,7 +741,7 @@ static int net_open_any(void)
 
  	if (cld_srv.port_file) {
  		char portstr[7];
-		snprintf(portstr, sizeof(portstr), "%u\n", port);
+		snprintf(portstr, sizeof(portstr), "%u", port);
  		return net_write_port(cld_srv.port_file, portstr);
  	}
  	return 0;
Quoted 4 lines
Quoted 2 lines

Our PID files contain a linefeed, as a courtesy to those who examine them from a shell. But CLD's contains two.

Agreed... wanna wrap that in a signed-off-by? :)

Sure, I can do it. Although, it was intended as a hint for Colin about the kind of bugs we have scattered all over (probably). This one was harmless.

-- pete