diff mbox

[17/26] tools/xenstore: get_handle: Allocate struct before opening fd

Message ID 1507564648-7580-18-git-send-email-ian.jackson@eu.citrix.com (mailing list archive)
State New, archived
Headers show

Commit Message

Ian Jackson Oct. 9, 2017, 3:57 p.m. UTC
Now we can also abolish the temporary local variable "fd" and simply
use h->fd.

This ordering is necessary to be able to call
xentoolcore__register_active_handle sensibly.

Signed-off-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
---
 tools/xenstore/xs.c | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)
diff mbox

Patch

diff --git a/tools/xenstore/xs.c b/tools/xenstore/xs.c
index 65cba86..7f85bb2 100644
--- a/tools/xenstore/xs.c
+++ b/tools/xenstore/xs.c
@@ -223,27 +223,26 @@  static struct xs_handle *get_handle(const char *connect_to)
 {
 	struct stat buf;
 	struct xs_handle *h = NULL;
-	int fd = -1, saved_errno;
+	int saved_errno;
+
+	h = malloc(sizeof(*h));
+	if (h == NULL)
+		goto err;
+
+	memset(h, 0, sizeof(*h));
+	h->fd = -1;
 
 	if (stat(connect_to, &buf) != 0)
 		goto err;
 
 	if (S_ISSOCK(buf.st_mode))
-		fd = get_socket(connect_to);
+		h->fd = get_socket(connect_to);
 	else
-		fd = get_dev(connect_to);
+		h->fd = get_dev(connect_to);
 
-	if (fd == -1)
+	if (h->fd == -1)
 		goto err;
 
-	h = malloc(sizeof(*h));
-	if (h == NULL)
-		goto err;
-
-	memset(h, 0, sizeof(*h));
-
-	h->fd = fd;
-
 	INIT_LIST_HEAD(&h->reply_list);
 	INIT_LIST_HEAD(&h->watch_list);
 
@@ -267,7 +266,10 @@  static struct xs_handle *get_handle(const char *connect_to)
 err:
 	saved_errno = errno;
 
-	if (fd >= 0) close(fd);
+	if (h) {
+		if (h->fd >= 0)
+			close(h->fd);
+	}
 	free(h);
 
 	errno = saved_errno;