Skip to contentSkip to conversation

Tools: hv

PATCH In review K. Y. Srinivasan 3 participants 11 messages Sep 5, 2012 Hyper-V/Azure core and drivers

lore
Share & tools
lore
#0 lore raw

cover Tools: hv

This patchset was authored by Ben Hutchings <[email protected]>. Ben asked me to submit this patchset. This patchset cleans up the KVP daemon code and fixes some bugs.

K. Y. Srinivasan (4):
tools: hv: Fix file handle leak
tools: hv: Fix exit() error code
tools: hv: Check for read/write errors
tools: hv: Parse /etc/os-release

 tools/hv/hv_kvp_daemon.c |   95 +++++++++++++++++++++++++++++++++++++--------
 1 files changed, 78 insertions(+), 17 deletions(-)
Signature · 1 line
1.7.4.1
#1 lore raw

PATCH 1/4 tools: hv: Fix file handle leak

Match up each fopen() with an fclose().

---
 tools/hv/hv_kvp_daemon.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
tools/hv/hv_kvp_daemon.c +2 -1
index c8e1013..4514fb4 100644
@@ -160,7 +160,7 @@ static void kvp_update_file(int pool)
 				sizeof(struct kvp_record),
 				kvp_file_info[pool].num_records, filep);
 
-	fflush(filep);
+	fclose(filep);
 	kvp_release_lock(pool);
 }
 
@@ -207,6 +207,7 @@ static void kvp_update_mem_state(int pool)
 	kvp_file_info[pool].records = record;
 	kvp_file_info[pool].num_records = records_read;
 
+	fclose(filep);
 	kvp_release_lock(pool);
 }
 static int kvp_file_init(void)
Signature · 1 line
1.7.4.1
#2 lore raw

PATCH 2/4 tools: hv: Fix exit() error code

Linux native exit codes are 8-bit unsigned values. exit(-1) results in an exit code of 255, which is usually reserved for shells reporting 'command not found'. Use the portable value EXIT_FAILURE. (Not that this matters much for a daemon.)

---
 tools/hv/hv_kvp_daemon.c |   22 +++++++++++-----------
 1 files changed, 11 insertions(+), 11 deletions(-)
tools/hv/hv_kvp_daemon.c +11 -11
index 4514fb4..01b3ca5 100644
@@ -122,7 +122,7 @@ static void kvp_acquire_lock(int pool)
 
 	if (fcntl(kvp_file_info[pool].fd, F_SETLKW, &fl) == -1) {
 		syslog(LOG_ERR, "Failed to acquire the lock pool: %d", pool);
-		exit(-1);
+		exit(EXIT_FAILURE);
 	}
 }
 
@@ -134,7 +134,7 @@ static void kvp_release_lock(int pool)
 	if (fcntl(kvp_file_info[pool].fd, F_SETLK, &fl) == -1) {
 		perror("fcntl");
 		syslog(LOG_ERR, "Failed to release the lock pool: %d", pool);
-		exit(-1);
+		exit(EXIT_FAILURE);
 	}
 }
 
@@ -153,7 +153,7 @@ static void kvp_update_file(int pool)
 	if (!filep) {
 		kvp_release_lock(pool);
 		syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
-		exit(-1);
+		exit(EXIT_FAILURE);
 	}
 
 	bytes_written = fwrite(kvp_file_info[pool].records,
@@ -179,7 +179,7 @@ static void kvp_update_mem_state(int pool)
 	if (!filep) {
 		kvp_release_lock(pool);
 		syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
-		exit(-1);
+		exit(EXIT_FAILURE);
 	}
 	while (!feof(filep)) {
 		readp = &record[records_read];
@@ -196,7 +196,7 @@ static void kvp_update_mem_state(int pool)
 
 			if (record == NULL) {
 				syslog(LOG_ERR, "malloc failed");
-				exit(-1);
+				exit(EXIT_FAILURE);
 			}
 			continue;
 		}
@@ -225,7 +225,7 @@ static int kvp_file_init(void)
 	if (access("/var/opt/hyperv", F_OK)) {
 		if (mkdir("/var/opt/hyperv", S_IRUSR | S_IWUSR | S_IROTH)) {
 			syslog(LOG_ERR, " Failed to create /var/opt/hyperv");
-			exit(-1);
+			exit(EXIT_FAILURE);
 		}
 	}
 
@@ -1358,13 +1358,13 @@ int main(void)
 
 	if (kvp_file_init()) {
 		syslog(LOG_ERR, "Failed to initialize the pools");
-		exit(-1);
+		exit(EXIT_FAILURE);
 	}
 
 	fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
 	if (fd < 0) {
 		syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
-		exit(-1);
+		exit(EXIT_FAILURE);
 	}
 	addr.nl_family = AF_NETLINK;
 	addr.nl_pad = 0;
@@ -1376,7 +1376,7 @@ int main(void)
 	if (error < 0) {
 		syslog(LOG_ERR, "bind failed; error:%d", error);
 		close(fd);
-		exit(-1);
+		exit(EXIT_FAILURE);
 	}
 	sock_opt = addr.nl_groups;
 	setsockopt(fd, 270, 1, &sock_opt, sizeof(sock_opt));
@@ -1396,7 +1396,7 @@ int main(void)
 	if (len < 0) {
 		syslog(LOG_ERR, "netlink_send failed; error:%d", len);
 		close(fd);
-		exit(-1);
+		exit(EXIT_FAILURE);
 	}
 
 	pfd.fd = fd;
@@ -1608,7 +1608,7 @@ kvp_done:
 		len = netlink_send(fd, incoming_cn_msg);
 		if (len < 0) {
 			syslog(LOG_ERR, "net_link send failed; error:%d", len);
-			exit(-1);
+			exit(EXIT_FAILURE);
 		}
 	}
 
Signature · 1 line
1.7.4.1
#3 lore raw

PATCH 3/4 tools: hv: Check for read/write errors

In reply to K. Y. Srinivasan

hv_kvp_daemon currently does not check whether fread() or fwrite() succeed. Add the necessary checks. Also, remove the incorrect use of feof() before fread().

---
 tools/hv/hv_kvp_daemon.c |   22 +++++++++++++++++++---
 1 files changed, 19 insertions(+), 3 deletions(-)
tools/hv/hv_kvp_daemon.c +19 -3
index 01b3ca5..3922abc 100644
@@ -160,7 +160,12 @@ static void kvp_update_file(int pool)
 				sizeof(struct kvp_record),
 				kvp_file_info[pool].num_records, filep);
 
-	fclose(filep);
+	if (ferror(filep) || fclose(filep)) {
+		kvp_release_lock(pool);
+		syslog(LOG_ERR, "Failed to write file, pool: %d", pool);
+		exit(EXIT_FAILURE);
+	}
+
 	kvp_release_lock(pool);
 }
 
@@ -181,12 +186,17 @@ static void kvp_update_mem_state(int pool)
 		syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
 		exit(EXIT_FAILURE);
 	}
-	while (!feof(filep)) {
+	for (;;) {
 		readp = &record[records_read];
 		records_read += fread(readp, sizeof(struct kvp_record),
 					ENTRIES_PER_BLOCK * num_blocks,
 					filep);
 
+		if (ferror(filep)) {
+			syslog(LOG_ERR, "Failed to read file, pool: %d", pool);
+			exit(EXIT_FAILURE);
+		}
+
 		if (!feof(filep)) {
 			/*
 			 * We have more data to read.
@@ -249,12 +259,18 @@ static int kvp_file_init(void)
 			fclose(filep);
 			return 1;
 		}
-		while (!feof(filep)) {
+		for (;;) {
 			readp = &record[records_read];
 			records_read += fread(readp, sizeof(struct kvp_record),
 					ENTRIES_PER_BLOCK,
 					filep);
 
+			if (ferror(filep)) {
+				syslog(LOG_ERR, "Failed to read file, pool: %d",
+				       i);
+				exit(EXIT_FAILURE);
+			}
+
 			if (!feof(filep)) {
 				/*
 				 * We have more data to read.
Signature · 1 line
1.7.4.1
#4 lore raw

PATCH 4/4 tools: hv: Parse /etc/os-release

In reply to K. Y. Srinivasan

There is a new convention, used by systemd and supported by most distributions, to put basic OS release information in /etc/os-release.

---
 tools/hv/hv_kvp_daemon.c |   50 +++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 47 insertions(+), 3 deletions(-)
tools/hv/hv_kvp_daemon.c +47 -3
index 3922abc..77a8413 100644
@@ -465,15 +465,59 @@ void kvp_get_os_info(void)
 	if (p)
 		*p = '\0';
 
+	/*
+	 * Parse the /etc/os-release file if present:
+	 * http://www.freedesktop.org/software/systemd/man/os-release.html
+	 */
+	file = fopen("/etc/os-release", "r");
+	if (file != NULL) {
+		while (fgets(buf, sizeof(buf), file)) {
+			char *value, *q;
+
+			/* Ignore comments */
+			if (buf[0] == '#')
+				continue;
+
+			/* Split into name=value */
+			p = strchr(buf, '=');
+			if (!p)
+				continue;
+			*p++ = 0;
+
+			/* Remove quotes and newline; un-escape */
+			value = p;
+			q = p;
+			while (*p) {
+				if (*p == '\\') {
+					++p;
+					if (!*p)
+						break;
+					*q++ = *p++;
+				} else if (*p == '\'' || *p == '"' ||
+					   *p == '\n') {
+					++p;
+				} else {
+					*q++ = *p++;
+				}
+			}
+			*q = 0;
+
+			if (!strcmp(buf, "NAME"))
+				os_name = strdup(value);
+			else if (!strcmp(buf, "VERSION_ID"))
+				os_major = strdup(value);
+		}
+		fclose(file);
+		return;
+	}
+
+	/* Fallback for older RH/SUSE releases */
 	file = fopen("/etc/SuSE-release", "r");
 	if (file != NULL)
 		goto kvp_osinfo_found;
 	file  = fopen("/etc/redhat-release", "r");
 	if (file != NULL)
 		goto kvp_osinfo_found;
-	/*
-	 * Add code for other supported platforms.
-	 */
 
 	/*
 	 * We don't have information about the os.
Signature · 1 line
1.7.4.1
#5 lore raw

Re: [PATCH 4/4] tools: hv: Parse /etc/os-release

tools/hv/hv_kvp_daemon.c void kvp_get_os_info(void) · line 508

Quoted 4 lines
tools/hv/hv_kvp_daemon.c +4 -0
+			if (!strcmp(buf, "NAME"))
+				os_name = strdup(value);
+			else if (!strcmp(buf, "VERSION_ID"))
+				os_major = strdup(value);

strdup can fail. The case where it leaks because NAME= occurs twice is harmless enough but the strdup ought to be checked or add an x_strdup() helper.

Alan

#6 lore raw

RE: [PATCH 4/4] tools: hv: Parse /etc/os-release

Quoted 11 lines

-----Original Message-----
From: Alan Cox [mailto:[email protected]]
Sent: Wednesday, September 05, 2012 12:58 PM
To: KY Srinivasan
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 4/4] tools: hv: Parse /etc/os-release

Quoted 8 lines

tools/hv/hv_kvp_daemon.c void kvp_get_os_info(void) · line 508

Quoted 4 lines
tools/hv/hv_kvp_daemon.c +4 -0
+			if (!strcmp(buf, "NAME"))
+				os_name = strdup(value);
+			else if (!strcmp(buf, "VERSION_ID"))
+				os_major = strdup(value);

strdup can fail. The case where it leaks because NAME= occurs twice is harmless enough but the strdup ought to be checked or add an x_strdup() helper.

I will fix this and resend this patch.

Thank you,

K. Y

#7 lore raw
In reply to K. Y. Srinivasan
Quoted 10 lines · K. Y. Srinivasan

This patchset was authored by Ben Hutchings <[email protected]>. Ben asked me to submit this patchset. This patchset cleans up the KVP daemon code and fixes some bugs.

K. Y. Srinivasan (4):
tools: hv: Fix file handle leak
tools: hv: Fix exit() error code
tools: hv: Check for read/write errors
tools: hv: Parse /etc/os-release

If Ben wrote these, why are you showing that you wrote them?

Please ALWAYS keep the proper authorship information on ALL patches. To not do so is rude and problematic on on so many different levels.

Fix this up and resend please.

greg k-h

#8 lore raw
Quoted 17 lines

-----Original Message-----
From: Greg KH [mailto:[email protected]]
Sent: Wednesday, September 05, 2012 3:11 PM
To: KY Srinivasan
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 0/4] Tools: hv

Quoted 5 lines · K. Y. Srinivasan

This patchset was authored by Ben Hutchings <[email protected]>. Ben asked me to submit this patchset. This patchset cleans up the KVP daemon code and fixes some bugs.

Quoted 7 lines
Quoted 5 lines

K. Y. Srinivasan (4):
tools: hv: Fix file handle leak
tools: hv: Fix exit() error code
tools: hv: Check for read/write errors
tools: hv: Parse /etc/os-release

If Ben wrote these, why are you showing that you wrote them?

Ben asked me to apply these patches on the tip (of my tree) and send the patches out and that is what I did. I recreated these patches after applying to my tree and that is what I have sent out. How could I have made the authorship more clear (other than noting so in the comment which I have done).

Regards,

K. Y

#9 lore raw
Quoted 12 lines · KY Srinivasan
Quoted 12 lines · K. Y. Srinivasan
Quoted 10 lines

This patchset was authored by Ben Hutchings <[email protected]>. Ben asked me to submit this patchset. This patchset cleans up the KVP daemon code and fixes some bugs.

K. Y. Srinivasan (4):
tools: hv: Fix file handle leak
tools: hv: Fix exit() error code
tools: hv: Check for read/write errors
tools: hv: Parse /etc/os-release

Quoted 6 lines
Quoted 1 line

If Ben wrote these, why are you showing that you wrote them?

Ben asked me to apply these patches on the tip (of my tree) and send the patches out and that is what I did. I recreated these patches after applying to my tree and that is what I have sent out. How could I have made the authorship more clear (other than noting so in the comment which I have done).

You could have actually _kept_ the proper authorship information so that git shows who wrote the patch. As your shortlog shows above, it looks like you wrote the patches, not Ben, which is not acceptable.

As to how to actually do that, well, I'll leave that as an exercise for the reader (hint, we document how to do this quite well...)

greg k-h

#10 lore raw
Quoted 26 lines

-----Original Message-----
From: Greg KH [mailto:[email protected]]
Sent: Wednesday, September 05, 2012 3:39 PM
To: KY Srinivasan
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 0/4] Tools: hv

Quoted 14 lines · KY Srinivasan
Quoted 13 lines · K. Y. Srinivasan
Quoted 10 lines

This patchset was authored by Ben Hutchings <[email protected]>. Ben asked me to submit this patchset. This patchset cleans up the KVP daemon code and fixes some bugs.

K. Y. Srinivasan (4):
tools: hv: Fix file handle leak
tools: hv: Fix exit() error code
tools: hv: Check for read/write errors
tools: hv: Parse /etc/os-release

If Ben wrote these, why are you showing that you wrote them?

Quoted 8 lines
Quoted 4 lines

Ben asked me to apply these patches on the tip (of my tree) and send the patches out and that is what I did. I recreated these patches after applying to my tree and that is what I have sent out. How could I have made the authorship more clear (other than noting so in the comment which I have done).

You could have actually _kept_ the proper authorship information so that git shows who wrote the patch. As your shortlog shows above, it looks like you wrote the patches, not Ben, which is not acceptable.

Obviously, I did not know how I could have done this. I will figure out the correct way of doing this and resubmit these patches.

Thanks,

K. Y